Skip to content

ir: Robust checking for init expr type #4570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
9 changes: 9 additions & 0 deletions test/compile_errors.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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, _};
Expand Down