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, 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 {
39 fn intercrate(&self) -> bool {
40 self.fields.infcx.intercrate
43 fn tcx(&self) -> TyCtxt<'tcx> {
47 fn param_env(&self) -> ty::ParamEnv<'tcx> {
51 fn a_is_expected(&self) -> bool {
55 fn mark_ambiguous(&mut self) {
56 self.fields.mark_ambiguous()
59 fn with_cause<F, R>(&mut self, cause: Cause, f: F) -> R
61 F: FnOnce(&mut Self) -> R,
63 debug!("sub with_cause={:?}", cause);
64 let old_cause = mem::replace(&mut self.fields.cause, Some(cause));
66 debug!("sub old_cause={:?}", old_cause);
67 self.fields.cause = old_cause;
71 fn relate_with_variance<T: Relate<'tcx>>(
73 variance: ty::Variance,
74 _info: ty::VarianceDiagInfo<'tcx>,
77 ) -> RelateResult<'tcx, T> {
79 ty::Invariant => self.fields.equate(self.a_is_expected).relate(a, b),
80 ty::Covariant => self.relate(a, b),
81 ty::Bivariant => Ok(a),
82 ty::Contravariant => self.with_expected_switched(|this| this.relate(b, a)),
86 #[instrument(skip(self), level = "debug")]
87 fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
92 let infcx = self.fields.infcx;
93 let a = infcx.inner.borrow_mut().type_variables().replace_if_possible(a);
94 let b = infcx.inner.borrow_mut().type_variables().replace_if_possible(b);
96 match (a.kind(), b.kind()) {
97 (&ty::Infer(TyVar(_)), &ty::Infer(TyVar(_))) => {
98 // Shouldn't have any LBR here, so we can safely put
99 // this under a binder below without fear of accidental
101 assert!(!a.has_escaping_bound_vars());
102 assert!(!b.has_escaping_bound_vars());
104 // can't make progress on `A <: B` if both A and B are
105 // type variables, so record an obligation.
106 self.fields.obligations.push(Obligation::new(
108 self.fields.trace.cause.clone(),
109 self.fields.param_env,
110 ty::Binder::dummy(ty::PredicateKind::Subtype(ty::SubtypePredicate {
111 a_is_expected: self.a_is_expected,
119 (&ty::Infer(TyVar(a_id)), _) => {
120 self.fields.instantiate(b, RelationDir::SupertypeOf, a_id, !self.a_is_expected)?;
123 (_, &ty::Infer(TyVar(b_id))) => {
124 self.fields.instantiate(a, RelationDir::SubtypeOf, b_id, self.a_is_expected)?;
128 (&ty::Error(e), _) | (_, &ty::Error(e)) => {
129 infcx.set_tainted_by_errors(e);
130 Ok(self.tcx().ty_error_with_guaranteed(e))
133 (&ty::Opaque(a_def_id, _), &ty::Opaque(b_def_id, _)) if a_def_id == b_def_id => {
134 self.fields.infcx.super_combine_tys(self, a, b)?;
137 (&ty::Opaque(did, ..), _) | (_, &ty::Opaque(did, ..))
138 if self.fields.define_opaque_types && did.is_local() =>
140 self.fields.obligations.extend(
146 &self.fields.trace.cause,
153 // Optimization of GeneratorWitness relation since we know that all
154 // free regions are replaced with bound regions during construction.
155 // This greatly speeds up subtyping of GeneratorWitness.
156 (&ty::GeneratorWitness(a_types), &ty::GeneratorWitness(b_types)) => {
157 let a_types = infcx.tcx.anonymize_bound_vars(a_types);
158 let b_types = infcx.tcx.anonymize_bound_vars(b_types);
159 if a_types.bound_vars() == b_types.bound_vars() {
160 let (a_types, b_types) = infcx.replace_bound_vars_with_placeholders(
161 a_types.map_bound(|a_types| (a_types, b_types.skip_binder())),
163 for (a, b) in std::iter::zip(a_types, b_types) {
168 Err(ty::error::TypeError::Sorts(ty::relate::expected_found(self, a, b)))
173 self.fields.infcx.super_combine_tys(self, a, b)?;
183 ) -> RelateResult<'tcx, ty::Region<'tcx>> {
184 debug!("{}.regions({:?}, {:?}) self.cause={:?}", self.tag(), a, b, self.fields.cause);
186 // FIXME -- we have more fine-grained information available
187 // from the "cause" field, we could perhaps give more tailored
189 let origin = SubregionOrigin::Subtype(Box::new(self.fields.trace.clone()));
194 .unwrap_region_constraints()
195 .make_subregion(origin, a, b);
204 ) -> RelateResult<'tcx, ty::Const<'tcx>> {
205 self.fields.infcx.super_combine_consts(self, a, b)
210 a: ty::Binder<'tcx, T>,
211 b: ty::Binder<'tcx, T>,
212 ) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
216 self.fields.higher_ranked_sub(a, b, self.a_is_expected)?;
221 impl<'tcx> ConstEquateRelation<'tcx> for Sub<'_, '_, 'tcx> {
222 fn const_equate_obligation(&mut self, a: ty::Const<'tcx>, b: ty::Const<'tcx>) {
223 self.fields.add_const_equate_obligation(self.a_is_expected, a, b);