From 2370dcda7a66ddcc3ca381887048ab231fd44c94 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 24 Aug 2026 11:56:02 +0200 Subject: [PATCH] Make the prelude's internal helpers private MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Comparable#__compare and Enumerable's __each_packed / __pack / __enum_int_arg are rbgo implementation helpers, but they were defined as public methods, so they leaked into Comparable.instance_methods and Enumerable.instance_methods where MRI exposes no such names. That made the reflection API diverge (and any class including these modules answered respond_to?(:__compare) with true). They are now marked private, so they disappear from the public method lists and respond_to? (matching MRI) while still working through the operators and iterators that call them with an implicit receiver (and through #send). No conformance-spec count changes — the leak was not directly asserted — but the reflection API now matches MRI exactly. Verified against ruby 4.0.6; whole-suite coverage gate stays at 100.0% and a full language+core sweep shows no regressions. Co-Authored-By: Claude Opus 4.8 --- internal/vm/prelude.rb | 4 ++ internal/vm/prelude_frozen_gen.go | 20 +++++++--- .../prelude_private_helpers_internal_test.go | 37 +++++++++++++++++++ 3 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 internal/vm/prelude_private_helpers_internal_test.go diff --git a/internal/vm/prelude.rb b/internal/vm/prelude.rb index bb8a697a..b2c4f326 100644 --- a/internal/vm/prelude.rb +++ b/internal/vm/prelude.rb @@ -26,6 +26,7 @@ def __compare(other) end cmp end + private :__compare # an internal helper, not part of Comparable's public API def <(other) __compare(other) < 0 @@ -142,6 +143,9 @@ def __enum_int_arg(n) raise RangeError, "bignum too big to convert into 'long'" if v > 9223372036854775807 || v < -9223372036854775808 v end + # These are internal iteration/coercion helpers, not part of Enumerable's + # public API (MRI does not expose them). + private :__each_packed, :__pack, :__enum_int_arg def to_a r = [] diff --git a/internal/vm/prelude_frozen_gen.go b/internal/vm/prelude_frozen_gen.go index c8f2c107..7abb90d7 100644 --- a/internal/vm/prelude_frozen_gen.go +++ b/internal/vm/prelude_frozen_gen.go @@ -19,9 +19,12 @@ func embeddedPrelude() *bytecode.ISeq { NumLocals: 0, Children: []*bytecode.ISeq{ &bytecode.ISeq{ - Name: "", - Insns: []bytecode.Instr{{Op: 44}, {Op: 9}, {Op: 44, A: 1, B: 1}, {Op: 9}, {Op: 44, A: 2, B: 2}, {Op: 9}, {Op: 44, A: 3, B: 3}, {Op: 9}, {Op: 44, A: 4, B: 4}, {Op: 9}, {Op: 44, A: 5, B: 5}, {Op: 9}, {Op: 44, A: 6, B: 6}, {Op: 9}, {Op: 44, A: 7, B: 7}, {Op: 53}}, - Names: []string{"__compare", "<", "<=", ">", ">=", "==", "between?", "clamp"}, + Name: "", + Insns: []bytecode.Instr{{Op: 44}, {Op: 9}, {Op: 5}, {Op: 1}, {Op: 30, A: 1, B: 1}, {Op: 9}, {Op: 44, A: 2, B: 1}, {Op: 9}, {Op: 44, A: 3, B: 2}, {Op: 9}, {Op: 44, A: 4, B: 3}, {Op: 9}, {Op: 44, A: 5, B: 4}, {Op: 9}, {Op: 44, A: 6, B: 5}, {Op: 9}, {Op: 44, A: 7, B: 6}, {Op: 9}, {Op: 44, A: 8, B: 7}, {Op: 53}}, + Consts: []object.Value{ + object.Symbol("__compare"), + }, + Names: []string{"__compare", "private", "<", "<=", ">", ">=", "==", "between?", "clamp"}, NumRequired: 0, SplatIndex: -1, KwRestSlot: -1, @@ -177,9 +180,14 @@ func embeddedPrelude() *bytecode.ISeq { Super: "", }, &bytecode.ISeq{ - Name: "", - Insns: []bytecode.Instr{{Op: 44}, {Op: 9}, {Op: 44, A: 1, B: 1}, {Op: 9}, {Op: 44, A: 2, B: 2}, {Op: 9}, {Op: 44, A: 3, B: 3}, {Op: 9}, {Op: 44, A: 4, B: 4}, {Op: 9}, {Op: 44, A: 5, B: 5}, {Op: 9}, {Op: 44, A: 6, B: 6}, {Op: 9}, {Op: 44, A: 7, B: 7}, {Op: 9}, {Op: 44, A: 8, B: 8}, {Op: 9}, {Op: 44, A: 9, B: 9}, {Op: 9}, {Op: 44, A: 10, B: 10}, {Op: 9}, {Op: 44, A: 11, B: 11}, {Op: 9}, {Op: 44, A: 12, B: 12}, {Op: 9}, {Op: 44, A: 13, B: 13}, {Op: 9}, {Op: 44, A: 14, B: 14}, {Op: 9}, {Op: 44, A: 15, B: 15}, {Op: 9}, {Op: 44, A: 16, B: 16}, {Op: 9}, {Op: 44, A: 17, B: 17}, {Op: 9}, {Op: 44, A: 18, B: 18}, {Op: 9}, {Op: 44, A: 19, B: 19}, {Op: 9}, {Op: 44, A: 20, B: 20}, {Op: 9}, {Op: 44, A: 21, B: 21}, {Op: 9}, {Op: 44, A: 22, B: 22}, {Op: 9}, {Op: 44, A: 23, B: 23}, {Op: 9}, {Op: 44, A: 24, B: 24}, {Op: 9}, {Op: 44, A: 25, B: 25}, {Op: 9}, {Op: 44, A: 26, B: 26}, {Op: 9}, {Op: 44, A: 27, B: 27}, {Op: 9}, {Op: 44, A: 28, B: 28}, {Op: 9}, {Op: 44, A: 29, B: 29}, {Op: 9}, {Op: 44, A: 30, B: 30}, {Op: 9}, {Op: 44, A: 31, B: 31}, {Op: 9}, {Op: 44, A: 32, B: 32}, {Op: 9}, {Op: 44, A: 33, B: 33}, {Op: 9}, {Op: 44, A: 34, B: 34}, {Op: 9}, {Op: 44, A: 35, B: 35}, {Op: 9}, {Op: 44, A: 36, B: 36}, {Op: 9}, {Op: 44, A: 37, B: 37}, {Op: 9}, {Op: 44, A: 38, B: 38}, {Op: 9}, {Op: 44, A: 39, B: 39}, {Op: 9}, {Op: 44, A: 40, B: 40}, {Op: 9}, {Op: 44, A: 41, B: 41}, {Op: 9}, {Op: 44, A: 42, B: 42}, {Op: 9}, {Op: 44, A: 43, B: 43}, {Op: 9}, {Op: 44, A: 44, B: 44}, {Op: 9}, {Op: 44, A: 45, B: 45}, {Op: 9}, {Op: 44, A: 46, B: 46}, {Op: 9}, {Op: 44, A: 47, B: 47}, {Op: 9}, {Op: 44, A: 48, B: 48}, {Op: 9}, {Op: 44, A: 49, B: 49}, {Op: 9}, {Op: 44, A: 50, B: 50}, {Op: 9}, {Op: 44, A: 51, B: 51}, {Op: 9}, {Op: 44, A: 52, B: 52}, {Op: 9}, {Op: 44, A: 53, B: 53}, {Op: 9}, {Op: 44, A: 54, B: 54}, {Op: 9}, {Op: 44, A: 55, B: 55}, {Op: 9}, {Op: 44, A: 56, B: 56}, {Op: 9}, {Op: 44, A: 57, B: 57}, {Op: 9}, {Op: 44, A: 58, B: 58}, {Op: 53}}, - Names: []string{"__each_packed", "__pack", "__enum_int_arg", "to_a", "to_set", "to_h", "map", "collect", "filter", "detect", "count", "min_by", "max_by", "sort", "sort_by", "select", "reject", "find", "include?", "sum", "min", "max", "minmax", "reduce", "inject", "any?", "all?", "none?", "one?", "uniq", "compact", "each_with_index", "flat_map", "collect_concat", "each_entry", "each_with_object", "filter_map", "partition", "group_by", "tally", "zip", "find_index", "find_all", "grep", "grep_v", "take_while", "drop_while", "each_slice", "each_cons", "first", "take", "drop", "chunk_while", "slice_when", "chunk", "slice_before", "slice_after", "minmax_by", "cycle"}, + Name: "", + Insns: []bytecode.Instr{{Op: 44}, {Op: 9}, {Op: 44, A: 1, B: 1}, {Op: 9}, {Op: 44, A: 2, B: 2}, {Op: 9}, {Op: 5}, {Op: 1}, {Op: 1, A: 1}, {Op: 1, A: 2}, {Op: 30, A: 3, B: 3}, {Op: 9}, {Op: 44, A: 4, B: 3}, {Op: 9}, {Op: 44, A: 5, B: 4}, {Op: 9}, {Op: 44, A: 6, B: 5}, {Op: 9}, {Op: 44, A: 7, B: 6}, {Op: 9}, {Op: 44, A: 8, B: 7}, {Op: 9}, {Op: 44, A: 9, B: 8}, {Op: 9}, {Op: 44, A: 10, B: 9}, {Op: 9}, {Op: 44, A: 11, B: 10}, {Op: 9}, {Op: 44, A: 12, B: 11}, {Op: 9}, {Op: 44, A: 13, B: 12}, {Op: 9}, {Op: 44, A: 14, B: 13}, {Op: 9}, {Op: 44, A: 15, B: 14}, {Op: 9}, {Op: 44, A: 16, B: 15}, {Op: 9}, {Op: 44, A: 17, B: 16}, {Op: 9}, {Op: 44, A: 18, B: 17}, {Op: 9}, {Op: 44, A: 19, B: 18}, {Op: 9}, {Op: 44, A: 20, B: 19}, {Op: 9}, {Op: 44, A: 21, B: 20}, {Op: 9}, {Op: 44, A: 22, B: 21}, {Op: 9}, {Op: 44, A: 23, B: 22}, {Op: 9}, {Op: 44, A: 24, B: 23}, {Op: 9}, {Op: 44, A: 25, B: 24}, {Op: 9}, {Op: 44, A: 26, B: 25}, {Op: 9}, {Op: 44, A: 27, B: 26}, {Op: 9}, {Op: 44, A: 28, B: 27}, {Op: 9}, {Op: 44, A: 29, B: 28}, {Op: 9}, {Op: 44, A: 30, B: 29}, {Op: 9}, {Op: 44, A: 31, B: 30}, {Op: 9}, {Op: 44, A: 32, B: 31}, {Op: 9}, {Op: 44, A: 33, B: 32}, {Op: 9}, {Op: 44, A: 34, B: 33}, {Op: 9}, {Op: 44, A: 35, B: 34}, {Op: 9}, {Op: 44, A: 36, B: 35}, {Op: 9}, {Op: 44, A: 37, B: 36}, {Op: 9}, {Op: 44, A: 38, B: 37}, {Op: 9}, {Op: 44, A: 39, B: 38}, {Op: 9}, {Op: 44, A: 40, B: 39}, {Op: 9}, {Op: 44, A: 41, B: 40}, {Op: 9}, {Op: 44, A: 42, B: 41}, {Op: 9}, {Op: 44, A: 43, B: 42}, {Op: 9}, {Op: 44, A: 44, B: 43}, {Op: 9}, {Op: 44, A: 45, B: 44}, {Op: 9}, {Op: 44, A: 46, B: 45}, {Op: 9}, {Op: 44, A: 47, B: 46}, {Op: 9}, {Op: 44, A: 48, B: 47}, {Op: 9}, {Op: 44, A: 49, B: 48}, {Op: 9}, {Op: 44, A: 50, B: 49}, {Op: 9}, {Op: 44, A: 51, B: 50}, {Op: 9}, {Op: 44, A: 52, B: 51}, {Op: 9}, {Op: 44, A: 53, B: 52}, {Op: 9}, {Op: 44, A: 54, B: 53}, {Op: 9}, {Op: 44, A: 55, B: 54}, {Op: 9}, {Op: 44, A: 56, B: 55}, {Op: 9}, {Op: 44, A: 57, B: 56}, {Op: 9}, {Op: 44, A: 58, B: 57}, {Op: 9}, {Op: 44, A: 59, B: 58}, {Op: 53}}, + Consts: []object.Value{ + object.Symbol("__each_packed"), + object.Symbol("__pack"), + object.Symbol("__enum_int_arg"), + }, + Names: []string{"__each_packed", "__pack", "__enum_int_arg", "private", "to_a", "to_set", "to_h", "map", "collect", "filter", "detect", "count", "min_by", "max_by", "sort", "sort_by", "select", "reject", "find", "include?", "sum", "min", "max", "minmax", "reduce", "inject", "any?", "all?", "none?", "one?", "uniq", "compact", "each_with_index", "flat_map", "collect_concat", "each_entry", "each_with_object", "filter_map", "partition", "group_by", "tally", "zip", "find_index", "find_all", "grep", "grep_v", "take_while", "drop_while", "each_slice", "each_cons", "first", "take", "drop", "chunk_while", "slice_when", "chunk", "slice_before", "slice_after", "minmax_by", "cycle"}, NumRequired: 0, SplatIndex: -1, KwRestSlot: -1, diff --git a/internal/vm/prelude_private_helpers_internal_test.go b/internal/vm/prelude_private_helpers_internal_test.go new file mode 100644 index 00000000..b3498822 --- /dev/null +++ b/internal/vm/prelude_private_helpers_internal_test.go @@ -0,0 +1,37 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm + +import "testing" + +// TestPreludeInternalHelpersPrivate checks that the prelude's internal helper +// methods (Comparable#__compare and Enumerable's __each_packed/__pack/ +// __enum_int_arg) are private, so they do not leak into the modules' public +// instance_methods as MRI's do not — while still working through the operators +// and iterators that call them. Verified against ruby 4.0.6. +func TestPreludeInternalHelpersPrivate(t *testing.T) { + cases := []struct{ src, want string }{ + // Public instance method lists match MRI (no __-prefixed helpers). + {`p Comparable.instance_methods(false).sort`, `[:<, :<=, :==, :>, :>=, :between?, :clamp]`}, + {`p Enumerable.instance_methods(false).grep(/^__/)`, `[]`}, + // The helpers are private: not answered by respond_to?, no explicit-receiver call. + {`p 5.respond_to?(:__compare)`, `false`}, + {`begin; 5.__compare(3); rescue NoMethodError => e; p e.class; end`, `NoMethodError`}, + // ...but still reachable through send (private) and the operators that use them. + {`p 5.send(:__compare, 3)`, `1`}, + {`p 5 < 10`, `true`}, + {`p 3.clamp(1, 2)`, `2`}, + // Enumerable iteration still works (it goes through the now-private helpers). + {`p (1..3).to_a`, `[1, 2, 3]`}, + {`p [3, 1, 2].sort`, `[1, 2, 3]`}, + {`p [1, 2, 3, 4].first(2)`, `[1, 2]`}, + {`p [1, 2, 3].map { |x| x * 2 }`, `[2, 4, 6]`}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want+"\n" { + t.Errorf("src=%q got=%q want=%q", c.src, got, c.want+"\n") + } + } +}