Commit a0e14abe

a0e14abe8ff5Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): widen module-constant array/hash from << writes (#333 sister to #303)

A module-level array constant initialized as an empty literal (LOG = []) was typed as sp_IntArray * regardless of how it was used. A later LOG << some_hash mistyped the hash pointer as mrb_int.

The existing refine_module_ivar_types pass already handled the parallel @slots = {} shape (issue #303): walk subsequent writes to the empty container, accumulate observed key/value types, pick the matching typed-array / typed-hash. Extend it to also walk ConstantWriteNode declarations so module-level constants get the same refinement.

Three pieces:

  • refine_module_ivar_types: detect ConstantWriteNode in addition to InstanceVariableWriteNode, mapping the constant's name to the same <Mod>_<name> slot in @const_types.

  • New scan_module_const_writes helper walks LOG << v / LOG[k] = v / LOG.push(v) shapes whose receiver is a ConstantReadNode named LOG. Mirrors scan_module_ivar_writes's shape, swapping the receiver-type check.

  • The temporary scope used inside the walk now also declares body locals via scan_locals. Without this, entry = {...}; LOG << entry had entry's LocalVariableReadNode resolve to the "int" default (since only params were declared), so val_t_set ended up ["int"] and pick_array_class returned the same int_array we started with. The ivar shape (@slots[k] = v where k/v are params) didn't need this because params were already declared.

Fixes #333

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
a0e14abe8ff5
Parent
e438d961b51c

spinel_codegen.rbmodifiedView file

@@ -8184,9 +8184,26 @@
81848184
81858185 def refine_module_ivar_types(mname, body_stmts)
81868186 body_stmts.each { |sid|
8187- next unless @nd_type[sid] == "InstanceVariableWriteNode"
8188- iname = @nd_name[sid]
8189- cname2 = mname + "_" + iname[1, iname.length - 1]
8187+ # Two shapes both refine the same `@const_types` slot:
8188+ # @slots = {} (InstanceVariableWriteNode in a module body —
8189+ # spinel hoists module ivars to file-scope
8190+ # constants named `<Mod>_<iname>`)
8191+ # LOG = [] (ConstantWriteNode — registered directly as
8192+ # `<Mod>_<cname>`)
8193+ # Issue #303 covered the ivar shape; #333 added the bare-
8194+ # constant shape, where `LOG << some_hash` writes never
8195+ # propagated back to flip the constant off the empty-array
8196+ # `int_array` default.
8197+ iv_write = @nd_type[sid] == "InstanceVariableWriteNode"
8198+ const_write = @nd_type[sid] == "ConstantWriteNode"
8199+ next unless iv_write || const_write
8200+ if iv_write
8201+ iname = @nd_name[sid]
8202+ cname2 = mname + "_" + iname[1, iname.length - 1]
8203+ else
8204+ iname = @nd_name[sid]
8205+ cname2 = mname + "_" + iname
8206+ end
81908207 ci = find_const_idx(cname2)
81918208 next if ci < 0
81928209 cur = @const_types[ci]
@@ -8225,10 +8242,30 @@
82258242 declare_var(pnames3[k3], pt3)
82268243 k3 = k3 + 1
82278244 end
8245+ # Issue #333: declare body locals too. Without this, a
8246+ # `LOG << entry` write where `entry = { ... }` was assigned
8247+ # earlier in the body has the LocalVariableReadNode read on
8248+ # `entry` resolve via infer_type's "int" default — and
8249+ # val_t_set ends up `["int"]` so pick_array_class returns
8250+ # the same int_array we started with. The ivar shape
8251+ # (`@slots[k] = v` where k/v are params) didn't need this
8252+ # because params were already declared above.
8253+ local_names = "".split(",")
8254+ local_types = "".split(",")
8255+ scan_locals(bid, local_names, local_types, pnames3)
8256+ lk = 0
8257+ while lk < local_names.length
8258+ declare_var(local_names[lk], local_types[lk])
8259+ lk = lk + 1
8260+ end
82288261 end
82298262 saved_meth = @current_method_name
82308263 @current_method_name = synth_name
8231- scan_module_ivar_writes(bid, iname, key_t_set, val_t_set)
8264+ if iv_write
8265+ scan_module_ivar_writes(bid, iname, key_t_set, val_t_set)
8266+ else
8267+ scan_module_const_writes(bid, iname, key_t_set, val_t_set)
8268+ end
82328269 @current_method_name = saved_meth
82338270 pop_scope
82348271 }
@@ -8282,6 +8319,40 @@
82828319
82838320 # Walk `nid` accumulating distinct key + value types observed at
82848321 # `@<iname>[k] = v` / `@<iname> << v` style writes.
8322+ # Issue #333: parallel to scan_module_ivar_writes for module-level
8323+ # constants. `LOG << v` / `LOG[k] = v` / `LOG.push(v)` where LOG is
8324+ # a ConstantReadNode (resolving via the enclosing module's lexical
8325+ # scope) contributes its key/value types to the same set the
8326+ # refinement uses to pick the typed-array / typed-hash shape.
8327+ def scan_module_const_writes(nid, cname, key_t_set, val_t_set)
8328+ if nid < 0
8329+ return
8330+ end
8331+ if @nd_type[nid] == "CallNode"
8332+ mname_call = @nd_name[nid]
8333+ recv = @nd_receiver[nid]
8334+ if recv >= 0 && @nd_type[recv] == "ConstantReadNode" && @nd_name[recv] == cname
8335+ args_id = @nd_arguments[nid]
8336+ if args_id >= 0
8337+ arg_ids = get_args(args_id)
8338+ if mname_call == "[]=" && arg_ids.length >= 2
8339+ uniq_push(key_t_set, infer_type(arg_ids[0]))
8340+ uniq_push(val_t_set, infer_type(arg_ids[1]))
8341+ elsif (mname_call == "<<" || mname_call == "push") && arg_ids.length >= 1
8342+ uniq_push(val_t_set, infer_type(arg_ids[0]))
8343+ end
8344+ end
8345+ end
8346+ end
8347+ cs = []
8348+ push_child_ids(nid, cs)
8349+ k = 0
8350+ while k < cs.length
8351+ scan_module_const_writes(cs[k], cname, key_t_set, val_t_set)
8352+ k = k + 1
8353+ end
8354+ end
8355+
82858356 def scan_module_ivar_writes(nid, iname, key_t_set, val_t_set)
82868357 if nid < 0
82878358 return