Commit 509895a3

509895a3ab3bMerged

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

Description

codegen,analyze: handle bitwise/shift and ||=/&&= on a poly class variable

For a class variable widened to poly under promote: route a bitwise/shift/mod op-assign (<<= >>= &= |= ^= %=) through unbox-op-rebox (cvar = sp_box_int(sp_poly_to_i(cvar) v)), and guard the truthiness in a ||= / &&= with sp_poly_truthy (a raw sp_RbVal is a struct, not a scalar condition), boxing the assigned value and yielding sp_box_nil() for the false arm. Also infer all four class-variable write nodes (write/op/or/and) as the cvar's slot type so a tail does not box the already-poly result twice. Unblocks bundle_class_05; equals prior behaviour for non-widened (default) cvars.

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

Details

Commit
509895a3ab3b
Parent
61f81b6c1563

src/analyze_infer.cmodifiedView file

@@ -2647,11 +2647,13 @@
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 || 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.) */
2650+ if (nk == NK_ClassVariableOperatorWriteNode || nk == NK_ClassVariableWriteNode ||
2651+ nk == NK_ClassVariableOrWriteNode || nk == NK_ClassVariableAndWriteNode) {
2652+ /* `@@x op= v` / `@@x = v` / `@@x ||= v` / `@@x &&= v` write the cvar and yield
2653+ the stored value, which the codegen coerces to the cvar's slot type (poly
2654+ when widened under promote) -- so the expression's type is the cvar's, not
2655+ v's. (For a non-widened cvar this equals v's type, so default mode is
2656+ unchanged.) */
26552657 const char *nm = nt_str(nt, id, "name");
26562658 Scope *s = comp_scope_of(c, id);
26572659 int cid = s ? s->class_id : -1;
@@ -2660,9 +2662,6 @@
26602662 if (idx >= 0) return c->classes[cid].cvar_types[idx];
26612663 return infer_type(c, nt_ref(nt, id, "value"));
26622664 }
2663- if (nk == NK_ClassVariableOrWriteNode ||
2664- nk == NK_ClassVariableAndWriteNode)
2665- return infer_type(c, nt_ref(nt, id, "value"));
26662665 if (nk == NK_IndexOrWriteNode || nk == NK_IndexAndWriteNode) {
26672666 int recv = nt_ref(nt, id, "receiver");
26682667 if (recv < 0) return TY_UNKNOWN;