fix(ir): array-dim/count path joins the unified float→int rule — all 5 sites consistent [F0.11]
The compile-time count fold (array dimension / Vector lane / value-param) was
integer-only: it folded a DIRECT integral float literal (`[4.0]`, `[N]` with
`N : f64 : 4.0`) but rejected an INTEGRAL expression built from a non-integral
float-const leaf (`[F + 1.5]` = 4.0, `F : f64 : 2.5`) — and a const folded from
one (`[K]` with `K : s64 : F + 1.5`) — as "must be a compile-time integer
constant". This was the last of issue 0095's five narrowing sites (local /
field / param / const / array-dim) still diverging.
Route the count fold through the SAME compile-time float evaluation the other
four sites use:
- New `program_index.foldCountI64` — the single int-or-integral-float count
fold: `evalConstIntExpr` first, then (only on failure) `evalConstFloatExpr` +
`floatToIntExact`. `foldDimU32` (dim/lane/u32 value-param), the non-u32
value-param gate, and `emitModuleConst`'s integer-const materialization all
delegate to it, so a const's emitted value and its use as a count come from
one fold (no parallel integral check, no two-resolver divergence — issue 0083).
- New `DimU32.non_integral_float` variant carries a non-integral float dim to a
distinct, accurate diagnostic ("array dimension must be an integer, but '2.75'
is a non-integral float") — the cast-escape advice the binding sites give does
not apply in a count position, so the dim wording omits it. `reportDimError`,
the Vector-lane resolver, and the top-level array-alias diagnostic all handle
the new variant, so the DIRECT and type-ALIAS forms emit the identical message.
- `type_bridge.StatelessInner.lookupFloatName` (via `moduleConstFloat`) is the
float twin of its `lookupDimName`, so the registration-time alias path folds a
float-const-leaf dimension to the SAME count as the stateful direct path.
`inline for` range bounds are spec endpoints, not counts (specs.md §2), so they
keep the int-only fold deliberately (no silent-truncation bug there).
Relaxes the F0.4 `examples/1132` wording: a non-integral float const dim now
reports the precise "non-integral float" message (it still errors).
Regression: 0168 (positive — `[F + 1.5]s64`, `[KF]s64`, alias `ArrFE` all fold
to len 4), 1146 (negative — `[F + 0.25]s64` errors), 1132 (precise wording), and
a `foldCountI64`/`foldDimU32` unit test. issues/0095 marked RESOLVED (attempt 4).
specs.md + readme.md state the unified rule across all five sites.
This commit is contained in:
@@ -302,7 +302,38 @@ pub fn evalConstFloatExpr(node: *const Node, ctx: anytype) ?f64 {
|
||||
};
|
||||
}
|
||||
|
||||
/// The outcome of folding a comptime-int and narrowing it to a `u32` count
|
||||
/// The outcome of folding a compile-time COUNT expression to an `i64` under the
|
||||
/// unified float→int narrowing rule (F0.11 / issue 0095). THE single int-or-
|
||||
/// integral-float count fold: `foldDimU32` (array dim / Vector lane / u32 value-
|
||||
/// param) and the non-`u32` value-param gate both route through `foldCountI64`,
|
||||
/// so no count site can disagree on which floats fold (the issue-0083 unify-or-
|
||||
/// diverge rule extended to floats).
|
||||
pub const CountFold = union(enum) {
|
||||
/// An integer expression, or an INTEGRAL compile-time float (`[F + 1.5]` → 4).
|
||||
int: i64,
|
||||
/// A compile-time float that is not integral (`[F + 0.25]` → 2.75).
|
||||
non_integral: f64,
|
||||
/// Not a compile-time constant (runtime value, unbound name, or overflow).
|
||||
not_const,
|
||||
};
|
||||
|
||||
/// Fold `node` to an `i64` count, accepting an INTEGRAL compile-time float as the
|
||||
/// integer it equals (`4.0`, `F + 1.5`, a const folding to either) and surfacing a
|
||||
/// NON-integral compile-time float distinctly so the caller can reject it. Reuses
|
||||
/// the SAME facility the typed local/field/param/const sites use — `evalConstIntExpr`
|
||||
/// first (so int literals, named consts, `.min`/`.max`, and a DIRECT integral float
|
||||
/// literal `4.0` all fold through the single int folder), then, only when that
|
||||
/// yields no integer, `evalConstFloatExpr` + `floatToIntExact` (so an integral SUM
|
||||
/// built from a non-integral float-const leaf, `F + 1.5` = 4.0, still folds, while
|
||||
/// `F + 0.25` = 2.75 reports as non-integral). No parallel integral check.
|
||||
pub fn foldCountI64(node: *const Node, ctx: anytype) CountFold {
|
||||
if (evalConstIntExpr(node, ctx)) |v| return .{ .int = v };
|
||||
const fv = evalConstFloatExpr(node, ctx) orelse return .not_const;
|
||||
if (floatToIntExact(fv)) |iv| return .{ .int = iv };
|
||||
return .{ .non_integral = fv };
|
||||
}
|
||||
|
||||
/// The outcome of folding a comptime count and narrowing it to a `u32`
|
||||
/// (array dimension / Vector lane / value-param count). `foldDimU32` is the
|
||||
/// SINGLE place a folded integer becomes a `u32`, so the i64→u32 narrowing is
|
||||
/// range-checked exactly once and no call site does a bare `@intCast` that could
|
||||
@@ -318,16 +349,23 @@ pub const DimU32 = union(enum) {
|
||||
below_min: i64,
|
||||
/// Folded, but greater than `maxInt(u32)` — too large for a `u32` count.
|
||||
too_large: i64,
|
||||
/// A compile-time float that is not integral (`[F + 0.25]`) — under the unified
|
||||
/// float→int rule it cannot serve as an integer count; reported, never truncated.
|
||||
non_integral_float: f64,
|
||||
};
|
||||
|
||||
/// Fold `node` to a `u32` count through `evalConstIntExpr`, then range-check
|
||||
/// against `[min, maxInt(u32)]`. THE single fold-to-u32 for every array
|
||||
/// dimension, Vector lane, and value-param count — routing all of them here
|
||||
/// guarantees the narrowing is checked once and can never abort the compiler
|
||||
/// (issue 0087). The fold itself stays in `i64`; only this one conversion is the
|
||||
/// `u32` gate.
|
||||
/// Fold `node` to a `u32` count through `foldCountI64` (the unified int-or-
|
||||
/// integral-float fold), then range-check against `[min, maxInt(u32)]`. THE single
|
||||
/// fold-to-u32 for every array dimension, Vector lane, and value-param count —
|
||||
/// routing all of them here guarantees the narrowing is checked once and can never
|
||||
/// abort the compiler (issue 0087). The fold itself stays in `i64`; only this one
|
||||
/// conversion is the `u32` gate.
|
||||
pub fn foldDimU32(node: *const Node, ctx: anytype, min: u32) DimU32 {
|
||||
const v = evalConstIntExpr(node, ctx) orelse return .not_const;
|
||||
const v = switch (foldCountI64(node, ctx)) {
|
||||
.int => |iv| iv,
|
||||
.non_integral => |fv| return .{ .non_integral_float = fv },
|
||||
.not_const => return .not_const,
|
||||
};
|
||||
if (v < @as(i64, min)) return .{ .below_min = v };
|
||||
if (v > std.math.maxInt(u32)) return .{ .too_large = v };
|
||||
return .{ .ok = @intCast(v) };
|
||||
@@ -348,6 +386,7 @@ pub fn reportDimError(diag: *errors.DiagnosticList, span: ?ast.Span, result: Dim
|
||||
.below_min => |v| diag.addFmt(.err, span, "array dimension must be non-negative, got {}", .{v}),
|
||||
.too_large => |v| diag.addFmt(.err, span, "array dimension {} does not fit in u32", .{v}),
|
||||
.not_const => diag.addFmt(.err, span, "array dimension must be a compile-time integer constant", .{}),
|
||||
.non_integral_float => |v| diag.addFmt(.err, span, "array dimension must be an integer, but '{d}' is a non-integral float", .{v}),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user