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