Commit aa2145e9

aa2145e9398eMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(analyze): back-propagate ivar hash type to []= writer params

Closes #488. A class method body like @data[key] = value where @data has been pinned to a concrete hash variant (e.g. str_str_hash, by a sibling method that iterates over a typed source) left the writer's key and value params at the mrb_int default. The C emit then passed mrb_int into the const char * key / value slots and tripped -Wint-conversion.

Sibling to #482's nil-default + Hash-receiver pass. The shape of the back-propagation gap is symmetric: the read side (v = @data[k]) already flows the slot's value type into v correctly, the write side (@data[k] = v) didn't flow the slot back into v's param type.

New pass widen_params_from_ivar_hash_aset walks every class method body for @hash[key_id] = value_id writes. When the recv is an InstanceVariableReadNode whose ivar has a typed hash variant in cls_ivar_type, and key_id / value_id are LocalVariableReadNodes of params currently typed int, widen the param to the hash's key / value spinel type (hash_key_type_from_variant / new hash_value_type_from_variant helper). Same fixpoint-loop hook as widen_nil_default_params_used_as_hash.

Validation: tests 460 pass (regression covers the exact repro from the issue plus a fetch round-trip), bench 57/57, bootstrap 4-way fixpoint OK.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
aa2145e9398e
Parent
1c819212d772

spinel_analyze.rbmodifiedView file

@@ -11723,6 +11723,183 @@
1172311723 end
1172411724 end
1172511725
11726+ # Spinel hash variants spell out (key, value) types in the name.
11727+ # Given a hash variant token, return the value-side spinel type.
11728+ def hash_value_type_from_variant(t)
11729+ bt = is_nullable_type(t) == 1 ? base_type(t) : t
11730+ if bt == "str_int_hash" || bt == "sym_int_hash"
11731+ return "int"
11732+ end
11733+ if bt == "str_str_hash" || bt == "sym_str_hash" || bt == "int_str_hash"
11734+ return "string"
11735+ end
11736+ if bt == "str_poly_hash" || bt == "sym_poly_hash" || bt == "poly_poly_hash"
11737+ return "poly"
11738+ end
11739+ ""
11740+ end
11741+
11742+ # Walk `nid` looking for `@<iname>[<key_id>] = <val_id>` writes
11743+ # against a typed hash ivar of class `ci`. For each match, push the
11744+ # observed (key_param_idx, key_t) and (val_param_idx, val_t) facts
11745+ # into the accumulators when the key / value AST node is a
11746+ # LocalVariableReadNode naming a method param. Helper for
11747+ # widen_params_from_ivar_hash_aset.
11748+ def collect_param_aset_through_ivar_hash(nid, ci, pnames, ptypes, key_widen, val_widen)
11749+ if nid < 0
11750+ return
11751+ end
11752+ if @nd_type[nid] == "DefNode"
11753+ return
11754+ end
11755+ if @nd_type[nid] == "ClassNode" || @nd_type[nid] == "ModuleNode"
11756+ return
11757+ end
11758+ if @nd_type[nid] == "CallNode" && @nd_name[nid] == "[]="
11759+ recv_aset = @nd_receiver[nid]
11760+ if recv_aset >= 0 && @nd_type[recv_aset] == "InstanceVariableReadNode"
11761+ ivt_aset = cls_ivar_type(ci, @nd_name[recv_aset])
11762+ kt_aset = hash_key_type_from_variant(ivt_aset)
11763+ vt_aset = hash_value_type_from_variant(ivt_aset)
11764+ if kt_aset != "" && vt_aset != ""
11765+ args_id_aset = @nd_arguments[nid]
11766+ if args_id_aset >= 0
11767+ a_ids_aset = get_args(args_id_aset)
11768+ if a_ids_aset.length >= 2
11769+ key_id_aset = a_ids_aset[0]
11770+ val_id_aset = a_ids_aset[a_ids_aset.length - 1]
11771+ if @nd_type[key_id_aset] == "LocalVariableReadNode"
11772+ pname_key = @nd_name[key_id_aset]
11773+ pi_key = 0
11774+ while pi_key < pnames.length
11775+ if pnames[pi_key] == pname_key && pi_key < ptypes.length && ptypes[pi_key] == "int" && kt_aset != "int"
11776+ key_widen[pi_key] = kt_aset
11777+ end
11778+ pi_key = pi_key + 1
11779+ end
11780+ end
11781+ if @nd_type[val_id_aset] == "LocalVariableReadNode"
11782+ pname_val = @nd_name[val_id_aset]
11783+ pi_val = 0
11784+ while pi_val < pnames.length
11785+ if pnames[pi_val] == pname_val && pi_val < ptypes.length && ptypes[pi_val] == "int" && vt_aset != "int"
11786+ val_widen[pi_val] = vt_aset
11787+ end
11788+ pi_val = pi_val + 1
11789+ end
11790+ end
11791+ end
11792+ end
11793+ end
11794+ end
11795+ end
11796+ # Recurse via the same field set scan_locals walks.
11797+ if @nd_body[nid] >= 0
11798+ collect_param_aset_through_ivar_hash(@nd_body[nid], ci, pnames, ptypes, key_widen, val_widen)
11799+ end
11800+ stmts_aset = parse_id_list(@nd_stmts[nid])
11801+ k = 0
11802+ while k < stmts_aset.length
11803+ collect_param_aset_through_ivar_hash(stmts_aset[k], ci, pnames, ptypes, key_widen, val_widen)
11804+ k = k + 1
11805+ end
11806+ if @nd_expression[nid] >= 0
11807+ collect_param_aset_through_ivar_hash(@nd_expression[nid], ci, pnames, ptypes, key_widen, val_widen)
11808+ end
11809+ if @nd_predicate[nid] >= 0
11810+ collect_param_aset_through_ivar_hash(@nd_predicate[nid], ci, pnames, ptypes, key_widen, val_widen)
11811+ end
11812+ if @nd_subsequent[nid] >= 0
11813+ collect_param_aset_through_ivar_hash(@nd_subsequent[nid], ci, pnames, ptypes, key_widen, val_widen)
11814+ end
11815+ if @nd_else_clause[nid] >= 0
11816+ collect_param_aset_through_ivar_hash(@nd_else_clause[nid], ci, pnames, ptypes, key_widen, val_widen)
11817+ end
11818+ if @nd_arguments[nid] >= 0
11819+ collect_param_aset_through_ivar_hash(@nd_arguments[nid], ci, pnames, ptypes, key_widen, val_widen)
11820+ end
11821+ if @nd_block[nid] >= 0
11822+ collect_param_aset_through_ivar_hash(@nd_block[nid], ci, pnames, ptypes, key_widen, val_widen)
11823+ end
11824+ if @nd_left[nid] >= 0
11825+ collect_param_aset_through_ivar_hash(@nd_left[nid], ci, pnames, ptypes, key_widen, val_widen)
11826+ end
11827+ if @nd_right[nid] >= 0
11828+ collect_param_aset_through_ivar_hash(@nd_right[nid], ci, pnames, ptypes, key_widen, val_widen)
11829+ end
11830+ end
11831+
11832+ # #488. A method whose body writes a param into a typed ivar hash
11833+ # (`@data[k] = v` where @data has been pinned to e.g. str_str_hash
11834+ # by another method) leaves the writer's `k` / `v` params at the
11835+ # `mrb_int` default. The C emit then passes mrb_int into the
11836+ # const char * key / value slots — Wint-conversion error.
11837+ # Sibling to #482's nil-default + Hash-receiver pass; this one
11838+ # covers the @hash-write side of the same back-propagation gap.
11839+ def widen_params_from_ivar_hash_aset
11840+ ci = 0
11841+ while ci < @cls_names.length
11842+ mnames = @cls_meth_names[ci].split(";")
11843+ bodies = @cls_meth_bodies[ci].split(";")
11844+ cls_changed = 0
11845+ mj = 0
11846+ while mj < mnames.length
11847+ pnames_j = cls_meth_pnames_get(ci, mj)
11848+ ptypes_j = cls_meth_ptypes_get(ci, mj)
11849+ bid_j = -1
11850+ if mj < bodies.length
11851+ bid_j = bodies[mj].to_i
11852+ end
11853+ if bid_j >= 0 && bid_j != -2 && pnames_j.length > 0
11854+ key_widen = "".split(",")
11855+ val_widen = "".split(",")
11856+ while key_widen.length < pnames_j.length
11857+ key_widen.push("")
11858+ val_widen.push("")
11859+ end
11860+ saved_ci_aset = @current_class_idx
11861+ @current_class_idx = ci
11862+ collect_param_aset_through_ivar_hash(bid_j, ci, pnames_j, ptypes_j, key_widen, val_widen)
11863+ @current_class_idx = saved_ci_aset
11864+ m_changed = 0
11865+ pk = 0
11866+ while pk < pnames_j.length
11867+ new_pt = ""
11868+ if pk < key_widen.length && key_widen[pk] != ""
11869+ new_pt = key_widen[pk]
11870+ end
11871+ if pk < val_widen.length && val_widen[pk] != ""
11872+ # If both key and value widening apply to the same param
11873+ # (highly unusual — same param flowing into both slots),
11874+ # the value type wins; the key path was speculative.
11875+ new_pt = val_widen[pk]
11876+ end
11877+ if new_pt != "" && pk < ptypes_j.length && ptypes_j[pk] == "int"
11878+ ptypes_j[pk] = new_pt
11879+ if new_pt == "str_str_hash"
11880+ @needs_str_str_hash = 1
11881+ end
11882+ if new_pt == "sym_str_hash"
11883+ @needs_sym_str_hash = 1
11884+ end
11885+ m_changed = 1
11886+ end
11887+ pk = pk + 1
11888+ end
11889+ if m_changed == 1
11890+ cls_meth_ptypes_put(ci, mj, ptypes_j)
11891+ cls_changed = 1
11892+ end
11893+ end
11894+ mj = mj + 1
11895+ end
11896+ if cls_changed == 1
11897+ @cls_meth_ptypes_version = @cls_meth_ptypes_version + 1
11898+ end
11899+ ci = ci + 1
11900+ end
11901+ end
11902+
1172611903 # Widen each method's stored parameter types to encompass any
1172711904 # in-body reassignments to the parameter name. Without this,
1172811905 # a body shape like
@@ -16669,6 +16846,7 @@
1666916846 narrow_param_types_from_body_method_calls
1667016847 infer_string_param_from_body
1667116848 widen_nil_default_params_used_as_hash
16849+ widen_params_from_ivar_hash_aset
1667216850 infer_param_type_from_callee_slot
1667316851 narrow_param_hash_types_from_body_writes
1667416852 # propagate hash-each block-arg types into