]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_infer/src/infer/higher_ranked/mod.rs
don't use `commit_if_ok` during `higher_ranked_sub`
[rust.git] / compiler / rustc_infer / src / 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};
6 use crate::infer::CombinedSnapshot;
7 use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
8 use rustc_middle::ty::{self, Binder, TypeFoldable};
9
10 impl<'a, 'tcx> CombineFields<'a, 'tcx> {
11     /// Checks whether `for<..> sub <: for<..> sup` holds.
12     ///
13     /// For this to hold, **all** instantiations of the super type
14     /// have to be a super type of **at least one** instantiation of
15     /// the subtype.
16     ///
17     /// This is implemented by first entering a new universe.
18     /// We then replace all bound variables in `sup` with placeholders,
19     /// and all bound variables in `sup` with inference vars.
20     /// We can then just relate the two resulting types as normal.
21     ///
22     /// Note: this is a subtle algorithm. For a full explanation, please see
23     /// the [rustc dev guide][rd]
24     ///
25     /// [rd]: https://rustc-dev-guide.rust-lang.org/borrow_check/region_inference/placeholders_and_universes.html
26     #[instrument(skip(self), level = "debug")]
27     pub fn higher_ranked_sub<T>(
28         &mut self,
29         sub: Binder<'tcx, T>,
30         sup: Binder<'tcx, T>,
31         sub_is_expected: bool,
32     ) -> RelateResult<'tcx, ()>
33     where
34         T: Relate<'tcx>,
35     {
36         let span = self.trace.cause.span;
37         // First, we instantiate each bound region in the supertype with a
38         // fresh placeholder region. Note that this automatically creates
39         // a new universe if needed.
40         let sup_prime = self.infcx.replace_bound_vars_with_placeholders(sup);
41
42         // Next, we instantiate each bound region in the subtype
43         // with a fresh region variable. These region variables --
44         // but no other pre-existing region variables -- can name
45         // the placeholders.
46         let sub_prime = self.infcx.replace_bound_vars_with_fresh_vars(span, HigherRankedType, sub);
47
48         debug!("a_prime={:?}", sub_prime);
49         debug!("b_prime={:?}", sup_prime);
50
51         // Compare types now that bound regions have been replaced.
52         let result = self.sub(sub_is_expected).relate(sub_prime, sup_prime)?;
53
54         debug!("OK result={result:?}");
55         // NOTE: returning the result here would be dangerous as it contains
56         // placeholders which **must not** be named afterwards.
57         Ok(())
58     }
59 }
60
61 impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
62     /// Replaces all bound variables (lifetimes, types, and constants) bound by
63     /// `binder` with placeholder variables in a new universe. This means that the
64     /// new placeholders can only be named by inference variables created after
65     /// this method has been called.
66     ///
67     /// This is the first step of checking subtyping when higher-ranked things are involved.
68     /// For more details visit the relevant sections of the [rustc dev guide].
69     ///
70     /// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/hrtb.html
71     #[instrument(level = "debug", skip(self))]
72     pub fn replace_bound_vars_with_placeholders<T>(&self, binder: ty::Binder<'tcx, T>) -> T
73     where
74         T: TypeFoldable<'tcx> + Copy,
75     {
76         if let Some(inner) = binder.no_bound_vars() {
77             return inner;
78         }
79
80         let next_universe = self.create_next_universe();
81
82         let fld_r = |br: ty::BoundRegion| {
83             self.tcx.mk_region(ty::RePlaceholder(ty::PlaceholderRegion {
84                 universe: next_universe,
85                 name: br.kind,
86             }))
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         };
95
96         let fld_c = |bound_var: ty::BoundVar, ty| {
97             self.tcx.mk_const(ty::ConstS {
98                 kind: ty::ConstKind::Placeholder(ty::PlaceholderConst {
99                     universe: next_universe,
100                     name: ty::BoundConst { var: bound_var, ty },
101                 }),
102                 ty,
103             })
104         };
105
106         let result = self.tcx.replace_bound_vars_uncached(binder, fld_r, fld_t, fld_c);
107         debug!(?next_universe, ?result);
108         result
109     }
110
111     /// See [RegionConstraintCollector::leak_check][1].
112     ///
113     /// [1]: crate::infer::region_constraints::RegionConstraintCollector::leak_check
114     pub fn leak_check(
115         &self,
116         overly_polymorphic: bool,
117         snapshot: &CombinedSnapshot<'_, 'tcx>,
118     ) -> RelateResult<'tcx, ()> {
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.
125         if self.tcx.sess.opts.debugging_opts.no_leak_check || self.skip_leak_check.get() {
126             return Ok(());
127         }
128
129         self.inner.borrow_mut().unwrap_region_constraints().leak_check(
130             self.tcx,
131             overly_polymorphic,
132             self.universe(),
133             snapshot,
134         )
135     }
136 }