fixes #20856; store defaults directly (#20859)

* fixes #20856; store defaults directly

* fixes

* fixes

* check

* fixes
This commit is contained in:
ringabout
2022-11-17 09:38:50 +08:00
committed by GitHub
parent cdbf5b4699
commit 1707bc4a99
3 changed files with 16 additions and 10 deletions

View File

@@ -387,8 +387,7 @@ proc defaultConstructionError(c: PContext, t: PType, info: TLineInfo) =
proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags; expectedType: PType = nil): PNode =
var t = semTypeNode(c, n[0], nil)
result = newNodeIT(nkObjConstr, n.info, t)
result.add newNodeIT(nkType, n.info, t) #This will contain the default values to be added in transf
for i in 1..<n.len:
for i in 0..<n.len:
result.add n[i]
if t == nil:
@@ -421,7 +420,6 @@ proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags; expectedType: PType
# branches will be reported as an error):
var constrCtx = initConstrContext(t, result)
let (initResult, defaults) = semConstructTypeAux(c, constrCtx, flags)
result[0].sons.add defaults
var hasError = false # needed to split error detect/report for better msgs
# It's possible that the object was not fully initialized while
@@ -457,6 +455,8 @@ proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags; expectedType: PType
hasError = true
break
result.sons.add defaults
if initResult == initFull:
incl result.flags, nfAllFieldsSet

View File

@@ -1066,13 +1066,6 @@ proc transform(c: PTransf, n: PNode): PNode =
result = n
of nkExceptBranch:
result = transformExceptBranch(c, n)
of nkObjConstr:
result = n
if result.typ.skipTypes({tyGenericInst, tyAlias, tySink}).kind == tyObject or
result.typ.skipTypes({tyGenericInst, tyAlias, tySink}).kind == tyRef and result.typ.skipTypes({tyGenericInst, tyAlias, tySink})[0].kind == tyObject:
result.sons.add result[0].sons
result[0] = newNodeIT(nkType, result.info, result.typ)
result = transformSons(c, result)
of nkCheckedFieldExpr:
result = transformSons(c, n)
if result[0].kind != nkDotExpr:

View File

@@ -105,3 +105,16 @@ block t7244:
proc test(foo: var Foo) = discard
proc test(bar: var Bar) = test(Foo(bar))
import std/macros
#bug #20856
macro ensureImplWorksOnConstr(t: typed): untyped =
expectKind(t, nnkObjConstr)
doAssert t[0].getTypeInst.getImpl.repr == "A = object"
doAssert t[0].getImpl.repr == "A = object"
type A = object
ensureImplWorksOnConstr(A())