Skip to content

Commit

Permalink
Fix parser translator ast when using anonymous forwarding in blocks/l…
Browse files Browse the repository at this point in the history
…ambda

Blocks and lambdas inherit anonymous arguments from the method they are a part of.
They themselves don't allow to introduce new anonymous arguments.
While you can write this:
```rb
def foo(*)
  bar { |**| }
end
```
referecing the new parameter inside of the block will always be a syntax error.
  • Loading branch information
Earlopain committed Jan 5, 2025
1 parent 8f086ac commit 2cbd27e
Show file tree
Hide file tree
Showing 5 changed files with 637 additions and 474 deletions.
4 changes: 2 additions & 2 deletions lib/prism/translation/parser/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,7 @@ def visit_lambda_node(node)
false
)
end,
node.body&.accept(copy_compiler(forwarding: implicit_parameters ? [] : find_forwarding(parameters&.parameters))),
visit(node.body),
[node.closing, srange(node.closing_loc)]
)
end
Expand Down Expand Up @@ -2042,7 +2042,7 @@ def visit_block(call, block)
false
)
end,
block.body&.accept(copy_compiler(forwarding: implicit_parameters ? [] : find_forwarding(parameters&.parameters))),
visit(block.body),
token(block.closing_loc)
)
else
Expand Down
4 changes: 4 additions & 0 deletions test/prism/fixtures/lambda.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

-> foo: bar do end

def foo(*, **)
->() { bar(*, **) }
end

p{|a:
b|}

Expand Down
2 changes: 2 additions & 0 deletions test/prism/fixtures/methods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ def foo = 123

def a(*); b(*); end

def a(*, **); b { c(*, **) }; end

def a(...); b(...); end

def a(...); b(1, 2, ...); end
Expand Down
169 changes: 123 additions & 46 deletions test/prism/snapshots/lambda.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2cbd27e

Please sign in to comment.