Commit 07c2faa7
07c2faa78c2eMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +1 added · ~2 modified
Description
Support Struct#to_h with a block
Struct#to_h { |name, value| [k, v] } was silently wrong: codegen
ignored the block and analyze typed the block params as int, so the
plain symbol-keyed hash came back instead of the block's transformation.
Yield each member's (symbol, value) pair to the block and build a poly-keyed poly hash from the returned [k, v] arrays:
- block_param_type_at types the to_h block params as (symbol, poly),
- the to_h call returns poly_poly_hash when a block is present,
- codegen inlines the block per member and stores the returned pair into a PolyPolyHash, normalizing whatever array variant the block returns to boxed poly elements.
Gated on a synthetic Struct with no user-defined to_h, so non-struct to_h and the plain (block-less) form are unaffected. Addresses J3 of the Time/Struct/Object method-gap bundle.
Refs #1222
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- 07c2faa78c2e
- Parent
- e9b946576a44
spinel_analyze.rbmodifiedView file
@@ -5737,6 +5737,13 @@
57375737
sci_sv = struct_recv_ci(recv)57385738
if sci_sv >= 0 && cls_find_method(sci_sv, mname) < 057395739
if mname == "to_h"5740+
# Block form transforms each (name, value) pair into an5741+
# arbitrary [k, v], so the keys/values are dynamic -> a5742+
# poly-keyed poly hash. Plain form keeps symbol member keys.5743+
if @nd_block[nid] >= 05744+
@needs_poly_poly_hash = 15745+
return "poly_poly_hash"5746+
end57405747
@needs_sym_poly_hash = 157415748
return "sym_poly_hash"57425749
end@@ -32892,6 +32899,20 @@
3289232899
if recv >= 03289332900
recv_t = infer_type(recv)3289432901
end32902+
# Struct#to_h { |member_name, value| ... } yields the member symbol32903+
# and its value. Members are heterogeneous, so the value param is32904+
# poly. Without this the params default to int and the block body32905+
# (e.g. `name.to_s`) dispatches on the wrong type.32906+
if mname == "to_h" && recv >= 032907+
sci_bp = struct_recv_ci(recv)32908+
if sci_bp >= 0 && cls_find_method(sci_bp, "to_h") < 032909+
if pi == 032910+
return "symbol"32911+
end32912+
@needs_rb_value = 132913+
return "poly"32914+
end32915+
end3289532916
# No-recv call to a user-defined top-level method that uses yield:3289632917
# look up the method body's yield arg type at position pi. Without3289732918
# this, `emit_rows { |row| ... }` against `def emit_rows; yield row;spinel_codegen.rbmodifiedView file
Loading diff…
test/struct_to_h_block.rbaddedView file
Loading diff…