fix error note using invalid source node

Closes #6153
This commit is contained in:
Vexu 2020-08-24 15:24:00 +03:00
parent 9589dc4c95
commit c1ee9efb7c
No known key found for this signature in database
GPG Key ID: 59AEB8936E16A6AC
2 changed files with 15 additions and 4 deletions

View File

@ -20695,8 +20695,13 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
if ((return_type->id == ZigTypeIdErrorUnion || return_type->id == ZigTypeIdErrorSet) &&
expected_return_type->id != ZigTypeIdErrorUnion && expected_return_type->id != ZigTypeIdErrorSet)
{
add_error_note(ira->codegen, ira->new_irb.exec->first_err_trace_msg,
ira->explicit_return_type_source_node, buf_create_from_str("function cannot return an error"));
if (call_result_loc->id == ResultLocIdReturn) {
add_error_note(ira->codegen, ira->new_irb.exec->first_err_trace_msg,
ira->explicit_return_type_source_node, buf_sprintf("function cannot return an error"));
} else {
add_error_note(ira->codegen, ira->new_irb.exec->first_err_trace_msg, result_loc->base.source_node,
buf_sprintf("cannot store an error in type '%s'", buf_ptr(&expected_return_type->name)));
}
}
return ira->codegen->invalid_inst_gen;
}

View File

@ -158,16 +158,22 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\export fn baz() void {
\\ try bar();
\\}
\\export fn quux() u32 {
\\export fn qux() u32 {
\\ return bar();
\\}
\\export fn quux() u32 {
\\ var buf: u32 = 0;
\\ buf = bar();
\\}
, &[_][]const u8{
"tmp.zig:2:17: error: expected type 'u32', found 'error{Ohno}'",
"tmp.zig:1:17: note: function cannot return an error",
"tmp.zig:8:5: error: expected type 'void', found '@TypeOf(bar).ReturnType.ErrorSet'",
"tmp.zig:7:17: note: function cannot return an error",
"tmp.zig:11:15: error: expected type 'u32', found '@TypeOf(bar).ReturnType.ErrorSet!u32'",
"tmp.zig:10:18: note: function cannot return an error",
"tmp.zig:10:17: note: function cannot return an error",
"tmp.zig:15:14: error: expected type 'u32', found '@TypeOf(bar).ReturnType.ErrorSet!u32'",
"tmp.zig:14:5: note: cannot store an error in type 'u32'",
});
cases.addTest("int/float conversion to comptime_int/float",