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