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
commit 1707bc4a99
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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 = proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags; expectedType: PType = nil): PNode =
var t = semTypeNode(c, n[0], nil) var t = semTypeNode(c, n[0], nil)
result = newNodeIT(nkObjConstr, n.info, t) 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 0..<n.len:
for i in 1..<n.len:
result.add n[i] result.add n[i]
if t == nil: if t == nil:
@ -421,7 +420,6 @@ proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags; expectedType: PType
# branches will be reported as an error): # branches will be reported as an error):
var constrCtx = initConstrContext(t, result) var constrCtx = initConstrContext(t, result)
let (initResult, defaults) = 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 var hasError = false # needed to split error detect/report for better msgs
# It's possible that the object was not fully initialized while # 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 hasError = true
break break
result.sons.add defaults
if initResult == initFull: if initResult == initFull:
incl result.flags, nfAllFieldsSet incl result.flags, nfAllFieldsSet

View file

@ -1066,13 +1066,6 @@ proc transform(c: PTransf, n: PNode): PNode =
result = n result = n
of nkExceptBranch: of nkExceptBranch:
result = transformExceptBranch(c, n) 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: of nkCheckedFieldExpr:
result = transformSons(c, n) result = transformSons(c, n)
if result[0].kind != nkDotExpr: if result[0].kind != nkDotExpr:

View file

@ -105,3 +105,16 @@ block t7244:
proc test(foo: var Foo) = discard proc test(foo: var Foo) = discard
proc test(bar: var Bar) = test(Foo(bar)) 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())