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
ClassInfo *ci = &c->classes[i]; for (int j = 0; j < ci->ncvars; j++) { TyKind t = ci->cvar_types[j] == TY_UNKNOWN ? TY_INT : ci->cvar_types[j]; /* static initializers must be constant. default_value() returns the sp_box_nil() *call* for poly, which is not a constant initializer at file scope; emit the equivalent constant aggregate instead (mirrors the civ class-ivar decls below). This matters under --int-overflow=promote where every int cvar is widened to poly. */ const char *init = t == TY_RANGE ? "{0}" : t == TY_POLY ? "{SP_TAG_NIL, 0, {0}}" : default_value(t); buf_puts(&b, "static "); emit_ctype(c, t, &b); buf_printf(&b, " cvar_%s_%s = %s;\n", ci->name, ci->cvars[j] + 2, t == TY_RANGE ? "{0}" : default_value(t)); buf_printf(&b, " cvar_%s_%s = %s;\n", ci->name, ci->cvars[j] + 2, init); } }