Skip to content

vm: Set#freeze — frozen? and FrozenError on every mutator - #992

Merged
tannevaled merged 2 commits into
mainfrom
set-freeze
Nov 29, 2026
Merged

tannevaled merged 2 commits into
mainfrom
set-freeze

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

What

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 too.
  • add, delete, clear and __replace_cbi (behind replace, map!, reset, …) check it first.
  • compare_by_identity checks it only when it would reset the table, since set_i_compare_by_identity returns early when the flag is already set.
  • add?, delete?, delete_if and keep_if (and so select!, filter!, reject!) check it before looking, through a new __check_frozen primitive. A frozen Set therefore refuses even a call that would change nothing, as MRI does.

Evidence

rubydiff against MRI 4.0.10 (ruby 4.0.10 (2026-11-19 revision a704b7cd95) +PRISM [arm64-darwin27]) on 31 cases: AGREE, except three that belong to other changes:

  • clone of a frozen Set stays unfrozen until vm: Set#dup/#clone go through rb_obj_dup; dup copies generic ivars #991 merges, because that PR routes Set#clone through Object#clone, which re-applies freeze.
  • MRI 4.0 inspects a Set subclass as #<Foo: {1}>; rbgo prints Foo[1].
  • An anonymous class's name is empty in FrozenError and Object#inspect (#<:0x…>, where MRI prints #<#<Class:0x…>:0x…>).

Tests

  • TestSetFreeze: 25 cases with MRI's output as the expectation.
  • 87 tests matching Set|Freeze|Frozen|Prelude pass, including the frozen-prelude drift test (prelude_frozen_gen.go was regenerated).
  • mutsweep (-sites all) on both new guards: caught.

Refs #866, #991

🤖 Generated with Claude Code

tannevaled and others added 2 commits November 29, 2026 18:27
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>
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>
@tannevaled
tannevaled merged commit ba22f2e into main Nov 29, 2026
26 checks passed
@tannevaled
tannevaled deleted the set-freeze branch November 29, 2026 20:09
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.

1 participant