Branch: refs/heads/main
Revision: c366cd519fc45f1c19e56ce2fc9ebee6444bde5e
Author: jabraham17
Link: Expand and improve support for union by jabraham17 · Pull Request #28601 · chapel-lang/chapel · GitHub
Log Message:
Expand and improve support for union (#28601)
Expands the support for the union type in Chapel
A key improvement is that users can use unions natively in a select
statement.
union U {
var x: int;
var y: real;
}
var u: U;
select u {
when U.x {
writeln("x is ", u.x);
}
when U.y {
writeln("y is ", u.y);
}
}
Or users can use functions to do dispatch
u.visit(proc(x: int) { writeln("x is ", u.x); },
proc(y: real) { writeln("y is ", u.y); });
Resolves determining the active field of a union · Issue #11452 · chapel-lang/chapel · GitHub
In this PR
- rewrites some compiler primitives to be 0 based instead of 1 based
- affects reflection primitives for accessing fields of aggregates and
union specific primitives - Adds an
any uniongeneric type to the compiler - Adds
union.getActiveIndexto get the active element of the union - Adds
UnionType.fieldNameto get that fields index - Adds special pattern matching allowing users to write
select uand
whenUnionType.fieldand have everything just work, see
test/types/unions/selectPatterns.chpl. - Adds
union.visit, allowing a user to pass FCF or functors to be
called for the active union element - Adds
union.getFieldIndexto get the index of a given field name (not
user facing)- this is just a convenience wrapper over reflection functions
- Adds
union.thisto access a union element by index (not user facing) - better compiler errors for unions
- better documentation for unions
- better error messages when accessing a union incorrectly
- union checking is turned off with
--no-checks - removes the deprecated
Reflection.numFields
- paratest with/without gasnet
This PR is not meant to make unions perfect, rather it incrementally
improves unions. Future work
- resolving the issues with unions and initializers:
multiple problems with initializers for unions · Issue #15189 · chapel-lang/chapel · GitHub and
Setting a union field from the union's constructor leaves the union undefined. · Issue #4972 · chapel-lang/chapel · GitHub - union comparisons: Add == and != operators for unions · Issue #14139 · chapel-lang/chapel · GitHub
- unions vs variants? variant types · Issue #11453 · chapel-lang/chapel · GitHub
- union bugs: Can't assign from one field of a union to another · Issue #14287 · chapel-lang/chapel · GitHub
- Should unions throw or halt? · Issue #28624 · chapel-lang/chapel · GitHub
- Create a pattern matching syntax for unions · Issue #28625 · chapel-lang/chapel · GitHub
[Reviewed by @DanilaFe]
Diff:
M compiler/AST/AggregateType.cpp
M compiler/AST/build.cpp
M compiler/AST/type.cpp
M compiler/include/driver.h
M compiler/include/type.h
M compiler/main/driver.cpp
M compiler/passes/ResolveScope.cpp
M compiler/passes/buildDefaultFunctions.cpp
M compiler/passes/convert-typed-uast.cpp
M compiler/resolution/expandVarArgs.cpp
M compiler/resolution/functionResolution.cpp
M compiler/resolution/preFold.cpp
M doc/rst/conf.py
M doc/rst/language/spec/unions.rst
M doc/rst/technotes/globalvars.rst
M frontend/include/chpl/framework/all-global-strings.h
M frontend/include/chpl/uast/compiler-globals-list.h
M frontend/lib/parsing/bison-chpl-lib.cpp
M frontend/lib/parsing/chpl.ypp
M frontend/lib/resolution/prims.cpp
M frontend/lib/uast/post-parse-checks.cpp
M frontend/test/parsing/testParseAggregate.cpp
M frontend/test/parsing/testParseAttributes.cpp
M frontend/test/resolution/testReflection.cpp
M man/chpl.rst
M modules/Makefile
M modules/internal/ChapelBase.chpl
M modules/internal/ChapelHashing.chpl
M modules/internal/ChapelStandard.chpl
A modules/internal/ChapelUnion.chpl
M modules/internal/fixInternalDocs.sh
M modules/standard/ChapelIO.chpl
M modules/standard/HaltWrappers.chpl
M modules/standard/Reflection.chpl
M test/classes/deitz/unions/union2.good
M test/classes/deitz/unions/union4.good
M test/classes/deitz/unions/union_method3.good
M test/compflags/bradc/help/userhelp.good
M test/compflags/ferguson/print-module-resolution.good
D test/deprecated/ReflectionNumFields.chpl
D test/deprecated/ReflectionNumFields.good
M test/io/serializers/deserialize-from.chpl
M test/param/ferguson/getfieldref.chpl
A test/types/unions/PREDIFF
A test/types/unions/accessByIndex-bad1.chpl
A test/types/unions/accessByIndex-bad1.good
A test/types/unions/accessByIndex-bad2.chpl
A test/types/unions/accessByIndex-bad2.good
A test/types/unions/accessByIndex-bad3.chpl
A test/types/unions/accessByIndex-bad3.good
A test/types/unions/accessByIndex-bad4.chpl
A test/types/unions/accessByIndex-bad4.good
A test/types/unions/accessByIndex.chpl
A test/types/unions/accessByIndex.good
A test/types/unions/bad-access1.chpl
A test/types/unions/bad-access1.good
A test/types/unions/bad-access2.chpl
A test/types/unions/bad-access2.good
A test/types/unions/bad-union-field.chpl
A test/types/unions/bad-union-field.good
A test/types/unions/bad-union-init-expr.chpl
A test/types/unions/bad-union-init-expr.good
A test/types/unions/bad-union-kw-syntax.chpl
A test/types/unions/bad-union-kw-syntax.good
A test/types/unions/badTypeAssign.chpl
A test/types/unions/badTypeAssign.good
A test/types/unions/badVisit1.chpl
A test/types/unions/badVisit1.good
A test/types/unions/badVisit2.chpl
A test/types/unions/badVisit2.good
A test/types/unions/badVisit3.chpl
A test/types/unions/badVisit3.good
A test/types/unions/badVisit4.chpl
A test/types/unions/badVisit4.good
A test/types/unions/badVisit5.chpl
A test/types/unions/badVisit5.good
A test/types/unions/one-elm.chpl
A test/types/unions/one-elm.good
A test/types/unions/selectPatterns.chpl
A test/types/unions/selectPatterns.good
A test/types/unions/statefulVisit.chpl
A test/types/unions/statefulVisit.good
M test/types/unions/union-in-list.chpl
M test/types/unions/union-readWrite.chpl
A test/types/unions/union-type-constraint.chpl
A test/types/unions/union-type-constraint.good
A test/types/unions/union-type-constraint.h
A test/types/unions/unionNesting.chpl
A test/types/unions/unionNesting.good
M test/users/ferguson/fielderator-ref.chpl
M test/users/ferguson/fielderator.chpl
A test/users/lydia/fieldNumToName.prediff
M test/users/lydia/fieldNumToNameError.chpl
M test/users/lydia/fieldNumToNameError.good
M util/chpl-completion.bash
https://github.com/chapel-lang/chapel/pull/28601.diff