Skip to content

Commit 04fd96a

Browse files
committed
[GR-40333] module#private, #public, #protected, #module_function now returns their arguments
PullRequest: truffleruby/3642
2 parents 38ac43b + 3e20014 commit 04fd96a

File tree

7 files changed

+28
-25
lines changed

7 files changed

+28
-25
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Compatibility:
7474
* Fix `Array` methods `reject`, `reject!`, `inject`, `map`, `select`, `each_index` and handle a case when array is modified by a passed block like CRuby does (#2822, @andrykonchin, @eregon).
7575
* Fix `Array` methods `select!` and `keep_if` and handle a case when exception is raised in a passed block properly (@andrykonchin).
7676
* Fix `Enumerable` methods `each_cons` and `each_slice` to return receiver (#2733, @horakivo)
77+
* `Module` methods `#private`, `#public`, `#protected`, `#module_function` now returns their arguments like in CRuby 3.1 (#2733, @horakivo)
7778

7879
Performance:
7980

spec/tags/core/module/module_function_tags.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
fails:Module#private continues to allow a prepended module method to call super
2-
fails:Module#private returns argument or arguments if given

spec/tags/core/module/protected_tags.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

spec/tags/core/module/public_tags.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
fails:Module#public on a superclass method calls the redefined method
2-
fails:Module#public returns argument or arguments if given

src/main/java/org/truffleruby/core/cast/SingleValueCastNode.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
*/
1010
package org.truffleruby.core.cast;
1111

12+
import com.oracle.truffle.api.dsl.GenerateUncached;
1213
import org.truffleruby.core.array.RubyArray;
1314
import org.truffleruby.language.RubyBaseNode;
1415
import org.truffleruby.language.RubyGuards;
1516

1617
import com.oracle.truffle.api.dsl.ImportStatic;
1718
import com.oracle.truffle.api.dsl.Specialization;
1819

20+
@GenerateUncached
1921
@ImportStatic(value = { RubyGuards.class })
2022
public abstract class SingleValueCastNode extends RubyBaseNode {
2123

src/main/java/org/truffleruby/core/module/ModuleNodes.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.truffleruby.core.array.RubyArray;
4848
import org.truffleruby.core.cast.BooleanCastWithDefaultNode;
4949
import org.truffleruby.core.cast.NameToJavaStringNode;
50+
import org.truffleruby.core.cast.SingleValueCastNode;
5051
import org.truffleruby.core.cast.ToIntNode;
5152
import org.truffleruby.core.cast.ToPathNodeGen;
5253
import org.truffleruby.core.cast.ToStrNode;
@@ -1682,21 +1683,22 @@ protected boolean isMethodDefined(RubyModule module, String name, boolean inheri
16821683
@CoreMethod(names = "module_function", rest = true, visibility = Visibility.PRIVATE, alwaysInlined = true)
16831684
public abstract static class ModuleFunctionNode extends AlwaysInlinedMethodNode {
16841685
@Specialization(guards = "names.length == 0")
1685-
protected RubyModule frame(Frame callerFrame, RubyModule module, Object[] rubyArgs, RootCallTarget target,
1686+
protected Object frame(Frame callerFrame, RubyModule module, Object[] rubyArgs, RootCallTarget target,
16861687
@Bind("getPositionalArguments(rubyArgs, false)") Object[] names,
16871688
@Cached BranchProfile errorProfile) {
16881689
checkNotClass(module, errorProfile);
16891690
needCallerFrame(callerFrame, "Module#module_function with no arguments");
16901691
DeclarationContext.setCurrentVisibility(callerFrame, Visibility.MODULE_FUNCTION);
1691-
return module;
1692+
return nil;
16921693
}
16931694

16941695
@Specialization(guards = "names.length > 0")
1695-
protected RubyModule methods(Frame callerFrame, RubyModule module, Object[] rubyArgs, RootCallTarget target,
1696+
protected Object methods(Frame callerFrame, RubyModule module, Object[] rubyArgs, RootCallTarget target,
16961697
@Bind("getPositionalArguments(rubyArgs, false)") Object[] names,
16971698
@Cached SetMethodVisibilityNode setMethodVisibilityNode,
16981699
@Cached BranchProfile errorProfile,
1699-
@Cached LoopConditionProfile loopProfile) {
1700+
@Cached LoopConditionProfile loopProfile,
1701+
@Cached SingleValueCastNode singleValueCastNode) {
17001702
checkNotClass(module, errorProfile);
17011703
int i = 0;
17021704
try {
@@ -1707,7 +1709,7 @@ protected RubyModule methods(Frame callerFrame, RubyModule module, Object[] ruby
17071709
} finally {
17081710
profileAndReportLoopCount(loopProfile, i);
17091711
}
1710-
return module;
1712+
return singleValueCastNode.executeSingleValue(names);
17111713
}
17121714

17131715
private void checkNotClass(RubyModule module, BranchProfile errorProfile) {
@@ -1769,21 +1771,22 @@ protected RubyArray nesting() {
17691771
@CoreMethod(names = "public", rest = true, visibility = Visibility.PRIVATE, alwaysInlined = true)
17701772
public abstract static class PublicNode extends AlwaysInlinedMethodNode {
17711773
@Specialization(guards = "names.length == 0")
1772-
protected RubyModule frame(Frame callerFrame, RubyModule module, Object[] rubyArgs, RootCallTarget target,
1774+
protected Object frame(Frame callerFrame, RubyModule module, Object[] rubyArgs, RootCallTarget target,
17731775
@Bind("getPositionalArguments(rubyArgs, false)") Object[] names) {
17741776
needCallerFrame(callerFrame, "Module#public with no arguments");
17751777
DeclarationContext.setCurrentVisibility(callerFrame, Visibility.PUBLIC);
1776-
return module;
1778+
return nil;
17771779
}
17781780

17791781
@Specialization(guards = "names.length > 0")
1780-
protected RubyModule methods(Frame callerFrame, RubyModule module, Object[] rubyArgs, RootCallTarget target,
1782+
protected Object methods(Frame callerFrame, RubyModule module, Object[] rubyArgs, RootCallTarget target,
17811783
@Bind("getPositionalArguments(rubyArgs, false)") Object[] names,
1782-
@Cached SetMethodVisibilityNode setMethodVisibilityNode) {
1784+
@Cached SetMethodVisibilityNode setMethodVisibilityNode,
1785+
@Cached SingleValueCastNode singleValueCastNode) {
17831786
for (Object name : names) {
17841787
setMethodVisibilityNode.execute(module, name, Visibility.PUBLIC);
17851788
}
1786-
return module;
1789+
return singleValueCastNode.executeSingleValue(names);
17871790
}
17881791
}
17891792

@@ -1810,21 +1813,22 @@ protected RubyModule publicClassMethod(RubyModule module, Object[] names) {
18101813
@CoreMethod(names = "private", rest = true, visibility = Visibility.PRIVATE, alwaysInlined = true)
18111814
public abstract static class PrivateNode extends AlwaysInlinedMethodNode {
18121815
@Specialization(guards = "names.length == 0")
1813-
protected RubyModule frame(Frame callerFrame, RubyModule module, Object[] rubyArgs, RootCallTarget target,
1816+
protected Object frame(Frame callerFrame, RubyModule module, Object[] rubyArgs, RootCallTarget target,
18141817
@Bind("getPositionalArguments(rubyArgs, false)") Object[] names) {
18151818
needCallerFrame(callerFrame, "Module#private with no arguments");
18161819
DeclarationContext.setCurrentVisibility(callerFrame, Visibility.PRIVATE);
1817-
return module;
1820+
return nil;
18181821
}
18191822

18201823
@Specialization(guards = "names.length > 0")
1821-
protected RubyModule methods(Frame callerFrame, RubyModule module, Object[] rubyArgs, RootCallTarget target,
1824+
protected Object methods(Frame callerFrame, RubyModule module, Object[] rubyArgs, RootCallTarget target,
18221825
@Bind("getPositionalArguments(rubyArgs, false)") Object[] names,
1823-
@Cached SetMethodVisibilityNode setMethodVisibilityNode) {
1826+
@Cached SetMethodVisibilityNode setMethodVisibilityNode,
1827+
@Cached SingleValueCastNode singleValueCastNode) {
18241828
for (Object name : names) {
18251829
setMethodVisibilityNode.execute(module, name, Visibility.PRIVATE);
18261830
}
1827-
return module;
1831+
return singleValueCastNode.executeSingleValue(names);
18281832
}
18291833
}
18301834

@@ -2114,21 +2118,22 @@ protected RubyModule publicConstant(RubyModule module, Object[] args) {
21142118
@CoreMethod(names = "protected", rest = true, visibility = Visibility.PRIVATE, alwaysInlined = true)
21152119
public abstract static class ProtectedNode extends AlwaysInlinedMethodNode {
21162120
@Specialization(guards = "names.length == 0")
2117-
protected RubyModule frame(Frame callerFrame, RubyModule module, Object[] rubyArgs, RootCallTarget target,
2121+
protected Object frame(Frame callerFrame, RubyModule module, Object[] rubyArgs, RootCallTarget target,
21182122
@Bind("getPositionalArguments(rubyArgs, false)") Object[] names) {
21192123
needCallerFrame(callerFrame, "Module#protected with no arguments");
21202124
DeclarationContext.setCurrentVisibility(callerFrame, Visibility.PROTECTED);
2121-
return module;
2125+
return nil;
21222126
}
21232127

21242128
@Specialization(guards = "names.length > 0")
2125-
protected RubyModule methods(Frame callerFrame, RubyModule module, Object[] rubyArgs, RootCallTarget target,
2129+
protected Object methods(Frame callerFrame, RubyModule module, Object[] rubyArgs, RootCallTarget target,
21262130
@Bind("getPositionalArguments(rubyArgs, false)") Object[] names,
2127-
@Cached SetMethodVisibilityNode setMethodVisibilityNode) {
2131+
@Cached SetMethodVisibilityNode setMethodVisibilityNode,
2132+
@Cached SingleValueCastNode singleValueCastNode) {
21282133
for (Object name : names) {
21292134
setMethodVisibilityNode.execute(module, name, Visibility.PROTECTED);
21302135
}
2131-
return module;
2136+
return singleValueCastNode.executeSingleValue(names);
21322137
}
21332138
}
21342139

0 commit comments

Comments
 (0)