diff options
author | ringabout <43030857+ringabout@users.noreply.github.com> | 2022-11-03 15:46:16 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-03 15:46:16 +0800 |
commit | c4e5dab4197ce57af03c5eaa6117b738279fa537 (patch) | |
tree | a9df84b105c6eede8ce20bf12215f7471b5096f4 | |
parent | 6b1e353aa109756cefc961c8a91d1df49912ab51 (diff) | |
download | Nim-c4e5dab4197ce57af03c5eaa6117b738279fa537.tar.gz |
fixes #20740; fixes pre-existing field visibility issues and removes `efSkipFieldVisibilityCheck` (#20741)
fixes #20740 pre-existing field visibility and refactoring
-rw-r--r-- | compiler/ast.nim | 2 | ||||
-rw-r--r-- | compiler/semdata.nim | 3 | ||||
-rw-r--r-- | compiler/semobjconstr.nim | 8 | ||||
-rw-r--r-- | compiler/vmgen.nim | 4 | ||||
-rw-r--r-- | tests/objects/tobject_default_value.nim | 22 |
5 files changed, 28 insertions, 11 deletions
diff --git a/compiler/ast.nim b/compiler/ast.nim index 228f7381c..22ad1aa27 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -1075,7 +1075,7 @@ const nfIsRef, nfIsPtr, nfPreventCg, nfLL, nfFromTemplate, nfDefaultRefsParam, nfExecuteOnReload, nfLastRead, - nfFirstWrite} + nfFirstWrite, nfUseDefaultField} namePos* = 0 patternPos* = 1 # empty except for term rewriting macros genericParamsPos* = 2 diff --git a/compiler/semdata.nim b/compiler/semdata.nim index 2442030f5..ec8224531 100644 --- a/compiler/semdata.nim +++ b/compiler/semdata.nim @@ -69,8 +69,7 @@ type efWantStmt, efAllowStmt, efDetermineType, efExplain, efWantValue, efOperand, efNoSemCheck, efNoEvaluateGeneric, efInCall, efFromHlo, efNoSem2Check, - efNoUndeclared, efIsDotCall, efCannotBeDotCall, - efSkipFieldVisibilityCheck + efNoUndeclared, efIsDotCall, efCannotBeDotCall # Use this if undeclared identifiers should not raise an error during # overload resolution. efNoDiagnostics diff --git a/compiler/semobjconstr.nim b/compiler/semobjconstr.nim index e51684913..1463ba833 100644 --- a/compiler/semobjconstr.nim +++ b/compiler/semobjconstr.nim @@ -76,8 +76,7 @@ proc semConstrField(c: PContext, flags: TExprFlags, let assignment = locateFieldInInitExpr(c, field, initExpr) if assignment != nil: if nfSem in assignment.flags: return assignment[1] - if nfUseDefaultField in assignment[1].flags or - efSkipFieldVisibilityCheck in flags: + if nfUseDefaultField in assignment[1].flags: discard elif not fieldVisible(c, field): localError(c.config, initExpr.info, @@ -416,10 +415,7 @@ proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags; expectedType: PType # 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, defaults) = if nfUseDefaultField in n.flags: - semConstructTypeAux(c, constrCtx, flags + {efSkipFieldVisibilityCheck}) - else: - semConstructTypeAux(c, constrCtx, flags) + let (initResult, defaults) = semConstructTypeAux(c, constrCtx, flags) result[0].sons.add defaults var hasError = false # needed to split error detect/report for better msgs diff --git a/compiler/vmgen.nim b/compiler/vmgen.nim index dbe053030..9680fe9ba 100644 --- a/compiler/vmgen.nim +++ b/compiler/vmgen.nim @@ -1810,7 +1810,9 @@ proc getNullValueAux(t: PType; obj: PNode, result: PNode; conf: ConfigRef; currP of nkSym: let field = newNodeI(nkExprColonExpr, result.info) field.add(obj) - field.add(getNullValue(obj.sym.typ, result.info, conf)) + let value = getNullValue(obj.sym.typ, result.info, conf) + value.flags.incl nfUseDefaultField + field.add(value) result.add field doAssert obj.sym.position == currPosition inc currPosition diff --git a/tests/objects/tobject_default_value.nim b/tests/objects/tobject_default_value.nim index 975a1e146..85be43daa 100644 --- a/tests/objects/tobject_default_value.nim +++ b/tests/objects/tobject_default_value.nim @@ -3,7 +3,7 @@ discard """ targets: "c cpp js" """ -import std/[times, tables] +import std/[times, tables, macros] type Guess = object @@ -457,5 +457,25 @@ template main {.dirty.} = var d = default(Bar) doAssert d.t == 0 + block: # bug #20740 + block: + proc foo(x: static DateTime = Datetime()) = + discard + + foo() + + block: + macro foo(x: static DateTime) = + discard x + + macro foo2: untyped = + var x = DateTime() + + result = quote do: + foo(`x`) + + foo2() + + static: main() main() |