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

@@ -9961,10 +9961,42 @@
99619961 end
99629962 end
99639963
9964+ # Returns 1 if `nid` is an explicit literal value (not a placeholder or
9965+ # inferred fallback). Used by scan_locals to distinguish a genuine int
9966+ # write like `x = 1` from a defaulted "int" from an unresolved read.
9967+ def is_literal_value_expr(nid)
9968+ if nid < 0
9969+ return 0
9970+ end
9971+ t = @nd_type[nid]
9972+ if t == "IntegerNode"
9973+ return 1
9974+ end
9975+ if t == "FloatNode"
9976+ return 1
9977+ end
9978+ if t == "StringNode"
9979+ return 1
9980+ end
9981+ if t == "SymbolNode"
9982+ return 1
9983+ end
9984+ if t == "TrueNode" || t == "FalseNode"
9985+ return 1
9986+ end
9987+ 0
9988+ end
9989+
99649990 def scan_locals(nid, names, types, params)
99659991 if nid < 0
99669992 return
99679993 end
9994+ # Parallel to `names`: "1" if this local's current stored type was set
9995+ # by an explicit literal write, "" otherwise. Reset when called with
9996+ # a fresh (empty) names array.
9997+ if names.length == 0
9998+ @scan_literal_flags = "".split(",")
9999+ end
996810000 if @nd_type[nid] == "MultiWriteNode"
996910001 targets = parse_id_list(@nd_targets[nid])
997010002 targets.each { |tid|
@@ -9974,6 +10006,7 @@
997410006 if not_in(lname, params) == 1
997510007 names.push(lname)
997610008 types.push("int")
10009+ @scan_literal_flags.push("")
997710010 end
997810011 end
997910012 end
@@ -9989,6 +10022,11 @@
998910022 if not_in(lname, params) == 1
999010023 names.push(lname)
999110024 types.push(infer_type(@nd_expression[nid]))
10025+ if is_literal_value_expr(@nd_expression[nid]) == 1
10026+ @scan_literal_flags.push("1")
10027+ else
10028+ @scan_literal_flags.push("")
10029+ end
999210030 end
999310031 else
999410032 if not_in(lname, params) == 1
@@ -9999,6 +10037,17 @@
999910037 if names[ki] == lname
1000010038 if types[ki] != at
1000110039 if types[ki] != "poly"
10040+ # Genuine polymorphism: both the first write and this
10041+ # write were explicit literals, and their types differ.
10042+ # This catches `x = 1; x = "hello"` which the legacy
10043+ # "int is fallback" rule below would silently coerce.
10044+ if ki < @scan_literal_flags.length && @scan_literal_flags[ki] == "1" && is_literal_value_expr(@nd_expression[nid]) == 1 && at != "nil" && types[ki] != "nil"
10045+ types[ki] = "poly"
10046+ @needs_rb_value = 1
10047+ @scan_literal_flags[ki] = ""
10048+ ki = ki + 1
10049+ next
10050+ end
1000210051 # Don't mark poly if new type is fallback "int" and existing is richer
1000310052 if at != "int"
1000410053 if types[ki] == "int"