Commit cde32be3

cde32be3990aMerged

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

Description

fix(codegen): class-method dispatch routes kwargs by param name

The class-method branch in compile_constant_recv_expr used the generic compile_call_args for the call site, which walked any KeywordHashNode argument as an opaque arg whose compile_expr lowered to a literal that resolved to 0 at the C-call site. W.write(200, set_cookies: cookies) against def self.write(status, set_cookies: {}) therefore emitted sp_W_cls_write(200LL, 0) — cookies silently dropped — even though the param slot was correctly typed (sp_SymStrHash * via body widening) and the missing-default case was already fixed in #486.

Fix: replace the compile_call_args + trailing-defaults shape with a kwarg-aware walk modeled after compile_call_args_with_defaults' instance-method handler. Extract positional + kwarg pairs from the call site, then walk the cmeth's parameter list in order: positional args fill slots first, then kwargs by matching param name, then defaults (routed through empty_hash_coerce when the default is {} and the param has a declared hash variant). Volatile-discard cast is preserved at the call-site emit so #487's main+rescue+pointer shape still compiles clean.

Validation: tests 455 pass (added regression test exercising both kwarg-supplied and default-omitted call sites for the same class method), bench 57/57, bootstrap 4-way fixpoint OK.

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

Details

Commit
cde32be3990a
Parent
31855b5ea404

spinel_codegen.rbmodifiedView file

@@ -17193,15 +17193,6 @@
1719317193 owner_ci = cls_method_owner_for(ci3, mname, "cmeth")
1719417194 if owner_ci >= 0
1719517195 owner_name = @cls_names[owner_ci]
17196- ca = compile_call_args(nid)
17197- # Fill in defaults for trailing params the caller
17198- # omitted. `def self.greet(name, msg = nil)` with call
17199- # site `C.greet("a")` would otherwise emit
17200- # `sp_C_cls_greet("a")` against the 2-arg signature and
17201- # the C compiler rejects with `too few arguments`. Look
17202- # up the cls method's recorded defaults (parallel to
17203- # @cls_meth_defaults for instance methods) and append
17204- # the missing trailing exprs.
1720517196 owner_cmnames = @cls_cmeth_names[owner_ci].split(";")
1720617197 owner_cmptypes = @cls_cmeth_ptypes[owner_ci].split("|")
1720717198 owner_cmdefaults = @cls_cmeth_defaults[owner_ci].split("|")
@@ -17212,36 +17203,92 @@
1721217203 end
1721317204 cmidx = cmidx + 1
1721417205 end
17206+ ca = ""
1721517207 if cmidx < owner_cmnames.length && cmidx < owner_cmptypes.length
17208+ owner_pn = cls_cmeth_pnames_get(owner_ci, cmidx)
1721617209 owner_pt = owner_cmptypes[cmidx].split(",")
1721717210 owner_df = "".split(",")
1721817211 if cmidx < owner_cmdefaults.length
1721917212 owner_df = owner_cmdefaults[cmidx].split(",")
1722017213 end
17221- args_id = @nd_arguments[nid]
17222- arg_count = 0
17223- if args_id >= 0
17224- arg_count = get_args(args_id).length
17214+ # Extract positional + kwarg pairs from the call site, so
17215+ # `W.write(200, set_cookies: cookies)` against
17216+ # `def self.write(status, set_cookies: {})` routes `cookies`
17217+ # into the set_cookies slot (instead of compiling the
17218+ # KeywordHashNode as a generic literal that lowers to 0).
17219+ # Sibling to the kwarg-fallback path in
17220+ # compile_call_args_with_defaults' instance-method handler.
17221+ args_id_cm2 = @nd_arguments[nid]
17222+ arg_ids_cm2 = []
17223+ if args_id_cm2 >= 0
17224+ arg_ids_cm2 = get_args(args_id_cm2)
1722517225 end
17226- kk = arg_count
17227- while kk < owner_pt.length
17228- if kk < owner_df.length
17229- def_id = owner_df[kk].to_i
17230- if def_id >= 0
17231- # Coerce an empty `{}` default to the param's declared
17232- # hash variant (sp_SymStrHash_new vs the generic
17233- # sp_StrIntHash_new). Same shape as the positional-arg
17234- # branch in compile_call_args_with_defaults.
17235- def_expr = empty_hash_coerce(def_id, owner_pt[kk])
17236- if def_expr == ""
17237- def_expr = compile_expr(def_id)
17226+ kw_names_cm = "".split(",")
17227+ kw_vals_cm = "".split(",")
17228+ positional_cm = []
17229+ ak_cm = 0
17230+ while ak_cm < arg_ids_cm2.length
17231+ if @nd_type[arg_ids_cm2[ak_cm]] == "KeywordHashNode"
17232+ elems_cm = parse_id_list(@nd_elements[arg_ids_cm2[ak_cm]])
17233+ ek_cm = 0
17234+ while ek_cm < elems_cm.length
17235+ if @nd_type[elems_cm[ek_cm]] == "AssocNode"
17236+ key_id_cm = @nd_key[elems_cm[ek_cm]]
17237+ if key_id_cm >= 0 && @nd_type[key_id_cm] == "SymbolNode"
17238+ kw_names_cm.push(@nd_content[key_id_cm])
17239+ kw_vals_cm.push(compile_expr(@nd_expression[elems_cm[ek_cm]]))
17240+ end
1723817241 end
17239- if ca == ""
17240- ca = def_expr
17242+ ek_cm = ek_cm + 1
17243+ end
17244+ else
17245+ positional_cm.push(arg_ids_cm2[ak_cm])
17246+ end
17247+ ak_cm = ak_cm + 1
17248+ end
17249+ pos_idx_cm = 0
17250+ kk = 0
17251+ while kk < owner_pn.length
17252+ slot_expr_cm = ""
17253+ if pos_idx_cm < positional_cm.length
17254+ slot_expr_cm = cast_away_volatile_arg(positional_cm[pos_idx_cm], compile_expr(positional_cm[pos_idx_cm]))
17255+ pos_idx_cm = pos_idx_cm + 1
17256+ else
17257+ # Try kwarg by name.
17258+ pname_cm = owner_pn[kk]
17259+ ki_cm = 0
17260+ while ki_cm < kw_names_cm.length
17261+ if kw_names_cm[ki_cm] == pname_cm
17262+ slot_expr_cm = kw_vals_cm[ki_cm]
17263+ ki_cm = kw_names_cm.length
1724117264 else
17242- ca = ca + ", " + def_expr
17265+ ki_cm = ki_cm + 1
1724317266 end
1724417267 end
17268+ if slot_expr_cm == ""
17269+ if kk < owner_df.length
17270+ def_id_cm = owner_df[kk].to_i
17271+ if def_id_cm >= 0
17272+ def_pt_cm = ""
17273+ if kk < owner_pt.length
17274+ def_pt_cm = owner_pt[kk]
17275+ end
17276+ coerced_cm = empty_hash_coerce(def_id_cm, def_pt_cm)
17277+ if coerced_cm != ""
17278+ slot_expr_cm = coerced_cm
17279+ else
17280+ slot_expr_cm = compile_expr(def_id_cm)
17281+ end
17282+ end
17283+ end
17284+ end
17285+ end
17286+ if slot_expr_cm != ""
17287+ if ca == ""
17288+ ca = slot_expr_cm
17289+ else
17290+ ca = ca + ", " + slot_expr_cm
17291+ end
1724517292 end
1724617293 kk = kk + 1
1724717294 end