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