Commit 23955d9b
23955d9bbcc4Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #374 from mame/pr/widen-param-from-body-write
fix(codegen): widen param type when body reassigns it to incompatible value
Details
- Commit
- 23955d9bbcc4
- Parent
- c7f273955dba
- Merge parent
- acc935da1f1c
spinel_codegen.rbmodifiedView file
@@ -11147,6 +11147,241 @@
1114711147
end1114811148
end1114911149
11150+
# Widen each method's stored parameter types to encompass any11151+
# in-body reassignments to the parameter name. Without this,11152+
# a body shape like11153+
#11154+
# def f(hclk, ...)11155+
# hclk = "forever" if hclk == FOREVER_CLOCK11156+
# ...11157+
# end11158+
#11159+
# leaves `hclk`'s slot at the call-site-inferred `int`, and the11160+
# `lv_hclk = (const char *)…` C statement fails -Wint-conversion.11161+
# Widening to "poly" lets the slot hold both shapes via sp_RbVal.11162+
#11163+
# `int` is treated as a default/fallback (matches unify_call_types):11164+
# int + concrete-non-int → concrete. Only genuinely incompatible11165+
# writes (e.g. ptype already concrete and a different concrete11166+
# write) escalate to poly.11167+
def widen_param_types_from_body_writes11168+
# Top-level methods.11169+
mi = 011170+
while mi < @meth_names.length11171+
bid = @meth_body_ids[mi]11172+
if bid >= 011173+
pnames = @meth_param_names[mi].split(",")11174+
ptypes = @meth_param_types[mi].split(",")11175+
ptypes_changed = 011176+
pk = 011177+
while pk < pnames.length11178+
if pk < ptypes.length11179+
new_t = scan_param_body_write_unify(bid, pnames[pk], ptypes[pk])11180+
if new_t != ptypes[pk]11181+
ptypes[pk] = new_t11182+
ptypes_changed = 111183+
end11184+
end11185+
pk = pk + 111186+
end11187+
if ptypes_changed == 111188+
@meth_param_types[mi] = ptypes.join(",")11189+
end11190+
end11191+
mi = mi + 111192+
end11193+
11194+
# Per-class instance and class methods.11195+
ci = 011196+
while ci < @cls_names.length11197+
mnames = @cls_meth_names[ci].split(";")11198+
all_params = @cls_meth_params[ci].split("|")11199+
all_ptypes = @cls_meth_ptypes[ci].split("|")11200+
bodies = @cls_meth_bodies[ci].split(";")11201+
cls_changed = 011202+
mj = 011203+
while mj < mnames.length11204+
if mj < all_params.length && mj < all_ptypes.length && mj < bodies.length11205+
bid_j = bodies[mj].to_i11206+
if bid_j >= 011207+
pnames_j = all_params[mj].split(",")11208+
ptypes_j = all_ptypes[mj].split(",")11209+
inner_changed = 011210+
pk = 011211+
while pk < pnames_j.length11212+
if pk < ptypes_j.length11213+
new_t = scan_param_body_write_unify(bid_j, pnames_j[pk], ptypes_j[pk])11214+
if new_t != ptypes_j[pk]11215+
ptypes_j[pk] = new_t11216+
inner_changed = 111217+
end11218+
end11219+
pk = pk + 111220+
end11221+
if inner_changed == 111222+
all_ptypes[mj] = ptypes_j.join(",")11223+
cls_changed = 111224+
end11225+
end11226+
end11227+
mj = mj + 111228+
end11229+
if cls_changed == 111230+
@cls_meth_ptypes[ci] = all_ptypes.join("|")11231+
end11232+
ci = ci + 111233+
end11234+
end11235+
11236+
# Walk under `nid` looking for `LocalVariableWriteNode`s targeting11237+
# `pname` and unify the literal-typed RHS with `cur_t`. Method-call11238+
# RHSes are skipped: this pass runs before return-type inference,11239+
# so `infer_type(<call>)` for an unanalyzed method falls through to11240+
# the `int` default and would falsely widen a string param to poly.11241+
# Literal RHSes (StringNode, IntegerNode, FloatNode, SymbolNode,11242+
# NilNode, true/false) are reliable enough to widen on.11243+
def scan_param_body_write_unify(nid, pname, cur_t)11244+
if nid < 011245+
return cur_t11246+
end11247+
nt = @nd_type[nid]11248+
if nt == "LocalVariableWriteNode"11249+
if @nd_name[nid] == pname11250+
rhs = @nd_expression[nid]11251+
if rhs >= 0 && is_reliable_literal_for_widen(rhs) == 111252+
at = infer_type(rhs)11253+
cur_t = unify_param_for_body_write(cur_t, at)11254+
end11255+
end11256+
end11257+
if nt == "MultiWriteNode"11258+
mw_targets = parse_id_list(@nd_targets[nid])11259+
mw_val = @nd_expression[nid]11260+
ti = 011261+
mw_targets.each { |tid|11262+
if @nd_type[tid] == "LocalVariableTargetNode" && @nd_name[tid] == pname11263+
if mw_val >= 0 && @nd_type[mw_val] == "ArrayNode"11264+
elems = parse_id_list(@nd_elements[mw_val])11265+
if ti < elems.length && is_reliable_literal_for_widen(elems[ti]) == 111266+
slot_t = infer_type(elems[ti])11267+
cur_t = unify_param_for_body_write(cur_t, slot_t)11268+
end11269+
end11270+
end11271+
ti = ti + 111272+
}11273+
end11274+
# Recurse into all child fields. Mirrors collect_param_methods'11275+
# walk shape so we don't miss conditional/loop branches.11276+
if @nd_body[nid] >= 011277+
cur_t = scan_param_body_write_unify(@nd_body[nid], pname, cur_t)11278+
end11279+
stmts = parse_id_list(@nd_stmts[nid])11280+
k = 011281+
while k < stmts.length11282+
cur_t = scan_param_body_write_unify(stmts[k], pname, cur_t)11283+
k = k + 111284+
end11285+
if @nd_expression[nid] >= 011286+
cur_t = scan_param_body_write_unify(@nd_expression[nid], pname, cur_t)11287+
end11288+
if @nd_predicate[nid] >= 011289+
cur_t = scan_param_body_write_unify(@nd_predicate[nid], pname, cur_t)11290+
end11291+
if @nd_subsequent[nid] >= 011292+
cur_t = scan_param_body_write_unify(@nd_subsequent[nid], pname, cur_t)11293+
end11294+
if @nd_else_clause[nid] >= 011295+
cur_t = scan_param_body_write_unify(@nd_else_clause[nid], pname, cur_t)11296+
end11297+
if @nd_receiver[nid] >= 011298+
cur_t = scan_param_body_write_unify(@nd_receiver[nid], pname, cur_t)11299+
end11300+
if @nd_arguments[nid] >= 011301+
cur_t = scan_param_body_write_unify(@nd_arguments[nid], pname, cur_t)11302+
end11303+
args = parse_id_list(@nd_args[nid])11304+
k = 011305+
while k < args.length11306+
cur_t = scan_param_body_write_unify(args[k], pname, cur_t)11307+
k = k + 111308+
end11309+
conds = parse_id_list(@nd_conditions[nid])11310+
k = 011311+
while k < conds.length11312+
cur_t = scan_param_body_write_unify(conds[k], pname, cur_t)11313+
k = k + 111314+
end11315+
if @nd_left[nid] >= 011316+
cur_t = scan_param_body_write_unify(@nd_left[nid], pname, cur_t)11317+
end11318+
if @nd_right[nid] >= 011319+
cur_t = scan_param_body_write_unify(@nd_right[nid], pname, cur_t)11320+
end11321+
if @nd_block[nid] >= 011322+
cur_t = scan_param_body_write_unify(@nd_block[nid], pname, cur_t)11323+
end11324+
elems = parse_id_list(@nd_elements[nid])11325+
k = 011326+
while k < elems.length11327+
cur_t = scan_param_body_write_unify(elems[k], pname, cur_t)11328+
k = k + 111329+
end11330+
if @nd_rescue_clause[nid] >= 011331+
cur_t = scan_param_body_write_unify(@nd_rescue_clause[nid], pname, cur_t)11332+
end11333+
if @nd_ensure_clause[nid] >= 011334+
cur_t = scan_param_body_write_unify(@nd_ensure_clause[nid], pname, cur_t)11335+
end11336+
cur_t11337+
end11338+
11339+
# `nid` is a literal whose type is reliable even before11340+
# return-type inference has run. Limits the body-write widening11341+
# to writes whose RHS is unambiguous, avoiding false widenings11342+
# driven by `infer_type` of unanalyzed method calls (which falls11343+
# through to the `int` default).11344+
def is_reliable_literal_for_widen(nid)11345+
if nid < 011346+
return 011347+
end11348+
nt = @nd_type[nid]11349+
if nt == "StringNode" || nt == "IntegerNode" || nt == "FloatNode" || nt == "SymbolNode" || nt == "NilNode" || nt == "TrueNode" || nt == "FalseNode"11350+
return 111351+
end11352+
011353+
end11354+
11355+
# Body-write unification rule. Distinct from `unify_call_types`11356+
# because the call-site rule treats `int` as a placeholder that11357+
# gets overwritten by any concrete `at` — that produces11358+
# `int + string → string`, which is fine for call-site widening11359+
# (`int` was a default) but wrong for body-write widening (the11360+
# call site really did pass int, so the slot must hold both shapes).11361+
def unify_param_for_body_write(pt_a, pt_b)11362+
if pt_a == pt_b11363+
return pt_a11364+
end11365+
# `nil` writes don't widen; `if x.nil?` body-side `x = sentinel`11366+
# is a common shape that mustn't push the slot to poly.11367+
if pt_a == "nil"11368+
return pt_b11369+
end11370+
if pt_b == "nil"11371+
return pt_a11372+
end11373+
# int + float coerce to float (numeric-compatible).11374+
if pt_a == "int" && pt_b == "float"11375+
return "float"11376+
end11377+
if pt_a == "float" && pt_b == "int"11378+
return "float"11379+
end11380+
# Genuinely incompatible: must hold both at runtime.11381+
@needs_rb_value = 111382+
return "poly"11383+
end11384+
1115011385
# Collect every method name called on `pname` anywhere under nid.1115111386
# Used by parameter type inference to find the class that satisfies1115211387
# ALL accesses, avoiding a single-reader match that ignores later@@ -12880,6 +13115,11 @@
1288013115
update_ivar_types_from_params1288113116
# Infer setter param types from ivar types1288213117
infer_writer_param_types13118+
# Widen param slot types when a body reassigns the param to an13119+
# incompatible value (e.g. `def f(hclk); hclk = "forever" if13120+
# hclk.nil?; end` — call sites pass int, body assigns string,13121+
# so the slot becomes poly).13122+
widen_param_types_from_body_writes1288313123
1288413124
# Top-level methods1288513125
i = 0test/param_widens_when_body_assigns_string.rbaddedView file
Loading diff…
test/param_widens_when_body_assigns_string.rb.expectedaddedView file
Loading diff…