]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_infer/src/infer/higher_ranked/mod.rs
New upstream version 1.48.0+dfsg1
[rustc.git] / compiler / rustc_infer / src / infer / higher_ranked / mod.rs
CommitLineData
1a4d82fc
JJ
1//! Helper routines for higher-ranked things. See the `doc` module at
2//! the end of the file for details.
3
c34b1796 4use super::combine::CombineFields;
0731742a 5use super::{HigherRankedType, InferCtxt, PlaceholderMap};
1a4d82fc 6
9fa01778 7use crate::infer::CombinedSnapshot;
ba9703b0
XL
8use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
9use rustc_middle::ty::{self, Binder, TypeFoldable};
3157f602 10
dc9dc135 11impl<'a, 'tcx> CombineFields<'a, 'tcx> {
0731742a
XL
12 pub fn higher_ranked_sub<T>(
13 &mut self,
f035d41b
XL
14 a: Binder<T>,
15 b: Binder<T>,
0731742a
XL
16 a_is_expected: bool,
17 ) -> RelateResult<'tcx, Binder<T>>
18 where
19 T: Relate<'tcx>,
1a4d82fc 20 {
0731742a 21 debug!("higher_ranked_sub(a={:?}, b={:?})", a, b);
1a4d82fc
JJ
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
0731742a 31 let span = self.trace.cause.span;
3157f602 32
f035d41b 33 self.infcx.commit_if_ok(|_| {
0bf4aa26
XL
34 // First, we instantiate each bound region in the supertype with a
35 // fresh placeholder region.
f035d41b 36 let (b_prime, _) = self.infcx.replace_bound_vars_with_placeholders(&b);
0bf4aa26
XL
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.
0731742a 42 let (a_prime, _) =
f035d41b 43 self.infcx.replace_bound_vars_with_fresh_vars(span, HigherRankedType, &a);
1a4d82fc 44
62682a34
SL
45 debug!("a_prime={:?}", a_prime);
46 debug!("b_prime={:?}", b_prime);
1a4d82fc
JJ
47
48 // Compare types now that bound regions have been replaced.
f035d41b 49 let result = self.sub(a_is_expected).relate(a_prime, b_prime)?;
1a4d82fc 50
a7813a04 51 debug!("higher_ranked_sub: OK result={:?}", result);
1a4d82fc 52
83c7162d 53 Ok(ty::Binder::bind(result))
ba9703b0 54 })
1a4d82fc 55 }
1a4d82fc
JJ
56}
57
dc9dc135 58impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
9fa01778 59 /// Replaces all regions (resp. types) bound by `binder` with placeholder
a1dfa0c6 60 /// regions (resp. types) and return a map indicating which bound-region
0731742a
XL
61 /// placeholder region. This is the first step of checking subtyping
62 /// when higher-ranked things are involved.
3157f602 63 ///
f035d41b
XL
64 /// **Important:** You have to be careful to not leak these placeholders,
65 /// for more information about how placeholders and HRTBs work, see
ba9703b0 66 /// the [rustc dev guide].
0531ce1d 67 ///
ba9703b0 68 /// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/hrtb.html
a1dfa0c6 69 pub fn replace_bound_vars_with_placeholders<T>(
0bf4aa26 70 &self,
0731742a 71 binder: &ty::Binder<T>,
0bf4aa26
XL
72 ) -> (T, PlaceholderMap<'tcx>)
73 where
0731742a 74 T: TypeFoldable<'tcx>,
a7813a04 75 {
f035d41b
XL
76 // Figure out what the next universe will be, but don't actually create
77 // it until after we've done the substitution (in particular there may
78 // be no bound variables). This is a performance optimization, since the
79 // leak check for example can be skipped if no new universes are created
80 // (i.e., if there are no placeholders).
81 let next_universe = self.universe().next_universe();
0bf4aa26 82
a1dfa0c6
XL
83 let fld_r = |br| {
84 self.tcx.mk_region(ty::RePlaceholder(ty::PlaceholderRegion {
0bf4aa26
XL
85 universe: next_universe,
86 name: br,
87 }))
a1dfa0c6
XL
88 };
89
90 let fld_t = |bound_ty: ty::BoundTy| {
91 self.tcx.mk_ty(ty::Placeholder(ty::PlaceholderType {
92 universe: next_universe,
93 name: bound_ty.var,
94 }))
95 };
1a4d82fc 96
48663c56 97 let fld_c = |bound_var: ty::BoundVar, ty| {
dfeec247
XL
98 self.tcx.mk_const(ty::Const {
99 val: ty::ConstKind::Placeholder(ty::PlaceholderConst {
100 universe: next_universe,
101 name: bound_var,
102 }),
103 ty,
104 })
48663c56
XL
105 };
106
107 let (result, map) = self.tcx.replace_bound_vars(binder, fld_r, fld_t, fld_c);
a1dfa0c6 108
f035d41b
XL
109 // If there were higher-ranked regions to replace, then actually create
110 // the next universe (this avoids needlessly creating universes).
111 if !map.is_empty() {
112 let n_u = self.create_next_universe();
113 assert_eq!(n_u, next_universe);
114 }
115
a1dfa0c6 116 debug!(
0731742a
XL
117 "replace_bound_vars_with_placeholders(\
118 next_universe={:?}, \
119 binder={:?}, \
120 result={:?}, \
121 map={:?})",
9fa01778 122 next_universe, binder, result, map,
a1dfa0c6 123 );
1a4d82fc 124
a7813a04 125 (result, map)
1a4d82fc 126 }
1a4d82fc 127
0731742a
XL
128 /// See `infer::region_constraints::RegionConstraintCollector::leak_check`.
129 pub fn leak_check(
0bf4aa26 130 &self,
0731742a 131 overly_polymorphic: bool,
0731742a
XL
132 snapshot: &CombinedSnapshot<'_, 'tcx>,
133 ) -> RelateResult<'tcx, ()> {
74b04a01
XL
134 // If the user gave `-Zno-leak-check`, or we have been
135 // configured to skip the leak check, then skip the leak check
136 // completely. The leak check is deprecated. Any legitimate
137 // subtyping errors that it would have caught will now be
138 // caught later on, during region checking. However, we
139 // continue to use it for a transition period.
140 if self.tcx.sess.opts.debugging_opts.no_leak_check || self.skip_leak_check.get() {
141 return Ok(());
142 }
143
144 self.inner.borrow_mut().unwrap_region_constraints().leak_check(
dfeec247
XL
145 self.tcx,
146 overly_polymorphic,
f035d41b 147 self.universe(),
dfeec247
XL
148 snapshot,
149 )
3157f602 150 }
1a4d82fc 151}