Commit 8de7725e

8de7725ed79cMerged

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

Description

fix(analyze): back-propagate Hash type to nil-default param used as [] recv

Closes #482. A method like def m(other = nil) whose body does other[key] failed C compile when the lookup result landed in a concretely-typed pointer field. With no caller passing a non-nil other, spinel typed the param at the mrb_int fallback. The body's other["key"] then fell through to the unresolved-call warning (emits 0) and the resulting lv_v assigned to the typed ivar slot tripped -Wint-conversion. Compounding the failure, both return if other.nil? and if !v.nil? folded away because spinel reasoned about mrb_int 0 as Integer 0 whose .nil? is false — the user-level guards never fired.

Sibling to #447's return-path back-propagation. Fix here: widen_nil_default_params_used_as_hash detects an int-typed parameter with nil default whose body uses it as a String-keyed Hash receiver (pname["literal"]) and widens the parameter's stored type to str_str_hash. Spinel already treats hash pointers as nullable, so the .nil? guards then resolve dynamically and the DCE doesn't fold the early-return away.

Limited scope: str_str_hash only — String keys, string-shaped result. The repro's ActionDispatch::Flash#initialize shape and the original toy repro both match this. Other hash variants (sym keys, int values, etc.) can extend the same recognizer when they show up.

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

Details

Commit
8de7725ed79c
Parent
01d16ad62711

spinel_analyze.rbmodifiedView file

@@ -11477,6 +11477,203 @@
1147711477 end
1147811478 end
1147911479
11480+ # Walk a body looking for `pname[<string-literal>]` index reads on
11481+ # `pname`. Returns 1 if any such site exists. Used by the
11482+ # nil-default + hash-index widening pass to decide that the param's
11483+ # static type should be str_str_hash rather than the mrb_int default.
11484+ def param_used_with_str_index?(nid, pname)
11485+ if nid < 0
11486+ return 0
11487+ end
11488+ # Don't cross nested scopes — pname may shadow.
11489+ if @nd_type[nid] == "DefNode"
11490+ return 0
11491+ end
11492+ if @nd_type[nid] == "ClassNode" || @nd_type[nid] == "ModuleNode"
11493+ return 0
11494+ end
11495+ if @nd_type[nid] == "CallNode" && @nd_name[nid] == "[]"
11496+ recv_pwi = @nd_receiver[nid]
11497+ if recv_pwi >= 0 && @nd_type[recv_pwi] == "LocalVariableReadNode" && @nd_name[recv_pwi] == pname
11498+ args_id_pwi = @nd_arguments[nid]
11499+ if args_id_pwi >= 0
11500+ a_ids_pwi = get_args(args_id_pwi)
11501+ if a_ids_pwi.length >= 1 && @nd_type[a_ids_pwi[0]] == "StringNode"
11502+ return 1
11503+ end
11504+ end
11505+ end
11506+ end
11507+ # Recurse into all child fields that can carry CallNode subtrees.
11508+ if @nd_body[nid] >= 0
11509+ if param_used_with_str_index?(@nd_body[nid], pname) == 1
11510+ return 1
11511+ end
11512+ end
11513+ stmts_pwi = parse_id_list(@nd_stmts[nid])
11514+ k = 0
11515+ while k < stmts_pwi.length
11516+ if param_used_with_str_index?(stmts_pwi[k], pname) == 1
11517+ return 1
11518+ end
11519+ k = k + 1
11520+ end
11521+ if @nd_expression[nid] >= 0
11522+ if param_used_with_str_index?(@nd_expression[nid], pname) == 1
11523+ return 1
11524+ end
11525+ end
11526+ if @nd_predicate[nid] >= 0
11527+ if param_used_with_str_index?(@nd_predicate[nid], pname) == 1
11528+ return 1
11529+ end
11530+ end
11531+ if @nd_subsequent[nid] >= 0
11532+ if param_used_with_str_index?(@nd_subsequent[nid], pname) == 1
11533+ return 1
11534+ end
11535+ end
11536+ if @nd_else_clause[nid] >= 0
11537+ if param_used_with_str_index?(@nd_else_clause[nid], pname) == 1
11538+ return 1
11539+ end
11540+ end
11541+ if @nd_receiver[nid] >= 0
11542+ if param_used_with_str_index?(@nd_receiver[nid], pname) == 1
11543+ return 1
11544+ end
11545+ end
11546+ if @nd_arguments[nid] >= 0
11547+ if param_used_with_str_index?(@nd_arguments[nid], pname) == 1
11548+ return 1
11549+ end
11550+ end
11551+ if @nd_block[nid] >= 0
11552+ if param_used_with_str_index?(@nd_block[nid], pname) == 1
11553+ return 1
11554+ end
11555+ end
11556+ if @nd_left[nid] >= 0
11557+ if param_used_with_str_index?(@nd_left[nid], pname) == 1
11558+ return 1
11559+ end
11560+ end
11561+ if @nd_right[nid] >= 0
11562+ if param_used_with_str_index?(@nd_right[nid], pname) == 1
11563+ return 1
11564+ end
11565+ end
11566+ 0
11567+ end
11568+
11569+ # Check whether the param at index `pi` has a `nil` default value.
11570+ # `defaults_str` is the per-method comma-joined list of default
11571+ # AST node ids ("-1" for no default; otherwise the def_id).
11572+ def param_default_is_nil?(defaults_str, pi)
11573+ if defaults_str == ""
11574+ return 0
11575+ end
11576+ defs = defaults_str.split(",")
11577+ if pi >= defs.length
11578+ return 0
11579+ end
11580+ def_id = defs[pi].to_i
11581+ if def_id < 0
11582+ return 0
11583+ end
11584+ if @nd_type[def_id] == "NilNode"
11585+ return 1
11586+ end
11587+ 0
11588+ end
11589+
11590+ # #482. A method with `def m(other = nil)` whose body does
11591+ # `other[key]` (Hash-receiver index read) leaves `other` at the
11592+ # `mrb_int` fallback when no caller passes a non-nil value. The
11593+ # body's `other["key"]` then falls through to `[] on int` (emits 0),
11594+ # the resulting local lands in a typed pointer field, and gcc fires
11595+ # an int-to-pointer conversion error. Worse, `return if other.nil?`
11596+ # / `if !v.nil?` collapse because spinel reasons about `mrb_int 0`
11597+ # as `Integer 0` (`.nil?` -> FALSE), losing the nil-encoded semantics.
11598+ #
11599+ # Sibling to #447 (return-path back-prop). Fix here: when the body
11600+ # uses an int-typed param with nil default as a String-keyed Hash
11601+ # receiver, widen the param's stored type to `str_str_hash`. Spinel
11602+ # already treats hash pointers as nullable, so the early-return /
11603+ # nil-guard then survives DCE.
11604+ def widen_nil_default_params_used_as_hash
11605+ mi = 0
11606+ while mi < @meth_names.length
11607+ bid = @meth_body_ids[mi]
11608+ if bid >= 0
11609+ pnames = @meth_param_names[mi].split(",")
11610+ ptypes = @meth_param_types[mi].split(",")
11611+ defaults_str_m = @meth_has_defaults[mi]
11612+ changed_m = 0
11613+ pk = 0
11614+ while pk < pnames.length
11615+ if pk < ptypes.length && ptypes[pk] == "int"
11616+ if param_default_is_nil?(defaults_str_m, pk) == 1
11617+ if param_used_with_str_index?(bid, pnames[pk]) == 1
11618+ ptypes[pk] = "str_str_hash"
11619+ changed_m = 1
11620+ end
11621+ end
11622+ end
11623+ pk = pk + 1
11624+ end
11625+ if changed_m == 1
11626+ @meth_param_types[mi] = ptypes.join(",")
11627+ end
11628+ end
11629+ mi = mi + 1
11630+ end
11631+ ci = 0
11632+ while ci < @cls_names.length
11633+ mnames = @cls_meth_names[ci].split(";")
11634+ bodies = @cls_meth_bodies[ci].split(";")
11635+ defaults_per = @cls_meth_defaults[ci].split("|")
11636+ cls_changed = 0
11637+ mj = 0
11638+ while mj < mnames.length
11639+ pnames_j = cls_meth_pnames_get(ci, mj)
11640+ ptypes_j = cls_meth_ptypes_get(ci, mj)
11641+ bid_j = -1
11642+ if mj < bodies.length
11643+ bid_j = bodies[mj].to_i
11644+ end
11645+ defaults_str_j = ""
11646+ if mj < defaults_per.length
11647+ defaults_str_j = defaults_per[mj]
11648+ end
11649+ if bid_j >= 0 && bid_j != -2
11650+ m_changed = 0
11651+ pk = 0
11652+ while pk < pnames_j.length
11653+ if pk < ptypes_j.length && ptypes_j[pk] == "int"
11654+ if param_default_is_nil?(defaults_str_j, pk) == 1
11655+ if param_used_with_str_index?(bid_j, pnames_j[pk]) == 1
11656+ ptypes_j[pk] = "str_str_hash"
11657+ m_changed = 1
11658+ end
11659+ end
11660+ end
11661+ pk = pk + 1
11662+ end
11663+ if m_changed == 1
11664+ cls_meth_ptypes_put(ci, mj, ptypes_j)
11665+ cls_changed = 1
11666+ end
11667+ end
11668+ mj = mj + 1
11669+ end
11670+ if cls_changed == 1
11671+ @cls_meth_ptypes_version = @cls_meth_ptypes_version + 1
11672+ end
11673+ ci = ci + 1
11674+ end
11675+ end
11676+
1148011677 # Widen each method's stored parameter types to encompass any
1148111678 # in-body reassignments to the parameter name. Without this,
1148211679 # a body shape like
@@ -16403,6 +16600,7 @@
1640316600 infer_param_array_type_from_body
1640416601 narrow_param_types_from_body_method_calls
1640516602 infer_string_param_from_body
16603+ widen_nil_default_params_used_as_hash
1640616604 infer_param_type_from_callee_slot
1640716605 narrow_param_hash_types_from_body_writes
1640816606 # propagate hash-each block-arg types into