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:

  1. scan_writer_calls' InstanceVariableWriteNode arm: when the RHS is a LocalVariableReadNode whose current type is int_array, look at the enclosing body's collect_param_push_elem_types observations on that local (the helper already serves both params and locals — it walks any matching LVR push recv). Apply empty_array_promotion_for and use the result. A new @cur_writer_body instance var threads the body root down so the inner arm can call back into collection.
  2. infer_body_return: when the body's implicit-return is a bare LocalVariableReadNode typed int_array, apply the same promotion. Lifts function-return types from sp_IntArray * to sp_<Obj>_ptr_array * for the def f; results = []; results << X.new; results; end shape.

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

@@ -20,6 +20,11 @@
2020 @indent = 0
2121 @temp_counter = 0
2222 @label_counter = 0
23+ # Body root tracked during scan_writer_calls so the
24+ # InstanceVariableWriteNode arm can look at the body for push
25+ # observations on a LVR rhs and derive a promoted array type
26+ # before pinning the ivar.
27+ @cur_writer_body = -1
2328
2429 # ---- AST node storage (parallel arrays by node ID) ----
2530 # Use "".split(",") for StrArray init (v1 infers StrArray from split)
@@ -12007,7 +12012,9 @@
1200712012 declare_var(ml[lk], mt[lk])
1200812013 lk = lk + 1
1200912014 end
12015+ @cur_writer_body = @meth_body_ids[i]
1201012016 scan_writer_calls(@meth_body_ids[i])
12017+ @cur_writer_body = -1
1201112018 end
1201212019 @current_method_name = saved_meth
1201312020 pop_scope
@@ -12043,7 +12050,9 @@
1204312050 declare_var(ml2[lk2], mt2[lk2])
1204412051 lk2 = lk2 + 1
1204512052 end
12053+ @cur_writer_body = bid
1204612054 scan_writer_calls(bid)
12055+ @cur_writer_body = -1
1204712056 pop_scope
1204812057 end
1204912058 bj = bj + 1
@@ -12084,7 +12093,9 @@
1208412093 declare_var(cml[clk], cmt[clk])
1208512094 clk = clk + 1
1208612095 end
12096+ @cur_writer_body = cbid
1208712097 scan_writer_calls(cbid)
12098+ @cur_writer_body = -1
1208812099 pop_scope
1208912100 end
1209012101 cbj = cbj + 1
@@ -12300,6 +12311,24 @@
1230012311 # widen the promoted type to poly on the next iteration.
1230112312 if is_empty_hash_literal(bottom) == 0 && is_empty_array_literal(bottom) == 0
1230212313 at = infer_type(bottom)
12314+ # `@ivar = <local>` where <local>'s slot type is "int_array"
12315+ # (the empty-array default that scan_locals records for `x =
12316+ # []`) often promotes later via push observations within the
12317+ # same body — `results = []; results << Article.new;
12318+ # @articles = results`. infer_type at scan time only sees the
12319+ # current scope-recorded type (int_array), so the ivar pins
12320+ # at int_array even though the assignment is meant to carry a
12321+ # ptr_array of obj. Look at the body's push elements to derive
12322+ # the promoted type before recording.
12323+ if at == "int_array" && @cur_writer_body >= 0 && @nd_type[bottom] == "LocalVariableReadNode"
12324+ lname_a = @nd_name[bottom]
12325+ elem_acc_a = "".split(",")
12326+ collect_param_push_elem_types(@cur_writer_body, lname_a, elem_acc_a)
12327+ promoted_a = empty_array_promotion_for(elem_acc_a)
12328+ if promoted_a != ""
12329+ at = promoted_a
12330+ end
12331+ end
1230312332 # Record this writer's concrete type observation while
1230412333 # the local-scope context is set up (params declared at
1230512334 # their iteratively-widened ptypes). After all
@@ -13475,8 +13504,23 @@
1347513504 # Collect all explicit return types
1347613505 types = "".split(",")
1347713506 collect_return_types_nid(body_id, types)
13478- # Add implicit return (last expression)
13479- last_type = infer_type(stmts.last)
13507+ # Add implicit return (last expression). When the implicit return
13508+ # is a bare LocalVariableReadNode of an "int_array"-typed local
13509+ # (the empty-`[]` default), look at the body's push observations
13510+ # on the same local to derive the promoted array type, so a
13511+ # `def f; results = []; results << Comment.new; results; end`
13512+ # body's return is sp_PtrArray<Comment> rather than sp_IntArray.
13513+ last_id = stmts.last
13514+ last_type = infer_type(last_id)
13515+ if last_type == "int_array" && @nd_type[last_id] == "LocalVariableReadNode"
13516+ lname_lr = @nd_name[last_id]
13517+ elem_acc_lr = "".split(",")
13518+ collect_param_push_elem_types(body_id, lname_lr, elem_acc_lr)
13519+ promoted_lr = empty_array_promotion_for(elem_acc_lr)
13520+ if promoted_lr != ""
13521+ last_type = promoted_lr
13522+ end
13523+ end
1348013524 types.push(last_type)
1348113525 # Unify all return path types
1348213526 unify_return_type(types)