Commit 35122ae5
35122ae5b78dMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
fix(analyze,codegen): Class.new(kw: val) bundles kwargs as positional hash
Sam Ruby's #530 segv localized to roundhouse's
Article.new(title: "", body: "Valid body...") lowering to
sp_Article_new(0). The receiver's initialize takes a single
positional attrs param; per CRuby semantics, kwargs whose
names don't match any param name bundle into that positional
slot as a hash. Spinel's widen_ptypes_from_args matched each
kwarg key against pnames and silently dropped non-matching
ones, leaving the positional at the int default. The call site
emitted 0 for the missing positional, and the body's
attrs[...] dereferenced NULL on macOS (Linux glibc's zero-
init happened to hide the crash).
Two paired changes:
- spinel_analyze.rb:
widen_ptypes_from_args's KeywordHashNode branch now tracks whether any key matched a param. If none matched and there's an unfilled positional, widen that positional's slot tostr_poly_hash(string keys via sym->s interning, poly values for kwarg-value variety). - spinel_codegen.rb:
compile_constructor_argsrecognises the same shape (single-positional init, no kwarg keys match, slot inferred as str_poly_hash) and builds a sp_StrPolyHash from the kwargs at the call site instead of falling through to the "0" default.
The new str_poly_hash carries both string and int values via
sp_box_*; the initialize body's attrs[key] then dispatches
through the existing poly-recv unbox path. Sam's
test/models/article_test.rb pattern compiles + runs without his
explicit-setter rewrite workaround now.
Test (test/new_kwarg_bundle.rb): exercises the bundle shape with two-key kwarg, two distinct call sites widening the same init, and a separate class with mixed value types (string + int) sharing the str_poly_hash slot.
Note: only the single-positional + no-matching-kwarg case is
handled. Multi-positional inits with mixed positional + kwarg
calls (new(a, b, opt1: x)) still take the existing path; the
kwarg-as-bundle widening only fires when there are NO matching
keys, so adding the new arm doesn't perturb the existing
matched-name path.
Closes #530
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 35122ae5b78d
- Parent
- abedaac8402e
spinel_analyze.rbmodifiedView file
while ai < arg_ids.length aid = arg_ids[ai] if @nd_type[aid] == "KeywordHashNode" # Try the existing per-key matching first: if a kwarg key name # matches a param name, widen that param's slot. Track whether # any key matched so we can fall back to "bundle the whole # KeywordHashNode into the next positional slot" for the # `Class.new(title: ...)` shape where the receiver's # initialize is `def initialize(attrs)` -- a single # positional param expecting the kwargs as a hash. Issue #530. elems = parse_id_list(@nd_elements[aid]) any_matched = 0 ei = 0 while ei < elems.length if @nd_type[elems[ei]] == "AssocNode" if pnames[pi] == kname && pi < ptypes.length at = infer_type(val_id) ptypes[pi] = unify_call_types(ptypes[pi], at, val_id) any_matched = 1 end pi = pi + 1 end end ei = ei + 1 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. if any_matched == 0 && pos_idx < ptypes.length if ptypes[pos_idx] == "int" || ptypes[pos_idx] == "nil" @needs_rb_value = 1 ptypes[pos_idx] = "str_poly_hash" end pos_idx = pos_idx + 1 end else if pos_idx < ptypes.length at = infer_type(aid)spinel_codegen.rbmodifiedView file
Loading diff…
test/new_kwarg_bundle.rbaddedView file
Loading diff…
test/new_kwarg_bundle.rb.expectedaddedView file
Loading diff…