Move LifetimeCollectVisitor to rustc_ast_lowering

This commit is contained in:
Santiago Pastorino 2022-07-29 22:38:07 -03:00
parent 9f77688d17
commit 4f334f2b97
No known key found for this signature in database
GPG Key ID: 8131A24E0C79EFAF
3 changed files with 67 additions and 61 deletions

View File

@ -25,9 +25,7 @@ pub use UnsafeSource::*;
use crate::ptr::P;
use crate::token::{self, CommentKind, Delimiter};
use crate::tokenstream::{DelimSpan, LazyTokenStream, TokenStream};
use crate::visit::{self, BoundKind, LifetimeCtxt, Visitor};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_data_structures::sync::Lrc;
@ -325,63 +323,6 @@ impl GenericBound {
pub type GenericBounds = Vec<GenericBound>;
struct LifetimeCollectVisitor<'ast> {
current_binders: Vec<NodeId>,
binders_to_ignore: FxHashMap<NodeId, Vec<NodeId>>,
collected_lifetimes: Vec<&'ast Lifetime>,
}
impl<'ast> Visitor<'ast> for LifetimeCollectVisitor<'ast> {
fn visit_lifetime(&mut self, lifetime: &'ast Lifetime, _: LifetimeCtxt) {
if !self.collected_lifetimes.contains(&lifetime) {
self.collected_lifetimes.push(lifetime);
}
self.binders_to_ignore.insert(lifetime.id, self.current_binders.clone());
}
fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef, m: &'ast TraitBoundModifier) {
self.current_binders.push(t.trait_ref.ref_id);
visit::walk_poly_trait_ref(self, t, m);
self.current_binders.pop();
}
fn visit_ty(&mut self, t: &'ast Ty) {
if let TyKind::BareFn(_) = t.kind {
self.current_binders.push(t.id);
}
visit::walk_ty(self, t);
if let TyKind::BareFn(_) = t.kind {
self.current_binders.pop();
}
}
}
pub fn lifetimes_in_ret_ty(ret_ty: &FnRetTy) -> (Vec<&Lifetime>, FxHashMap<NodeId, Vec<NodeId>>) {
let mut visitor = LifetimeCollectVisitor {
current_binders: Vec::new(),
binders_to_ignore: FxHashMap::default(),
collected_lifetimes: Vec::new(),
};
visitor.visit_fn_ret_ty(ret_ty);
(visitor.collected_lifetimes, visitor.binders_to_ignore)
}
pub fn lifetimes_in_bounds(
bounds: &GenericBounds,
) -> (Vec<&Lifetime>, FxHashMap<NodeId, Vec<NodeId>>) {
let mut visitor = LifetimeCollectVisitor {
current_binders: Vec::new(),
binders_to_ignore: FxHashMap::default(),
collected_lifetimes: Vec::new(),
};
for bound in bounds {
visitor.visit_param_bound(bound, BoundKind::Bound);
}
(visitor.collected_lifetimes, visitor.binders_to_ignore)
}
/// Specifies the enforced ordering for generic parameters. In the future,
/// if we wanted to relax this order, we could override `PartialEq` and
/// `PartialOrd`, to allow the kinds to be unordered.

View File

@ -77,6 +77,7 @@ mod block;
mod expr;
mod index;
mod item;
mod lifetime_collector;
mod pat;
mod path;
@ -1352,7 +1353,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}),
);
let (lifetimes_in_bounds, binders_to_ignore) = ast::lifetimes_in_bounds(bounds);
let (lifetimes_in_bounds, binders_to_ignore) =
lifetime_collector::lifetimes_in_bounds(bounds);
debug!(?lifetimes_in_bounds);
debug!(?binders_to_ignore);
@ -1756,7 +1758,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}),
);
let (lifetimes_in_bounds, binders_to_ignore) = ast::lifetimes_in_ret_ty(output);
let (lifetimes_in_bounds, binders_to_ignore) =
lifetime_collector::lifetimes_in_ret_ty(output);
debug!(?lifetimes_in_bounds);
debug!(?binders_to_ignore);

View File

@ -0,0 +1,62 @@
use rustc_ast::visit::{self, BoundKind, LifetimeCtxt, Visitor};
use rustc_ast::{
FnRetTy, GenericBounds, Lifetime, NodeId, PolyTraitRef, TraitBoundModifier, Ty, TyKind,
};
use rustc_data_structures::fx::FxHashMap;
struct LifetimeCollectVisitor<'ast> {
current_binders: Vec<NodeId>,
binders_to_ignore: FxHashMap<NodeId, Vec<NodeId>>,
collected_lifetimes: Vec<&'ast Lifetime>,
}
impl<'ast> Visitor<'ast> for LifetimeCollectVisitor<'ast> {
fn visit_lifetime(&mut self, lifetime: &'ast Lifetime, _: LifetimeCtxt) {
if !self.collected_lifetimes.contains(&lifetime) {
self.collected_lifetimes.push(lifetime);
}
self.binders_to_ignore.insert(lifetime.id, self.current_binders.clone());
}
fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef, m: &'ast TraitBoundModifier) {
self.current_binders.push(t.trait_ref.ref_id);
visit::walk_poly_trait_ref(self, t, m);
self.current_binders.pop();
}
fn visit_ty(&mut self, t: &'ast Ty) {
if let TyKind::BareFn(_) = t.kind {
self.current_binders.push(t.id);
}
visit::walk_ty(self, t);
if let TyKind::BareFn(_) = t.kind {
self.current_binders.pop();
}
}
}
pub fn lifetimes_in_ret_ty(ret_ty: &FnRetTy) -> (Vec<&Lifetime>, FxHashMap<NodeId, Vec<NodeId>>) {
let mut visitor = LifetimeCollectVisitor {
current_binders: Vec::new(),
binders_to_ignore: FxHashMap::default(),
collected_lifetimes: Vec::new(),
};
visitor.visit_fn_ret_ty(ret_ty);
(visitor.collected_lifetimes, visitor.binders_to_ignore)
}
pub fn lifetimes_in_bounds(
bounds: &GenericBounds,
) -> (Vec<&Lifetime>, FxHashMap<NodeId, Vec<NodeId>>) {
let mut visitor = LifetimeCollectVisitor {
current_binders: Vec::new(),
binders_to_ignore: FxHashMap::default(),
collected_lifetimes: Vec::new(),
};
for bound in bounds {
visitor.visit_param_bound(bound, BoundKind::Bound);
}
(visitor.collected_lifetimes, visitor.binders_to_ignore)
}