diff --git a/src/ir.cpp b/src/ir.cpp index a2056a4c9b..c8b6c432ee 100644 --- 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 index f5ebc02691..fe11763ea1 100644 --- 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",