Commit 649feee0
649feee0e99fMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
fix(analyze,codegen): raise ... unless x.is_a?(C) narrows x for rest of scope
Sam Ruby's #493 proposal: raise ... unless x.is_a?(C) and the
equivalent if !x.is_a?(C); raise ...; end are definite-throw
type assertions. After such a statement, control only flows when
the predicate held, so x is provably of class C for the rest
of the enclosing scope. Spinel previously emitted the runtime
check correctly but didn't carry the narrow forward -- field
access on x after the guard fell into the full cls_id-switch
over every class that declared the same field name, inflating
binary size and producing dispatch noise that breaks under
unrelated additions elsewhere in the program.
This change makes the narrow stack sibling-scoped for raise-guard statements: pushed at the statement that contains the guard, popped at the end of the enclosing StatementsNode. The push/pop asymmetry vs. the existing if-arm narrows (matched per-arm) is the non-trivial part.
Implementation:
-
spinel_analyze.rb: new helpers
body_definitely_raises?andparse_raise_guard_narrowrecognizeUnlessNodewhose body raises andIfNodewhose negated!is_a?predicate gates a raising body. Three body-walker call sites (scan_new_calls,collect_return_types_nid,scan_cls_method_calls) push the narrow at each guard stmt in their stmts loop and pop them all after the loop. Callee param widening from post-guard sibling calls now seesxas the narrowed type via the existingfind_var_typenarrow- first lookup. -
spinel_codegen.rb: copies the same helpers (
narrow_type_for_class/parse_is_a_predicate/body_definitely_raises?/parse_raise_guard_narrow) plus the missingpush_type_narrow/pop_type_narrowwrappers (codegen had the narrow stack but no public push/pop).compile_body_returnnow wraps an inner_innerimplementation with a baseline-and-cleanup pattern so the inner's many early-return paths leave the stack consistent without per- return pop calls.compile_call_exprcasts the recv expr through.v.pwhen the receiver is aLocalVariableReadNodewhose narrowed type isobj_<C>but declared type ispoly-- the underlying storage is still sp_RbVal so the call site has to unbox before reading struct fields.infer_typeadds a pre-cache narrow-aware arm that resolvesLV(narrowed).attr_reader_namedirectly to the narrowed class's ivar type, bypassing the stale@nd_inferred_typecache that was filled by analyze before the narrow was active.
Scope notes:
- Only
is_a?/kind_of?predicates are recognized (the existingparse_is_a_predicatehandles both).nil?-based guards (raise ... if x.nil?) follow the same shape but aren't part of this change. - The body must terminate in a
raisecall. Bodies that raise inside nested control flow (unless cond; if other; raise; end; end) leave the narrow stack alone -- conservative. IfNodeguards require noelse/elsifchain; an else branch means fall-through doesn't imply the predicate held.
Test (test/raise_unless_isa_narrow.rb): exercises both spellings
plus a two-guard sequence that accumulates narrows. Verified
the emit shape (single cls_id arm, direct struct-field access)
matches the issue's "desired output" via spinel ... -S.
Closes #493
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 649feee0e99f
- Parent
- 880d7270f2c0
spinel_analyze.rbmodifiedView file
# and the argument is a constant naming a known class. Returns # `["", ""]` if the predicate isn't a narrowable shape (the empty # var name is the sentinel; the caller skips the push). # Returns 1 if `body_id` is a body whose statements always raise # (every reachable tail is a `raise ...` call). The simple form # is a single-statement body whose statement is `raise ...`; # we treat that as the canonical "definite throw" shape and # leave more complex shapes (raise inside nested if/case) to a # future extension. Issue #493. def body_definitely_raises?(body_id) if body_id < 0 return 0 end stmts_r = get_stmts(body_id) if stmts_r.length == 0 return 0 end # Multi-statement bodies: check the LAST stmt for the raise. # CRuby's semantics treat the body as definitely-throwing iff # the tail throws; earlier statements may do bookkeeping # before the raise (logger.error, etc.). last = stmts_r[stmts_r.length - 1] if @nd_type[last] != "CallNode" return 0 end if @nd_name[last] != "raise" return 0 end 1 end # Decode `raise ... unless x.is_a?(C)` / `if !x.is_a?(C); raise; end` # / `raise ... if !x.is_a?(C)` into `(var_name, narrow_type)`. After # such a guard, the rest of the enclosing scope can assume `x` is # of class `C` (the raise eliminated the other path). Returns # `["", ""]` for non-guard statements. Used by the body-walker # in scan_new_calls / collect_return_types_nid / # scan_cls_method_calls (and the codegen-side body walkers) to # push a sibling-scope narrow for the statements that follow. # Issue #493. def parse_raise_guard_narrow(nid) if nid < 0 return ["", ""] end t = @nd_type[nid] if t == "UnlessNode" # `raise ... unless x.is_a?(C)`: body fires when predicate is # false. Fall-through assumes `x.is_a?(C)` is true. body_u = @nd_body[nid] if body_definitely_raises?(body_u) == 0 return ["", ""] end return parse_is_a_predicate(@nd_predicate[nid]) end if t == "IfNode" # `raise ... if !x.is_a?(C)` and `if !x.is_a?(C); raise; end`: # body fires when the negated predicate is true (so x is NOT # of class C). Fall-through assumes x IS of class C. Peel one # level of `!` off the predicate before reusing # parse_is_a_predicate. pred_i = @nd_predicate[nid] if pred_i < 0 || @nd_type[pred_i] != "CallNode" || @nd_name[pred_i] != "!" return ["", ""] end inner_i = @nd_receiver[pred_i] if inner_i < 0 return ["", ""] end body_i = @nd_body[nid] if body_definitely_raises?(body_i) == 0 return ["", ""] end sub_i = @nd_subsequent[nid] else_i = @nd_else_clause[nid] if sub_i >= 0 || else_i >= 0 # Has an else clause — falling through doesn't necessarily # mean the predicate was true (the else might run). Skip. return ["", ""] end return parse_is_a_predicate(inner_i) end ["", ""] end def parse_is_a_predicate(pred_id) if pred_id < 0 return ["", ""] scan_new_calls(@nd_body[nid]) end stmts = parse_id_list(@nd_stmts[nid]) # Sibling-scope narrow for `raise ... unless x.is_a?(C)` guards. # When stmts[k] is a definite-throw guard, push the narrow # before walking siblings k+1..N so any recursive call inside # those siblings sees `x` as the narrowed type (and # unify_call_types widens the callee's param accordingly). # The narrow is asymmetric vs. the if-arm shape: pushed at # stmts[k], popped at end-of-StatementsNode. Issue #493. pushed_raise_guards_snc = 0 k = 0 while k < stmts.length scan_new_calls(stmts[k]) rg_p = parse_raise_guard_narrow(stmts[k]) if rg_p[0] != "" push_type_narrow(rg_p[0], rg_p[1]) pushed_raise_guards_snc = pushed_raise_guards_snc + 1 end k = k + 1 end while pushed_raise_guards_snc > 0 pop_type_narrow pushed_raise_guards_snc = pushed_raise_guards_snc - 1 end if @nd_receiver[nid] >= 0 scan_new_calls(@nd_receiver[nid]) end def collect_return_types_nid(nid, types) stmts = get_stmts(nid) # Sibling-scope narrow for `raise unless x.is_a?(C)` guards, so # a `return v` inside a sibling sees the narrowed type and the # return-unify uses the narrow instead of the wider underlying # poly param. Mirrors scan_new_calls' stmts loop. Issue #493. pushed_raise_guards_crt = 0 k = 0 while k < stmts.length collect_return_types(stmts[k], types) rg_p = parse_raise_guard_narrow(stmts[k]) if rg_p[0] != "" push_type_narrow(rg_p[0], rg_p[1]) pushed_raise_guards_crt = pushed_raise_guards_crt + 1 end k = k + 1 end while pushed_raise_guards_crt > 0 pop_type_narrow pushed_raise_guards_crt = pushed_raise_guards_crt - 1 end end def collect_return_types(nid, types) scan_cls_method_calls(ci, @nd_body[nid]) end stmts = parse_id_list(@nd_stmts[nid]) # Sibling-scope narrow for `raise unless x.is_a?(C)` guards. # Same shape as scan_new_calls' stmts loop. Issue #493. pushed_raise_guards_scm = 0 k = 0 while k < stmts.length scan_cls_method_calls(ci, stmts[k]) rg_p = parse_raise_guard_narrow(stmts[k]) if rg_p[0] != "" push_type_narrow(rg_p[0], rg_p[1]) pushed_raise_guards_scm = pushed_raise_guards_scm + 1 end k = k + 1 end while pushed_raise_guards_scm > 0 pop_type_narrow pushed_raise_guards_scm = pushed_raise_guards_scm - 1 end if @nd_receiver[nid] >= 0 scan_cls_method_calls(ci, @nd_receiver[nid]) endspinel_codegen.rbmodifiedView file
Loading diff…
test/raise_unless_isa_narrow.rbaddedView file
Loading diff…
test/raise_unless_isa_narrow.rb.expectedaddedView file
Loading diff…