-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Conversation
As described here: #1587 (comment) when using 'try' in a function with that expects a non-error return type you get a confusing error message.
This is my first time looking at zig source code, please tell me if this is the wrong way/place to handle this |
I think this is a huge improvement. There's a better place to put the So you would check if There's a better way to get access to the function too, which is ZigFn *fn_entry = exec_fn_entry(ira->new_irb.exec);
assert(fn_entry); // this assertion will be OK since we check earlier for `return` outside a function. |
Note that this will change the compile error even if the user does
|
As described here: #1587 (comment) when using 'try' in a function with that expects a non-error return type you get a confusing error message.
As described here: #1587 (comment) when using 'try' in a function with that expects a non-error return type you get a confusing error message.
I noticed that my changes do not apply to async functions and they require a different type of check |
I wouldn't focus too much on the async function aspect of it yet. I'm in the middle of rewriting async functions in another branch. Maybe you can revisit it once that's merged. |
As described here: #1587 (comment) when using 'try' in a function with that expects a non-error return type you get a confusing error message.
@@ -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; |
There was a problem hiding this comment.
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.
case ZigTypeIdErrorSet: | ||
return true; | ||
case ZigTypeIdOptional: | ||
return type_is_error(type->data.maybe.child_type); |
There was a problem hiding this comment.
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?
I solved this in a more generic way: export fn foo() void {
return true;
}
export fn bar() void {
try baz();
}
fn baz() anyerror!void {}
|
As described here: #1587 (comment) when using 'try' in a function with that expects a non-error return type you get a confusing error message.