Commit fa88c483
fa88c4832cfaMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +1 added · ~2 modified
Description
Implement Hash#flatten / #rassoc / #compact on typed hashes
These three previously raised NoMethodError on every typed hash variant. Iterate the variant's insertion order and reuse the variant's get helper:
- flatten interleaves boxed keys and values into a poly_array ([:a, 1, :b, 2]).
- rassoc finds the first pair whose value matches (via sp_poly_eq on the boxed value), returning a [k, v] poly_array or nil on a miss.
- compact copies entries into a fresh same-type hash, dropping nil values for poly-valued variants (other value types can't hold nil, so it's a straight copy), preserving the hash variant.
Covers the sym/str/int-keyed int/str/poly-valued variants. Adding new dispatches rather than retyping an existing op keeps the self-host fixpoint stable. Verified against CRuby; full suite + self-host fixpoint
- optcarrot all green.
Refs #1227
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- fa88c4832cfa
- Parent
- cc46c1a4533c
spinel_analyze.rbmodifiedView file
@@ -6157,6 +6157,12 @@
61576157
if mname == "flatten"61586158
if recv >= 061596159
rt_fl = infer_type(recv)6160+
# `hash.flatten` interleaves keys and values into a flat boxed6161+
# poly_array ([:a, 1, :b, 2]).6162+
if is_hash_type(rt_fl) == 16163+
@needs_rb_value = 16164+
return "poly_array"6165+
end61606166
# A typed-array-of-<T>_array (e.g. `[[1,2],[3,4]]` ->61616167
# int_array_ptr_array) flattens to a flat <T>_array. Issue #73961626168
# added the int shape; str / float mirror it.spinel_codegen.rbmodifiedView file
Loading diff…
test/hash_flatten_rassoc_compact.rbaddedView file
Loading diff…