validate: a unit test that size grows its own stack

This commit is contained in:
Meta Vens 2026-11-28 01:29:50 +01:00
commit c6a2e21e2b
2 changed files with 35 additions and 31 deletions

View file

@ -73,3 +73,38 @@ pub(super) fn check(root: &Node, limits: &Limits, surface: &str) -> Vec<Diagnost
}
found
}
#[cfg(test)]
mod tests {
use thing_testkit::build::{boxed, text};
use super::*;
/// Runs `work` with about `left` bytes of stack remaining: the stack is burned first, so a recursion
/// that does not grow its own runs out inside it.
#[inline(never)]
fn with_stack_left(left: usize, work: &mut dyn FnMut()) {
if stacker::remaining_stack().expect("the thread's stack limit is known") > left {
let burn = [0u8; 4096];
std::hint::black_box(&burn);
with_stack_left(left, work);
std::hint::black_box(&burn);
} else {
work();
}
}
#[test]
fn size_of_a_deep_subtree_grows_its_own_stack() {
// expand counts a slot argument with `size` after the argument was expanded, from wherever the
// stack stands; a 1,000-level argument must not run it out.
let mut deep = text("bottom", "x");
for level in 1..1000 {
deep = boxed(&format!("level-{level}"), vec![deep]);
}
let mut counted = 0;
with_stack_left(100 * 1024, &mut || counted = size(&deep));
assert_eq!(counted, 1000);
// Dropping a tree this deep needs the room the test thread has outside `with_stack_left`.
}
}

View file

@ -994,37 +994,6 @@ fn the_stack_bound_counts_a_slot_argument() {
stack_bound_counts(Through::SlotArgument);
}
/// Runs `work` with only about `left` bytes of stack remaining: the stack is burned first, so a
/// recursion that does not grow its own stack runs out inside it.
#[inline(never)]
fn with_stack_left(left: usize, work: &mut dyn FnMut()) {
let remaining = stacker::remaining_stack().expect("the thread's stack limit is known");
if remaining > left {
let burn = [0u8; 4096];
std::hint::black_box(&burn);
with_stack_left(left, work);
std::hint::black_box(&burn);
} else {
work();
}
}
#[test]
fn counting_a_deep_slot_argument_grows_its_own_stack() {
// The body places a 900-level slot argument; the node limit refuses it where it is counted
// (validate::size, a recursion over the argument), before anything clones it. Run with little
// stack left, the count must move to a fresh segment to finish.
let limits = limited(1500);
let body = composition("c", &[("body", ParamKind::Slot)], boxed("b", vec![slot("body")]));
let state = state_with(&[body]);
let document = surface(boxed("root", vec![use_of("u", "c", vec![slot_arg("body", vec![chain(900, false)])])]), &[]);
let mut resolution = None;
with_stack_left(600 * 1024, &mut || resolution = Some(resolve(&document, &state, &limits)));
let resolution = resolution.unwrap();
refused_for_limit(&resolution);
assert!(!too_deep_to_resolve(&resolution), "{:?}", resolution.diagnostics);
}
#[test]
fn a_slot_naming_a_param_of_another_kind_is_invalid() {
let body = composition("c", &[("x", ParamKind::Text)], boxed("b", vec![slot("x")]));