Commit 1adfa330
1adfa330a411Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
analyze: trace push/append through inherited lazy-init getters
getter << x where getter is a no-arg method whose last statement
returns @ivar was not updating the ivar's type — the body-usage scan
only handled LocalVariableReadNode and InstanceVariableReadNode as
direct push receivers.
Add a CallNode arm: when is_push and the receiver is a no-arg call,
look up the method in the current class's inheritance chain, check if
its body's last statement is an InstanceVariableReadNode, and update
that ivar's type slot in the defining class.
Fixes ivar_lazy_init_push_via_inherited_getter (629 pass, was 628).
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- 1adfa330a411
- Parent
- 5416cf121eca
src/analyze.cmodifiedView file
@@ -2913,6 +2913,30 @@
29132913
if (iv < 0) continue;29142914
slot = &ci->ivar_types[iv];29152915
}2916+
else if (is_push && rty && !strcmp(rty, "CallNode")) {2917+
/* `getter_method << x` where getter returns @ivar: trace through2918+
to that ivar so cross-class lazy-init getters get widened. */2919+
int recv_args = nt_ref(nt, recv, "arguments");2920+
int recv_argc = 0;2921+
if (recv_args >= 0) nt_arr(nt, recv_args, "arguments", &recv_argc);2922+
if (recv_argc != 0) continue;2923+
const char *mname = nt_str(nt, recv, "name");2924+
if (!mname) continue;2925+
Scope *caller = comp_scope_of(c, recv);2926+
if (!caller || caller->class_id < 0) continue;2927+
int defcls2 = caller->class_id;2928+
int getter_mi = comp_method_in_chain(c, caller->class_id, mname, &defcls2);2929+
if (getter_mi < 0) continue;2930+
int last2 = scope_body_last(c, getter_mi);2931+
if (last2 < 0 || !nt_type(nt, last2) ||2932+
strcmp(nt_type(nt, last2), "InstanceVariableReadNode")) continue;2933+
const char *inm2 = nt_str(nt, last2, "name");2934+
if (!inm2) continue;2935+
ClassInfo *ci2 = &c->classes[defcls2];2936+
int iv2 = comp_ivar_index(ci2, inm2);2937+
if (iv2 < 0) continue;2938+
slot = &ci2->ivar_types[iv2];2939+
}29162940
else continue;29172941
29182942
TyKind before = *slot;