]> git.lizzy.rs Git - rust.git/blob - src/librustc/infer/glb.rs
Remove ty::BrFresh and new_bound
[rust.git] / src / librustc / infer / glb.rs
1 use super::combine::CombineFields;
2 use super::InferCtxt;
3 use super::lattice::{self, LatticeDir};
4 use super::Subtype;
5
6 use crate::traits::ObligationCause;
7 use crate::ty::{self, Ty, TyCtxt};
8 use crate::ty::relate::{Relate, RelateResult, TypeRelation};
9
10 /// "Greatest lower bound" (common subtype)
11 pub struct Glb<'combine, 'infcx: 'combine, 'gcx: 'infcx+'tcx, 'tcx: 'infcx> {
12     fields: &'combine mut CombineFields<'infcx, 'gcx, 'tcx>,
13     a_is_expected: bool,
14 }
15
16 impl<'combine, 'infcx, 'gcx, 'tcx> Glb<'combine, 'infcx, 'gcx, 'tcx> {
17     pub fn new(fields: &'combine mut CombineFields<'infcx, 'gcx, 'tcx>, a_is_expected: bool)
18         -> Glb<'combine, 'infcx, 'gcx, 'tcx>
19     {
20         Glb { fields: fields, a_is_expected: a_is_expected }
21     }
22 }
23
24 impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx>
25     for Glb<'combine, 'infcx, 'gcx, 'tcx>
26 {
27     fn tag(&self) -> &'static str { "Glb" }
28
29     fn tcx(&self) -> TyCtxt<'infcx, 'gcx, 'tcx> { self.fields.tcx() }
30
31     fn a_is_expected(&self) -> bool { self.a_is_expected }
32
33     fn relate_with_variance<T: Relate<'tcx>>(&mut self,
34                                              variance: ty::Variance,
35                                              a: &T,
36                                              b: &T)
37                                              -> RelateResult<'tcx, T>
38     {
39         match variance {
40             ty::Invariant => self.fields.equate(self.a_is_expected).relate(a, b),
41             ty::Covariant => self.relate(a, b),
42             // FIXME(#41044) -- not correct, need test
43             ty::Bivariant => Ok(a.clone()),
44             ty::Contravariant => self.fields.lub(self.a_is_expected).relate(a, b),
45         }
46     }
47
48     fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
49         lattice::super_lattice_tys(self, a, b)
50     }
51
52     fn regions(&mut self, a: ty::Region<'tcx>, b: ty::Region<'tcx>)
53                -> RelateResult<'tcx, ty::Region<'tcx>> {
54         debug!("{}.regions({:?}, {:?})",
55                self.tag(),
56                a,
57                b);
58
59         let origin = Subtype(self.fields.trace.clone());
60         Ok(self.fields.infcx.borrow_region_constraints().glb_regions(self.tcx(), origin, a, b))
61     }
62
63     fn consts(
64         &mut self,
65         a: &'tcx ty::Const<'tcx>,
66         b: &'tcx ty::Const<'tcx>,
67     ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
68         debug!("{}.consts({:?}, {:?})", self.tag(), a, b);
69         if a == b {
70             return Ok(a);
71         }
72
73         self.fields.infcx.super_combine_consts(self, a, b)
74     }
75
76     fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
77                   -> RelateResult<'tcx, ty::Binder<T>>
78         where T: Relate<'tcx>
79     {
80         debug!("binders(a={:?}, b={:?})", a, b);
81
82         // When higher-ranked types are involved, computing the LUB is
83         // very challenging, switch to invariance. This is obviously
84         // overly conservative but works ok in practice.
85         self.relate_with_variance(ty::Variance::Invariant, a, b)?;
86         Ok(a.clone())
87     }
88 }
89
90 impl<'combine, 'infcx, 'gcx, 'tcx> LatticeDir<'infcx, 'gcx, 'tcx>
91     for Glb<'combine, 'infcx, 'gcx, 'tcx>
92 {
93     fn infcx(&self) -> &'infcx InferCtxt<'infcx, 'gcx, 'tcx> {
94         self.fields.infcx
95     }
96
97     fn cause(&self) -> &ObligationCause<'tcx> {
98         &self.fields.trace.cause
99     }
100
101     fn relate_bound(&mut self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()> {
102         let mut sub = self.fields.sub(self.a_is_expected);
103         sub.relate(&v, &a)?;
104         sub.relate(&v, &b)?;
105         Ok(())
106     }
107 }