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