Skip to content

Commit 1d94a9e

Browse files
committed
Fix handling of PM_CONSTANT_PATH_NODE node in keyword arguments with ARGS_SPLAT
This was handled correctly in parse.y (NODE_COLON2), but not in prism. This wasn't caught earlier, because I only added tests for the optimized case and not the unoptimized case. Add tests for the unoptimized case. In code terms: ```ruby m(*a, kw: lvar::X) # Does not require allocation for *a m(*a, kw: method()::X) # Requires allocation for *a ``` This commit fixes the second case when prism is used.
1 parent 112ba70 commit 1d94a9e

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

prism_compile.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1855,7 +1855,6 @@ pm_setup_args_dup_rest_p(const pm_node_t *node)
18551855
switch (PM_NODE_TYPE(node)) {
18561856
case PM_BACK_REFERENCE_READ_NODE:
18571857
case PM_CLASS_VARIABLE_READ_NODE:
1858-
case PM_CONSTANT_PATH_NODE:
18591858
case PM_CONSTANT_READ_NODE:
18601859
case PM_FALSE_NODE:
18611860
case PM_FLOAT_NODE:
@@ -1874,6 +1873,13 @@ pm_setup_args_dup_rest_p(const pm_node_t *node)
18741873
case PM_SYMBOL_NODE:
18751874
case PM_TRUE_NODE:
18761875
return false;
1876+
case PM_CONSTANT_PATH_NODE: {
1877+
const pm_constant_path_node_t *cast = (const pm_constant_path_node_t *) node;
1878+
if (cast->parent != NULL) {
1879+
return pm_setup_args_dup_rest_p(cast->parent);
1880+
}
1881+
return false;
1882+
}
18771883
case PM_IMPLICIT_NODE:
18781884
return pm_setup_args_dup_rest_p(((const pm_implicit_node_t *) node)->value);
18791885
default:

test/ruby/test_allocation.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -781,14 +781,16 @@ def self.r2k(*a#{block}); end
781781
def test_no_array_allocation_with_splat_and_nonstatic_keywords
782782
check_allocations(<<~RUBY)
783783
def self.keyword(a: nil, b: nil#{block}); end
784+
def self.Object; Object end
784785
785786
check_allocations(0, 1, "keyword(*nil, a: empty_array#{block})") # LVAR
786787
check_allocations(0, 1, "keyword(*empty_array, a: empty_array#{block})") # LVAR
787788
check_allocations(0, 1, "->{keyword(*empty_array, a: empty_array#{block})}.call") # DVAR
788789
check_allocations(0, 1, "$x = empty_array; keyword(*empty_array, a: $x#{block})") # GVAR
789790
check_allocations(0, 1, "@x = empty_array; keyword(*empty_array, a: @x#{block})") # IVAR
790791
check_allocations(0, 1, "self.class.const_set(:X, empty_array); keyword(*empty_array, a: X#{block})") # CONST
791-
check_allocations(0, 1, "keyword(*empty_array, a: Object::X#{block})") # COLON2
792+
check_allocations(0, 1, "keyword(*empty_array, a: Object::X#{block})") # COLON2 - safe
793+
check_allocations(1, 1, "keyword(*empty_array, a: Object()::X#{block})") # COLON2 - unsafe
792794
check_allocations(0, 1, "keyword(*empty_array, a: ::X#{block})") # COLON3
793795
check_allocations(0, 1, "T = self; #{'B = block' unless block.empty?}; class Object; @@x = X; T.keyword(*X, a: @@x#{', &B' unless block.empty?}) end") # CVAR
794796
check_allocations(0, 1, "keyword(*empty_array, a: empty_array, b: 1#{block})") # INTEGER
@@ -850,13 +852,15 @@ def test_no_array_allocation_with_splat_and_nonstatic_keywords
850852

851853
check_allocations(<<~RUBY)
852854
keyword = keyword = proc{ |a: nil, b: nil #{block}| }
855+
def self.Object; Object end
853856
854857
check_allocations(0, 1, "keyword.(*empty_array, a: empty_array#{block})") # LVAR
855858
check_allocations(0, 1, "->{keyword.(*empty_array, a: empty_array#{block})}.call") # DVAR
856859
check_allocations(0, 1, "$x = empty_array; keyword.(*empty_array, a: $x#{block})") # GVAR
857860
check_allocations(0, 1, "@x = empty_array; keyword.(*empty_array, a: @x#{block})") # IVAR
858861
check_allocations(0, 1, "self.class.const_set(:X, empty_array); keyword.(*empty_array, a: X#{block})") # CONST
859-
check_allocations(0, 1, "keyword.(*empty_array, a: Object::X#{block})") # COLON2
862+
check_allocations(0, 1, "keyword.(*empty_array, a: Object::X#{block})") # COLON2 - safe
863+
check_allocations(1, 1, "keyword.(*empty_array, a: Object()::X#{block})") # COLON2 - unsafe
860864
check_allocations(0, 1, "keyword.(*empty_array, a: ::X#{block})") # COLON3
861865
check_allocations(0, 1, "T = keyword; #{'B = block' unless block.empty?}; class Object; @@x = X; T.(*X, a: @@x#{', &B' unless block.empty?}) end") # CVAR
862866
check_allocations(0, 1, "keyword.(*empty_array, a: empty_array, b: 1#{block})") # INTEGER

0 commit comments

Comments
 (0)