Skip to content

Commit

Permalink
[Bug #20423] Disallow anonymous block within argument forwarding
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Apr 12, 2024
1 parent 971b552 commit e369884
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 20 deletions.
2 changes: 1 addition & 1 deletion parse.y
Expand Up @@ -4263,7 +4263,7 @@ block_arg : tAMPER arg_value
}
| tAMPER
{
forwarding_arg_check(p, idFWD_BLOCK, 0, "block");
forwarding_arg_check(p, idFWD_BLOCK, idFWD_ALL, "block");
$$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$);
/*% ripper: Qnil %*/
}
Expand Down
1 change: 0 additions & 1 deletion prism/prism.c
Expand Up @@ -13486,7 +13486,6 @@ parse_parameters(
update_parameter_state(parser, &parser->current, &order);
parser_lex(parser);

parser->current_scope->parameters |= PM_SCOPE_PARAMETERS_FORWARDING_BLOCK;
parser->current_scope->parameters |= PM_SCOPE_PARAMETERS_FORWARDING_ALL;

pm_forwarding_parameter_node_t *param = pm_forwarding_parameter_node_create(parser, &parser->previous);
Expand Down
2 changes: 1 addition & 1 deletion test/prism/fixtures/methods.txt
Expand Up @@ -175,7 +175,7 @@ def f x:!a; end
def foo x:%(xx); end

def foo(...)
bar(&)
bar(...)
end

def foo(bar = (def baz(bar) = bar; 1)) = 2
Expand Down
17 changes: 9 additions & 8 deletions test/prism/snapshots/methods.txt
Expand Up @@ -1908,21 +1908,22 @@
│ │ │ @ ForwardingParameterNode (location: (177,8)-(177,11))
│ │ └── block: ∅
│ ├── body:
│ │ @ StatementsNode (location: (178,2)-(178,7))
│ │ @ StatementsNode (location: (178,2)-(178,10))
│ │ └── body: (length: 1)
│ │ └── @ CallNode (location: (178,2)-(178,7))
│ │ └── @ CallNode (location: (178,2)-(178,10))
│ │ ├── flags: ignore_visibility
│ │ ├── receiver: ∅
│ │ ├── call_operator_loc: ∅
│ │ ├── name: :bar
│ │ ├── message_loc: (178,2)-(178,5) = "bar"
│ │ ├── opening_loc: (178,5)-(178,6) = "("
│ │ ├── arguments: ∅
│ │ ├── closing_loc: (178,7)-(178,8) = ")"
│ │ └── block:
│ │ @ BlockArgumentNode (location: (178,6)-(178,7))
│ │ ├── expression: ∅
│ │ └── operator_loc: (178,6)-(178,7) = "&"
│ │ ├── arguments:
│ │ │ @ ArgumentsNode (location: (178,6)-(178,9))
│ │ │ ├── flags: ∅
│ │ │ └── arguments: (length: 1)
│ │ │ └── @ ForwardingArgumentsNode (location: (178,6)-(178,9))
│ │ ├── closing_loc: (178,9)-(178,10) = ")"
│ │ └── block: ∅
│ ├── locals: []
│ ├── def_keyword_loc: (177,0)-(177,3) = "def"
│ ├── operator_loc: ∅
Expand Down
9 changes: 0 additions & 9 deletions test/ruby/test_compile_prism.rb
Expand Up @@ -1839,15 +1839,6 @@ def o.bar(&) = foo(&)
o.bar { :ok }
RUBY

# Test anonymous block forwarding from argument forwarding
assert_prism_eval(<<~RUBY)
o = Object.new
def o.foo = yield
def o.bar(...) = foo(&)
o.bar { :ok }
RUBY
end

def test_BlockLocalVariableNode
Expand Down
1 change: 1 addition & 0 deletions test/ruby/test_syntax.rb
Expand Up @@ -190,6 +190,7 @@ def test_argument_forwarding_with_anon_rest_kwrest_and_block
assert_syntax_error("def f(...); g(0, *); end", /no anonymous rest parameter/)
assert_syntax_error("def f(...); g(**); end", /no anonymous keyword rest parameter/)
assert_syntax_error("def f(...); g(x: 1, **); end", /no anonymous keyword rest parameter/)
assert_syntax_error("def f(...); g(&); end", /no anonymous block parameter/)
end

def test_newline_in_block_parameters
Expand Down

0 comments on commit e369884

Please sign in to comment.