Skip to content

Match MRI for sprintf hex-float strings and named-key KeyError - #506

Merged
tannevaled merged 1 commit into
mainfrom
feat/agent-sprintf
Aug 30, 2026
Merged

tannevaled merged 1 commit into
mainfrom
feat/agent-sprintf

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Advances MRI-Ruby-4.0.6 conformance of the sprintf / Kernel#format /
String#% cluster, entirely within the rbgo<->go-ruby-format binding
layer (internal/vm/format_bind.go). Every behaviour is verified
byte-for-byte against MRI.

Fixes

  1. Hexadecimal-float String operands — parseFormatFloat now parses
    a String exactly as Kernel#Float (the "%e/%f/%g behaves as if
    calling Kernel#Float" rule) by routing through
    normalizeFloatLiteral: "%f" % "0xA" => "10.000000", and an
    out-of-range magnitude yields ±Inf / 0.0 rather than raising. Decimal
    parsing is unchanged. Previously any hex string raised
    ArgumentError.

  2. KeyError#key / #receiver for named references — an unmatched
    %<name> / %{name} reference now raises a KeyError whose #key
    is the missing key as a Symbol and whose #receiver is the very
    hash operand (object identity), matching MRI.

  3. Un-doubled coercion messages — parseFormatInteger /
    parseFormatFloat return only the bare message on a malformed
    operand. The engine wraps err.Error(), so returning a
    *format.Error (whose Error() is "Class: Message") doubled the
    prefix. "%d" % "abc" now reports invalid value for Integer(): "abc" instead of ArgumentError: invalid value for Integer(): "abc".

Per-file spec deltas (ruby/spec, MRI 4.0.6)

spec before after
core/string/modulo_spec.rb 245 pass 252 pass (+7)
core/kernel/sprintf_spec.rb 5 pass 5 pass
core/kernel/format_spec.rb 0 pass 0 pass

The +7 in modulo_spec are the five %e/%E/%f/%g/%G hex-string
examples and the two "sets the unmatched key as the key of KeyError"
examples.

Out of scope (engine / harness, not the binding)

Remaining failures live outside this repo's binding layer: the harness's
unsupported complain matcher, the String encoding subsystem
(US-ASCII/BINARY negotiation, Encoding::CompatibilityError), and
go-ruby-format engine internals (%#a alternate form, %0…a
zero-fill placement, %c to_str coercion, * positional
width/precision, Hash#default for named refs which needs an engine
lookup callback). These would need changes in the separate
go-ruby-format/format module.

Verification

  • go test ./internal/vm/ and go test ./... green (GOWORK=off CGO_ENABLED=0).
  • GOOS=wasip1 GOARCH=wasm go build ./... OK.
  • gofmt -l empty on changed files.
  • New / changed code 100% covered
    (internal/vm/sprintf_conformance_test.go +
    format_bind_internal_test.go).

🤖 Generated with Claude Code

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> / %{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 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 2db7133 into main Aug 30, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/agent-sprintf branch August 30, 2026 14:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant