Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
Newruntime: assignment to discriminant field in case objects not supported #11205
type MyEnum = enum A, B, C MyCaseObject = object case kind: MyEnum of A: iseq: seq[int] of B: fseq: seq[float] of C: str: string var x = MyCaseObject() x.iseq.add 1 x.kind = B x.fseq.add -3.0
The issue is more fundamental then it seems. Descriminator field assignments are quite widespread even in stdlib hence can't be forbidden. Such assignment needs destructor call or even more accurately subdestructor call. Subdestructor will destroy only branches of the union part of the object.
Looks like liftdestructor needs to generate extra destructor for the case field in the object. These destructors are field sym bound and not type bound which makes it difficult. Inject destructor needs to call them on descriminator assignments.
The good news once it is done, existing issues with object variants will be resolved.
@Araq: Possibly you have better ideas?
The problem is that example above comes from stdlib. Check for example https://github.com/nim-lang/Nim/blob/devel/lib/pure/json.nim: descriminator assignment happens 20+ times.
Manual doesn't mention actively used loophole: descriminant value of zero is treated differently.
In my opinion that code example is supposed to crash, even on the old runtime. The only question is how it should crash.
@cooldome Yes the descriminator is assigned many times, but the difference is, it isn't assigned after it's members have been assigned. I don't even know what the code above is supposed to do. Should it reinterpet
Btw I see a clear conflict here with discriminant objects and objects that are not initialized with 0 data.
Yeah, the serialisation problem is a big issue which I have no good solution for.