Commit 1635e7c6

1635e7c62c12Merged

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

Description

fix(codegen): substitute default arguments at class-method call sites

Issue #49: methods with default arguments compiled correctly, and their C signatures encoded the inferred default type, but call sites that omitted the optional argument emitted no value at all, producing C-level "too few arguments" errors:

class Counter def initialize(start = 0); @n = start; end end Counter.new # error: missing 'lv_start' Counter.new(0) # ok

@cls_meth_defaults[ci] was already being populated correctly during def-time (collect_defaults_str gathers each optional/keyword param's expression nid). The gap was on the consuming side: the helpers that emit class-method call arguments didn't read it.

Two changes:

  1. compile_typed_call_args (the obj.method(...) and inheritance path) now reads @cls_meth_defaults for the target method and fills in each trailing parameter the caller omitted with that default's compiled expression. The early args_id < 0 → return "" guard is replaced with a defaults-aware loop. Returns "" only when the target method has zero parameters at all, preserving the existing "no args" branch in callers.

  2. compile_constructor_args' args_id < 0 early-return delegates to compile_typed_call_args against the init's owning class so Counter.new (no parens, no args) picks up start = 0.

  3. The bare-method dispatch path inside class bodies (bar calling another method on implicit self) was using compile_call_args with no class index — switched to compile_typed_call_args by looking up owner_ci/owner_midx via find_method_owner + cls_find_method_direct.

Hash-default = {} now also compiles (free win — it was the same gap, exercised differently).

Coverage:

  • test/default_arg_ctor.rb — Counter/initialize default
  • test/default_arg_method.rb — Greeter/hello default
  • test/default_arg_self_call.rb — implicit-self bare-method dispatch
  • test/default_arg_inherited.rb — child invokes parent's default
  • test/default_arg_partial.rb — mixed: f(10), f(10,20), f(10,20,30)

Bootstrap fixed-point clean; 125 tests + 56 benches pass (was 120 + 56 — the five new tests are exactly the additions).

Closes #49

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

Details

Commit
1635e7c62c12
Parent
7479937893af

spinel_codegen.rbmodifiedView file

@@ -12973,8 +12973,20 @@
1297312973 if @current_class_idx >= 0
1297412974 cidx = cls_find_method(@current_class_idx, mname)
1297512975 if cidx >= 0
12976- ca = compile_call_args(nid)
1297712976 owner = find_method_owner(@current_class_idx, mname)
12977+ # Look up the method's owning class so we can fill in defaults
12978+ # from @cls_meth_defaults (issue #49).
12979+ owner_ci = find_class_idx(owner)
12980+ owner_midx = -1
12981+ if owner_ci >= 0
12982+ owner_midx = cls_find_method_direct(owner_ci, mname)
12983+ end
12984+ ca = ""
12985+ if owner_midx >= 0
12986+ ca = compile_typed_call_args(nid, owner_ci, owner_midx)
12987+ else
12988+ ca = compile_call_args(nid)
12989+ end
1297812990 if ca != ""
1297912991 return "sp_" + owner + "_" + sanitize_name(mname) + "(self, " + ca + ")"
1298012992 else
@@ -16271,6 +16283,15 @@
1627116283 def compile_constructor_args(ci, nid)
1627216284 args_id = @nd_arguments[nid]
1627316285 if args_id < 0
16286+ # No call-site args. If init has parameters with defaults, fill them
16287+ # in here (issue #49: `Counter.new` for `initialize(start = 0)`).
16288+ init_ci = find_init_class(ci)
16289+ if init_ci >= 0
16290+ init_idx = cls_find_method_direct(init_ci, "initialize")
16291+ if init_idx >= 0
16292+ return compile_typed_call_args(nid, init_ci, init_idx)
16293+ end
16294+ end
1627416295 return ""
1627516296 end
1627616297 arg_ids = get_args(args_id)
@@ -16469,43 +16490,72 @@
1646916490 end
1647016491
1647116492 def compile_typed_call_args(nid, target_ci, target_midx)
16472- # Like compile_call_args but casts arguments to match target method param types
16493+ # Like compile_call_args but casts arguments to match target method param
16494+ # types AND fills in defaults from @cls_meth_defaults for trailing
16495+ # parameters the caller omitted (issue #49). Returns "" only when the
16496+ # method takes no parameters at all.
1647316497 args_id = @nd_arguments[nid]
16474- if args_id < 0
16475- return ""
16498+ arg_ids = []
16499+ if args_id >= 0
16500+ arg_ids = get_args(args_id)
1647616501 end
16477- arg_ids = get_args(args_id)
1647816502 all_ptypes = @cls_meth_ptypes[target_ci].split("|")
16503+ all_defaults = @cls_meth_defaults[target_ci].split("|")
1647916504 ptypes = "".split(",")
16505+ defaults = "".split(",")
1648016506 if target_midx < all_ptypes.length
1648116507 ptypes = all_ptypes[target_midx].split(",")
1648216508 end
16509+ if target_midx < all_defaults.length
16510+ defaults = all_defaults[target_midx].split(",")
16511+ end
16512+ total = ptypes.length
16513+ if arg_ids.length > total
16514+ total = arg_ids.length
16515+ end
16516+ if total == 0
16517+ return ""
16518+ end
1648316519 result = ""
1648416520 pcname = ""
1648516521 k = 0
16486- while k < arg_ids.length
16522+ while k < total
1648716523 if k > 0
1648816524 result = result + ", "
1648916525 end
16490- aexpr = compile_expr(arg_ids[k])
16491- at = infer_type(arg_ids[k])
16492- if k < ptypes.length
16493- pt = ptypes[k]
16494- if at == "int"
16495- if is_obj_type(pt) == 1
16496- # Cast int to object pointer
16497- pcname = pt[4, pt.length - 4]
16498- aexpr = "(sp_" + pcname + " *)" + aexpr
16526+ if k < arg_ids.length
16527+ aexpr = compile_expr(arg_ids[k])
16528+ at = infer_type(arg_ids[k])
16529+ if k < ptypes.length
16530+ pt = ptypes[k]
16531+ if at == "int"
16532+ if is_obj_type(pt) == 1
16533+ # Cast int to object pointer
16534+ pcname = pt[4, pt.length - 4]
16535+ aexpr = "(sp_" + pcname + " *)" + aexpr
16536+ end
16537+ end
16538+ if is_obj_type(at) == 1
16539+ if pt == "int"
16540+ # Cast object pointer to int
16541+ aexpr = "(mrb_int)" + aexpr
16542+ end
1649916543 end
1650016544 end
16501- if is_obj_type(at) == 1
16502- if pt == "int"
16503- # Cast object pointer to int
16504- aexpr = "(mrb_int)" + aexpr
16545+ result = result + aexpr
16546+ else
16547+ # Caller omitted this trailing arg — emit the method's default.
16548+ if k < defaults.length
16549+ def_id = defaults[k].to_i
16550+ if def_id >= 0
16551+ result = result + compile_expr(def_id)
16552+ else
16553+ result = result + "0"
1650516554 end
16555+ else
16556+ result = result + "0"
1650616557 end
1650716558 end
16508- result = result + aexpr
1650916559 k = k + 1
1651016560 end
1651116561 result