Skip to content

Commit 6cbb732

Browse files
LemonBoyandrewrk
authored andcommitted
Extern unions should not trigger active field check
Fixes #3378
1 parent 4e81df1 commit 6cbb732

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/ir.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17951,7 +17951,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
1795117951
union_val->special = ConstValSpecialStatic;
1795217952
bigint_init_bigint(&union_val->data.x_union.tag, &field->enum_field->value);
1795317953
union_val->data.x_union.payload = payload_val;
17954-
} else {
17954+
} else if (bare_type->data.unionation.layout != ContainerLayoutExtern) {
1795517955
TypeUnionField *actual_field = find_union_field_by_tag(bare_type, &union_val->data.x_union.tag);
1795617956
if (actual_field == nullptr)
1795717957
zig_unreachable();

test/stage1/behavior/union.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,3 +511,13 @@ test "union with comptime_int tag" {
511511
};
512512
comptime expect(@TagType(@TagType(Union)) == comptime_int);
513513
}
514+
515+
test "extern union doesn't trigger field check at comptime" {
516+
const U = extern union {
517+
x: u32,
518+
y: u8,
519+
};
520+
521+
const x = U{ .x = 0x55AAAA55 };
522+
comptime expect(x.y == 0x55);
523+
}

0 commit comments

Comments
 (0)