diff options
author | ringabout <43030857+ringabout@users.noreply.github.com> | 2024-09-26 12:28:40 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-26 06:28:40 +0200 |
commit | 6d6489a9ab2f83f17f5fdebe3372e86a610d7a3c (patch) | |
tree | 0e0eed8bf08a76dd9c1be5a01649a24734e47a8c | |
parent | 3b85c1a2e9b913a11fc7cd202b7e0a617d782cf4 (diff) | |
download | Nim-6d6489a9ab2f83f17f5fdebe3372e86a610d7a3c.tar.gz |
fixes requiresInit for var statements without initialization (#24177)
ref https://forum.nim-lang.org/t/12530
-rw-r--r-- | compiler/semdata.nim | 1 | ||||
-rw-r--r-- | compiler/semobjconstr.nim | 11 | ||||
-rw-r--r-- | tests/objects/trequireinit.nim | 10 |
3 files changed, 18 insertions, 4 deletions
diff --git a/compiler/semdata.nim b/compiler/semdata.nim index 8c8e2c2dc..ca35ddc53 100644 --- a/compiler/semdata.nim +++ b/compiler/semdata.nim @@ -75,6 +75,7 @@ type # overload resolution. efTypeAllowed # typeAllowed will be called after efWantNoDefaults + efIgnoreDefaults # var statements without initialization efAllowSymChoice # symchoice node should not be resolved TExprFlags* = set[TExprFlag] diff --git a/compiler/semobjconstr.nim b/compiler/semobjconstr.nim index 96b2d702d..048053115 100644 --- a/compiler/semobjconstr.nim +++ b/compiler/semobjconstr.nim @@ -387,10 +387,13 @@ proc semConstructFields(c: PContext, n: PNode, constrCtx: var ObjConstrContext, if e != nil: result.status = initFull elif field.ast != nil: - result.status = initUnknown - result.defaults.add newTree(nkExprColonExpr, n, field.ast) + if efIgnoreDefaults notin flags: + result.status = initUnknown + result.defaults.add newTree(nkExprColonExpr, n, field.ast) + else: + result.status = initNone else: - if efWantNoDefaults notin flags: # cannot compute defaults at the typeRightPass + if {efWantNoDefaults, efIgnoreDefaults} * flags == {}: # cannot compute defaults at the typeRightPass let defaultExpr = defaultNodeField(c, n, constrCtx.checkDefault) if defaultExpr != nil: result.status = initUnknown @@ -443,7 +446,7 @@ proc defaultConstructionError(c: PContext, t: PType, info: TLineInfo) = assert objType != nil if objType.kind == tyObject: var constrCtx = initConstrContext(objType, newNodeI(nkObjConstr, info)) - let initResult = semConstructTypeAux(c, constrCtx, {efWantNoDefaults}) + let initResult = semConstructTypeAux(c, constrCtx, {efIgnoreDefaults}) if constrCtx.missingFields.len > 0: localError(c.config, info, "The $1 type doesn't have a default value. The following fields must be initialized: $2." % [typeToString(t), listSymbolNames(constrCtx.missingFields)]) diff --git a/tests/objects/trequireinit.nim b/tests/objects/trequireinit.nim new file mode 100644 index 000000000..202667b02 --- /dev/null +++ b/tests/objects/trequireinit.nim @@ -0,0 +1,10 @@ +discard """ + errormsg: "The MPlayerObj type doesn't have a default value. The following fields must be initialized: foo." +""" + +type + MPlayerObj* {.requiresInit.} = object + foo: range[5..10] = 5 + +var a: MPlayerObj +echo a.foo \ No newline at end of file |