1 use super::combine::{CombineFields, RelationDir};
2 use super::SubregionOrigin;
4 use crate::infer::combine::ConstEquateRelation;
5 use crate::traits::Obligation;
6 use rustc_middle::ty::fold::TypeFoldable;
7 use rustc_middle::ty::relate::{Cause, Relate, RelateResult, TypeRelation};
8 use rustc_middle::ty::TyVar;
9 use rustc_middle::ty::{self, ToPredicate, Ty, TyCtxt};
12 /// Ensures `a` is made a subtype of `b`. Returns `a` on success.
13 pub struct Sub<'combine, 'infcx, 'tcx> {
14 fields: &'combine mut CombineFields<'infcx, 'tcx>,
18 impl<'combine, 'infcx, 'tcx> Sub<'combine, 'infcx, 'tcx> {
20 f: &'combine mut CombineFields<'infcx, 'tcx>,
22 ) -> Sub<'combine, 'infcx, 'tcx> {
23 Sub { fields: f, a_is_expected }
26 fn with_expected_switched<R, F: FnOnce(&mut Self) -> R>(&mut self, f: F) -> R {
27 self.a_is_expected = !self.a_is_expected;
29 self.a_is_expected = !self.a_is_expected;
34 impl TypeRelation<'tcx> for Sub<'combine, 'infcx, 'tcx> {
35 fn tag(&self) -> &'static str {
38 fn tcx(&self) -> TyCtxt<'tcx> {
42 fn param_env(&self) -> ty::ParamEnv<'tcx> {
46 fn a_is_expected(&self) -> bool {
50 fn with_cause<F, R>(&mut self, cause: Cause, f: F) -> R
52 F: FnOnce(&mut Self) -> R,
54 debug!("sub with_cause={:?}", cause);
55 let old_cause = mem::replace(&mut self.fields.cause, Some(cause));
57 debug!("sub old_cause={:?}", old_cause);
58 self.fields.cause = old_cause;
62 fn relate_with_variance<T: Relate<'tcx>>(
64 variance: ty::Variance,
65 _info: ty::VarianceDiagInfo<'tcx>,
68 ) -> RelateResult<'tcx, T> {
70 ty::Invariant => self.fields.equate(self.a_is_expected).relate(a, b),
71 ty::Covariant => self.relate(a, b),
72 ty::Bivariant => Ok(a),
73 ty::Contravariant => self.with_expected_switched(|this| this.relate(b, a)),
77 fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
78 debug!("{}.tys({:?}, {:?})", self.tag(), a, b);
84 let infcx = self.fields.infcx;
85 let a = infcx.inner.borrow_mut().type_variables().replace_if_possible(a);
86 let b = infcx.inner.borrow_mut().type_variables().replace_if_possible(b);
87 match (a.kind(), b.kind()) {
88 (&ty::Infer(TyVar(a_vid)), &ty::Infer(TyVar(b_vid))) => {
89 // Shouldn't have any LBR here, so we can safely put
90 // this under a binder below without fear of accidental
92 assert!(!a.has_escaping_bound_vars());
93 assert!(!b.has_escaping_bound_vars());
95 // can't make progress on `A <: B` if both A and B are
96 // type variables, so record an obligation. We also
97 // have to record in the `type_variables` tracker that
98 // the two variables are equal modulo subtyping, which
99 // is important to the occurs check later on.
100 infcx.inner.borrow_mut().type_variables().sub(a_vid, b_vid);
101 self.fields.obligations.push(Obligation::new(
102 self.fields.trace.cause.clone(),
103 self.fields.param_env,
104 ty::PredicateKind::Subtype(ty::SubtypePredicate {
105 a_is_expected: self.a_is_expected,
109 .to_predicate(self.tcx()),
114 (&ty::Infer(TyVar(a_id)), _) => {
115 self.fields.instantiate(b, RelationDir::SupertypeOf, a_id, !self.a_is_expected)?;
118 (_, &ty::Infer(TyVar(b_id))) => {
119 self.fields.instantiate(a, RelationDir::SubtypeOf, b_id, self.a_is_expected)?;
123 (&ty::Error(_), _) | (_, &ty::Error(_)) => {
124 infcx.set_tainted_by_errors();
125 Ok(self.tcx().ty_error())
129 self.fields.infcx.super_combine_tys(self, a, b)?;
139 ) -> RelateResult<'tcx, ty::Region<'tcx>> {
140 debug!("{}.regions({:?}, {:?}) self.cause={:?}", self.tag(), a, b, self.fields.cause);
142 // FIXME -- we have more fine-grained information available
143 // from the "cause" field, we could perhaps give more tailored
145 let origin = SubregionOrigin::Subtype(box self.fields.trace.clone());
150 .unwrap_region_constraints()
151 .make_subregion(origin, a, b);
158 a: &'tcx ty::Const<'tcx>,
159 b: &'tcx ty::Const<'tcx>,
160 ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
161 self.fields.infcx.super_combine_consts(self, a, b)
166 a: ty::Binder<'tcx, T>,
167 b: ty::Binder<'tcx, T>,
168 ) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
172 self.fields.higher_ranked_sub(a, b, self.a_is_expected)
176 impl<'tcx> ConstEquateRelation<'tcx> for Sub<'_, '_, 'tcx> {
177 fn const_equate_obligation(&mut self, a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>) {
178 self.fields.add_const_equate_obligation(self.a_is_expected, a, b);