Files
Nim/tests/vm/tcaseobj.nim
metagn 75b512bc6a prevent codegen of inactive case fields in VM object constructor nodes (#24442)
fixes #17571

Objects in the VM are represented as object constructor nodes that
contain every single field, including ones in different case branches.
This is so that every field has a unique invariant index in the object
constructor that can be written to and read from. However when
converting this node back into semantic code, fields from inactive case
branches can remain in the constructor which causes bad codegen,
generating assignments to fields from other case branches.

To fix this, fields from inactive branches are now detected in
`semmacrosanity.annotateType` (called in `fixupTypeAfterEval`) and
marked to prevent the codegen of their assignments. In #24441 these
fields were excluded from the resulting node, but this causes issues
when the node is directly supposed to go back into the VM, for example
as `const` values. I don't know if this is the only case where this
happens, so I wasn't sure about how to keep that implementation working.
2024-11-16 10:43:58 +01:00

20 lines
321 B
Nim

# issue #17571
import std/[macros, objectdollar]
type
MyEnum = enum
F, S, T
Foo = object
case o: MyEnum
of F:
f: string
of S:
s: string
of T:
t: string
let val = static(Foo(o: F, f: "foo")).f
doAssert val == "foo"
doAssert $static(Foo(o: F, f: "foo")) == $Foo(o: F, f: "foo")