Skip to content

Add support for compiler-defined union initializers#28984

Open
bradcray wants to merge 9 commits into
chapel-lang:mainfrom
bradcray:union-one-field-inits
Open

Add support for compiler-defined union initializers#28984
bradcray wants to merge 9 commits into
chapel-lang:mainfrom
bradcray:union-one-field-inits

Conversation

@bradcray

@bradcray bradcray commented Jun 17, 2026

Copy link
Copy Markdown
Member

Where compiler-generated initializers for classes and records have an argument per field, for unions, it makes more sense to have an initializer per field since only one field can be active at a time. Prior to this PR, we only had a compiler-generated 0-argument initializer, which is also useful for putting the union into its uninitialized state. This PR adds an initializer per field, accepting a single argument of that field type. A new test is added to check that the initializers work; and an old test is updated to reflect the new overloads.

This was fairly simple due to the fact that unions currently don't support type-less fields, fields with initializers, type fields, or param fields. If/when those things change, it could become more complex. One such change that feels reasonable would be for a single union field to support an initializer, at which point the 0-argument initializer we currently generate could be replaced by the initializer for that field taking a default argument. Future work

TODO:

  • open an issue for the one-field initializer idea above
  • update docs
  • hmm… test/classes/diten/constructUnion.chpl fails unless it contains a new u() call; and similarly my test fails if I comment out that line…

Resolves #4972 (once item 3 above is done)

Where compiler-generated initializers for classes and records have an
argument per field, for unions, it makes more sense to have an
initializer per field since only one field can be active at a time.
Prior to this PR, we only had a compiler-generated 0-argument
initializer, which is also useful for putting the union into its
uninitialized state.  This PR adds an initializer per field, accepting
a single argument of that field type.  A new test is added to check
that the initializers work; and an old test is updated to reflect the
new overloads.

This was fairly simple due to the fact that unions currently don't
support type-less fields, fields with initializers, type fields, or
param fields.  If/when those things change, it could become more
complex.  One such change that feels reasonable would be for a single
union field to support an initializer, at which point the 0-argument
initializer we currently generate could be replaced by the initializer
for that field taking a default argument.  Future work

TODO:
- [ ] open an issue for the one-field initializer idea above

---
Signed-off-by: Brad Chamberlain <bradcray@users.noreply.github.com>
---
Signed-off-by: Brad Chamberlain <bradcray@users.noreply.github.com>
bradcray added 7 commits June 17, 2026 20:04
---
Signed-off-by: Brad Chamberlain <bradcray@users.noreply.github.com>
…should've

I was accidentally using the `this` argument from the 0-argument union
initializer for _part of_ the 1-argument union initializers, which
caused things to blow up in a big way when the 0-argument initializer
wasn't called.  Basically, I believe it would get dead-code
eliminated, taking the _this with it, and breaking other routines.  It
was one of the weirdest behaviors I've ever seen, though, where
valgrind didn't complain and we tried calling qualType() on a former
ArgSymbol that the compiler considered to now be a BaseAST, so we got
a C++ "pure virtual method called" error.

---
Signed-off-by: Brad Chamberlain <bradcray@users.noreply.github.com>
… post-parse checks

Made an INT_FATAL into a USR_FATAL; added new tests locking in the
errors (seemingly we didn't have any

Mystery: Why do the post-process checks not catch typeless multi-var decls

---
Signed-off-by: Brad Chamberlain <bradcray@users.noreply.github.com>
---
Signed-off-by: Brad Chamberlain <bradcray@users.noreply.github.com>
Rationale: DEFAULT here apparently means "can be called with 0
arguments" rather than "is created by the compiler as a default since
the user didn't supply any."

---
Signed-off-by: Brad Chamberlain <bradcray@users.noreply.github.com>
---
Signed-off-by: Brad Chamberlain <bradcray@users.noreply.github.com>
…ed union inits

This fixes union-init3.chpl

---
Signed-off-by: Brad Chamberlain <bradcray@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Setting a union field from the union's constructor leaves the union undefined.

1 participant