-
-
Notifications
You must be signed in to change notification settings - Fork 315
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
Constrain early returns in functions in addition to closures #7204
Constrain early returns in functions in addition to closures #7204
Conversation
I've been investigating this issue on and off in the past week, and I've found a small reproduction of the issue, which seems to be somewhere at or before monomorphization. With the following function: validateInput : Str -> Result U64 _
validateInput = \str ->
num =
when Str.toU64 str is
Ok a -> a
Err err ->
return Err err
Ok num
validateInput "123" If I comment out the type annotation, it compiles correctly. Of note is that the monomorphized early return branch of the
Which defines
The monomorphized early return branch in the annotated version is just
Without a type annotation, we extract the payloads for each tag union from the I'll keep investigating this, my suspicion is that we are doing something different in type-constraining untyped functions vs. typed functions and that leads us to handle pattern binding extraction differently between these two cases. |
Okay, it seems that in the above example, the return type is being inferred as |
Even more specifically, the type of values in early return statements are being totally ignored in type-annotated function definitions. The reason I thought that the return type was being inferred as Attempting to enable type unification with early returns now. |
dc9630b
to
f857872
Compare
This has been fixed and is ready for review, pending tests working in CI as they did on my local machine. |
When I initially implemented the return keyword, I only added type constraints to early returns in untyped closures because I misunderstood how typed closures/functions were constrained. This copies the code for early return constraints to the function type constraining code.
Note: if you annotate a function like
myFunction : Result U64 [MyErr] -> Result U64 [MyErr, Other]
, the error types will not properly unify, and you'll get a type error. This is a behavior that exists without early returns (I checked), and you can find discussion from Ayaz surrounding it here.