authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-22 12:44:35-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-22 12:44:35-07:00
log0d422ce342977b56247ec95aa0bd58045ee8b66b
tree009f7b991f8ffff7f92355384ebe67b937fea0ce
parente620b692c6c9f7c4b655f30aae5e51a87dc56e91

Sema: auto-numbered enums increment from last tag value

This matches stage1 and the existing behavior tests.

2 files changed, 11 insertions(+), 4 deletions(-)

src/Sema.zig+11-2
......@@ -1938,6 +1938,7 @@ fn zirEnumDecl(
19381938 var bit_bag_index: usize = body_end;
19391939 var cur_bit_bag: u32 = undefined;
19401940 var field_i: u32 = 0;
1941 var last_tag_val: ?Value = null;
19411942 while (field_i < fields_len) : (field_i += 1) {
19421943 if (field_i % 32 == 0) {
19431944 cur_bit_bag = sema.code.extra[bit_bag_index];
......@@ -1976,13 +1977,21 @@ fn zirEnumDecl(
19761977 // that points to this default value expression rather than the struct.
19771978 // But only resolve the source location if we need to emit a compile error.
19781979 const tag_val = (try sema.resolveInstConst(block, src, tag_val_ref)).val;
1980 last_tag_val = tag_val;
19791981 const copied_tag_val = try tag_val.copy(new_decl_arena_allocator);
19801982 enum_obj.values.putAssumeCapacityNoClobberContext(copied_tag_val, {}, .{
19811983 .ty = enum_obj.tag_ty,
19821984 });
19831985 } else if (any_values) {
1984 const tag_val = try Value.Tag.int_u64.create(new_decl_arena_allocator, field_i);
1985 enum_obj.values.putAssumeCapacityNoClobberContext(tag_val, {}, .{ .ty = enum_obj.tag_ty });
1986 const tag_val = if (last_tag_val) |val|
1987 try val.intAdd(Value.one, sema.arena)
1988 else
1989 Value.zero;
1990 last_tag_val = tag_val;
1991 const copied_tag_val = try tag_val.copy(new_decl_arena_allocator);
1992 enum_obj.values.putAssumeCapacityNoClobberContext(copied_tag_val, {}, .{
1993 .ty = enum_obj.tag_ty,
1994 });
19861995 }
19871996 }
19881997
test/behavior/enum.zig-2
......@@ -873,8 +873,6 @@ test "method call on an enum" {
873873}
874874
875875test "enum value allocation" {
876 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
877
878876 const LargeEnum = enum(u32) {
879877 A0 = 0x80000000,
880878 A1,