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 to str_poly_hash (string keys via sym->s interning, poly values for kwarg-value variety).
  • spinel_codegen.rb: compile_constructor_args recognises 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

@@ -10604,7 +10604,15 @@
1060410604 while ai < arg_ids.length
1060510605 aid = arg_ids[ai]
1060610606 if @nd_type[aid] == "KeywordHashNode"
10607+ # Try the existing per-key matching first: if a kwarg key name
10608+ # matches a param name, widen that param's slot. Track whether
10609+ # any key matched so we can fall back to "bundle the whole
10610+ # KeywordHashNode into the next positional slot" for the
10611+ # `Class.new(title: ...)` shape where the receiver's
10612+ # initialize is `def initialize(attrs)` -- a single
10613+ # positional param expecting the kwargs as a hash. Issue #530.
1060710614 elems = parse_id_list(@nd_elements[aid])
10615+ any_matched = 0
1060810616 ei = 0
1060910617 while ei < elems.length
1061010618 if @nd_type[elems[ei]] == "AssocNode"
@@ -10620,6 +10628,7 @@
1062010628 if pnames[pi] == kname && pi < ptypes.length
1062110629 at = infer_type(val_id)
1062210630 ptypes[pi] = unify_call_types(ptypes[pi], at, val_id)
10631+ any_matched = 1
1062310632 end
1062410633 pi = pi + 1
1062510634 end
@@ -10627,6 +10636,20 @@
1062710636 end
1062810637 ei = ei + 1
1062910638 end
10639+ # Bundle-as-positional fallback: no kwarg matched a param name
10640+ # AND there's an unfilled positional slot still at the default
10641+ # int type. Widen it to str_poly_hash (string keys from
10642+ # Symbol-to-s conversion; poly values for kwarg mixing). The
10643+ # body's `attrs[...]` accesses then resolve through the
10644+ # str_poly_hash poly-recv dispatch instead of the unresolved
10645+ # "[] on int" fallback.
10646+ if any_matched == 0 && pos_idx < ptypes.length
10647+ if ptypes[pos_idx] == "int" || ptypes[pos_idx] == "nil"
10648+ @needs_rb_value = 1
10649+ ptypes[pos_idx] = "str_poly_hash"
10650+ end
10651+ pos_idx = pos_idx + 1
10652+ end
1063010653 else
1063110654 if pos_idx < ptypes.length
1063210655 at = infer_type(aid)