Rollup merge of #70277 - matthewjasper:remove-closurebound, r=nikomatsakis

Remove `ReClosureBound`

We now substitute external names for regions in the query response.

r? @nikomatsakis
This commit is contained in:
Mazdak Farrokhzad 2020-03-24 00:49:48 +01:00 committed by GitHub
commit 6c58e0194e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 50 additions and 129 deletions

View File

@ -92,9 +92,6 @@ impl<'a> HashStable<StableHashingContext<'a>> for ty::RegionKind {
ty::ReFree(ref free_region) => {
free_region.hash_stable(hcx, hasher);
}
ty::ReClosureBound(vid) => {
vid.hash_stable(hcx, hasher);
}
ty::ReVar(..) | ty::RePlaceholder(..) => {
bug!("StableHasher: unexpected region {:?}", *self)
}

View File

@ -88,34 +88,35 @@ pub struct ConstQualifs {
/// requirements are then verified and proved by the closure's
/// creating function. This struct encodes those requirements.
///
/// The requirements are listed as being between various
/// `RegionVid`. The 0th region refers to `'static`; subsequent region
/// vids refer to the free regions that appear in the closure (or
/// generator's) type, in order of appearance. (This numbering is
/// actually defined by the `UniversalRegions` struct in the NLL
/// region checker. See for example
/// `UniversalRegions::closure_mapping`.) Note that we treat the free
/// regions in the closure's type "as if" they were erased, so their
/// precise identity is not important, only their position.
/// The requirements are listed as being between various `RegionVid`. The 0th
/// region refers to `'static`; subsequent region vids refer to the free
/// regions that appear in the closure (or generator's) type, in order of
/// appearance. (This numbering is actually defined by the `UniversalRegions`
/// struct in the NLL region checker. See for example
/// `UniversalRegions::closure_mapping`.) Note the free regions in the
/// closure's signature and captures are erased.
///
/// Example: If type check produces a closure with the closure substs:
///
/// ```text
/// ClosureSubsts = [
/// i8, // the "closure kind"
/// for<'x> fn(&'a &'x u32) -> &'x u32, // the "closure signature"
/// &'a String, // some upvar
/// 'a, // From the parent.
/// 'b,
/// i8, // the "closure kind"
/// for<'x> fn(&'<erased> &'x u32) -> &'x u32, // the "closure signature"
/// &'<erased> String, // some upvar
/// ]
/// ```
///
/// here, there is one unique free region (`'a`) but it appears
/// twice. We would "renumber" each occurrence to a unique vid, as follows:
/// We would "renumber" each free region to a unique vid, as follows:
///
/// ```text
/// ClosureSubsts = [
/// i8, // the "closure kind"
/// for<'x> fn(&'1 &'x u32) -> &'x u32, // the "closure signature"
/// &'2 String, // some upvar
/// '1, // From the parent.
/// '2,
/// i8, // the "closure kind"
/// for<'x> fn(&'3 &'x u32) -> &'x u32, // the "closure signature"
/// &'4 String, // some upvar
/// ]
/// ```
///
@ -124,14 +125,12 @@ pub struct ConstQualifs {
/// can be extracted from its type and constrained to have the given
/// outlives relationship.
///
/// In some cases, we have to record outlives requirements between
/// types and regions as well. In that case, if those types include
/// any regions, those regions are recorded as `ReClosureBound`
/// instances assigned one of these same indices. Those regions will
/// be substituted away by the creator. We use `ReClosureBound` in
/// that case because the regions must be allocated in the global
/// `TyCtxt`, and hence we cannot use `ReVar` (which is what we use
/// internally within the rest of the NLL code).
/// In some cases, we have to record outlives requirements between types and
/// regions as well. In that case, if those types include any regions, those
/// regions are recorded using their external names (`ReStatic`,
/// `ReEarlyBound`, `ReFree`). We use these because in a query response we
/// cannot use `ReVar` (which is what we use internally within the rest of the
/// NLL code).
#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
pub struct ClosureRegionRequirements<'tcx> {
/// The number of external regions defined on the closure. In our

View File

@ -1574,7 +1574,7 @@ impl<F: fmt::Write> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx, F> {
ty::ReVar(_) | ty::ReScope(_) | ty::ReErased => false,
ty::ReStatic | ty::ReEmpty(_) | ty::ReClosureBound(_) => true,
ty::ReStatic | ty::ReEmpty(_) => true,
}
}
@ -1686,12 +1686,6 @@ impl<F: fmt::Write> FmtPrinter<'_, '_, F> {
p!(write("'<empty:{:?}>", ui));
return Ok(self);
}
// The user should never encounter these in unsubstituted form.
ty::ReClosureBound(vid) => {
p!(write("{:?}", vid));
return Ok(self);
}
}
p!(write("'_"));

View File

@ -81,8 +81,6 @@ impl fmt::Debug for ty::RegionKind {
match *self {
ty::ReEarlyBound(ref data) => write!(f, "ReEarlyBound({}, {})", data.index, data.name),
ty::ReClosureBound(ref vid) => write!(f, "ReClosureBound({:?})", vid),
ty::ReLateBound(binder_id, ref bound_region) => {
write!(f, "ReLateBound({:?}, {:?})", binder_id, bound_region)
}

View File

@ -1352,12 +1352,6 @@ pub enum RegionKind {
/// Erased region, used by trait selection, in MIR and during codegen.
ReErased,
/// These are regions bound in the "defining type" for a
/// closure. They are used ONLY as part of the
/// `ClosureRegionRequirements` that are produced by MIR borrowck.
/// See `ClosureRegionRequirements` for more details.
ReClosureBound(RegionVid),
}
impl<'tcx> rustc_serialize::UseSpecializedDecodable for Region<'tcx> {}
@ -1567,7 +1561,6 @@ impl RegionKind {
RegionKind::RePlaceholder(placeholder) => placeholder.name.is_named(),
RegionKind::ReEmpty(_) => false,
RegionKind::ReErased => false,
RegionKind::ReClosureBound(..) => false,
}
}
@ -1648,9 +1641,6 @@ impl RegionKind {
ty::ReEmpty(_) | ty::ReStatic => {
flags = flags | TypeFlags::HAS_FREE_REGIONS;
}
ty::ReClosureBound(..) => {
flags = flags | TypeFlags::HAS_FREE_REGIONS;
}
ty::ReLateBound(..) => {
flags = flags | TypeFlags::HAS_RE_LATE_BOUND;
}

View File

@ -336,10 +336,6 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
| ty::ReEmpty(_)
| ty::RePlaceholder(..)
| ty::ReErased => self.canonicalize_region_mode.canonicalize_free_region(self, r),
ty::ReClosureBound(..) => {
bug!("closure bound region encountered during canonicalization");
}
}
}

View File

@ -581,10 +581,6 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
return Ok(r);
}
ty::ReClosureBound(..) => {
span_bug!(self.span, "encountered unexpected ReClosureBound: {:?}", r,);
}
ty::RePlaceholder(..)
| ty::ReVar(..)
| ty::ReEmpty(_)

View File

@ -152,11 +152,6 @@ pub(super) fn note_and_explain_region(
ty::ReVar(_) | ty::ReLateBound(..) | ty::ReErased => {
(format!("lifetime {:?}", region), None)
}
// We shouldn't encounter an error message with ReClosureBound.
ty::ReClosureBound(..) => {
bug!("encountered unexpected ReClosureBound: {:?}", region,);
}
};
emit_msg_span(err, prefix, description, span, suffix);

View File

@ -135,10 +135,6 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
// replace all free regions with 'erased
self.tcx().lifetimes.re_erased
}
ty::ReClosureBound(..) => {
bug!("encountered unexpected region: {:?}", r,);
}
}
}

View File

@ -493,12 +493,7 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
/// term "concrete regions").
fn lub_concrete_regions(&self, a: Region<'tcx>, b: Region<'tcx>) -> Region<'tcx> {
let r = match (a, b) {
(&ty::ReClosureBound(..), _)
| (_, &ty::ReClosureBound(..))
| (&ReLateBound(..), _)
| (_, &ReLateBound(..))
| (&ReErased, _)
| (_, &ReErased) => {
(&ReLateBound(..), _) | (_, &ReLateBound(..)) | (&ReErased, _) | (_, &ReErased) => {
bug!("cannot relate region: LUB({:?}, {:?})", a, b);
}

View File

@ -798,7 +798,7 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
| ty::ReEarlyBound(..) => ty::UniverseIndex::ROOT,
ty::ReEmpty(ui) => ui,
ty::RePlaceholder(placeholder) => placeholder.universe,
ty::ReClosureBound(vid) | ty::ReVar(vid) => self.var_universe(vid),
ty::ReVar(vid) => self.var_universe(vid),
ty::ReLateBound(..) => bug!("universe(): encountered bound region {:?}", region),
}
}

View File

@ -292,8 +292,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
| ty::ReVar(..)
| ty::RePlaceholder(..)
| ty::ReEmpty(_)
| ty::ReErased
| ty::ReClosureBound(..) => None,
| ty::ReErased => None,
}
}

View File

@ -940,8 +940,9 @@ impl<'tcx> RegionInferenceContext<'tcx> {
/// inference variables with some region from the closure
/// signature -- this is not always possible, so this is a
/// fallible process. Presuming we do find a suitable region, we
/// will represent it with a `ReClosureBound`, which is a
/// `RegionKind` variant that can be allocated in the gcx.
/// will use it's *external name*, which will be a `RegionKind`
/// variant that can be used in query responses such as
/// `ReEarlyBound`.
fn try_promote_type_test_subject(
&self,
infcx: &InferCtxt<'_, 'tcx>,
@ -991,14 +992,14 @@ impl<'tcx> RegionInferenceContext<'tcx> {
// find an equivalent.
let upper_bound = self.non_local_universal_upper_bound(region_vid);
if self.region_contains(region_vid, upper_bound) {
tcx.mk_region(ty::ReClosureBound(upper_bound))
self.definitions[upper_bound].external_name.unwrap_or(r)
} else {
// In the case of a failure, use a `ReVar`
// result. This will cause the `lift` later on to
// fail.
// In the case of a failure, use a `ReVar` result. This will
// cause the `has_local_value` later on to return `None`.
r
}
});
debug!("try_promote_type_test_subject: folded ty = {:?}", ty);
// `has_local_value` will only be true if we failed to promote some region.
@ -2029,15 +2030,6 @@ pub trait ClosureRegionRequirementsExt<'tcx> {
closure_def_id: DefId,
closure_substs: SubstsRef<'tcx>,
) -> Vec<QueryOutlivesConstraint<'tcx>>;
fn subst_closure_mapping<T>(
&self,
tcx: TyCtxt<'tcx>,
closure_mapping: &IndexVec<RegionVid, ty::Region<'tcx>>,
value: &T,
) -> T
where
T: TypeFoldable<'tcx>;
}
impl<'tcx> ClosureRegionRequirementsExt<'tcx> for ClosureRegionRequirements<'tcx> {
@ -2094,7 +2086,6 @@ impl<'tcx> ClosureRegionRequirementsExt<'tcx> for ClosureRegionRequirements<'tcx
}
ClosureOutlivesSubject::Ty(ty) => {
let ty = self.subst_closure_mapping(tcx, closure_mapping, &ty);
debug!(
"apply_requirements: ty={:?} \
outlived_region={:?} \
@ -2107,22 +2098,4 @@ impl<'tcx> ClosureRegionRequirementsExt<'tcx> for ClosureRegionRequirements<'tcx
})
.collect()
}
fn subst_closure_mapping<T>(
&self,
tcx: TyCtxt<'tcx>,
closure_mapping: &IndexVec<RegionVid, ty::Region<'tcx>>,
value: &T,
) -> T
where
T: TypeFoldable<'tcx>,
{
tcx.fold_regions(value, &mut false, |r, _depth| {
if let ty::ReClosureBound(vid) = r {
closure_mapping[*vid]
} else {
bug!("subst_closure_mapping: encountered non-closure bound free region {:?}", r)
}
})
}
}

View File

@ -816,11 +816,7 @@ impl TypeFolder<'tcx> for ReverseMapper<'tcx> {
// The regions that we expect from borrow checking.
ty::ReEarlyBound(_) | ty::ReFree(_) | ty::ReEmpty(ty::UniverseIndex::ROOT) => {}
ty::ReEmpty(_)
| ty::RePlaceholder(_)
| ty::ReVar(_)
| ty::ReScope(_)
| ty::ReClosureBound(_) => {
ty::ReEmpty(_) | ty::RePlaceholder(_) | ty::ReVar(_) | ty::ReScope(_) => {
// All of the regions in the type should either have been
// erased by writeback, or mapped back to named regions by
// borrow checking.

View File

@ -170,7 +170,6 @@ fn is_free_region(tcx: TyCtxt<'_>, region: Region<'_>) -> bool {
// These regions don't appear in types from type declarations:
RegionKind::ReErased
| RegionKind::ReClosureBound(..)
| RegionKind::ReScope(..)
| RegionKind::ReVar(..)
| RegionKind::RePlaceholder(..)

View File

@ -449,7 +449,6 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
}
ty::ReFree(..)
| ty::ReClosureBound(..)
| ty::ReScope(..)
| ty::ReVar(..)
| ty::RePlaceholder(..)

View File

@ -450,7 +450,6 @@ impl Clean<Option<Lifetime>> for ty::RegionKind {
| ty::ReVar(..)
| ty::RePlaceholder(..)
| ty::ReEmpty(_)
| ty::ReClosureBound(_)
| ty::ReErased => {
debug!("cannot clean region {:?}", self);
None

View File

@ -111,7 +111,7 @@ LL | with_signature(cell, t, |cell, t| require(cell, t));
(),
]
= note: number of external vids: 4
= note: where <T as Anything<ReClosureBound('_#2r)>>::AssocType: '_#3r
= note: where <T as Anything<ReEarlyBound(1, 'b)>>::AssocType: '_#3r
note: no external requirements
--> $DIR/projection-one-region-closure.rs:62:1

View File

@ -93,7 +93,7 @@ LL | with_signature(cell, t, |cell, t| require(cell, t));
(),
]
= note: number of external vids: 4
= note: where <T as Anything<ReClosureBound('_#2r)>>::AssocType: '_#3r
= note: where <T as Anything<ReEarlyBound(1, 'b)>>::AssocType: '_#3r
note: no external requirements
--> $DIR/projection-one-region-trait-bound-closure.rs:52:1

View File

@ -11,7 +11,7 @@ LL | with_signature(cell, t, |cell, t| require(cell, t));
]
= note: late-bound region is '_#4r
= note: number of external vids: 5
= note: where <T as Anything<ReClosureBound('_#1r), ReClosureBound('_#2r)>>::AssocType: '_#3r
= note: where <T as Anything<ReEarlyBound(0, 'b), ReEarlyBound(1, 'c)>>::AssocType: '_#3r
note: no external requirements
--> $DIR/projection-two-region-trait-bound-closure.rs:34:1
@ -27,13 +27,13 @@ LL | | }
|
= note: defining type: no_relationships_late::<'_#1r, '_#2r, T>
error[E0309]: the associated type `<T as Anything<'_#5r, '_#6r>>::AssocType` may not live long enough
error[E0309]: the associated type `<T as Anything<ReEarlyBound(0, 'b), ReEarlyBound(1, 'c)>>::AssocType` may not live long enough
--> $DIR/projection-two-region-trait-bound-closure.rs:38:29
|
LL | with_signature(cell, t, |cell, t| require(cell, t));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider adding an explicit lifetime bound `<T as Anything<'_#5r, '_#6r>>::AssocType: 'a`...
= help: consider adding an explicit lifetime bound `<T as Anything<ReEarlyBound(0, 'b), ReEarlyBound(1, 'c)>>::AssocType: 'a`...
note: external requirements
--> $DIR/projection-two-region-trait-bound-closure.rs:48:29
@ -47,7 +47,7 @@ LL | with_signature(cell, t, |cell, t| require(cell, t));
(),
]
= note: number of external vids: 5
= note: where <T as Anything<ReClosureBound('_#2r), ReClosureBound('_#3r)>>::AssocType: '_#4r
= note: where <T as Anything<ReEarlyBound(1, 'b), ReEarlyBound(2, 'c)>>::AssocType: '_#4r
note: no external requirements
--> $DIR/projection-two-region-trait-bound-closure.rs:43:1
@ -63,13 +63,13 @@ LL | | }
|
= note: defining type: no_relationships_early::<'_#1r, '_#2r, '_#3r, T>
error[E0309]: the associated type `<T as Anything<'_#6r, '_#7r>>::AssocType` may not live long enough
error[E0309]: the associated type `<T as Anything<ReEarlyBound(1, 'b), ReEarlyBound(2, 'c)>>::AssocType` may not live long enough
--> $DIR/projection-two-region-trait-bound-closure.rs:48:29
|
LL | with_signature(cell, t, |cell, t| require(cell, t));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider adding an explicit lifetime bound `<T as Anything<'_#6r, '_#7r>>::AssocType: 'a`...
= help: consider adding an explicit lifetime bound `<T as Anything<ReEarlyBound(1, 'b), ReEarlyBound(2, 'c)>>::AssocType: 'a`...
note: external requirements
--> $DIR/projection-two-region-trait-bound-closure.rs:61:29
@ -83,7 +83,7 @@ LL | with_signature(cell, t, |cell, t| require(cell, t));
(),
]
= note: number of external vids: 5
= note: where <T as Anything<ReClosureBound('_#2r), ReClosureBound('_#3r)>>::AssocType: '_#4r
= note: where <T as Anything<ReEarlyBound(1, 'b), ReEarlyBound(2, 'c)>>::AssocType: '_#4r
note: no external requirements
--> $DIR/projection-two-region-trait-bound-closure.rs:53:1
@ -111,7 +111,7 @@ LL | with_signature(cell, t, |cell, t| require(cell, t));
(),
]
= note: number of external vids: 5
= note: where <T as Anything<ReClosureBound('_#2r), ReClosureBound('_#3r)>>::AssocType: '_#4r
= note: where <T as Anything<ReEarlyBound(1, 'b), ReEarlyBound(2, 'c)>>::AssocType: '_#4r
note: no external requirements
--> $DIR/projection-two-region-trait-bound-closure.rs:65:1
@ -139,7 +139,7 @@ LL | with_signature(cell, t, |cell, t| require(cell, t));
(),
]
= note: number of external vids: 5
= note: where <T as Anything<ReClosureBound('_#2r), ReClosureBound('_#3r)>>::AssocType: '_#4r
= note: where <T as Anything<ReEarlyBound(1, 'b), ReEarlyBound(2, 'c)>>::AssocType: '_#4r
note: no external requirements
--> $DIR/projection-two-region-trait-bound-closure.rs:74:1
@ -168,7 +168,7 @@ LL | with_signature(cell, t, |cell, t| require(cell, t));
]
= note: late-bound region is '_#3r
= note: number of external vids: 4
= note: where <T as Anything<ReClosureBound('_#1r), ReClosureBound('_#1r)>>::AssocType: '_#2r
= note: where <T as Anything<ReEarlyBound(0, 'b), ReEarlyBound(0, 'b)>>::AssocType: '_#2r
note: no external requirements
--> $DIR/projection-two-region-trait-bound-closure.rs:83:1
@ -209,7 +209,7 @@ LL | with_signature(cell, t, |cell, t| require(cell, t));
(),
]
= note: number of external vids: 4
= note: where <T as Anything<ReClosureBound('_#2r), ReClosureBound('_#2r)>>::AssocType: '_#3r
= note: where <T as Anything<ReEarlyBound(1, 'b), ReEarlyBound(1, 'b)>>::AssocType: '_#3r
note: no external requirements
--> $DIR/projection-two-region-trait-bound-closure.rs:92:1
@ -237,7 +237,7 @@ LL | with_signature(cell, t, |cell, t| require(cell, t));
(),
]
= note: number of external vids: 3
= note: where <T as Anything<ReClosureBound('_#1r), ReClosureBound('_#1r)>>::AssocType: '_#2r
= note: where <T as Anything<ReEarlyBound(0, 'a), ReEarlyBound(0, 'a)>>::AssocType: '_#2r
note: no external requirements
--> $DIR/projection-two-region-trait-bound-closure.rs:101:1