Commit 33785ae0
33785ae0404bMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
fix: mark local as poly when literal writes have different types
scan_locals treated "int" as a fallback/placeholder and silently
upgraded the stored type when a later write had a more specific type.
That's correct when the "int" came from an unresolved read, but wrong
when x = 1 is an explicit IntegerNode literal — a following
x = "hello" then clobbers the int with string, and the declared
local type doesn't reflect the genuine polymorphism.
Track a parallel flag array (@scan_literal_flags) recording whether each local's current stored type came from an explicit literal write (IntegerNode, FloatNode, StringNode, SymbolNode, TrueNode, FalseNode). On a later write, if both the stored type and the new type come from literals and differ, mark the local as "poly" immediately instead of falling through to the legacy upgrade path.
Heuristic-preserving: x = 0; x = some_call() (first literal, second
non-literal) still follows the existing fallback-int rule and becomes
the call's return type. Only mixed-literal-types flip to poly.
bm_poly: 1 warning → 0. 74 tests / 55 benchmarks pass. Total test+benchmark warnings now: 0.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 33785ae0404b
- Parent
- 437ecd14c6ad
spinel_codegen.rbmodifiedView file
end end # Returns 1 if `nid` is an explicit literal value (not a placeholder or # inferred fallback). Used by scan_locals to distinguish a genuine int # write like `x = 1` from a defaulted "int" from an unresolved read. def is_literal_value_expr(nid) if nid < 0 return 0 end t = @nd_type[nid] if t == "IntegerNode" return 1 end if t == "FloatNode" return 1 end if t == "StringNode" return 1 end if t == "SymbolNode" return 1 end if t == "TrueNode" || t == "FalseNode" return 1 end 0 end def scan_locals(nid, names, types, params) if nid < 0 return end # Parallel to `names`: "1" if this local's current stored type was set # by an explicit literal write, "" otherwise. Reset when called with # a fresh (empty) names array. if names.length == 0 @scan_literal_flags = "".split(",") end if @nd_type[nid] == "MultiWriteNode" targets = parse_id_list(@nd_targets[nid]) targets.each { |tid| if not_in(lname, params) == 1 names.push(lname) types.push("int") @scan_literal_flags.push("") end end end if not_in(lname, params) == 1 names.push(lname) types.push(infer_type(@nd_expression[nid])) if is_literal_value_expr(@nd_expression[nid]) == 1 @scan_literal_flags.push("1") else @scan_literal_flags.push("") end end else if not_in(lname, params) == 1 if names[ki] == lname if types[ki] != at if types[ki] != "poly" # Genuine polymorphism: both the first write and this # write were explicit literals, and their types differ. # This catches `x = 1; x = "hello"` which the legacy # "int is fallback" rule below would silently coerce. if ki < @scan_literal_flags.length && @scan_literal_flags[ki] == "1" && is_literal_value_expr(@nd_expression[nid]) == 1 && at != "nil" && types[ki] != "nil" types[ki] = "poly" @needs_rb_value = 1 @scan_literal_flags[ki] = "" ki = ki + 1 next end # Don't mark poly if new type is fallback "int" and existing is richer if at != "int" if types[ki] == "int"