]>
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, | |
17df50a5 | 46 | _info: ty::VarianceDiagInfo<'tcx>, |
f035d41b XL |
47 | a: T, |
48 | b: T, | |
dfeec247 | 49 | ) -> RelateResult<'tcx, T> { |
c34b1796 | 50 | match variance { |
5bcae85e | 51 | ty::Invariant => self.fields.equate(self.a_is_expected).relate(a, b), |
c34b1796 | 52 | ty::Covariant => self.relate(a, b), |
cc61c64b | 53 | // FIXME(#41044) -- not correct, need test |
1b1a35ee | 54 | ty::Bivariant => Ok(a), |
5bcae85e | 55 | ty::Contravariant => self.fields.glb(self.a_is_expected).relate(a, b), |
1a4d82fc JJ |
56 | } |
57 | } | |
58 | ||
c34b1796 AL |
59 | fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> { |
60 | lattice::super_lattice_tys(self, a, b) | |
1a4d82fc JJ |
61 | } |
62 | ||
dfeec247 XL |
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); | |
1a4d82fc | 69 | |
94222f64 | 70 | let origin = Subtype(Box::new(self.fields.trace.clone())); |
74b04a01 XL |
71 | Ok(self.fields.infcx.inner.borrow_mut().unwrap_region_constraints().lub_regions( |
72 | self.tcx(), | |
73 | origin, | |
74 | a, | |
75 | b, | |
76 | )) | |
1a4d82fc JJ |
77 | } |
78 | ||
48663c56 XL |
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>> { | |
48663c56 XL |
84 | self.fields.infcx.super_combine_consts(self, a, b) |
85 | } | |
86 | ||
dfeec247 XL |
87 | fn binders<T>( |
88 | &mut self, | |
cdc7bbd5 XL |
89 | a: ty::Binder<'tcx, T>, |
90 | b: ty::Binder<'tcx, T>, | |
91 | ) -> RelateResult<'tcx, ty::Binder<'tcx, T>> | |
dfeec247 XL |
92 | where |
93 | T: Relate<'tcx>, | |
c34b1796 | 94 | { |
abe05a73 | 95 | debug!("binders(a={:?}, b={:?})", a, b); |
abe05a73 XL |
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. | |
17df50a5 | 100 | self.relate_with_variance(ty::Variance::Invariant, ty::VarianceDiagInfo::default(), a, b)?; |
f035d41b | 101 | Ok(a) |
1a4d82fc | 102 | } |
c34b1796 | 103 | } |
1a4d82fc | 104 | |
f9f354fc XL |
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 | ||
dc9dc135 XL |
111 | impl<'combine, 'infcx, 'tcx> LatticeDir<'infcx, 'tcx> for Lub<'combine, 'infcx, 'tcx> { |
112 | fn infcx(&self) -> &'infcx InferCtxt<'infcx, 'tcx> { | |
c34b1796 AL |
113 | self.fields.infcx |
114 | } | |
115 | ||
476ff2be SL |
116 | fn cause(&self) -> &ObligationCause<'tcx> { |
117 | &self.fields.trace.cause | |
118 | } | |
119 | ||
5bcae85e SL |
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); | |
f035d41b XL |
122 | sub.relate(a, v)?; |
123 | sub.relate(b, v)?; | |
c34b1796 | 124 | Ok(()) |
1a4d82fc JJ |
125 | } |
126 | } |