aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2018-11-19 11:22:57 -0500
committerAndrew Kelley <superjoe30@gmail.com>2018-11-19 11:22:57 -0500
commit2f5d1ec5006ccae81f6de80233fa7800237779e2 (patch)
tree719a681a4d2281fa29376643e1506bf1dbcbae5e
parent81d9403dcea172ce2fdb44610c9350821109adae (diff)
downloadzig-2f5d1ec5006ccae81f6de80233fa7800237779e2.tar.gz
zig-2f5d1ec5006ccae81f6de80233fa7800237779e2.zip
improve error message when wrong type returned
closes #1650
-rw-r--r--src/ir.cpp8
-rw-r--r--test/compile_errors.zig1
2 files changed, 8 insertions, 1 deletions
diff --git a/src/ir.cpp b/src/ir.cpp
index 55f17b811d..03389a2320 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<IrInstruction *> 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 45bd6cb40b..16690daf29 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(