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