]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_infer/src/infer/lub.rs
Auto merge of #88804 - Mark-Simulacrum:never-algo-v2, r=nikomatsakis,jackh726
[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         _info: ty::VarianceDiagInfo<'tcx>,
47         a: T,
48         b: T,
49     ) -> RelateResult<'tcx, T> {
50         match variance {
51             ty::Invariant => self.fields.equate(self.a_is_expected).relate(a, b),
52             ty::Covariant => self.relate(a, b),
53             // FIXME(#41044) -- not correct, need test
54             ty::Bivariant => Ok(a),
55             ty::Contravariant => self.fields.glb(self.a_is_expected).relate(a, b),
56         }
57     }
58
59     fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
60         lattice::super_lattice_tys(self, a, b)
61     }
62
63     fn regions(
64         &mut self,
65         a: ty::Region<'tcx>,
66         b: ty::Region<'tcx>,
67     ) -> RelateResult<'tcx, ty::Region<'tcx>> {
68         debug!("{}.regions({:?}, {:?})", self.tag(), a, b);
69
70         let origin = Subtype(Box::new(self.fields.trace.clone()));
71         Ok(self.fields.infcx.inner.borrow_mut().unwrap_region_constraints().lub_regions(
72             self.tcx(),
73             origin,
74             a,
75             b,
76         ))
77     }
78
79     fn consts(
80         &mut self,
81         a: &'tcx ty::Const<'tcx>,
82         b: &'tcx ty::Const<'tcx>,
83     ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
84         self.fields.infcx.super_combine_consts(self, a, b)
85     }
86
87     fn binders<T>(
88         &mut self,
89         a: ty::Binder<'tcx, T>,
90         b: ty::Binder<'tcx, T>,
91     ) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
92     where
93         T: Relate<'tcx>,
94     {
95         debug!("binders(a={:?}, b={:?})", a, b);
96
97         // When higher-ranked types are involved, computing the LUB is
98         // very challenging, switch to invariance. This is obviously
99         // overly conservative but works ok in practice.
100         self.relate_with_variance(ty::Variance::Invariant, ty::VarianceDiagInfo::default(), a, b)?;
101         Ok(a)
102     }
103 }
104
105 impl<'tcx> ConstEquateRelation<'tcx> for Lub<'_, '_, 'tcx> {
106     fn const_equate_obligation(&mut self, a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>) {
107         self.fields.add_const_equate_obligation(self.a_is_expected, a, b);
108     }
109 }
110
111 impl<'combine, 'infcx, 'tcx> LatticeDir<'infcx, 'tcx> for Lub<'combine, 'infcx, 'tcx> {
112     fn infcx(&self) -> &'infcx InferCtxt<'infcx, 'tcx> {
113         self.fields.infcx
114     }
115
116     fn cause(&self) -> &ObligationCause<'tcx> {
117         &self.fields.trace.cause
118     }
119
120     fn relate_bound(&mut self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()> {
121         let mut sub = self.fields.sub(self.a_is_expected);
122         sub.relate(a, v)?;
123         sub.relate(b, v)?;
124         Ok(())
125     }
126 }