summaryrefslogtreecommitdiffstats
path: root/vendor/chalk-ir/src/visit/visitors.rs
blob: 51c0878404164441d55da840047f398c55caca5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//! TypeVisitor helpers

use crate::{BoundVar, ControlFlow, DebruijnIndex, Interner, TypeVisitable, TypeVisitor};

/// TypeVisitor extensions.
pub trait VisitExt<I: Interner>: TypeVisitable<I> {
    /// Check whether there are free (non-bound) variables.
    fn has_free_vars(&self, interner: I) -> bool {
        let flow = self.visit_with(
            &mut FindFreeVarsVisitor { interner },
            DebruijnIndex::INNERMOST,
        );
        matches!(flow, ControlFlow::Break(_))
    }
}

impl<T, I: Interner> VisitExt<I> for T where T: TypeVisitable<I> {}

struct FindFreeVarsVisitor<I: Interner> {
    interner: I,
}

impl<I: Interner> TypeVisitor<I> for FindFreeVarsVisitor<I> {
    type BreakTy = ();

    fn as_dyn(&mut self) -> &mut dyn TypeVisitor<I, BreakTy = Self::BreakTy> {
        self
    }

    fn interner(&self) -> I {
        self.interner
    }

    fn visit_free_var(
        &mut self,
        _bound_var: BoundVar,
        _outer_binder: DebruijnIndex,
    ) -> ControlFlow<()> {
        ControlFlow::Break(())
    }
}