commit c1ee9efb7c2a1589b14d6efeffb1f5e6c2d9b994 (tree)
parent 9589dc4c954c1fd10bdf075c62418aeed42ae96f
Author: Vexu <git@vexu.eu>
Date: Mon, 24 Aug 2020 15:24:00 +0300
fix error note using invalid source node
Closes #6153
Diffstat:
2 files changed, 15 insertions(+), 4 deletions(-)
diff --git a/src/ir.cpp b/src/ir.cpp
@@ -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;
}
diff --git a/test/compile_errors.zig b/test/compile_errors.zig
@@ -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",