1 use super::combine::{CombineFields, RelationDir};
2 use super::SubregionOrigin;
4 use crate::traits::Obligation;
5 use crate::ty::fold::TypeFoldable;
6 use crate::ty::relate::{Cause, Relate, RelateResult, TypeRelation};
8 use crate::ty::{self, Ty, TyCtxt};
11 /// Ensures `a` is made a subtype of `b`. Returns `a` on success.
12 pub struct Sub<'combine, 'infcx, 'tcx> {
13 fields: &'combine mut CombineFields<'infcx, 'tcx>,
17 impl<'combine, 'infcx, 'tcx> Sub<'combine, 'infcx, 'tcx> {
19 f: &'combine mut CombineFields<'infcx, 'tcx>,
21 ) -> Sub<'combine, 'infcx, 'tcx> {
22 Sub { fields: f, a_is_expected: a_is_expected }
25 fn with_expected_switched<R, F: FnOnce(&mut Self) -> R>(&mut self, f: F) -> R {
26 self.a_is_expected = !self.a_is_expected;
28 self.a_is_expected = !self.a_is_expected;
33 impl TypeRelation<'tcx> for Sub<'combine, 'infcx, 'tcx> {
34 fn tag(&self) -> &'static str {
37 fn tcx(&self) -> TyCtxt<'tcx> {
41 fn param_env(&self) -> ty::ParamEnv<'tcx> {
45 fn a_is_expected(&self) -> bool {
49 fn with_cause<F, R>(&mut self, cause: Cause, f: F) -> R
51 F: FnOnce(&mut Self) -> R,
53 debug!("sub with_cause={:?}", cause);
54 let old_cause = mem::replace(&mut self.fields.cause, Some(cause));
56 debug!("sub old_cause={:?}", old_cause);
57 self.fields.cause = old_cause;
61 fn relate_with_variance<T: Relate<'tcx>>(
63 variance: ty::Variance,
66 ) -> RelateResult<'tcx, T> {
68 ty::Invariant => self.fields.equate(self.a_is_expected).relate(a, b),
69 ty::Covariant => self.relate(a, b),
70 ty::Bivariant => Ok(a.clone()),
71 ty::Contravariant => self.with_expected_switched(|this| this.relate(b, a)),
75 fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
76 debug!("{}.tys({:?}, {:?})", self.tag(), a, b);
82 let infcx = self.fields.infcx;
83 let a = infcx.type_variables.borrow_mut().replace_if_possible(a);
84 let b = infcx.type_variables.borrow_mut().replace_if_possible(b);
85 match (&a.kind, &b.kind) {
86 (&ty::Infer(TyVar(a_vid)), &ty::Infer(TyVar(b_vid))) => {
87 // Shouldn't have any LBR here, so we can safely put
88 // this under a binder below without fear of accidental
90 assert!(!a.has_escaping_bound_vars());
91 assert!(!b.has_escaping_bound_vars());
93 // can't make progress on `A <: B` if both A and B are
94 // type variables, so record an obligation. We also
95 // have to record in the `type_variables` tracker that
96 // the two variables are equal modulo subtyping, which
97 // is important to the occurs check later on.
98 infcx.type_variables.borrow_mut().sub(a_vid, b_vid);
99 self.fields.obligations.push(Obligation::new(
100 self.fields.trace.cause.clone(),
101 self.fields.param_env,
102 ty::Predicate::Subtype(ty::Binder::dummy(ty::SubtypePredicate {
103 a_is_expected: self.a_is_expected,
111 (&ty::Infer(TyVar(a_id)), _) => {
112 self.fields.instantiate(b, RelationDir::SupertypeOf, a_id, !self.a_is_expected)?;
115 (_, &ty::Infer(TyVar(b_id))) => {
116 self.fields.instantiate(a, RelationDir::SubtypeOf, b_id, self.a_is_expected)?;
120 (&ty::Error, _) | (_, &ty::Error) => {
121 infcx.set_tainted_by_errors();
122 Ok(self.tcx().types.err)
126 self.fields.infcx.super_combine_tys(self, a, b)?;
136 ) -> RelateResult<'tcx, ty::Region<'tcx>> {
137 debug!("{}.regions({:?}, {:?}) self.cause={:?}", self.tag(), a, b, self.fields.cause);
139 // FIXME -- we have more fine-grained information available
140 // from the "cause" field, we could perhaps give more tailored
142 let origin = SubregionOrigin::Subtype(box self.fields.trace.clone());
143 self.fields.infcx.borrow_region_constraints().make_subregion(origin, a, b);
150 a: &'tcx ty::Const<'tcx>,
151 b: &'tcx ty::Const<'tcx>,
152 ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
153 self.fields.infcx.super_combine_consts(self, a, b)
160 ) -> RelateResult<'tcx, ty::Binder<T>>
164 self.fields.higher_ranked_sub(a, b, self.a_is_expected)