From 2f5d1ec5006ccae81f6de80233fa7800237779e2 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 19 Nov 2018 11:22:57 -0500 Subject: [PATCH] improve error message when wrong type returned closes #1650 --- src/ir.cpp | 8 +++++++- test/compile_errors.zig | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/ir.cpp b/src/ir.cpp index 55f17b811..03389a232 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -34,6 +34,7 @@ struct IrAnalyze { size_t old_bb_index; size_t instruction_index; ZigType *explicit_return_type; + AstNode *explicit_return_type_source_node; ZigList src_implicit_return_type_list; IrBasicBlock *const_predecessor_bb; }; @@ -11162,8 +11163,12 @@ static IrInstruction *ir_analyze_instruction_return(IrAnalyze *ira, IrInstructio return ir_unreach_error(ira); IrInstruction *casted_value = ir_implicit_cast(ira, value, ira->explicit_return_type); - if (type_is_invalid(casted_value->value.type)) + if (type_is_invalid(casted_value->value.type) && ira->explicit_return_type_source_node != nullptr) { + ErrorMsg *msg = ira->codegen->errors.last(); + add_error_note(ira->codegen, msg, ira->explicit_return_type_source_node, + buf_sprintf("return type declared here")); return ir_unreach_error(ira); + } if (casted_value->value.special == ConstValSpecialRuntime && casted_value->value.type->id == ZigTypeIdPointer && @@ -21213,6 +21218,7 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_ ZigFn *fn_entry = exec_fn_entry(old_exec); bool is_async = fn_entry != nullptr && fn_entry->type_entry->data.fn.fn_type_id.cc == CallingConventionAsync; ira->explicit_return_type = is_async ? get_promise_type(codegen, expected_type) : expected_type; + ira->explicit_return_type_source_node = expected_type_source_node; ira->old_irb.codegen = codegen; ira->old_irb.exec = old_exec; diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 45bd6cb40..16690daf2 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -3219,6 +3219,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\fn something() anyerror!void { } , ".tmp_source.zig:2:5: error: expected type 'void', found 'anyerror'", + ".tmp_source.zig:1:15: note: return type declared here", ); cases.add(