Lines
100 %
Functions
60 %
Branches
//! Depth-safe teardown for a constructed [`Expr`] datum (plan 0011 Phase 0).
//!
//! `Expr`'s recursive variants (`Cons`, `List`, `Quote`/`Quasiquote`/
//! `Unquote`/`UnquoteSplicing`, `Lambda`) nest through `Box<Expr>` /
//! `Vec<Expr>`. Rust's compiler-generated drop glue tears these down one
//! native stack frame per nesting level, with no compile-time recursion
//! guard able to see it — the overflow would happen *after* elaboration,
//! when a deep macro-generated or quasiquote-spliced datum is discarded.
//! `Expr::RuntimeValue` embeds a [`Value`], which recurses the same way
//! through its own `Pair` / `Vector` / `Struct` / `Closure` variants, so
//! teardown carries a second, matching worklist for those.
//! [`drop_iteratively`] flattens the structure into owned worklists before
//! dropping it, so teardown cost is bounded by heap allocation, never by
//! native stack depth. A blanket `impl Drop for Expr` (or `Value`) was
//! rejected: both types are pattern-matched by value (moving variant fields
//! out) at dozens of call sites across the compiler, and a type that
//! implements `Drop` cannot be partially moved out of — every one of
//! those sites would need rewriting to borrow instead, which is a
//! behaviour-changing refactor far outside this framework-only phase.
//! `drop_iteratively` is called explicitly instead, at the sites that
//! construct or discard datums large enough to matter (e.g. a constant-
//! folded list). `Pair` itself has an iterative `Drop` along the cdr
//! (nothing moves its fields out), which leaves the car, vector and struct
//! nesting to these worklists.
use crate::ast::{Expr, LambdaParams};
use crate::runtime::Value;
/// Drops `expr` without recursing the native stack, regardless of how
/// deeply it — or any `Value` it embeds via `Expr::RuntimeValue` — nests.
pub(crate) fn drop_iteratively(mut expr: Expr) {
let mut pending_exprs = Vec::new();
let mut pending_values = Vec::new();
take_children(&mut expr, &mut pending_exprs, &mut pending_values);
drop(expr);
loop {
if let Some(mut child) = pending_exprs.pop() {
take_children(&mut child, &mut pending_exprs, &mut pending_values);
drop(child);
continue;
}
let Some(mut value) = pending_values.pop() else {
break;
};
take_value_children(&mut value, &mut pending_values);
drop(value);
/// Moves every direct `Expr` child out of `expr` (replacing each with the
/// leaf `Expr::Nil`) and appends them to `pending_exprs` — or, for an
/// embedded `Value`, to `pending_values` — leaving `expr` itself childless
/// so dropping it directly costs one stack frame.
fn take_children(expr: &mut Expr, pending_exprs: &mut Vec<Expr>, pending_values: &mut Vec<Value>) {
match expr {
Expr::Cons(car, cdr) => {
pending_exprs.push(std::mem::replace(car.as_mut(), Expr::Nil));
pending_exprs.push(std::mem::replace(cdr.as_mut(), Expr::Nil));
Expr::List(items) => pending_exprs.append(items),
Expr::Quote(inner)
| Expr::Datum(inner)
| Expr::Quasiquote(inner)
| Expr::Unquote(inner)
| Expr::UnquoteSplicing(inner) => {
pending_exprs.push(std::mem::replace(inner.as_mut(), Expr::Nil));
Expr::Lambda(params, body) => {
pending_exprs.push(std::mem::replace(body.as_mut(), Expr::Nil));
take_param_defaults(params, pending_exprs);
Expr::RuntimeValue(value) => {
pending_values.push(std::mem::replace(value, Value::Nil));
Expr::Nil
| Expr::T
| Expr::Number(_)
| Expr::String(_)
| Expr::Symbol(_)
| Expr::Keyword(_)
| Expr::Bytes(_)
| Expr::WasmRuntime(_)
| Expr::WasmLocal(_, _)
| Expr::WasmGlobal(_, _)
| Expr::Instance(_, _) => {}
fn take_param_defaults(params: &mut LambdaParams, pending: &mut Vec<Expr>) {
let defaults = params
.optional
.iter_mut()
.map(|(_, default)| default)
.chain(params.key.iter_mut().map(|param| &mut param.default))
.chain(params.aux.iter_mut().map(|(_, default)| default));
pending.extend(defaults.filter_map(Option::take));
/// Moves every direct `Value` child out of `value` (replacing each with the
/// leaf `Value::Nil`) and appends them to `pending`, leaving `value` itself
/// childless so dropping it directly costs one stack frame.
fn take_value_children(value: &mut Value, pending: &mut Vec<Value>) {
match value {
Value::Pair(pair) => {
pending.push(std::mem::replace(&mut pair.car, Value::Nil));
pending.push(std::mem::replace(&mut pair.cdr, Value::Nil));
Value::Vector(items) => pending.append(items),
Value::Struct { fields, .. } => pending.extend(fields.drain(..).map(|(_, item)| item)),
Value::Closure(closure) => pending.append(&mut closure.env),
Value::Nil
| Value::T
| Value::Number(_)
| Value::String(_)
| Value::Symbol(_)
| Value::Bytes(_)
| Value::Condition
| Value::Commodity { .. } => {}
#[cfg(test)]
#[path = "ast_teardown_tests.rs"]
mod tests;