Skip to content

Commit 7fbe4cd

Browse files
committed
Handle having not enough arguments in ReadPostArgumentNode
* Which can happen, e.g., for proc { |a,*b,c| p c }.call()
1 parent d9e08e1 commit 7fbe4cd

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/main/java/org/truffleruby/language/arguments/ReadPostArgumentNode.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
package org.truffleruby.language.arguments;
1111

12+
import com.oracle.truffle.api.profiles.ConditionProfile;
1213
import org.truffleruby.language.RubyContextSourceNode;
1314
import org.truffleruby.language.RubyGuards;
1415

@@ -18,27 +19,33 @@ public class ReadPostArgumentNode extends RubyContextSourceNode {
1819

1920
private final int indexFromCount;
2021
private final boolean keywordArguments;
21-
private final int minimumForKWargs;
22+
private final int required;
23+
private final ConditionProfile enoughArguments = ConditionProfile.create();
2224

23-
public ReadPostArgumentNode(int indexFromCount, boolean keywordArguments, int minimumForKWargs) {
25+
public ReadPostArgumentNode(int indexFromCount, boolean keywordArguments, int required) {
2426
this.indexFromCount = indexFromCount;
2527
this.keywordArguments = keywordArguments;
26-
this.minimumForKWargs = minimumForKWargs;
28+
this.required = required;
2729
}
2830

2931
@Override
3032
public Object execute(VirtualFrame frame) {
3133
int count = RubyArguments.getArgumentsCount(frame);
3234

33-
if (keywordArguments && count > minimumForKWargs) {
35+
if (keywordArguments && count > required) {
3436
final Object lastArgument = RubyArguments.getArgument(frame, count - 1);
3537
if (RubyGuards.isRubyHash(lastArgument)) {
3638
count--;
3739
}
3840
}
3941

40-
final int effectiveIndex = count - indexFromCount;
41-
return RubyArguments.getArgument(frame, effectiveIndex);
42+
if (enoughArguments.profile(count >= required)) {
43+
final int effectiveIndex = count - indexFromCount;
44+
return RubyArguments.getArgument(frame, effectiveIndex);
45+
} else {
46+
// CheckArityNode will prevent this case for methods & lambdas, but it is still possible for procs.
47+
return nil;
48+
}
4249
}
4350

4451
@Override

0 commit comments

Comments
 (0)