summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorringabout <43030857+ringabout@users.noreply.github.com>2024-02-20 14:31:58 +0800
committerGitHub <noreply@github.com>2024-02-20 07:31:58 +0100
commit39f2df19723d98eaa006cfd0ef13ec93d9f8c1c5 (patch)
tree45fd925e67856eb8ebd1cee126e5ce210dcd2801
parentd6f0f1aca75b97186baf2d785e30cc426482870d (diff)
downloadNim-39f2df19723d98eaa006cfd0ef13ec93d9f8c1c5.tar.gz
fixes #23295; don't expand constants for complex structures (#23297)
fixes #23295
-rw-r--r--compiler/trees.nim3
-rw-r--r--tests/objvariant/tconstobjvariant.nim18
2 files changed, 19 insertions, 2 deletions
diff --git a/compiler/trees.nim b/compiler/trees.nim
index 99b6a9d01..41b54eb09 100644
--- a/compiler/trees.nim
+++ b/compiler/trees.nim
@@ -226,8 +226,7 @@ proc stupidStmtListExpr*(n: PNode): bool =
 proc dontInlineConstant*(orig, cnst: PNode): bool {.inline.} =
   # symbols that expand to a complex constant (array, etc.) should not be
   # inlined, unless it's the empty array:
-  result = orig.kind != cnst.kind and
-           cnst.kind in {nkCurly, nkPar, nkTupleConstr, nkBracket, nkObjConstr} and
+  result = cnst.kind in {nkCurly, nkPar, nkTupleConstr, nkBracket, nkObjConstr} and
            cnst.len > ord(cnst.kind == nkObjConstr)
 
 proc isRunnableExamples*(n: PNode): bool =
diff --git a/tests/objvariant/tconstobjvariant.nim b/tests/objvariant/tconstobjvariant.nim
new file mode 100644
index 000000000..45a647707
--- /dev/null
+++ b/tests/objvariant/tconstobjvariant.nim
@@ -0,0 +1,18 @@
+# This is a sample code, the first echo statement prints out the error
+type
+  A = object
+    case w: uint8
+    of 1:
+      n: int
+    else:
+      other: string
+
+const
+  a = A(w: 1, n: 5)
+
+proc foo =
+
+  let c = [a]
+  doAssert c[0].n == 5
+
+foo()
\ No newline at end of file
id='n181' href='#n181'>181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210