Skip to content

Commit f5eaf06

Browse files
committed
[GR-20433] Fix Struct#hash for keyword_init: true Struct.
PullRequest: truffleruby/1242
2 parents 97ec341 + c22784e commit f5eaf06

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Bug fixes:
5555
* Made `Kernel#caller_locations` accept a range argument, and return `nil` when appropriate.
5656
* Made `rb_respond_to` work with primitives (#1869, @chrisseaton).
5757
* Fixed issue with missing backtrace for `rescue $ERROR_INFO` (#1660).
58+
* Fixed `Struct#hash` for `keyword_init: true` `Struct`.
5859

5960
Compatibility:
6061

spec/ruby/core/struct/hash_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@
1919
car.hash.should == similar_car.hash
2020
end
2121

22+
it "returns different hashes for structs with different values" do
23+
s1 = StructClasses::Ruby.new('2.7.0', 'linux')
24+
s2 = StructClasses::Ruby.new('2.7.0', 'macos')
25+
s1.hash.should_not == s2.hash
26+
end
27+
28+
it "returns different hashes for structs with different values when using keyword_init: true" do
29+
key = :"1 non symbol member"
30+
struct_class = Struct.new(key, keyword_init: true)
31+
t1 = struct_class.new(key => 1)
32+
t2 = struct_class.new(key => 2)
33+
t1.hash.should_not == t2.hash
34+
end
35+
2236
it "allows for overriding methods in an included module" do
2337
mod = Module.new do
2438
def hash

src/main/ruby/truffleruby/core/struct.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,9 @@ def hash
309309
val = TrufflePrimitive.vm_hash_start(CLASS_SALT)
310310
val = TrufflePrimitive.vm_hash_update(val, size)
311311
return val if Thread.detect_outermost_recursion self do
312-
_attrs.each { |var| TrufflePrimitive.vm_hash_update(val, TrufflePrimitive.object_hidden_var_get(self, var).hash) }
312+
_attrs.each do |var|
313+
val = TrufflePrimitive.vm_hash_update(val, TrufflePrimitive.object_hidden_var_get(self, var).hash)
314+
end
313315
end
314316
TrufflePrimitive.vm_hash_end(val)
315317
end

0 commit comments

Comments
 (0)