]>
Commit | Line | Data |
---|---|---|
c34b1796 | 1 | use super::combine::CombineFields; |
c34b1796 | 2 | use super::lattice::{self, LatticeDir}; |
dfeec247 | 3 | use super::InferCtxt; |
c34b1796 | 4 | use super::Subtype; |
1a4d82fc | 5 | |
f9f354fc | 6 | use crate::infer::combine::ConstEquateRelation; |
9fa01778 | 7 | use crate::traits::ObligationCause; |
ba9703b0 XL |
8 | use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation}; |
9 | use rustc_middle::ty::{self, Ty, TyCtxt}; | |
1a4d82fc JJ |
10 | |
11 | /// "Least upper bound" (common supertype) | |
dc9dc135 XL |
12 | pub struct Lub<'combine, 'infcx, 'tcx> { |
13 | fields: &'combine mut CombineFields<'infcx, 'tcx>, | |
5bcae85e | 14 | a_is_expected: bool, |
1a4d82fc JJ |
15 | } |
16 | ||
dc9dc135 XL |
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> { | |
74b04a01 | 22 | Lub { fields, a_is_expected } |
54a0048b | 23 | } |
c34b1796 | 24 | } |
1a4d82fc | 25 | |
dc9dc135 | 26 | impl TypeRelation<'tcx> for Lub<'combine, 'infcx, 'tcx> { |
dfeec247 XL |
27 | fn tag(&self) -> &'static str { |
28 | "Lub" | |
29 | } | |
1a4d82fc | 30 | |
dfeec247 XL |
31 | fn tcx(&self) -> TyCtxt<'tcx> { |
32 | self.fields.tcx() | |
33 | } | |
1a4d82fc | 34 | |
dfeec247 XL |
35 | fn param_env(&self) -> ty::ParamEnv<'tcx> { |
36 | self.fields.param_env | |
37 | } | |
416331ca | 38 | |
dfeec247 XL |
39 | fn a_is_expected(&self) -> bool { |
40 | self.a_is_expected | |
41 | } | |
1a4d82fc | 42 | |
dfeec247 XL |
43 | fn relate_with_variance<T: Relate<'tcx>>( |
44 | &mut self, | |
45 | variance: ty::Variance, | |
f035d41b XL |
46 | a: T, |
47 | b: T, | |
dfeec247 | 48 | ) -> RelateResult<'tcx, T> { |
c34b1796 | 49 | match variance { |
5bcae85e | 50 | ty::Invariant => self.fields.equate(self.a_is_expected).relate(a, b), |
c34b1796 | 51 | ty::Covariant => self.relate(a, b), |
cc61c64b XL |
52 | // FIXME(#41044) -- not correct, need test |
53 | ty::Bivariant => Ok(a.clone()), | |
5bcae85e | 54 | ty::Contravariant => self.fields.glb(self.a_is_expected).relate(a, b), |
1a4d82fc JJ |
55 | } |
56 | } | |
57 | ||
c34b1796 AL |
58 | fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> { |
59 | lattice::super_lattice_tys(self, a, b) | |
1a4d82fc JJ |
60 | } |
61 | ||
dfeec247 XL |
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); | |
1a4d82fc | 68 | |
e1599b0c | 69 | let origin = Subtype(box self.fields.trace.clone()); |
74b04a01 XL |
70 | Ok(self.fields.infcx.inner.borrow_mut().unwrap_region_constraints().lub_regions( |
71 | self.tcx(), | |
72 | origin, | |
73 | a, | |
74 | b, | |
75 | )) | |
1a4d82fc JJ |
76 | } |
77 | ||
48663c56 XL |
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>> { | |
48663c56 XL |
83 | self.fields.infcx.super_combine_consts(self, a, b) |
84 | } | |
85 | ||
dfeec247 XL |
86 | fn binders<T>( |
87 | &mut self, | |
f035d41b XL |
88 | a: ty::Binder<T>, |
89 | b: ty::Binder<T>, | |
dfeec247 XL |
90 | ) -> RelateResult<'tcx, ty::Binder<T>> |
91 | where | |
92 | T: Relate<'tcx>, | |
c34b1796 | 93 | { |
abe05a73 | 94 | debug!("binders(a={:?}, b={:?})", a, b); |
abe05a73 XL |
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. | |
a1dfa0c6 | 99 | self.relate_with_variance(ty::Variance::Invariant, a, b)?; |
f035d41b | 100 | Ok(a) |
1a4d82fc | 101 | } |
c34b1796 | 102 | } |
1a4d82fc | 103 | |
f9f354fc XL |
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 | ||
dc9dc135 XL |
110 | impl<'combine, 'infcx, 'tcx> LatticeDir<'infcx, 'tcx> for Lub<'combine, 'infcx, 'tcx> { |
111 | fn infcx(&self) -> &'infcx InferCtxt<'infcx, 'tcx> { | |
c34b1796 AL |
112 | self.fields.infcx |
113 | } | |
114 | ||
476ff2be SL |
115 | fn cause(&self) -> &ObligationCause<'tcx> { |
116 | &self.fields.trace.cause | |
117 | } | |
118 | ||
5bcae85e SL |
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); | |
f035d41b XL |
121 | sub.relate(a, v)?; |
122 | sub.relate(b, v)?; | |
c34b1796 | 123 | Ok(()) |
1a4d82fc JJ |
124 | } |
125 | } |