Skip to content

Commit 555d379

Browse files
committed
[GR-19220] Add Module#refinements (#3093)
PullRequest: truffleruby/3878
2 parents e6bee4f + af5b297 commit 555d379

File tree

7 files changed

+35
-5
lines changed

7 files changed

+35
-5
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
/CHANGELOG.md merge=union
44

5+
/spec/truffleruby.next-specs merge=union
6+
57
# Rules for GitHub's Linguist language-classification system. We're abusing the
68
# 'vendored' attribute to exclude files as a lot of this isn't really vendored,
79
# and a whole lot of actually vendored code isn't listed! What we want to do is

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Compatibility:
2626
* Add optional `timeout` argument to `Thread::Queue#pop` (#3039, @itarato).
2727
* Add optional `timeout` argument to `Thread::SizedsQueue#pop` (#3039, @itarato).
2828
* Handle `long long` and aliases in `Fiddle` (#3128, @eregon).
29+
* Add `Module#refinements` (#3039, @itarato).
2930

3031
Performance:
3132

spec/tags/truffle/methods_tags.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,4 @@ fails:Public methods on UnboundMethod should include private?
114114
fails:Public methods on UnboundMethod should include protected?
115115
fails:Public methods on UnboundMethod should include public?
116116
fails:Public methods on String should not include bytesplice
117+
fails:Public methods on Module should not include refinements

spec/truffleruby.next-specs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ spec/ruby/core/queue/shift_spec.rb
2929
spec/ruby/core/sizedqueue/deq_spec.rb
3030
spec/ruby/core/sizedqueue/pop_spec.rb
3131
spec/ruby/core/sizedqueue/shift_spec.rb
32+
33+
spec/ruby/core/module/refinements_spec.rb

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package org.truffleruby.core.module;
1111

1212
import java.util.ArrayList;
13+
import java.util.Collections;
1314
import java.util.HashMap;
1415
import java.util.HashSet;
1516
import java.util.LinkedHashSet;
@@ -37,7 +38,6 @@
3738
import org.truffleruby.builtins.CoreMethodArrayArgumentsNode;
3839
import org.truffleruby.builtins.CoreMethodNode;
3940
import org.truffleruby.annotations.CoreModule;
40-
import org.truffleruby.builtins.NonStandard;
4141
import org.truffleruby.annotations.Primitive;
4242
import org.truffleruby.builtins.PrimitiveArrayArgumentsNode;
4343
import org.truffleruby.builtins.PrimitiveNode;
@@ -2315,7 +2315,6 @@ protected RubyArray usedModules() {
23152315

23162316
}
23172317

2318-
@NonStandard
23192318
@CoreMethod(names = "used_refinements", onSingleton = true)
23202319
public abstract static class UsedRefinementsNode extends CoreMethodArrayArgumentsNode {
23212320

@@ -2326,15 +2325,24 @@ protected RubyArray usedRefinements() {
23262325
final DeclarationContext declarationContext = RubyArguments.getDeclarationContext(frame);
23272326
final Set<RubyModule> refinements = new HashSet<>();
23282327
for (RubyModule[] refinementModules : declarationContext.getRefinements().values()) {
2329-
for (RubyModule refinementModule : refinementModules) {
2330-
refinements.add(refinementModule);
2331-
}
2328+
Collections.addAll(refinements, refinementModules);
23322329
}
23332330
return createArray(refinements.toArray());
23342331
}
23352332

23362333
}
23372334

2335+
@CoreMethod(names = "refinements")
2336+
public abstract static class RefinementsNode extends CoreMethodArrayArgumentsNode {
2337+
2338+
@TruffleBoundary
2339+
@Specialization
2340+
protected RubyArray refinements(RubyModule self) {
2341+
return createArray(self.fields.getRefinements().values().toArray());
2342+
}
2343+
2344+
}
2345+
23382346
@GenerateUncached
23392347
@ImportStatic(ArrayGuards.class)
23402348
public abstract static class SetMethodVisibilityNode extends RubyBaseNode {

test/mri/excludes/TestRefinement.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
exclude :test_unbound_refine_method, "needs investigation"
1111
exclude :test_ancestors, "[ruby-core:86949] [Bug #14744]."
1212
exclude :test_import_methods, "NoMethodError: undefined method `bar' for #<TestRefinement::TestImport::A:0x17bee78>"
13+
exclude :test_refinements, "TruffleRuby does not guarantee refinement list ordering"

test/mri/tests/ruby/test_refinement.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,6 +1835,21 @@ def test_used_modules
18351835
assert_equal [ref::RefB, ref::RefA], ref::Combined::USED_MODS
18361836
end
18371837

1838+
def test_refinements
1839+
int_refinement = nil
1840+
str_refinement = nil
1841+
m = Module.new {
1842+
refine Integer do
1843+
int_refinement = self
1844+
end
1845+
1846+
refine String do
1847+
str_refinement = self
1848+
end
1849+
}
1850+
assert_equal([int_refinement, str_refinement], m.refinements)
1851+
end
1852+
18381853
def test_warn_setconst_in_refinmenet
18391854
bug10103 = '[ruby-core:64143] [Bug #10103]'
18401855
warnings = [

0 commit comments

Comments
 (0)