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::relate::{Cause, Relate, RelateResult, TypeRelation};
7 use rustc_middle::ty::visit::TypeVisitable;
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, 'a, 'tcx> {
14 fields: &'combine mut CombineFields<'a, '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<'tcx> TypeRelation<'tcx> for Sub<'_, '_, '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 #[instrument(skip(self), level = "debug")]
78 fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
83 let infcx = self.fields.infcx;
84 let a = infcx.inner.borrow_mut().type_variables().replace_if_possible(a);
85 let b = infcx.inner.borrow_mut().type_variables().replace_if_possible(b);
87 match (a.kind(), b.kind()) {
88 (&ty::Infer(TyVar(_)), &ty::Infer(TyVar(_))) => {
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.
97 self.fields.obligations.push(Obligation::new(
98 self.fields.trace.cause.clone(),
99 self.fields.param_env,
100 ty::Binder::dummy(ty::PredicateKind::Subtype(ty::SubtypePredicate {
101 a_is_expected: self.a_is_expected,
105 .to_predicate(self.tcx()),
110 (&ty::Infer(TyVar(a_id)), _) => {
111 self.fields.instantiate(b, RelationDir::SupertypeOf, a_id, !self.a_is_expected)?;
114 (_, &ty::Infer(TyVar(b_id))) => {
115 self.fields.instantiate(a, RelationDir::SubtypeOf, b_id, self.a_is_expected)?;
119 (&ty::Error(_), _) | (_, &ty::Error(_)) => {
120 infcx.set_tainted_by_errors();
121 Ok(self.tcx().ty_error())
124 (&ty::Opaque(a_def_id, _), &ty::Opaque(b_def_id, _)) if a_def_id == b_def_id => {
125 self.fields.infcx.super_combine_tys(self, a, b)?;
128 (&ty::Opaque(did, ..), _) | (_, &ty::Opaque(did, ..))
129 if self.fields.define_opaque_types && did.is_local() =>
131 self.fields.obligations.extend(
137 &self.fields.trace.cause,
144 // Optimization of GeneratorWitness relation since we know that all
145 // free regions are replaced with bound regions during construction.
146 // This greatly speeds up subtyping of GeneratorWitness.
147 (&ty::GeneratorWitness(a_types), &ty::GeneratorWitness(b_types)) => {
148 let a_types = infcx.tcx.anonymize_bound_vars(a_types);
149 let b_types = infcx.tcx.anonymize_bound_vars(b_types);
150 if a_types.bound_vars() == b_types.bound_vars() {
151 let (a_types, b_types) = infcx.replace_bound_vars_with_placeholders(
152 a_types.map_bound(|a_types| (a_types, b_types.skip_binder())),
154 for (a, b) in std::iter::zip(a_types, b_types) {
159 Err(ty::error::TypeError::Sorts(ty::relate::expected_found(self, a, b)))
164 self.fields.infcx.super_combine_tys(self, a, b)?;
174 ) -> RelateResult<'tcx, ty::Region<'tcx>> {
175 debug!("{}.regions({:?}, {:?}) self.cause={:?}", self.tag(), a, b, self.fields.cause);
177 // FIXME -- we have more fine-grained information available
178 // from the "cause" field, we could perhaps give more tailored
180 let origin = SubregionOrigin::Subtype(Box::new(self.fields.trace.clone()));
185 .unwrap_region_constraints()
186 .make_subregion(origin, a, b);
195 ) -> RelateResult<'tcx, ty::Const<'tcx>> {
196 self.fields.infcx.super_combine_consts(self, a, b)
201 a: ty::Binder<'tcx, T>,
202 b: ty::Binder<'tcx, T>,
203 ) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
207 self.fields.higher_ranked_sub(a, b, self.a_is_expected)?;
212 impl<'tcx> ConstEquateRelation<'tcx> for Sub<'_, '_, 'tcx> {
213 fn const_equate_obligation(&mut self, a: ty::Const<'tcx>, b: ty::Const<'tcx>) {
214 self.fields.add_const_equate_obligation(self.a_is_expected, a, b);