Skip to content

Commit

Permalink
Allow calling #[]= with a block using method syntax (#14161)
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil authored Feb 2, 2024
1 parent 3def4de commit 54f185d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
7 changes: 7 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1999,6 +1999,13 @@ describe Crystal::Formatter do
assert_format "foo.[] = 1"
assert_format "foo.[1, 2] = 3"

%w(<= == >= != []= ===).each do |operator|
assert_format "1.#{operator} { 3 }"
assert_format "1.#{operator}() { 3 }"
assert_format "1.#{operator}(2) { 3 }"
assert_format "1.#{operator} do\nend"
end

assert_format "@foo : Int32 # comment\n\ndef foo\nend"
assert_format "getter foo # comment\n\ndef foo\nend"
assert_format "getter foo : Int32 # comment\n\ndef foo\nend"
Expand Down
6 changes: 3 additions & 3 deletions spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,6 @@ 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 "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|
Expand Down Expand Up @@ -630,10 +628,12 @@ module Crystal
it_parses "->Foo.#{op}(Int32)", ProcPointer.new("Foo".path, op, ["Int32".path] of ASTNode)
end

["bar", "+", "-", "*", "/", "<", "<=", "==", ">", ">=", "%", "|", "&", "^", "**", "===", "=~", "!~"].each do |name|
["bar", "+", "-", "*", "/", "<", "<=", "==", ">", ">=", "%", "|", "&", "^", "**", "===", "=~", "!=", "[]=", "!~"].each do |name|
it_parses "foo.#{name}", Call.new("foo".call, name)
it_parses "foo.#{name} 1, 2", Call.new("foo".call, name, 1.int32, 2.int32)
it_parses "foo.#{name}(1, 2)", Call.new("foo".call, name, 1.int32, 2.int32)
it_parses "foo.#{name}(1, 2) { 3 }", Call.new("foo".call, name, args: [1.int32, 2.int32] of ASTNode, block: Block.new(body: 3.int32))
it_parses "foo.#{name} do end", Call.new("foo".call, name, block: Block.new)
end

["+", "-", "*", "/", "//", "%", "|", "&", "^", "**", "<<", ">>", "&+", "&-", "&*"].each do |op|
Expand Down
6 changes: 0 additions & 6 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -788,12 +788,6 @@ module Crystal
end

block = parse_block(block, stop_on_do: @stop_on_do)
if block || block_arg
if name == "[]="
raise "setter method '[]=' cannot be called with a block"
end
end

atomic = Call.new atomic, name, (args || [] of ASTNode), block, block_arg, named_args
atomic.has_parentheses = has_parentheses
atomic.name_location = name_location
Expand Down

0 comments on commit 54f185d

Please sign in to comment.