Commit 3a227557

3a22755772ceMerged

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

Description

codegen: emit poly cvar static init as a constant aggregate

A file-scope static sp_RbVal cvar_X = default_value(TY_POLY) expands to sp_box_nil(), a function call, which is not a constant initializer and fails the C compile ("initializer element is not constant"). Emit the equivalent constant aggregate {SP_TAG_NIL, 0, {0}} instead, mirroring the civ class-ivar decls. This unblocks every poly class variable, which under --int-overflow=promote is every int cvar after the full int->poly widen (class_var_read now passes in promote mode); it also fixes a latent default-mode bug for genuinely heterogeneous class variables.

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

Details

Commit
3a22755772ce
Parent
5faff8781125

src/codegen.cmodifiedView file

@@ -2741,10 +2741,17 @@
27412741 ClassInfo *ci = &c->classes[i];
27422742 for (int j = 0; j < ci->ncvars; j++) {
27432743 TyKind t = ci->cvar_types[j] == TY_UNKNOWN ? TY_INT : ci->cvar_types[j];
2744+ /* static initializers must be constant. default_value() returns the
2745+ sp_box_nil() *call* for poly, which is not a constant initializer at
2746+ file scope; emit the equivalent constant aggregate instead (mirrors the
2747+ civ class-ivar decls below). This matters under --int-overflow=promote
2748+ where every int cvar is widened to poly. */
2749+ const char *init = t == TY_RANGE ? "{0}"
2750+ : t == TY_POLY ? "{SP_TAG_NIL, 0, {0}}"
2751+ : default_value(t);
27442752 buf_puts(&b, "static ");
27452753 emit_ctype(c, t, &b);
2746- buf_printf(&b, " cvar_%s_%s = %s;\n", ci->name, ci->cvars[j] + 2,
2747- t == TY_RANGE ? "{0}" : default_value(t));
2754+ buf_printf(&b, " cvar_%s_%s = %s;\n", ci->name, ci->cvars[j] + 2, init);
27482755 }
27492756 }
27502757