Commit 77a1a216d2

Andrew Kelley <superjoe30@gmail.com>
2018-05-07 22:43:20
tagged union field access prioritizes members over enum tags
closes #959
1 parent 2f63345
Changed files (2)
src
test
cases
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)));
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);
+}