lang F1 2.6: pack-index edge cases (runtime-index error, comptime OOB)

Per locked Decision 1 a pack is comptime-only with no runtime value, so xs[i]
is valid only for a comptime index. lowerIndexExpr now emits a clear error
("pack <p> must be indexed by a compile-time constant ...") for a runtime
index, instead of the confusing "unresolved <p>" the slice-index fall-through
produced. diagPackIndexOOB switched from int-literal-only to comptimeIndexOf so
an inline-for cursor that goes out of bounds is also caught.

Repurposed examples/163-pack-runtime-index.sx (was aspirational: expected
runtime indexing to materialise a []Any slice and print 4, contradicting
Decision 1) into the runtime-index error test. Comptime + OOB cases already
covered by examples/199/200/161. 236 examples + unit green.
This commit is contained in:
agra
2026-05-30 01:30:11 +03:00
parent 9e38bb924a
commit 82bdcd634a
4 changed files with 35 additions and 20 deletions

View File

@@ -1,17 +1,13 @@
// Variadic heterogeneous type packs — follow-up #4 (runtime
// pack indexing).
// Variadic heterogeneous type packs — Step 2.6: indexing a pack with a
// RUNTIME index is a compile error.
//
// `args[<literal>]` substitutes through `pack_arg_nodes` (step
// 2a.B). But `args[<runtime_int>]` — a loop counter, an
// expression — falls through to the standard slice-indexing
// path, which fails because the pack-mono doesn't materialise
// the `args` slice. Output today: "unresolved 'args'".
//
// Pairs with follow-up #3: the same `[]Any` slice
// materialisation handles both `args` bare and `args[i]`
// runtime. Element type becomes `Any` (lossy on per-position
// types — that's the inherent trade-off of runtime indexing
// into a heterogeneous pack).
// Per locked Decision 1, a pack is comptime-only and has NO runtime
// representation — so `args[i]` is valid only when `i` is a compile-time
// constant (a literal, or an `inline for` cursor). A runtime index (here a
// `while`-loop counter) must produce a clear diagnostic, not the confusing
// "unresolved 'args'" the slice-index fall-through used to give. To walk a
// pack, use `inline for 0..args.len (i) { ... }`, which unrolls so each
// `args[i]` is a comptime index.
#import "modules/std.sx";
@@ -19,9 +15,7 @@ count_anys :: (..$args) -> s64 {
total : s64 = 0;
i : s64 = 0;
while i < args.len {
// Runtime index — should resolve through the
// materialised slice once #3+#4 lands.
x : Any = args[i];
x : Any = args[i]; // ERROR: runtime index into a comptime-only pack
_ = x;
total = total + 1;
i = i + 1;

View File

@@ -4797,6 +4797,21 @@ pub const Lowering = struct {
if (self.diagPackIndexOOB(ie)) {
return self.builder.constInt(0, .s64);
}
// Runtime index into a comptime-only pack (Decision 1): a pack has no
// runtime representation, so the index must be a compile-time constant.
// A runtime index is a hard error — clearer than the "unresolved
// '<pack>'" the slice-index fall-through would otherwise produce.
if (self.pack_param_count) |ppc| {
if (ie.object.data == .identifier) {
const pname = ie.object.data.identifier.name;
if (ppc.contains(pname) and self.comptimeIndexOf(ie.index) == null) {
if (self.diagnostics) |diags| {
diags.addFmt(.err, ie.index.span, "pack '{s}' must be indexed by a compile-time constant — a pack is comptime-only and has no runtime value", .{pname});
}
return self.builder.constInt(0, .s64);
}
}
}
const obj = self.lowerExpr(ie.object);
const idx = self.lowerExpr(ie.index);
// Infer element type from the object's slice/array type
@@ -4815,8 +4830,10 @@ pub const Lowering = struct {
if (ie.object.data != .identifier) return false;
const pack_name = ie.object.data.identifier.name;
const n = ppc.get(pack_name) orelse return false;
if (ie.index.data != .int_literal) return false;
const raw: i64 = ie.index.data.int_literal.value;
// Any comptime index (int literal or a comptime-constant cursor) that's
// out of range — runtime indices are handled by the caller's
// must-be-comptime check.
const raw: i64 = self.comptimeIndexOf(ie.index) orelse return false;
if (raw >= 0 and @as(u32, @intCast(raw)) < n) return false;
if (self.diagnostics) |diags| {
diags.addFmt(.err, ie.index.span, "pack index {} out of bounds: '{s}' has {} element{s}", .{

View File

@@ -1 +1 @@
0
1

View File

@@ -1 +1,5 @@
4
error: pack 'args' must be indexed by a compile-time constant — a pack is comptime-only and has no runtime value
--> /Users/agra/projects/sx/examples/163-pack-runtime-index.sx:18:24
|
18 | x : Any = args[i]; // ERROR: runtime index into a comptime-only pack
| ^