Skip to content

Commit

Permalink
Better error reporting for implicit pragma
Browse files Browse the repository at this point in the history
  • Loading branch information
flyx committed Dec 9, 2023
1 parent da5d37e commit 4e84186
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions yaml/native.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1434,29 +1434,30 @@ proc isImplicitVariantObject(t: typedesc): bool {.compileTime.} =
else:
return false

proc canBeImplicit(t: typedesc): bool {.compileTime.} =
proc canBeImplicit(t: typedesc): string {.compileTime.} =
## returns empty string if type can be implicit, else the reason why it can't
let tDesc = getType(t)
if tDesc.kind != nnkObjectTy: return false
if tDesc[2].len != 1: return false
if tDesc[2][0].kind != nnkRecCase: return false
if tDesc.kind != nnkObjectTy: return "type is not an object"
if tDesc[2].len != 1 or tDesc[2][0].kind != nnkRecCase:
return "type doesn't exclusively contain record case"
var foundEmptyBranch = false
for i in 1.. tDesc[2][0].len - 1:
case tDesc[2][0][i][1].recListlen # branch contents
of 0:
if foundEmptyBranch: return false
if foundEmptyBranch: return "record case has more than one empty branch"
else: foundEmptyBranch = true
of 1: discard
else: return false
return true
else: return "record case contains more than one field"

proc constructChild*[T](
ctx : var ConstructionContext,
result: var T,
) =
let item = ctx.input.peek()
when isImplicitVariantObject(T):
when not canBeImplicit(T):
{. fatal: "This type cannot be marked as implicit" .}
const checkRes = canBeImplicit(T)
when not len(checkRes) > 0:
{. fatal: "cannot be marked as implicit: " & checkRes .}
var possibleTags = newSeq[Tag]()
case item.kind
of yamlScalar:
Expand Down

0 comments on commit 4e84186

Please sign in to comment.