1 use super::combine::{CombineFields, RelationDir};
2 use super::SubregionOrigin;
4 use crate::infer::combine::ConstEquateRelation;
5 use crate::infer::{TypeVariableOrigin, TypeVariableOriginKind};
6 use crate::traits::Obligation;
7 use rustc_middle::ty::relate::{Cause, Relate, RelateResult, TypeRelation};
8 use rustc_middle::ty::visit::TypeVisitable;
9 use rustc_middle::ty::TyVar;
10 use rustc_middle::ty::{self, ToPredicate, Ty, TyCtxt};
13 /// Ensures `a` is made a subtype of `b`. Returns `a` on success.
14 pub struct Sub<'combine, 'infcx, 'tcx> {
15 fields: &'combine mut CombineFields<'infcx, 'tcx>,
19 impl<'combine, 'infcx, 'tcx> Sub<'combine, 'infcx, 'tcx> {
21 f: &'combine mut CombineFields<'infcx, 'tcx>,
23 ) -> Sub<'combine, 'infcx, 'tcx> {
24 Sub { fields: f, a_is_expected }
27 fn with_expected_switched<R, F: FnOnce(&mut Self) -> R>(&mut self, f: F) -> R {
28 self.a_is_expected = !self.a_is_expected;
30 self.a_is_expected = !self.a_is_expected;
35 impl<'tcx> TypeRelation<'tcx> for Sub<'_, '_, 'tcx> {
36 fn tag(&self) -> &'static str {
39 fn tcx(&self) -> TyCtxt<'tcx> {
43 fn param_env(&self) -> ty::ParamEnv<'tcx> {
47 fn a_is_expected(&self) -> bool {
51 fn with_cause<F, R>(&mut self, cause: Cause, f: F) -> R
53 F: FnOnce(&mut Self) -> R,
55 debug!("sub with_cause={:?}", cause);
56 let old_cause = mem::replace(&mut self.fields.cause, Some(cause));
58 debug!("sub old_cause={:?}", old_cause);
59 self.fields.cause = old_cause;
63 fn relate_with_variance<T: Relate<'tcx>>(
65 variance: ty::Variance,
66 _info: ty::VarianceDiagInfo<'tcx>,
69 ) -> RelateResult<'tcx, T> {
71 ty::Invariant => self.fields.equate(self.a_is_expected).relate(a, b),
72 ty::Covariant => self.relate(a, b),
73 ty::Bivariant => Ok(a),
74 ty::Contravariant => self.with_expected_switched(|this| this.relate(b, a)),
78 #[instrument(skip(self), level = "debug")]
79 fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
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);
88 match (a.kind(), b.kind()) {
89 (&ty::Infer(TyVar(_)), &ty::Infer(TyVar(_))) => {
90 // Shouldn't have any LBR here, so we can safely put
91 // this under a binder below without fear of accidental
93 assert!(!a.has_escaping_bound_vars());
94 assert!(!b.has_escaping_bound_vars());
96 // can't make progress on `A <: B` if both A and B are
97 // type variables, so record an obligation.
98 self.fields.obligations.push(Obligation::new(
99 self.fields.trace.cause.clone(),
100 self.fields.param_env,
101 ty::Binder::dummy(ty::PredicateKind::Subtype(ty::SubtypePredicate {
102 a_is_expected: self.a_is_expected,
106 .to_predicate(self.tcx()),
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().ty_error())
125 (&ty::Opaque(a_def_id, _), &ty::Opaque(b_def_id, _)) if a_def_id == b_def_id => {
126 self.fields.infcx.super_combine_tys(self, a, b)?;
129 (&ty::Opaque(did, ..), _) | (_, &ty::Opaque(did, ..))
130 if self.fields.define_opaque_types && did.is_local() =>
132 let mut generalize = |ty, ty_is_expected| {
133 let var = infcx.next_ty_var_id_in_universe(
135 kind: TypeVariableOriginKind::MiscVariable,
136 span: self.fields.trace.cause.span,
138 ty::UniverseIndex::ROOT,
140 self.fields.instantiate(ty, RelationDir::SubtypeOf, var, ty_is_expected)?;
141 Ok(infcx.tcx.mk_ty_var(var))
143 let (a, b) = if self.a_is_expected { (a, b) } else { (b, a) };
144 let (a, b) = match (a.kind(), b.kind()) {
145 (&ty::Opaque(..), _) => (a, generalize(b, true)?),
146 (_, &ty::Opaque(..)) => (generalize(a, false)?, b),
149 self.fields.obligations.extend(
151 .handle_opaque_type(a, b, true, &self.fields.trace.cause, self.param_env())?
158 self.fields.infcx.super_combine_tys(self, a, b)?;
168 ) -> RelateResult<'tcx, ty::Region<'tcx>> {
169 debug!("{}.regions({:?}, {:?}) self.cause={:?}", self.tag(), a, b, self.fields.cause);
171 // FIXME -- we have more fine-grained information available
172 // from the "cause" field, we could perhaps give more tailored
174 let origin = SubregionOrigin::Subtype(Box::new(self.fields.trace.clone()));
179 .unwrap_region_constraints()
180 .make_subregion(origin, a, b);
189 ) -> RelateResult<'tcx, ty::Const<'tcx>> {
190 self.fields.infcx.super_combine_consts(self, a, b)
195 a: ty::Binder<'tcx, T>,
196 b: ty::Binder<'tcx, T>,
197 ) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
201 self.fields.higher_ranked_sub(a, b, self.a_is_expected)?;
206 impl<'tcx> ConstEquateRelation<'tcx> for Sub<'_, '_, 'tcx> {
207 fn const_equate_obligation(&mut self, a: ty::Const<'tcx>, b: ty::Const<'tcx>) {
208 self.fields.add_const_equate_obligation(self.a_is_expected, a, b);