fix(ir): precise oversized-dim diagnostic on the alias path (0083)

The stateless alias-registration array-dim path collapsed foldDimU32's
distinct .too_large / .below_min outcomes into null, so an oversized type
alias (Big :: [5000000000]s64) emitted the FALSE 'an array dimension is not
a compile-time integer constant' message while the direct form correctly
reported 'array dimension 5000000000 does not fit in u32'.

Add program_index.reportDimError as the single source of dim-error wording
(the stateful path now emits through it too) and type_bridge.foldArrayDim to
surface the DimU32 reason at the alias-registration site. An oversized/negative
alias dim now routes to reportDimError for the same precise message as the
direct form; a genuinely non-const alias dim keeps the alias-specific message.

Regression: examples/1131-diagnostics-array-dim-oversized-u32-alias.sx
This commit is contained in:
agra
2026-06-04 12:31:24 +03:00
parent efc09699e8
commit e8cc9d03de
8 changed files with 114 additions and 21 deletions

View File

@@ -0,0 +1,24 @@
// An array dimension that folds to a valid compile-time integer but exceeds a
// `u32` is a hard error — and it must report the SAME precise diagnostic whether
// the array is written directly (`a : [5_000_000_000]s64`, see example 1130) or
// behind a type ALIAS (`Big :: [5_000_000_000]s64`, here). Both forms now route
// the dimension through one shared folder + one shared message map, so they
// cannot diverge.
//
// Regression (issue 0083 / F0.4 attempt 7): the stateless alias-registration
// path collapsed `foldDimU32`'s distinct `.too_large` outcome into `null` and
// emitted ONE generic "an array dimension is not a compile-time integer
// constant" message — FALSE, since 5_000_000_000 IS a compile-time integer
// constant; it merely doesn't fit a `u32`. The alias path now consults the
// shared fold and emits the precise "does not fit in u32" message, matching the
// direct form. (A genuinely non-const alias dim still gets the generic message —
// see example 1129.)
#import "modules/std.sx";
Big :: [5000000000]s64;
main :: () {
a : Big = ---;
a[0] = 7;
print("unreachable: {}\n", a[0]);
}

View File

@@ -0,0 +1,5 @@
error: array dimension 5000000000 does not fit in u32
--> examples/1131-diagnostics-array-dim-oversized-u32-alias.sx:18:9
|
18 | Big :: [5000000000]s64;
| ^^^^^^^^^^

View File

@@ -122,6 +122,27 @@
> `program_index.foldDimU32` gate. Files: `src/ir/semantic_diagnostics.zig`, > `program_index.foldDimU32` gate. Files: `src/ir/semantic_diagnostics.zig`,
> `src/ir/lower.zig`, `src/ir/program_index.zig`, `src/ir/type_bridge.zig`. > `src/ir/lower.zig`, `src/ir/program_index.zig`, `src/ir/type_bridge.zig`.
> Regression: `examples/0208-generics-value-param-type-function.sx`. > Regression: `examples/0208-generics-value-param-type-function.sx`.
>
> **Diagnostic-accuracy parity (attempt 7).** The fold + layout were correct, but
> the two paths still DIVERGED on the error MESSAGE for an oversized dim. The
> direct form (`a : [5_000_000_000]s64`) reported the accurate "array dimension
> 5000000000 does not fit in u32" (from the stateful `resolveArrayLen`, which
> branches on `foldDimU32`'s `.too_large` / `.below_min` / `.not_const` variants),
> but the type-ALIAS form (`Big :: [5_000_000_000]s64`) reported a FALSE "an array
> dimension is not a compile-time integer constant" — because the stateless
> `resolveArrayLen` collapsed every non-`.ok` `DimU32` to `null`, so the
> alias-registration site had only one generic message to emit. Fix: a single
> wording source `program_index.reportDimError(diag, span, DimU32)` now owns the
> dim-error text; the stateful path emits through it, and the alias-registration
> site re-folds a top-level array dim via the new `type_bridge.foldArrayDim`
> (same shared `foldDimU32`) and routes a `.too_large` / `.below_min` result to
> `reportDimError` — so an oversized alias dim now reports the SAME precise
> message as the direct form. A genuinely non-const alias dim (`[get()]`) still
> gets the alias-specific "not a compile-time integer constant" message (1129).
> Files: `src/ir/program_index.zig`, `src/ir/type_bridge.zig`, `src/ir/lower.zig`.
> Regression: `examples/1131-diagnostics-array-dim-oversized-u32-alias.sx`
> (oversized dim via alias → "does not fit in u32", matching direct example 1130;
> 1129 still proves the non-const path keeps the generic message).
## Symptom ## Symptom
A fixed array whose dimension is a module-global integer constant (`N :: 16; A fixed array whose dimension is a module-global integer constant (`N :: 16;

View File

@@ -712,10 +712,26 @@ pub const Lowering = struct {
// dimension is not a compile-time integer constant. Surface // dimension is not a compile-time integer constant. Surface
// it as a clean diagnostic so the build aborts here rather // it as a clean diagnostic so the build aborts here rather
// than letting `.unresolved` reach codegen and `@panic` in // than letting `.unresolved` reach codegen and `@panic` in
// sizeOf (issue 0083 — no fabricated 0-length array). // sizeOf (issue 0083 — no fabricated 0-length array). For a
// top-level array alias, re-fold the dimension so an
// oversized / negative constant emits the SAME precise
// message as the direct form (`a : [N]T`) via the shared
// `program_index.reportDimError` — only a genuinely
// non-const dim gets the generic alias message.
if (target_ty == .unresolved) { if (target_ty == .unresolved) {
if (self.diagnostics) |d| if (self.diagnostics) |d| {
d.addFmt(.err, cd.value.span, "type alias '{s}' could not be resolved: an array dimension is not a compile-time integer constant", .{cd.name}); const precise: ?program_index_mod.DimU32 = if (cd.value.data == .array_type_expr) blk: {
const dim = type_bridge.foldArrayDim(cd.value.data.array_type_expr.length, &self.module.types, &self.program_index.type_alias_map, &self.program_index.module_const_map);
break :blk switch (dim) {
.too_large, .below_min => dim,
else => null,
};
} else null;
if (precise) |dim|
program_index_mod.reportDimError(d, cd.value.data.array_type_expr.length.span, dim)
else
d.addFmt(.err, cd.value.span, "type alias '{s}' could not be resolved: an array dimension is not a compile-time integer constant", .{cd.name});
}
} }
self.program_index.type_alias_map.put(cd.name, target_ty) catch {}; self.program_index.type_alias_map.put(cd.name, target_ty) catch {};
} else if (cd.value.data == .identifier) { } else if (cd.value.data == .identifier) {
@@ -11651,24 +11667,17 @@ pub const Lowering = struct {
/// diagnostic surfaces). The diagnostic — not the returned length — is what /// diagnostic surfaces). The diagnostic — not the returned length — is what
/// guarantees no garbage ships (issue 0083). /// guarantees no garbage ships (issue 0083).
pub fn resolveArrayLen(self: *Lowering, len_node: *const Node) ?u32 { pub fn resolveArrayLen(self: *Lowering, len_node: *const Node) ?u32 {
switch (program_index_mod.foldDimU32(len_node, self, 0)) { const result = program_index_mod.foldDimU32(len_node, self, 0);
.ok => |n| return n, if (result == .ok) return result.ok;
.below_min => |v| { // A non-const / oversized / negative dim is a hard error. Emit the
if (self.diagnostics) |d| // shared diagnostic (single wording source — `program_index.reportDimError`,
d.addFmt(.err, len_node.span, "array dimension must be non-negative, got {}", .{v}); // also used by the stateless alias path so the two cannot diverge), then
return 0; // return a harmless `0` so body lowering finishes without touching the
}, // `.unresolved` sentinel (which would `@panic` in `sizeOf` mid-lowering,
.too_large => |v| { // before the diagnostic surfaces). The diagnostic — not the returned
if (self.diagnostics) |d| // length — guarantees no garbage ships (issue 0083).
d.addFmt(.err, len_node.span, "array dimension {} does not fit in u32", .{v}); if (self.diagnostics) |d| program_index_mod.reportDimError(d, len_node.span, result);
return 0; return 0;
},
.not_const => {
if (self.diagnostics) |d|
d.addFmt(.err, len_node.span, "array dimension must be a compile-time integer constant", .{});
return 0;
},
}
} }
/// Leaf-name lookup for the shared dimension evaluator: a name bound to a /// Leaf-name lookup for the shared dimension evaluator: a name bound to a

View File

@@ -2,6 +2,7 @@ const std = @import("std");
const ast = @import("../ast.zig"); const ast = @import("../ast.zig");
const types = @import("types.zig"); const types = @import("types.zig");
const inst = @import("inst.zig"); const inst = @import("inst.zig");
const errors = @import("../errors.zig");
const Node = ast.Node; const Node = ast.Node;
const TypeId = types.TypeId; const TypeId = types.TypeId;
@@ -147,6 +148,24 @@ pub fn foldDimU32(node: *const Node, ctx: anytype, min: u32) DimU32 {
return .{ .ok = @intCast(v) }; return .{ .ok = @intCast(v) };
} }
/// THE single source of array-dimension diagnostic wording. Both array-dim
/// resolvers — the stateful body-lowering path (`Lowering.resolveArrayLen`) and
/// the stateless registration-time path (the alias-registration site, via
/// `type_bridge.foldArrayDim`) — emit through here, so an oversized / negative /
/// non-const dimension reports the SAME message regardless of whether it was
/// written directly (`a : [N]T`) or via a type alias (`Arr :: [N]T`). Folding
/// the wording into one place is the diagnostic-accuracy half of the issue-0083
/// unify-or-diverge story: `foldDimU32` is the single fold, this is the single
/// message map. Only call with a non-`.ok` result (the `.ok` arm is a no-op).
pub fn reportDimError(diag: *errors.DiagnosticList, span: ?ast.Span, result: DimU32) void {
switch (result) {
.ok => {},
.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", .{}),
}
}
pub const GlobalInfo = struct { id: inst.GlobalId, ty: TypeId }; pub const GlobalInfo = struct { id: inst.GlobalId, ty: TypeId };
/// Single lowering access point for declaration-name / import / visibility /// Single lowering access point for declaration-name / import / visibility

View File

@@ -81,6 +81,19 @@ const StatelessInner = struct {
} }
}; };
/// Fold a registration-time array dimension to its `DimU32` outcome through the
/// SAME shared `program_index.foldDimU32` that `StatelessInner.resolveArrayLen`
/// uses — but surface the reason instead of collapsing it to `null`. The
/// alias-registration site calls this so an unresolved `Arr :: [N]T` alias can
/// emit the PRECISE dim diagnostic (oversized `[5_000_000_000]` / negative /
/// non-const) that matches the stateful direct form, rather than one generic
/// "not a compile-time integer constant" message for every failure (issue 0083 —
/// the stateful/stateless diagnostic divergence).
pub fn foldArrayDim(len_node: *const Node, table: *TypeTable, alias_map: AliasMap, consts: ConstMap) program_index_mod.DimU32 {
const si = StatelessInner{ .table = table, .alias_map = alias_map, .consts = consts };
return program_index_mod.foldDimU32(len_node, si, 0);
}
// ── AST Node → TypeId ─────────────────────────────────────────────────── // ── AST Node → TypeId ───────────────────────────────────────────────────
// Resolve an AST type node into an IR TypeId. Used during lowering when // Resolve an AST type node into an IR TypeId. Used during lowering when
// we only have the parsed AST (no codegen type registry). // we only have the parsed AST (no codegen type registry).