diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index ee1b56fed4cb..08d6d44e6e8c 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -867,9 +867,13 @@ proc semConst(c: PContext, n: PNode): PNode = styleCheckDef(c, v) onDef(a[j].info, v) - setVarType(c, v, typ) - when false: - v.ast = def # no need to copy + var fillSymbol = true + if v.typ != nil: + # symbol already has type and probably value + # don't mutate + fillSymbol = false + else: + setVarType(c, v, typ) b = newNodeI(nkConstDef, a.info) if importantComments(c.config): b.comment = a.comment # postfix not generated here (to generate, get rid of it in transf) @@ -882,8 +886,9 @@ proc semConst(c: PContext, n: PNode): PNode = b.add newSymNode(v) b.add a[1] b.add copyTree(def) - v.ast = b - addToVarSection(c, result, n, b) + if fillSymbol: + v.ast = b + addToVarSection(c, result, n, b) dec c.inStaticContext include semfields diff --git a/tests/vm/tconstresem.nim b/tests/vm/tconstresem.nim new file mode 100644 index 000000000000..4526cb891d56 --- /dev/null +++ b/tests/vm/tconstresem.nim @@ -0,0 +1,10 @@ +block: # issue #19849 + type + Vec2[T] = object + x, y: T + Vec2i = Vec2[int] + template getX(p: Vec2i): int = p.x + let x = getX: + const t = Vec2i(x: 1, y: 2) + t + doAssert x == 1