Commit abc369ae

abc369aebf68Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +1 added · ~3 modified

Description

Collapse keyword args into a Symbol-keyed positional hash param

A method with no keyword parameters that receives keyword arguments (configure(timeout: 5) for def configure(opts = {})) must fold them into the trailing positional Hash, keyed by the kwarg symbols, the way CRuby does. The top-level call path dropped the kwargs entirely (passing the empty {} default) and the constructor path bundled them into a string-keyed str_poly_hash, so the ubiquitous DEFAULTS.merge(opts) options idiom raised undefined method 'merge' for sym_poly_hash -- the sym-keyed constant could not merge a str-keyed or empty bundle.

  • analyze: widen the receiving positional slot to sym_poly_hash in both scan_new_calls (top-level methods) and widen_ptypes_from_args (the constructor / class-method path), skipping **h double-splats and calls that match a real keyword param so kw-param methods are untouched.
  • codegen: build the bundle as a sp_SymPolyHash with symbol keys at all three call-arg sites (compile_call_args_with_defaults, the constructor path, and compile_typed_call_args).
  • merge: when a sym_poly_hash receiver merges the empty {} default (str_int_hash), re-intern the str keys to symbols so a never-widened options param still returns the receiver's defaults.

The kwargs bundle is now Symbol-keyed (matching CRuby) rather than the previous string-keyed quirk; bundle_classd_13.rb is updated to read the bundle by symbol (attrs[:title]). Adds kwargs_merge_default_hash.rb.

Self-host fixpoint OK; 845 tests pass; optcarrot checksum 59662; bench green.

Fixes #1331

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
abc369aebf68
Parent
164ce35025db

spinel_analyze.rbmodifiedView file

@@ -6393,11 +6393,24 @@
63936393 @needs_rb_value = 1
63946394 return "sym_poly_hash"
63956395 end
6396- # Cross-key-shape: sym recv + str arg -> result has mixed
6397- # keys. Promote to str_poly_hash; codegen-side merge dispatch
6398- # routes both sides through StrPolyHash converters.
6399- if arg_t_mg == "str_int_hash" || arg_t_mg == "str_str_hash" || arg_t_mg == "str_poly_hash"
6396+ # sym recv + empty `{}` default arg: an options param that's
6397+ # never widened by a kwarg call site keeps its str_int_hash
6398+ # default type (`def m(opts = {}); SYM_CONST.merge(opts); end;
6399+ # m`). CRuby folds the (empty) hash into the receiver's symbol
6400+ # keys, so the result is sym_poly_hash; codegen re-interns any
6401+ # str arg keys to symbols.
6402+ if arg_t_mg == "str_int_hash"
64006403 @needs_rb_value = 1
6404+ @needs_sym_poly_hash = 1
6405+ # codegen re-interns the str arg's keys via sp_sym_intern.
6406+ @needs_sym_intern = 1
6407+ return "sym_poly_hash"
6408+ end
6409+ # Cross-key-shape: sym recv + str-keyed arg with genuine string
6410+ # keys -> result has mixed keys. Promote to str_poly_hash;
6411+ # codegen routes both sides through StrPolyHash converters.
6412+ if arg_t_mg == "str_str_hash" || arg_t_mg == "str_poly_hash"
6413+ @needs_rb_value = 1
64016414 return "str_poly_hash"
64026415 end
64036416 end
@@ -15905,24 +15918,24 @@
1590515918 end
1590615919 # Bundle-as-positional fallback: no kwarg matched a param name
1590715920 # AND there's an unfilled positional slot still at the default
15908- # int type. Widen it to str_poly_hash (string keys from
15909- # Symbol-to-s conversion; poly values for kwarg mixing). The
15910- # body's `attrs[...]` accesses then resolve through the
15911- # str_poly_hash poly-recv dispatch instead of the unresolved
15912- # "[] on int" fallback.
15921+ # int type. The kwargs collapse into that positional slot as a
15922+ # Symbol-keyed hash (CRuby folds trailing unmatched kwargs into a
15923+ # positional Hash whose keys are the kwarg *symbols*). Widen the
15924+ # slot to sym_poly_hash so `attrs[:k]` resolves and the constant
15925+ # idiom `DEFAULTS.merge(attrs)` dispatches sym-keyed merge.
1591315926 #
1591415927 # Also fires when the slot is already a narrow hash type from a
1591515928 # `attrs = {}` default — `def self.create(attrs = {})` infers
1591615929 # `attrs` as `str_int_hash` from the literal default. A
1591715930 # `Class.create(article_id: 1, body: "...")` call site has
1591815931 # mixed-type values that don't fit str_int_hash; widen so the
15919- # codegen bundle emits sp_StrPolyHash_set with boxed values.
15932+ # codegen bundle emits sp_SymPolyHash_set with boxed values.
1592015933 if any_matched == 0 && pos_idx < ptypes.length
1592115934 pt_w = ptypes[pos_idx]
1592215935 if pt_w == "int" || pt_w == "nil" || is_hash_type(pt_w) == 1
1592315936 @needs_rb_value = 1
15924- @needs_str_poly_hash = 1
15925- ptypes[pos_idx] = "str_poly_hash"
15937+ @needs_sym_poly_hash = 1
15938+ ptypes[pos_idx] = "sym_poly_hash"
1592615939 end
1592715940 pos_idx = pos_idx + 1
1592815941 end
@@ -16077,9 +16090,13 @@
1607716090 while ak < arg_ids.length
1607816091 if @nd_type[arg_ids[ak]] == "KeywordHashNode"
1607916092 elems = parse_id_list(@nd_elements[arg_ids[ak]])
16093+ any_matched_tl = 0
16094+ named_assoc_tl = 0
16095+ assoc_splat_tl = 0
1608016096 ek = 0
1608116097 while ek < elems.length
1608216098 if @nd_type[elems[ek]] == "AssocNode"
16099+ named_assoc_tl = 1
1608316100 key_id = @nd_key[elems[ek]]
1608416101 if key_id >= 0
1608516102 kname = ""
@@ -16094,13 +16111,38 @@
1609416111 if pi < ptypes.length
1609516112 ptypes[pi] = unify_call_types(ptypes[pi], at, @nd_expression[elems[ek]])
1609616113 end
16114+ any_matched_tl = 1
1609716115 end
1609816116 pi = pi + 1
1609916117 end
1610016118 end
16119+ elsif @nd_type[elems[ek]] == "AssocSplatNode"
16120+ assoc_splat_tl = 1
1610116121 end
1610216122 ek = ek + 1
1610316123 end
16124+ # Bundle-as-positional fallback for a top-level method whose
16125+ # callee has no matching keyword param (and no kwrest): CRuby
16126+ # folds the trailing unmatched kwargs into the next positional
16127+ # slot as a Symbol-keyed Hash. Mirror widen_ptypes_from_args so
16128+ # `configure(timeout: 5)` -> `def configure(opts = {})` widens
16129+ # `opts` to sym_poly_hash, letting `DEFAULTS.merge(opts)` resolve.
16130+ # Skip `foo(**h)`: a double-splat (AssocSplatNode) targets the
16131+ # callee's keyword params / **kwrest, never a positional hash.
16132+ # Require at least one named kwarg so a lone splat doesn't widen
16133+ # a keyword param's slot to sym_poly_hash.
16134+ tl_kwrest = -1
16135+ if mi < @meth_kwrest_index.length
16136+ tl_kwrest = @meth_kwrest_index[mi]
16137+ end
16138+ if any_matched_tl == 0 && named_assoc_tl == 1 && assoc_splat_tl == 0 && rest_param_idx < 0 && tl_kwrest < 0 && ak < ptypes.length
16139+ pt_tlw = ptypes[ak]
16140+ if pt_tlw == "int" || pt_tlw == "nil" || is_hash_type(pt_tlw) == 1
16141+ @needs_rb_value = 1
16142+ @needs_sym_poly_hash = 1
16143+ ptypes[ak] = "sym_poly_hash"
16144+ end
16145+ end
1610416146 else
1610516147 # SplatNode: treat the splat source's element type as
1610616148 # contributing to *every* fixed param from `ak` up to the