bend/fun/check/
unbound_refs.rs

1use crate::{
2  diagnostics::Diagnostics,
3  fun::{Book, Ctx, Name, Term},
4  maybe_grow,
5};
6use std::collections::HashSet;
7
8impl Ctx<'_> {
9  pub fn check_unbound_refs(&mut self) -> Result<(), Diagnostics> {
10    for def in self.book.defs.values() {
11      let mut unbounds = HashSet::new();
12      for rule in def.rules.iter() {
13        rule.body.check_unbound_refs(self.book, &mut unbounds);
14      }
15      for unbound in unbounds {
16        self.info.add_function_error(
17          format!("Reference to undefined function '{unbound}'"),
18          def.name.clone(),
19          def.source.clone(),
20        );
21      }
22    }
23    self.info.fatal(())
24  }
25}
26
27impl Term {
28  pub fn check_unbound_refs(&self, book: &Book, unbounds: &mut HashSet<Name>) {
29    maybe_grow(|| {
30      if let Term::Ref { nam } = self {
31        if !(book.defs.contains_key(nam) || book.hvm_defs.contains_key(nam)) {
32          unbounds.insert(nam.clone());
33        }
34      }
35      for child in self.children() {
36        child.check_unbound_refs(book, unbounds);
37      }
38    })
39  }
40}