Skip to content

Commit 5ea79bf

Browse files
committed
fix not checking type of return pointer
Thanks to Vexu for the test cases. Closes #3422 Closes #3646 Closes #3224 Closes #3327 Closes #3269
1 parent 04ee3b0 commit 5ea79bf

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/ir.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19591,6 +19591,12 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
1959119591
if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {
1959219592
return result_loc;
1959319593
}
19594+
IrInstGen *dummy_value = ir_const(ira, source_instr, impl_fn_type_id->return_type);
19595+
dummy_value->value->special = ConstValSpecialRuntime;
19596+
IrInstGen *dummy_result = ir_implicit_cast2(ira, source_instr,
19597+
dummy_value, result_loc->value->type->data.pointer.child_type);
19598+
if (type_is_invalid(dummy_result->value->type))
19599+
return ira->codegen->invalid_inst_gen;
1959419600
ZigType *res_child_type = result_loc->value->type->data.pointer.child_type;
1959519601
if (res_child_type == ira->codegen->builtin_types.entry_var) {
1959619602
res_child_type = impl_fn_type_id->return_type;
@@ -19723,6 +19729,12 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
1972319729
if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {
1972419730
return result_loc;
1972519731
}
19732+
IrInstGen *dummy_value = ir_const(ira, source_instr, return_type);
19733+
dummy_value->value->special = ConstValSpecialRuntime;
19734+
IrInstGen *dummy_result = ir_implicit_cast2(ira, source_instr,
19735+
dummy_value, result_loc->value->type->data.pointer.child_type);
19736+
if (type_is_invalid(dummy_result->value->type))
19737+
return ira->codegen->invalid_inst_gen;
1972619738
ZigType *res_child_type = result_loc->value->type->data.pointer.child_type;
1972719739
if (res_child_type == ira->codegen->builtin_types.entry_var) {
1972819740
res_child_type = return_type;

test/compile_errors.zig

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

55
pub fn addCases(cases: *tests.CompileErrorContext) void {
6+
cases.add("function call assigned to incorrect type",
7+
\\export fn entry() void {
8+
\\ var arr: [4]f32 = undefined;
9+
\\ arr = concat();
10+
\\}
11+
\\fn concat() [16]f32 {
12+
\\ return [1]f32{0}**16;
13+
\\}
14+
, &[_][]const u8{
15+
"tmp.zig:3:17: error: expected type '[4]f32', found '[16]f32'",
16+
});
17+
18+
cases.add("generic function call assigned to incorrect type",
19+
\\pub export fn entry() void {
20+
\\ var res: []i32 = undefined;
21+
\\ res = myAlloc(i32);
22+
\\}
23+
\\fn myAlloc(comptime arg: type) anyerror!arg{
24+
\\ unreachable;
25+
\\}
26+
, &[_][]const u8{
27+
"tmp.zig:3:18: error: expected type '[]i32', found 'anyerror!i32",
28+
});
29+
630
cases.addTest("dependency loop in top-level decl with @TypeInfo",
731
\\export const foo = @typeInfo(@This());
832
, &[_][]const u8{

0 commit comments

Comments
 (0)