Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow foo.bar(&block) do end #14826

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
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
Loading