]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_infer/src/infer/lub.rs
Rollup merge of #107598 - chenyukang:yukang/fix-core-bench, r=thomcc
[rust.git] / compiler / rustc_infer / src / infer / lub.rs
1 //! Least upper bound. See [`lattice`].
2
3 use super::combine::CombineFields;
4 use super::lattice::{self, LatticeDir};
5 use super::InferCtxt;
6 use super::Subtype;
7
8 use crate::infer::combine::ConstEquateRelation;
9 use crate::traits::{ObligationCause, PredicateObligation};
10 use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
11 use rustc_middle::ty::{self, Ty, TyCtxt};
12
13 /// "Least upper bound" (common supertype)
14 pub struct Lub<'combine, 'infcx, 'tcx> {
15     fields: &'combine mut CombineFields<'infcx, 'tcx>,
16     a_is_expected: bool,
17 }
18
19 impl<'combine, 'infcx, 'tcx> Lub<'combine, 'infcx, 'tcx> {
20     pub fn new(
21         fields: &'combine mut CombineFields<'infcx, 'tcx>,
22         a_is_expected: bool,
23     ) -> Lub<'combine, 'infcx, 'tcx> {
24         Lub { fields, a_is_expected }
25     }
26 }
27
28 impl<'tcx> TypeRelation<'tcx> for Lub<'_, '_, 'tcx> {
29     fn tag(&self) -> &'static str {
30         "Lub"
31     }
32
33     fn intercrate(&self) -> bool {
34         assert!(!self.fields.infcx.intercrate);
35         false
36     }
37
38     fn tcx(&self) -> TyCtxt<'tcx> {
39         self.fields.tcx()
40     }
41
42     fn param_env(&self) -> ty::ParamEnv<'tcx> {
43         self.fields.param_env
44     }
45
46     fn a_is_expected(&self) -> bool {
47         self.a_is_expected
48     }
49
50     fn mark_ambiguous(&mut self) {
51         bug!("mark_ambiguous used outside of coherence");
52     }
53
54     fn relate_with_variance<T: Relate<'tcx>>(
55         &mut self,
56         variance: ty::Variance,
57         _info: ty::VarianceDiagInfo<'tcx>,
58         a: T,
59         b: T,
60     ) -> RelateResult<'tcx, T> {
61         match variance {
62             ty::Invariant => self.fields.equate(self.a_is_expected).relate(a, b),
63             ty::Covariant => self.relate(a, b),
64             // FIXME(#41044) -- not correct, need test
65             ty::Bivariant => Ok(a),
66             ty::Contravariant => self.fields.glb(self.a_is_expected).relate(a, b),
67         }
68     }
69
70     fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
71         lattice::super_lattice_tys(self, a, b)
72     }
73
74     fn regions(
75         &mut self,
76         a: ty::Region<'tcx>,
77         b: ty::Region<'tcx>,
78     ) -> RelateResult<'tcx, ty::Region<'tcx>> {
79         debug!("{}.regions({:?}, {:?})", self.tag(), a, b);
80
81         let origin = Subtype(Box::new(self.fields.trace.clone()));
82         // LUB(&'static u8, &'a u8) == &RegionGLB('static, 'a) u8 == &'a u8
83         Ok(self.fields.infcx.inner.borrow_mut().unwrap_region_constraints().glb_regions(
84             self.tcx(),
85             origin,
86             a,
87             b,
88         ))
89     }
90
91     fn consts(
92         &mut self,
93         a: ty::Const<'tcx>,
94         b: ty::Const<'tcx>,
95     ) -> RelateResult<'tcx, ty::Const<'tcx>> {
96         self.fields.infcx.super_combine_consts(self, a, b)
97     }
98
99     fn binders<T>(
100         &mut self,
101         a: ty::Binder<'tcx, T>,
102         b: ty::Binder<'tcx, T>,
103     ) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
104     where
105         T: Relate<'tcx>,
106     {
107         // LUB of a binder and itself is just itself
108         if a == b {
109             return Ok(a);
110         }
111
112         debug!("binders(a={:?}, b={:?})", a, b);
113         if a.skip_binder().has_escaping_bound_vars() || b.skip_binder().has_escaping_bound_vars() {
114             // When higher-ranked types are involved, computing the LUB is
115             // very challenging, switch to invariance. This is obviously
116             // overly conservative but works ok in practice.
117             self.relate_with_variance(
118                 ty::Variance::Invariant,
119                 ty::VarianceDiagInfo::default(),
120                 a,
121                 b,
122             )?;
123             Ok(a)
124         } else {
125             Ok(ty::Binder::dummy(self.relate(a.skip_binder(), b.skip_binder())?))
126         }
127     }
128 }
129
130 impl<'tcx> ConstEquateRelation<'tcx> for Lub<'_, '_, 'tcx> {
131     fn const_equate_obligation(&mut self, a: ty::Const<'tcx>, b: ty::Const<'tcx>) {
132         self.fields.add_const_equate_obligation(self.a_is_expected, a, b);
133     }
134 }
135
136 impl<'combine, 'infcx, 'tcx> LatticeDir<'infcx, 'tcx> for Lub<'combine, 'infcx, 'tcx> {
137     fn infcx(&self) -> &'infcx InferCtxt<'tcx> {
138         self.fields.infcx
139     }
140
141     fn cause(&self) -> &ObligationCause<'tcx> {
142         &self.fields.trace.cause
143     }
144
145     fn add_obligations(&mut self, obligations: Vec<PredicateObligation<'tcx>>) {
146         self.fields.obligations.extend(obligations)
147     }
148
149     fn relate_bound(&mut self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()> {
150         let mut sub = self.fields.sub(self.a_is_expected);
151         sub.relate(a, v)?;
152         sub.relate(b, v)?;
153         Ok(())
154     }
155
156     fn define_opaque_types(&self) -> bool {
157         self.fields.define_opaque_types
158     }
159 }