A constant-FOLDABLE expression array dimension (`[M + 1]`, `[M * N]`, `[N - M]`, nested `[M + N - 1]`, parenthesised `[(M + 1) * 2]`, mixing untyped and typed module consts) was wrongly rejected as "not a compile-time integer constant" even though every operand is compile-time-known. Attempts 1-3 resolved only a bare named-const dim or a literal; an expression dim must be EVALUATED, not rejected. Fix: the shared dim resolver now routes the dimension through a single constant integer-expression evaluator (`program_index.evalConstIntExpr`) that folds integer `+ - * / %` and unary negate over literals and named/typed module consts, recursively (parentheses carry no AST node). The leaf-name lookup is delegated via `ctx.lookupDimName`, so the stateful body-lowering path (`Lowering`, which also sees comptime constants and generic `$N` values) and the stateless registration path (`type_bridge.StatelessInner`, module consts only) share the EXACT SAME folding logic and cannot diverge — an expression dim via a type alias resolves identically to the direct form. No-fabrication discipline unchanged: a genuinely non-comptime dimension (runtime local, non-comptime call, unbound name) or arithmetic that overflows / divides by zero still yields null -> `.unresolved` -> the same clean compile-halting diagnostic, never a fabricated length. - examples/0144-types-const-expr-array-dim.sx: every expression form, direct vs alias, scalar / string / struct element types (fails on the pre-fix compiler, passes after). - examples/1129 re-pointed at a genuinely non-const dimension (`[get()]s64`, a runtime call) so it still proves the stateless clean-halt (a foldable expression is no longer an error). - program_index.test.zig: unit test for evalConstIntExpr folding and clean-halt-on-non-const.
173 lines
7.2 KiB
Zig
173 lines
7.2 KiB
Zig
const std = @import("std");
|
|
const pi = @import("program_index.zig");
|
|
const ProgramIndex = pi.ProgramIndex;
|
|
const ast = @import("../ast.zig");
|
|
const types = @import("types.zig");
|
|
const inst = @import("inst.zig");
|
|
|
|
test "ProgramIndex.init starts empty with unset borrowed views" {
|
|
var idx = ProgramIndex.init(std.testing.allocator);
|
|
defer idx.deinit();
|
|
try std.testing.expectEqual(@as(u32, 0), idx.import_flags.count());
|
|
try std.testing.expect(idx.module_scopes == null);
|
|
try std.testing.expect(idx.import_graph == null);
|
|
}
|
|
|
|
test "ProgramIndex.import_flags round-trips imported vs local" {
|
|
var idx = ProgramIndex.init(std.testing.allocator);
|
|
defer idx.deinit();
|
|
try idx.import_flags.put("printf", true);
|
|
try idx.import_flags.put("main", false);
|
|
try std.testing.expectEqual(@as(?bool, true), idx.import_flags.get("printf"));
|
|
try std.testing.expectEqual(@as(?bool, false), idx.import_flags.get("main"));
|
|
try std.testing.expect(idx.import_flags.get("absent") == null);
|
|
}
|
|
|
|
test "ProgramIndex borrows module_scopes / import_graph without owning them" {
|
|
const ScopeSet = std.StringHashMap(std.StringHashMap(void));
|
|
var scopes = ScopeSet.init(std.testing.allocator);
|
|
defer scopes.deinit();
|
|
var graph = ScopeSet.init(std.testing.allocator);
|
|
defer graph.deinit();
|
|
|
|
var idx = ProgramIndex.init(std.testing.allocator);
|
|
defer idx.deinit();
|
|
idx.module_scopes = &scopes;
|
|
idx.import_graph = &graph;
|
|
|
|
// Reads go through the borrowed pointer; the backing stays caller-owned,
|
|
// so idx.deinit() must not free it (testing.allocator would flag a
|
|
// double-free / leak otherwise).
|
|
try std.testing.expect(idx.module_scopes.? == &scopes);
|
|
try std.testing.expect(idx.import_graph.? == &graph);
|
|
try std.testing.expectEqual(@as(u32, 0), idx.module_scopes.?.count());
|
|
}
|
|
|
|
test "ProgramIndex declaration maps round-trip (A1.1b)" {
|
|
var idx = ProgramIndex.init(std.testing.allocator);
|
|
defer idx.deinit();
|
|
|
|
// Minimal AST node reused wherever a *Node is required.
|
|
var blk = ast.Node{ .span = .{ .start = 0, .end = 0 }, .data = .{ .block = .{ .stmts = &.{} } } };
|
|
|
|
// fn_ast_map: function name → AST decl.
|
|
const fd = ast.FnDecl{ .name = "main", .params = &.{}, .return_type = null, .body = &blk };
|
|
try idx.fn_ast_map.put("main", &fd);
|
|
try std.testing.expect(idx.fn_ast_map.get("main").? == &fd);
|
|
|
|
// type_alias_map: alias name → target TypeId.
|
|
try idx.type_alias_map.put("ShaderHandle", .s64);
|
|
try std.testing.expectEqual(@as(?types.TypeId, .s64), idx.type_alias_map.get("ShaderHandle"));
|
|
|
|
// global_names: #run global name → GlobalInfo.
|
|
try idx.global_names.put("g", .{ .id = inst.GlobalId.fromIndex(0), .ty = .s64 });
|
|
try std.testing.expect(idx.global_names.get("g").?.id == inst.GlobalId.fromIndex(0));
|
|
|
|
// module_const_map: const name → ModuleConstInfo.
|
|
try idx.module_const_map.put("AF_INET", .{ .value = &blk, .ty = .s32 });
|
|
try std.testing.expect(idx.module_const_map.get("AF_INET").?.value == &blk);
|
|
|
|
// foreign_class_map: sx alias → ForeignClassDecl.
|
|
const fcd = ast.ForeignClassDecl{
|
|
.name = "NSString",
|
|
.foreign_path = "NSString",
|
|
.runtime = .objc_class,
|
|
.members = &.{},
|
|
.is_foreign = true,
|
|
.is_main = false,
|
|
};
|
|
try idx.foreign_class_map.put("NSString", &fcd);
|
|
try std.testing.expect(idx.foreign_class_map.get("NSString").? == &fcd);
|
|
|
|
// protocol_decl_map: protocol name → ProtocolDeclInfo.
|
|
try idx.protocol_decl_map.put("Show", .{ .name = "Show", .is_inline = false, .methods = &.{} });
|
|
try std.testing.expectEqualStrings("Show", idx.protocol_decl_map.get("Show").?.name);
|
|
|
|
// protocol_ast_map: protocol name → AST decl.
|
|
const pd = ast.ProtocolDecl{ .name = "Show", .methods = &.{} };
|
|
try idx.protocol_ast_map.put("Show", &pd);
|
|
try std.testing.expect(idx.protocol_ast_map.get("Show").? == &pd);
|
|
|
|
// struct_template_map: generic struct name → template.
|
|
try idx.struct_template_map.put("List", .{ .name = "List", .type_params = &.{}, .field_names = &.{}, .field_type_nodes = &.{} });
|
|
try std.testing.expectEqualStrings("List", idx.struct_template_map.get("List").?.name);
|
|
|
|
// ufcs_alias_map: alias name → target function name.
|
|
try idx.ufcs_alias_map.put("len", "list_len");
|
|
try std.testing.expectEqualStrings("list_len", idx.ufcs_alias_map.get("len").?);
|
|
}
|
|
|
|
/// Stand-in for the leaf-name lookup both array-dimension resolvers pass to the
|
|
/// shared `evalConstIntExpr`: `M`/`N` resolve to integers, everything else is
|
|
/// genuinely non-comptime.
|
|
const DimCtx = struct {
|
|
pub fn lookupDimName(_: DimCtx, name: []const u8) ?i64 {
|
|
if (std.mem.eql(u8, name, "M")) return 4;
|
|
if (std.mem.eql(u8, name, "N")) return 6;
|
|
return null;
|
|
}
|
|
};
|
|
|
|
fn nLit(v: i64) ast.Node {
|
|
return .{ .span = .{ .start = 0, .end = 0 }, .data = .{ .int_literal = .{ .value = v } } };
|
|
}
|
|
fn nIdent(name: []const u8) ast.Node {
|
|
return .{ .span = .{ .start = 0, .end = 0 }, .data = .{ .identifier = .{ .name = name } } };
|
|
}
|
|
fn nBin(op: ast.BinaryOp.Op, l: *ast.Node, r: *ast.Node) ast.Node {
|
|
return .{ .span = .{ .start = 0, .end = 0 }, .data = .{ .binary_op = .{ .op = op, .lhs = l, .rhs = r } } };
|
|
}
|
|
fn nNeg(operand: *ast.Node) ast.Node {
|
|
return .{ .span = .{ .start = 0, .end = 0 }, .data = .{ .unary_op = .{ .op = .negate, .operand = operand } } };
|
|
}
|
|
|
|
test "evalConstIntExpr folds constant-expression array dimensions, halts on non-const" {
|
|
const eval = pi.evalConstIntExpr;
|
|
const ctx = DimCtx{};
|
|
|
|
var l5 = nLit(5);
|
|
var one = nLit(1);
|
|
var two = nLit(2);
|
|
var zero = nLit(0);
|
|
var m = nIdent("M");
|
|
var n = nIdent("N");
|
|
var z = nIdent("Z"); // unbound — genuinely non-comptime
|
|
|
|
// Leaves: literal, named const, unbound name.
|
|
try std.testing.expectEqual(@as(?i64, 5), eval(&l5, ctx));
|
|
try std.testing.expectEqual(@as(?i64, 4), eval(&m, ctx));
|
|
try std.testing.expect(eval(&z, ctx) == null);
|
|
|
|
// `M + 1`, `M * N`, `N - M`.
|
|
var add = nBin(.add, &m, &one);
|
|
var mul = nBin(.mul, &m, &n);
|
|
var sub = nBin(.sub, &n, &m);
|
|
try std.testing.expectEqual(@as(?i64, 5), eval(&add, ctx));
|
|
try std.testing.expectEqual(@as(?i64, 24), eval(&mul, ctx));
|
|
try std.testing.expectEqual(@as(?i64, 2), eval(&sub, ctx));
|
|
|
|
// Nested `(M + N) - 1` and parenthesised `(M + 1) * 2` (parens carry no node).
|
|
var addmn = nBin(.add, &m, &n);
|
|
var nested = nBin(.sub, &addmn, &one);
|
|
var paren = nBin(.mul, &add, &two);
|
|
try std.testing.expectEqual(@as(?i64, 9), eval(&nested, ctx));
|
|
try std.testing.expectEqual(@as(?i64, 10), eval(&paren, ctx));
|
|
|
|
// Unary negate.
|
|
var neg = nNeg(&m);
|
|
try std.testing.expectEqual(@as(?i64, -4), eval(&neg, ctx));
|
|
|
|
// Genuinely non-const operand, division by zero, a non-arithmetic operator,
|
|
// and overflow all yield null → the caller's clean compile-halt (no panic,
|
|
// no fabricated length).
|
|
var addz = nBin(.add, &m, &z);
|
|
var divz = nBin(.div, &m, &zero);
|
|
var cmp = nBin(.lt, &m, &n);
|
|
var big = nLit(std.math.maxInt(i64));
|
|
var ovf = nBin(.mul, &big, &two);
|
|
try std.testing.expect(eval(&addz, ctx) == null);
|
|
try std.testing.expect(eval(&divz, ctx) == null);
|
|
try std.testing.expect(eval(&cmp, ctx) == null);
|
|
try std.testing.expect(eval(&ovf, ctx) == null);
|
|
}
|