29120, "bradcray", "Assigning a 'new R()' expression to a union field calls the initializer twice", "2026-07-13T22:20:43Z"
When assigning new R() to a field of a union of type record R when it is not already active, we seem to call R's initializer twice rather than once. If the field is already active, we only call it once. This seems like overkill, and like a sign we're doing something we shouldn't be.
Summary of Problem
Description:
When assigning new R() to a field of a union of type record R when it is not already active, we seem to call R's initializer twice rather than once. If the field is already active, we only call it once. This seems like overkill, and like a sign we're doing something we shouldn't be. For example,
record R {
proc init() {
writeln("In R.init");
}
}
union U {
var x: R;
var y: int;
}
var u: U;
u.x = new R();
prints the initializer message twice rather than just once, as I'd expect:
In R.init
In R.init
I stumbled into this while implementing #28984 and thought I'd broken something, but it seems to be a pre-existing condition.