]> git.lizzy.rs Git - rust.git/blob - src/librustc_infer/infer/higher_ranked/mod.rs
b94221785ae755b4a15dd4d32239dab74b0207df
[rust.git] / src / librustc_infer / infer / higher_ranked / mod.rs
1 //! Helper routines for higher-ranked things. See the `doc` module at
2 //! the end of the file for details.
3
4 use super::combine::CombineFields;
5 use super::{HigherRankedType, InferCtxt, PlaceholderMap};
6
7 use crate::infer::CombinedSnapshot;
8 use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
9 use rustc_middle::ty::{self, Binder, TypeFoldable};
10
11 impl<'a, 'tcx> CombineFields<'a, 'tcx> {
12     pub fn higher_ranked_sub<T>(
13         &mut self,
14         a: &Binder<T>,
15         b: &Binder<T>,
16         a_is_expected: bool,
17     ) -> RelateResult<'tcx, Binder<T>>
18     where
19         T: Relate<'tcx>,
20     {
21         debug!("higher_ranked_sub(a={:?}, b={:?})", a, b);
22
23         // Rather than checking the subtype relationship between `a` and `b`
24         // as-is, we need to do some extra work here in order to make sure
25         // that function subtyping works correctly with respect to regions
26         //
27         // Note: this is a subtle algorithm.  For a full explanation,
28         // please see the large comment at the end of the file in the (inlined) module
29         // `doc`.
30
31         let span = self.trace.cause.span;
32
33         self.infcx.commit_if_ok(|snapshot| {
34             // First, we instantiate each bound region in the supertype with a
35             // fresh placeholder region.
36             let (b_prime, _) = self.infcx.replace_bound_vars_with_placeholders(b);
37
38             // Next, we instantiate each bound region in the subtype
39             // with a fresh region variable. These region variables --
40             // but no other pre-existing region variables -- can name
41             // the placeholders.
42             let (a_prime, _) =
43                 self.infcx.replace_bound_vars_with_fresh_vars(span, HigherRankedType, a);
44
45             debug!("a_prime={:?}", a_prime);
46             debug!("b_prime={:?}", b_prime);
47
48             // Compare types now that bound regions have been replaced.
49             let result = self.sub(a_is_expected).relate(&a_prime, &b_prime)?;
50
51             self.infcx.leak_check(!a_is_expected, snapshot)?;
52
53             debug!("higher_ranked_sub: OK result={:?}", result);
54
55             Ok(ty::Binder::bind(result))
56         })
57     }
58 }
59
60 impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
61     /// Replaces all regions (resp. types) bound by `binder` with placeholder
62     /// regions (resp. types) and return a map indicating which bound-region
63     /// placeholder region. This is the first step of checking subtyping
64     /// when higher-ranked things are involved.
65     ///
66     /// **Important:** You have to be careful to not leak these placeholders,
67     /// for more information about how placeholders and HRTBs work, see
68     /// the [rustc dev guide].
69     ///
70     /// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/hrtb.html
71     pub fn replace_bound_vars_with_placeholders<T>(
72         &self,
73         binder: &ty::Binder<T>,
74     ) -> (T, PlaceholderMap<'tcx>)
75     where
76         T: TypeFoldable<'tcx>,
77     {
78         let next_universe = self.create_next_universe();
79
80         let fld_r = |br| {
81             self.tcx.mk_region(ty::RePlaceholder(ty::PlaceholderRegion {
82                 universe: next_universe,
83                 name: br,
84             }))
85         };
86
87         let fld_t = |bound_ty: ty::BoundTy| {
88             self.tcx.mk_ty(ty::Placeholder(ty::PlaceholderType {
89                 universe: next_universe,
90                 name: bound_ty.var,
91             }))
92         };
93
94         let fld_c = |bound_var: ty::BoundVar, ty| {
95             self.tcx.mk_const(ty::Const {
96                 val: ty::ConstKind::Placeholder(ty::PlaceholderConst {
97                     universe: next_universe,
98                     name: bound_var,
99                 }),
100                 ty,
101             })
102         };
103
104         let (result, map) = self.tcx.replace_bound_vars(binder, fld_r, fld_t, fld_c);
105
106         debug!(
107             "replace_bound_vars_with_placeholders(\
108              next_universe={:?}, \
109              binder={:?}, \
110              result={:?}, \
111              map={:?})",
112             next_universe, binder, result, map,
113         );
114
115         (result, map)
116     }
117
118     /// See `infer::region_constraints::RegionConstraintCollector::leak_check`.
119     pub fn leak_check(
120         &self,
121         overly_polymorphic: bool,
122         snapshot: &CombinedSnapshot<'_, 'tcx>,
123     ) -> RelateResult<'tcx, ()> {
124         // If the user gave `-Zno-leak-check`, or we have been
125         // configured to skip the leak check, then skip the leak check
126         // completely. The leak check is deprecated. Any legitimate
127         // subtyping errors that it would have caught will now be
128         // caught later on, during region checking. However, we
129         // continue to use it for a transition period.
130         if self.tcx.sess.opts.debugging_opts.no_leak_check || self.skip_leak_check.get() {
131             return Ok(());
132         }
133
134         self.inner.borrow_mut().unwrap_region_constraints().leak_check(
135             self.tcx,
136             overly_polymorphic,
137             self.universe(),
138             snapshot,
139         )
140     }
141 }