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:
@@ -1,17 +1,13 @@
|
|||||||
// Variadic heterogeneous type packs — follow-up #4 (runtime
|
// Variadic heterogeneous type packs — Step 2.6: indexing a pack with a
|
||||||
// pack indexing).
|
// RUNTIME index is a compile error.
|
||||||
//
|
//
|
||||||
// `args[<literal>]` substitutes through `pack_arg_nodes` (step
|
// Per locked Decision 1, a pack is comptime-only and has NO runtime
|
||||||
// 2a.B). But `args[<runtime_int>]` — a loop counter, an
|
// representation — so `args[i]` is valid only when `i` is a compile-time
|
||||||
// expression — falls through to the standard slice-indexing
|
// constant (a literal, or an `inline for` cursor). A runtime index (here a
|
||||||
// path, which fails because the pack-mono doesn't materialise
|
// `while`-loop counter) must produce a clear diagnostic, not the confusing
|
||||||
// the `args` slice. Output today: "unresolved 'args'".
|
// "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
|
||||||
// Pairs with follow-up #3: the same `[]Any` slice
|
// `args[i]` is a comptime index.
|
||||||
// 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).
|
|
||||||
|
|
||||||
#import "modules/std.sx";
|
#import "modules/std.sx";
|
||||||
|
|
||||||
@@ -19,9 +15,7 @@ count_anys :: (..$args) -> s64 {
|
|||||||
total : s64 = 0;
|
total : s64 = 0;
|
||||||
i : s64 = 0;
|
i : s64 = 0;
|
||||||
while i < args.len {
|
while i < args.len {
|
||||||
// Runtime index — should resolve through the
|
x : Any = args[i]; // ERROR: runtime index into a comptime-only pack
|
||||||
// materialised slice once #3+#4 lands.
|
|
||||||
x : Any = args[i];
|
|
||||||
_ = x;
|
_ = x;
|
||||||
total = total + 1;
|
total = total + 1;
|
||||||
i = i + 1;
|
i = i + 1;
|
||||||
|
|||||||
@@ -4797,6 +4797,21 @@ pub const Lowering = struct {
|
|||||||
if (self.diagPackIndexOOB(ie)) {
|
if (self.diagPackIndexOOB(ie)) {
|
||||||
return self.builder.constInt(0, .s64);
|
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 obj = self.lowerExpr(ie.object);
|
||||||
const idx = self.lowerExpr(ie.index);
|
const idx = self.lowerExpr(ie.index);
|
||||||
// Infer element type from the object's slice/array type
|
// 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;
|
if (ie.object.data != .identifier) return false;
|
||||||
const pack_name = ie.object.data.identifier.name;
|
const pack_name = ie.object.data.identifier.name;
|
||||||
const n = ppc.get(pack_name) orelse return false;
|
const n = ppc.get(pack_name) orelse return false;
|
||||||
if (ie.index.data != .int_literal) return false;
|
// Any comptime index (int literal or a comptime-constant cursor) that's
|
||||||
const raw: i64 = ie.index.data.int_literal.value;
|
// 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 (raw >= 0 and @as(u32, @intCast(raw)) < n) return false;
|
||||||
if (self.diagnostics) |diags| {
|
if (self.diagnostics) |diags| {
|
||||||
diags.addFmt(.err, ie.index.span, "pack index {} out of bounds: '{s}' has {} element{s}", .{
|
diags.addFmt(.err, ie.index.span, "pack index {} out of bounds: '{s}' has {} element{s}", .{
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
0
|
1
|
||||||
|
|||||||
@@ -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
|
||||||
|
| ^
|
||||||
|
|||||||
Reference in New Issue
Block a user