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:
@@ -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).
|
||||
|
||||
Reference in New Issue
Block a user