diff --git a/src/all_types.hpp b/src/all_types.hpp index 6f36d34bfd80a54158dcdb4f6fc73a53b3dbb329..3d732d4ace12c998eceb2a7388a8ddb8a6bfcce7 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1096,6 +1096,8 @@ struct TypeTableEntryUnion { size_t gen_union_index; size_t gen_tag_index; + bool have_explicit_tag_type; + uint32_t union_size_bytes; TypeTableEntry *most_aligned_union_member; diff --git a/src/analyze.cpp b/src/analyze.cpp index 66d29634251659a54f91b643a4bc67cf06f2c0af..74dfd003d95286204ac3ba2fe1919ebb181dd810 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -2558,6 +2558,8 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) { HashMap occupied_tag_values = {}; AstNode *enum_type_node = decl_node->data.container_decl.init_arg_expr; + union_type->data.unionation.have_explicit_tag_type = decl_node->data.container_decl.auto_enum || + enum_type_node != nullptr; bool auto_layout = (union_type->data.unionation.layout == ContainerLayoutAuto); bool want_safety = (field_count >= 2) && (auto_layout || enum_type_node != nullptr); TypeTableEntry *tag_type; diff --git a/src/ir.cpp b/src/ir.cpp index cdb8201b2b39eefd224ded04a6f98dd880849c9b..811744e9d0e9f2f5f6a23038a17e69c1078026ab 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -14137,6 +14137,14 @@ static IrInstruction *ir_analyze_union_tag(IrAnalyze *ira, IrInstruction *source buf_sprintf("expected enum or union type, found '%s'", buf_ptr(&value->value.type->name))); return ira->codegen->invalid_instruction; } + if (!value->value.type->data.unionation.have_explicit_tag_type && !source_instr->is_gen) { + ErrorMsg *msg = ir_add_error(ira, source_instr, buf_sprintf("union has no associated enum")); + if (value->value.type->data.unionation.decl_node != nullptr) { + add_error_note(ira->codegen, msg, value->value.type->data.unionation.decl_node, + buf_sprintf("declared here")); + } + return ira->codegen->invalid_instruction; + } TypeTableEntry *tag_type = value->value.type->data.unionation.tag_type; assert(tag_type->id == TypeTableEntryIdEnum); diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 1bca093e791fbaa60af47ec460fe3ad7db857a6e..564ad5d5214943c2e832c9f9cd36d44b90d09a92 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -1,6 +1,19 @@ const tests = @import("tests.zig"); pub fn addCases(cases: &tests.CompileErrorContext) void { + cases.add("@tagName used on union with no associated enum tag", + \\const FloatInt = extern union { + \\ Float: f32, + \\ Int: i32, + \\}; + \\export fn entry() void { + \\ var fi = FloatInt{.Float = 123.45}; + \\ var tagName = @tagName(fi); + \\} + , + ".tmp_source.zig:7:19: error: union has no associated enum", + ".tmp_source.zig:1:18: note: declared here"); + cases.add("returning error from void async function", \\const std = @import("std"); \\export fn entry() void {