summary refs log tree commit diff stats
diff options
context:
space:
mode:
authormetagn <metagngn@gmail.com>2023-09-01 09:59:48 +0300
committerGitHub <noreply@github.com>2023-09-01 08:59:48 +0200
commit53d9fb259fc7d36660be20be0a199625ef126376 (patch)
tree8668e07ec4f3a7329fd1b192fa014c4a0ef72a5a
parentaffd3f78587f1b18a8b4bc2fc51967cd55cb7531 (diff)
downloadNim-53d9fb259fc7d36660be20be0a199625ef126376.tar.gz
don't update const symbol on const section re-sems (#22609)
fixes #19849
-rw-r--r--compiler/semstmts.nim15
-rw-r--r--tests/vm/tconstresem.nim10
2 files changed, 20 insertions, 5 deletions
diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim
index ee1b56fed..08d6d44e6 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 000000000..4526cb891
--- /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