Commit 470384e4
470384e41da1Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · ~2 modified
Description
analyze/codegen: widen LV slot on ||= / &&= cross-type writes
`a = nil; a ||= 5` left `a` typed as nil because scan_locals had type-merge rules for LocalVariableWriteNode but no equivalent for LocalVariableOrWriteNode / LocalVariableAndWriteNode -- the conditional rhs never participated in the slot's type. Downstream `puts a.inspect` then resolved against the nil-typed slot at compile time and emitted the literal "nil" even though the runtime value was 5.
Three coordinated changes:
- scan_locals: apply the same nil/T merge rules used for plain LocalVariableWriteNode -- nil + int stays int? if a scalar nullable pointer family exists; otherwise widens to poly.
- infer_type: report the slot's type for LocalVariableOrWriteNode / AndWriteNode as expression value, so `y = (x ||= 7)` gives y the same widened type as x.
- expression-form codegen for both nodes: when the slot is poly / scalar-nullable, build the ternary with truthy_c_expr and box the rhs through box_value_to_poly. The statement-form emit already had this path (added in an earlier commit referenced here as "issue #702"); the expression-form was the missing sibling.
Fixes #702
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 470384e41da1
- Parent
- a976fcfe53b5
spinel_analyze.rbmodifiedView file
@@ -2102,6 +2102,18 @@
21022102
end21032103
return infer_type(@nd_expression[nid])21042104
end2105+
if t == "LocalVariableOrWriteNode" || t == "LocalVariableAndWriteNode"2106+
# `var ||= expr` / `var &&= expr` as expression value lowers to2107+
# the slot's type after widening (which scan_locals already did2108+
# via the merge rules below). Falling back to the rhs type made2109+
# the surrounding LV slot type for `y = (x ||= 7)` mismatch2110+
# when scan_locals widened x to poly. Issue #702.2111+
vt_oa = find_var_type(@nd_name[nid])2112+
if vt_oa != ""2113+
return vt_oa2114+
end2115+
return infer_type(@nd_expression[nid])2116+
end21052117
if t == "MultiWriteNode"21062118
# `(a, b = 10, 11)` as an expression returns the RHS array.21072119
# The rhs ArrayNode's type infers normally; the wrapping@@ -26072,6 +26084,48 @@
2607226084
end2607326085
end2607426086
end26087+
# `a ||= rhs` / `a &&= rhs` -- the conditional write can switch26088+
# `a` from its initial type to the rhs type. Widen the slot26089+
# so reads downstream see the union (poly for incompatible26090+
# base types). Same merge rules as LocalVariableWriteNode.26091+
# Issue #702.26092+
if @nd_type[nid] == "LocalVariableOrWriteNode" || @nd_type[nid] == "LocalVariableAndWriteNode"26093+
lname = @nd_name[nid]26094+
if not_in(lname, names) == 126095+
if not_in(lname, params) == 126096+
names.push(lname)26097+
types.push(infer_type(@nd_expression[nid]))26098+
@scan_literal_flags.push("")26099+
@scan_empty_flags.push("")26100+
@scan_empty_hash_flags.push("")26101+
end26102+
else26103+
if not_in(lname, params) == 126104+
at_oa = infer_type(@nd_expression[nid])26105+
koa = 026106+
while koa < names.length26107+
if names[koa] == lname && types[koa] != at_oa && types[koa] != "poly"26108+
if at_oa == "nil" && is_nullable_pointer_type(types[koa]) == 126109+
if types[koa][types[koa].length - 1] != "?"26110+
types[koa] = types[koa] + "?"26111+
end26112+
elsif types[koa] == "nil" && is_nullable_pointer_type(at_oa) == 126113+
types[koa] = is_nullable_type(at_oa) == 1 ? at_oa : (at_oa + "?")26114+
elsif base_type(types[koa]) == at_oa || base_type(at_oa) == types[koa] || base_type(types[koa]) == base_type(at_oa)26115+
# compatible — leave the richer one26116+
if base_type(at_oa) == types[koa]26117+
types[koa] = at_oa26118+
end26119+
else26120+
types[koa] = "poly"26121+
@needs_rb_value = 126122+
end26123+
end26124+
koa = koa + 126125+
end26126+
end26127+
end26128+
end2607526129
if @nd_type[nid] == "LocalVariableOperatorWriteNode"2607626130
lname = @nd_name[nid]2607726131
rhs_type = infer_type(@nd_expression[nid])spinel_codegen.rbmodifiedView file
Loading diff…