Skip to content

Commit

Permalink
Allow multiple parameters and blocks for operators ending in = (#14159
Browse files Browse the repository at this point in the history
)
  • Loading branch information
HertzDevil authored Jan 10, 2024
1 parent c3eb0eb commit b56c7d9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 7 additions & 1 deletion spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,16 @@ module Crystal
assert_syntax_error "def foo=(*args); end", "setter method 'foo=' cannot have more than one parameter"
assert_syntax_error "def foo=(**kwargs); end", "setter method 'foo=' cannot have more than one parameter"
assert_syntax_error "def foo=(&block); end", "setter method 'foo=' cannot have a block"
assert_syntax_error "def []=(&block); end", "setter method '[]=' cannot have a block"
assert_syntax_error "f.[]= do |a| end", "setter method '[]=' cannot be called with a block"
assert_syntax_error "f.[]= { |bar| }", "setter method '[]=' cannot be called with a block"

# #10397
%w(<= >= == != []= ===).each do |operator|
it_parses "def #{operator}(other, file = 1); end", Def.new(operator, ["other".arg, Arg.new("file", 1.int32)])
it_parses "def #{operator}(*args, **opts); end", Def.new(operator, ["args".arg], splat_index: 0, double_splat: "opts".arg)
it_parses "def #{operator}(*args, **opts, &); end", Def.new(operator, ["args".arg], splat_index: 0, double_splat: "opts".arg, block_arg: Arg.new(""), block_arity: 0)
end

# #5895, #6042, #5997
%w(
begin nil true false yield with abstract
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3658,8 +3658,8 @@ module Crystal

end_location = token_end_location

if name.ends_with?('=')
if name != "[]=" && (params.size > 1 || found_splat || found_double_splat)
if Lexer.setter?(name)
if params.size > 1 || found_splat || found_double_splat
raise "setter method '#{name}' cannot have more than one parameter"
elsif found_block
raise "setter method '#{name}' cannot have a block"
Expand Down

0 comments on commit b56c7d9

Please sign in to comment.