Commit dc7467e9

dc7467e98cbcMerged

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

Description

Support ||= and &&= (LocalVariableOr/AndWriteNode)

a ||= v / a &&= v: for a poly var, guard on sp_poly_truthy and box the RHS; for a bool var, plain truthiness; an always-truthy var makes ||= a no-op and &&= an unconditional assign. Inference unifies the prior type with the RHS.

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

Details

Commit
dc7467e98cbc
Parent
be4469eeb0b3

src/analyze.cmodifiedView file

@@ -851,6 +851,15 @@
851851 newt = (ct == TY_FLOAT || vt == TY_FLOAT) ? TY_FLOAT : TY_INT;
852852 else newt = ct;
853853 }
854+ else if (!strcmp(ty, "LocalVariableOrWriteNode") ||
855+ !strcmp(ty, "LocalVariableAndWriteNode")) {
856+ /* a ||= v / a &&= v : the variable can hold its prior value or v */
857+ nm = nt_str(nt, id, "name");
858+ Scope *s = comp_scope_of(c, id);
859+ LocalVar *cur = nm ? scope_local(s, nm) : NULL;
860+ TyKind ct = cur ? (TyKind)cur->gc_root : TY_UNKNOWN;
861+ newt = ty_unify(ct, infer_type(c, nt_ref(nt, id, "value")));
862+ }
854863 else {
855864 continue;
856865 }