Commit acc935da

acc935da1f1cMerged

Yusuke Endoh <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): widen param type when body reassigns it to incompatible value

A method body that reassigns one of its parameters to a value whose type is incompatible with the call-site-inferred ptype left the param's slot at the call-site shape and produced a cross-type C assignment:

def debug_logging(scanline, hclk, hclk_target)
  hclk = "forever" if hclk == FOREVER_CLOCK
  ...
end

— call sites pass int, body assigns string, generated C has

mrb_int lv_hclk;
if (lv_hclk == 4294967295) lv_hclk = "forever";   /* int = char* */

Add a pre-pass widen_param_types_from_body_writes that, for each method, scans the body for LocalVariableWriteNode / MultiWriteNode writes targeting a parameter name and unifies the literal-typed RHS into the stored param type. Method-call RHSes are intentionally skipped — this pass runs before return-type inference, so unanalyzed call returns fall through to the int default and would falsely widen a string param to poly. Restricting to literal RHSes (StringNode, IntegerNode, FloatNode, SymbolNode, NilNode, true/false) keeps the signal reliable.

Body-write unification rule is distinct from unify_call_types: the call-site rule treats int as a placeholder that gets overwritten by any concrete at (int + string → string), which is fine when int is just a default — but for body writes the call site really did pass int, so the slot must hold both shapes. The new unify_param_for_body_write falls to poly on genuinely incompatible writes.

Details

Commit
acc935da1f1c
Parent
59f48e594a9d

spinel_codegen.rbmodifiedView file

@@ -11114,6 +11114,241 @@
1111411114 end
1111511115 end
1111611116
11117+ # Widen each method's stored parameter types to encompass any
11118+ # in-body reassignments to the parameter name. Without this,
11119+ # a body shape like
11120+ #
11121+ # def f(hclk, ...)
11122+ # hclk = "forever" if hclk == FOREVER_CLOCK
11123+ # ...
11124+ # end
11125+ #
11126+ # leaves `hclk`'s slot at the call-site-inferred `int`, and the
11127+ # `lv_hclk = (const char *)…` C statement fails -Wint-conversion.
11128+ # Widening to "poly" lets the slot hold both shapes via sp_RbVal.
11129+ #
11130+ # `int` is treated as a default/fallback (matches unify_call_types):
11131+ # int + concrete-non-int → concrete. Only genuinely incompatible
11132+ # writes (e.g. ptype already concrete and a different concrete
11133+ # write) escalate to poly.
11134+ def widen_param_types_from_body_writes
11135+ # Top-level methods.
11136+ mi = 0
11137+ while mi < @meth_names.length
11138+ bid = @meth_body_ids[mi]
11139+ if bid >= 0
11140+ pnames = @meth_param_names[mi].split(",")
11141+ ptypes = @meth_param_types[mi].split(",")
11142+ ptypes_changed = 0
11143+ pk = 0
11144+ while pk < pnames.length
11145+ if pk < ptypes.length
11146+ new_t = scan_param_body_write_unify(bid, pnames[pk], ptypes[pk])
11147+ if new_t != ptypes[pk]
11148+ ptypes[pk] = new_t
11149+ ptypes_changed = 1
11150+ end
11151+ end
11152+ pk = pk + 1
11153+ end
11154+ if ptypes_changed == 1
11155+ @meth_param_types[mi] = ptypes.join(",")
11156+ end
11157+ end
11158+ mi = mi + 1
11159+ end
11160+
11161+ # Per-class instance and class methods.
11162+ ci = 0
11163+ while ci < @cls_names.length
11164+ mnames = @cls_meth_names[ci].split(";")
11165+ all_params = @cls_meth_params[ci].split("|")
11166+ all_ptypes = @cls_meth_ptypes[ci].split("|")
11167+ bodies = @cls_meth_bodies[ci].split(";")
11168+ cls_changed = 0
11169+ mj = 0
11170+ while mj < mnames.length
11171+ if mj < all_params.length && mj < all_ptypes.length && mj < bodies.length
11172+ bid_j = bodies[mj].to_i
11173+ if bid_j >= 0
11174+ pnames_j = all_params[mj].split(",")
11175+ ptypes_j = all_ptypes[mj].split(",")
11176+ inner_changed = 0
11177+ pk = 0
11178+ while pk < pnames_j.length
11179+ if pk < ptypes_j.length
11180+ new_t = scan_param_body_write_unify(bid_j, pnames_j[pk], ptypes_j[pk])
11181+ if new_t != ptypes_j[pk]
11182+ ptypes_j[pk] = new_t
11183+ inner_changed = 1
11184+ end
11185+ end
11186+ pk = pk + 1
11187+ end
11188+ if inner_changed == 1
11189+ all_ptypes[mj] = ptypes_j.join(",")
11190+ cls_changed = 1
11191+ end
11192+ end
11193+ end
11194+ mj = mj + 1
11195+ end
11196+ if cls_changed == 1
11197+ @cls_meth_ptypes[ci] = all_ptypes.join("|")
11198+ end
11199+ ci = ci + 1
11200+ end
11201+ end
11202+
11203+ # Walk under `nid` looking for `LocalVariableWriteNode`s targeting
11204+ # `pname` and unify the literal-typed RHS with `cur_t`. Method-call
11205+ # RHSes are skipped: this pass runs before return-type inference,
11206+ # so `infer_type(<call>)` for an unanalyzed method falls through to
11207+ # the `int` default and would falsely widen a string param to poly.
11208+ # Literal RHSes (StringNode, IntegerNode, FloatNode, SymbolNode,
11209+ # NilNode, true/false) are reliable enough to widen on.
11210+ def scan_param_body_write_unify(nid, pname, cur_t)
11211+ if nid < 0
11212+ return cur_t
11213+ end
11214+ nt = @nd_type[nid]
11215+ if nt == "LocalVariableWriteNode"
11216+ if @nd_name[nid] == pname
11217+ rhs = @nd_expression[nid]
11218+ if rhs >= 0 && is_reliable_literal_for_widen(rhs) == 1
11219+ at = infer_type(rhs)
11220+ cur_t = unify_param_for_body_write(cur_t, at)
11221+ end
11222+ end
11223+ end
11224+ if nt == "MultiWriteNode"
11225+ mw_targets = parse_id_list(@nd_targets[nid])
11226+ mw_val = @nd_expression[nid]
11227+ ti = 0
11228+ mw_targets.each { |tid|
11229+ if @nd_type[tid] == "LocalVariableTargetNode" && @nd_name[tid] == pname
11230+ if mw_val >= 0 && @nd_type[mw_val] == "ArrayNode"
11231+ elems = parse_id_list(@nd_elements[mw_val])
11232+ if ti < elems.length && is_reliable_literal_for_widen(elems[ti]) == 1
11233+ slot_t = infer_type(elems[ti])
11234+ cur_t = unify_param_for_body_write(cur_t, slot_t)
11235+ end
11236+ end
11237+ end
11238+ ti = ti + 1
11239+ }
11240+ end
11241+ # Recurse into all child fields. Mirrors collect_param_methods'
11242+ # walk shape so we don't miss conditional/loop branches.
11243+ if @nd_body[nid] >= 0
11244+ cur_t = scan_param_body_write_unify(@nd_body[nid], pname, cur_t)
11245+ end
11246+ stmts = parse_id_list(@nd_stmts[nid])
11247+ k = 0
11248+ while k < stmts.length
11249+ cur_t = scan_param_body_write_unify(stmts[k], pname, cur_t)
11250+ k = k + 1
11251+ end
11252+ if @nd_expression[nid] >= 0
11253+ cur_t = scan_param_body_write_unify(@nd_expression[nid], pname, cur_t)
11254+ end
11255+ if @nd_predicate[nid] >= 0
11256+ cur_t = scan_param_body_write_unify(@nd_predicate[nid], pname, cur_t)
11257+ end
11258+ if @nd_subsequent[nid] >= 0
11259+ cur_t = scan_param_body_write_unify(@nd_subsequent[nid], pname, cur_t)
11260+ end
11261+ if @nd_else_clause[nid] >= 0
11262+ cur_t = scan_param_body_write_unify(@nd_else_clause[nid], pname, cur_t)
11263+ end
11264+ if @nd_receiver[nid] >= 0
11265+ cur_t = scan_param_body_write_unify(@nd_receiver[nid], pname, cur_t)
11266+ end
11267+ if @nd_arguments[nid] >= 0
11268+ cur_t = scan_param_body_write_unify(@nd_arguments[nid], pname, cur_t)
11269+ end
11270+ args = parse_id_list(@nd_args[nid])
11271+ k = 0
11272+ while k < args.length
11273+ cur_t = scan_param_body_write_unify(args[k], pname, cur_t)
11274+ k = k + 1
11275+ end
11276+ conds = parse_id_list(@nd_conditions[nid])
11277+ k = 0
11278+ while k < conds.length
11279+ cur_t = scan_param_body_write_unify(conds[k], pname, cur_t)
11280+ k = k + 1
11281+ end
11282+ if @nd_left[nid] >= 0
11283+ cur_t = scan_param_body_write_unify(@nd_left[nid], pname, cur_t)
11284+ end
11285+ if @nd_right[nid] >= 0
11286+ cur_t = scan_param_body_write_unify(@nd_right[nid], pname, cur_t)
11287+ end
11288+ if @nd_block[nid] >= 0
11289+ cur_t = scan_param_body_write_unify(@nd_block[nid], pname, cur_t)
11290+ end
11291+ elems = parse_id_list(@nd_elements[nid])
11292+ k = 0
11293+ while k < elems.length
11294+ cur_t = scan_param_body_write_unify(elems[k], pname, cur_t)
11295+ k = k + 1
11296+ end
11297+ if @nd_rescue_clause[nid] >= 0
11298+ cur_t = scan_param_body_write_unify(@nd_rescue_clause[nid], pname, cur_t)
11299+ end
11300+ if @nd_ensure_clause[nid] >= 0
11301+ cur_t = scan_param_body_write_unify(@nd_ensure_clause[nid], pname, cur_t)
11302+ end
11303+ cur_t
11304+ end
11305+
11306+ # `nid` is a literal whose type is reliable even before
11307+ # return-type inference has run. Limits the body-write widening
11308+ # to writes whose RHS is unambiguous, avoiding false widenings
11309+ # driven by `infer_type` of unanalyzed method calls (which falls
11310+ # through to the `int` default).
11311+ def is_reliable_literal_for_widen(nid)
11312+ if nid < 0
11313+ return 0
11314+ end
11315+ nt = @nd_type[nid]
11316+ if nt == "StringNode" || nt == "IntegerNode" || nt == "FloatNode" || nt == "SymbolNode" || nt == "NilNode" || nt == "TrueNode" || nt == "FalseNode"
11317+ return 1
11318+ end
11319+ 0
11320+ end
11321+
11322+ # Body-write unification rule. Distinct from `unify_call_types`
11323+ # because the call-site rule treats `int` as a placeholder that
11324+ # gets overwritten by any concrete `at` — that produces
11325+ # `int + string → string`, which is fine for call-site widening
11326+ # (`int` was a default) but wrong for body-write widening (the
11327+ # call site really did pass int, so the slot must hold both shapes).
11328+ def unify_param_for_body_write(pt_a, pt_b)
11329+ if pt_a == pt_b
11330+ return pt_a
11331+ end
11332+ # `nil` writes don't widen; `if x.nil?` body-side `x = sentinel`
11333+ # is a common shape that mustn't push the slot to poly.
11334+ if pt_a == "nil"
11335+ return pt_b
11336+ end
11337+ if pt_b == "nil"
11338+ return pt_a
11339+ end
11340+ # int + float coerce to float (numeric-compatible).
11341+ if pt_a == "int" && pt_b == "float"
11342+ return "float"
11343+ end
11344+ if pt_a == "float" && pt_b == "int"
11345+ return "float"
11346+ end
11347+ # Genuinely incompatible: must hold both at runtime.
11348+ @needs_rb_value = 1
11349+ return "poly"
11350+ end
11351+
1111711352 # Collect every method name called on `pname` anywhere under nid.
1111811353 # Used by parameter type inference to find the class that satisfies
1111911354 # ALL accesses, avoiding a single-reader match that ignores later
@@ -12842,6 +13077,11 @@
1284213077 update_ivar_types_from_params
1284313078 # Infer setter param types from ivar types
1284413079 infer_writer_param_types
13080+ # Widen param slot types when a body reassigns the param to an
13081+ # incompatible value (e.g. `def f(hclk); hclk = "forever" if
13082+ # hclk.nil?; end` — call sites pass int, body assigns string,
13083+ # so the slot becomes poly).
13084+ widen_param_types_from_body_writes
1284513085
1284613086 # Top-level methods
1284713087 i = 0