Skip to content

vm: Set#dup/#clone go through rb_obj_dup; dup copies generic ivars - #991

Merged
tannevaled merged 1 commit into
mainfrom
set-dup-clone
Nov 29, 2026
Merged

tannevaled merged 1 commit into
mainfrom
set-dup-clone

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

What

Set#dup and #clone now go through Object#dup / #clone, as in MRI. set.c (v4.0.10, Init_Set) defines no dup or clone for Set, only initialize_copy. rbgo's overrides built a bare Set, so a copy lost:

  • its instance variables;
  • its singleton methods (clone);
  • a subclass's initialize_copy hook;
  • clone(freeze:).

Changes:

  • dupValue allocates an empty Set of the receiver's class.
  • New private Set#initialize_copy, porting set_i_initialize_copy:
    • a self-copy returns at once, even inside #each;
    • a replace during iteration raises RuntimeError: cannot replace set during iteration;
    • a non-Set source raises TypeError: wrong argument type Array (expected set);
    • the members and the compare_by_identity flag are copied, as set_copy copies the table with its type.
  • Object#dup and #clone now copy the generic ivar table before the hook. This is rb_copy_generic_ivar in init_copy (object.c). Without it, Array, String and Hash lost every instance variable on dup and clone too, not only Set.

Evidence

rubydiff against MRI 4.0.10 (ruby 4.0.10 (2026-11-19 revision a704b7cd95) +PRISM [arm64-darwin27]). The cases measured were:

  • subclass class and ivars after dup and clone;
  • independence of the copy;
  • compare_by_identity;
  • singleton methods on clone but not dup;
  • a user hook with and without super;
  • the initialize_copy edge cases above;
  • generic ivars on Array, String, Hash and Set.

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 calls rb_check_frozen in every mutator. That is a separate change, the next pull request.

Tests

  • TestSetDupClone and TestDupCopiesGenericIvars take MRI's output as their expectations.
  • TestCopyGenericIvarsToImmediate covers a copy onto a value with no table.
  • 124 tests matching Set|Dup|Clone|Ivar|InstanceVar|Copy|Freeze|Frozen pass.
  • mutsweep (-sites all) on the five new guards: all caught by assertions.

Closes #866

🤖 Generated with Claude Code

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
tannevaled merged commit 1a810ac into main Nov 29, 2026
26 checks passed
@tannevaled
tannevaled deleted the set-dup-clone branch November 29, 2026 16:59
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>
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.

Set#dup and Set#clone drop the subclass, its ivars and its singleton

1 participant