don't update const symbol on const section re-sems (#22609)

fixes #19849
This commit is contained in:
metagn
2023-09-01 09:59:48 +03:00
committed by GitHub
parent affd3f7858
commit 53d9fb259f
2 changed files with 20 additions and 5 deletions

View File

@@ -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

10
tests/vm/tconstresem.nim Normal file
View File

@@ -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