1 //! Greatest lower bound. See [`lattice`].
3 use super::combine::CombineFields;
4 use super::lattice::{self, LatticeDir};
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};
13 /// "Greatest lower bound" (common subtype)
14 pub struct Glb<'combine, 'infcx, 'tcx> {
15 fields: &'combine mut CombineFields<'infcx, 'tcx>,
19 impl<'combine, 'infcx, 'tcx> Glb<'combine, 'infcx, 'tcx> {
21 fields: &'combine mut CombineFields<'infcx, 'tcx>,
23 ) -> Glb<'combine, 'infcx, 'tcx> {
24 Glb { fields, a_is_expected }
28 impl<'tcx> TypeRelation<'tcx> for Glb<'_, '_, 'tcx> {
29 fn tag(&self) -> &'static str {
33 fn intercrate(&self) -> bool {
34 assert!(!self.fields.infcx.intercrate);
38 fn tcx(&self) -> TyCtxt<'tcx> {
42 fn param_env(&self) -> ty::ParamEnv<'tcx> {
46 fn a_is_expected(&self) -> bool {
50 fn mark_ambiguous(&mut self) {
51 bug!("mark_ambiguous used outside of coherence");
54 fn relate_with_variance<T: Relate<'tcx>>(
56 variance: ty::Variance,
57 _info: ty::VarianceDiagInfo<'tcx>,
60 ) -> RelateResult<'tcx, T> {
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.lub(self.a_is_expected).relate(a, b),
70 fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
71 lattice::super_lattice_tys(self, a, b)
78 ) -> RelateResult<'tcx, ty::Region<'tcx>> {
79 debug!("{}.regions({:?}, {:?})", self.tag(), a, b);
81 let origin = Subtype(Box::new(self.fields.trace.clone()));
82 Ok(self.fields.infcx.inner.borrow_mut().unwrap_region_constraints().glb_regions(
94 ) -> RelateResult<'tcx, ty::Const<'tcx>> {
95 self.fields.infcx.super_combine_consts(self, a, b)
100 a: ty::Binder<'tcx, T>,
101 b: ty::Binder<'tcx, T>,
102 ) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
106 // GLB of a binder and itself is just itself
111 debug!("binders(a={:?}, b={:?})", a, b);
112 if a.skip_binder().has_escaping_bound_vars() || b.skip_binder().has_escaping_bound_vars() {
113 // When higher-ranked types are involved, computing the GLB is
114 // very challenging, switch to invariance. This is obviously
115 // overly conservative but works ok in practice.
116 self.relate_with_variance(
117 ty::Variance::Invariant,
118 ty::VarianceDiagInfo::default(),
124 Ok(ty::Binder::dummy(self.relate(a.skip_binder(), b.skip_binder())?))
129 impl<'combine, 'infcx, 'tcx> LatticeDir<'infcx, 'tcx> for Glb<'combine, 'infcx, 'tcx> {
130 fn infcx(&self) -> &'infcx InferCtxt<'tcx> {
134 fn cause(&self) -> &ObligationCause<'tcx> {
135 &self.fields.trace.cause
138 fn add_obligations(&mut self, obligations: Vec<PredicateObligation<'tcx>>) {
139 self.fields.obligations.extend(obligations)
142 fn relate_bound(&mut self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()> {
143 let mut sub = self.fields.sub(self.a_is_expected);
149 fn define_opaque_types(&self) -> bool {
150 self.fields.define_opaque_types
154 impl<'tcx> ConstEquateRelation<'tcx> for Glb<'_, '_, 'tcx> {
155 fn const_equate_obligation(&mut self, a: ty::Const<'tcx>, b: ty::Const<'tcx>) {
156 self.fields.add_const_equate_obligation(self.a_is_expected, a, b);