]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_infer/src/infer/sub.rs
Rollup merge of #88136 - spastorino:fix-test-directory, r=oli-obk
[rust.git] / compiler / rustc_infer / src / infer / sub.rs
1 use super::combine::{CombineFields, RelationDir};
2 use super::SubregionOrigin;
3
4 use crate::infer::combine::ConstEquateRelation;
5 use crate::traits::Obligation;
6 use rustc_middle::ty::fold::TypeFoldable;
7 use rustc_middle::ty::relate::{Cause, Relate, RelateResult, TypeRelation};
8 use rustc_middle::ty::TyVar;
9 use rustc_middle::ty::{self, ToPredicate, Ty, TyCtxt};
10 use std::mem;
11
12 /// Ensures `a` is made a subtype of `b`. Returns `a` on success.
13 pub struct Sub<'combine, 'infcx, 'tcx> {
14     fields: &'combine mut CombineFields<'infcx, 'tcx>,
15     a_is_expected: bool,
16 }
17
18 impl<'combine, 'infcx, 'tcx> Sub<'combine, 'infcx, 'tcx> {
19     pub fn new(
20         f: &'combine mut CombineFields<'infcx, 'tcx>,
21         a_is_expected: bool,
22     ) -> Sub<'combine, 'infcx, 'tcx> {
23         Sub { fields: f, a_is_expected }
24     }
25
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;
28         let result = f(self);
29         self.a_is_expected = !self.a_is_expected;
30         result
31     }
32 }
33
34 impl TypeRelation<'tcx> for Sub<'combine, 'infcx, 'tcx> {
35     fn tag(&self) -> &'static str {
36         "Sub"
37     }
38     fn tcx(&self) -> TyCtxt<'tcx> {
39         self.fields.infcx.tcx
40     }
41
42     fn param_env(&self) -> ty::ParamEnv<'tcx> {
43         self.fields.param_env
44     }
45
46     fn a_is_expected(&self) -> bool {
47         self.a_is_expected
48     }
49
50     fn with_cause<F, R>(&mut self, cause: Cause, f: F) -> R
51     where
52         F: FnOnce(&mut Self) -> R,
53     {
54         debug!("sub with_cause={:?}", cause);
55         let old_cause = mem::replace(&mut self.fields.cause, Some(cause));
56         let r = f(self);
57         debug!("sub old_cause={:?}", old_cause);
58         self.fields.cause = old_cause;
59         r
60     }
61
62     fn relate_with_variance<T: Relate<'tcx>>(
63         &mut self,
64         variance: ty::Variance,
65         _info: ty::VarianceDiagInfo<'tcx>,
66         a: T,
67         b: T,
68     ) -> RelateResult<'tcx, T> {
69         match variance {
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)),
74         }
75     }
76
77     fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
78         debug!("{}.tys({:?}, {:?})", self.tag(), a, b);
79
80         if a == b {
81             return Ok(a);
82         }
83
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);
87         match (a.kind(), b.kind()) {
88             (&ty::Infer(TyVar(a_vid)), &ty::Infer(TyVar(b_vid))) => {
89                 // Shouldn't have any LBR here, so we can safely put
90                 // this under a binder below without fear of accidental
91                 // capture.
92                 assert!(!a.has_escaping_bound_vars());
93                 assert!(!b.has_escaping_bound_vars());
94
95                 // can't make progress on `A <: B` if both A and B are
96                 // type variables, so record an obligation. We also
97                 // have to record in the `type_variables` tracker that
98                 // the two variables are equal modulo subtyping, which
99                 // is important to the occurs check later on.
100                 infcx.inner.borrow_mut().type_variables().sub(a_vid, b_vid);
101                 self.fields.obligations.push(Obligation::new(
102                     self.fields.trace.cause.clone(),
103                     self.fields.param_env,
104                     ty::PredicateKind::Subtype(ty::SubtypePredicate {
105                         a_is_expected: self.a_is_expected,
106                         a,
107                         b,
108                     })
109                     .to_predicate(self.tcx()),
110                 ));
111
112                 Ok(a)
113             }
114             (&ty::Infer(TyVar(a_id)), _) => {
115                 self.fields.instantiate(b, RelationDir::SupertypeOf, a_id, !self.a_is_expected)?;
116                 Ok(a)
117             }
118             (_, &ty::Infer(TyVar(b_id))) => {
119                 self.fields.instantiate(a, RelationDir::SubtypeOf, b_id, self.a_is_expected)?;
120                 Ok(a)
121             }
122
123             (&ty::Error(_), _) | (_, &ty::Error(_)) => {
124                 infcx.set_tainted_by_errors();
125                 Ok(self.tcx().ty_error())
126             }
127
128             _ => {
129                 self.fields.infcx.super_combine_tys(self, a, b)?;
130                 Ok(a)
131             }
132         }
133     }
134
135     fn regions(
136         &mut self,
137         a: ty::Region<'tcx>,
138         b: ty::Region<'tcx>,
139     ) -> RelateResult<'tcx, ty::Region<'tcx>> {
140         debug!("{}.regions({:?}, {:?}) self.cause={:?}", self.tag(), a, b, self.fields.cause);
141
142         // FIXME -- we have more fine-grained information available
143         // from the "cause" field, we could perhaps give more tailored
144         // error messages.
145         let origin = SubregionOrigin::Subtype(Box::new(self.fields.trace.clone()));
146         self.fields
147             .infcx
148             .inner
149             .borrow_mut()
150             .unwrap_region_constraints()
151             .make_subregion(origin, a, b);
152
153         Ok(a)
154     }
155
156     fn consts(
157         &mut self,
158         a: &'tcx ty::Const<'tcx>,
159         b: &'tcx ty::Const<'tcx>,
160     ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
161         self.fields.infcx.super_combine_consts(self, a, b)
162     }
163
164     fn binders<T>(
165         &mut self,
166         a: ty::Binder<'tcx, T>,
167         b: ty::Binder<'tcx, T>,
168     ) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
169     where
170         T: Relate<'tcx>,
171     {
172         self.fields.higher_ranked_sub(a, b, self.a_is_expected)
173     }
174 }
175
176 impl<'tcx> ConstEquateRelation<'tcx> for Sub<'_, '_, 'tcx> {
177     fn const_equate_obligation(&mut self, a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>) {
178         self.fields.add_const_equate_obligation(self.a_is_expected, a, b);
179     }
180 }