Skip to content

Commit dc3c05f

Browse files
committed
Fix import_methods supports methods with super
1 parent 73afc4b commit dc3c05f

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Bug fixes:
4646
* Fix `Monitor#exit` to raise `ThreadError` when monitor not owned by the current thread (#2922, @andrykonchin).
4747
* Fix `MatchData#[]` to support negative `length` argument (#2929, @andrykonchin).
4848
* Fix `IO` line reading calls when using a multi-byte delimiter (`IO#{each,gets,readline,readlines,etc.}) (#2961, @vinistock, @nirvdrum).
49+
* Fix `Refinement#import_methods` to supports module methods with super (#2971, @horakivo).
4950

5051
Compatibility:
5152

spec/ruby/core/refinement/import_methods_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,31 @@ def self.indent(level)
219219
end
220220
end
221221

222+
it "imports module methods with super" do
223+
class_to_refine = Class.new do
224+
def foo(number)
225+
2 * number
226+
end
227+
end
228+
229+
extension = Module.new do
230+
def foo(number)
231+
super * 2
232+
end
233+
end
234+
235+
refinement = Module.new do
236+
refine class_to_refine do
237+
import_methods extension
238+
end
239+
end
240+
241+
Module.new do
242+
using refinement
243+
class_to_refine.new.foo(2).should == 8
244+
end
245+
end
246+
222247
context "when methods are not defined in Ruby code" do
223248
it "raises ArgumentError" do
224249
Module.new do

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2489,7 +2489,7 @@ private String createErrorMessage(InternalMethod method, RubyModule module) {
24892489
private void importMethodsFromModuleToRefinement(RubyModule module, RubyModule refinement) {
24902490
var declarationContext = createDeclarationContextWithRefinement(refinement);
24912491
for (InternalMethod methodToCopy : module.fields.getMethods()) {
2492-
var clonedMethod = cloneMethod(methodToCopy, declarationContext);
2492+
var clonedMethod = cloneMethod(methodToCopy, declarationContext, refinement);
24932493
refinement.fields.addMethod(getContext(), this, clonedMethod);
24942494
}
24952495
}
@@ -2514,9 +2514,11 @@ private DeclarationContext createDeclarationContextWithRefinement(RubyModule ref
25142514
refinements);
25152515
}
25162516

2517-
private InternalMethod cloneMethod(InternalMethod method, DeclarationContext declarationContext) {
2517+
private InternalMethod cloneMethod(InternalMethod method, DeclarationContext declarationContext,
2518+
RubyModule refinement) {
25182519
var clonedCallTarget = cloneCallTarget(method);
2519-
return method.withCallTargetAndDeclarationContext(clonedCallTarget, declarationContext);
2520+
return method.withCallTargetAndDeclarationContextAndDeclarationModule(clonedCallTarget, declarationContext,
2521+
refinement);
25202522
}
25212523

25222524
private RootCallTarget cloneCallTarget(InternalMethod method) {

src/main/java/org/truffleruby/language/methods/InternalMethod.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,17 +384,18 @@ public InternalMethod withDeclarationContext(DeclarationContext newDeclarationCo
384384
}
385385
}
386386

387-
public InternalMethod withCallTargetAndDeclarationContext(RootCallTarget rootCallTarget,
388-
DeclarationContext newDeclarationContext) {
389-
if (rootCallTarget == this.callTarget && newDeclarationContext == declarationContext) {
387+
public InternalMethod withCallTargetAndDeclarationContextAndDeclarationModule(RootCallTarget rootCallTarget,
388+
DeclarationContext newDeclarationContext, RubyModule newDeclaringModule) {
389+
if (rootCallTarget == this.callTarget && newDeclarationContext == declarationContext &&
390+
newDeclaringModule == this.declaringModule) {
390391
return this;
391392
} else {
392393
return new InternalMethod(
393394
sharedMethodInfo,
394395
lexicalScope,
395396
newDeclarationContext,
396397
name,
397-
declaringModule,
398+
newDeclaringModule,
398399
owner,
399400
visibility,
400401
undefined,

0 commit comments

Comments
 (0)