-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Fix empty case statement normalizes to condition expression #6915
Fix empty case statement normalizes to condition expression #6915
Conversation
@@ -13,6 +13,14 @@ describe "Code gen: case" do | |||
run("require \"prelude\"; case 1; when 0; 2; else; 3; end").to_i.should eq(3) | |||
end | |||
|
|||
it "codegens case without whens" do | |||
run("require \"prelude\"; case 1; end").to_string.should eq "" |
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 it's a good idea to turn a nil
into a string like this... it probably works, but it's not clear why (I wouldn't add a test for this if the normalizer is already tested, and same for the test below).
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.
to_string
actually just returns @output
of Crystal::SpecRunOutput
which is already a string.
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.
But I can remove these specs, if their not considered particularly useful.
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.
Ah, you are right. You can leave them if you want.
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.
Thank you @straight-shoota 👍
After #6367, an empty case statement (without
when
orelse
) would be normalized to just the condition. This means thatcase "foo"; else
is normalized to"foo"
. But that's semantically incorrect. The return type of an emptycase
should just benil
because there is no branch that could return anything else.This fix adds a
nil
after the condition when there are nowhen
orelse
branches, so the example normalizes to"foo"; nil
.This PR also adds two codegen specs which are not necessary to document this fix, but they should have been added in #6367.