Commit 435ea3b7

435ea3b76984Merged

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

Description

codegen,analyze: route widened class-variable op-assign through sp_poly_

A poly-widened class variable cannot use the C compound assignment cvar op= v (no operator on sp_RbVal). Emit cvar = sp_poly_<op>(cvar, boxed v) in both the statement and expression forms (mirrors the local op-assign poly arm). Also fix the inference: a ClassVariableOperatorWriteNode reads-operates-writes the cvar, so its result type is the cvar's slot type (poly when widened), not the RHS value's type -- otherwise the tail-return path boxes the already-poly result a second time. The cvar type equals the old value type for a non-widened int cvar, so default mode is unchanged. Unblocks op_assign_tail_return.

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

Details

Commit
435ea3b76984
Parent
519739794707

src/analyze_infer.cmodifiedView file

@@ -2647,8 +2647,18 @@
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. */
2653+ const char *nm = nt_str(nt, id, "name");
2654+ Scope *s = comp_scope_of(c, id);
2655+ int cid = s ? s->class_id : -1;
2656+ if (cid < 0) cid = comp_class_index(c, "Toplevel");
2657+ int idx = (cid >= 0 && nm) ? comp_cvar_index(&c->classes[cid], nm) : -1;
2658+ if (idx >= 0) return c->classes[cid].cvar_types[idx];
2659+ return infer_type(c, nt_ref(nt, id, "value"));
2660+ }
26502661 if (nk == NK_ClassVariableWriteNode ||
2651- nk == NK_ClassVariableOperatorWriteNode ||
26522662 nk == NK_ClassVariableOrWriteNode ||
26532663 nk == NK_ClassVariableAndWriteNode)
26542664 return infer_type(c, nt_ref(nt, id, "value"));