Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message when using 'try' #1650

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/ir.cpp
Expand Up @@ -11217,11 +11217,45 @@ static IrInstruction *ir_analyze_instruction_add_implicit_return_type(IrAnalyze
return ir_const_void(ira, &instruction->base);
}

static bool type_is_error(ZigType *type) {
if (!type)
return false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could hide a bug. Better to assert that your parameter is non-null.


switch(type->id) {
case ZigTypeIdErrorUnion:
case ZigTypeIdErrorSet:
return true;
case ZigTypeIdOptional:
return type_is_error(type->data.maybe.child_type);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this case should be here? What would the zig source look like that uses this?

default:
return false;
}
}

static IrInstruction *ir_analyze_instruction_return(IrAnalyze *ira, IrInstructionReturn *instruction) {
IrInstruction *value = instruction->value->child;
if (type_is_invalid(value->value.type))
return ir_unreach_error(ira);

// returning error type in function that does not have an error return type
ZigFn *fn_entry = exec_fn_entry(ira->new_irb.exec);
if (type_is_error(value->value.type) && !type_is_error(ira->explicit_return_type) && fn_entry) {
Buf *err_msg;

if(instruction->value->id == IrInstructionIdUnwrapErrCode) {
err_msg = buf_sprintf("try when the return type cannot handle an error");
} else {
err_msg = buf_sprintf("return error '%s' when the return type cannot handle the error",
buf_ptr(&value->value.type->name));
}

ErrorMsg *err = ir_add_error_node(ira, value->source_node, err_msg);
add_error_note(ira->codegen, err, fn_entry->proto_node->data.fn_proto.return_type,
buf_sprintf("function return type was defined here; did you mean '!%s'?",
buf_ptr(&ira->explicit_return_type->name)));
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))
return ir_unreach_error(ira);
Expand Down
3 changes: 2 additions & 1 deletion test/compile_errors.zig
Expand Up @@ -3169,7 +3169,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\}
\\fn something() error!void { }
,
".tmp_source.zig:2:5: error: expected type 'void', found 'error'",
".tmp_source.zig:2:5: error: try when the return type cannot handle an error",
".tmp_source.zig:1:15: note: function return type was defined here; did you mean '!void'?"
);

cases.add(
Expand Down