Skip to content

Commit f2d0b66

Browse files
committed
[GR-18163] UnboundMethod#hash is the same for the same method in subclass and superclass
PullRequest: truffleruby/3493
2 parents e306ad3 + 9ae3205 commit f2d0b66

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Compatibility:
5959
* Fix exception for `Fiddle::Handle.new` with a missing library (#2714, @eregon).
6060
* Fix arguments implicit type conversion for `BasicObject#instance_eval`, `Module#class_eval`, `Module#module_eval`, `Module#define_method` (@andrykonchin).
6161
* Raise `ArgumentError` unconditionally when `Proc.new` is called without a block argument (@andrykonchin).
62+
* Fix `UnboundMethod#hash` to not depend on a module it was retrieved from (#2728, @andrykonchin).
6263

6364
Performance:
6465

spec/ruby/core/unboundmethod/fixtures/classes.rb

+10
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,14 @@ def overridden; end
8484
class C < B
8585
def overridden; end
8686
end
87+
88+
module HashSpecs
89+
class SuperClass
90+
def foo
91+
end
92+
end
93+
94+
class SubClass < SuperClass
95+
end
96+
end
8797
end

spec/ruby/core/unboundmethod/hash_spec.rb

+7
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,11 @@
1212
to_s, inspect = Array.instance_method(:to_s), Array.instance_method(:inspect)
1313
to_s.hash.should == inspect.hash
1414
end
15+
16+
it "equals a hash of the same method in the superclass" do
17+
foo_in_superclass = UnboundMethodSpecs::HashSpecs::SuperClass.instance_method(:foo)
18+
foo = UnboundMethodSpecs::HashSpecs::SubClass.instance_method(:foo)
19+
20+
foo.hash.should == foo_in_superclass.hash
21+
end
1522
end

src/main/java/org/truffleruby/core/method/UnboundMethodNodes.java

-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ public abstract static class HashNode extends CoreMethodArrayArgumentsNode {
121121
protected long hash(RubyUnboundMethod rubyMethod) {
122122
final InternalMethod method = rubyMethod.method;
123123
long h = getContext().getHashing(this).start(method.getDeclaringModule().hashCode());
124-
h = Hashing.update(h, rubyMethod.origin.hashCode());
125124
h = Hashing.update(h, MethodNodes.hashInternalMethod(method));
126125
return Hashing.end(h);
127126
}

0 commit comments

Comments
 (0)