diff --git a/examples/1131-diagnostics-array-dim-oversized-u32-alias.sx b/examples/1131-diagnostics-array-dim-oversized-u32-alias.sx new file mode 100644 index 0000000..db32bc0 --- /dev/null +++ b/examples/1131-diagnostics-array-dim-oversized-u32-alias.sx @@ -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]); +} diff --git a/examples/expected/1131-diagnostics-array-dim-oversized-u32-alias.exit b/examples/expected/1131-diagnostics-array-dim-oversized-u32-alias.exit new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/examples/expected/1131-diagnostics-array-dim-oversized-u32-alias.exit @@ -0,0 +1 @@ +1 diff --git a/examples/expected/1131-diagnostics-array-dim-oversized-u32-alias.stderr b/examples/expected/1131-diagnostics-array-dim-oversized-u32-alias.stderr new file mode 100644 index 0000000..7de2858 --- /dev/null +++ b/examples/expected/1131-diagnostics-array-dim-oversized-u32-alias.stderr @@ -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; + | ^^^^^^^^^^ diff --git a/examples/expected/1131-diagnostics-array-dim-oversized-u32-alias.stdout b/examples/expected/1131-diagnostics-array-dim-oversized-u32-alias.stdout new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/examples/expected/1131-diagnostics-array-dim-oversized-u32-alias.stdout @@ -0,0 +1 @@ + diff --git a/issues/0083-named-const-array-dimension-miscompiled.md b/issues/0083-named-const-array-dimension-miscompiled.md index fe789a4..61a07f7 100644 --- a/issues/0083-named-const-array-dimension-miscompiled.md +++ b/issues/0083-named-const-array-dimension-miscompiled.md @@ -122,6 +122,27 @@ > `program_index.foldDimU32` gate. Files: `src/ir/semantic_diagnostics.zig`, > `src/ir/lower.zig`, `src/ir/program_index.zig`, `src/ir/type_bridge.zig`. > 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 A fixed array whose dimension is a module-global integer constant (`N :: 16; diff --git a/src/ir/lower.zig b/src/ir/lower.zig index 1d1995a..0e04c8c 100644 --- a/src/ir/lower.zig +++ b/src/ir/lower.zig @@ -712,10 +712,26 @@ pub const Lowering = struct { // dimension is not a compile-time integer constant. Surface // it as a clean diagnostic so the build aborts here rather // 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 (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}); + if (self.diagnostics) |d| { + 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 {}; } else if (cd.value.data == .identifier) { @@ -11651,24 +11667,17 @@ pub const Lowering = struct { /// diagnostic surfaces). The diagnostic — not the returned length — is what /// guarantees no garbage ships (issue 0083). pub fn resolveArrayLen(self: *Lowering, len_node: *const Node) ?u32 { - switch (program_index_mod.foldDimU32(len_node, self, 0)) { - .ok => |n| return n, - .below_min => |v| { - if (self.diagnostics) |d| - d.addFmt(.err, len_node.span, "array dimension must be non-negative, got {}", .{v}); - return 0; - }, - .too_large => |v| { - if (self.diagnostics) |d| - d.addFmt(.err, len_node.span, "array dimension {} does not fit in u32", .{v}); - 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; - }, - } + const result = program_index_mod.foldDimU32(len_node, self, 0); + if (result == .ok) return result.ok; + // A non-const / oversized / negative dim is a hard error. Emit the + // shared diagnostic (single wording source — `program_index.reportDimError`, + // also used by the stateless alias path so the two cannot diverge), then + // return a harmless `0` so body lowering finishes without touching the + // `.unresolved` sentinel (which would `@panic` in `sizeOf` mid-lowering, + // before the diagnostic surfaces). The diagnostic — not the returned + // length — guarantees no garbage ships (issue 0083). + if (self.diagnostics) |d| program_index_mod.reportDimError(d, len_node.span, result); + return 0; } /// Leaf-name lookup for the shared dimension evaluator: a name bound to a diff --git a/src/ir/program_index.zig b/src/ir/program_index.zig index 3f063b5..91861b4 100644 --- a/src/ir/program_index.zig +++ b/src/ir/program_index.zig @@ -2,6 +2,7 @@ const std = @import("std"); const ast = @import("../ast.zig"); const types = @import("types.zig"); const inst = @import("inst.zig"); +const errors = @import("../errors.zig"); const Node = ast.Node; const TypeId = types.TypeId; @@ -147,6 +148,24 @@ pub fn foldDimU32(node: *const Node, ctx: anytype, min: u32) DimU32 { 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 }; /// Single lowering access point for declaration-name / import / visibility diff --git a/src/ir/type_bridge.zig b/src/ir/type_bridge.zig index 711d351..eef4c67 100644 --- a/src/ir/type_bridge.zig +++ b/src/ir/type_bridge.zig @@ -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 ─────────────────────────────────────────────────── // Resolve an AST type node into an IR TypeId. Used during lowering when // we only have the parsed AST (no codegen type registry).