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_predicate helper 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 cached int? 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 end
14001400
1401+ # Recognize `<lv> == nil`, `<lv> != nil`, `<lv>.nil?` predicates
1402+ # for flow-sensitive narrowing in if/ternary arms. Returns
1403+ # [var_name, then_is_nil] where then_is_nil is "nil" when the
1404+ # then-arm sees `<lv>` as nil (i.e. `== nil` / `.nil?`) or
1405+ # "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 < 0
1409+ return ["", ""]
1410+ end
1411+ pt = @nd_type[pred_id]
1412+ if pt != "CallNode"
1413+ return ["", ""]
1414+ end
1415+ 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+ end
1421+ return ["", ""]
1422+ end
1423+ if pname == "==" || pname == "!="
1424+ recv_nc = @nd_receiver[pred_id]
1425+ args_nc = @nd_arguments[pred_id]
1426+ if recv_nc < 0 || args_nc < 0
1427+ return ["", ""]
1428+ end
1429+ arg_ids_nc = get_args(args_nc)
1430+ if arg_ids_nc.length < 1
1431+ return ["", ""]
1432+ end
1433+ # Either side may be the nil; the other must be a bare LV read.
1434+ lv_side = -1
1435+ if @nd_type[recv_nc] == "LocalVariableReadNode" && @nd_type[arg_ids_nc[0]] == "NilNode"
1436+ lv_side = recv_nc
1437+ elsif @nd_type[arg_ids_nc[0]] == "LocalVariableReadNode" && @nd_type[recv_nc] == "NilNode"
1438+ lv_side = arg_ids_nc[0]
1439+ end
1440+ if lv_side < 0
1441+ return ["", ""]
1442+ end
1443+ tag = pname == "==" ? "nil" : "non_nil"
1444+ return [@nd_name[lv_side], tag]
1445+ end
1446+ ["", ""]
1447+ end
1448+
14011449 def parse_is_a_predicate(pred_id)
14021450 if pred_id < 0
14031451 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 else
2426+ # arm pushes a narrowed type onto @type_narrow_names; the cache
2427+ # was filled by annotate_all_node_types without seeing the
2428+ # narrow, so consult find_var_type whenever a narrow for this
2429+ # var is in effect. Same pattern as the existing `is_a?`-based
2430+ # narrowing.
2431+ if @nd_type[nid] == "LocalVariableReadNode" && @type_narrow_names.length > 0
2432+ lv_name_nch = @nd_name[nid]
2433+ ni_nch = @type_narrow_names.length - 1
2434+ while ni_nch >= 0
2435+ if @type_narrow_names[ni_nch] == lv_name_nch
2436+ return @type_narrow_types[ni_nch]
2437+ end
2438+ ni_nch = ni_nch - 1
2439+ end
2440+ end
23772441 return cached
23782442 end
23792443 t = @nd_type[nid]
@@ -24610,13 +24674,41 @@
2461024674
2461124675 # Fast path: every branch is at most one statement, so the
2461224676 # whole if-expression collapses to a C ternary.
24677+ # Flow-sensitive narrowing for `<lv> == nil ? T : F` and
24678+ # `<lv> != nil ? T : F` shapes: the arm that sees `<lv>` as
24679+ # non-nil narrows the var's type from `T?` to `T`, so reads
24680+ # 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) == 1
24689+ stripped_t_ie = slot_t_ie[0, slot_t_ie.length - 1]
24690+ end
24691+ if stripped_t_ie != ""
24692+ if nc_pred_ie[1] == "non_nil"
24693+ nc_then_narrow_ie = stripped_t_ie
24694+ elsif nc_pred_ie[1] == "nil"
24695+ nc_else_narrow_ie = stripped_t_ie
24696+ end
24697+ end
24698+ end
2461324699 then_val = "0"
2461424700 then_t = "nil"
2461524701 if body >= 0
2461624702 stmts = get_stmts(body)
2461724703 if stmts.length > 0
24704+ if nc_then_narrow_ie != ""
24705+ push_type_narrow(nc_var_ie, nc_then_narrow_ie)
24706+ end
2461824707 then_t = infer_type(stmts.last)
2461924708 then_val = compile_expr(stmts.last)
24709+ if nc_then_narrow_ie != ""
24710+ pop_type_narrow
24711+ end
2462024712 end
2462124713 end
2462224714 else_val = "0"
@@ -24627,8 +24719,14 @@
2462724719 if eb >= 0
2462824720 es = get_stmts(eb)
2462924721 if es.length > 0
24722+ if nc_else_narrow_ie != ""
24723+ push_type_narrow(nc_var_ie, nc_else_narrow_ie)
24724+ end
2463024725 else_t = infer_type(es.last)
2463124726 else_val = compile_expr(es.last)
24727+ if nc_else_narrow_ie != ""
24728+ pop_type_narrow
24729+ end
2463224730 end
2463324731 end
2463424732 else