Skip to content

Commit 6c950ba

Browse files
Reject invalid operator names for implicit object calls (#15526)
1 parent 976507c commit 6c950ba

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

spec/compiler/parser/parser_spec.cr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,6 +1513,11 @@ module Crystal
15131513
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?`)"
15141514
assert_syntax_error "case 1\nin _;", "'when _' is not supported"
15151515

1516+
atomic_methods = Crystal::Parser::AtomicWithMethodCheck.join(", ")
1517+
assert_syntax_error "case 1\nwhen .=(2)", "expecting any of these tokens: #{atomic_methods} (not '=')"
1518+
assert_syntax_error "case 1\nwhen .+=(2)", "expecting any of these tokens: #{atomic_methods} (not '+=')"
1519+
assert_syntax_error "case 1\nwhen .&&(2)", "expecting any of these tokens: #{atomic_methods} (not '&&')"
1520+
15161521
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)
15171522

15181523
it_parses "select\nwhen foo\n2\nend", Select.new([When.new("foo".call, 2.int32)])

src/compiler/crystal/syntax/parser.cr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4238,6 +4238,8 @@ module Crystal
42384238
end_location = token_end_location
42394239
doc = @token.doc
42404240

4241+
check AtomicWithMethodCheck
4242+
42414243
if @token.type.op_bang?
42424244
# only trigger from `parse_when_expression`
42434245
obj = Var.new("self").at(location)

0 commit comments

Comments
 (0)