Commit e1fc6f9a

e1fc6f9a521dMerged

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

Description

fix(analyze): def []= value param widens to poly on mixed ivar slots (#439)

def []=(name, value) whose body dispatches on name and writes value into ivars of differing types (@id = value and @name = value, with @id: mrb_int and @name: const char *) emitted with lv_value: mrb_int from initialize-time observation, then the second ivar assignment failed C compile with incompatible-integer-to-pointer conversion.

widen_param_types_from_body_writes' per-class arm now also walks the body for InstanceVariableWriteNode whose RHS is the param. When two or more distinct ivar slot types appear, the param widens to poly so each ivar-write branch can unbox/cast to its slot type (downstream pinning then promotes the ivars themselves to poly, giving a uniform sp_RbVal store on both arms).

Gated on mnames[mj] == "[]=" to keep the existing pattern (def ir_set_<X>_ivar(name, val); if name == "..."; @x = val; ... in Compiler / IR setters) intact — those rely on val staying typed against the slot at the call site, not on per-branch unboxing. The []= shape from #439 is the canonical abstract-setter pattern where the body genuinely needs the param to be heterogeneous.

Closes #439

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

Details

Commit
e1fc6f9a521d
Parent
c655d53dbc21

spinel_analyze.rbmodifiedView file

@@ -10877,6 +10877,22 @@
1087710877 while pk < pnames_j.length
1087810878 if pk < ptypes_j.length
1087910879 new_t = scan_param_body_write_unify(bid_j, pnames_j[pk], ptypes_j[pk])
10880+ # `def []=(name, value)` style: the body dispatches on
10881+ # `name` and writes `value` into ivars of differing
10882+ # types (e.g. `@id = value` AND `@name = value`, with
10883+ # @id mrb_int and @name const char*). Without widening
10884+ # the param to poly, the C emitter pins lv_value at the
10885+ # first-observed type and rejects the other ivar's
10886+ # assignment. Sniff for >1 distinct ivar slot type
10887+ # observed as the RHS-is-pname target, and promote to
10888+ # "poly" when found.
10889+ if new_t != "poly" && mnames[mj] == "[]="
10890+ ivt_set = "".split(",")
10891+ collect_param_ivar_write_slot_types(bid_j, pnames_j[pk], ci, ivt_set)
10892+ if ivt_set.length >= 2
10893+ new_t = "poly"
10894+ end
10895+ end
1088010896 if new_t != ptypes_j[pk]
1088110897 ptypes_j[pk] = new_t
1088210898 inner_changed = 1
@@ -10900,6 +10916,37 @@
1090010916 end
1090110917 end
1090210918
10919+ # Walk `nid` looking for `InstanceVariableWriteNode` whose RHS is
10920+ # a `LocalVariableReadNode` named `pname`, and accumulate each
10921+ # written-to ivar's declared slot type into `ivt_set` (unique).
10922+ # Used by widen_param_types_from_body_writes to detect
10923+ # `def []=(_, value); case ... when :a then @a = value;
10924+ # when :b then @b = value; end` shapes where the param flows
10925+ # into ivars of differing types — the param then needs to widen
10926+ # to poly so each ivar-write arm can unbox/cast to its slot type.
10927+ def collect_param_ivar_write_slot_types(nid, pname, ci, ivt_set)
10928+ if nid < 0
10929+ return
10930+ end
10931+ if @nd_type[nid] == "InstanceVariableWriteNode"
10932+ rhs = @nd_expression[nid]
10933+ if rhs >= 0 && @nd_type[rhs] == "LocalVariableReadNode" && @nd_name[rhs] == pname
10934+ iname = @nd_name[nid]
10935+ slot_t = cls_ivar_type(ci, iname)
10936+ if slot_t != "" && not_in(slot_t, ivt_set) == 1
10937+ ivt_set.push(slot_t)
10938+ end
10939+ end
10940+ end
10941+ cs = []
10942+ push_child_ids(nid, cs)
10943+ k = 0
10944+ while k < cs.length
10945+ collect_param_ivar_write_slot_types(cs[k], pname, ci, ivt_set)
10946+ k = k + 1
10947+ end
10948+ end
10949+
1090310950 # Walk under `nid` looking for `LocalVariableWriteNode`s targeting
1090410951 # `pname` and unify the literal-typed RHS with `cur_t`. Method-call
1090510952 # RHSes are skipped: this pass runs before return-type inference,