Skip to content

Commit 2696c8b

Browse files
LemonBoyandrewrk
authored andcommitted
ir: Robust checking for init expr type
Closes #3979
1 parent 6a0927d commit 2696c8b

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/ir.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14875,19 +14875,19 @@ static IrInstGen *ir_analyze_cast(IrAnalyze *ira, IrInst *source_instr,
1487514875

1487614876
// cast from inferred struct type to array, union, or struct
1487714877
if (is_anon_container(actual_type)) {
14878-
AstNode *decl_node = actual_type->data.structure.decl_node;
14879-
ir_assert(decl_node->type == NodeTypeContainerInitExpr, source_instr);
14880-
ContainerInitKind init_kind = decl_node->data.container_init_expr.kind;
14881-
uint32_t field_count = actual_type->data.structure.src_field_count;
14882-
if (wanted_type->id == ZigTypeIdArray && (init_kind == ContainerInitKindArray || field_count == 0) &&
14878+
const bool is_array_init =
14879+
actual_type->data.structure.special == StructSpecialInferredTuple;
14880+
const uint32_t field_count = actual_type->data.structure.src_field_count;
14881+
14882+
if (wanted_type->id == ZigTypeIdArray && (is_array_init || field_count == 0) &&
1488314883
wanted_type->data.array.len == field_count)
1488414884
{
1488514885
return ir_analyze_struct_literal_to_array(ira, source_instr, value, wanted_type);
1488614886
} else if (wanted_type->id == ZigTypeIdStruct &&
14887-
(init_kind == ContainerInitKindStruct || field_count == 0))
14887+
(!is_array_init || field_count == 0))
1488814888
{
1488914889
return ir_analyze_struct_literal_to_struct(ira, source_instr, value, wanted_type);
14890-
} else if (wanted_type->id == ZigTypeIdUnion && init_kind == ContainerInitKindStruct && field_count == 1) {
14890+
} else if (wanted_type->id == ZigTypeIdUnion && !is_array_init && field_count == 1) {
1489114891
return ir_analyze_struct_literal_to_union(ira, source_instr, value, wanted_type);
1489214892
}
1489314893
}

test/compile_errors.zig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ const builtin = @import("builtin");
33
const Target = @import("std").Target;
44

55
pub fn addCases(cases: *tests.CompileErrorContext) void {
6+
cases.addTest("type mismatch with tuple concatenation",
7+
\\export fn entry() void {
8+
\\ var x = .{};
9+
\\ x = x ++ .{ 1, 2, 3 };
10+
\\}
11+
, &[_][]const u8{
12+
"tmp.zig:3:11: error: expected type 'struct:2:14', found 'struct:3:11'",
13+
});
14+
615
cases.addTest("@tagName on invalid value of non-exhaustive enum",
716
\\test "enum" {
817
\\ const E = enum(u8) {A, B, _};

0 commit comments

Comments
 (0)