diff options
author | ringabout <43030857+ringabout@users.noreply.github.com> | 2023-01-18 18:52:18 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-18 11:52:18 +0100 |
commit | fc35f83eee55610af3931f95771b6d1bce1fc845 (patch) | |
tree | ff4314e15cfb5405160c45a0afa67b9033de0be2 /compiler/semobjconstr.nim | |
parent | c4035d7f7c424caa81ee449d2032ecd8e8967ebd (diff) | |
download | Nim-fc35f83eee55610af3931f95771b6d1bce1fc845.tar.gz |
fixes #21260; add check for illegal recursion for defaults (#21270)
* fixes #21260; add check for illegal recursion for defaults * fixes differently
Diffstat (limited to 'compiler/semobjconstr.nim')
-rw-r--r-- | compiler/semobjconstr.nim | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/compiler/semobjconstr.nim b/compiler/semobjconstr.nim index 206602677..15e53a639 100644 --- a/compiler/semobjconstr.nim +++ b/compiler/semobjconstr.nim @@ -326,10 +326,13 @@ proc semConstructFields(c: PContext, n: PNode, constrCtx: var ObjConstrContext, result.status = initUnknown result.defaults.add newTree(nkExprColonExpr, n, field.ast) else: - let defaultExpr = defaultNodeField(c, n) - if defaultExpr != nil: - result.status = initUnknown - result.defaults.add newTree(nkExprColonExpr, n, defaultExpr) + if efWantNoDefaults notin flags: # cannot compute defaults at the typeRightPass + let defaultExpr = defaultNodeField(c, n) + if defaultExpr != nil: + result.status = initUnknown + result.defaults.add newTree(nkExprColonExpr, n, defaultExpr) + else: + result.status = initNone else: result.status = initNone else: @@ -364,7 +367,7 @@ proc initConstrContext(t: PType, initExpr: PNode): ObjConstrContext = proc computeRequiresInit(c: PContext, t: PType): bool = assert t.kind == tyObject var constrCtx = initConstrContext(t, newNode(nkObjConstr)) - let initResult = semConstructTypeAux(c, constrCtx, {}) + let initResult = semConstructTypeAux(c, constrCtx, {efWantNoDefaults}) constrCtx.missingFields.len > 0 proc defaultConstructionError(c: PContext, t: PType, info: TLineInfo) = @@ -374,7 +377,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, {}) + let initResult = semConstructTypeAux(c, constrCtx, {efWantNoDefaults}) 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)]) |