diff --git a/spec/compiler/parser/parser_spec.cr b/spec/compiler/parser/parser_spec.cr index faee7516d728..1c78b323f1c3 100644 --- a/spec/compiler/parser/parser_spec.cr +++ b/spec/compiler/parser/parser_spec.cr @@ -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)]) diff --git a/src/compiler/crystal/syntax/parser.cr b/src/compiler/crystal/syntax/parser.cr index 7896aa2026a4..afc0e5bd5a96 100644 --- a/src/compiler/crystal/syntax/parser.cr +++ b/src/compiler/crystal/syntax/parser.cr @@ -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)