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(...@@ -1938,6 +1938,7 @@ fn zirEnumDecl(
1938 var bit_bag_index: usize = body_end;1938 var bit_bag_index: usize = body_end;
1939 var cur_bit_bag: u32 = undefined;1939 var cur_bit_bag: u32 = undefined;
1940 var field_i: u32 = 0;1940 var field_i: u32 = 0;
1941 var last_tag_val: ?Value = null;
1941 while (field_i < fields_len) : (field_i += 1) {1942 while (field_i < fields_len) : (field_i += 1) {
1942 if (field_i % 32 == 0) {1943 if (field_i % 32 == 0) {
1943 cur_bit_bag = sema.code.extra[bit_bag_index];1944 cur_bit_bag = sema.code.extra[bit_bag_index];
...@@ -1976,13 +1977,21 @@ fn zirEnumDecl(...@@ -1976,13 +1977,21 @@ fn zirEnumDecl(
1976 // that points to this default value expression rather than the struct.1977 // that points to this default value expression rather than the struct.
1977 // But only resolve the source location if we need to emit a compile error.1978 // But only resolve the source location if we need to emit a compile error.
1978 const tag_val = (try sema.resolveInstConst(block, src, tag_val_ref)).val;1979 const tag_val = (try sema.resolveInstConst(block, src, tag_val_ref)).val;
1980 last_tag_val = tag_val;
1979 const copied_tag_val = try tag_val.copy(new_decl_arena_allocator);1981 const copied_tag_val = try tag_val.copy(new_decl_arena_allocator);
1980 enum_obj.values.putAssumeCapacityNoClobberContext(copied_tag_val, {}, .{1982 enum_obj.values.putAssumeCapacityNoClobberContext(copied_tag_val, {}, .{
1981 .ty = enum_obj.tag_ty,1983 .ty = enum_obj.tag_ty,
1982 });1984 });
1983 } else if (any_values) {1985 } else if (any_values) {
1984 const tag_val = try Value.Tag.int_u64.create(new_decl_arena_allocator, field_i);1986 const tag_val = if (last_tag_val) |val|
1985 enum_obj.values.putAssumeCapacityNoClobberContext(tag_val, {}, .{ .ty = enum_obj.tag_ty });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 });
1986 }1995 }
1987 }1996 }
19881997
test/behavior/enum.zig-2
...@@ -873,8 +873,6 @@ test "method call on an enum" {...@@ -873,8 +873,6 @@ test "method call on an enum" {
873}873}
874874
875test "enum value allocation" {875test "enum value allocation" {
876 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
877
878 const LargeEnum = enum(u32) {876 const LargeEnum = enum(u32) {
879 A0 = 0x80000000,877 A0 = 0x80000000,
880 A1,878 A1,