Skip to content

Commit

Permalink
don't update const symbol on const section re-sems (#22609)
Browse files Browse the repository at this point in the history
fixes #19849
  • Loading branch information
metagn committed Sep 1, 2023
1 parent affd3f7 commit 53d9fb2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
15 changes: 10 additions & 5 deletions compiler/semstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions tests/vm/tconstresem.nim
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 53d9fb2

Please sign in to comment.