Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When
unionDeclInner
builds the trailing data for its UnionDecl payload, the bit to indicate that a field has a type is set ifmember.ast.type_expr != 0
. However, theRef
to the type is only actually included in the field data if additionallynode_tags[member.ast.type_expr] != .@"anytype"
. Anything walking over the trailing field data for a union with ananytype
field will start reading garbage after this point. For example, runningast-check -t
onpanics with an index of out bounds on my machine.
These changes revise #9235, which fixed an
unreachable
statement being hit in astgen'sexpr
. The problem there was that an.anytype
field type node was not handled specially and was being passed totypeExpr
, which callsexpr
, which explicitly says a tag of.anytype
is unreachable. Instead, anytype union fields should, like anytype struct fields, write an explicit.none
forfield_type
in the UnionDecl payload. Omitting the field type is already used with tagged unions to indicate an inferred void type. This distinction is also missing fromsemaUnionFields
, which currently treats a missing type and a.none
type as both indicating a void field type, so I followed the example ofsemaStructFields
and set theType
of an anytype field to.noreturn
.The other fixes for
unionDeclInner
are just error reporting. First, when error is reported for a field assignment in a union without an explicit integer tag type, this is done by checking whetherarg_node
is unset. This only catches the cases ofunion
andunion(enum)
and not the case of a union with an explicit tag type. So, for example,passes ast-check. Second, untagged unions cannot omit field types, and astgen can easily check this but isn't currently. I also added tests for these errors to
test/stage2/cbe.zig
. That seems to be where tests for errors that can be caught this early in compilation are being put, but I'm not sure.