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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//! This module contains impls of `TypeFoldable` for those types that
//! introduce binders.
//!
//! The more interesting impls of `TypeFoldable` remain in the `fold` module.

use crate::*;

impl<I: Interner> TypeFoldable<I> for FnPointer<I> {
    fn try_fold_with<E>(
        self,
        folder: &mut dyn FallibleTypeFolder<I, Error = E>,
        outer_binder: DebruijnIndex,
    ) -> Result<Self, E> {
        let FnPointer {
            num_binders,
            substitution,
            sig,
        } = self;
        Ok(FnPointer {
            num_binders,
            substitution: substitution.try_fold_with(folder, outer_binder.shifted_in())?,
            sig: FnSig {
                abi: sig.abi,
                safety: sig.safety,
                variadic: sig.variadic,
            },
        })
    }
}

impl<T, I: Interner> TypeFoldable<I> for Binders<T>
where
    T: HasInterner<Interner = I> + TypeFoldable<I>,
    I: Interner,
{
    fn try_fold_with<E>(
        self,
        folder: &mut dyn FallibleTypeFolder<I, Error = E>,
        outer_binder: DebruijnIndex,
    ) -> Result<Self, E> {
        let Binders {
            binders: self_binders,
            value: self_value,
        } = self;
        let value = self_value.try_fold_with(folder, outer_binder.shifted_in())?;
        let binders = VariableKinds {
            interned: self_binders.interned().clone(),
        };
        Ok(Binders::new(binders, value))
    }
}

impl<I, T> TypeFoldable<I> for Canonical<T>
where
    I: Interner,
    T: HasInterner<Interner = I> + TypeFoldable<I>,
{
    fn try_fold_with<E>(
        self,
        folder: &mut dyn FallibleTypeFolder<I, Error = E>,
        outer_binder: DebruijnIndex,
    ) -> Result<Self, E> {
        let Canonical {
            binders: self_binders,
            value: self_value,
        } = self;
        let value = self_value.try_fold_with(folder, outer_binder.shifted_in())?;
        let binders = CanonicalVarKinds {
            interned: self_binders.interned().clone(),
        };
        Ok(Canonical { binders, value })
    }
}