Skip to content

Commit

Permalink
fixes #17437 - crash where error reporting > 1 (#17547)
Browse files Browse the repository at this point in the history
* fixes #17437

* Fix bug reference comment

Co-authored-by: Timothee Cour <[email protected]>

* [skip ci] describe why we have hasError

Co-authored-by: Timothee Cour <[email protected]>
  • Loading branch information
saem and timotheecour authored Mar 29, 2021
1 parent 8b26b3a commit c6dc9c0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
16 changes: 12 additions & 4 deletions compiler/semobjconstr.nim
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags): PNode =

if t == nil:
localError(c.config, n.info, errGenerated, "object constructor needs an object type")
return
return errorNode(c, result)

t = skipTypes(t, {tyGenericInst, tyAlias, tySink, tyOwned})
if t.kind == tyRef:
Expand All @@ -392,17 +392,19 @@ proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags): PNode =
result.typ.flags.incl tfHasOwned
if t.kind != tyObject:
localError(c.config, n.info, errGenerated, "object constructor needs an object type")
return
return errorNode(c, result)

# Check if the object is fully initialized by recursively testing each
# field (if this is a case object, initialized fields in two different
# branches will be reported as an error):
var constrCtx = initConstrContext(t, result)
let initResult = semConstructTypeAux(c, constrCtx, flags)
var hasError = false # needed to split error detect/report for better msgs

# It's possible that the object was not fully initialized while
# specifying a .requiresInit. pragma:
if constrCtx.missingFields.len > 0:
hasError = true
localError(c.config, result.info,
"The $1 type requires the following fields to be initialized: $2.",
[t.sym.name.s, listSymbolNames(constrCtx.missingFields)])
Expand All @@ -415,6 +417,7 @@ proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags): PNode =
if nfSem notin field.flags:
if field.kind != nkExprColonExpr:
invalidObjConstr(c, field)
hasError = true
continue
let id = considerQuotedIdent(c, field[0])
# This node was not processed. There are two possible reasons:
Expand All @@ -423,10 +426,15 @@ proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags): PNode =
let prevId = considerQuotedIdent(c, result[j][0])
if prevId.id == id.id:
localError(c.config, field.info, errFieldInitTwice % id.s)
return
hasError = true
break
# 2) No such field exists in the constructed type
localError(c.config, field.info, errUndeclaredFieldX % id.s)
return
hasError = true
break

if initResult == initFull:
incl result.flags, nfAllFieldsSet

# wrap in an error see #17437
if hasError: result = errorNode(c, result)
22 changes: 22 additions & 0 deletions tests/objects/t17437.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
discard """
cmd: "nim check $file"
errormsg: ""
nimout: '''
t17437.nim(20, 16) Error: undeclared identifier: 'x'
t17437.nim(20, 16) Error: expression 'x' has no type (or is ambiguous)
t17437.nim(20, 19) Error: incorrect object construction syntax
t17437.nim(20, 19) Error: incorrect object construction syntax
t17437.nim(20, 12) Error: expression '' has no type (or is ambiguous)
'''
"""

# bug #17437 invalid object construction should result in error

type
V = ref object
x, y: int

proc m =
var v = V(x: x, y)

m()

0 comments on commit c6dc9c0

Please sign in to comment.