Commit c9958f27

c9958f2712aaMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): #314 (B) widen kwargs into per-param slots in scan_new_calls

A module class method's required keyword params used to default to "int" forever — the call-site widening pass walked the ArgumentsNode positionally, unifying the trailing KeywordHashNode into ptypes[0] and never touching the per-kwarg slots. Symptom: a method like def self.form_with(model:, model_name:, action:, ...) ended up with all params typed mrb_int regardless of what call sites actually passed.

Three coordinated fixes:

  • New widen_ptypes_from_args helper walks AssocNode children of a KeywordHashNode and unifies each key: value pair into the ptypes slot whose name matches. Wired into the issue-#239 module class method widening site; positional args still unify by index.

  • Compile-side compile_call_args_with_defaults and _splat replaced their hardcoded sp_box_str(kw_vals[ki]) for poly kwargs with box_expr_to_poly(kw_arg_ids[ki]) so the box helper is picked from the arg's actual source type. The pre-fix str hardcoding mistyped any non-string source — exactly the path the poly-widened model: kwarg now flows through.

  • infer_function_body_call_types now pins @current_method_name per method so current_lexical_scope_name's <Mod>_cls_<m> peel resolves bare class refs in the body. Without this, a body like def self.form_with(model:, ...); FormBuilder.new(model, ...); end left find_class_idx("FormBuilder") at -1 (the class is registered as ViewHelpers_FormBuilder) and skipped the constructor-arg widening — so model: widened to poly upstream but FormBuilder.@model stayed mrb_int.

Defensive companion: detect_poly_arg_passed_types excludes from value-type optimization any class whose instances flow into a poly-typed param slot at a call site (whether positional or keyword). The pre-fix path didn't need this because params didn't widen to poly; with this issue's widening fix in place, an inline X.new(...) at a poly-typed kwarg slot would otherwise box a struct-by-value into void * (sp_box_obj's first arg) — same exclusion shape as the existing #118 / poly-return guard.

Surfaced by the Roundhouse archive's form_with cascade. Fixing this collapses the residual 5 errors / 30+ warnings on make spinel-test-build down to the 2-error (A) ArticleRow family.

Refs #314

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
c9958f2712aa
Parent
8ff9aa915a30

spinel_codegen.rbmodifiedView file

@@ -9397,6 +9397,62 @@
93979397 end
93989398 end
93999399
9400+ # Issue #314: widen a callee's `ptypes` array from a single call
9401+ # site's argument list, correctly handling keyword args. Positional
9402+ # args unify by index; a `KeywordHashNode` (kwargs) unifies each
9403+ # `key: value` pair into the slot whose param-name matches the key.
9404+ # Mutates `ptypes` in place; the caller is responsible for joining
9405+ # the result back into the storage table (`@meth_param_types[mi]`,
9406+ # `@cls_cmeth_ptypes[ci]`, etc.).
9407+ #
9408+ # Without kwargs handling, the existing positional-only loop
9409+ # unifies a `KeywordHashNode` into `ptypes[0]` (because the AST
9410+ # presents kwargs as a single trailing hash arg), so a callee's
9411+ # required kwargs default-fall-through-to-int never widens — every
9412+ # kwarg-only call site through a module class method (e.g.
9413+ # `M.render(model: <obj>, label: "x")`) lands as
9414+ # `sp_M_cls_render(mrb_int, mrb_int)` regardless of what the call
9415+ # site actually passes.
9416+ def widen_ptypes_from_args(arg_ids, pnames, ptypes)
9417+ pos_idx = 0
9418+ ai = 0
9419+ while ai < arg_ids.length
9420+ aid = arg_ids[ai]
9421+ if @nd_type[aid] == "KeywordHashNode"
9422+ elems = parse_id_list(@nd_elements[aid])
9423+ ei = 0
9424+ while ei < elems.length
9425+ if @nd_type[elems[ei]] == "AssocNode"
9426+ key_id = @nd_key[elems[ei]]
9427+ val_id = @nd_expression[elems[ei]]
9428+ if key_id >= 0 && val_id >= 0 && @nd_type[key_id] == "SymbolNode"
9429+ kname = @nd_content[key_id]
9430+ if kname == ""
9431+ kname = @nd_name[key_id]
9432+ end
9433+ pi = 0
9434+ while pi < pnames.length
9435+ if pnames[pi] == kname && pi < ptypes.length
9436+ at = infer_type(val_id)
9437+ ptypes[pi] = unify_call_types(ptypes[pi], at, val_id)
9438+ end
9439+ pi = pi + 1
9440+ end
9441+ end
9442+ end
9443+ ei = ei + 1
9444+ end
9445+ else
9446+ if pos_idx < ptypes.length
9447+ at = infer_type(aid)
9448+ ptypes[pos_idx] = unify_call_types(ptypes[pos_idx], at, aid)
9449+ end
9450+ pos_idx = pos_idx + 1
9451+ end
9452+ ai = ai + 1
9453+ end
9454+ end
9455+
94009456 def scan_new_calls(nid)
94019457 if nid < 0
94029458 return
@@ -9767,15 +9823,9 @@
97679823 args_id239 = @nd_arguments[nid]
97689824 if args_id239 >= 0
97699825 arg_ids239 = get_args(args_id239)
9826+ pnames239 = @meth_param_names[mi239].split(",")
97709827 ptypes239 = @meth_param_types[mi239].split(",")
9771- kk239 = 0
9772- while kk239 < arg_ids239.length
9773- at239 = infer_type(arg_ids239[kk239])
9774- if kk239 < ptypes239.length
9775- ptypes239[kk239] = unify_call_types(ptypes239[kk239], at239, arg_ids239[kk239])
9776- end
9777- kk239 = kk239 + 1
9778- end
9828+ widen_ptypes_from_args(arg_ids239, pnames239, ptypes239)
97799829 @meth_param_types[mi239] = ptypes239.join(",")
97809830 end
97819831 end
@@ -13253,6 +13303,16 @@
1325313303 if bid >= 0
1325413304 # Build local scope for this function
1325513305 push_scope
13306+ # Issue #314: pin @current_method_name so
13307+ # current_lexical_scope_name's `<Mod>_cls_<m>` peel resolves
13308+ # bare class refs in the body. Without this, scan_new_calls'
13309+ # constructor branch on `Inner.new(x)` inside `M.make` does
13310+ # find_class_idx("Inner") (returning -1, since the class is
13311+ # registered as `M_Inner`) and skips param widening — so a
13312+ # poly arg flowing in via the kwargs widening above never
13313+ # reaches the inner class's initialize ptypes.
13314+ saved_method_name = @current_method_name
13315+ @current_method_name = @meth_names[mi]
1325613316 pnames = @meth_param_names[mi].split(",")
1325713317 ptypes = @meth_param_types[mi].split(",")
1325813318 pk = 0
@@ -13273,6 +13333,7 @@
1327313333 end
1327413334 # Now scan for calls within this function body
1327513335 scan_new_calls(bid)
13336+ @current_method_name = saved_method_name
1327613337 pop_scope
1327713338 end
1327813339 mi = mi + 1
@@ -14952,6 +15013,131 @@
1495215013 end
1495315014 end
1495415015
15016+ # Issue #314: classes whose instances flow into a poly-typed param
15017+ # slot at any call site. The boxing helper `sp_box_obj(p, ci)` takes
15018+ # `void *p`; a value-type-eligible class would emit
15019+ # `sp_box_obj(sp_<C>_new(...), ci)` (or `sp_box_obj(local, ci)`
15020+ # where `local` is the value-type struct), feeding a struct-by-value
15021+ # into a `void *` slot — a C type error. Excluding such classes
15022+ # from the value-type optimization keeps `<C>` heap-allocated, so
15023+ # the boxing argument is a stable pointer.
15024+ #
15025+ # Surfaces specifically when a kwargs widening pass (this issue's
15026+ # main fix) collapses two or more concrete obj-typed call sites for
15027+ # the same kwarg into "poly" — at which point the call site starts
15028+ # boxing the instance, hitting the same struct-by-void-* mismatch
15029+ # the ptr_array / poly-return passes already guard against.
15030+ def detect_poly_arg_passed_types
15031+ @poly_arg_passed_types = "".split(",")
15032+ nid = 0
15033+ while nid < @nd_type.length
15034+ if @nd_type[nid] == "CallNode"
15035+ # Top-level / module class method (no recv, or recv is module
15036+ # constant). Look up the callee in @meth_*.
15037+ mfn = ""
15038+ recv = @nd_receiver[nid]
15039+ mname = @nd_name[nid]
15040+ if recv < 0
15041+ mfn = mname
15042+ else
15043+ if @nd_type[recv] == "ConstantReadNode" || @nd_type[recv] == "ConstantPathNode"
15044+ rcn = resolve_const_ref_name(recv)
15045+ if rcn != "" && module_name_exists(rcn) == 1
15046+ mfn = rcn + "_cls_" + mname
15047+ end
15048+ end
15049+ end
15050+ if mfn != ""
15051+ mi = find_method_idx(mfn)
15052+ if mi >= 0
15053+ pnames = @meth_param_names[mi].split(",")
15054+ ptypes = @meth_param_types[mi].split(",")
15055+ args_id = @nd_arguments[nid]
15056+ if args_id >= 0
15057+ arg_ids = get_args(args_id)
15058+ record_obj_args_into_poly_params(arg_ids, pnames, ptypes)
15059+ end
15060+ end
15061+ end
15062+ # Constructor `<C>.new(args)` — params live in @cls_meth_*.
15063+ if mname == "new" && recv >= 0
15064+ cname2 = constructor_class_name(recv)
15065+ if cname2 != ""
15066+ ci2 = find_class_idx(cname2)
15067+ if ci2 >= 0
15068+ init_idx = cls_find_method_direct(ci2, "initialize")
15069+ if init_idx >= 0
15070+ all_params = @cls_meth_params[ci2].split("|")
15071+ all_ptypes = @cls_meth_ptypes[ci2].split("|")
15072+ pnames2 = "".split(",")
15073+ ptypes2 = "".split(",")
15074+ if init_idx < all_params.length
15075+ pnames2 = all_params[init_idx].split(",")
15076+ end
15077+ if init_idx < all_ptypes.length
15078+ ptypes2 = all_ptypes[init_idx].split(",")
15079+ end
15080+ args_id2 = @nd_arguments[nid]
15081+ if args_id2 >= 0
15082+ record_obj_args_into_poly_params(get_args(args_id2), pnames2, ptypes2)
15083+ end
15084+ end
15085+ end
15086+ end
15087+ end
15088+ end
15089+ nid = nid + 1
15090+ end
15091+ end
15092+
15093+ def record_obj_args_into_poly_params(arg_ids, pnames, ptypes)
15094+ pos_idx = 0
15095+ ai = 0
15096+ while ai < arg_ids.length
15097+ aid = arg_ids[ai]
15098+ if @nd_type[aid] == "KeywordHashNode"
15099+ elems = parse_id_list(@nd_elements[aid])
15100+ ei = 0
15101+ while ei < elems.length
15102+ if @nd_type[elems[ei]] == "AssocNode"
15103+ key_id = @nd_key[elems[ei]]
15104+ val_id = @nd_expression[elems[ei]]
15105+ if key_id >= 0 && val_id >= 0 && @nd_type[key_id] == "SymbolNode"
15106+ kname = @nd_content[key_id]
15107+ if kname == ""
15108+ kname = @nd_name[key_id]
15109+ end
15110+ pi = 0
15111+ while pi < pnames.length
15112+ if pnames[pi] == kname && pi < ptypes.length && ptypes[pi] == "poly"
15113+ at = infer_type(val_id)
15114+ if is_obj_type(at) == 1
15115+ if not_in(at, @poly_arg_passed_types) == 1
15116+ @poly_arg_passed_types.push(at)
15117+ end
15118+ end
15119+ end
15120+ pi = pi + 1
15121+ end
15122+ end
15123+ end
15124+ ei = ei + 1
15125+ end
15126+ else
15127+ if pos_idx < ptypes.length && ptypes[pos_idx] == "poly"
15128+ at = infer_type(aid)
15129+ if is_obj_type(at) == 1
15130+ if not_in(at, @poly_arg_passed_types) == 1
15131+ @poly_arg_passed_types.push(at)
15132+ end
15133+ end
15134+ end
15135+ pos_idx = pos_idx + 1
15136+ end
15137+ ai = ai + 1
15138+ end
15139+ end
15140+
1495515141 def detect_ptr_array_stored_types
1495615142 # Find object types `obj_<C>` that appear as the element type of an
1495715143 # array literal. Such an array becomes a `sp_PtrArray *` whose
@@ -15275,6 +15461,7 @@
1527515461 detect_param_mutated_types
1527615462 detect_ptr_array_stored_types
1527715463 detect_poly_returned_types
15464+ detect_poly_arg_passed_types
1527815465 detect_method_taken_classes
1527915466 # Multiple passes: value type detection depends on other classes
1528015467 2.times do
@@ -15357,6 +15544,21 @@
1535715544 pri = pri + 1
1535815545 end
1535915546 end
15547+ # Issue #314: same exclusion for classes whose instances flow
15548+ # into a poly-typed param at any call site. Surfaces when
15549+ # kwargs widening collapses two obj types into "poly" — the
15550+ # call site then boxes a value-type instance, mismatching
15551+ # sp_box_obj's `void *` slot.
15552+ if all_val == 1
15553+ type_str314 = "obj_" + @cls_names[i]
15554+ pai = 0
15555+ while pai < @poly_arg_passed_types.length
15556+ if @poly_arg_passed_types[pai] == type_str314
15557+ all_val = 0
15558+ end
15559+ pai = pai + 1
15560+ end
15561+ end
1536015562 # Exclude classes whose instances are captured by a Method
1536115563 # (via `method(:foo)` on self, or `<obj>.method(:foo)` from
1536215564 # anywhere). The captured receiver must be a stable heap
@@ -25452,7 +25654,7 @@
2545225654 # single SplatNode in positional args. The conceptual positional list
2545325655 # is (prefix... ++ splat_array ++ suffix...); fixed params eat from the
2545425656 # left; the rest param (if any) gets the remainder.
25455- def compile_call_args_splat(nid, mi, pnames, ptypes, defaults, kw_names, kw_vals, positional_ids, splat_idx, rest_param_idx)
25657+ def compile_call_args_splat(nid, mi, pnames, ptypes, defaults, kw_names, kw_vals, kw_arg_ids, positional_ids, splat_idx, rest_param_idx)
2545625658 splat_node = positional_ids[splat_idx]
2545725659 splat_src_id = @nd_expression[splat_node]
2545825660 prefix_count = splat_idx
@@ -25501,7 +25703,13 @@
2550125703 if kw_names[ki] == pnames[k]
2550225704 kw_found = 1
2550325705 if k < ptypes.length && ptypes[k] == "poly"
25504- result = result + "sp_box_str(" + kw_vals[ki] + ")"
25706+ # Issue #314: pick the box helper from the arg's actual
25707+ # source type via box_expr_to_poly. The pre-fix sp_box_str
25708+ # hardcoding mistyped any non-string source (e.g. an
25709+ # `obj_<C>` instance) as SP_TAG_STR — invalid C and the
25710+ # boxed value disagrees with sp_RbVal's tag/cls_id contract
25711+ # at runtime.
25712+ result = result + box_expr_to_poly(kw_arg_ids[ki])
2550525713 else
2550625714 result = result + kw_vals[ki]
2550725715 end
@@ -25654,9 +25862,14 @@
2565425862 rest_param_idx = -1
2565525863 end
2565625864
25657- # Check if args contain a KeywordHashNode - extract kw pairs
25865+ # Check if args contain a KeywordHashNode - extract kw pairs.
25866+ # Track kw_arg_ids parallel to kw_vals so the poly-boxing branch
25867+ # below (Issue #314) can pick the right box helper from the arg's
25868+ # actual source type via box_expr_to_poly, rather than hardcoding
25869+ # sp_box_str.
2565825870 kw_names = "".split(",")
2565925871 kw_vals = "".split(",")
25872+ kw_arg_ids = []
2566025873 positional_ids = []
2566125874 splat_idx = -1
2566225875 splat_count_local = 0
@@ -25675,6 +25888,7 @@
2567525888 end
2567625889 kw_names.push(kname)
2567725890 kw_vals.push(compile_expr(@nd_expression[elems[ek]]))
25891+ kw_arg_ids.push(@nd_expression[elems[ek]])
2567825892 end
2567925893 end
2568025894 ek = ek + 1
@@ -25692,7 +25906,7 @@
2569225906 end
2569325907
2569425908 if splat_count_local == 1
25695- return compile_call_args_splat(nid, mi, pnames, ptypes, defaults, kw_names, kw_vals, positional_ids, splat_idx, rest_param_idx)
25909+ return compile_call_args_splat(nid, mi, pnames, ptypes, defaults, kw_names, kw_vals, kw_arg_ids, positional_ids, splat_idx, rest_param_idx)
2569625910 end
2569725911
2569825912 result = ""
@@ -25710,8 +25924,14 @@
2571025924 # Check if param is poly
2571125925 if k < ptypes.length
2571225926 if ptypes[k] == "poly"
25713- # Need to box - kw_vals[ki] is already compiled
25714- result = result + "sp_box_str(" + kw_vals[ki] + ")"
25927+ # Issue #314: pick the box helper from the arg's actual
25928+ # source type via box_expr_to_poly. The pre-fix sp_box_str
25929+ # hardcoding mistyped any non-string source — and a kwargs
25930+ # call site with a poly-widened param (e.g. `model:` after
25931+ # the call-site widening pass folds Article+Comment into
25932+ # poly) is exactly the path that flows non-string objs
25933+ # through here.
25934+ result = result + box_expr_to_poly(kw_arg_ids[ki])
2571525935 else
2571625936 result = result + kw_vals[ki]
2571725937 end