mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
stage1: Make sure union(enum(T)) is valid
The T type should be wide enough to fit values in the 0...num field range. Closes #6988
This commit is contained in:
parent
f85d719952
commit
78840c4ab2
2 changed files with 47 additions and 0 deletions
|
|
@ -3178,6 +3178,22 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
|
||||||
union_type->data.unionation.resolve_status = ResolveStatusInvalid;
|
union_type->data.unionation.resolve_status = ResolveStatusInvalid;
|
||||||
return ErrorSemanticAnalyzeFail;
|
return ErrorSemanticAnalyzeFail;
|
||||||
}
|
}
|
||||||
|
if (tag_int_type->id == ZigTypeIdInt) {
|
||||||
|
BigInt bi;
|
||||||
|
bigint_init_unsigned(&bi, field_count - 1);
|
||||||
|
if (!bigint_fits_in_bits(&bi,
|
||||||
|
tag_int_type->data.integral.bit_count,
|
||||||
|
tag_int_type->data.integral.is_signed))
|
||||||
|
{
|
||||||
|
ErrorMsg *msg = add_node_error(g, enum_type_node,
|
||||||
|
buf_sprintf("specified integer tag type cannot represent every field"));
|
||||||
|
add_error_note(g, msg, enum_type_node,
|
||||||
|
buf_sprintf("type %s cannot fit values in range 0...%" PRIu32,
|
||||||
|
buf_ptr(&tag_int_type->name), field_count - 1));
|
||||||
|
union_type->data.unionation.resolve_status = ResolveStatusInvalid;
|
||||||
|
return ErrorSemanticAnalyzeFail;
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
tag_int_type = get_smallest_unsigned_int_type(g, field_count - 1);
|
tag_int_type = get_smallest_unsigned_int_type(g, field_count - 1);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,37 @@ const tests = @import("tests.zig");
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
pub fn addCases(cases: *tests.CompileErrorContext) void {
|
pub fn addCases(cases: *tests.CompileErrorContext) void {
|
||||||
|
cases.add("union with too small explicit signed tag type",
|
||||||
|
\\const U = union(enum(i2)) {
|
||||||
|
\\ A: u8,
|
||||||
|
\\ B: u8,
|
||||||
|
\\ C: u8,
|
||||||
|
\\ D: u8,
|
||||||
|
\\};
|
||||||
|
\\export fn entry() void {
|
||||||
|
\\ _ = U{ .D = 1 };
|
||||||
|
\\}
|
||||||
|
, &[_][]const u8{
|
||||||
|
"tmp.zig:1:22: error: specified integer tag type cannot represent every field",
|
||||||
|
"tmp.zig:1:22: note: type i2 cannot fit values in range 0...3",
|
||||||
|
});
|
||||||
|
|
||||||
|
cases.add("union with too small explicit unsigned tag type",
|
||||||
|
\\const U = union(enum(u2)) {
|
||||||
|
\\ A: u8,
|
||||||
|
\\ B: u8,
|
||||||
|
\\ C: u8,
|
||||||
|
\\ D: u8,
|
||||||
|
\\ E: u8,
|
||||||
|
\\};
|
||||||
|
\\export fn entry() void {
|
||||||
|
\\ _ = U{ .E = 1 };
|
||||||
|
\\}
|
||||||
|
, &[_][]const u8{
|
||||||
|
"tmp.zig:1:22: error: specified integer tag type cannot represent every field",
|
||||||
|
"tmp.zig:1:22: note: type u2 cannot fit values in range 0...4",
|
||||||
|
});
|
||||||
|
|
||||||
cases.add("unreachable executed at comptime",
|
cases.add("unreachable executed at comptime",
|
||||||
\\fn foo(comptime x: i32) i32 {
|
\\fn foo(comptime x: i32) i32 {
|
||||||
\\ comptime {
|
\\ comptime {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue