-
Notifications
You must be signed in to change notification settings - Fork 283
WIP: Implement throw & catch statements #6916
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
base: master
Are you sure you want to change the base?
WIP: Implement throw & catch statements #6916
Conversation
It already existed in the IR, so only parsing, checking and lowering was missing.
Thank for taking initiative to continue the error handling implementation! I think we can just made the current scope not visible to catch block for simplicity. |
Likely very broken.
I noticed one "gotcha" in the existing design of the enum MyError
{
Failure1,
Failure2
};
float f() throws MyError
{
// Problem: the value of Failure1 is 0, so this throw gets
// interpreted as a valid return value.
throw MyError.Failure1;
}
export __extern_cpp int main()
{
{
let res = try f();
printf("%f\n", res); // This is executed, with undefined value for 'res'
catch(MyError err)
{ // This is not executed.
printf("Caught an error %d\n", err);
}
}
return 0;
} Because I'd expect I'm also still missing a check for |
We want to reserve 0 for success so that it works with COM interface convention. The problem with using max instead of 0 for success is you will lose forward binary compatibility. I think we should only allow enum types that inherit Error type to be used as error type, where Error is just a builtin enum that declares a general success code (0) and a general failure code (1). |
Inheriting from an enum doesn't seem to exist as a feature yet. Should I implement that? It doesn't seem terribly difficult, assuming it just means inheriting the underlying type and cases. I have a couple of other options in mind as well: 1. interface IError
{
bool isSuccess();
} The above could allow non-integer error types as well, but admittedly doesn't enforce adherence to the COM interface convention. 2. |
As those may actually not be available at that point.
Whenever possible, we should solve the problem with the type system instead of through new modifiers or attributes. We could add support for enum type inheritance or IError type. I think we will need something like IError in the long term so it is a good idea to consider implementing it now. |
It may not exist if the function returns void, but getting the error value is still legitimate.
... To make SPIR-V happy.
54d24f1
to
2d97ba7
Compare
The current status of the error handling mechanism is that
throws
in function signatures andtry
expressions work,throw
only exists on the IR level (to allowtry
to re-throw) andcatch
is completely unimplemented.This PR finishes
throw
such that it can be used directly, and aims to implementcatch
.WIP: Might take a while. Finishing up
throw
seemed simple as it was mostly done already, butcatch
is a bit more complex. One non-trivial issue is the lifetime of variables that may be referenced fromcatch
: