Skip to content

Commit 4f334f2

Browse files
committed
Move LifetimeCollectVisitor to rustc_ast_lowering
1 parent 9f77688 commit 4f334f2

File tree

3 files changed

+67
-61
lines changed

3 files changed

+67
-61
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ pub use UnsafeSource::*;
2525
use crate::ptr::P;
2626
use crate::token::{self, CommentKind, Delimiter};
2727
use crate::tokenstream::{DelimSpan, LazyTokenStream, TokenStream};
28-
use crate::visit::{self, BoundKind, LifetimeCtxt, Visitor};
2928

30-
use rustc_data_structures::fx::FxHashMap;
3129
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
3230
use rustc_data_structures::stack::ensure_sufficient_stack;
3331
use rustc_data_structures::sync::Lrc;
@@ -325,63 +323,6 @@ impl GenericBound {
325323

326324
pub type GenericBounds = Vec<GenericBound>;
327325

328-
struct LifetimeCollectVisitor<'ast> {
329-
current_binders: Vec<NodeId>,
330-
binders_to_ignore: FxHashMap<NodeId, Vec<NodeId>>,
331-
collected_lifetimes: Vec<&'ast Lifetime>,
332-
}
333-
334-
impl<'ast> Visitor<'ast> for LifetimeCollectVisitor<'ast> {
335-
fn visit_lifetime(&mut self, lifetime: &'ast Lifetime, _: LifetimeCtxt) {
336-
if !self.collected_lifetimes.contains(&lifetime) {
337-
self.collected_lifetimes.push(lifetime);
338-
}
339-
self.binders_to_ignore.insert(lifetime.id, self.current_binders.clone());
340-
}
341-
342-
fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef, m: &'ast TraitBoundModifier) {
343-
self.current_binders.push(t.trait_ref.ref_id);
344-
345-
visit::walk_poly_trait_ref(self, t, m);
346-
347-
self.current_binders.pop();
348-
}
349-
350-
fn visit_ty(&mut self, t: &'ast Ty) {
351-
if let TyKind::BareFn(_) = t.kind {
352-
self.current_binders.push(t.id);
353-
}
354-
visit::walk_ty(self, t);
355-
if let TyKind::BareFn(_) = t.kind {
356-
self.current_binders.pop();
357-
}
358-
}
359-
}
360-
361-
pub fn lifetimes_in_ret_ty(ret_ty: &FnRetTy) -> (Vec<&Lifetime>, FxHashMap<NodeId, Vec<NodeId>>) {
362-
let mut visitor = LifetimeCollectVisitor {
363-
current_binders: Vec::new(),
364-
binders_to_ignore: FxHashMap::default(),
365-
collected_lifetimes: Vec::new(),
366-
};
367-
visitor.visit_fn_ret_ty(ret_ty);
368-
(visitor.collected_lifetimes, visitor.binders_to_ignore)
369-
}
370-
371-
pub fn lifetimes_in_bounds(
372-
bounds: &GenericBounds,
373-
) -> (Vec<&Lifetime>, FxHashMap<NodeId, Vec<NodeId>>) {
374-
let mut visitor = LifetimeCollectVisitor {
375-
current_binders: Vec::new(),
376-
binders_to_ignore: FxHashMap::default(),
377-
collected_lifetimes: Vec::new(),
378-
};
379-
for bound in bounds {
380-
visitor.visit_param_bound(bound, BoundKind::Bound);
381-
}
382-
(visitor.collected_lifetimes, visitor.binders_to_ignore)
383-
}
384-
385326
/// Specifies the enforced ordering for generic parameters. In the future,
386327
/// if we wanted to relax this order, we could override `PartialEq` and
387328
/// `PartialOrd`, to allow the kinds to be unordered.

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ mod block;
7777
mod expr;
7878
mod index;
7979
mod item;
80+
mod lifetime_collector;
8081
mod pat;
8182
mod path;
8283

@@ -1352,7 +1353,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13521353
}),
13531354
);
13541355

1355-
let (lifetimes_in_bounds, binders_to_ignore) = ast::lifetimes_in_bounds(bounds);
1356+
let (lifetimes_in_bounds, binders_to_ignore) =
1357+
lifetime_collector::lifetimes_in_bounds(bounds);
13561358
debug!(?lifetimes_in_bounds);
13571359
debug!(?binders_to_ignore);
13581360

@@ -1756,7 +1758,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17561758
}),
17571759
);
17581760

1759-
let (lifetimes_in_bounds, binders_to_ignore) = ast::lifetimes_in_ret_ty(output);
1761+
let (lifetimes_in_bounds, binders_to_ignore) =
1762+
lifetime_collector::lifetimes_in_ret_ty(output);
17601763
debug!(?lifetimes_in_bounds);
17611764
debug!(?binders_to_ignore);
17621765

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use rustc_ast::visit::{self, BoundKind, LifetimeCtxt, Visitor};
2+
use rustc_ast::{
3+
FnRetTy, GenericBounds, Lifetime, NodeId, PolyTraitRef, TraitBoundModifier, Ty, TyKind,
4+
};
5+
use rustc_data_structures::fx::FxHashMap;
6+
7+
struct LifetimeCollectVisitor<'ast> {
8+
current_binders: Vec<NodeId>,
9+
binders_to_ignore: FxHashMap<NodeId, Vec<NodeId>>,
10+
collected_lifetimes: Vec<&'ast Lifetime>,
11+
}
12+
13+
impl<'ast> Visitor<'ast> for LifetimeCollectVisitor<'ast> {
14+
fn visit_lifetime(&mut self, lifetime: &'ast Lifetime, _: LifetimeCtxt) {
15+
if !self.collected_lifetimes.contains(&lifetime) {
16+
self.collected_lifetimes.push(lifetime);
17+
}
18+
self.binders_to_ignore.insert(lifetime.id, self.current_binders.clone());
19+
}
20+
21+
fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef, m: &'ast TraitBoundModifier) {
22+
self.current_binders.push(t.trait_ref.ref_id);
23+
24+
visit::walk_poly_trait_ref(self, t, m);
25+
26+
self.current_binders.pop();
27+
}
28+
29+
fn visit_ty(&mut self, t: &'ast Ty) {
30+
if let TyKind::BareFn(_) = t.kind {
31+
self.current_binders.push(t.id);
32+
}
33+
visit::walk_ty(self, t);
34+
if let TyKind::BareFn(_) = t.kind {
35+
self.current_binders.pop();
36+
}
37+
}
38+
}
39+
40+
pub fn lifetimes_in_ret_ty(ret_ty: &FnRetTy) -> (Vec<&Lifetime>, FxHashMap<NodeId, Vec<NodeId>>) {
41+
let mut visitor = LifetimeCollectVisitor {
42+
current_binders: Vec::new(),
43+
binders_to_ignore: FxHashMap::default(),
44+
collected_lifetimes: Vec::new(),
45+
};
46+
visitor.visit_fn_ret_ty(ret_ty);
47+
(visitor.collected_lifetimes, visitor.binders_to_ignore)
48+
}
49+
50+
pub fn lifetimes_in_bounds(
51+
bounds: &GenericBounds,
52+
) -> (Vec<&Lifetime>, FxHashMap<NodeId, Vec<NodeId>>) {
53+
let mut visitor = LifetimeCollectVisitor {
54+
current_binders: Vec::new(),
55+
binders_to_ignore: FxHashMap::default(),
56+
collected_lifetimes: Vec::new(),
57+
};
58+
for bound in bounds {
59+
visitor.visit_param_bound(bound, BoundKind::Bound);
60+
}
61+
(visitor.collected_lifetimes, visitor.binders_to_ignore)
62+
}

0 commit comments

Comments
 (0)