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 end
1114811148 end
1114911149
11150+ # Widen each method's stored parameter types to encompass any
11151+ # in-body reassignments to the parameter name. Without this,
11152+ # a body shape like
11153+ #
11154+ # def f(hclk, ...)
11155+ # hclk = "forever" if hclk == FOREVER_CLOCK
11156+ # ...
11157+ # end
11158+ #
11159+ # leaves `hclk`'s slot at the call-site-inferred `int`, and the
11160+ # `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 incompatible
11165+ # writes (e.g. ptype already concrete and a different concrete
11166+ # write) escalate to poly.
11167+ def widen_param_types_from_body_writes
11168+ # Top-level methods.
11169+ mi = 0
11170+ while mi < @meth_names.length
11171+ bid = @meth_body_ids[mi]
11172+ if bid >= 0
11173+ pnames = @meth_param_names[mi].split(",")
11174+ ptypes = @meth_param_types[mi].split(",")
11175+ ptypes_changed = 0
11176+ pk = 0
11177+ while pk < pnames.length
11178+ if pk < ptypes.length
11179+ new_t = scan_param_body_write_unify(bid, pnames[pk], ptypes[pk])
11180+ if new_t != ptypes[pk]
11181+ ptypes[pk] = new_t
11182+ ptypes_changed = 1
11183+ end
11184+ end
11185+ pk = pk + 1
11186+ end
11187+ if ptypes_changed == 1
11188+ @meth_param_types[mi] = ptypes.join(",")
11189+ end
11190+ end
11191+ mi = mi + 1
11192+ end
11193+
11194+ # Per-class instance and class methods.
11195+ ci = 0
11196+ while ci < @cls_names.length
11197+ 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 = 0
11202+ mj = 0
11203+ while mj < mnames.length
11204+ if mj < all_params.length && mj < all_ptypes.length && mj < bodies.length
11205+ bid_j = bodies[mj].to_i
11206+ if bid_j >= 0
11207+ pnames_j = all_params[mj].split(",")
11208+ ptypes_j = all_ptypes[mj].split(",")
11209+ inner_changed = 0
11210+ pk = 0
11211+ while pk < pnames_j.length
11212+ if pk < ptypes_j.length
11213+ 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_t
11216+ inner_changed = 1
11217+ end
11218+ end
11219+ pk = pk + 1
11220+ end
11221+ if inner_changed == 1
11222+ all_ptypes[mj] = ptypes_j.join(",")
11223+ cls_changed = 1
11224+ end
11225+ end
11226+ end
11227+ mj = mj + 1
11228+ end
11229+ if cls_changed == 1
11230+ @cls_meth_ptypes[ci] = all_ptypes.join("|")
11231+ end
11232+ ci = ci + 1
11233+ end
11234+ end
11235+
11236+ # Walk under `nid` looking for `LocalVariableWriteNode`s targeting
11237+ # `pname` and unify the literal-typed RHS with `cur_t`. Method-call
11238+ # RHSes are skipped: this pass runs before return-type inference,
11239+ # so `infer_type(<call>)` for an unanalyzed method falls through to
11240+ # 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 < 0
11245+ return cur_t
11246+ end
11247+ nt = @nd_type[nid]
11248+ if nt == "LocalVariableWriteNode"
11249+ if @nd_name[nid] == pname
11250+ rhs = @nd_expression[nid]
11251+ if rhs >= 0 && is_reliable_literal_for_widen(rhs) == 1
11252+ at = infer_type(rhs)
11253+ cur_t = unify_param_for_body_write(cur_t, at)
11254+ end
11255+ end
11256+ end
11257+ if nt == "MultiWriteNode"
11258+ mw_targets = parse_id_list(@nd_targets[nid])
11259+ mw_val = @nd_expression[nid]
11260+ ti = 0
11261+ mw_targets.each { |tid|
11262+ if @nd_type[tid] == "LocalVariableTargetNode" && @nd_name[tid] == pname
11263+ 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]) == 1
11266+ slot_t = infer_type(elems[ti])
11267+ cur_t = unify_param_for_body_write(cur_t, slot_t)
11268+ end
11269+ end
11270+ end
11271+ ti = ti + 1
11272+ }
11273+ end
11274+ # 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] >= 0
11277+ cur_t = scan_param_body_write_unify(@nd_body[nid], pname, cur_t)
11278+ end
11279+ stmts = parse_id_list(@nd_stmts[nid])
11280+ k = 0
11281+ while k < stmts.length
11282+ cur_t = scan_param_body_write_unify(stmts[k], pname, cur_t)
11283+ k = k + 1
11284+ end
11285+ if @nd_expression[nid] >= 0
11286+ cur_t = scan_param_body_write_unify(@nd_expression[nid], pname, cur_t)
11287+ end
11288+ if @nd_predicate[nid] >= 0
11289+ cur_t = scan_param_body_write_unify(@nd_predicate[nid], pname, cur_t)
11290+ end
11291+ if @nd_subsequent[nid] >= 0
11292+ cur_t = scan_param_body_write_unify(@nd_subsequent[nid], pname, cur_t)
11293+ end
11294+ if @nd_else_clause[nid] >= 0
11295+ cur_t = scan_param_body_write_unify(@nd_else_clause[nid], pname, cur_t)
11296+ end
11297+ if @nd_receiver[nid] >= 0
11298+ cur_t = scan_param_body_write_unify(@nd_receiver[nid], pname, cur_t)
11299+ end
11300+ if @nd_arguments[nid] >= 0
11301+ cur_t = scan_param_body_write_unify(@nd_arguments[nid], pname, cur_t)
11302+ end
11303+ args = parse_id_list(@nd_args[nid])
11304+ k = 0
11305+ while k < args.length
11306+ cur_t = scan_param_body_write_unify(args[k], pname, cur_t)
11307+ k = k + 1
11308+ end
11309+ conds = parse_id_list(@nd_conditions[nid])
11310+ k = 0
11311+ while k < conds.length
11312+ cur_t = scan_param_body_write_unify(conds[k], pname, cur_t)
11313+ k = k + 1
11314+ end
11315+ if @nd_left[nid] >= 0
11316+ cur_t = scan_param_body_write_unify(@nd_left[nid], pname, cur_t)
11317+ end
11318+ if @nd_right[nid] >= 0
11319+ cur_t = scan_param_body_write_unify(@nd_right[nid], pname, cur_t)
11320+ end
11321+ if @nd_block[nid] >= 0
11322+ cur_t = scan_param_body_write_unify(@nd_block[nid], pname, cur_t)
11323+ end
11324+ elems = parse_id_list(@nd_elements[nid])
11325+ k = 0
11326+ while k < elems.length
11327+ cur_t = scan_param_body_write_unify(elems[k], pname, cur_t)
11328+ k = k + 1
11329+ end
11330+ if @nd_rescue_clause[nid] >= 0
11331+ cur_t = scan_param_body_write_unify(@nd_rescue_clause[nid], pname, cur_t)
11332+ end
11333+ if @nd_ensure_clause[nid] >= 0
11334+ cur_t = scan_param_body_write_unify(@nd_ensure_clause[nid], pname, cur_t)
11335+ end
11336+ cur_t
11337+ end
11338+
11339+ # `nid` is a literal whose type is reliable even before
11340+ # return-type inference has run. Limits the body-write widening
11341+ # to writes whose RHS is unambiguous, avoiding false widenings
11342+ # driven by `infer_type` of unanalyzed method calls (which falls
11343+ # through to the `int` default).
11344+ def is_reliable_literal_for_widen(nid)
11345+ if nid < 0
11346+ return 0
11347+ end
11348+ nt = @nd_type[nid]
11349+ if nt == "StringNode" || nt == "IntegerNode" || nt == "FloatNode" || nt == "SymbolNode" || nt == "NilNode" || nt == "TrueNode" || nt == "FalseNode"
11350+ return 1
11351+ end
11352+ 0
11353+ end
11354+
11355+ # Body-write unification rule. Distinct from `unify_call_types`
11356+ # because the call-site rule treats `int` as a placeholder that
11357+ # gets overwritten by any concrete `at` — that produces
11358+ # `int + string → string`, which is fine for call-site widening
11359+ # (`int` was a default) but wrong for body-write widening (the
11360+ # 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_b
11363+ return pt_a
11364+ end
11365+ # `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_b
11369+ end
11370+ if pt_b == "nil"
11371+ return pt_a
11372+ end
11373+ # int + float coerce to float (numeric-compatible).
11374+ if pt_a == "int" && pt_b == "float"
11375+ return "float"
11376+ end
11377+ if pt_a == "float" && pt_b == "int"
11378+ return "float"
11379+ end
11380+ # Genuinely incompatible: must hold both at runtime.
11381+ @needs_rb_value = 1
11382+ return "poly"
11383+ end
11384+
1115011385 # Collect every method name called on `pname` anywhere under nid.
1115111386 # Used by parameter type inference to find the class that satisfies
1115211387 # ALL accesses, avoiding a single-reader match that ignores later
@@ -12880,6 +13115,11 @@
1288013115 update_ivar_types_from_params
1288113116 # Infer setter param types from ivar types
1288213117 infer_writer_param_types
13118+ # Widen param slot types when a body reassigns the param to an
13119+ # incompatible value (e.g. `def f(hclk); hclk = "forever" if
13120+ # hclk.nil?; end` — call sites pass int, body assigns string,
13121+ # so the slot becomes poly).
13122+ widen_param_types_from_body_writes
1288313123
1288413124 # Top-level methods
1288513125 i = 0