authorgravatar for ian@ianjohnson.devIan Johnson <ian@ianjohnson.dev> 2025-03-03 21:57:52-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-03-08 14:29:20-05:00
log0bce4a4e05ef377b82667997870e73a11dc98c88
treed3c72bd340e8df7bd03b41613c6416275f50f769
parent61c588d726f85551ad36c32fd2917087d3a4763b

Sema: handle generated tag enums in union field order check

Fixes #23059 The "note: enum field here" now references the field in the base union type rather than crashing.

2 files changed, 28 insertions(+), 1 deletions(-)

src/Sema.zig+1-1
...@@ -36719,7 +36719,7 @@ fn unionFields(...@@ -36719,7 +36719,7 @@ fn unionFields(
36719 if (enum_index != field_i) {36719 if (enum_index != field_i) {
36720 const msg = msg: {36720 const msg = msg: {
36721 const enum_field_src: LazySrcLoc = .{36721 const enum_field_src: LazySrcLoc = .{
36722 .base_node_inst = tag_info.zir_index.unwrap().?,36722 .base_node_inst = Type.fromInterned(tag_ty).typeDeclInstAllowGeneratedTag(zcu).?,
36723 .offset = .{ .container_field_name = enum_index },36723 .offset = .{ .container_field_name = enum_index },
36724 };36724 };
36725 const msg = try sema.errMsg(name_src, "union field '{}' ordered differently than corresponding enum field", .{36725 const msg = try sema.errMsg(name_src, "union field '{}' ordered differently than corresponding enum field", .{
test/cases/compile_errors/union_field_ordered_differently_than_enum.zig created+27
...@@ -0,0 +1,27 @@
1const Tag = enum { a, b };
2
3const Union = union(Tag) {
4 b,
5 a,
6};
7
8const BaseUnion = union(enum) {
9 a,
10 b,
11};
12
13const GeneratedTagUnion = union(@typeInfo(BaseUnion).@"union".tag_type.?) {
14 b,
15 a,
16};
17
18export fn entry() usize {
19 return @sizeOf(Union) + @sizeOf(GeneratedTagUnion);
20}
21
22// error
23//
24// :4:5: error: union field 'b' ordered differently than corresponding enum field
25// :1:23: note: enum field here
26// :14:5: error: union field 'b' ordered differently than corresponding enum field
27// :10:5: note: enum field here