fix(0113): negated-literal global initializers fold as constants

globalInitValue had no unary_op arm, so g : s64 = -1; fell into the
catch-all 'must be initialized by a compile-time constant' even though
constExprValue already folds negate(literal) for the module-const
identifier route. The new arm routes through constExprValue and applies
the direct-literal rules to the folded value: checkIntLiteralFits on
ints (g : s8 = -300 gets the range diagnostic), and a negated float at
an integer global narrows only when integral (-4.0 folds to -4, -4.5
errors). Binary-op initializers keep the specific non-constant
diagnostic.

Regression: examples/0175-types-negative-literal-global.sx.
This commit is contained in:
agra
2026-06-10 22:39:52 +03:00
parent 67313e1dad
commit 12149eb548
7 changed files with 59 additions and 4 deletions

View File

@@ -0,0 +1,16 @@
// A negated literal is a compile-time constant for a global initializer:
// ints serialize directly, an integral negative float narrows into an
// integer global (non-integral errors), and boundary values fit exactly.
// Out-of-range negatives get the literal fits-check, not "non-constant".
// Regression (issue 0113): `g : s64 = -1;` was rejected as not a
// compile-time constant (globalInitValue had no unary_op arm).
#import "modules/std.sx";
g1 : s64 = -1;
g2 : s64 = -4.0;
g3 : s8 = -128;
main :: () {
print("{} {} {}\n", g1, g2, g3);
}

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1 @@
-1 -4 -128

View File

@@ -1,3 +1,24 @@
# RESOLVED — 0113: negative-literal global initializer rejected as "not a compile-time constant"
**Root cause:** `globalInitValue` (src/ir/lower/decl.zig) had no `.unary_op`
arm, so a negated literal fell into the catch-all "must be initialized by a
compile-time constant" — even though `constExprValue` already folds
`negate(int/float literal)` for the module-const identifier route.
**Fix:** a `.unary_op` arm routes the initializer through `constExprValue`;
the folded value follows the direct-literal rules — `checkIntLiteralFits` on
ints (`g : s8 = -300;` gets the range diagnostic, not "non-constant"), and a
negated float at an integer global narrows only when integral
(`g : s64 = -4.0;` → -4; `-4.5` errors). Binary-op initializers
(`g : s32 = 2 + 3;`) remain unsupported and keep the specific
"must be initialized by a compile-time constant" diagnostic — const-expr
folding for those is a separate feature if ever wanted.
**Regression test:** `examples/0175-types-negative-literal-global.sx`
(prints `-1 -4 -128`; failed "non-constant" pre-fix).
---
# 0113 — negative-literal global initializer rejected as "not a compile-time constant"
**Symptom.** A top-level global initialized with a negated literal fails to

View File

@@ -1,4 +0,0 @@
#import "modules/std.sx";
g : s64 = -1;
main :: () { print("{}
", g); }

View File

@@ -980,6 +980,25 @@ pub fn globalInitValue(self: *Lowering, vd: *const ast.VarDecl, var_ty: TypeId)
self.checkIntLiteralFits(il.value, var_ty, v.span);
break :blk .{ .int = il.value };
},
// A negated literal (`g : s64 = -1;`) folds through the shared
// const-expr serializer. The folded value follows the same rules as
// the direct literal arms: int fits-check; a float at an integer
// global narrows only when integral.
.unary_op => blk: {
if (self.constExprValue(v, var_ty)) |cv| {
switch (cv) {
.int => |iv| self.checkIntLiteralFits(iv, var_ty, v.span),
.float => |fv| if (self.isIntEx(var_ty)) {
if (program_index_mod.floatToIntExact(fv)) |iv| break :blk inst_mod.ConstantValue{ .int = iv };
self.diagNonIntegralNarrow(v.span, fv, var_ty);
break :blk null;
},
else => {},
}
break :blk cv;
}
break :blk self.diagnoseNonConstGlobal(vd, v);
},
.bool_literal => |bl| .{ .boolean = bl.value },
// A float initializer at an integer-typed global follows the
// implicit narrowing rule (integral folds, non-integral errors).