Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #17437 - crash where error reporting > 1 #17547

Merged
merged 3 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
saem marked this conversation as resolved.
Show resolved Hide resolved
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()