Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ module Crystal
assert_syntax_error "def foo!=; end", "unexpected token: !="
assert_syntax_error "def foo?=(x); end", "unexpected token: ?"

# #5856
assert_syntax_error "def foo=(a,b); end", "setter method 'foo=' cannot have more than one argument"
assert_syntax_error "def foo=(a = 1, b = 2); end", "setter method 'foo=' cannot have more than one argument"
assert_syntax_error "def foo=(*args); end", "setter method 'foo=' cannot have more than one argument"
assert_syntax_error "def foo=(**kwargs); end", "setter method 'foo=' cannot have more than one argument"
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"

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

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

atomic = Call.new atomic, name, (args || [] of ASTNode), block, block_arg, named_args, name_column_number: name_column_number
else
atomic = args ? (Call.new atomic, name, args, named_args: named_args, name_column_number: name_column_number) : (Call.new atomic, name, name_column_number: name_column_number)
Expand Down Expand Up @@ -3298,6 +3302,7 @@ module Crystal
found_default_value = false
found_splat = false
found_double_splat = nil
found_block = false

index = 0
splat_index = nil
Expand Down Expand Up @@ -3329,6 +3334,7 @@ module Crystal
if block_arg = extras.block_arg
compute_block_arg_yields block_arg
check :")"
found_block = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!block_arg.nil? == found_block

So the found_block var can be removed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, but I also see found_splat, etc., so it's fine to keep this variable 👍

break
elsif @token.type == :","
next_token_skip_space_or_newline
Expand All @@ -3341,6 +3347,14 @@ module Crystal
index += 1
end

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

if splat_index == args.size - 1 && args.last.name.empty?
raise "named arguments must follow bare *", args.last.location.not_nil!
end
Expand Down
10 changes: 6 additions & 4 deletions src/object.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1092,11 +1092,13 @@ class Object
{{object.id}}.{{method.id}}(*args, **options)
end

def {{method.id}}(*args, **options)
{{object.id}}.{{method.id}}(*args, **options) do |*yield_args|
yield *yield_args
{% if method.id != "[]=" %}
def {{method.id}}(*args, **options)
{{object.id}}.{{method.id}}(*args, **options) do |*yield_args|
yield *yield_args
end
end
end
{% end %}
{% end %}
{% end %}
end
Expand Down