Repository navigation
vm: Set#freeze — frozen? and FrozenError on every mutator - #992
Merged
Merged
Conversation
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
force-pushed
the
set-freeze
branch
from
November 29, 2026 17:31
b274cb3 to
77ad3ba
Compare
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
rbgo's Set had no frozen state.
Object#freezedid nothing to it andfrozen?answered false, so a "frozen" Set kept changing. set.c (v4.0.10) keeps the state in the object's flag and callsrb_check_frozenat the top of every mutator, before it looks at the members.frozenflag.Object#freezesets it;isFrozenandexplicitlyFrozenread it, soinstance_variable_setanddef s.xrefuse too.add,delete,clearand__replace_cbi(behindreplace,map!,reset, …) check it first.compare_by_identitychecks it only when it would reset the table, sinceset_i_compare_by_identityreturns early when the flag is already set.add?,delete?,delete_ifandkeep_if(and soselect!,filter!,reject!) check it before looking, through a new__check_frozenprimitive. A frozen Set therefore refuses even a call that would change nothing, as MRI does.Evidence
rubydiffagainst 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:cloneof a frozen Set stays unfrozen until vm: Set#dup/#clone go through rb_obj_dup; dup copies generic ivars #991 merges, because that PR routesSet#clonethroughObject#clone, which re-appliesfreeze.#<Foo: {1}>; rbgo printsFoo[1].FrozenErrorandObject#inspect(#<:0x…>, where MRI prints#<#<Class:0x…>:0x…>).Tests
TestSetFreeze: 25 cases with MRI's output as the expectation.Set|Freeze|Frozen|Preludepass, including the frozen-prelude drift test (prelude_frozen_gen.gowas regenerated).-sites all) on both new guards: caught.Refs #866, #991
🤖 Generated with Claude Code