]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_trait_selection/src/traits/error_reporting/method_chain.rs
Rollup merge of #105975 - jeremystucki:rustc-remove-needless-lifetimes, r=eholk
[rust.git] / compiler / rustc_trait_selection / src / traits / error_reporting / method_chain.rs
1 use crate::infer::InferCtxt;
2
3 use rustc_middle::ty::error::TypeError;
4 use rustc_middle::ty::relate::{self, Relate, RelateResult, TypeRelation};
5 use rustc_middle::ty::{self, Ty, TyCtxt};
6
7 pub struct CollectAllMismatches<'a, 'tcx> {
8     pub infcx: &'a InferCtxt<'tcx>,
9     pub param_env: ty::ParamEnv<'tcx>,
10     pub errors: Vec<TypeError<'tcx>>,
11 }
12
13 impl<'a, 'tcx> TypeRelation<'tcx> for CollectAllMismatches<'a, 'tcx> {
14     fn tag(&self) -> &'static str {
15         "CollectAllMismatches"
16     }
17
18     fn tcx(&self) -> TyCtxt<'tcx> {
19         self.infcx.tcx
20     }
21
22     fn intercrate(&self) -> bool {
23         false
24     }
25
26     fn param_env(&self) -> ty::ParamEnv<'tcx> {
27         self.param_env
28     }
29
30     fn a_is_expected(&self) -> bool {
31         true
32     }
33
34     fn mark_ambiguous(&mut self) {
35         bug!()
36     }
37
38     fn relate_with_variance<T: Relate<'tcx>>(
39         &mut self,
40         _: ty::Variance,
41         _: ty::VarianceDiagInfo<'tcx>,
42         a: T,
43         b: T,
44     ) -> RelateResult<'tcx, T> {
45         self.relate(a, b)
46     }
47
48     fn regions(
49         &mut self,
50         a: ty::Region<'tcx>,
51         _b: ty::Region<'tcx>,
52     ) -> RelateResult<'tcx, ty::Region<'tcx>> {
53         Ok(a)
54     }
55
56     fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
57         self.infcx.probe(|_| {
58             if a.is_ty_infer() || b.is_ty_infer() {
59                 Ok(a)
60             } else {
61                 self.infcx.super_combine_tys(self, a, b).or_else(|e| {
62                     self.errors.push(e);
63                     Ok(a)
64                 })
65             }
66         })
67     }
68
69     fn consts(
70         &mut self,
71         a: ty::Const<'tcx>,
72         b: ty::Const<'tcx>,
73     ) -> RelateResult<'tcx, ty::Const<'tcx>> {
74         if a == b {
75             return Ok(a);
76         }
77         relate::super_relate_consts(self, a, b) // could do something similar here for constants!
78     }
79
80     fn binders<T: Relate<'tcx>>(
81         &mut self,
82         a: ty::Binder<'tcx, T>,
83         b: ty::Binder<'tcx, T>,
84     ) -> RelateResult<'tcx, ty::Binder<'tcx, T>> {
85         Ok(a.rebind(self.relate(a.skip_binder(), b.skip_binder())?))
86     }
87 }