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))
134 &ty::Alias(ty::Opaque, ty::AliasTy { def_id: a_def_id, substs: _ }),
135 &ty::Alias(ty::Opaque, ty::AliasTy { def_id: b_def_id, substs: _ }),
136 ) if a_def_id == b_def_id => {
137 self.fields.infcx.super_combine_tys(self, a, b)?;
140 (&ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs: _ }), _)
141 | (_, &ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs: _ }))
142 if self.fields.define_opaque_types && def_id.is_local() =>
144 self.fields.obligations.extend(
150 &self.fields.trace.cause,
157 // Optimization of GeneratorWitness relation since we know that all
158 // free regions are replaced with bound regions during construction.
159 // This greatly speeds up subtyping of GeneratorWitness.
160 (&ty::GeneratorWitness(a_types), &ty::GeneratorWitness(b_types)) => {
161 let a_types = infcx.tcx.anonymize_bound_vars(a_types);
162 let b_types = infcx.tcx.anonymize_bound_vars(b_types);
163 if a_types.bound_vars() == b_types.bound_vars() {
164 let (a_types, b_types) = infcx.replace_bound_vars_with_placeholders(
165 a_types.map_bound(|a_types| (a_types, b_types.skip_binder())),
167 for (a, b) in std::iter::zip(a_types, b_types) {
172 Err(ty::error::TypeError::Sorts(ty::relate::expected_found(self, a, b)))
177 self.fields.infcx.super_combine_tys(self, a, b)?;
187 ) -> RelateResult<'tcx, ty::Region<'tcx>> {
188 debug!("{}.regions({:?}, {:?}) self.cause={:?}", self.tag(), a, b, self.fields.cause);
190 // FIXME -- we have more fine-grained information available
191 // from the "cause" field, we could perhaps give more tailored
193 let origin = SubregionOrigin::Subtype(Box::new(self.fields.trace.clone()));
198 .unwrap_region_constraints()
199 .make_subregion(origin, a, b);
208 ) -> RelateResult<'tcx, ty::Const<'tcx>> {
209 self.fields.infcx.super_combine_consts(self, a, b)
214 a: ty::Binder<'tcx, T>,
215 b: ty::Binder<'tcx, T>,
216 ) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
220 // A binder is always a subtype of itself if it's structually equal to itself
225 self.fields.higher_ranked_sub(a, b, self.a_is_expected)?;
230 impl<'tcx> ConstEquateRelation<'tcx> for Sub<'_, '_, 'tcx> {
231 fn const_equate_obligation(&mut self, a: ty::Const<'tcx>, b: ty::Const<'tcx>) {
232 self.fields.add_const_equate_obligation(self.a_is_expected, a, b);