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
**hdouble-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
@needs_rb_value = 1 return "sym_poly_hash" end # Cross-key-shape: sym recv + str arg -> result has mixed # keys. Promote to str_poly_hash; codegen-side merge dispatch # routes both sides through StrPolyHash converters. if arg_t_mg == "str_int_hash" || arg_t_mg == "str_str_hash" || arg_t_mg == "str_poly_hash" # sym recv + empty `{}` default arg: an options param that's # never widened by a kwarg call site keeps its str_int_hash # default type (`def m(opts = {}); SYM_CONST.merge(opts); end; # m`). CRuby folds the (empty) hash into the receiver's symbol # keys, so the result is sym_poly_hash; codegen re-interns any # str arg keys to symbols. if arg_t_mg == "str_int_hash" @needs_rb_value = 1 @needs_sym_poly_hash = 1 # codegen re-interns the str arg's keys via sp_sym_intern. @needs_sym_intern = 1 return "sym_poly_hash" end # Cross-key-shape: sym recv + str-keyed arg with genuine string # keys -> result has mixed keys. Promote to str_poly_hash; # codegen routes both sides through StrPolyHash converters. if arg_t_mg == "str_str_hash" || arg_t_mg == "str_poly_hash" @needs_rb_value = 1 return "str_poly_hash" end end end # Bundle-as-positional fallback: no kwarg matched a param name # AND there's an unfilled positional slot still at the default # int type. Widen it to str_poly_hash (string keys from # Symbol-to-s conversion; poly values for kwarg mixing). The # body's `attrs[...]` accesses then resolve through the # str_poly_hash poly-recv dispatch instead of the unresolved # "[] on int" fallback. # int type. The kwargs collapse into that positional slot as a # Symbol-keyed hash (CRuby folds trailing unmatched kwargs into a # positional Hash whose keys are the kwarg *symbols*). Widen the # slot to sym_poly_hash so `attrs[:k]` resolves and the constant # idiom `DEFAULTS.merge(attrs)` dispatches sym-keyed merge. # # Also fires when the slot is already a narrow hash type from a # `attrs = {}` default — `def self.create(attrs = {})` infers # `attrs` as `str_int_hash` from the literal default. A # `Class.create(article_id: 1, body: "...")` call site has # mixed-type values that don't fit str_int_hash; widen so the # codegen bundle emits sp_StrPolyHash_set with boxed values. # codegen bundle emits sp_SymPolyHash_set with boxed values. if any_matched == 0 && pos_idx < ptypes.length pt_w = ptypes[pos_idx] if pt_w == "int" || pt_w == "nil" || is_hash_type(pt_w) == 1 @needs_rb_value = 1 @needs_str_poly_hash = 1 ptypes[pos_idx] = "str_poly_hash" @needs_sym_poly_hash = 1 ptypes[pos_idx] = "sym_poly_hash" end pos_idx = pos_idx + 1 end while ak < arg_ids.length if @nd_type[arg_ids[ak]] == "KeywordHashNode" elems = parse_id_list(@nd_elements[arg_ids[ak]]) any_matched_tl = 0 named_assoc_tl = 0 assoc_splat_tl = 0 ek = 0 while ek < elems.length if @nd_type[elems[ek]] == "AssocNode" named_assoc_tl = 1 key_id = @nd_key[elems[ek]] if key_id >= 0 kname = "" if pi < ptypes.length ptypes[pi] = unify_call_types(ptypes[pi], at, @nd_expression[elems[ek]]) end any_matched_tl = 1 end pi = pi + 1 end end elsif @nd_type[elems[ek]] == "AssocSplatNode" assoc_splat_tl = 1 end ek = ek + 1 end # Bundle-as-positional fallback for a top-level method whose # callee has no matching keyword param (and no kwrest): CRuby # folds the trailing unmatched kwargs into the next positional # slot as a Symbol-keyed Hash. Mirror widen_ptypes_from_args so # `configure(timeout: 5)` -> `def configure(opts = {})` widens # `opts` to sym_poly_hash, letting `DEFAULTS.merge(opts)` resolve. # Skip `foo(**h)`: a double-splat (AssocSplatNode) targets the # callee's keyword params / **kwrest, never a positional hash. # Require at least one named kwarg so a lone splat doesn't widen # a keyword param's slot to sym_poly_hash. tl_kwrest = -1 if mi < @meth_kwrest_index.length tl_kwrest = @meth_kwrest_index[mi] end if any_matched_tl == 0 && named_assoc_tl == 1 && assoc_splat_tl == 0 && rest_param_idx < 0 && tl_kwrest < 0 && ak < ptypes.length pt_tlw = ptypes[ak] if pt_tlw == "int" || pt_tlw == "nil" || is_hash_type(pt_tlw) == 1 @needs_rb_value = 1 @needs_sym_poly_hash = 1 ptypes[ak] = "sym_poly_hash" end end else # SplatNode: treat the splat source's element type as # contributing to *every* fixed param from `ak` up to thespinel_codegen.rbmodifiedView file
Loading diff…
test/bundle_classd_13.rbmodifiedView file
Loading diff…
test/kwargs_merge_default_hash.rbaddedView file
Loading diff…