diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 3503e5de8ccd..870a7c0be33d 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -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; -struct LifetimeCollectVisitor<'ast> { - current_binders: Vec, - binders_to_ignore: FxHashMap>, - 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>) { - 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>) { - 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. diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 4ca771bf4738..2883299291e3 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -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); diff --git a/compiler/rustc_ast_lowering/src/lifetime_collector.rs b/compiler/rustc_ast_lowering/src/lifetime_collector.rs new file mode 100644 index 000000000000..1dab8799ae5f --- /dev/null +++ b/compiler/rustc_ast_lowering/src/lifetime_collector.rs @@ -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, + binders_to_ignore: FxHashMap>, + 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>) { + 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>) { + 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) +}