Commit b047f1a8
b047f1a8c88cMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
fix(analyze,codegen): narrow int|nil locals to int after return-on-nil guard
Mirror of d3ee2b2 (#493) for the nil-vs-not case. After
return X if h.nil? (or raise X if h.nil?), the rest of the
enclosing scope sees h as non-nil. For a local whose most
recent writer was a known int-or-nil source (String#index,
rindex, find_index), the non-nil arm is mrb_int; pin the
narrow type to int.
Triggered by the 0210389 String#index return-type change.
Tep collected ~18 s.index(needle) callsites that all started
widening downstream locals to sp_RbVal; Ori Pekelman's
workaround was a pure-int Tep.str_find helper. The narrow
restores the idiomatic shape:
def find_or_neg(s, needle) h = s.index(needle) return -1 if h.nil? h + 1 end
Pre-fix: find_or_neg returns sp_RbVal; h + 1 lowers via
sp_poly_add and boxes the result.
Post-fix: find_or_neg returns mrb_int; h + 1 lowers as
(lv_h).v.i + 1LL (the narrow detects the declared-poly /
narrow-int mismatch at the LocalVariableReadNode emit and
inserts an explicit unbox).
Three pieces:
-
parse_nil_guard_var (analyze + codegen) recognizes
return X if h.nil?/raise X if h.nil?and returns the LV name; scan_back_writer_narrow_for walks the preceding statements to find h's writer and infer the narrow type from the writer's source call. -
The 3 analyze body-walker call sites (scan_new_calls, collect_return_types_nid, scan_cls_method_calls) plus infer_body_return push the narrow after each guard, so callee param widening AND implicit-return inference both see the narrowed type.
-
compile_expr's LocalVariableReadNode emit (codegen) unboxes via
.v.iwhen the declared C type is sp_RbVal but the active narrow says int. Existing #493 obj_ narrows over poly locals are unaffected (they route through the dispatch-level paths).
The two helpers (parse_nil_guard_var + scan_back_writer_narrow_for) return scalars instead of one combined [var, narrow_type] array on purpose: spinel-self's iterative inference widens an array-return into poly_array at some iteration order, and the cascade reaches push_type_narrow's param signature; once widened, sp_StrArray_push inside push_type_narrow's body fails to compile because it expects const char *. Keeping the helpers scalar avoids the array-element widening path.
Closes #550
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- b047f1a8c88c
- Parent
- 483fd088b459
spinel_analyze.rbmodifiedView file
["", ""] end # `<lv>.nil?` predicate. Returns the LV name; otherwise "". def parse_nil_predicate(pred_id) if pred_id < 0 return "" end if @nd_type[pred_id] != "CallNode" return "" end if @nd_name[pred_id] != "nil?" return "" end recv = @nd_receiver[pred_id] if recv < 0 || @nd_type[recv] != "LocalVariableReadNode" return "" end @nd_name[recv] end # Body is a single `return ...` or single `raise ...`. Used by # the nil-guard narrow (issue #550) to identify scope-exiting # bodies that mirror the raise-guard shape. def body_definitely_exits?(body_id) if body_id < 0 return 0 end stmts_r = get_stmts(body_id) if stmts_r.length == 0 return 0 end last = stmts_r[stmts_r.length - 1] if @nd_type[last] == "ReturnNode" return 1 end if @nd_type[last] == "CallNode" && @nd_name[last] == "raise" return 1 end 0 end # Given the rhs of the most recent write to a local variable # whose nil? was just checked, return the type the variable # narrows to after the nil-exit. Currently recognizes # `<string>.index(needle)` (and rindex / find_index) returning # int-or-nil; the non-nil arm is mrb_int. Returns "" when the # writer's shape isn't a known int-or-nil source so the caller # leaves the type alone. Issue #550. def infer_nil_guard_narrow_type(expr_id) if expr_id < 0 return "" end if @nd_type[expr_id] != "CallNode" return "" end mname_eg = @nd_name[expr_id] if mname_eg != "index" && mname_eg != "rindex" && mname_eg != "find_index" return "" end recv_eg = @nd_receiver[expr_id] if recv_eg < 0 return "" end rt_eg = infer_type(recv_eg) if rt_eg == "string" || rt_eg == "mutable_str" return "int" end "" end # Recognize `return X if h.nil?` shape; return the LV name or # "". Caller threads the stmt list separately into # scan_back_writer_narrow_for to derive the narrow type. (Two # separate calls instead of one [var, type] return because # spinel-self's inference widens an array-return into poly, # cascading into push_type_narrow's param signature.) # Issue #550. def parse_nil_guard_var(nid) if nid < 0 return "" end if @nd_type[nid] != "IfNode" return "" end body_i = @nd_body[nid] if body_definitely_exits?(body_i) == 0 return "" end sub_i = @nd_subsequent[nid] else_i = @nd_else_clause[nid] if sub_i >= 0 || else_i >= 0 return "" end parse_nil_predicate(@nd_predicate[nid]) end def scan_back_writer_narrow_for(stmts_list, before_idx, varname) j = before_idx - 1 while j >= 0 stmt = stmts_list[j] if @nd_type[stmt] == "LocalVariableWriteNode" && @nd_name[stmt] == varname return infer_nil_guard_narrow_type(@nd_expression[stmt]) end j = j - 1 end "" end def parse_is_a_predicate(pred_id) if pred_id < 0 return ["", ""] push_type_narrow(rg_p[0], rg_p[1]) pushed_raise_guards_snc = pushed_raise_guards_snc + 1 end ng_var_snc = parse_nil_guard_var(stmts[k]) if ng_var_snc != "" ng_narrow_snc = scan_back_writer_narrow_for(stmts, k, ng_var_snc) if ng_narrow_snc != "" push_type_narrow(ng_var_snc, ng_narrow_snc) pushed_raise_guards_snc = pushed_raise_guards_snc + 1 end end k = k + 1 end while pushed_raise_guards_snc > 0 # Collect all explicit return types types = "".split(",") collect_return_types_nid(body_id, types) # Push sibling-scope narrows from preceding guards so the # implicit-return type sees the narrowed local types. Without # this, `def f; h = s.index(...); return -1 if h.nil?; h+1; end` # infers `h+1` against h's poly source type, widening f's # return to poly. Issues #493 / #550. pushed_guards_ibr = 0 kk_ibr = 0 while kk_ibr < stmts.length - 1 rg_p_ibr = parse_raise_guard_narrow(stmts[kk_ibr]) if rg_p_ibr[0] != "" push_type_narrow(rg_p_ibr[0], rg_p_ibr[1]) pushed_guards_ibr = pushed_guards_ibr + 1 end ng_var_ibr = parse_nil_guard_var(stmts[kk_ibr]) if ng_var_ibr != "" ng_narrow_ibr = scan_back_writer_narrow_for(stmts, kk_ibr, ng_var_ibr) if ng_narrow_ibr != "" push_type_narrow(ng_var_ibr, ng_narrow_ibr) pushed_guards_ibr = pushed_guards_ibr + 1 end end kk_ibr = kk_ibr + 1 end # Add implicit return (last expression). When the implicit return # is a bare LocalVariableReadNode of an "int_array"-typed local # (the empty-`[]` default), look at the body's push observations end end types.push(last_type) while pushed_guards_ibr > 0 pop_type_narrow pushed_guards_ibr = pushed_guards_ibr - 1 end # Unify all return path types unify_return_type(types) end push_type_narrow(rg_p[0], rg_p[1]) pushed_raise_guards_crt = pushed_raise_guards_crt + 1 end ng_var_crt = parse_nil_guard_var(stmts[k]) if ng_var_crt != "" ng_narrow_crt = scan_back_writer_narrow_for(stmts, k, ng_var_crt) if ng_narrow_crt != "" push_type_narrow(ng_var_crt, ng_narrow_crt) pushed_raise_guards_crt = pushed_raise_guards_crt + 1 end end k = k + 1 end while pushed_raise_guards_crt > 0 push_type_narrow(rg_p[0], rg_p[1]) pushed_raise_guards_scm = pushed_raise_guards_scm + 1 end ng_var_scm = parse_nil_guard_var(stmts[k]) if ng_var_scm != "" ng_narrow_scm = scan_back_writer_narrow_for(stmts, k, ng_var_scm) if ng_narrow_scm != "" push_type_narrow(ng_var_scm, ng_narrow_scm) pushed_raise_guards_scm = pushed_raise_guards_scm + 1 end end k = k + 1 end while pushed_raise_guards_scm > 0spinel_codegen.rbmodifiedView file
Loading diff…
test/nil_guard_narrows_int_or_nil.rbaddedView file
Loading diff…
test/nil_guard_narrows_int_or_nil.rb.expectedaddedView file
Loading diff…