]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_infer/src/traits/structural_impls.rs
Folding revamp.
[rust.git] / compiler / rustc_infer / src / traits / structural_impls.rs
1 use crate::traits;
2 use crate::traits::project::Normalized;
3 use rustc_middle::ty;
4 use rustc_middle::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeVisitor};
5
6 use std::fmt;
7 use std::ops::ControlFlow;
8
9 // Structural impls for the structs in `traits`.
10
11 impl<'tcx, T: fmt::Debug> fmt::Debug for Normalized<'tcx, T> {
12     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13         write!(f, "Normalized({:?}, {:?})", self.value, self.obligations)
14     }
15 }
16
17 impl<'tcx, O: fmt::Debug> fmt::Debug for traits::Obligation<'tcx, O> {
18     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19         if ty::tls::with(|tcx| tcx.sess.verbose()) {
20             write!(
21                 f,
22                 "Obligation(predicate={:?}, cause={:?}, param_env={:?}, depth={})",
23                 self.predicate, self.cause, self.param_env, self.recursion_depth
24             )
25         } else {
26             write!(f, "Obligation(predicate={:?}, depth={})", self.predicate, self.recursion_depth)
27         }
28     }
29 }
30
31 impl<'tcx> fmt::Debug for traits::FulfillmentError<'tcx> {
32     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33         write!(f, "FulfillmentError({:?},{:?})", self.obligation, self.code)
34     }
35 }
36
37 impl<'tcx> fmt::Debug for traits::FulfillmentErrorCode<'tcx> {
38     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39         match *self {
40             super::CodeSelectionError(ref e) => write!(f, "{:?}", e),
41             super::CodeProjectionError(ref e) => write!(f, "{:?}", e),
42             super::CodeSubtypeError(ref a, ref b) => {
43                 write!(f, "CodeSubtypeError({:?}, {:?})", a, b)
44             }
45             super::CodeConstEquateError(ref a, ref b) => {
46                 write!(f, "CodeConstEquateError({:?}, {:?})", a, b)
47             }
48             super::CodeAmbiguity => write!(f, "Ambiguity"),
49         }
50     }
51 }
52
53 impl<'tcx> fmt::Debug for traits::MismatchedProjectionTypes<'tcx> {
54     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55         write!(f, "MismatchedProjectionTypes({:?})", self.err)
56     }
57 }
58
59 ///////////////////////////////////////////////////////////////////////////
60 // TypeFoldable implementations.
61
62 impl<'tcx, O: TypeFoldable<'tcx>> TypeFoldable<'tcx> for traits::Obligation<'tcx, O> {
63     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
64         Ok(traits::Obligation {
65             cause: self.cause,
66             recursion_depth: self.recursion_depth,
67             predicate: self.predicate.try_fold_with(folder)?,
68             param_env: self.param_env.try_fold_with(folder)?,
69         })
70     }
71
72     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
73         self.predicate.visit_with(visitor)?;
74         self.param_env.visit_with(visitor)
75     }
76 }