diff options
-rw-r--r-- | compiler/ccgexprs.nim | 2 | ||||
-rw-r--r-- | compiler/trees.nim | 4 | ||||
-rw-r--r-- | tests/ccgbugs/tconstobj.nim | 16 |
3 files changed, 20 insertions, 2 deletions
diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index e05828f8f..547d0d376 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -1718,7 +1718,7 @@ proc genMagicExpr(p: BProc, e: PNode, d: var TLoc, op: TMagic) = proc genConstExpr(p: BProc, n: PNode): PRope proc handleConstExpr(p: BProc, n: PNode, d: var TLoc): bool = - if (nfAllConst in n.flags) and (d.k == locNone) and (sonsLen(n) > 0): + if nfAllConst in n.flags and d.k == locNone and n.len > 0 and n.isDeepConstExpr: var t = getUniqueType(n.typ) discard getTypeDesc(p.module, t) # so that any fields are initialized var id = nodeTableTestOrSet(p.module.dataCache, n, gBackendId) diff --git a/compiler/trees.nim b/compiler/trees.nim index b1edd21f3..86a1139a0 100644 --- a/compiler/trees.nim +++ b/compiler/trees.nim @@ -117,7 +117,9 @@ proc isDeepConstExpr*(n: PNode): bool = of nkCurly, nkBracket, nkPar, nkObjConstr, nkClosure: for i in 0 .. <n.len: if not isDeepConstExpr(n.sons[i]): return false - result = true + # XXX once constant objects are supported by the codegen this needs to be + # weakened: + result = n.typ.isNil or n.typ.skipTypes({tyGenericInst, tyDistinct}).kind != tyObject else: discard proc flattenTreeAux(d, a: PNode, op: TMagic) = diff --git a/tests/ccgbugs/tconstobj.nim b/tests/ccgbugs/tconstobj.nim new file mode 100644 index 000000000..98f441e83 --- /dev/null +++ b/tests/ccgbugs/tconstobj.nim @@ -0,0 +1,16 @@ +discard """ + output: '''(FirstName: James, LastName: Franco)''' +""" + +# bug #1547 +import tables + +type Person* = object + FirstName*: string + LastName*: string + +let people = { + "001": Person(FirstName: "James", LastName: "Franco") +}.toTable() + +echo people["001"] |