summaryrefslogtreecommitdiff
path: root/compiler/rustc_middle/src/traits/solve.rs
blob: df2fe5f03c81f52f8917c1a6fe452bb213dc8689 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use std::ops::ControlFlow;

use rustc_data_structures::intern::Interned;

use crate::ty::{
    ir::{self, TypeFoldable, TypeVisitable},
    FallibleTypeFolder, Ty, TypeFolder, TypeVisitor,
};

#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
pub struct ExternalConstraints<'tcx>(pub(crate) Interned<'tcx, ExternalConstraintsData<'tcx>>);

impl<'tcx> std::ops::Deref for ExternalConstraints<'tcx> {
    type Target = ExternalConstraintsData<'tcx>;

    fn deref(&self) -> &Self::Target {
        &*self.0
    }
}

/// Additional constraints returned on success.
#[derive(Debug, PartialEq, Eq, Clone, Hash, Default)]
pub struct ExternalConstraintsData<'tcx> {
    // FIXME: implement this.
    pub regions: (),
    pub opaque_types: Vec<(Ty<'tcx>, Ty<'tcx>)>,
}

impl<'tcx> TypeFoldable<'tcx> for ExternalConstraints<'tcx> {
    fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
        Ok(ir::FallibleTypeFolder::tcx(folder).intern_external_constraints(
            ExternalConstraintsData {
                regions: (),
                opaque_types: self
                    .opaque_types
                    .iter()
                    .map(|opaque| opaque.try_fold_with(folder))
                    .collect::<Result<_, F::Error>>()?,
            },
        ))
    }

    fn fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
        ir::TypeFolder::tcx(folder).intern_external_constraints(ExternalConstraintsData {
            regions: (),
            opaque_types: self.opaque_types.iter().map(|opaque| opaque.fold_with(folder)).collect(),
        })
    }
}

impl<'tcx> TypeVisitable<'tcx> for ExternalConstraints<'tcx> {
    fn visit_with<V: TypeVisitor<'tcx>>(
        &self,
        visitor: &mut V,
    ) -> std::ops::ControlFlow<V::BreakTy> {
        self.regions.visit_with(visitor)?;
        self.opaque_types.visit_with(visitor)?;
        ControlFlow::Continue(())
    }
}