Commit 2e0f15a6

2e0f15a6e310Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

analyze: a class-variable write expression yields the cvar's slot type

A ClassVariableWriteNode used as a value (e.g. tail return @@x = 5) yields the stored value coerced to the cvar's slot type, which is poly when the cvar is widened under promote -- so its inferred type is the cvar's, not the RHS value's. Otherwise the tail boxes an already-poly assignment a second time (sp_box_int(sp_box_int(...))). Equals the value type for a non-widened cvar, so default mode is unchanged. Unblocks cvar_method_first_assign.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
2e0f15a6e310
Parent
000ced33b657

src/analyze_infer.cmodifiedView file

@@ -2647,9 +2647,11 @@
26472647 int idx = nm ? comp_cvar_index(&c->classes[cid], nm) : -1;
26482648 return idx >= 0 ? c->classes[cid].cvar_types[idx] : TY_UNKNOWN;
26492649 }
2650- if (nk == NK_ClassVariableOperatorWriteNode) {
2651- /* `@@x op= v` reads, operates, and writes back the cvar, so it yields the
2652- cvar's slot type (which may be widened to poly under promote), not v's. */
2650+ if (nk == NK_ClassVariableOperatorWriteNode || nk == NK_ClassVariableWriteNode) {
2651+ /* `@@x op= v` / `@@x = v` writes the cvar and yields the stored value, which
2652+ the codegen coerces to the cvar's slot type (poly when widened under
2653+ promote) -- so the expression's type is the cvar's, not v's. (For a
2654+ non-widened cvar this equals v's type, so default mode is unchanged.) */
26532655 const char *nm = nt_str(nt, id, "name");
26542656 Scope *s = comp_scope_of(c, id);
26552657 int cid = s ? s->class_id : -1;
@@ -2658,8 +2660,7 @@
26582660 if (idx >= 0) return c->classes[cid].cvar_types[idx];
26592661 return infer_type(c, nt_ref(nt, id, "value"));
26602662 }
2661- if (nk == NK_ClassVariableWriteNode ||
2662- nk == NK_ClassVariableOrWriteNode ||
2663+ if (nk == NK_ClassVariableOrWriteNode ||
26632664 nk == NK_ClassVariableAndWriteNode)
26642665 return infer_type(c, nt_ref(nt, id, "value"));
26652666 if (nk == NK_IndexOrWriteNode || nk == NK_IndexAndWriteNode) {