Skip to content

Commit

Permalink
Reject invalid operator names for implicit object calls (pt. 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
nobodywasishere committed Feb 26, 2025
1 parent 6b3e658 commit 423397a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
7 changes: 5 additions & 2 deletions spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1512,8 +1512,11 @@ module Crystal
assert_syntax_error "case 1\nin 1; 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 .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"
assert_syntax_error "case 1\nwhen .=(2)", %(unexpected token: "=")
assert_syntax_error "case 1\nwhen .+=(2)", %(unexpected token: "+=")

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)

Expand Down
6 changes: 2 additions & 4 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4236,6 +4236,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 Expand Up @@ -4264,10 +4266,6 @@ module Crystal
# Not a special call, go on
end

if @token.type.op_eq? || @token.type.assignment_operator?
unexpected_token
end

name = @token.value.to_s
name_location = @token.location

Expand Down

0 comments on commit 423397a

Please sign in to comment.