Skip to content
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
5 changes: 5 additions & 0 deletions spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1513,6 +1513,11 @@ module Crystal
assert_syntax_error "case 1\nin .nil?; 2", "expression of exhaustive case (case ... in) must be a constant (like `IO::Memory`), a generic (like `Array(Int32)`), a bool literal (true or false), a nil literal (nil) or a question method (like `.red?`)"
assert_syntax_error "case 1\nin _;", "'when _' is not supported"

atomic_methods = Crystal::Parser::AtomicWithMethodCheck.join(", ")
assert_syntax_error "case 1\nwhen .=(2)", "expecting any of these tokens: #{atomic_methods} (not '=')"
assert_syntax_error "case 1\nwhen .+=(2)", "expecting any of these tokens: #{atomic_methods} (not '+=')"
assert_syntax_error "case 1\nwhen .&&(2)", "expecting any of these tokens: #{atomic_methods} (not '&&')"

it_parses "case 1; when 2 then /foo/; end", Case.new(1.int32, [When.new([2.int32] of ASTNode, RegexLiteral.new("foo".string))], else: nil, exhaustive: false)

it_parses "select\nwhen foo\n2\nend", Select.new([When.new("foo".call, 2.int32)])
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4238,6 +4238,8 @@ module Crystal
end_location = token_end_location
doc = @token.doc

check AtomicWithMethodCheck

if @token.type.op_bang?
# only trigger from `parse_when_expression`
obj = Var.new("self").at(location)
Expand Down
Loading