Skip to content

Coerce a non-Hash keyword splat (**obj) via #to_hash - #504

Merged
tannevaled merged 1 commit into
mainfrom
feat/kwsplat-to-hash
Aug 30, 2026
Merged

tannevaled merged 1 commit into
mainfrom
feat/kwsplat-to-hash

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

A **operand keyword splat whose operand was not already a Hash raised TypeError unconditionally, so f(**obj) for an object with #to_hash — and f(**nil) — failed where MRI accepts them.

The OpHashMerge handler now mirrors MRI's implicit keyword-splat conversion:

  • nil contributes nothing (f(**nil) ≡ f());
  • any other non-Hash operand is coerced with #to_hash;
  • a value with no #to_hash raises no implicit conversion of X into Hash;
  • a value whose #to_hash yields a non-Hash raises can't convert X to Hash (X#to_hash gives Y).

A Hash operand and a hash-literal ** stay on the unchanged fast path.

Verified byte-for-byte against MRI Ruby 4.0.6, including the Encoding::Converter.new(…, **opts) spec whose options object carries #to_hash.

Conformance: ruby/spec ratchet FLOOR 16315 → 16335 (measured total 16391). go test ./... green, native + wasip1/wasm build, gofmt clean, 100% coverage of the new branches.

🤖 Generated with Claude Code

A `**operand` keyword splat whose operand was not already a Hash raised
TypeError unconditionally, so `f(**obj)` for an object with #to_hash — and
`f(**nil)` — failed where MRI accepts them. The OpHashMerge handler now
mirrors MRI's implicit keyword-splat conversion: nil contributes nothing
(`f(**nil)` is `f()`), any other non-Hash operand is coerced with #to_hash,
and a value with no #to_hash ("no implicit conversion of X into Hash") or
whose #to_hash yields a non-Hash ("can't convert X to Hash (X#to_hash
gives Y)") raises TypeError with MRI's message. A Hash operand and a
hash-literal `**` stay on the unchanged fast path.

Verified against MRI Ruby 4.0.6, including the `Encoding::Converter.new(…,
**opts)` spec whose options carry #to_hash. ruby/spec ratchet FLOOR
16305 -> 16335 (measured total 16391).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 17fdd9d into main Aug 30, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/kwsplat-to-hash branch August 30, 2026 12:54
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