From cb60aca2bcc97e399d3526e815c8ebe678de092a Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 30 Aug 2026 15:23:24 +0200 Subject: [PATCH] Match MRI for sprintf hex-float strings and named-key KeyError MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Advance the format-string binding (Kernel#sprintf / Kernel#format / String#%) toward MRI 4.0.6 in three byte-for-byte respects, all in the thin rbgo<->go-ruby-format binding layer: - parseFormatFloat now parses a String operand exactly as Kernel#Float does (the "%e/%f/%g behaves as if calling Kernel#Float" rule), routing through normalizeFloatLiteral: a "0x…" hexadecimal-float literal is accepted ("%f" % "0xA" => "10.000000") and an out-of-range magnitude yields ±Inf / 0.0 rather than an error, while decimal parsing is unchanged. Previously any hex string raised ArgumentError. - An unmatched % / %{name} reference now raises a KeyError whose #key is the missing key as a Symbol and whose #receiver is the very hash operand (identity), matching MRI's KeyError#key / #receiver. - parseFormatInteger / parseFormatFloat return only the bare message on a malformed operand. The engine promotes a parse error by wrapping err.Error(); a *format.Error there (whose Error() is "Class: Message") doubled the prefix, so "%d" % "abc" reported "ArgumentError: invalid value for Integer(): \"abc\"". It now reads "invalid value for Integer(): \"abc\"" as in MRI. Spec deltas (ruby/spec, MRI 4.0.6): - core/string/modulo_spec.rb: 245 -> 252 pass (+7: five %e/%E/%f/%g/%G hex-string examples, two "sets the unmatched key" KeyError examples). - core/kernel/sprintf_spec.rb / format_spec.rb: unchanged; their remaining failures are the harness's unsupported `complain` matcher, the encoding subsystem, and engine-internal parsing (%a alternate form, %c to_str coercion, `*` positional width/precision), all outside this binding. New code is covered 100% by internal/vm/sprintf_conformance_test.go plus the updated format_bind_internal_test.go; every expectation is verified against MRI. Co-Authored-By: Claude Opus 4.8 --- internal/vm/format_bind.go | 84 +++++++++++---- internal/vm/format_bind_internal_test.go | 2 +- internal/vm/sprintf_conformance_test.go | 127 +++++++++++++++++++++++ 3 files changed, 194 insertions(+), 19 deletions(-) create mode 100644 internal/vm/sprintf_conformance_test.go diff --git a/internal/vm/format_bind.go b/internal/vm/format_bind.go index 0f47a2a6..3aa667d1 100644 --- a/internal/vm/format_bind.go +++ b/internal/vm/format_bind.go @@ -5,6 +5,7 @@ package vm import ( + "errors" "math/big" "strconv" "strings" @@ -209,26 +210,43 @@ func parseFormatInteger(s, inspect string) (*big.Int, error) { // "0777"->511, "0b1101_0000"->208, but "123__456"/"_1"/"08" raise ArgumentError. v, ok := intFromString(s, 0) if !ok { - return nil, &format.Error{ - Class: "ArgumentError", - Message: "invalid value for Integer(): " + inspect, - } + // Return only the message: the format engine promotes a non-nil parse + // error to an ArgumentError by wrapping err.Error(), so a *format.Error + // here (whose Error() is "Class: Message") would double the class prefix. + return nil, errors.New("invalid value for Integer(): " + inspect) } z, _ := object.BigOf(v) return z, nil } -// parseFormatFloat parses a String operand for a float conversion as MRI's -// Float() does for sprintf: surrounding whitespace is trimmed. A malformed -// value yields a non-nil error whose message matches MRI's -// `invalid value for Float(): `. +// parseFormatFloat parses a String operand for a float conversion exactly as +// MRI's Kernel#Float does (the "%e/%f/%g behaves as if calling Kernel#Float" +// rule): it honours a "0x…" hexadecimal-float literal ("0xA" -> 10.0), applies +// MRI's strict digit-separator underscore placement, and treats an out-of-range +// magnitude as ±Inf / 0.0 (overflow/underflow) rather than an error — routing +// through the same normalizeFloatLiteral primitive as Kernel#Float so the two +// stay byte-for-byte identical. A genuinely malformed value yields a non-nil +// error whose message matches MRI's `invalid value for Float(): ` (the +// library promotes it to an ArgumentError). func parseFormatFloat(s, inspect string) (float64, error) { - f, err := strconv.ParseFloat(strings.TrimSpace(s), 64) + // Return only the message: the format engine promotes a non-nil parse error + // to an ArgumentError by wrapping err.Error(), so a *format.Error here (whose + // Error() is "Class: Message") would double the class prefix. + badFloat := func() error { + return errors.New("invalid value for Float(): " + inspect) + } + norm, ok := normalizeFloatLiteral(s) + if !ok { + return 0, badFloat() + } + f, err := strconv.ParseFloat(norm, 64) if err != nil { - return 0, &format.Error{ - Class: "ArgumentError", - Message: "invalid value for Float(): " + inspect, + // An out-of-range literal is not malformed: MRI yields ±Infinity (overflow) + // or 0.0 (underflow), which is exactly what ParseFloat returns with ErrRange. + if ne, ok := err.(*strconv.NumError); ok && ne.Err == strconv.ErrRange { + return f, nil } + return 0, badFloat() } return f, nil } @@ -277,7 +295,7 @@ func (vm *VM) formatString(fmtStr string, args []object.Value) string { } out, err := format.Format(fmtStr, vals, vm.formatNamedArgs(args)) if err != nil { - raiseFormatError(err) + vm.raiseFormatError(err, args) } return out } @@ -286,12 +304,42 @@ func (vm *VM) formatString(fmtStr string, args []object.Value) string { // exception: a *format.Error carries the MRI exception class and message // (ArgumentError / KeyError / TypeError) verbatim; any other error (which the // library never produces, but is handled defensively) surfaces as an -// ArgumentError. It never returns. -func raiseFormatError(err error) { - if fe, ok := err.(*format.Error); ok { - raise(fe.Class, "%s", fe.Message) +// ArgumentError. Every KeyError the engine raises comes from an unmatched +// %/%{name} reference, which is possible only when the sole operand is a +// hash; that KeyError is re-raised with MRI's #key (the missing key as a Symbol) +// and #receiver (that hash) set, so `rescue KeyError => e; e.key; e.receiver` +// behave as MRI does. args is the positional operand list. It never returns. +func (vm *VM) raiseFormatError(err error, args []object.Value) { + fe, ok := err.(*format.Error) + if !ok { + raise("ArgumentError", "%s", err.Error()) } - raise("ArgumentError", "%s", err.Error()) + if fe.Class == "KeyError" { + var receiver object.Value + if len(args) == 1 { + if h, isHash := args[0].(*object.Hash); isHash { + receiver = h + } + } + vm.raiseWithIvars("KeyError", fe.Message, map[string]object.Value{ + "@key": object.Symbol(namedKeyFromMessage(fe.Message)), + "@receiver": receiver, + }) + } + raise(fe.Class, "%s", fe.Message) +} + +// namedKeyFromMessage extracts the referenced key from the format engine's +// unmatched-reference KeyError message, which is `key{NAME} not found` for a +// %{NAME} reference and `key not found` for a % one; either bracket +// style yields the same MRI #key. The NAME token never itself contains a +// reference bracket (such a byte would terminate the reference), so trimming the +// fixed prefix/suffix and the surrounding brackets recovers it exactly. +func namedKeyFromMessage(msg string) string { + inner := strings.TrimPrefix(strings.TrimSuffix(msg, " not found"), "key") + return strings.TrimFunc(inner, func(r rune) bool { + return r == '{' || r == '}' || r == '<' || r == '>' + }) } // formatArgs unpacks the right-hand operand of String#%: an Array spreads into diff --git a/internal/vm/format_bind_internal_test.go b/internal/vm/format_bind_internal_test.go index 8d3d9b72..c9dac8ea 100644 --- a/internal/vm/format_bind_internal_test.go +++ b/internal/vm/format_bind_internal_test.go @@ -128,5 +128,5 @@ func TestRaiseFormatErrorFallback(t *testing.T) { t.Fatalf("want ArgumentError boom, got %v", re) } }() - raiseFormatError(errors.New("boom")) + New(io.Discard).raiseFormatError(errors.New("boom"), nil) } diff --git a/internal/vm/sprintf_conformance_test.go b/internal/vm/sprintf_conformance_test.go new file mode 100644 index 00000000..cb14ab5a --- /dev/null +++ b/internal/vm/sprintf_conformance_test.go @@ -0,0 +1,127 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm_test + +import ( + "strings" + "testing" +) + +// TestSprintfFloatStringHex covers the format binding's parseFormatFloat: a +// String operand for a float conversion is parsed exactly as MRI's Kernel#Float +// (the "%f/%e/%g behaves as if calling Kernel#Float" rule), so a hexadecimal +// float literal is accepted and an out-of-range magnitude yields ±Inf / 0.0 +// rather than an error. Every expectation is byte-for-byte against MRI 4.0. +func TestSprintfFloatStringHex(t *testing.T) { + tests := []struct { + name, src, want string + }{ + // Hexadecimal float strings (the previously-failing case): "0xA" == 10.0. + {"hex_f", `p("%f" % "0xA")`, "\"10.000000\"\n"}, + {"hex_e", `p("%e" % "0xA")`, "\"1.000000e+01\"\n"}, + {"hex_E", `p("%E" % "0xA")`, "\"1.000000E+01\"\n"}, + {"hex_g", `p("%g" % "0xA")`, "\"10\"\n"}, + {"hex_G", `p("%G" % "0xA")`, "\"10\"\n"}, + {"hex_scaled", `p("%f" % "0x1.8p3")`, "\"12.000000\"\n"}, + {"hex_signed", `p("%f" % "-0xA")`, "\"-10.000000\"\n"}, + // Overflow -> +Infinity, underflow -> 0.0 (strconv's ErrRange path). + {"overflow", `p("%f" % "1e400")`, "\"Inf\"\n"}, + {"underflow", `p("%f" % "1e-400")`, "\"0.000000\"\n"}, + // Decimal strings and underscores keep working (regression guard). + {"decimal", `p("%f" % "1.5")`, "\"1.500000\"\n"}, + {"integerish", `p("%f" % "10")`, "\"10.000000\"\n"}, + {"underscore", `p("%f" % "1_000.5")`, "\"1000.500000\"\n"}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + if got := eval(t, tc.src); got != tc.want { + t.Errorf("src=%q\n got=%q\nwant=%q", tc.src, got, tc.want) + } + }) + } +} + +// TestSprintfFloatStringErrors covers parseFormatFloat's error path: a malformed +// String operand raises ArgumentError with MRI's exact, un-doubled message +// "invalid value for Float(): " (the engine wraps err.Error(), so the +// binding must return only the message, not a class-prefixed *format.Error). +func TestSprintfFloatStringErrors(t *testing.T) { + tests := []struct{ name, src, wantMsg string }{ + {"garbage", `"%f" % "xyz"`, `invalid value for Float(): "xyz"`}, + {"binary_prefix", `"%f" % "0b1"`, `invalid value for Float(): "0b1"`}, + {"octal_prefix", `"%f" % "0o17"`, `invalid value for Float(): "0o17"`}, + {"bad_hex_underscore", `"%f" % "0x1_p0"`, `invalid value for Float(): "0x1_p0"`}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + err := runErr(t, tc.src) + if err == nil { + t.Fatalf("src=%q: expected ArgumentError, got nil", tc.src) + } + if !strings.Contains(err.Error(), "ArgumentError") { + t.Errorf("src=%q: want ArgumentError, got %v", tc.src, err) + } + // The message must not carry a doubled "ArgumentError: " prefix. + if !strings.Contains(err.Error(), tc.wantMsg) || strings.Contains(err.Error(), "ArgumentError: ArgumentError") { + t.Errorf("src=%q: want message %q un-doubled, got %v", tc.src, tc.wantMsg, err) + } + }) + } +} + +// TestSprintfIntegerStringErrorMessage covers parseFormatInteger's error path: +// like the float path, a malformed integer String operand raises ArgumentError +// with the un-doubled "invalid value for Integer(): " message. +func TestSprintfIntegerStringErrorMessage(t *testing.T) { + got := eval(t, `begin; "%d" % "abc"; rescue => e; p e.message; end`) + want := "\"invalid value for Integer(): \\\"abc\\\"\"\n" + if got != want { + t.Errorf("got=%q want=%q", got, want) + } +} + +// TestSprintfIntegerStringOK guards the parseFormatInteger success path: a valid +// radix-prefixed String operand still formats (so the error-path change did not +// disturb the accepted case). +func TestSprintfIntegerStringOK(t *testing.T) { + if got := eval(t, `p("%d" % "0x1f")`); got != "\"31\"\n" { + t.Errorf("got=%q want %q", got, "\"31\"\n") + } +} + +// TestSprintfNamedKeyError covers raiseFormatError's KeyError path: an unmatched +// %{name} / % reference raises a KeyError whose #key is the missing key as +// a Symbol and whose #receiver is the very hash operand (identity), matching +// MRI. Both bracket styles are exercised (namedKeyFromMessage handles each). +func TestSprintfNamedKeyError(t *testing.T) { + // %{name}: message, class, and #key. + got := eval(t, `begin; "%{foo}" % {a: 5}; rescue KeyError => e; p [e.message, e.key]; end`) + if want := "[\"key{foo} not found\", :foo]\n"; got != want { + t.Errorf("braced: got=%q want=%q", got, want) + } + // %: #key.to_s and #receiver identity with the passed hash. + got = eval(t, `h = {a: 5} +begin + "%s" % h +rescue KeyError => e + p [e.key.to_s, e.receiver.equal?(h)] +end`) + if want := "[\"foo\", true]\n"; got != want { + t.Errorf("angle: got=%q want=%q", got, want) + } +} + +// TestSprintfNonKeyErrorUnaffected guards raiseFormatError's non-KeyError branch: +// a ClassName-driven TypeError from an unconvertible operand and an ArgumentError +// from a bad literal still surface with their own class (the KeyError special- +// casing does not intercept them). +func TestSprintfNonKeyErrorUnaffected(t *testing.T) { + if err := runErr(t, `"%d" % :sym`); err == nil || !strings.Contains(err.Error(), "TypeError") { + t.Errorf("want TypeError, got %v", err) + } + if err := runErr(t, `"%d" % "abc"`); err == nil || !strings.Contains(err.Error(), "ArgumentError") { + t.Errorf("want ArgumentError, got %v", err) + } +}