-
-
Notifications
You must be signed in to change notification settings - Fork 312
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
Implement the return
keyword
#7173
Conversation
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.
Parser changes LGTM
5080431
to
8a0cc10
Compare
return
keywordreturn
keyword
crates/reporting/src/error/type.rs
Outdated
alloc.concat([ | ||
alloc.reflow("But I need every "), | ||
alloc.keyword("return"), | ||
alloc.reflow(" statement in that function to return:"), |
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.
minor: this should probably say "but I need every branch of this function..."
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.
I see what you're getting at, but I think this is misleading when we have if
expressions/statements and early returns. For example, in this function:
myFunc = \x ->
y =
if x == 5 then
return "early return"
else
x + 2
if y == 4 then
(y, 1)
else
(y, 2)
Your suggested error would imply that the branches of the y
expression should both be (Num *, Num *)
even though the branches of y
are actually Num *
.
I'm not sure of a better way to express this, so I'll leave it for now.
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.
I'm not sure I follow, those are not the branches of the function, they are the branches of a def inside the function.
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.
I agree. I think since "branches" refers usually to if/when branches, referring to the function's "branches" is a little confusing I expect, even if I personally can figure out what you mean. It seems like the most precise phrasing would be "every place your function can return from should return XYZ". Would that be worse or better in your eyes?
We should probably add tests to |
I've added tests for |
4989178
to
346a2d9
Compare
@@ -715,7 +726,6 @@ pub fn canonicalize_expr<'a>( | |||
|
|||
let output = Output { | |||
references, | |||
tail_call: None, |
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.
These were removed during my attempts to fix tail calls, but didn't need to be. However, their default value is None
anyway, so I kept them out.
497c641
to
be363b1
Compare
be363b1
to
c19cfb0
Compare
@ayazhafiz I've responded to all of your comments, let me know if there's anything else you want to have fixed. P.S. I used all of your wording suggestions, but there are a couple articles different, I hope you don't mind. |
@ayazhafiz thanks for the approval! I had a one-line clippy issue that's been fixed, please re-approve when you get the chance. |
Closes #7104
This adds the
return
keyword to Roc, which allows returning early from anywhere in a function body. It unblocks the newtry
keyword, which is implemented usingreturn
: #7087Here's a demo of the feature:
Let's test this:
You'll notice that there's a static value that has a
return
in it. This PR marks those as warnings because we can still treat them as "early returns" to the end of the def block, pretty convenient that the implementation worked out that way!