Skip to content

Commit

Permalink
Disallow foo.bar(&block) do end
Browse files Browse the repository at this point in the history
  • Loading branch information
FnControlOption committed Jul 22, 2024
1 parent 670543a commit de918ae
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,10 @@ module Crystal
it_parses "1.foo do; 1; end", Call.new(1.int32, "foo", block: Block.new(body: 1.int32))
it_parses "a b() {}", Call.new(nil, "a", Call.new(nil, "b", block: Block.new))

assert_syntax_error "foo(&block) {}"
assert_syntax_error "foo(&block) {}", "can't use captured and non-captured blocks together"
assert_syntax_error "foo(&block) do end", "can't use captured and non-captured blocks together"
assert_syntax_error "foo.bar(&block) {}", "can't use captured and non-captured blocks together"
assert_syntax_error "foo.bar(&block) do end", "can't use captured and non-captured blocks together"

it_parses "foo { |a, (b, c), (d, e)| a; b; c; d; e }", Call.new(nil, "foo",
block: Block.new(
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,11 @@ module Crystal
end

block = parse_block(block, stop_on_do: @stop_on_do)

if block && block_arg
raise "can't use captured and non-captured blocks together", location
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 de918ae

Please sign in to comment.