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