Skip to content

Commit 47ec1d3

Browse files
committed
[GR-26542] Correctly handle post args in super calls.
PullRequest: truffleruby/2049
2 parents ec41098 + b5756b0 commit 47ec1d3

File tree

3 files changed

+70
-8
lines changed

3 files changed

+70
-8
lines changed

spec/ruby/language/fixtures/super.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,52 @@ def m_modified(a, b, *args)
455455
end
456456
end
457457

458+
module ZSuperWithRestAndPost
459+
class A
460+
def m(*args, a, b)
461+
args
462+
end
463+
464+
def m_modified(*args, a, b)
465+
args
466+
end
467+
end
468+
469+
class B < A
470+
def m(*args, a, b)
471+
super
472+
end
473+
474+
def m_modified(*args, a, b)
475+
args[1] = 14
476+
super
477+
end
478+
end
479+
end
480+
481+
module ZSuperWithRestOthersAndPost
482+
class A
483+
def m(a, *args, b)
484+
args
485+
end
486+
487+
def m_modified(a, *args, b)
488+
args
489+
end
490+
end
491+
492+
class B < A
493+
def m(a, *args, b)
494+
super
495+
end
496+
497+
def m_modified(a, *args, b)
498+
args[1] = 14
499+
super
500+
end
501+
end
502+
end
503+
458504
module ZSuperWithRestReassigned
459505
class A
460506
def a(*args)

spec/ruby/language/super_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,21 @@ def obj.foobar(array)
293293

294294
it "without explicit arguments passes arguments and rest arguments" do
295295
SuperSpecs::ZSuperWithRestAndOthers::B.new.m(1, 2, 3, 4, 5).should == [3, 4, 5]
296+
SuperSpecs::ZSuperWithRestAndOthers::B.new.m(1, 2).should == []
297+
end
298+
299+
it "without explicit arguments passes arguments, rest arguments, and post arguments" do
300+
SuperSpecs::ZSuperWithRestAndPost::B.new.m(1, 2, 3, 4, 5).should == [1, 2, 3]
301+
SuperSpecs::ZSuperWithRestOthersAndPost::B.new.m(1, 2, 3, 4, 5).should == [2, 3, 4]
302+
SuperSpecs::ZSuperWithRestAndPost::B.new.m(1, 2).should == []
303+
SuperSpecs::ZSuperWithRestOthersAndPost::B.new.m(1, 2).should == []
304+
end
305+
306+
it "without explicit arguments passes arguments, rest arguments including modifications, and post arguments" do
307+
SuperSpecs::ZSuperWithRestAndPost::B.new.m_modified(1, 2, 3, 4, 5).should == [1, 14, 3]
308+
SuperSpecs::ZSuperWithRestOthersAndPost::B.new.m_modified(1, 2, 3, 4, 5).should == [2, 14, 4]
309+
SuperSpecs::ZSuperWithRestAndPost::B.new.m_modified(1, 2).should == [nil, 14]
310+
SuperSpecs::ZSuperWithRestOthersAndPost::B.new.m_modified(1, 2).should == [nil, 14]
296311
end
297312

298313
it "without explicit arguments passes arguments and rest arguments including any modifications" do

src/main/java/org/truffleruby/parser/ReloadArgumentsTranslator.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.util.List;
1414

1515
import org.truffleruby.RubyContext;
16-
import org.truffleruby.RubyLanguage;
1716
import org.truffleruby.core.hash.ConcatHashLiteralNode;
1817
import org.truffleruby.core.hash.HashLiteralNode;
1918
import org.truffleruby.language.RubyNode;
@@ -62,8 +61,6 @@ public int getRestParameterIndex() {
6261

6362
@Override
6463
public RubyNode visitArgsNode(ArgsParseNode node) {
65-
final SourceIndexLength sourceSection = node.getPosition();
66-
6764
final List<RubyNode> sequence = new ArrayList<>();
6865
final ParseNode[] args = node.getArgs();
6966
final int preCount = node.getPreCount();
@@ -89,11 +86,15 @@ public RubyNode visitArgsNode(ArgsParseNode node) {
8986
sequence.add(node.getRestArgNode().accept(this));
9087
}
9188

92-
if (node.getPostCount() > 0) {
93-
RubyLanguage.LOGGER.warning(
94-
String.format(
95-
"post args in zsuper not yet implemented at %s%n",
96-
RubyContext.fileLine(sourceSection.toSourceSection(source))));
89+
int postCount = node.getPostCount();
90+
91+
if (postCount > 0) {
92+
index = -postCount;
93+
int postIndex = node.getPostIndex();
94+
for (int i = 0; i < postCount; i++) {
95+
sequence.add(args[postIndex + i].accept(this));
96+
index++;
97+
}
9798
}
9899

99100
RubyNode kwArgsNode = null;

0 commit comments

Comments
 (0)