summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorringabout <43030857+ringabout@users.noreply.github.com>2022-11-17 09:38:50 +0800
committerGitHub <noreply@github.com>2022-11-17 09:38:50 +0800
commit1707bc4a992bc966a7439facb9ee819023c22f77 (patch)
tree34faa9fea4f0734e63225bfc0f496aca8df94d7c
parentcdbf5b469949366d56f1eb2dbc7721eaf0cbc218 (diff)
downloadNim-1707bc4a992bc966a7439facb9ee819023c22f77.tar.gz
fixes #20856; store defaults directly (#20859)
* fixes #20856; store defaults directly

* fixes

* fixes

* check

* fixes
-rw-r--r--compiler/semobjconstr.nim6
-rw-r--r--compiler/transf.nim7
-rw-r--r--tests/objects/tobjects_various.nim13
3 files changed, 16 insertions, 10 deletions
diff --git a/compiler/semobjconstr.nim b/compiler/semobjconstr.nim
index 7f76dbee9..206602677 100644
--- a/compiler/semobjconstr.nim
+++ b/compiler/semobjconstr.nim
@@ -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
 
diff --git a/compiler/transf.nim b/compiler/transf.nim
index ff480dcf4..4cf048ba6 100644
--- a/compiler/transf.nim
+++ b/compiler/transf.nim
@@ -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:
diff --git a/tests/objects/tobjects_various.nim b/tests/objects/tobjects_various.nim
index 8ec090f42..55db9312e 100644
--- a/tests/objects/tobjects_various.nim
+++ b/tests/objects/tobjects_various.nim
@@ -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())