Skip to content

vm: reset singleton-class body visibility to public on class << obj entry - #603

Merged
tannevaled merged 1 commit into
mainfrom
wave22/vmvis
Sep 9, 2026
Merged

tannevaled merged 1 commit into
mainfrom
wave22/vmvis

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

The bug

OpOpenSingletonClass (the class << obj opcode) ran the singleton body
against the object's persistent singleton class — a class's cached
metaclass, or an object's cached singleton — without resetting its
defaultVis/funcMode. So a bare private/protected in one
class << self block leaked into a later reopened block: a public
singleton method defined after a private in a prior block wrongly became
private (and vice-versa). Two prior conformance waves (19/20/21) flagged
this as blocking core/kernel and core/module methods/public_methods.

class C
  class << self; private; def a; end; end
  class << self; def b; end; end   # MRI: b is PUBLIC
end
C.respond_to?(:b)   # MRI => true;  rbgo (before) => false

The fix

One line in the OpOpenSingletonClass handler, mirroring what
defineClassIn/defineModuleIn already do for every ordinary class/module
(re)open — MRI resets scope visibility to public on every
class/module/singleton frame push (vm_insnhelper.c):

sc.defaultVis, sc.funcMode = visPublic, false

The reset is body-entry only, so an in-block private still applies to
defs that follow it in the same block. The ordinary class/module path is
untouched.

Verification

Byte-for-byte against MRI Ruby 4.0.5 for every shape changed: reopened
class << self with private/protected/public, def self.x after a
class << self private, per-object singleton reopen, module receiver,
private_class_method interplay, and no-arg mode switches. New whitebox
test internal/vm/singleton_vis_reset_test.go (added line covered,
count=27).

ruby/spec language+core (frozen shim, pinned SHA): 19992 -> 19996
(+4). The only three files that moved, all improvements, all
visibility-related:

spec file before after
core/kernel/methods_spec.rb 16 17
core/kernel/public_methods_spec.rb 11 13
language/def_spec.rb 60 61

(Module#methods/#public_methods are exercised by the Kernel specs.)
No regression across the whole corpus — identical file-load set, no other
file's pass count changed.

Gates: go build ./..., GOWORK=off go test ./... (green), wasip1 build,
gofmt -l internal/vm/ empty.

🤖 Generated with Claude Code

… entry (core/kernel +3, language/def +1)

OpOpenSingletonClass ran the `class << obj` body against the object's
persistent singleton class (a class's cached metaclass, or an object's
cached singleton) without resetting its defaultVis/funcMode. So a bare
`private`/`protected` in one `class << self` block leaked into a later
reopened block: a public singleton method defined after a `private` in a
prior block wrongly became private.

MRI resets scope visibility to public on every class/module/singleton
frame push (vm_insnhelper.c), which defineClassIn/defineModuleIn already
mirror. Do the same on the singleton-class-open path:
`sc.defaultVis, sc.funcMode = visPublic, false` before running the body.
The reset is body-entry only, so an in-block `private` still applies to
defs that follow it in the same block.

Verified byte-for-byte against MRI 4.0.5 (reopened `class << self` with
private/protected/public, per-object singleton reopen, module receiver,
private_class_method interplay, no-arg mode switch). ruby/spec
language+core: 19992 -> 19996 (core/kernel methods_spec +1,
public_methods_spec +2, language/def_spec +1), no regressions.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit fe2daf5 into main Sep 9, 2026
7 checks passed
@tannevaled
tannevaled deleted the wave22/vmvis branch September 9, 2026 16:17
tannevaled added a commit that referenced this pull request Sep 10, 2026
…606)

First run past 20,000 passing ruby/spec examples.

Wave-22 merged three bibliography-first conformance PRs:
- #605 core/io: IO.pipe encodings, IO#write/write_nonblock transcoding, bom|
  handling, off_t RangeError, IO.foreach $./$_, IO#readpartial, IO.copy_stream
  with duck-typed sources and destinations — +77.
- #604 core/kernel: Kernel#putc (new), #p/#print/#puts forwarding through
  $stdout so a mocked stream observes them, #p rendering per MRI rb_inspect — +15.
- #603 VM: OpOpenSingletonClass now resets defaultVis, so a private in one
  'class << self' body no longer leaks into a later reopened one — +4.

Measured total on merged main: 20093 passing. Local measure at TIMEOUT=25;
CI runs TIMEOUT=60 so its total is >= this floor.

Co-authored-by: Claude Opus 5 (1M context) <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.

1 participant