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:

  1. 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.
  2. 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.
  3. 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 end
21032103 return infer_type(@nd_expression[nid])
21042104 end
2105+ if t == "LocalVariableOrWriteNode" || t == "LocalVariableAndWriteNode"
2106+ # `var ||= expr` / `var &&= expr` as expression value lowers to
2107+ # the slot's type after widening (which scan_locals already did
2108+ # via the merge rules below). Falling back to the rhs type made
2109+ # the surrounding LV slot type for `y = (x ||= 7)` mismatch
2110+ # 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_oa
2114+ end
2115+ return infer_type(@nd_expression[nid])
2116+ end
21052117 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 end
2607326085 end
2607426086 end
26087+ # `a ||= rhs` / `a &&= rhs` -- the conditional write can switch
26088+ # `a` from its initial type to the rhs type. Widen the slot
26089+ # so reads downstream see the union (poly for incompatible
26090+ # 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) == 1
26095+ if not_in(lname, params) == 1
26096+ 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+ end
26102+ else
26103+ if not_in(lname, params) == 1
26104+ at_oa = infer_type(@nd_expression[nid])
26105+ koa = 0
26106+ while koa < names.length
26107+ if names[koa] == lname && types[koa] != at_oa && types[koa] != "poly"
26108+ if at_oa == "nil" && is_nullable_pointer_type(types[koa]) == 1
26109+ if types[koa][types[koa].length - 1] != "?"
26110+ types[koa] = types[koa] + "?"
26111+ end
26112+ elsif types[koa] == "nil" && is_nullable_pointer_type(at_oa) == 1
26113+ 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 one
26116+ if base_type(at_oa) == types[koa]
26117+ types[koa] = at_oa
26118+ end
26119+ else
26120+ types[koa] = "poly"
26121+ @needs_rb_value = 1
26122+ end
26123+ end
26124+ koa = koa + 1
26125+ end
26126+ end
26127+ end
26128+ end
2607526129 if @nd_type[nid] == "LocalVariableOperatorWriteNode"
2607626130 lname = @nd_name[nid]
2607726131 rhs_type = infer_type(@nd_expression[nid])