Skip to content

Commit 26eeec0

Browse files
committed
Compute item_generics_num_lifetimes during resolution.
1 parent 74fb87e commit 26eeec0

File tree

4 files changed

+30
-54
lines changed

4 files changed

+30
-54
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ use rustc_ast::node_id::NodeMap;
3939
use rustc_ast::token::{self, Token};
4040
use rustc_ast::tokenstream::{CanSynthesizeMissingTokens, TokenStream, TokenTree};
4141
use rustc_ast::visit::{self, AssocCtxt, Visitor};
42-
use rustc_ast::walk_list;
4342
use rustc_ast::{self as ast, *};
4443
use rustc_ast_pretty::pprust;
4544
use rustc_data_structures::captures::Captures;
@@ -48,7 +47,7 @@ use rustc_data_structures::sync::Lrc;
4847
use rustc_errors::{struct_span_err, Applicability};
4948
use rustc_hir as hir;
5049
use rustc_hir::def::{DefKind, Namespace, PartialRes, PerNS, Res};
51-
use rustc_hir::def_id::{DefId, DefIdMap, DefPathHash, LocalDefId, CRATE_DEF_ID};
50+
use rustc_hir::def_id::{DefId, DefPathHash, LocalDefId, CRATE_DEF_ID};
5251
use rustc_hir::definitions::{DefKey, DefPathData, Definitions};
5352
use rustc_hir::intravisit;
5453
use rustc_hir::{ConstArg, GenericArg, InferKind, ParamName};
@@ -159,8 +158,6 @@ struct LoweringContext<'a, 'hir: 'a> {
159158

160159
current_module: LocalDefId,
161160

162-
type_def_lifetime_params: DefIdMap<usize>,
163-
164161
current_hir_id_owner: (LocalDefId, u32),
165162
item_local_id_counters: NodeMap<u32>,
166163
node_id_to_hir_id: IndexVec<NodeId, Option<hir::HirId>>,
@@ -172,7 +169,7 @@ struct LoweringContext<'a, 'hir: 'a> {
172169
pub trait ResolverAstLowering {
173170
fn def_key(&mut self, id: DefId) -> DefKey;
174171

175-
fn item_generics_num_lifetimes(&self, def: DefId, sess: &Session) -> usize;
172+
fn item_generics_num_lifetimes(&self, def: DefId) -> usize;
176173

177174
fn legacy_const_generic_args(&mut self, expr: &Expr) -> Option<Vec<usize>>;
178175

@@ -336,7 +333,6 @@ pub fn lower_crate<'a, 'hir>(
336333
is_in_trait_impl: false,
337334
is_in_dyn_type: false,
338335
anonymous_lifetime_mode: AnonymousLifetimeMode::PassThrough,
339-
type_def_lifetime_params: Default::default(),
340336
current_module: CRATE_DEF_ID,
341337
current_hir_id_owner: (CRATE_DEF_ID, 0),
342338
item_local_id_counters: Default::default(),
@@ -452,26 +448,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
452448
fn visit_item(&mut self, item: &'tcx Item) {
453449
self.lctx.allocate_hir_id_counter(item.id);
454450

455-
match item.kind {
456-
ItemKind::Struct(_, ref generics)
457-
| ItemKind::Union(_, ref generics)
458-
| ItemKind::Enum(_, ref generics)
459-
| ItemKind::TyAlias(box TyAliasKind(_, ref generics, ..))
460-
| ItemKind::Trait(box TraitKind(_, _, ref generics, ..)) => {
461-
let def_id = self.lctx.resolver.local_def_id(item.id);
462-
let count = generics
463-
.params
464-
.iter()
465-
.filter(|param| {
466-
matches!(param.kind, ast::GenericParamKind::Lifetime { .. })
467-
})
468-
.count();
469-
self.lctx.type_def_lifetime_params.insert(def_id.to_def_id(), count);
470-
}
471-
ItemKind::Use(ref use_tree) => {
472-
self.allocate_use_tree_hir_id_counters(use_tree);
473-
}
474-
_ => {}
451+
if let ItemKind::Use(ref use_tree) = item.kind {
452+
self.allocate_use_tree_hir_id_counters(use_tree);
475453
}
476454

477455
visit::walk_item(self, item);
@@ -486,23 +464,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
486464
self.lctx.allocate_hir_id_counter(item.id);
487465
visit::walk_foreign_item(self, item);
488466
}
489-
490-
fn visit_ty(&mut self, t: &'tcx Ty) {
491-
match t.kind {
492-
// Mirrors the case in visit::walk_ty
493-
TyKind::BareFn(ref f) => {
494-
walk_list!(self, visit_generic_param, &f.generic_params);
495-
// Mirrors visit::walk_fn_decl
496-
for parameter in &f.decl.inputs {
497-
// We don't lower the ids of argument patterns
498-
self.visit_pat(&parameter.pat);
499-
self.visit_ty(&parameter.ty)
500-
}
501-
self.visit_fn_ret_ty(&f.decl.output)
502-
}
503-
_ => visit::walk_ty(self, t),
504-
}
505-
}
506467
}
507468

508469
self.lower_node_id(CRATE_NODE_ID);

compiler/rustc_ast_lowering/src/path.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
9090
_ => ParenthesizedGenericArgs::Err,
9191
};
9292

93-
let num_lifetimes = type_def_id.map_or(0, |def_id| {
94-
if let Some(&n) = self.type_def_lifetime_params.get(&def_id) {
95-
return n;
96-
}
97-
assert!(!def_id.is_local());
98-
let n = self.resolver.item_generics_num_lifetimes(def_id, self.sess);
99-
self.type_def_lifetime_params.insert(def_id, n);
100-
n
101-
});
93+
let num_lifetimes = type_def_id
94+
.map_or(0, |def_id| self.resolver.item_generics_num_lifetimes(def_id));
10295
self.lower_path_segment(
10396
p.span,
10497
segment,

compiler/rustc_resolve/src/late.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
952952
match item.kind {
953953
ItemKind::TyAlias(box TyAliasKind(_, ref generics, _, _))
954954
| ItemKind::Fn(box FnKind(_, _, ref generics, _)) => {
955+
self.compute_num_lifetime_params(item.id, generics);
955956
self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| {
956957
visit::walk_item(this, item)
957958
});
@@ -960,6 +961,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
960961
ItemKind::Enum(_, ref generics)
961962
| ItemKind::Struct(_, ref generics)
962963
| ItemKind::Union(_, ref generics) => {
964+
self.compute_num_lifetime_params(item.id, generics);
963965
self.resolve_adt(item, generics);
964966
}
965967

@@ -970,10 +972,12 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
970972
items: ref impl_items,
971973
..
972974
}) => {
975+
self.compute_num_lifetime_params(item.id, generics);
973976
self.resolve_implementation(generics, of_trait, &self_ty, item.id, impl_items);
974977
}
975978

976979
ItemKind::Trait(box TraitKind(.., ref generics, ref bounds, ref trait_items)) => {
980+
self.compute_num_lifetime_params(item.id, generics);
977981
// Create a new rib for the trait-wide type parameters.
978982
self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| {
979983
let local_def_id = this.r.local_def_id(item.id).to_def_id();
@@ -1025,6 +1029,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
10251029
}
10261030

10271031
ItemKind::TraitAlias(ref generics, ref bounds) => {
1032+
self.compute_num_lifetime_params(item.id, generics);
10281033
// Create a new rib for the trait-wide type parameters.
10291034
self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| {
10301035
let local_def_id = this.r.local_def_id(item.id).to_def_id();
@@ -2463,6 +2468,16 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
24632468
Some((ident.name, ns)),
24642469
)
24652470
}
2471+
2472+
fn compute_num_lifetime_params(&mut self, id: NodeId, generics: &Generics) {
2473+
let def_id = self.r.local_def_id(id);
2474+
let count = generics
2475+
.params
2476+
.iter()
2477+
.filter(|param| matches!(param.kind, ast::GenericParamKind::Lifetime { .. }))
2478+
.count();
2479+
self.r.item_generics_num_lifetimes.insert(def_id, count);
2480+
}
24662481
}
24672482

24682483
impl<'a> Resolver<'a> {

compiler/rustc_resolve/src/lib.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,8 @@ pub struct Resolver<'a> {
10301030
trait_impl_items: FxHashSet<LocalDefId>,
10311031

10321032
legacy_const_generic_args: FxHashMap<DefId, Option<Vec<usize>>>,
1033+
/// Amount of lifetime parameters for each item in the crate.
1034+
item_generics_num_lifetimes: FxHashMap<LocalDefId, usize>,
10331035

10341036
main_def: Option<MainDefinition>,
10351037
}
@@ -1109,8 +1111,12 @@ impl ResolverAstLowering for Resolver<'_> {
11091111
}
11101112
}
11111113

1112-
fn item_generics_num_lifetimes(&self, def_id: DefId, sess: &Session) -> usize {
1113-
self.cstore().item_generics_num_lifetimes(def_id, sess)
1114+
fn item_generics_num_lifetimes(&self, def_id: DefId) -> usize {
1115+
if let Some(def_id) = def_id.as_local() {
1116+
self.item_generics_num_lifetimes[&def_id]
1117+
} else {
1118+
self.cstore().item_generics_num_lifetimes(def_id, self.session)
1119+
}
11141120
}
11151121

11161122
fn legacy_const_generic_args(&mut self, expr: &Expr) -> Option<Vec<usize>> {
@@ -1390,6 +1396,7 @@ impl<'a> Resolver<'a> {
13901396
next_disambiguator: Default::default(),
13911397
trait_impl_items: Default::default(),
13921398
legacy_const_generic_args: Default::default(),
1399+
item_generics_num_lifetimes: Default::default(),
13931400
main_def: Default::default(),
13941401
};
13951402

0 commit comments

Comments
 (0)