1 //! Helper routines for higher-ranked things. See the `doc` module at
2 //! the end of the file for details.
4 use super::combine::CombineFields;
5 use super::{HigherRankedType, InferCtxt, PlaceholderMap};
7 use crate::infer::{CombinedSnapshot, ConstVariableOrigin};
8 use crate::ty::relate::{Relate, RelateResult, TypeRelation};
9 use crate::ty::{self, Binder, TypeFoldable};
11 use syntax_pos::DUMMY_SP;
13 impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
14 pub fn higher_ranked_sub<T>(
19 ) -> RelateResult<'tcx, Binder<T>>
23 debug!("higher_ranked_sub(a={:?}, b={:?})", a, b);
25 // Rather than checking the subtype relationship between `a` and `b`
26 // as-is, we need to do some extra work here in order to make sure
27 // that function subtyping works correctly with respect to regions
29 // Note: this is a subtle algorithm. For a full explanation,
30 // please see the large comment at the end of the file in the (inlined) module
33 let span = self.trace.cause.span;
35 return self.infcx.commit_if_ok(|snapshot| {
36 // First, we instantiate each bound region in the supertype with a
37 // fresh placeholder region.
38 let (b_prime, placeholder_map) = self.infcx.replace_bound_vars_with_placeholders(b);
40 // Next, we instantiate each bound region in the subtype
41 // with a fresh region variable. These region variables --
42 // but no other pre-existing region variables -- can name
46 .replace_bound_vars_with_fresh_vars(span, HigherRankedType, a);
48 debug!("a_prime={:?}", a_prime);
49 debug!("b_prime={:?}", b_prime);
51 // Compare types now that bound regions have been replaced.
52 let result = self.sub(a_is_expected).relate(&a_prime, &b_prime)?;
55 .leak_check(!a_is_expected, &placeholder_map, snapshot)?;
57 debug!("higher_ranked_sub: OK result={:?}", result);
59 Ok(ty::Binder::bind(result))
64 impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
65 /// Replaces all regions (resp. types) bound by `binder` with placeholder
66 /// regions (resp. types) and return a map indicating which bound-region
67 /// placeholder region. This is the first step of checking subtyping
68 /// when higher-ranked things are involved.
70 /// **Important:** you must call this function from within a snapshot.
71 /// Moreover, before committing the snapshot, you must eventually call
72 /// either `plug_leaks` or `pop_placeholders` to remove the placeholder
73 /// regions. If you rollback the snapshot (or are using a probe), then
74 /// the pop occurs as part of the rollback, so an explicit call is not
75 /// needed (but is also permitted).
77 /// For more information about how placeholders and HRTBs work, see
78 /// the [rustc guide].
80 /// [rustc guide]: https://rust-lang.github.io/rustc-guide/traits/hrtb.html
81 pub fn replace_bound_vars_with_placeholders<T>(
83 binder: &ty::Binder<T>,
84 ) -> (T, PlaceholderMap<'tcx>)
86 T: TypeFoldable<'tcx>,
88 let next_universe = self.create_next_universe();
91 self.tcx.mk_region(ty::RePlaceholder(ty::PlaceholderRegion {
92 universe: next_universe,
97 let fld_t = |bound_ty: ty::BoundTy| {
98 self.tcx.mk_ty(ty::Placeholder(ty::PlaceholderType {
99 universe: next_universe,
104 let fld_c = |_: ty::BoundVar, ty| {
105 self.next_const_var_in_universe(
107 // FIXME(const_generics): do we want a placeholder const?
108 ConstVariableOrigin::MiscVariable(DUMMY_SP),
113 let (result, map) = self.tcx.replace_bound_vars(binder, fld_r, fld_t, fld_c);
116 "replace_bound_vars_with_placeholders(\
117 next_universe={:?}, \
121 next_universe, binder, result, map,
127 /// See `infer::region_constraints::RegionConstraintCollector::leak_check`.
130 overly_polymorphic: bool,
131 placeholder_map: &PlaceholderMap<'tcx>,
132 snapshot: &CombinedSnapshot<'_, 'tcx>,
133 ) -> RelateResult<'tcx, ()> {
134 self.borrow_region_constraints()
135 .leak_check(self.tcx, overly_polymorphic, placeholder_map, snapshot)