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:

  1. 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 to str_poly_hash matches the slot to a poly value bundle the codegen synthesizes from the kwargs. Without this, def self.create(attrs = {}) left attrs typed str_int_hash, so the existing kwarg-as-bundle arm in compile_constructor_args (which only fires for str_poly_hash) never engaged.

  2. 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 fresh sp_StrPolyHash populated from the leftover kwargs (each value boxed via box_expr_to_poly). compile_typed_call_args gets the same treatment via a per-invocation @typed_call_bundle_slot / @typed_call_bundle_expr snapshot 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

@@ -11025,9 +11025,18 @@
1102511025 # body's `attrs[...]` accesses then resolve through the
1102611026 # str_poly_hash poly-recv dispatch instead of the unresolved
1102711027 # "[] on int" fallback.
11028+ #
11029+ # Also fires when the slot is already a narrow hash type from a
11030+ # `attrs = {}` default — `def self.create(attrs = {})` infers
11031+ # `attrs` as `str_int_hash` from the literal default. A
11032+ # `Class.create(article_id: 1, body: "...")` call site has
11033+ # mixed-type values that don't fit str_int_hash; widen so the
11034+ # codegen bundle emits sp_StrPolyHash_set with boxed values.
1102811035 if any_matched == 0 && pos_idx < ptypes.length
11029- if ptypes[pos_idx] == "int" || ptypes[pos_idx] == "nil"
11036+ pt_w = ptypes[pos_idx]
11037+ if pt_w == "int" || pt_w == "nil" || is_hash_type(pt_w) == 1
1103011038 @needs_rb_value = 1
11039+ @needs_str_poly_hash = 1
1103111040 ptypes[pos_idx] = "str_poly_hash"
1103211041 end
1103311042 pos_idx = pos_idx + 1