Lines
99.01 %
Functions
91.67 %
Branches
100 %
//! Acceptance rows for plan 0011 Phase 0's `Budget` and depth guards: a
//! large datum fold and a deep macro expansion each exhaust the shared work
//! budget with a structured error instead of running unboundedly; a
//! 5000-clause `COND` compiles without the compiler's own native stack ever
//! overflowing — surviving the test process at all is the proof of that
//! last part. Row E (`C_30`) is covered here only for compile-time
//! survival; its "gives `t`" half needs an actual run and lives in
//! `rpc::session::tests::plan0011_phase3_acceptance_tests` (row R3 sits
//! beside it there for the same reason).
use nomiscript::{Compiler, Reader, SymbolTable};
use super::common::{
compile_and_validate, compile_expect_error, validate_wasm, wrap_with_runtime_i32,
};
/// Builds `C_k` per plan row E: `C0 = t`, `Ck = (cond (Ck-1 t))`.
fn cond_chain_source(depth: u32) -> String {
let mut src = "T".to_string();
for _ in 0..depth {
src = format!("(cond ({src} T))");
}
src
/// `C_30` compiles inside the work budget. Before plan 0011 Phase 3 the
/// `eval_value` classify-then-apply doubling (`E(k) = 2*E(k-1)`) exhausted
/// it, which is what an earlier version of this test pinned as a budget
/// error. (Its "gives `t`" half needs an actual run — see this module's doc
/// comment.)
#[test]
fn c30_cond_chain_compiles_within_the_work_budget() {
let src = cond_chain_source(30);
let program = Reader::parse(&src).unwrap_or_else(|e| panic!("parse {src:?}: {e}"));
let mut symbols = SymbolTable::with_builtins_for_wasm();
let mut compiler = Compiler::new();
let wasm = compiler
.compile(&program, &mut symbols)
.unwrap_or_else(|e| panic!("compile {src:?}: {e}"));
validate_wasm(&wasm);
/// A much shallower chain must compile normally and fold to `t` — the
/// budget must not reject legitimate, small programs of the same shape.
fn a_shallow_cond_chain_still_compiles() {
let src = cond_chain_source(3);
compile_and_validate(&format!("(defvar *r* {src})"));
fn cond_clause(i: u32) -> String {
format!("((> IDX {i}) {i})")
/// Row R3 / the plan's "5000-clause COND" bullet: a runtime-clause chain is
/// walked in a loop and emitted as one flat block, so this long a chain
/// compiles and validates without native recursion.
fn five_thousand_runtime_clause_cond_compiles_without_recursing() {
let clauses: String = (0..5_000u32).map(cond_clause).collect::<Vec<_>>().join(" ");
let src = wrap_with_runtime_i32(&format!("(cond {clauses} (t -1))"));
compile_and_validate(&src);
/// A small runtime-clause `COND` (well under the recursion ceiling) must
/// still compile — the guard must not reject ordinary programs.
fn a_few_runtime_clauses_still_compile() {
let clauses: String = (0..3u32).map(cond_clause).collect::<Vec<_>>().join(" ");
/// A macro whose body constant-folds a huge `LIST` call before ever
/// returning its expansion: evaluating each of the 2,000,000 elements
/// charges `eval_value` once inside the macro's private expansion
/// environment (`expand_macro`), and `expand_macro_then` additionally
/// charges the produced expansion's own `datum::cost` — either charge
/// alone exhausts the budget with a structured error.
fn a_macro_body_building_a_huge_datum_exhausts_the_budget() {
let elements: String = std::iter::repeat_n("1", 2_000_000)
.collect::<Vec<_>>()
.join(" ");
let src = format!("(defmacro biglist () (list {elements})) (defvar *r* (biglist))");
let err = compile_expect_error(&src);
assert!(
err.contains("compile-time work budget exceeded"),
"expected a budget error, got: {err}"
);
/// A handful of expansions of the same macro must still compile normally.
fn a_few_macro_expansions_still_compile() {
let src = "(defmacro noop () 1) (+ (noop) (noop) (noop))";
compile_and_validate(src);
/// A fold over a huge constant list is charged upfront by its length
/// (`fold::constant_fold_fold`) — large enough to exceed `MAX_ELAB_WORK`
/// without ever running the per-element loop.
fn folding_a_huge_constant_list_exhausts_the_budget() {
let src = format!("(fold (lambda (acc x) (+ acc x)) 0 (list {elements}))");
/// Folding a small constant list still compiles and folds to a constant.
fn folding_a_small_constant_list_still_compiles() {
compile_and_validate("(fold (lambda (acc x) (+ acc x)) 0 (list 1 2 3))");
/// A 25-runtime-clause `COND` (past the shared recursion ceiling of 24) is
/// also accepted where the eval mirror classifies it first: a `DEFVAR`
/// initializer.
fn a_25_runtime_clause_cond_compiles_as_a_defvar_initializer() {
let clauses: String = (0..25u32).map(|i| format!("((= IDX {i}) {i}) ")).collect();
let src = wrap_with_runtime_i32(&format!("(defvar *r* (cond {clauses}(t -1)))"));
/// Same, as a `LET` binding initializer.
fn a_25_runtime_clause_cond_compiles_as_a_let_initializer() {
let src = wrap_with_runtime_i32(&format!("(let ((x (cond {clauses}(t -1)))) x)"));
/// Folding cons onto a flat accumulator is linear work, not quadratic: the
/// datum depth check never charges the accumulator's size to the budget.
fn consing_a_1500_element_fold_stays_within_the_budget() {
let elements: String = (0..1500u32).map(|i| format!("{i} ")).collect();
compile_and_validate(&format!(
"(length (fold (lambda (acc x) (cons x acc)) nil (list {elements})))"
));
/// The nested list is built at macro-expansion time: a FOLD in ordinary
/// code is not fold-safe (0014 Stage 2) and builds its list at runtime,
/// where no compile-time depth ceiling applies.
fn nesting_fold_source(levels: u32) -> String {
let elements: String = (0..levels).map(|i| format!("{i} ")).collect();
format!(
"(defmacro deep () (list 'quote (fold (lambda (acc x) (list acc x)) nil (list {elements})))) \
(length (deep))"
)
/// `MAX_ELAB_DEPTH` counts user-visible list levels: a quoted nested list
/// costs one level per list, not two, so 100 levels fit under the 128
/// ceiling while 200 are refused.
fn a_fold_nesting_100_list_levels_compiles_and_200_are_refused() {
compile_and_validate(&nesting_fold_source(100));
let err = compile_expect_error(&nesting_fold_source(200));
assert!(err.contains("elaboration depth limit"), "{err}");
/// A LET nested in a LET init, each with a nil accumulator, evaluates its
/// inits once per level, so the work stays linear in the depth.
fn nested_lets_with_nil_accumulators_compile_within_the_work_budget() {
let mut src = "(entity-count)".to_string();
for _ in 0..16 {
src = format!("(let ((a {src}) (b nil)) (setf b (cons a b)) a)");
compile_and_validate(&format!("(list {src})"));
/// A chain of defuns, each calling the previous one in a LET init beside a
/// nil accumulator, evaluates each init once.
fn chained_defuns_with_nil_accumulators_compile_within_the_work_budget() {
let defuns: String = (1..=14)
.map(|i| {
"(defun f{i} (x) (let ((y (f{} x)) (acc nil)) (setf acc (cons y acc)) (car acc)))",
i - 1
})
.collect();
"(defun f0 (x) x) {defuns} (list (f14 (entity-count)))"