tagged unions

This commit is contained in:
2026-06-28 22:54:20 +02:00
parent f328c44154
commit 981ccb047a
7 changed files with 396 additions and 14 deletions
+19
View File
@@ -1048,6 +1048,25 @@ validate_type_nodes :: proc(checker: ^Checker) {
)
}
}
// A tagged union (`item.child` set) must tag with an enum, and each variant
// must name a member of it. The synthesized `union(enum)` tag satisfies this
// by construction; the check guards the explicit `union(Enum)` form.
if item.kind == .Union && types.is_valid(item.child) {
if !types.is_enum(item.child, &checker.module.types) {
source.add(checker.diagnostics, source.Span{}, "a tagged union's tag must be an enum")
} else {
for field in types.fields_for(&checker.module.types, id) {
if _, ok := find_enum_member(checker, item.child, symbol.Id(field.name)); !ok {
source.addf(
checker.diagnostics,
source.Span{},
"union variant '%s' is not a member of the tag enum",
symbol_text(checker, symbol.Id(field.name)),
)
}
}
}
}
}
if item.kind == .Function {
if !item.c_abi {