From b060cc65b9a9c64fca75b7a3ac047be29c4d1b30 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Thu, 5 Nov 2026 22:44:35 +0100 Subject: [PATCH] foobar() is an FCALL: parentheses stop a local from shadowing a method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit language/send_spec.rb: pass=73 fail=5 -> pass=74 fail=4. foobar = 100 def foobar; 200; end foobar # 100, the local -- correct foobar() # 100 as well, and MRI says 200 compiler.go took the local-variable shortcut for any receiver-less, argument-less, block-less call: if v.Recv == nil && v.Block == nil && len(v.Args) == 0 { if depth, slot, ok := b.resolve(v.Name); ok { `foobar()` matches all three, so the read won over the method for every name the scope happened to have assigned. ## The bit was already read thirty lines below `foobar()` is `fcall paren_args` (parse.y-ruby_4_0:3572-3577, 5242-5248), which builds NEW_FCALL and never consults the local table. The vcall flag already says exactly this, cites the same productions, and uses ast.Call.Paren: vcall := v.Recv == nil && v.Block == nil && !v.Safe && !v.Paren && ... One bit, needed in two places, read in one of them. ## Measured cmd/oracle against /opt/homebrew/opt/ruby@4.0/bin/ruby (ruby 4.0.7 2026-09-15 revision 229531a6cf +PRISM): nine probe rows, treatment matches MRI BYTE FOR BYTE where the control differed on four of them. Not just the spec's example: only_local = 7 ; only_local() 7 -> NoMethodError callable = ->{} ; callable() # -> NoMethodError def v;9;end; v=1; [v, v(), self.v] [1, 1, 9] -> [1, 9, 9] ⛔ Control and treatment printed the SAME NUMBER OF BYTES on the first probe (211 vs 211) -- 100 and 200 are the same length. A comparison by length would have called this parity, which is why cmd/oracle compares content. ## Negative control With the conjunct disabled, 5 rows fail and 6 CONTROLS STILL PASS -- the bare name, a call with arguments, the NameError/NoMethodError miss pair, the eval'd binding local, and a block closing over a local. The controls are not passing vacuously, and the eval row is the one that would catch narrowing the shortcut too far: the parser emits a Call, not a VarRef, for an identifier whose assignment it never saw, which is how an eval'd string reaches binding locals. go test ./... : 14 packages ok, 0 failures. ## Also here: cmd/oracle stops reporting an agreement as its own defect Measuring this change, the three-engine run printed === the engines DISAGREED === ruby vs rbgo-main: stdout 150 vs 101 bytes ruby vs rbgo-fixed: nothing -- which is a defect in this tool, not an agreement differences() has carried that sentinel since it was written. It was RIGHT, and a sentence naming a defect is not a fix for it: the line kept printing, in the exact shape the tool exists for -- reference, control, treatment -- and answered "which engine agrees" backwards, on the line carrying the result. The agreeing engine is now named as agreeing, `sameAnswer` is ONE definition shared with agreed() so a pairwise check cannot drift from the all-engines one, and a three-engine row covers it. ## Not fixed here `p(withblock { 7 })` after `withblock = 5` is a parse error in rbgo and prints 7 in MRI. It needs all three lines -- the brace block alone parses -- and rbgo at e5619211 fails identically, so it predates this change. Filed separately; the row is out of this test. Refs #799 Co-Authored-By: Claude Opus 5 --- internal/compiler/compiler.go | 16 ++- internal/vm/paren_fcall_test.go | 111 ++++++++++++++++++ .../conformance/rubyspec/cmd/oracle/main.go | 29 ++++- .../rubyspec/cmd/oracle/main_test.go | 13 ++ 4 files changed, 165 insertions(+), 4 deletions(-) create mode 100644 internal/vm/paren_fcall_test.go diff --git a/internal/compiler/compiler.go b/internal/compiler/compiler.go index 404fe94a..1f518df5 100644 --- a/internal/compiler/compiler.go +++ b/internal/compiler/compiler.go @@ -1420,7 +1420,21 @@ func (c *Compiler) compileCall(v *ast.Call) { // send. The parser emits a Call (not a VarRef) for an identifier whose // assignment it never saw — which is exactly how an eval'd string references // the binding locals seeded into this scope. - if v.Recv == nil && v.Block == nil && len(v.Args) == 0 { + // + // ⛔ !v.Paren, because PARENTHESES SETTLE IT. `foobar()` is `fcall paren_args` + // (parse.y-ruby_4_0:3572-3577, 5242-5248), which builds NEW_FCALL and never + // consults the local table: a local named foobar does not shadow a method + // foobar at a call written with parentheses. Without this conjunct + // + // foobar = 100 + // def foobar; 200; end + // foobar # => 100, the local + // foobar() # => 100 as well, and MRI says 200 + // + // and the read silently wins over the method for every name a scope happens + // to have assigned. The same bit already gates the vcall flag thirty lines + // below, quoting the same productions — it was read there and not here. + if v.Recv == nil && v.Block == nil && len(v.Args) == 0 && !v.Paren { if depth, slot, ok := b.resolve(v.Name); ok { b.emit(bytecode.OpGetLocal, slot, depth) return diff --git a/internal/vm/paren_fcall_test.go b/internal/vm/paren_fcall_test.go new file mode 100644 index 00000000..904ee453 --- /dev/null +++ b/internal/vm/paren_fcall_test.go @@ -0,0 +1,111 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm + +import "testing" + +// Parentheses settle whether a receiver-less name is a method call or a local +// variable read. `foobar()` is `fcall paren_args` (parse.y-ruby_4_0:3572-3577, +// 5242-5248), which builds NEW_FCALL and never consults the local table, so a +// local named foobar does NOT shadow a method foobar at a parenthesised call. +// +// Every expectation below was measured on +// `ruby 4.0.7 (2026-09-15 revision 229531a6cf) +PRISM [arm64-darwin25]` +// through scripts/conformance/rubyspec/cmd/oracle. +// +// ⛔ The compiler already read ast.Call.Paren for the vcall flag, citing these +// same productions, while the local-variable shortcut thirty lines above it did +// not. One bit, needed in two places, read in one. +func TestParenthesesMakeACallAnFCallAndNotALocalRead(t *testing.T) { + for _, tc := range []struct{ name, src, want string }{ + // ⛔ The decisive row: language/send_spec.rb "with same names as existing + // variables is ok". Before the fix the second line printed 100. + {"a local does not shadow a method at a parenthesised call", + `def shadowed; 200; end +shadowed = 100 +p shadowed +p shadowed()`, "100\n200"}, + + {"the parenthesised call reaches the method, not the local's value", + `def m; :method; end +m = :local +p m()`, ":method"}, + + // A local with NO method of that name is not returned: the call misses. + {"a parenthesised call with only a local raises NoMethodError", + `only_local = 7 +begin + only_local() +rescue NoMethodError => e + p e.class +end`, "NoMethodError"}, + + // Even when the local holds something callable, `f()` is not `f.call`. + {"a local holding a lambda is not called by parentheses", + `callable = -> { :from_proc } +begin + callable() +rescue NoMethodError => e + p e.class +end`, "NoMethodError"}, + + {"the bare name is still the local", + `def bare; 200; end +bare = 100 +p bare`, "100"}, + + {"it holds inside a method body, not only at the top level", + `class C + def v; 9; end + def go + v = 1 + [v, v(), self.v] + end +end +p C.new.go`, "[1, 9, 9]"}, + + // ⛔ A control for a shape that was never the local anyway: a regression + // here would be in this change's blast radius rather than in its rule. + {"a parenthesised call with arguments was always a call", + `def witharg(x); x * 2; end +witharg = 5 +p witharg(21)`, "42"}, + + // ⛔ The vcall/fcall distinction this change must NOT disturb: a miss + // without parentheses is NameError, with parentheses NoMethodError. + {"a miss without parentheses is still NameError", + `begin + nope +rescue NameError => e + p e.class +end`, "NameError"}, + + {"a miss with parentheses is still NoMethodError", + `begin + nope() +rescue NoMethodError => e + p e.class +end`, "NoMethodError"}, + + // ⛔ The behaviour the local-variable shortcut exists for, and the reason + // !v.Paren is a conjunct rather than a removal. The parser emits a Call, + // not a VarRef, for an identifier whose assignment it never saw — which is + // how an eval'd string reaches the binding's locals seeded into the scope. + // This row fails if the shortcut is narrowed too far. + {"a local seeded into an eval'd binding is still readable", + `seeded = 42 +p eval("seeded")`, "42"}, + + {"a local is still readable from a block that closes over it", + `x = 3 +p [1].map { x }`, "[3]"}, + } { + t.Run(tc.name, func(t *testing.T) { + if got := runSrc(t, tc.src); got != tc.want { + t.Errorf("%s\n got %q\nwant %q", tc.src, got, tc.want) + } + }) + } +} diff --git a/scripts/conformance/rubyspec/cmd/oracle/main.go b/scripts/conformance/rubyspec/cmd/oracle/main.go index 961b0d8c..7b013752 100644 --- a/scripts/conformance/rubyspec/cmd/oracle/main.go +++ b/scripts/conformance/rubyspec/cmd/oracle/main.go @@ -120,6 +120,21 @@ func main1(args []string, out, errOut io.Writer) int { } fmt.Fprintf(out, "=== the engines DISAGREED ===\n") for _, r := range runs[1:] { + // ⛔ An engine that AGREES with the reference is named as agreeing, not + // handed to differences() to describe nothing. The shape this tool is + // built for is three engines — reference, control, treatment — where the + // whole question is WHICH of the two matches the reference, and a line + // reading "nothing" under a DISAGREED heading answers it backwards. + // + // differences() has carried a sentinel for this case since it was + // written ("a defect in this tool, not an agreement"). It was right, and + // a sentence that names a defect is not a fix for it: the sentinel went + // on printing for two years of use. It stays below as an assertion that + // this branch is the only way here. + if sameAnswer(runs[0], r) { + fmt.Fprintf(out, " %s vs %s: AGREES, byte for byte\n", runs[0].engine, r.engine) + continue + } fmt.Fprintf(out, " %s vs %s: %s\n", runs[0].engine, r.engine, differences(runs[0], r)) } return 1 @@ -180,11 +195,19 @@ func report(w io.Writer, r run) { } } +// sameAnswer is the whole notion of agreement: the same exit status and the +// same bytes on both streams. It is ONE definition because a pairwise check +// that drifted from the all-engines check could report "the engines DISAGREED" +// and then name every pair as agreeing. +func sameAnswer(a, b run) bool { + return a.status == b.status && + bytes.Equal(a.stdout, b.stdout) && + bytes.Equal(a.stderr, b.stderr) +} + func agreed(runs []run) bool { for _, r := range runs[1:] { - if r.status != runs[0].status || - !bytes.Equal(r.stdout, runs[0].stdout) || - !bytes.Equal(r.stderr, runs[0].stderr) { + if !sameAnswer(runs[0], r) { return false } } diff --git a/scripts/conformance/rubyspec/cmd/oracle/main_test.go b/scripts/conformance/rubyspec/cmd/oracle/main_test.go index 5ffe1867..2fb147b7 100644 --- a/scripts/conformance/rubyspec/cmd/oracle/main_test.go +++ b/scripts/conformance/rubyspec/cmd/oracle/main_test.go @@ -85,6 +85,19 @@ func TestOracleComparesEngines(t *testing.T) { {"stderr diverges", []string{quiet, noisy}, 1, "stderr 0 vs 19 bytes"}, {"status diverges", []string{quiet, failing}, 1, "exit status 0 vs 1"}, {"one engine only", []string{quiet}, 0, "exit status: 0"}, + + // ⛔ THE SHAPE THIS TOOL IS FOR: reference, control, treatment — where the + // whole question is WHICH of the two agrees with the reference. The + // agreeing pair must be NAMED as agreeing. It used to be handed to + // differences(), which had nothing to describe and said so: + // + // ruby vs rbgo-fixed: nothing — which is a defect in this tool + // + // under a heading reading "the engines DISAGREED" — the answer backwards, + // on the line that carries the result. Found 2026-11-05 while measuring a + // compiler fix, where the treatment was the engine that agreed. + {"the engine that agrees is named as agreeing", + []string{quiet, noisy, alsoQuiet}, 1, "AGREES, byte for byte"}, } { t.Run(tc.name, func(t *testing.T) { var out, errOut bytes.Buffer