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

Parser: error on duplicate case when condition #5036

Merged
merged 1 commit into from Sep 29, 2017
Merged

Parser: error on duplicate case when condition #5036

merged 1 commit into from Sep 29, 2017

Conversation

asterite
Copy link
Member

@asterite asterite commented Sep 26, 2017

#5011 was manually detected, but maybe it's better if the compiler can help us here :-)

Also removes some duplicate cases in the compiler's code and the standard library. You gotta love bootstrapping :-D

@@ -3992,7 +4034,7 @@ module Crystal
end
when :"{"
return nil unless allow_curly
when :CHAR, :STRING, :DELIMITER_START, :STRING_ARRAY_START, :SYMBOL_ARRAY_START, :NUMBER, :IDENT, :SYMBOL, :INSTANCE_VAR, :CLASS_VAR, :CONST, :GLOBAL, :"$~", :"$?", :GLOBAL_MATCH_DATA_INDEX, :REGEX, :"(", :"!", :"[", :"[]", :"+", :"-", :"~", :"&", :"->", :"{{", :__LINE__, :__END_LINE__, :__FILE__, :__DIR__, :UNDERSCORE
when :CHAR, :STRING, :DELIMITER_START, :STRING_ARRAY_START, :SYMBOL_ARRAY_START, :NUMBER, :IDENT, :SYMBOL, :INSTANCE_VAR, :CLASS_VAR, :CONST, :GLOBAL, :"$~", :"$?", :GLOBAL_MATCH_DATA_INDEX, :REGEX, :"(", :"!", :"[", :"[]", :"~", :"->", :"{{", :__LINE__, :__END_LINE__, :__FILE__, :__DIR__, :UNDERSCORE
Copy link
Contributor

@bew bew Sep 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this change about?

Copy link
Member Author

@asterite asterite Sep 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It removes duplicate cases...

Copy link
Contributor

@bew bew Sep 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooooh yes of course, didn't checked the lines above, I thought the other cases where in the same line ><

when_exp_constant?(exp.value)
when RangeLiteral
when_exp_constant?(exp.from) &&
when_exp_constant?(exp.to)
Copy link
Contributor

@akzhan akzhan Sep 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to test ranges at all (intersection with other values and ranges).

Copy link
Member Author

@asterite asterite Sep 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean? Here I just check that both ends are "constants" as well. The check for duplicates is in the includes? call in add_when_exp.

Copy link
Contributor

@akzhan akzhan Sep 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when 5..10
when 8
when 7..11

afaiu, all of these cases intersect and should be disallowed together.

Copy link
Member Author

@asterite asterite Sep 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works for me. 5..10, 6 and 7..11 are all different AST nodes, so the duplicate error won't trigger.

Copy link
Contributor

@akzhan akzhan Sep 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But they should trigger error.

Anyway when you enables these intersections, you must to check {from, to} as single value instead of two separated.

when 2..5
when 3..5
when 2..4

will fail now afaiu.

Copy link
Member Author

@asterite asterite Sep 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see what you mean. This change is just about detecting duplicates, what you ask is much more complex. Maybe it can be done for simple ranges like this, but we can do it in a separate PR.

Did you try that code? It compiles fine. I don't understand why you say it will fail now.

Copy link
Contributor

@akzhan akzhan Sep 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wrong, you are right here. But yes, intersection checks should be applied, but it's subject of another PR.

when_conds << parse_when_expression(cond)
exp = parse_when_expression(cond)
when_conds << exp
add_when_exp(when_exps, exp)
Copy link
Contributor

@lbguilherme lbguilherme Sep 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when_conds << exp shouldn't be done here, as it would cause a raise in add_when_exp. if when_exps.includes?(exp) will be true always.

Copy link
Member Author

@asterite asterite Sep 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add_when_exp tests against when_exps, not against when_conds, I don't know if that's what you mean

Copy link
Contributor

@lbguilherme lbguilherme Sep 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I misread it!! My mistake

RX14
RX14 approved these changes Sep 26, 2017
@RX14 RX14 added this to the Next milestone Sep 26, 2017
@asterite asterite merged commit c82606d into crystal-lang:master Sep 29, 2017
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants