Commit 19f0f007
19f0f007c216Merged
Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
fix(codegen): unify disagreeing call-site param types to poly + box
scan_new_calls (obj-recv path) and scan_cls_method_calls
(no-recv self-call path) only widened a method's recorded param
type when the existing slot was still "int". The first non-int
call site froze the type; later sites disagreeing with it were
silently accepted and the C signature came out wrong:
def add(addr, ...); end add(100, ...) # ptypes[0] = int → no change add(0..10, ...) # ptypes[0] = int → range add(200, ...) # ptypes[0] = range → kept range ← bug
final signature: sp_Range lv_addr; int callers fail to compile
Replace the int-gated assignment with a call to unify_call_types,
which already collapses incompatible types to "poly". Now the third
call sees (range, int, literal=true) and widens to poly.
That widening alone produces sp_RbVal lv_addr but the call sites
still pass raw int / pointer values (the existing arg-cast branch
in compile_typed_call_args only handled int↔obj, not anything→
poly). Add a pt == "poly" && at != "poly" branch that routes
through box_expr_to_poly so the widening reaches the call sites.
In optcarrot this shrinks the C-compile error count by ~50 — every
sp_Optcarrot_CPU_add_mappings and similar dispatch where addr is
called with a mix of Integer literals and other shapes from
different sites was failing.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 19f0f007c216
- Parent
- c64c82b343ed
spinel_codegen.rbmodifiedView file
while kk < arg_ids.length at = infer_type(arg_ids[kk]) if kk < ptypes.length if ptypes[kk] == "int" if at != "int" ptypes[kk] = at end end # Unify against the existing param type rather # than only widening from "int". The previous # rule let the FIRST non-int call site freeze # the param type; subsequent disagreeing calls # (e.g. addr arg seen as Range from one site # and as Integer from another) compiled to a # signature mismatch. unify_call_types collapses # incompatible types to "poly". ptypes[kk] = unify_call_types(ptypes[kk], at, arg_ids[kk]) end kk = kk + 1 end while kk < arg_ids.length at = infer_type(arg_ids[kk]) if kk < ptypes.length # Same unify-instead-of-only-widen-from-int change as # the obj-recv path in scan_new_calls. A no-recv self # call inside the same class is the path that, e.g., # `def boot; add_mappings(0x..0x, ...); end` inside # CPU#boot lands on, and disagreeing arg types from # different self-calls need to widen to poly rather # than freezing on the first non-int call site. ptypes[kk] = unify_call_types(ptypes[kk], at, arg_ids[kk]) end kk = kk + 1 result = result + ", " end if k < arg_ids.length aexpr = compile_expr(arg_ids[k]) at = infer_type(arg_ids[k]) if k < ptypes.length pt = ptypes[k] if pt == "poly" result = result + box_expr_to_poly(arg_ids[k]) k = k + 1 next end if at == "int" if is_obj_type(pt) == 1 # Cast int to object pointer pcname = pt[4, pt.length - 4] aexpr = "(sp_" + pcname + " *)" + aexpr # If the param is poly and the arg is anything else, box it. # The unify pass widens disagreeing param types to "poly" but # leaves the call sites untouched; without this branch the # call passes a raw mrb_int / pointer / range to a sp_RbVal # parameter and the C compiler rejects it. if pt == "poly" && at != "poly" aexpr = box_expr_to_poly(arg_ids[k]) else aexpr = compile_expr(arg_ids[k]) if at == "int" if is_obj_type(pt) == 1 # Cast int to object pointer pcname = pt[4, pt.length - 4] aexpr = "(sp_" + pcname + " *)" + aexpr end end end if is_obj_type(at) == 1 if pt == "int" # Cast object pointer to int aexpr = "(mrb_int)" + aexpr if is_obj_type(at) == 1 if pt == "int" # Cast object pointer to int aexpr = "(mrb_int)" + aexpr end end end else aexpr = compile_expr(arg_ids[k]) end result = result + aexpr elsetest/method_param_unify_to_poly.rbaddedView file
Loading diff…