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