-
-
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
Parser: error on duplicate case when condition #5036
Parser: error on duplicate case when condition #5036
Conversation
@@ -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 |
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.
What is this change about?
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.
It removes duplicate cases...
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.
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) |
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.
We need to test ranges at all (intersection with other values and ranges).
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.
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
.
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.
when 5..10
when 8
when 7..11
afaiu, all of these cases intersect and should be disallowed together.
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.
It works for me. 5..10
, 6
and 7..11
are all different AST nodes, so the duplicate error won't trigger.
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 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.
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.
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.
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 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) |
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.
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.
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.
add_when_exp
tests against when_exps
, not against when_conds
, I don't know if that's what you mean
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.
Oh, I misread it!! My mistake
#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