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;