Lines
98.53 %
Functions
100 %
Branches
//! The syntactic question `eval_block` asks of a BLOCK body before folding
//! it: whether a `return-from` may leave the block. A client of the scoped
//! walk in `expr::fold::walk`, narrowed to one block name.
//!
//! The answer errs towards "cannot fold". A macro call counts as an exit
//! unless the macro is a pure template: any other expansion may depend on the
//! environment at the call site (lexical bindings, earlier stores in the
//! body), which the scan cannot reconstruct.
use crate::ast::Expr;
use crate::compiler::expr::fold::walk::{Node, Scope, Step, Visit, walk_body};
#[cfg(test)]
use crate::runtime::SymbolKind;
use crate::runtime::SymbolTable;
use super::block_parse::{NIL_LABEL, label};
use super::nil_block::is_loop;
pub(super) const DEFMACRO: &str = "DEFMACRO";
/// Whether a `(return-from name …)` in `body` may leave the block `name`.
/// Quoted data, inner blocks rebinding `name`, the dead branch of a
/// conditional whose test is a literal, and a lambda discarded by a body are
/// skipped; everything else, lambda bodies included, counts. A template macro
/// is expanded (its expansion is the same in every environment); any other
/// macro call, or one that fails to expand, counts as an exit, as does any
/// body that defines a macro.
pub(super) fn has_exit_to(symbols: &SymbolTable, name: &str, body: &[Expr]) -> bool {
scan(
symbols,
ExitTo {
name,
own_loop: false,
defining_exits: true,
},
body,
)
}
/// Whether a `(return …)` in `looped`, a DO / DO* / DOLIST form, may leave
/// the NIL block the loop itself opens; the loops nested in it open their own.
pub(super) fn loop_exits(symbols: &SymbolTable, looped: &Expr) -> bool {
name: NIL_LABEL,
own_loop: true,
defining_exits: false,
std::slice::from_ref(looped),
fn scan(symbols: &SymbolTable, mut visitor: ExitTo, body: &[Expr]) -> bool {
(visitor.defining_exits && body.iter().any(defines_macro))
|| !matches!(walk_body(symbols, &[], body, &mut visitor), Ok(false))
/// Whether `form` defines a macro anywhere outside quoted data: its
/// expansion is not analysed, so the enclosing block may be left through it.
pub(super) fn defines_macro(form: &Expr) -> bool {
match form {
Expr::List(elems) => {
matches!(elems.first(), Some(Expr::Symbol(h)) if h.eq_ignore_ascii_case(DEFMACRO))
|| elems.iter().any(defines_macro)
Expr::Cons(car, cdr) => defines_macro(car) || defines_macro(cdr),
Expr::Quasiquote(inner)
| Expr::Unquote(inner)
| Expr::UnquoteSplicing(inner)
| Expr::Lambda(_, inner) => defines_macro(inner),
_ => false,
struct ExitTo<'a> {
name: &'a str,
/// The first loop met is the one whose own block is asked about.
own_loop: bool,
/// A macro definition may be used to leave the block, unseen: true unless
/// a loop's own block is asked about, where a use is an opaque macro.
defining_exits: bool,
impl ExitTo<'_> {
fn names(&self, elems: &[Expr]) -> bool {
elems
.get(1)
.and_then(label)
.is_some_and(|n| n.eq_ignore_ascii_case(self.name))
fn shadows(&mut self, head: &str) -> bool {
if !is_loop(head) || !self.name.eq_ignore_ascii_case(NIL_LABEL) {
return false;
!std::mem::take(&mut self.own_loop)
impl Visit for ExitTo<'_> {
fn visit(&mut self, _env: &SymbolTable, _scope: &Scope, node: Node<'_>) -> Step {
match node {
Node::Special(head, elems) if head.eq_ignore_ascii_case("RETURN-FROM") => {
if self.names(elems) {
Step::Found
} else {
Step::Descend
Node::Special(head, elems)
if head.eq_ignore_ascii_case("BLOCK") && self.names(elems) =>
{
if elems.iter().any(defines_macro) {
Step::Skip
Node::Special(head, _) if self.shadows(head) => Step::Skip,
Node::Special(head, _) if head.eq_ignore_ascii_case(DEFMACRO) => {
match self.defining_exits {
true => Step::Found,
false => Step::Skip,
Node::OpaqueMacro => Step::Found,
Node::DiscardedLambda | Node::Datum | Node::Free(_) => Step::Skip,
Node::Special(..) | Node::Native(_) | Node::Call | Node::Other => Step::Descend,
fn prunes_dead_branches(&self) -> bool {
true
#[path = "exit_scan_tests.rs"]
mod tests;