Commit cc45317a
cc45317ae7cdMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(analyze): propagate local empty-array push promotion to ivar/return types
#450 cascade 3. A common Ruby idiom:
results = [] while ... results << SomeClass.new(...) end @x = results # (a) ivar assignment results # (b) implicit return
leaves results typed int_array in scan_writer_calls'
view (the empty-[] default) because scan_locals records the
empty-array type and per-push widening of the local doesn't
land until a later refinement pass. When the ivar-write or the
return-type unification reads the local's type at the
analyze-time snapshot it sees int_array, and both downstream
sites stay pinned to sp_IntArray * even though every push is a
typed object — failing C compile when the consumer expects
sp_PtrArray *.
Two coordinated fixes:
- scan_writer_calls' InstanceVariableWriteNode arm: when the
RHS is a
LocalVariableReadNodewhose current type isint_array, look at the enclosing body'scollect_param_push_elem_typesobservations on that local (the helper already serves both params and locals — it walks any matching LVR push recv). Applyempty_array_promotion_forand use the result. A new@cur_writer_bodyinstance var threads the body root down so the inner arm can call back into collection. - infer_body_return: when the body's implicit-return is a
bare
LocalVariableReadNodetypedint_array, apply the same promotion. Lifts function-return types fromsp_IntArray *tosp_<Obj>_ptr_array *for thedef f; results = []; results << X.new; results; endshape.
Tests: 405 pass (+1 regression covering both shapes), bench 52, optcarrot checksum 59662.
Refs #450 (cascade 3 of 3)
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- cc45317ae7cd
- Parent
- abe5219a9d46
spinel_analyze.rbmodifiedView file
@indent = 0 @temp_counter = 0 @label_counter = 0 # Body root tracked during scan_writer_calls so the # InstanceVariableWriteNode arm can look at the body for push # observations on a LVR rhs and derive a promoted array type # before pinning the ivar. @cur_writer_body = -1 # ---- AST node storage (parallel arrays by node ID) ---- # Use "".split(",") for StrArray init (v1 infers StrArray from split) declare_var(ml[lk], mt[lk]) lk = lk + 1 end @cur_writer_body = @meth_body_ids[i] scan_writer_calls(@meth_body_ids[i]) @cur_writer_body = -1 end @current_method_name = saved_meth pop_scope declare_var(ml2[lk2], mt2[lk2]) lk2 = lk2 + 1 end @cur_writer_body = bid scan_writer_calls(bid) @cur_writer_body = -1 pop_scope end bj = bj + 1 declare_var(cml[clk], cmt[clk]) clk = clk + 1 end @cur_writer_body = cbid scan_writer_calls(cbid) @cur_writer_body = -1 pop_scope end cbj = cbj + 1 # widen the promoted type to poly on the next iteration. if is_empty_hash_literal(bottom) == 0 && is_empty_array_literal(bottom) == 0 at = infer_type(bottom) # `@ivar = <local>` where <local>'s slot type is "int_array" # (the empty-array default that scan_locals records for `x = # []`) often promotes later via push observations within the # same body — `results = []; results << Article.new; # @articles = results`. infer_type at scan time only sees the # current scope-recorded type (int_array), so the ivar pins # at int_array even though the assignment is meant to carry a # ptr_array of obj. Look at the body's push elements to derive # the promoted type before recording. if at == "int_array" && @cur_writer_body >= 0 && @nd_type[bottom] == "LocalVariableReadNode" lname_a = @nd_name[bottom] elem_acc_a = "".split(",") collect_param_push_elem_types(@cur_writer_body, lname_a, elem_acc_a) promoted_a = empty_array_promotion_for(elem_acc_a) if promoted_a != "" at = promoted_a end end # Record this writer's concrete type observation while # the local-scope context is set up (params declared at # their iteratively-widened ptypes). After all # Collect all explicit return types types = "".split(",") collect_return_types_nid(body_id, types) # Add implicit return (last expression) last_type = infer_type(stmts.last) # 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 # on the same local to derive the promoted array type, so a # `def f; results = []; results << Comment.new; results; end` # body's return is sp_PtrArray<Comment> rather than sp_IntArray. last_id = stmts.last last_type = infer_type(last_id) if last_type == "int_array" && @nd_type[last_id] == "LocalVariableReadNode" lname_lr = @nd_name[last_id] elem_acc_lr = "".split(",") collect_param_push_elem_types(body_id, lname_lr, elem_acc_lr) promoted_lr = empty_array_promotion_for(elem_acc_lr) if promoted_lr != "" last_type = promoted_lr end end types.push(last_type) # Unify all return path types unify_return_type(types)test/local_empty_array_push_promotion_propagates.rbaddedView file
Loading diff…
test/local_empty_array_push_promotion_propagates.rb.expectedaddedView file
Loading diff…