Skip to content

Commit 224ea85

Browse files
committed
Fix up CI
1 parent ff68c09 commit 224ea85

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

lib/prism/translation/parser/compiler.rb

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class CompilationError < StandardError
2525
# offsets in the file.
2626
attr_reader :offset_cache
2727

28-
# The locals in the current scope.
29-
attr_reader :locals
28+
# The types of values that can be forwarded in the current scope.
29+
attr_reader :forwarding
3030

3131
# Whether or not the current node is in a destructure.
3232
attr_reader :in_destructure
@@ -36,13 +36,13 @@ class CompilationError < StandardError
3636

3737
# Initialize a new compiler with the given parser, offset cache, and
3838
# options.
39-
def initialize(parser, offset_cache, locals: nil, in_destructure: false, in_pattern: false)
39+
def initialize(parser, offset_cache, forwarding: [], in_destructure: false, in_pattern: false)
4040
@parser = parser
4141
@builder = parser.builder
4242
@source_buffer = parser.source_buffer
4343
@offset_cache = offset_cache
4444

45-
@locals = locals
45+
@forwarding = forwarding
4646
@in_destructure = in_destructure
4747
@in_pattern = in_pattern
4848
end
@@ -135,7 +135,7 @@ def visit_assoc_node(node)
135135
# { **foo }
136136
# ^^^^^
137137
def visit_assoc_splat_node(node)
138-
if node.value.nil? && locals.include?(:**)
138+
if node.value.nil? && forwarding.include?(:**)
139139
builder.forwarded_kwrestarg(token(node.operator_loc))
140140
else
141141
builder.kwsplat(token(node.operator_loc), visit(node.value))
@@ -372,7 +372,7 @@ def visit_class_node(node)
372372
visit(node.constant_path),
373373
token(node.inheritance_operator_loc),
374374
visit(node.superclass),
375-
node.body&.accept(copy_compiler(locals: node.locals)),
375+
node.body&.accept(copy_compiler(forwarding: [])),
376376
token(node.end_keyword_loc)
377377
)
378378
end
@@ -519,6 +519,8 @@ def visit_constant_path_target_node(node)
519519
# def self.foo; end
520520
# ^^^^^^^^^^^^^^^^^
521521
def visit_def_node(node)
522+
forwarding = find_forwarding(node.parameters)
523+
522524
if node.equal_loc
523525
if node.receiver
524526
builder.def_endless_singleton(
@@ -528,15 +530,15 @@ def visit_def_node(node)
528530
token(node.name_loc),
529531
builder.args(token(node.lparen_loc), visit(node.parameters) || [], token(node.rparen_loc), false),
530532
token(node.equal_loc),
531-
node.body&.accept(copy_compiler(locals: node.locals))
533+
node.body&.accept(copy_compiler(forwarding: forwarding))
532534
)
533535
else
534536
builder.def_endless_method(
535537
token(node.def_keyword_loc),
536538
token(node.name_loc),
537539
builder.args(token(node.lparen_loc), visit(node.parameters) || [], token(node.rparen_loc), false),
538540
token(node.equal_loc),
539-
node.body&.accept(copy_compiler(locals: node.locals))
541+
node.body&.accept(copy_compiler(forwarding: forwarding))
540542
)
541543
end
542544
elsif node.receiver
@@ -546,15 +548,15 @@ def visit_def_node(node)
546548
token(node.operator_loc),
547549
token(node.name_loc),
548550
builder.args(token(node.lparen_loc), visit(node.parameters) || [], token(node.rparen_loc), false),
549-
node.body&.accept(copy_compiler(locals: node.locals)),
551+
node.body&.accept(copy_compiler(forwarding: forwarding)),
550552
token(node.end_keyword_loc)
551553
)
552554
else
553555
builder.def_method(
554556
token(node.def_keyword_loc),
555557
token(node.name_loc),
556558
builder.args(token(node.lparen_loc), visit(node.parameters) || [], token(node.rparen_loc), false),
557-
node.body&.accept(copy_compiler(locals: node.locals)),
559+
node.body&.accept(copy_compiler(forwarding: forwarding)),
558560
token(node.end_keyword_loc)
559561
)
560562
end
@@ -1008,7 +1010,7 @@ def visit_lambda_node(node)
10081010
else
10091011
builder.args(nil, [], nil, false)
10101012
end,
1011-
node.body&.accept(copy_compiler(locals: node.locals)),
1013+
node.body&.accept(copy_compiler(forwarding: find_forwarding(node.parameters&.parameters))),
10121014
[node.closing, srange(node.closing_loc)]
10131015
)
10141016
end
@@ -1103,7 +1105,7 @@ def visit_module_node(node)
11031105
builder.def_module(
11041106
token(node.module_keyword_loc),
11051107
visit(node.constant_path),
1106-
node.body&.accept(copy_compiler(locals: node.locals)),
1108+
node.body&.accept(copy_compiler(forwarding: [])),
11071109
token(node.end_keyword_loc)
11081110
)
11091111
end
@@ -1284,7 +1286,7 @@ def visit_pre_execution_node(node)
12841286

12851287
# The top-level program node.
12861288
def visit_program_node(node)
1287-
node.statements.accept(copy_compiler(locals: node.locals))
1289+
visit(node.statements)
12881290
end
12891291

12901292
# 0..5
@@ -1415,7 +1417,7 @@ def visit_singleton_class_node(node)
14151417
token(node.class_keyword_loc),
14161418
token(node.operator_loc),
14171419
visit(node.expression),
1418-
node.body&.accept(copy_compiler(locals: node.locals)),
1420+
node.body&.accept(copy_compiler(forwarding: [])),
14191421
token(node.end_keyword_loc)
14201422
)
14211423
end
@@ -1447,7 +1449,7 @@ def visit_source_line_node(node)
14471449
# def foo(*); bar(*); end
14481450
# ^
14491451
def visit_splat_node(node)
1450-
if node.expression.nil? && locals.include?(:*)
1452+
if node.expression.nil? && forwarding.include?(:*)
14511453
builder.forwarded_restarg(token(node.operator_loc))
14521454
elsif in_destructure
14531455
builder.restarg(token(node.operator_loc), token(node.expression&.location))
@@ -1658,8 +1660,23 @@ def visit_yield_node(node)
16581660

16591661
# Initialize a new compiler with the given option overrides, used to
16601662
# visit a subtree with the given options.
1661-
def copy_compiler(locals: self.locals, in_destructure: self.in_destructure, in_pattern: self.in_pattern)
1662-
Compiler.new(parser, offset_cache, locals: locals, in_destructure: in_destructure, in_pattern: in_pattern)
1663+
def copy_compiler(forwarding: self.forwarding, in_destructure: self.in_destructure, in_pattern: self.in_pattern)
1664+
Compiler.new(parser, offset_cache, forwarding: forwarding, in_destructure: in_destructure, in_pattern: in_pattern)
1665+
end
1666+
1667+
# When *, **, &, or ... are used as an argument in a method call, we
1668+
# check if they were allowed by the current context. To determine that
1669+
# we build this lookup table.
1670+
def find_forwarding(node)
1671+
return [] if node.nil?
1672+
1673+
forwarding = []
1674+
forwarding << :* if node.rest.is_a?(RestParameterNode) && node.rest.name.nil?
1675+
forwarding << :** if node.keyword_rest.is_a?(KeywordRestParameterNode) && node.keyword_rest.name.nil?
1676+
forwarding << :& if !node.block.nil? && node.block.name.nil?
1677+
forwarding |= [:&, :"..."] if node.keyword_rest.is_a?(ForwardingParameterNode)
1678+
1679+
forwarding
16631680
end
16641681

16651682
# Blocks can have a special set of parameters that automatically expand
@@ -1732,7 +1749,7 @@ def visit_block(call, block)
17321749
else
17331750
builder.args(nil, [], nil, false)
17341751
end,
1735-
visit(block.body),
1752+
block.body&.accept(copy_compiler(forwarding: find_forwarding(block.parameters&.parameters))),
17361753
token(block.closing_loc)
17371754
)
17381755
else

0 commit comments

Comments
 (0)