Skip to content

Commit c81ffc8

Browse files
tannevaledclaude
andcommitted
vm: home enum_for/to_enum on Kernel, and hold the VCALL verdict to plain names
Three things, each measured against ruby 4.0.5 before and after. enum_for / to_enum (G3). registerEnumerator runs from NewVM AFTER rehomeKernelMethods (#735) -- it needs the prelude's Enumerable -- so its two vm.cObject.define calls re-introduced on Object the last two names MRI puts on Kernel. Object.instance_method(:enum_for).owner now answers Kernel as it does on 4.0.5, Kernel.public_instance_methods(false) carries the pair, and Object.public_instance_methods(false) drops from 78 to 76. The exact-set assertion in wave_d_kernel_homing_test.go that named this pair as known residue is now an assertion of the EMPTY set, so the shape that produced it -- a Kernel method built after the re-homing pass -- still fails there. The definition lives in MRI's enumerator.c, which this C corpus does not carry, so the oracle for the placement is the running 4.0.5, stated as such. vcallNameShape. `nope!` and `nope?` are written with no receiver, no parentheses, no arguments and no block, so the compiler's SHAPE test sets FlagSendVCall for them -- but they lex as tFID and `primary : tFID` builds NEW_FCALL (parse.y-ruby_4_0:4370), while gettable reaches NEW_VCALL only under case ID_LOCAL (parse.y-ruby_4_0:13086). Both raise NoMethodError on 4.0.5 under `--parser=parse.y` AND `--parser=prism`. The name test belongs beside the shape test in internal/compiler, which is outside this cluster; it is at the raise site meanwhile, where it costs nothing because an error is already being raised. Three tests encoded the old answer for a bare name and now encode MRI's. string_plus / string_concat_encoding asserted that an error raised inside #to_str propagates rather than being swallowed as a TypeError: their probes take an explicit receiver so they keep asserting NoMethodError, with the bare form added beside one of them as NameError. TestIRBErrorDisplay now expects (NameError), which is what `irb` itself prints for a bare undefined name. Two comments in kernel_introspect.go named registerKernelModuleFunctions, which no longer exists; the split is rehomeKernelMethods over kernelModuleFunctionNames. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 2cd317a commit c81ffc8

8 files changed

Lines changed: 373 additions & 23 deletions

‎internal/vm/builtins.go‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@ func (vm *VM) bootstrap() {
10851085
// rb_nomethod_err_new. #name and #receiver are set either way; #args is not,
10861086
// and a VCALL carries no arguments, so callArgs is nil here regardless.
10871087
cls, undef := "NoMethodError", "undefined method '"
1088-
if vm.missingVCall {
1088+
if vm.missingVCall && vcallNameShape(string(nameSym)) {
10891089
cls, undef = "NameError", "undefined local variable or method '"
10901090
}
10911091
vm.missingVCall = false
@@ -12466,3 +12466,33 @@ func (vm *VM) cmpIntValue(v object.Value) int {
1246612466
}
1246712467
return 0
1246812468
}
12469+
12470+
// vcallNameShape reports whether name can be MRI's NODE_VCALL at all. Two
12471+
// grammar rules decide it and the VM has to honour both, because
12472+
// bytecode.FlagSendVCall records only the SHAPE of the call site (no receiver,
12473+
// no parentheses, no arguments, no block) and not the shape of the NAME:
12474+
//
12475+
// - gettable reaches NEW_VCALL only under case ID_LOCAL (parse.y-ruby_4_0:13086),
12476+
// i.e. for a plain tIDENTIFIER;
12477+
// - a name ending in '!' or '?' lexes as tFID, and `primary : tFID` builds
12478+
// NEW_FCALL instead (parse.y-ruby_4_0:4370).
12479+
//
12480+
// So `nope!` and `nope?` are FCALLs and raise NoMethodError. Measured on ruby
12481+
// 4.0.5 under BOTH parsers — `ruby --parser=parse.y` and `ruby --parser=prism`
12482+
// agree here — so this is not one of the places where the two instruments
12483+
// differ. A setter name (`nope=`) is likewise never a bare identifier.
12484+
//
12485+
// The test belongs in the compiler, beside the shape test that sets the flag
12486+
// (internal/compiler/compiler.go); it is here because the compiler is outside
12487+
// this change's file cluster, and it costs nothing on the hot path because it
12488+
// runs only where an error is already being raised.
12489+
func vcallNameShape(name string) bool {
12490+
if name == "" {
12491+
return false
12492+
}
12493+
switch name[len(name)-1] {
12494+
case '!', '?', '=':
12495+
return false
12496+
}
12497+
return true
12498+
}

‎internal/vm/enumerator.go‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,18 @@ func (vm *VM) registerEnumerator() {
428428
}
429429
return &Enumerator{recv: self, meth: meth, args: rest, sizeBlock: blk}
430430
}
431-
vm.cObject.define("enum_for", enumForFn)
432-
vm.cObject.define("to_enum", enumForFn)
431+
// On Kernel, not Object: MRI defines nothing on rb_cObject, and
432+
// Object.instance_method(:enum_for).owner answers Kernel on ruby 4.0.5 (the
433+
// pair is documented under Kernel's "Other" section, r4-object.c:4372; the
434+
// definition itself lives in enumerator.c, which this C corpus does not
435+
// carry, so the oracle here is the running 4.0.5, not a grep). registerEnumerator
436+
// runs from NewVM AFTER rehomeKernelMethods, so defining them on vm.cObject
437+
// re-introduced two names Object must not introduce (#731). Dispatch is
438+
// unchanged either way -- Object includes Kernel -- but #owner,
439+
// Object.public_instance_methods(false) and Kernel.instance_methods(false)
440+
// all reported the wrong module.
441+
vm.cKernel.define("enum_for", enumForFn)
442+
vm.cKernel.define("to_enum", enumForFn)
433443

434444
// Enumerable#chain — a chain of self followed by the given enumerables. Mixed
435445
// into every Enumerable (Array/Range/…) and inherited by Enumerator.

‎internal/vm/irb_internal_test.go‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,13 @@ func TestIRBSession(t *testing.T) {
124124

125125
// TestIRBErrorDisplay covers the exception branch: a bare undefined name raises,
126126
// and the REPL reports it MRI-style ("message (Class)") on $stdout and keeps
127-
// going.
127+
// going. A bare name is MRI's NODE_VCALL, so the class is NameError — `irb`
128+
// itself prints "undefined local variable or method 'nope' for main
129+
// (NameError)" on ruby 4.0.5, under both --parser=parse.y and --parser=prism.
128130
func TestIRBErrorDisplay(t *testing.T) {
129131
got := runFS(t, `$stdin = StringIO.new("nope\nexit\n"); IRB.conf[:PROMPT_MODE] = :SIMPLE; IRB.start`)
130-
if !strings.Contains(got, "(NoMethodError)") {
131-
t.Errorf("expected a NoMethodError report, got %q", got)
132+
if !strings.Contains(got, "(NameError)") {
133+
t.Errorf("expected a NameError report, got %q", got)
132134
}
133135
if !strings.HasPrefix(got, ">> ") || !strings.HasSuffix(got, ">> ") {
134136
t.Errorf("expected the loop to survive the error, got %q", got)

‎internal/vm/kernel_introspect.go‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ func (vm *VM) registerKernelIntrospection() {
284284
// exit!(status = false) differs only in its default (EXIT_FAILURE) and in
285285
// skipping at_exit handlers; the embedded host has no process to _exit() from,
286286
// so it unwinds the same way. Both are also reachable as Kernel.exit /
287-
// Kernel.exit! — registerKernelModuleFunctions mirrors the records.
287+
// Kernel.exit! — rehomeKernelMethods mirrors the records onto Kernel's
288+
// singleton for every name in kernelModuleFunctionNames (builtins.go).
288289
vm.cObject.define("exit", func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value {
289290
return vm.raiseSystemExit(vm.exitStatusArg(args, 0), "exit")
290291
})
@@ -346,9 +347,9 @@ func (vm *VM) raiseSystemExit(status int64, message string) object.Value {
346347
// CRuby defines by forwarding to another object. MRI declares each with
347348
// rb_define_global_function (io.c:15615-15628, process.c), which makes it a
348349
// private instance method reachable without a receiver AND — through the
349-
// Kernel module_function split registerKernelModuleFunctions applies — a public
350-
// method on the Kernel module; the names are listed there so the split covers
351-
// them.
350+
// Kernel module_function split rehomeKernelMethods applies — a public method on
351+
// the Kernel module; the names are listed in kernelModuleFunctionNames
352+
// (builtins.go) so the split covers them.
352353
//
353354
// Every body here forwards through vm.send to the object CRuby forwards to, so
354355
// a spec that stubs ARGF.gets (core/kernel/gets_spec) or replaces IO.select

‎internal/vm/string_concat_encoding_internal_test.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ func TestStringConcatEncoding(t *testing.T) {
5353
}
5454
}
5555
// A NoMethodError raised inside #to_str propagates (it is not swallowed as a
56-
// TypeError).
57-
if got := eval(t, `o = Object.new; def o.to_str; nope; end
56+
// TypeError). The probe carries an EXPLICIT receiver: a bare `nope` is MRI's
57+
// NODE_VCALL and raises NameError (ruby 4.0.5, both parsers), so it would
58+
// make this assertion about the wrong class.
59+
if got := eval(t, `o = Object.new; def o.to_str; 42.nope; end
5860
p (("x".dup << o; :no) rescue $!.class)`); got != "NoMethodError\n" {
5961
t.Errorf("to_str NoMethodError: got=%q", got)
6062
}

‎internal/vm/string_plus_internal_test.go‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,17 @@ func TestStringPlus(t *testing.T) {
3535
t.Errorf("%s: got=%q want %s", c.src, got, c.cls)
3636
}
3737
}
38-
// A NoMethodError raised inside #to_str propagates.
39-
if got := eval(t, `o = Object.new; def o.to_str; nope; end
38+
// A NoMethodError raised inside #to_str propagates. The probe carries an
39+
// EXPLICIT receiver: a bare `nope` is MRI's NODE_VCALL and raises NameError,
40+
// not NoMethodError (ruby 4.0.5 under both --parser=parse.y and
41+
// --parser=prism), which would make this assertion about the wrong class.
42+
if got := eval(t, `o = Object.new; def o.to_str; 42.nope; end
4043
p (("a" + o; :no) rescue $!.class)`); got != "NoMethodError\n" {
4144
t.Errorf("to_str NoMethodError: got=%q", got)
4245
}
46+
// And the bare form beside it, which propagates just the same as a NameError.
47+
if got := eval(t, `o = Object.new; def o.to_str; nope; end
48+
p (("a" + o; :no) rescue $!.class)`); got != "NameError\n" {
49+
t.Errorf("to_str NameError: got=%q", got)
50+
}
4351
}

‎internal/vm/wave_d_kernel_homing_test.go‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,13 @@ func TestWaveDKernelMethodsAreOwnedByKernel(t *testing.T) {
4545
func TestWaveDObjectDefinesNothingKernelDefines(t *testing.T) {
4646
vm := New(io.Discard)
4747
all := append(append(append([]string{}, kernelPublicNames...), kernelPrivateNames...), kernelModuleFunctionNames...)
48-
// enum_for / to_enum are the one known exception, and the set is asserted
49-
// EXACTLY so it cannot quietly grow: registerEnumerator runs from NewVM AFTER
50-
// setupBuiltins (it needs the prelude's Enumerable), so its
51-
// vm.cObject.define("enum_for", …) / .define("to_enum", …) re-introduce the
52-
// pair on Object after rehomeKernelMethods has run. The fix is two characters
53-
// of internal/vm/enumerator.go — define them on vm.cKernel instead of
54-
// vm.cObject — which is outside this change's file cluster. When it lands,
55-
// this exception must be deleted and this test will say so.
56-
const knownResidue = "enum_for to_enum"
48+
// There is NO exception left: registerEnumerator still runs from NewVM after
49+
// rehomeKernelMethods (it needs the prelude's Enumerable), but it now defines
50+
// enum_for / to_enum on vm.cKernel, so nothing re-introduces them on Object.
51+
// The empty set is asserted exactly, so a Kernel method built after the
52+
// re-homing pass — the shape that produced this residue — fails here instead
53+
// of quietly answering Object.
54+
const knownResidue = ""
5755
var residue []string
5856
for _, name := range all {
5957
if m := vm.cObject.methods[name]; m != nil {

0 commit comments

Comments
 (0)