Sema: auto-numbered enums increment from last tag value

This matches stage1 and the existing behavior tests.
This commit is contained in:
Andrew Kelley 2022-02-22 12:44:35 -07:00
parent e620b692c6
commit 0d422ce342
2 changed files with 11 additions and 4 deletions

View File

@ -1938,6 +1938,7 @@ fn zirEnumDecl(
var bit_bag_index: usize = body_end;
var cur_bit_bag: u32 = undefined;
var field_i: u32 = 0;
var last_tag_val: ?Value = null;
while (field_i < fields_len) : (field_i += 1) {
if (field_i % 32 == 0) {
cur_bit_bag = sema.code.extra[bit_bag_index];
@ -1976,13 +1977,21 @@ fn zirEnumDecl(
// that points to this default value expression rather than the struct.
// But only resolve the source location if we need to emit a compile error.
const tag_val = (try sema.resolveInstConst(block, src, tag_val_ref)).val;
last_tag_val = tag_val;
const copied_tag_val = try tag_val.copy(new_decl_arena_allocator);
enum_obj.values.putAssumeCapacityNoClobberContext(copied_tag_val, {}, .{
.ty = enum_obj.tag_ty,
});
} else if (any_values) {
const tag_val = try Value.Tag.int_u64.create(new_decl_arena_allocator, field_i);
enum_obj.values.putAssumeCapacityNoClobberContext(tag_val, {}, .{ .ty = enum_obj.tag_ty });
const tag_val = if (last_tag_val) |val|
try val.intAdd(Value.one, sema.arena)
else
Value.zero;
last_tag_val = tag_val;
const copied_tag_val = try tag_val.copy(new_decl_arena_allocator);
enum_obj.values.putAssumeCapacityNoClobberContext(copied_tag_val, {}, .{
.ty = enum_obj.tag_ty,
});
}
}

View File

@ -873,8 +873,6 @@ test "method call on an enum" {
}
test "enum value allocation" {
if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
const LargeEnum = enum(u32) {
A0 = 0x80000000,
A1,