Repository navigation
vm: Set#dup/#clone go through rb_obj_dup; dup copies generic ivars - #991
Merged
Merged
Conversation
Set overrode dup and clone with a function that built a bare Set, so a copy lost its instance variables, its singleton (clone), a user initialize_copy hook and clone(freeze:). set.c (v4.0.10, Init_Set) defines no dup or clone at all, only initialize_copy. The overrides are gone: - dupValue allocates an empty Set of the receiver's class; - Set#initialize_copy (private) ports set_i_initialize_copy: a self-copy returns at once, a replace during iteration raises RuntimeError, a non-Set source raises TypedData_Get_Struct's TypeError; - Object#dup / #clone copy the generic ivar table first, which is rb_copy_generic_ivar in init_copy (object.c). Without it, Array, String, Hash and Set lost every instance variable on dup and clone. Measured against MRI 4.0.10 with rubydiff: AGREE on every case except a frozen Set, which rbgo's Set cannot record at all yet (filed separately). Closes #866 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tannevaled
added a commit
that referenced
this pull request
Nov 29, 2026
rbgo's Set had no frozen state: Object#freeze did nothing to it and frozen? answered false, so a "frozen" Set kept changing. set.c (v4.0.10) keeps the state in the object's flag and calls rb_check_frozen at the top of every mutator, before it looks at the members. - Set gains a frozen flag; Object#freeze sets it, isFrozen and explicitlyFrozen read it (so instance_variable_set and `def s.x` refuse). - add, delete, clear, __replace_cbi (replace/map!/reset/...) check it first; compare_by_identity checks only when it would reset, as set_i_compare_by_identity returns early when the flag is already set. - add?, delete?, delete_if, keep_if (and select!/filter!/reject!) check it before looking, through a new __check_frozen primitive, so a call that would change nothing is refused too, as in MRI. Measured against MRI 4.0.10 with rubydiff on 31 cases: AGREE except Set#clone of a frozen Set (needs #991, which routes clone through Object#clone) and two inspect formats (subclass and anonymous class), which are a separate change. Refs #866 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tannevaled
added a commit
that referenced
this pull request
Nov 29, 2026
* vm: Set#freeze — frozen? and FrozenError on every mutator rbgo's Set had no frozen state: Object#freeze did nothing to it and frozen? answered false, so a "frozen" Set kept changing. set.c (v4.0.10) keeps the state in the object's flag and calls rb_check_frozen at the top of every mutator, before it looks at the members. - Set gains a frozen flag; Object#freeze sets it, isFrozen and explicitlyFrozen read it (so instance_variable_set and `def s.x` refuse). - add, delete, clear, __replace_cbi (replace/map!/reset/...) check it first; compare_by_identity checks only when it would reset, as set_i_compare_by_identity returns early when the flag is already set. - add?, delete?, delete_if, keep_if (and select!/filter!/reject!) check it before looking, through a new __check_frozen primitive, so a call that would change nothing is refused too, as in MRI. Measured against MRI 4.0.10 with rubydiff on 31 cases: AGREE except Set#clone of a frozen Set (needs #991, which routes clone through Object#clone) and two inspect formats (subclass and anonymous class), which are a separate change. Refs #866 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * vm: a frozen Set refuses a singleton method The ubuntu coverage ratchet put explicitlyFrozen at 90.9%: the *Set arm this branch added was reached by nothing. Two cases in TestSetFreeze drive it through define_singleton_method and `def s.g`, both FrozenError on MRI 4.0.10 (measured with rubydiff). Only the class is pinned. MRI's message prints the receiver after rb_singleton_class has given it a singleton, so it reads "can't modify frozen Set: #<Class:0x…>[1]", where rbgo says "Set[1]". That is Set#inspect's divergence, not freeze's. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Set#dupand#clonenow go throughObject#dup/#clone, as in MRI. set.c (v4.0.10,Init_Set) defines noduporclonefor Set, onlyinitialize_copy. rbgo's overrides built a bare Set, so a copy lost:clone);initialize_copyhook;clone(freeze:).Changes:
dupValueallocates an empty Set of the receiver's class.Set#initialize_copy, portingset_i_initialize_copy:#each;RuntimeError: cannot replace set during iteration;TypeError: wrong argument type Array (expected set);compare_by_identityflag are copied, asset_copycopies the table with its type.Object#dupand#clonenow copy the generic ivar table before the hook. This isrb_copy_generic_ivarininit_copy(object.c). Without it, Array, String and Hash lost every instance variable ondupandclonetoo, not only Set.Evidence
rubydiffagainst MRI 4.0.10 (ruby 4.0.10 (2026-11-19 revision a704b7cd95) +PRISM [arm64-darwin27]). The cases measured were:dupandclone;compare_by_identity;clonebut notdup;super;initialize_copyedge cases above;Result: AGREE on every case.
One divergence remains: a frozen Set.
Set[1].freeze.frozen?returns false in rbgo, because its Set has no frozen state at all, while set.c callsrb_check_frozenin every mutator. That is a separate change, the next pull request.Tests
TestSetDupCloneandTestDupCopiesGenericIvarstake MRI's output as their expectations.TestCopyGenericIvarsToImmediatecovers a copy onto a value with no table.Set|Dup|Clone|Ivar|InstanceVar|Copy|Freeze|Frozenpass.-sites all) on the five new guards: all caught by assertions.Closes #866
🤖 Generated with Claude Code