]> git.proxmox.com Git - rustc.git/blob - src/librustc/infer/higher_ranked/mod.rs
New upstream version 1.34.2+dfsg1
[rustc.git] / src / librustc / 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 crate::ty::relate::{Relate, RelateResult, TypeRelation};
9 use crate::ty::{self, Binder, TypeFoldable};
10
11 impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, '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 return 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, placeholder_map) = 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
44 .replace_bound_vars_with_fresh_vars(span, HigherRankedType, a);
45
46 debug!("a_prime={:?}", a_prime);
47 debug!("b_prime={:?}", b_prime);
48
49 // Compare types now that bound regions have been replaced.
50 let result = self.sub(a_is_expected).relate(&a_prime, &b_prime)?;
51
52 self.infcx
53 .leak_check(!a_is_expected, &placeholder_map, snapshot)?;
54
55 debug!("higher_ranked_sub: OK result={:?}", result);
56
57 Ok(ty::Binder::bind(result))
58 });
59 }
60 }
61
62 impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
63 /// Replaces all regions (resp. types) bound by `binder` with placeholder
64 /// regions (resp. types) and return a map indicating which bound-region
65 /// placeholder region. This is the first step of checking subtyping
66 /// when higher-ranked things are involved.
67 ///
68 /// **Important:** you must call this function from within a snapshot.
69 /// Moreover, before committing the snapshot, you must eventually call
70 /// either `plug_leaks` or `pop_placeholders` to remove the placeholder
71 /// regions. If you rollback the snapshot (or are using a probe), then
72 /// the pop occurs as part of the rollback, so an explicit call is not
73 /// needed (but is also permitted).
74 ///
75 /// For more information about how placeholders and HRTBs work, see
76 /// the [rustc guide].
77 ///
78 /// [rustc guide]: https://rust-lang.github.io/rustc-guide/traits/hrtb.html
79 pub fn replace_bound_vars_with_placeholders<T>(
80 &self,
81 binder: &ty::Binder<T>,
82 ) -> (T, PlaceholderMap<'tcx>)
83 where
84 T: TypeFoldable<'tcx>,
85 {
86 let next_universe = self.create_next_universe();
87
88 let fld_r = |br| {
89 self.tcx.mk_region(ty::RePlaceholder(ty::PlaceholderRegion {
90 universe: next_universe,
91 name: br,
92 }))
93 };
94
95 let fld_t = |bound_ty: ty::BoundTy| {
96 self.tcx.mk_ty(ty::Placeholder(ty::PlaceholderType {
97 universe: next_universe,
98 name: bound_ty.var,
99 }))
100 };
101
102 let (result, map) = self.tcx.replace_bound_vars(binder, fld_r, fld_t);
103
104 debug!(
105 "replace_bound_vars_with_placeholders(\
106 next_universe={:?}, \
107 binder={:?}, \
108 result={:?}, \
109 map={:?})",
110 next_universe, binder, result, map,
111 );
112
113 (result, map)
114 }
115
116 /// See `infer::region_constraints::RegionConstraintCollector::leak_check`.
117 pub fn leak_check(
118 &self,
119 overly_polymorphic: bool,
120 placeholder_map: &PlaceholderMap<'tcx>,
121 snapshot: &CombinedSnapshot<'_, 'tcx>,
122 ) -> RelateResult<'tcx, ()> {
123 self.borrow_region_constraints()
124 .leak_check(self.tcx, overly_polymorphic, placeholder_map, snapshot)
125 }
126 }