]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_infer/src/infer/higher_ranked/mod.rs
New upstream version 1.72.1+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};
9fa01778 6use crate::infer::CombinedSnapshot;
064997fb 7use rustc_middle::ty::fold::FnMutDelegate;
ba9703b0 8use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
fe692bf9 9use rustc_middle::ty::{self, Binder, Ty, TyCtxt, TypeFoldable};
3157f602 10
dc9dc135 11impl<'a, 'tcx> CombineFields<'a, 'tcx> {
923072b8
FG
12 /// Checks whether `for<..> sub <: for<..> sup` holds.
13 ///
14 /// For this to hold, **all** instantiations of the super type
15 /// have to be a super type of **at least one** instantiation of
16 /// the subtype.
17 ///
18 /// This is implemented by first entering a new universe.
19 /// We then replace all bound variables in `sup` with placeholders,
064997fb 20 /// and all bound variables in `sub` with inference vars.
923072b8
FG
21 /// We can then just relate the two resulting types as normal.
22 ///
23 /// Note: this is a subtle algorithm. For a full explanation, please see
24 /// the [rustc dev guide][rd]
25 ///
26 /// [rd]: https://rustc-dev-guide.rust-lang.org/borrow_check/region_inference/placeholders_and_universes.html
c295e0f8 27 #[instrument(skip(self), level = "debug")]
0731742a
XL
28 pub fn higher_ranked_sub<T>(
29 &mut self,
923072b8
FG
30 sub: Binder<'tcx, T>,
31 sup: Binder<'tcx, T>,
32 sub_is_expected: bool,
33 ) -> RelateResult<'tcx, ()>
0731742a
XL
34 where
35 T: Relate<'tcx>,
1a4d82fc 36 {
0731742a 37 let span = self.trace.cause.span;
064997fb
FG
38 // First, we instantiate each bound region in the supertype with a
39 // fresh placeholder region. Note that this automatically creates
40 // a new universe if needed.
9ffffee4 41 let sup_prime = self.infcx.instantiate_binder_with_placeholders(sup);
3157f602 42
064997fb
FG
43 // Next, we instantiate each bound region in the subtype
44 // with a fresh region variable. These region variables --
49aad941 45 // but no other preexisting region variables -- can name
064997fb 46 // the placeholders.
9ffffee4 47 let sub_prime = self.infcx.instantiate_binder_with_fresh_vars(span, HigherRankedType, sub);
0bf4aa26 48
064997fb
FG
49 debug!("a_prime={:?}", sub_prime);
50 debug!("b_prime={:?}", sup_prime);
1a4d82fc 51
064997fb
FG
52 // Compare types now that bound regions have been replaced.
53 let result = self.sub(sub_is_expected).relate(sub_prime, sup_prime)?;
1a4d82fc 54
064997fb
FG
55 debug!("OK result={result:?}");
56 // NOTE: returning the result here would be dangerous as it contains
57 // placeholders which **must not** be named afterwards.
58 Ok(())
1a4d82fc 59 }
1a4d82fc
JJ
60}
61
2b03887a 62impl<'tcx> InferCtxt<'tcx> {
5e7ed085 63 /// Replaces all bound variables (lifetimes, types, and constants) bound by
923072b8
FG
64 /// `binder` with placeholder variables in a new universe. This means that the
65 /// new placeholders can only be named by inference variables created after
66 /// this method has been called.
3157f602 67 ///
5e7ed085
FG
68 /// This is the first step of checking subtyping when higher-ranked things are involved.
69 /// For more details visit the relevant sections of the [rustc dev guide].
0531ce1d 70 ///
ba9703b0 71 /// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/hrtb.html
f2b60f7d 72 #[instrument(level = "debug", skip(self), ret)]
9ffffee4 73 pub fn instantiate_binder_with_placeholders<T>(&self, binder: ty::Binder<'tcx, T>) -> T
0bf4aa26 74 where
9ffffee4 75 T: TypeFoldable<TyCtxt<'tcx>> + Copy,
a7813a04 76 {
923072b8
FG
77 if let Some(inner) = binder.no_bound_vars() {
78 return inner;
79 }
80
81 let next_universe = self.create_next_universe();
0bf4aa26 82
064997fb 83 let delegate = FnMutDelegate {
f2b60f7d 84 regions: &mut |br: ty::BoundRegion| {
fe692bf9
FG
85 ty::Region::new_placeholder(
86 self.tcx,
87 ty::PlaceholderRegion { universe: next_universe, bound: br },
88 )
064997fb 89 },
f2b60f7d 90 types: &mut |bound_ty: ty::BoundTy| {
fe692bf9
FG
91 Ty::new_placeholder(
92 self.tcx,
93 ty::PlaceholderType { universe: next_universe, bound: bound_ty },
94 )
064997fb 95 },
f2b60f7d 96 consts: &mut |bound_var: ty::BoundVar, ty| {
fe692bf9
FG
97 ty::Const::new_placeholder(
98 self.tcx,
353b0b11
FG
99 ty::PlaceholderConst { universe: next_universe, bound: bound_var },
100 ty,
101 )
064997fb 102 },
48663c56
XL
103 };
104
f2b60f7d
FG
105 debug!(?next_universe);
106 self.tcx.replace_bound_vars_uncached(binder, delegate)
1a4d82fc 107 }
1a4d82fc 108
fe692bf9
FG
109 /// See [RegionConstraintCollector::leak_check][1]. We only check placeholder
110 /// leaking into `outer_universe`, i.e. placeholders which cannot be named by that
111 /// universe.
5e7ed085
FG
112 ///
113 /// [1]: crate::infer::region_constraints::RegionConstraintCollector::leak_check
0731742a 114 pub fn leak_check(
0bf4aa26 115 &self,
fe692bf9
FG
116 outer_universe: ty::UniverseIndex,
117 only_consider_snapshot: Option<&CombinedSnapshot<'tcx>>,
0731742a 118 ) -> RelateResult<'tcx, ()> {
74b04a01
XL
119 // If the user gave `-Zno-leak-check`, or we have been
120 // configured to skip the leak check, then skip the leak check
121 // completely. The leak check is deprecated. Any legitimate
122 // subtyping errors that it would have caught will now be
123 // caught later on, during region checking. However, we
124 // continue to use it for a transition period.
fe692bf9 125 if self.tcx.sess.opts.unstable_opts.no_leak_check || self.skip_leak_check {
74b04a01
XL
126 return Ok(());
127 }
128
129 self.inner.borrow_mut().unwrap_region_constraints().leak_check(
dfeec247 130 self.tcx,
fe692bf9 131 outer_universe,
f035d41b 132 self.universe(),
fe692bf9 133 only_consider_snapshot,
dfeec247 134 )
3157f602 135 }
1a4d82fc 136}