fix(ir): resolve named-const array dims (0083) + materialize literal slice args (0084)

Two silent-miscompile codegen fixes:

0083 — named-const array dimension. `TypeResolver.resolveCompound`'s array
arm resolved the dimension with `if int_literal ... else 0`, so a named const
(`N :: 16; [N]T`) hit the silent `else 0`: the array became 0-length / 0-byte
and element access ran out of bounds (garbage for scalars, bus error for
slice/pointer/struct elements). The arm now delegates the dimension to
`inner.resolveArrayLen` (symmetric with `inner.resolveInner` for the element).
The stateful `Lowering.resolveArrayLen` evaluates it as a compile-time integer
across the comptime-constant / generic-value / module-global const tables and
emits a diagnostic — no fabricated length — when it isn't one.

0084 — `.[...]` literal passed directly as a call arg. `lowerArrayLiteral`
always yields an aggregate array value; the array→slice conversion is the
caller's job. The local-bound var-decl path did it, but the call-arg coercion
path had no array→slice arm, so `classify([N]T, []T)` returned `.none` and the
raw array was passed where a slice was expected (callee read its {ptr,len}
header off the wrong bytes → 0 / garbage / segfault). `classify` now returns a
new `.array_to_slice` plan for same-element `[N]T → []T`, and `coerceToType`
emits the existing `array_to_slice` op — identical to the local-bound path.

Regressions (fail-before/pass-after demonstrated on the pre-fix compiler):
  examples/0140-types-named-const-array-dim.sx (s64 + string + struct elems)
  examples/0141-types-slice-literal-direct-call-arg.sx (string + []s64)

Gate: zig build, zig build test, bash tests/run_examples.sh (387 passed).
Issues 0083 and 0084 marked RESOLVED.
This commit is contained in:
agra
2026-06-04 08:22:45 +03:00
parent 3b36264e65
commit 12552e125d
15 changed files with 251 additions and 1 deletions

View File

@@ -42,6 +42,7 @@ pub const CoercionResolver = struct {
ptr_int_bitcast, // ptr ↔ int
widen, // same kind, dst wider
narrow, // same kind, dst narrower
array_to_slice, // [N]T → []T (materialize backing storage + header)
none, // nothing applies — pass the value through
};
@@ -65,6 +66,20 @@ pub const CoercionResolver = struct {
}
}
// Fixed array → slice of the same element: an aggregate array value
// (e.g. a `.[...]` literal passed directly as a call arg) needs to be
// materialized into addressable storage and wrapped in a {ptr,len}
// header. Without this the array value is passed where a slice is
// expected — the callee reads the header off the wrong bytes (issue
// 0084). The local-bound path already does this conversion on its own.
if (!src_ty.isBuiltin() and !dst_ty.isBuiltin()) {
const si = self.l.module.types.get(src_ty);
const di = self.l.module.types.get(dst_ty);
if (si == .array and di == .slice and si.array.element == di.slice.element) {
return .array_to_slice;
}
}
// Optional → Concrete unwrap (narrowing).
if (!src_ty.isBuiltin()) {
const src_info = self.l.module.types.get(src_ty);

View File

@@ -11612,6 +11612,55 @@ pub const Lowering = struct {
return self.resolveTypeWithBindings(node);
}
/// Fixed-array dimension hook for `TypeResolver.resolveCompound`. A literal
/// `[16]T` and a named-const `N :: 16; [N]T` must resolve to the SAME length:
/// the dimension is a compile-time integer, looked up in the comptime / value
/// / module-const tables the stateful lowering owns. A dimension that isn't a
/// compile-time integer is a hard error — emitting a diagnostic (rather than
/// fabricating a 0 length, which gives a 0-byte array and out-of-bounds
/// element access, issue 0083).
pub fn resolveArrayLen(self: *Lowering, len_node: *const Node) u32 {
if (self.comptimeArrayDim(len_node)) |n| {
if (n < 0) {
if (self.diagnostics) |d|
d.addFmt(.err, len_node.span, "array dimension must be non-negative, got {}", .{n});
return 0;
}
return @intCast(n);
}
if (self.diagnostics) |d|
d.addFmt(.err, len_node.span, "array dimension must be a compile-time integer constant", .{});
return 0;
}
/// Evaluate a fixed-array dimension to a compile-time integer: a literal, or
/// a name bound to an integer in the comptime-constant (`OS`/loop cursors),
/// generic-value (`$N`), or module-global const (`N :: 16`) tables. Returns
/// null when the dimension isn't a compile-time integer.
fn comptimeArrayDim(self: *Lowering, node: *const Node) ?i64 {
return switch (node.data) {
.int_literal => |lit| lit.value,
.identifier => |id| self.comptimeIntNamed(id.name),
.type_expr => |te| self.comptimeIntNamed(te.name),
else => null,
};
}
/// Resolve a name to a compile-time integer across the three const tables.
fn comptimeIntNamed(self: *Lowering, name: []const u8) ?i64 {
if (self.comptime_constants.get(name)) |cv| switch (cv) {
.int_val => |iv| return iv,
else => {},
};
if (self.comptime_value_bindings) |cvb| {
if (cvb.get(name)) |v| return v;
}
if (self.program_index.module_const_map.get(name)) |ci| {
if (ci.value.data == .int_literal) return ci.value.data.int_literal.value;
}
return null;
}
/// Resolve a type node, checking type_bindings first for generic type params.
pub fn resolveTypeWithBindings(self: *Lowering, node: *const Node) TypeId {
// Pack-index in a type position: `$<pack>[<lit>]` resolves to the
@@ -13984,6 +14033,7 @@ pub const Lowering = struct {
.ptr_int_bitcast => return self.builder.emit(.{ .bitcast = .{ .operand = val, .from = src_ty, .to = dst_ty } }, dst_ty),
.narrow => return self.builder.emit(.{ .narrow = .{ .operand = val, .from = src_ty, .to = dst_ty } }, dst_ty),
.widen => return self.builder.emit(.{ .widen = .{ .operand = val, .from = src_ty, .to = dst_ty } }, dst_ty),
.array_to_slice => return self.builder.emit(.{ .array_to_slice = .{ .operand = val } }, dst_ty),
}
}

View File

@@ -28,6 +28,18 @@ const StatelessInner = struct {
pub fn resolveInner(self: StatelessInner, node: *const Node) TypeId {
return resolveAstType(node, self.table, self.alias_map);
}
/// Fixed-array dimension at registration time (no bindings / const tables).
/// Only a literal dimension is knowable here; a named-const dimension
/// (`N :: 16; [N]T`) is resolved by the stateful caller
/// (`Lowering.resolveArrayLen`) before it ever reaches this binding-free
/// path — mirroring how `pack_index_type_expr` is handled stateful-first.
pub fn resolveArrayLen(self: StatelessInner, len_node: *const Node) u32 {
_ = self;
return switch (len_node.data) {
.int_literal => |lit| @intCast(lit.value),
else => 0,
};
}
};
// ── AST Node → TypeId ───────────────────────────────────────────────────

View File

@@ -23,6 +23,12 @@ const PrimInner = struct {
else => .unresolved,
};
}
pub fn resolveArrayLen(_: PrimInner, len_node: *const Node) u32 {
return switch (len_node.data) {
.int_literal => |lit| @intCast(lit.value),
else => 0,
};
}
};
test "TypeResolver.resolvePrimitive maps builtin keywords, null otherwise" {

View File

@@ -93,7 +93,12 @@ pub const TypeResolver = struct {
.optional_type_expr => |ot| table.optionalOf(inner.resolveInner(ot.inner_type)),
.array_type_expr => |at| blk: {
const elem = inner.resolveInner(at.element_type);
const len: u32 = if (at.length.data == .int_literal) @intCast(at.length.data.int_literal.value) else 0;
// The dimension is delegated to `inner` exactly like the element
// type: a literal `[16]T` and a named-const `N :: 16; [N]T` must
// produce the same length. The stateful resolver consults the
// const tables; the binding-free one handles literal dims (issue
// 0083 — a 0 here gives a 0-byte array and OOB element access).
const len = inner.resolveArrayLen(at.length);
break :blk table.arrayOf(elem, len);
},
.function_type_expr => |ft| blk: {