]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_infer/src/infer/glb.rs
Rollup merge of #81926 - henryboisdequin:fix-81907, r=estebank
[rust.git] / compiler / rustc_infer / src / infer / glb.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 /// "Greatest lower bound" (common subtype)
12 pub struct Glb<'combine, 'infcx, 'tcx> {
13     fields: &'combine mut CombineFields<'infcx, 'tcx>,
14     a_is_expected: bool,
15 }
16
17 impl<'combine, 'infcx, 'tcx> Glb<'combine, 'infcx, 'tcx> {
18     pub fn new(
19         fields: &'combine mut CombineFields<'infcx, 'tcx>,
20         a_is_expected: bool,
21     ) -> Glb<'combine, 'infcx, 'tcx> {
22         Glb { fields, a_is_expected }
23     }
24 }
25
26 impl TypeRelation<'tcx> for Glb<'combine, 'infcx, 'tcx> {
27     fn tag(&self) -> &'static str {
28         "Glb"
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),
54             ty::Contravariant => self.fields.lub(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().glb_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<'combine, 'infcx, 'tcx> LatticeDir<'infcx, 'tcx> for Glb<'combine, 'infcx, 'tcx> {
105     fn infcx(&self) -> &'infcx InferCtxt<'infcx, 'tcx> {
106         self.fields.infcx
107     }
108
109     fn cause(&self) -> &ObligationCause<'tcx> {
110         &self.fields.trace.cause
111     }
112
113     fn relate_bound(&mut self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()> {
114         let mut sub = self.fields.sub(self.a_is_expected);
115         sub.relate(v, a)?;
116         sub.relate(v, b)?;
117         Ok(())
118     }
119 }
120
121 impl<'tcx> ConstEquateRelation<'tcx> for Glb<'_, '_, 'tcx> {
122     fn const_equate_obligation(&mut self, a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>) {
123         self.fields.add_const_equate_obligation(self.a_is_expected, a, b);
124     }
125 }