Commit 15f7e5f3

15f7e5f356d7Merged

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

Description

fix(codegen): default args on def self.method now apply at call sites

Issue #239: a class method declared with default arguments (def self.greet(name, msg = nil); ...; end) emitted a C function whose signature included all parameters, but call sites that omitted the optional args emitted C calls with too few arguments — cc rejected with too few arguments to function call. The default- arg fill existed for instance methods (@cls_meth_defaults) and top-level def methods (@meth_has_defaults), but def self.x on both modules and classes missed the synthesis. Sister to #229 (cls method DCE) — both about def self.method codegen gaps.

Two distinct paths because module cls methods and class cls methods live in different storage:

  • Module form (module M; def self.x; end; end) lowers to a top-level method named M_cls_x in @meth_*. Pre-fix:

    1. collect_module_with_prefix pushed a hardcoded "0" into @meth_has_defaults instead of collect_defaults_str(sid), so compile_call_args_with_defaults had nothing to fill;
    2. the class-receiver dispatch path used the un-typed compile_call_args instead of compile_call_args_with_defaults;
    3. scan_new_calls's issue #207 widening branch only walked @cls_cmeth_* and skipped module names, so a M.greet("a") call site couldn't widen lv_name from mrb_int to const char *. Fixed all three.
  • Class form (class C; def self.x; end; end) stores the cls method in @cls_cmeth_* — but there was no @cls_cmeth_defaults table parallel to @cls_meth_defaults. Added the array, threaded defaults through append_cls_cmeth, captured it in collect_class_method, propagated it via #224's propagate_inherited_class_methods so subclasses inherit defaults too, and added a default-fill loop to the class- receiver dispatch site that walks the recorded ptypes/defaults pair and appends compile_expr(default_id) for each missing trailing arg.

Test fixture covers both module and class forms, both string and explicit-pass call shapes, and inheritance through propagate_inherited_class_methods.

292 pass, 0 fail, bootstrap clean (gen2 == gen3).

Closes #239 Refs #229 #224

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

Details

Commit
15f7e5f356d7
Parent
64c18e5ccfd5

spinel_codegen.rbmodifiedView file

@@ -142,6 +142,7 @@
142142 @cls_cmeth_ptypes = "".split(",")
143143 @cls_cmeth_returns = "".split(",")
144144 @cls_cmeth_bodies = "".split(",")
145+ @cls_cmeth_defaults = "".split(",")
145146 @cls_is_value_type = []
146147 # SRA (scalar replacement of aggregates) eligibility flag per class.
147148 # Classes marked here can have their non-escaping instances replaced
@@ -4866,6 +4867,7 @@
48664867 p_cmbodies = @cls_cmeth_bodies[pi].split(";")
48674868 p_cmparams = @cls_cmeth_params[pi].split("|")
48684869 p_cmptypes = @cls_cmeth_ptypes[pi].split("|")
4870+ p_cmdefaults = @cls_cmeth_defaults[pi].split("|")
48694871 pj = 0
48704872 while pj < p_cmnames.length
48714873 mname = p_cmnames[pj]
@@ -4886,7 +4888,11 @@
48864888 if pj < p_cmptypes.length
48874889 pptypes = p_cmptypes[pj]
48884890 end
4889- append_cls_cmeth(ci, mname, pparams, pptypes, pret, pbid)
4891+ pdefaults = ""
4892+ if pj < p_cmdefaults.length
4893+ pdefaults = p_cmdefaults[pj]
4894+ end
4895+ append_cls_cmeth(ci, mname, pparams, pptypes, pret, pbid, pdefaults)
48904896 seen.push(mname)
48914897 end
48924898 pj = pj + 1
@@ -5734,6 +5740,7 @@
57345740 @cls_cmeth_ptypes.push("")
57355741 @cls_cmeth_returns.push("")
57365742 @cls_cmeth_bodies.push("")
5743+ @cls_cmeth_defaults.push("")
57375744 @cls_meth_has_yield.push("")
57385745
57395746 # Collect class body
@@ -5844,7 +5851,7 @@
58445851 params_str = collect_params_str(nid)
58455852 ptypes_str = collect_ptypes_str(nid, ci)
58465853 defaults_str = collect_defaults_str(nid)
5847- append_cls_cmeth(ci, mname, params_str, ptypes_str, "int", body_id)
5854+ append_cls_cmeth(ci, mname, params_str, ptypes_str, "int", body_id, defaults_str)
58485855 return
58495856 end
58505857 end
@@ -6098,19 +6105,21 @@
60986105 end
60996106 end
61006107
6101- def append_cls_cmeth(ci, name, params, ptypes, ret, body_id)
6108+ def append_cls_cmeth(ci, name, params, ptypes, ret, body_id, defaults = "")
61026109 if @cls_cmeth_names[ci] != ""
61036110 @cls_cmeth_names[ci] = @cls_cmeth_names[ci] + ";" + name
61046111 @cls_cmeth_params[ci] = @cls_cmeth_params[ci] + "|" + params
61056112 @cls_cmeth_ptypes[ci] = @cls_cmeth_ptypes[ci] + "|" + ptypes
61066113 @cls_cmeth_returns[ci] = @cls_cmeth_returns[ci] + ";" + ret
61076114 @cls_cmeth_bodies[ci] = @cls_cmeth_bodies[ci] + ";" + body_id.to_s
6115+ @cls_cmeth_defaults[ci] = @cls_cmeth_defaults[ci] + "|" + defaults
61086116 else
61096117 @cls_cmeth_names[ci] = name
61106118 @cls_cmeth_params[ci] = params
61116119 @cls_cmeth_ptypes[ci] = ptypes
61126120 @cls_cmeth_returns[ci] = ret
61136121 @cls_cmeth_bodies[ci] = body_id.to_s
6122+ @cls_cmeth_defaults[ci] = defaults
61146123 end
61156124 end
61166125
@@ -6842,7 +6851,13 @@
68426851 @meth_return_types.push("int")
68436852 @meth_body_ids.push(@nd_body[sid])
68446853 @meth_has_yield.push(0)
6845- @meth_has_defaults.push("0")
6854+ # Issue #239: capture default-arg expressions so call
6855+ # sites that omit trailing args get them filled in by
6856+ # compile_call_args_with_defaults. Pre-fix this slot
6857+ # was hardcoded to "0", which `compile_expr` lowered to
6858+ # the literal `0` regardless of the actual default
6859+ # (string literals etc.).
6860+ @meth_has_defaults.push(collect_defaults_str(sid))
68466861 @meth_rest_index.push(collect_rest_index(sid))
68476862 end
68486863 end
@@ -6970,6 +6985,7 @@
69706985 @cls_cmeth_ptypes.push("")
69716986 @cls_cmeth_returns.push("")
69726987 @cls_cmeth_bodies.push("")
6988+ @cls_cmeth_defaults.push("")
69736989 @cls_meth_has_yield.push("")
69746990
69756991 # Get field names from symbol args (skip keyword_init hash)
@@ -7777,6 +7793,36 @@
77777793 cmidx = cmidx + 1
77787794 end
77797795 end
7796+ # Issue #239: module class methods (`module M; def
7797+ # self.greet(...); end; end`) live in the top-level
7798+ # `@meth_*` tables under the synthetic name
7799+ # `<Mod>_cls_<m>`, not in `@cls_cmeth_*`. The widening
7800+ # branch above doesn't reach them because `find_class_idx`
7801+ # returns -1 for module names. Add a parallel branch
7802+ # that widens `@meth_param_types` for the prefixed name
7803+ # so a `M.greet("a")` call site teaches the synthetic
7804+ # function to accept `const char *` instead of the
7805+ # default `mrb_int`.
7806+ if module_name_exists(rcname) == 1
7807+ mfn239 = rcname + "_cls_" + @nd_name[nid]
7808+ mi239 = find_method_idx(mfn239)
7809+ if mi239 >= 0
7810+ args_id239 = @nd_arguments[nid]
7811+ if args_id239 >= 0
7812+ arg_ids239 = get_args(args_id239)
7813+ ptypes239 = @meth_param_types[mi239].split(",")
7814+ kk239 = 0
7815+ while kk239 < arg_ids239.length
7816+ at239 = infer_type(arg_ids239[kk239])
7817+ if kk239 < ptypes239.length
7818+ ptypes239[kk239] = unify_call_types(ptypes239[kk239], at239, arg_ids239[kk239])
7819+ end
7820+ kk239 = kk239 + 1
7821+ end
7822+ @meth_param_types[mi239] = ptypes239.join(",")
7823+ end
7824+ end
7825+ end
77807826 end
77817827 end
77827828 end
@@ -21176,7 +21222,12 @@
2117621222 mfn = rcname + "_cls_" + mname
2117721223 mfi = find_method_idx(mfn)
2117821224 if mfi >= 0
21179- ca = compile_call_args(nid)
21225+ # Issue #239: route through compile_call_args_with_defaults
21226+ # so a `def self.greet(name, msg = nil); end` inside a
21227+ # module accepts shorter call sites — caller-omitted
21228+ # trailing args get filled in from the method's recorded
21229+ # default expressions, the same as top-level method calls.
21230+ ca = compile_call_args_with_defaults(nid, mfi)
2118021231 if ca != ""
2118121232 return "sp_" + sanitize_name(mfn) + "(" + ca + ")"
2118221233 else
@@ -21196,6 +21247,50 @@
2119621247 if owner_ci >= 0
2119721248 owner_name = @cls_names[owner_ci]
2119821249 ca = compile_call_args(nid)
21250+ # Issue #239: fill in defaults for trailing params the
21251+ # caller omitted. `def self.greet(name, msg = nil)` with
21252+ # call site `C.greet("a")` would otherwise emit
21253+ # `sp_C_cls_greet("a")` against the 2-arg signature and
21254+ # the C compiler rejects with `too few arguments`. Look
21255+ # up the cls method's recorded defaults (parallel to
21256+ # @cls_meth_defaults for instance methods) and append
21257+ # the missing trailing exprs.
21258+ owner_cmnames = @cls_cmeth_names[owner_ci].split(";")
21259+ owner_cmptypes = @cls_cmeth_ptypes[owner_ci].split("|")
21260+ owner_cmdefaults = @cls_cmeth_defaults[owner_ci].split("|")
21261+ cmidx239 = 0
21262+ while cmidx239 < owner_cmnames.length
21263+ if owner_cmnames[cmidx239] == mname
21264+ break
21265+ end
21266+ cmidx239 = cmidx239 + 1
21267+ end
21268+ if cmidx239 < owner_cmnames.length && cmidx239 < owner_cmptypes.length
21269+ owner_pt = owner_cmptypes[cmidx239].split(",")
21270+ owner_df = "".split(",")
21271+ if cmidx239 < owner_cmdefaults.length
21272+ owner_df = owner_cmdefaults[cmidx239].split(",")
21273+ end
21274+ args_id239 = @nd_arguments[nid]
21275+ arg_count239 = 0
21276+ if args_id239 >= 0
21277+ arg_count239 = get_args(args_id239).length
21278+ end
21279+ kk239 = arg_count239
21280+ while kk239 < owner_pt.length
21281+ if kk239 < owner_df.length
21282+ def_id239 = owner_df[kk239].to_i
21283+ if def_id239 >= 0
21284+ if ca == ""
21285+ ca = compile_expr(def_id239)
21286+ else
21287+ ca = ca + ", " + compile_expr(def_id239)
21288+ end
21289+ end
21290+ end
21291+ kk239 = kk239 + 1
21292+ end
21293+ end
2119921294 if ca != ""
2120021295 return "sp_" + owner_name + "_cls_" + sanitize_name(mname) + "(" + ca + ")"
2120121296 else