From 2696c8b42d7fcec1b8cf3f332742df4638c519b1 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Thu, 27 Feb 2020 13:02:21 +0100 Subject: [PATCH] ir: Robust checking for init expr type Closes #3979 --- src/ir.cpp | 14 +++++++------- test/compile_errors.zig | 9 +++++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/ir.cpp b/src/ir.cpp index 4d24ec7dfb..7de45ebcd3 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -14875,19 +14875,19 @@ static IrInstGen *ir_analyze_cast(IrAnalyze *ira, IrInst *source_instr, // cast from inferred struct type to array, union, or struct if (is_anon_container(actual_type)) { - AstNode *decl_node = actual_type->data.structure.decl_node; - ir_assert(decl_node->type == NodeTypeContainerInitExpr, source_instr); - ContainerInitKind init_kind = decl_node->data.container_init_expr.kind; - uint32_t field_count = actual_type->data.structure.src_field_count; - if (wanted_type->id == ZigTypeIdArray && (init_kind == ContainerInitKindArray || field_count == 0) && + const bool is_array_init = + actual_type->data.structure.special == StructSpecialInferredTuple; + const uint32_t field_count = actual_type->data.structure.src_field_count; + + if (wanted_type->id == ZigTypeIdArray && (is_array_init || field_count == 0) && wanted_type->data.array.len == field_count) { return ir_analyze_struct_literal_to_array(ira, source_instr, value, wanted_type); } else if (wanted_type->id == ZigTypeIdStruct && - (init_kind == ContainerInitKindStruct || field_count == 0)) + (!is_array_init || field_count == 0)) { return ir_analyze_struct_literal_to_struct(ira, source_instr, value, wanted_type); - } else if (wanted_type->id == ZigTypeIdUnion && init_kind == ContainerInitKindStruct && field_count == 1) { + } else if (wanted_type->id == ZigTypeIdUnion && !is_array_init && field_count == 1) { return ir_analyze_struct_literal_to_union(ira, source_instr, value, wanted_type); } } diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 5e4f76baf4..91c17d8807 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -3,6 +3,15 @@ const builtin = @import("builtin"); const Target = @import("std").Target; pub fn addCases(cases: *tests.CompileErrorContext) void { + cases.addTest("type mismatch with tuple concatenation", + \\export fn entry() void { + \\ var x = .{}; + \\ x = x ++ .{ 1, 2, 3 }; + \\} + , &[_][]const u8{ + "tmp.zig:3:11: error: expected type 'struct:2:14', found 'struct:3:11'", + }); + cases.addTest("@tagName on invalid value of non-exhaustive enum", \\test "enum" { \\ const E = enum(u8) {A, B, _};