Commit c1ee9efb7c
Changed files (2)
src
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;
}
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",