diff --git a/src/ir.cpp b/src/ir.cpp index 5339931590030316d3bc077593431884cfd4e0c3..cdf56f7feea27e5204356859cca3187e58fc9a44 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -13736,22 +13736,6 @@ 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 && - (child_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr || - child_type->data.unionation.decl_node->data.container_decl.auto_enum)) - { - ensure_complete_type(ira->codegen, child_type); - if (type_is_invalid(child_type)) - return ira->codegen->builtin_types.entry_invalid; - TypeUnionField *field = find_union_type_field(child_type, field_name); - if (field) { - TypeTableEntry *enum_type = child_type->data.unionation.tag_type; - bool ptr_is_const = true; - bool ptr_is_volatile = false; - return ir_analyze_const_ptr(ira, &field_ptr_instruction->base, - create_const_enum(enum_type, &field->enum_field->value), enum_type, - ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile); - } } ScopeDecls *container_scope = get_container_scope(child_type); if (container_scope != nullptr) { @@ -13761,6 +13745,23 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru 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)) + { + ensure_complete_type(ira->codegen, child_type); + if (type_is_invalid(child_type)) + return ira->codegen->builtin_types.entry_invalid; + TypeUnionField *field = find_union_type_field(child_type, field_name); + if (field) { + TypeTableEntry *enum_type = child_type->data.unionation.tag_type; + bool ptr_is_const = true; + bool ptr_is_volatile = false; + return ir_analyze_const_ptr(ira, &field_ptr_instruction->base, + create_const_enum(enum_type, &field->enum_field->value), enum_type, + ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile); + } + } 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 e7d9c23d77c4f829b75bf882f0cc2547940cfa2f..f1fef4665736b5504727dc7baaad5ed3f566ecbc 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); +}