Commit 133fb24a
133fb24a0a2cMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
feat(codegen): flow-sensitive nil-check narrowing in if/ternary arms
x == nil ? T : F / x != nil ? T : F / x.nil? ? T : F ternaries
now narrow x's nullable slot type to non-nullable in the arm
where x is statically non-nil.
- New
parse_nil_check_predicatehelper recognizes the three shapes against a bare LV-read. compile_if_expr's fast-path ternary pushes a type narrow onto the existing @type_narrow stack before compiling the matching arm; pops after.infer_type's LocalVariableReadNode arm consults the narrow stack on cache hit so the cachedint?is overridden when a narrow is in effect (mirrors the existing is_a? narrowing).
Note: this fixes the canonical RBS-pinned int? shape. Issue
#645's specific case has slash widened to "poly" (sp_RbVal) by
the writer-scan, not "int?", so the slot-type narrow above doesn't
fire — that case needs writer-observed types or RBS pinning to
reduce the slot first.
Refs #645.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 133fb24a0a2c
- Parent
- 9191dec7118d
spinel_codegen.rbmodifiedView file
@@ -1398,6 +1398,54 @@
13981398
""13991399
end14001400
1401+
# Recognize `<lv> == nil`, `<lv> != nil`, `<lv>.nil?` predicates1402+
# for flow-sensitive narrowing in if/ternary arms. Returns1403+
# [var_name, then_is_nil] where then_is_nil is "nil" when the1404+
# then-arm sees `<lv>` as nil (i.e. `== nil` / `.nil?`) or1405+
# "non_nil" when the then-arm sees it as non-nil (i.e. `!= nil`).1406+
# Returns ["", ""] when the predicate doesn't match.1407+
def parse_nil_check_predicate(pred_id)1408+
if pred_id < 01409+
return ["", ""]1410+
end1411+
pt = @nd_type[pred_id]1412+
if pt != "CallNode"1413+
return ["", ""]1414+
end1415+
pname = @nd_name[pred_id]1416+
if pname == "nil?"1417+
recv_nc = @nd_receiver[pred_id]1418+
if recv_nc >= 0 && @nd_type[recv_nc] == "LocalVariableReadNode"1419+
return [@nd_name[recv_nc], "nil"]1420+
end1421+
return ["", ""]1422+
end1423+
if pname == "==" || pname == "!="1424+
recv_nc = @nd_receiver[pred_id]1425+
args_nc = @nd_arguments[pred_id]1426+
if recv_nc < 0 || args_nc < 01427+
return ["", ""]1428+
end1429+
arg_ids_nc = get_args(args_nc)1430+
if arg_ids_nc.length < 11431+
return ["", ""]1432+
end1433+
# Either side may be the nil; the other must be a bare LV read.1434+
lv_side = -11435+
if @nd_type[recv_nc] == "LocalVariableReadNode" && @nd_type[arg_ids_nc[0]] == "NilNode"1436+
lv_side = recv_nc1437+
elsif @nd_type[arg_ids_nc[0]] == "LocalVariableReadNode" && @nd_type[recv_nc] == "NilNode"1438+
lv_side = arg_ids_nc[0]1439+
end1440+
if lv_side < 01441+
return ["", ""]1442+
end1443+
tag = pname == "==" ? "nil" : "non_nil"1444+
return [@nd_name[lv_side], tag]1445+
end1446+
["", ""]1447+
end1448+
14011449
def parse_is_a_predicate(pred_id)14021450
if pred_id < 014031451
return ["", ""]@@ -2374,6 +2422,22 @@
23742422
# for codegen's own infer_type to handle).23752423
cached = @nd_inferred_type[nid]23762424
if cached != ""2425+
# LocalVariableReadNode: a `<lv> == nil ? T : F` ternary's else2426+
# arm pushes a narrowed type onto @type_narrow_names; the cache2427+
# was filled by annotate_all_node_types without seeing the2428+
# narrow, so consult find_var_type whenever a narrow for this2429+
# var is in effect. Same pattern as the existing `is_a?`-based2430+
# narrowing.2431+
if @nd_type[nid] == "LocalVariableReadNode" && @type_narrow_names.length > 02432+
lv_name_nch = @nd_name[nid]2433+
ni_nch = @type_narrow_names.length - 12434+
while ni_nch >= 02435+
if @type_narrow_names[ni_nch] == lv_name_nch2436+
return @type_narrow_types[ni_nch]2437+
end2438+
ni_nch = ni_nch - 12439+
end2440+
end23772441
return cached23782442
end23792443
t = @nd_type[nid]@@ -24610,13 +24674,41 @@
2461024674
2461124675
# Fast path: every branch is at most one statement, so the2461224676
# whole if-expression collapses to a C ternary.24677+
# Flow-sensitive narrowing for `<lv> == nil ? T : F` and24678+
# `<lv> != nil ? T : F` shapes: the arm that sees `<lv>` as24679+
# non-nil narrows the var's type from `T?` to `T`, so reads24680+
# inside that arm pick the unboxed slot. Issue #645.24681+
nc_pred_ie = parse_nil_check_predicate(@nd_predicate[nid])24682+
nc_var_ie = nc_pred_ie[0]24683+
nc_then_narrow_ie = ""24684+
nc_else_narrow_ie = ""24685+
if nc_var_ie != ""24686+
slot_t_ie = find_var_type(nc_var_ie)24687+
stripped_t_ie = ""24688+
if is_nullable_type(slot_t_ie) == 124689+
stripped_t_ie = slot_t_ie[0, slot_t_ie.length - 1]24690+
end24691+
if stripped_t_ie != ""24692+
if nc_pred_ie[1] == "non_nil"24693+
nc_then_narrow_ie = stripped_t_ie24694+
elsif nc_pred_ie[1] == "nil"24695+
nc_else_narrow_ie = stripped_t_ie24696+
end24697+
end24698+
end2461324699
then_val = "0"2461424700
then_t = "nil"2461524701
if body >= 02461624702
stmts = get_stmts(body)2461724703
if stmts.length > 024704+
if nc_then_narrow_ie != ""24705+
push_type_narrow(nc_var_ie, nc_then_narrow_ie)24706+
end2461824707
then_t = infer_type(stmts.last)2461924708
then_val = compile_expr(stmts.last)24709+
if nc_then_narrow_ie != ""24710+
pop_type_narrow24711+
end2462024712
end2462124713
end2462224714
else_val = "0"@@ -24627,8 +24719,14 @@
2462724719
if eb >= 02462824720
es = get_stmts(eb)2462924721
if es.length > 024722+
if nc_else_narrow_ie != ""24723+
push_type_narrow(nc_var_ie, nc_else_narrow_ie)24724+
end2463024725
else_t = infer_type(es.last)2463124726
else_val = compile_expr(es.last)24727+
if nc_else_narrow_ie != ""24728+
pop_type_narrow24729+
end2463224730
end2463324731
end2463424732
else