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

@@ -6538,11 +6538,15 @@
65386538 while kk < arg_ids.length
65396539 at = infer_type(arg_ids[kk])
65406540 if kk < ptypes.length
6541- if ptypes[kk] == "int"
6542- if at != "int"
6543- ptypes[kk] = at
6544- end
6545- end
6541+ # Unify against the existing param type rather
6542+ # than only widening from "int". The previous
6543+ # rule let the FIRST non-int call site freeze
6544+ # the param type; subsequent disagreeing calls
6545+ # (e.g. addr arg seen as Range from one site
6546+ # and as Integer from another) compiled to a
6547+ # signature mismatch. unify_call_types collapses
6548+ # incompatible types to "poly".
6549+ ptypes[kk] = unify_call_types(ptypes[kk], at, arg_ids[kk])
65466550 end
65476551 kk = kk + 1
65486552 end
@@ -9580,6 +9584,13 @@
95809584 while kk < arg_ids.length
95819585 at = infer_type(arg_ids[kk])
95829586 if kk < ptypes.length
9587+ # Same unify-instead-of-only-widen-from-int change as
9588+ # the obj-recv path in scan_new_calls. A no-recv self
9589+ # call inside the same class is the path that, e.g.,
9590+ # `def boot; add_mappings(0x..0x, ...); end` inside
9591+ # CPU#boot lands on, and disagreeing arg types from
9592+ # different self-calls need to widen to poly rather
9593+ # than freezing on the first non-int call site.
95839594 ptypes[kk] = unify_call_types(ptypes[kk], at, arg_ids[kk])
95849595 end
95859596 kk = kk + 1
@@ -20139,28 +20150,34 @@
2013920150 result = result + ", "
2014020151 end
2014120152 if k < arg_ids.length
20142- aexpr = compile_expr(arg_ids[k])
2014320153 at = infer_type(arg_ids[k])
2014420154 if k < ptypes.length
2014520155 pt = ptypes[k]
20146- if pt == "poly"
20147- result = result + box_expr_to_poly(arg_ids[k])
20148- k = k + 1
20149- next
20150- end
20151- if at == "int"
20152- if is_obj_type(pt) == 1
20153- # Cast int to object pointer
20154- pcname = pt[4, pt.length - 4]
20155- aexpr = "(sp_" + pcname + " *)" + aexpr
20156+ # If the param is poly and the arg is anything else, box it.
20157+ # The unify pass widens disagreeing param types to "poly" but
20158+ # leaves the call sites untouched; without this branch the
20159+ # call passes a raw mrb_int / pointer / range to a sp_RbVal
20160+ # parameter and the C compiler rejects it.
20161+ if pt == "poly" && at != "poly"
20162+ aexpr = box_expr_to_poly(arg_ids[k])
20163+ else
20164+ aexpr = compile_expr(arg_ids[k])
20165+ if at == "int"
20166+ if is_obj_type(pt) == 1
20167+ # Cast int to object pointer
20168+ pcname = pt[4, pt.length - 4]
20169+ aexpr = "(sp_" + pcname + " *)" + aexpr
20170+ end
2015620171 end
20157- end
20158- if is_obj_type(at) == 1
20159- if pt == "int"
20160- # Cast object pointer to int
20161- aexpr = "(mrb_int)" + aexpr
20172+ if is_obj_type(at) == 1
20173+ if pt == "int"
20174+ # Cast object pointer to int
20175+ aexpr = "(mrb_int)" + aexpr
20176+ end
2016220177 end
2016320178 end
20179+ else
20180+ aexpr = compile_expr(arg_ids[k])
2016420181 end
2016520182 result = result + aexpr
2016620183 else