diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2018-05-07 16:43:20 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2018-05-07 16:43:20 -0400 |
| commit | 77a1a216d2b3ceb956869ba1716fbb6c0c7eabe8 (patch) | |
| tree | f4b33b6b983451391e4d627acc45411e3730c8d5 | |
| parent | 2f633452bb337a3f173c4fd82c2b4a0880f981f5 (diff) | |
| download | zig-77a1a216d2b3ceb956869ba1716fbb6c0c7eabe8.tar.gz zig-77a1a216d2b3ceb956869ba1716fbb6c0c7eabe8.zip | |
tagged union field access prioritizes members over enum tags
closes #959
| -rw-r--r-- | src/ir.cpp | 19 | ||||
| -rw-r--r-- | test/cases/union.zig | 12 |
2 files changed, 22 insertions, 9 deletions
diff --git a/src/ir.cpp b/src/ir.cpp index 5339931590..cdf56f7fee 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -13736,7 +13736,16 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru create_const_enum(child_type, &field->value), child_type, ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile); } - } else if (child_type->id == TypeTableEntryIdUnion && + } + ScopeDecls *container_scope = get_container_scope(child_type); + if (container_scope != nullptr) { + auto entry = container_scope->decl_table.maybe_get(field_name); + Tld *tld = entry ? entry->value : nullptr; + if (tld) { + return ir_analyze_decl_ref(ira, &field_ptr_instruction->base, tld); + } + } + if (child_type->id == TypeTableEntryIdUnion && (child_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr || child_type->data.unionation.decl_node->data.container_decl.auto_enum)) { @@ -13753,14 +13762,6 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile); } } - ScopeDecls *container_scope = get_container_scope(child_type); - if (container_scope != nullptr) { - auto entry = container_scope->decl_table.maybe_get(field_name); - Tld *tld = entry ? entry->value : nullptr; - if (tld) { - return ir_analyze_decl_ref(ira, &field_ptr_instruction->base, tld); - } - } ir_add_error(ira, &field_ptr_instruction->base, buf_sprintf("container '%s' has no member called '%s'", buf_ptr(&child_type->name), buf_ptr(field_name))); diff --git a/test/cases/union.zig b/test/cases/union.zig index e7d9c23d77..f1fef46657 100644 --- a/test/cases/union.zig +++ b/test/cases/union.zig @@ -272,3 +272,15 @@ const PartialInstWithPayload = union(enum) { Compiled: i32, }; + +test "access a member of tagged union with conflicting enum tag name" { + const Bar = union(enum) { + A: A, + B: B, + + const A = u8; + const B = void; + }; + + comptime assert(Bar.A == u8); +} |
