Skip to content

Fix SortedSet.new(sorted_set) on Ruby 4.0#20

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

Fix SortedSet.new(sorted_set) on Ruby 4.0#20
joshuay03 wants to merge 1 commit into
knu:masterfrom
joshuay03:fix-initialize-with-sorted-set

Conversation

@joshuay03
Copy link
Copy Markdown

@joshuay03 joshuay03 commented Apr 16, 2026

On Ruby 4.0, SortedSet.new(another_sorted_set) always returns an empty set. The Ruby 4.0 implementation overrides replace to handle a SortedSet argument by copying @tree directly, but C's Set#initialize does not call the Ruby level replace when given a Set subclass. It
takes a fast path that bypasses our overrides, so @tree is never populated and the new instance behaves as if empty.

The fix mirrors the existing replace logic inside initialize: if the argument is already a SortedSet, copy its @tree directly and pass nil to super so the C initializer sets up an empty base without attempting to populate it. All other arguments fall through to super as before, preserving block transformation and all existing behaviour.

I would appreciate a release with this fix if possible. We are in the middle of a Ruby 4.0 upgrade and this is a blocker for us.

cc @knu

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

Bump 🙏🏽

Copy link
Copy Markdown
Owner

@knu knu left a comment

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
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