Commit 4c62268e
4c62268e2452Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
fix(analyze,codegen): bundle unmatched kwargs into a default-hash positional param
CRuby auto-folds trailing kwargs that don't match a kwarg-typed
param into the next positional hash slot. Spinel was dropping
them silently for the attrs = {} shape that ActiveRecord-style
APIs lean on (Class.create(article_id: 1, body: "..."),
Class.new(id: 42, name: "x")).
Two cooperating fixes:
-
Analyzer widen_ptypes_from_args: the "bundle slot is still at the int default" fallback now also fires when the slot is already a narrow hash type (str_int_hash from a literal
{}default, etc.). Widening tostr_poly_hashmatches the slot to a poly value bundle the codegen synthesizes from the kwargs. Without this,def self.create(attrs = {})left attrs typedstr_int_hash, so the existing kwarg-as-bundle arm in compile_constructor_args (which only fires forstr_poly_hash) never engaged. -
Codegen class-method dispatch (the
_cls_<m>resolution arm around line 18860): track which kwargs landed on a named param and, for the first unfilled hash-typed slot, emit a freshsp_StrPolyHashpopulated from the leftover kwargs (each value boxed viabox_expr_to_poly). compile_typed_call_args gets the same treatment via a per-invocation@typed_call_bundle_slot/@typed_call_bundle_exprsnapshot so nested compile_expr re-entry doesn't clobber the outer bundle.
Surfaced by Sam Ruby's roundhouse comment_test.rb
test_belongs_to_article_association (issue #572's "second
failure"): Comment.create(article_id: article.id, ...) against
def self.create(attrs = {}) lowered to
sp_Comment_cls_create(sp_StrIntHash_new()) -- empty hash, so the
created Comment's article_id stayed 0 and the comparison against
article.id (=1) raised "assert_equal failed".
After the fix: CommentTest: 5 tests passed against the
roundhouse archive. ArticleTest still passes (4/4). The
controller tests still fail at C-compile on an unrelated
sp_ActionResponse * vs sp_ActionController_Base * shape that
predates this work.
make test: 563/563 pass.
make optcarrot: checksum 59662 (no regression).
Stage-2 bootstrap: fixpoint clean.
Refs #572
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 4c62268e2452
- Parent
- 2c053fc7f248
spinel_analyze.rbmodifiedView file
# body's `attrs[...]` accesses then resolve through the # str_poly_hash poly-recv dispatch instead of the unresolved # "[] on int" fallback. # # 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. if any_matched == 0 && pos_idx < ptypes.length if ptypes[pos_idx] == "int" || ptypes[pos_idx] == "nil" 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" end pos_idx = pos_idx + 1spinel_codegen.rbmodifiedView file
Loading diff…
test/kwargs_into_default_hash_param.rbaddedView file
Loading diff…
test/kwargs_into_default_hash_param.rb.expectedaddedView file
Loading diff…