authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-12-24 11:19:46+01:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-12-24 15:57:12+02:00
loge18abab55aea4a5c3b347274e41de9fdc24e950c
treefdf2716108151ffcddde84eaf5431fb27fbfac5f
parent83646df2cce59f254822355ec1ceeb6884e1177e

stage1: Create a new declaration scope for union enum types

Making the enum type share the scope with the parent union means every declaration "bleeds" into the enum scope. Let's mint a fresh empty scope for the enum type. Thanks to @Vexu for the test case. Closes #7532

2 files changed, 17 insertions(+), 1 deletions(-)

src/stage1/analyze.cpp+2-1
...@@ -3281,7 +3281,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -3281,7 +3281,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
3281 tag_type->data.enumeration.src_field_count = field_count;3281 tag_type->data.enumeration.src_field_count = field_count;
3282 tag_type->data.enumeration.fields = heap::c_allocator.allocate<TypeEnumField>(field_count);3282 tag_type->data.enumeration.fields = heap::c_allocator.allocate<TypeEnumField>(field_count);
3283 tag_type->data.enumeration.fields_by_name.init(field_count);3283 tag_type->data.enumeration.fields_by_name.init(field_count);
3284 tag_type->data.enumeration.decls_scope = union_type->data.unionation.decls_scope;3284 tag_type->data.enumeration.decls_scope = create_decls_scope(
3285 g, nullptr, nullptr, tag_type, get_scope_import(scope), &tag_type->name);
3285 } else if (enum_type_node != nullptr) {3286 } else if (enum_type_node != nullptr) {
3286 tag_type = analyze_type_expr(g, scope, enum_type_node);3287 tag_type = analyze_type_expr(g, scope, enum_type_node);
3287 } else {3288 } else {
test/stage1/behavior/union.zig+15
...@@ -761,3 +761,18 @@ test "@unionInit on union w/ tag but no fields" {...@@ -761,3 +761,18 @@ test "@unionInit on union w/ tag but no fields" {
761 S.doTheTest();761 S.doTheTest();
762 comptime S.doTheTest();762 comptime S.doTheTest();
763}763}
764
765test "union enum type gets a separate scope" {
766 const S = struct {
767 const U = union(enum) {
768 a: u8,
769 const foo = 1;
770 };
771
772 fn doTheTest() void {
773 expect(!@hasDecl(@TagType(U), "foo"));
774 }
775 };
776
777 S.doTheTest();
778}