Skip to content

Commit 65022af

Browse files
committed
Count number of lifetime parameters in a separate pass.
1 parent 88af837 commit 65022af

File tree

1 file changed

+39
-13
lines changed

1 file changed

+39
-13
lines changed

compiler/rustc_resolve/src/late.rs

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,6 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
959959
match item.kind {
960960
ItemKind::TyAlias(box TyAlias { ref generics, .. })
961961
| ItemKind::Fn(box Fn { ref generics, .. }) => {
962-
self.compute_num_lifetime_params(item.id, generics);
963962
self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| {
964963
visit::walk_item(this, item)
965964
});
@@ -968,7 +967,6 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
968967
ItemKind::Enum(_, ref generics)
969968
| ItemKind::Struct(_, ref generics)
970969
| ItemKind::Union(_, ref generics) => {
971-
self.compute_num_lifetime_params(item.id, generics);
972970
self.resolve_adt(item, generics);
973971
}
974972

@@ -979,12 +977,10 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
979977
items: ref impl_items,
980978
..
981979
}) => {
982-
self.compute_num_lifetime_params(item.id, generics);
983980
self.resolve_implementation(generics, of_trait, &self_ty, item.id, impl_items);
984981
}
985982

986983
ItemKind::Trait(box Trait { ref generics, ref bounds, ref items, .. }) => {
987-
self.compute_num_lifetime_params(item.id, generics);
988984
// Create a new rib for the trait-wide type parameters.
989985
self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| {
990986
let local_def_id = this.r.local_def_id(item.id).to_def_id();
@@ -1036,7 +1032,6 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
10361032
}
10371033

10381034
ItemKind::TraitAlias(ref generics, ref bounds) => {
1039-
self.compute_num_lifetime_params(item.id, generics);
10401035
// Create a new rib for the trait-wide type parameters.
10411036
self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| {
10421037
let local_def_id = this.r.local_def_id(item.id).to_def_id();
@@ -2501,20 +2496,51 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
25012496
Some((ident.name, ns)),
25022497
)
25032498
}
2499+
}
25042500

2505-
fn compute_num_lifetime_params(&mut self, id: NodeId, generics: &Generics) {
2506-
let def_id = self.r.local_def_id(id);
2507-
let count = generics
2508-
.params
2509-
.iter()
2510-
.filter(|param| matches!(param.kind, ast::GenericParamKind::Lifetime { .. }))
2511-
.count();
2512-
self.r.item_generics_num_lifetimes.insert(def_id, count);
2501+
struct LifetimeCountVisitor<'a, 'b> {
2502+
r: &'b mut Resolver<'a>,
2503+
}
2504+
2505+
/// Walks the whole crate in DFS order, visiting each item, counting the declared number of
2506+
/// lifetime generic parameters.
2507+
impl<'ast> Visitor<'ast> for LifetimeCountVisitor<'_, '_> {
2508+
fn visit_item(&mut self, item: &'ast Item) {
2509+
match &item.kind {
2510+
ItemKind::TyAlias(box TyAlias { ref generics, .. })
2511+
| ItemKind::Fn(box Fn { ref generics, .. })
2512+
| ItemKind::Enum(_, ref generics)
2513+
| ItemKind::Struct(_, ref generics)
2514+
| ItemKind::Union(_, ref generics)
2515+
| ItemKind::Impl(box Impl { ref generics, .. })
2516+
| ItemKind::Trait(box Trait { ref generics, .. })
2517+
| ItemKind::TraitAlias(ref generics, _) => {
2518+
let def_id = self.r.local_def_id(item.id);
2519+
let count = generics
2520+
.params
2521+
.iter()
2522+
.filter(|param| matches!(param.kind, ast::GenericParamKind::Lifetime { .. }))
2523+
.count();
2524+
self.r.item_generics_num_lifetimes.insert(def_id, count);
2525+
}
2526+
2527+
ItemKind::Mod(..)
2528+
| ItemKind::ForeignMod(..)
2529+
| ItemKind::Static(..)
2530+
| ItemKind::Const(..)
2531+
| ItemKind::Use(..)
2532+
| ItemKind::ExternCrate(..)
2533+
| ItemKind::MacroDef(..)
2534+
| ItemKind::GlobalAsm(..)
2535+
| ItemKind::MacCall(..) => {}
2536+
}
2537+
visit::walk_item(self, item)
25132538
}
25142539
}
25152540

25162541
impl<'a> Resolver<'a> {
25172542
pub(crate) fn late_resolve_crate(&mut self, krate: &Crate) {
2543+
visit::walk_crate(&mut LifetimeCountVisitor { r: self }, krate);
25182544
let mut late_resolution_visitor = LateResolutionVisitor::new(self);
25192545
visit::walk_crate(&mut late_resolution_visitor, krate);
25202546
for (id, span) in late_resolution_visitor.diagnostic_metadata.unused_labels.iter() {

0 commit comments

Comments
 (0)