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

Drop 'when _' support #6150

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions spec/compiler/normalize/case_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,4 @@ describe "Normalize: case" do
it "normalizes case with multiple expressions and non-tuple" do
assert_expand_second "x, y = 1, 2; case {x, y}; when 1; 4; end", "if 1 === ({x, y})\n 4\nend"
end

it "normalizes case with single expressions with underscore" do
assert_expand_second "x = 1; case x; when _; 2; end", "if true\n 2\nend"
end
end
3 changes: 3 additions & 0 deletions spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1657,6 +1657,9 @@ describe "Parser" do
assert_syntax_error %(case x; when 1..2; 2; when 1..2; end), "duplicate when 1..2 in case"
assert_syntax_error %(case x; when /x/; 2; when /x/; end), "duplicate when /x/ in case"
assert_syntax_error %(case x; when X; 2; when X; end), "duplicate when X in case"
assert_syntax_error "case x; when _; end", "'when _' is not supported, use 'else' block instead"
assert_syntax_error "case x; when 1; when _; end", "'when _' is not supported, use 'else' block instead"
assert_syntax_error "case x; when 1, _; end", "'when _' is not supported, use 'else' block instead"

it_parses "%w{one two}", (["one".string, "two".string] of ASTNode).array_of(Path.global("String"))
it_parses "%w{one\ntwo}", (["one".string, "two".string] of ASTNode).array_of(Path.global("String"))
Expand Down
10 changes: 6 additions & 4 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2485,7 +2485,7 @@ module Crystal
tuple_elements = [] of ASTNode

while true
tuple_elements << parse_when_expression(cond)
tuple_elements << parse_when_expression(cond, single: false)
skip_space
if @token.type == :","
next_token_skip_space_or_newline
Expand All @@ -2505,7 +2505,7 @@ module Crystal
check :"}"
next_token_skip_space
else
exp = parse_when_expression(cond)
exp = parse_when_expression(cond, single: true)
when_conds << exp
add_when_exp(when_exps, exp)
skip_space
Expand All @@ -2515,7 +2515,7 @@ module Crystal
end
else
while true
exp = parse_when_expression(cond)
exp = parse_when_expression(cond, single: true)
when_conds << exp
add_when_exp(when_exps, exp)
skip_space
Expand Down Expand Up @@ -2609,7 +2609,7 @@ module Crystal
false
end

def parse_when_expression(cond)
def parse_when_expression(cond, single)
if cond && @token.type == :"."
next_token
call = parse_var_or_call(force_call: true)
Expand All @@ -2623,6 +2623,8 @@ module Crystal
raise "BUG: expected Call, RespondsTo, IsA, Cast or NilableCast"
end
call
elsif single && @token.type == :UNDERSCORE
raise "'when _' is not supported, use 'else' block instead"
else
parse_op_assign_no_control
end
Expand Down