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) < 0
57395739 if mname == "to_h"
5740+ # Block form transforms each (name, value) pair into an
5741+ # arbitrary [k, v], so the keys/values are dynamic -> a
5742+ # poly-keyed poly hash. Plain form keeps symbol member keys.
5743+ if @nd_block[nid] >= 0
5744+ @needs_poly_poly_hash = 1
5745+ return "poly_poly_hash"
5746+ end
57405747 @needs_sym_poly_hash = 1
57415748 return "sym_poly_hash"
57425749 end
@@ -32892,6 +32899,20 @@
3289232899 if recv >= 0
3289332900 recv_t = infer_type(recv)
3289432901 end
32902+ # Struct#to_h { |member_name, value| ... } yields the member symbol
32903+ # and its value. Members are heterogeneous, so the value param is
32904+ # poly. Without this the params default to int and the block body
32905+ # (e.g. `name.to_s`) dispatches on the wrong type.
32906+ if mname == "to_h" && recv >= 0
32907+ sci_bp = struct_recv_ci(recv)
32908+ if sci_bp >= 0 && cls_find_method(sci_bp, "to_h") < 0
32909+ if pi == 0
32910+ return "symbol"
32911+ end
32912+ @needs_rb_value = 1
32913+ return "poly"
32914+ end
32915+ end
3289532916 # 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. Without
3289732918 # this, `emit_rows { |row| ... }` against `def emit_rows; yield row;