ty: remove unnecessary Predicate::walk_tys
and collect_regions
.
This commit is contained in:
parent
3410aeddbe
commit
b7fdc7b619
@ -263,20 +263,6 @@ where
|
||||
// Region folder
|
||||
|
||||
impl<'tcx> TyCtxt<'tcx> {
|
||||
/// Collects the free and escaping regions in `value` into `region_set`. Returns
|
||||
/// whether any late-bound regions were skipped
|
||||
pub fn collect_regions<T>(self, value: &T, region_set: &mut FxHashSet<ty::Region<'tcx>>) -> bool
|
||||
where
|
||||
T: TypeFoldable<'tcx>,
|
||||
{
|
||||
let mut have_bound_regions = false;
|
||||
self.fold_regions(value, &mut have_bound_regions, |r, d| {
|
||||
region_set.insert(self.mk_region(r.shifted_out_to_binder(d)));
|
||||
r
|
||||
});
|
||||
have_bound_regions
|
||||
}
|
||||
|
||||
/// Folds the escaping and free regions in `value` using `f`, and
|
||||
/// sets `skipped_regions` to true if any late-bound region was found
|
||||
/// and skipped.
|
||||
|
@ -1518,77 +1518,7 @@ impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> {
|
||||
}
|
||||
}
|
||||
|
||||
// A custom iterator used by `Predicate::walk_tys`.
|
||||
enum WalkTysIter<'tcx, I, J, K>
|
||||
where
|
||||
I: Iterator<Item = Ty<'tcx>>,
|
||||
J: Iterator<Item = Ty<'tcx>>,
|
||||
K: Iterator<Item = Ty<'tcx>>,
|
||||
{
|
||||
None,
|
||||
One(Ty<'tcx>),
|
||||
Two(Ty<'tcx>, Ty<'tcx>),
|
||||
Types(I),
|
||||
InputTypes(J),
|
||||
ProjectionTypes(K),
|
||||
}
|
||||
|
||||
impl<'tcx, I, J, K> Iterator for WalkTysIter<'tcx, I, J, K>
|
||||
where
|
||||
I: Iterator<Item = Ty<'tcx>>,
|
||||
J: Iterator<Item = Ty<'tcx>>,
|
||||
K: Iterator<Item = Ty<'tcx>>,
|
||||
{
|
||||
type Item = Ty<'tcx>;
|
||||
|
||||
fn next(&mut self) -> Option<Ty<'tcx>> {
|
||||
match *self {
|
||||
WalkTysIter::None => None,
|
||||
WalkTysIter::One(item) => {
|
||||
*self = WalkTysIter::None;
|
||||
Some(item)
|
||||
}
|
||||
WalkTysIter::Two(item1, item2) => {
|
||||
*self = WalkTysIter::One(item2);
|
||||
Some(item1)
|
||||
}
|
||||
WalkTysIter::Types(ref mut iter) => iter.next(),
|
||||
WalkTysIter::InputTypes(ref mut iter) => iter.next(),
|
||||
WalkTysIter::ProjectionTypes(ref mut iter) => iter.next(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> Predicate<'tcx> {
|
||||
/// Iterates over the types in this predicate. Note that in all
|
||||
/// cases this is skipping over a binder, so late-bound regions
|
||||
/// with depth 0 are bound by the predicate.
|
||||
pub fn walk_tys(&'a self) -> impl Iterator<Item = Ty<'tcx>> + 'a {
|
||||
match *self {
|
||||
ty::Predicate::Trait(ref data, _) => {
|
||||
WalkTysIter::InputTypes(data.skip_binder().input_types())
|
||||
}
|
||||
ty::Predicate::Subtype(binder) => {
|
||||
let SubtypePredicate { a, b, a_is_expected: _ } = binder.skip_binder();
|
||||
WalkTysIter::Two(a, b)
|
||||
}
|
||||
ty::Predicate::TypeOutlives(binder) => WalkTysIter::One(binder.skip_binder().0),
|
||||
ty::Predicate::RegionOutlives(..) => WalkTysIter::None,
|
||||
ty::Predicate::Projection(ref data) => {
|
||||
let inner = data.skip_binder();
|
||||
WalkTysIter::ProjectionTypes(
|
||||
inner.projection_ty.substs.types().chain(Some(inner.ty)),
|
||||
)
|
||||
}
|
||||
ty::Predicate::WellFormed(data) => WalkTysIter::One(data),
|
||||
ty::Predicate::ObjectSafe(_trait_def_id) => WalkTysIter::None,
|
||||
ty::Predicate::ClosureKind(_closure_def_id, closure_substs, _kind) => {
|
||||
WalkTysIter::Types(closure_substs.types())
|
||||
}
|
||||
ty::Predicate::ConstEvaluatable(_, substs) => WalkTysIter::Types(substs.types()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_opt_poly_trait_ref(&self) -> Option<PolyTraitRef<'tcx>> {
|
||||
match *self {
|
||||
Predicate::Trait(ref t, _) => Some(t.to_poly_trait_ref()),
|
||||
|
@ -315,25 +315,28 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
|
||||
tcx: TyCtxt<'tcx>,
|
||||
pred: ty::Predicate<'tcx>,
|
||||
) -> FxHashSet<GenericParamDef> {
|
||||
pred.walk_tys()
|
||||
.flat_map(|t| {
|
||||
let mut regions = FxHashSet::default();
|
||||
tcx.collect_regions(&t, &mut regions);
|
||||
let regions = match pred {
|
||||
ty::Predicate::Trait(poly_trait_pred, _) => {
|
||||
tcx.collect_referenced_late_bound_regions(&poly_trait_pred)
|
||||
}
|
||||
ty::Predicate::Projection(poly_proj_pred) => {
|
||||
tcx.collect_referenced_late_bound_regions(&poly_proj_pred)
|
||||
}
|
||||
_ => return FxHashSet::default(),
|
||||
};
|
||||
|
||||
regions.into_iter().flat_map(|r| {
|
||||
match r {
|
||||
// We only care about late bound regions, as we need to add them
|
||||
// to the 'for<>' section
|
||||
&ty::ReLateBound(_, ty::BoundRegion::BrNamed(_, name)) => {
|
||||
Some(GenericParamDef {
|
||||
name: name.to_string(),
|
||||
kind: GenericParamDefKind::Lifetime,
|
||||
})
|
||||
}
|
||||
&ty::ReVar(_) | &ty::ReEarlyBound(_) | &ty::ReStatic => None,
|
||||
_ => panic!("Unexpected region type {:?}", r),
|
||||
}
|
||||
})
|
||||
regions
|
||||
.into_iter()
|
||||
.filter_map(|br| {
|
||||
match br {
|
||||
// We only care about named late bound regions, as we need to add them
|
||||
// to the 'for<>' section
|
||||
ty::BrNamed(_, name) => Some(GenericParamDef {
|
||||
name: name.to_string(),
|
||||
kind: GenericParamDefKind::Lifetime,
|
||||
}),
|
||||
_ => None,
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user