Skip to content
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

Merged

Conversation

straight-shoota
Copy link
Member

@straight-shoota straight-shoota commented Oct 7, 2018

After #6367, an empty case statement (without when or else) would be normalized to just the condition. This means that case "foo"; else is normalized to "foo". But that's semantically incorrect. The return type of an empty case should just be nil because there is no branch that could return anything else.

This fix adds a nil after the condition when there are no when or else 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.

@@ -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 ""
Copy link
Member

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).

Copy link
Member Author

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.

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member

@sdogruyol sdogruyol left a 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 👍

@sdogruyol sdogruyol added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler labels Oct 8, 2018
@sdogruyol sdogruyol merged commit b53dcd9 into crystal-lang:master Oct 8, 2018
@sdogruyol sdogruyol added this to the 0.27.0 milestone Oct 8, 2018
@straight-shoota straight-shoota deleted the jm/fix/empty-case-type branch October 8, 2018 12:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants