Skip to content

Fix SortedSet initialization and equality on Ruby 4.0 - #20

Open
joshuay03 wants to merge 1 commit into
knu:masterfrom
joshuay03:fix-initialize-with-sorted-set
Open

Fix SortedSet initialization and equality on Ruby 4.0#20
joshuay03 wants to merge 1 commit into
knu:masterfrom
joshuay03:fix-initialize-with-sorted-set

Conversation

@joshuay03

@joshuay03 joshuay03 commented Apr 16, 2026

Copy link
Copy Markdown

Problem

Ruby 4 stores Set elements in C, while SortedSet keeps its contents in a separate backing tree. The built-in Set fast paths therefore cannot see SortedSet's logical contents: copying another SortedSet produces an empty set, and equality can compare the wrong storage.

Fix

Copy construction now duplicates the backing tree when the source has the same class and no transformation block. Transforming initializers continue to enumerate the source, and copies remain independent and mutable.

SortedSet#== now compares sorted sets by backing-tree semantics and ordinary sets through comparator-based membership. #eql? remains exact-class and element-eql? based to preserve correct Hash behavior. A narrow Set override restores comparisons when an ordinary Set is the receiver without changing comparisons between ordinary sets. JRuby receives equivalent behavior, including a comparator fallback when TreeSet rejects otherwise Ruby-comparable values.

Tested on Ruby 2.7.8, 3.4.10, 4.0.6, and JRuby 10.1.1.0.

@joshuay03 joshuay03 moved this to In Progress / Pending Review in Open Source Apr 16, 2026
@joshuay03

Copy link
Copy Markdown
Author

Bump 🙏🏽

@knu knu left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense to me.

Comment thread lib/sorted_set/ruby4.rb Outdated
@joshuay03
joshuay03 force-pushed the fix-initialize-with-sorted-set branch from 0053dd7 to 3bfb949 Compare May 14, 2026 00:53
@joshuay03
joshuay03 requested a review from knu May 14, 2026 00:54
@joshuay03

Copy link
Copy Markdown
Author

Bumping again 🙏🏽

@joshuay03

Copy link
Copy Markdown
Author

Bumping once more 🙏🏽

@joshuay03
joshuay03 force-pushed the fix-initialize-with-sorted-set branch from 3bfb949 to 66cca55 Compare August 18, 2026 03:23
@joshuay03 joshuay03 changed the title Fix SortedSet.new(sorted_set) on Ruby 4.0 Fix SortedSet.new(sorted_set) and hash-key lookups on Ruby 4.0 Aug 18, 2026
@mergify

mergify Bot commented Aug 18, 2026

Copy link
Copy Markdown

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@joshuay03
joshuay03 force-pushed the fix-initialize-with-sorted-set branch from 66cca55 to 4b5231c Compare August 18, 2026 04:39
@mergify

mergify Bot commented Aug 18, 2026

Copy link
Copy Markdown

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@joshuay03 joshuay03 changed the title Fix SortedSet.new(sorted_set) and hash-key lookups on Ruby 4.0 Fix SortedSet.new(sorted_set) and Hash lookups on Ruby 4.0 Aug 18, 2026
@joshuay03
joshuay03 force-pushed the fix-initialize-with-sorted-set branch from 4b5231c to fa614bf Compare August 19, 2026 22:40
@mergify

mergify Bot commented Aug 19, 2026

Copy link
Copy Markdown

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

1 similar comment
@mergify

mergify Bot commented Aug 25, 2026

Copy link
Copy Markdown

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@joshuay03
joshuay03 force-pushed the fix-initialize-with-sorted-set branch 3 times, most recently from f8b4cde to 7c3dcb6 Compare August 25, 2026 04:54
@joshuay03 joshuay03 changed the title Fix SortedSet.new(sorted_set) and Hash lookups on Ruby 4.0 Fix SortedSet initialization and equality on Ruby 4.0 Aug 25, 2026
@mergify

mergify Bot commented Aug 25, 2026

Copy link
Copy Markdown

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@joshuay03
joshuay03 force-pushed the fix-initialize-with-sorted-set branch from 7c3dcb6 to 1e03cc5 Compare August 25, 2026 20:47
Ruby 4.0's Set initializer can bypass SortedSet's Ruby-level overrides when
copying another SortedSet, leaving its separate backing tree empty. Copy the
backing tree directly when no transformation block is given, while preserving
normal enumeration for transforming initializers.

Set's built-in equality also compares inherited storage that SortedSet does
not use. Restore SortedSet content and comparator semantics, Set-receiver
comparisons, and strict eql? behavior for Hash keys. Apply equivalent behavior
to JRuby, including a comparator fallback when Java rejects mixed numeric
lookups.
@mergify

mergify Bot commented Sep 2, 2026

Copy link
Copy Markdown

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@joshuay03
joshuay03 force-pushed the fix-initialize-with-sorted-set branch from 1e03cc5 to 3d15f57 Compare September 2, 2026 04:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants