-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
VM: const case object gets some fields zeroed out at runtime #13081
Labels
VM
see also `const` label
Comments
I believe it is related issue |
@cooldome I just tried your PR, it doesn't fix the above bug (note that this issue isn't using |
Retested on latest dev and it works for me. |
timotheecour
added a commit
to timotheecour/Nim
that referenced
this issue
Oct 8, 2020
block: # bug #13081
type Kind = enum
k0, k1, k2, k3
type Foo = object
x0: float
case kind: Kind
of k0: discard
of k1: x1: int
of k2: x2: string
of k3: x3: string
const j1 = Foo(x0: 1.2, kind: k1, x1: 12)
const j2 = Foo(x0: 1.3, kind: k2, x2: "abc")
const j3 = Foo(x0: 1.3, kind: k3, x3: "abc2")
static:
doAssert $j1 == "(x0: 1.2, kind: k1, x1: 12)"
doAssert $j2 == """(x0: 1.3, kind: k2, x2: "abc")"""
doAssert $j3 == """(x0: 1.3, kind: k3, x3: "abc2")"""
doAssert $j1 == "(x0: 1.2, kind: k1, x1: 12)"
doAssert $j2 == """(x0: 1.3, kind: k2, x2: "abc")"""
doAssert $j3 == """(x0: 1.3, kind: k3, x3: "abc2")"""
when true:
# BUG: this doesn't work yet
# Error: unhandled exception: 'sons' is not accessible using discriminant 'kind' of type 'TNode' [FieldDefect]
discard j1.x1
static:
# ditto
discard j1.x1 note, this also affects #15528
EDIT: => #15532 |
mildred
pushed a commit
to mildred/Nim
that referenced
this issue
Jan 11, 2021
* close nim-lang#13081 * fixup
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
when a const case object is used at runtime, some fields get re-initialized to 0. But not all, see below
Example
Current Output
Expected Output
workaround
use
const x2 = j2.x2
, the x2 will get correct value at RT since it's not a case objectAdditional Information
devel 5e9ebe9
related: False positive detection of conflicting branches of object variant #11862
related: const array indexing zeroes data in object variants at runtime #8015 (comment)
however IMO compiler SHOULD support case objects, instead of outright disabling them. It makes more code usable at CT, even if above mentioned caveats; the workaround mentioned above works; whereas if we outright disabled const case objects, we'd have no easy equivalent of this (not without some contorsions at least)
so let's fix it instead of disabling it :)
If there's one thing we may disable until this is fixed, it's the runtime access to a const case object field
The text was updated successfully, but these errors were encountered: