From 82bdcd634ad32bbb7d04c98b5f85fb5275ee5399 Mon Sep 17 00:00:00 2001 From: agra Date: Sat, 30 May 2026 01:30:11 +0300 Subject: [PATCH] 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

must be indexed by a compile-time constant ...") for a runtime index, instead of the confusing "unresolved

" 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. --- examples/163-pack-runtime-index.sx | 26 +++++++++------------- src/ir/lower.zig | 21 +++++++++++++++-- tests/expected/163-pack-runtime-index.exit | 2 +- tests/expected/163-pack-runtime-index.txt | 6 ++++- 4 files changed, 35 insertions(+), 20 deletions(-) diff --git a/examples/163-pack-runtime-index.sx b/examples/163-pack-runtime-index.sx index 939bdca..d68ec05 100644 --- a/examples/163-pack-runtime-index.sx +++ b/examples/163-pack-runtime-index.sx @@ -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[]` substitutes through `pack_arg_nodes` (step -// 2a.B). But `args[]` — 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; diff --git a/src/ir/lower.zig b/src/ir/lower.zig index 5aa5534..1ac0430 100644 --- a/src/ir/lower.zig +++ b/src/ir/lower.zig @@ -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 + // ''" 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}", .{ diff --git a/tests/expected/163-pack-runtime-index.exit b/tests/expected/163-pack-runtime-index.exit index 573541a..d00491f 100644 --- a/tests/expected/163-pack-runtime-index.exit +++ b/tests/expected/163-pack-runtime-index.exit @@ -1 +1 @@ -0 +1 diff --git a/tests/expected/163-pack-runtime-index.txt b/tests/expected/163-pack-runtime-index.txt index b8626c4..9d541d0 100644 --- a/tests/expected/163-pack-runtime-index.txt +++ b/tests/expected/163-pack-runtime-index.txt @@ -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 + | ^