mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-23 21:23:20 +00:00
Compute item_generics_num_lifetimes during resolution.
This commit is contained in:
parent
74fb87e3a0
commit
26eeec0baf
@ -39,7 +39,6 @@ use rustc_ast::node_id::NodeMap;
|
||||
use rustc_ast::token::{self, Token};
|
||||
use rustc_ast::tokenstream::{CanSynthesizeMissingTokens, TokenStream, TokenTree};
|
||||
use rustc_ast::visit::{self, AssocCtxt, Visitor};
|
||||
use rustc_ast::walk_list;
|
||||
use rustc_ast::{self as ast, *};
|
||||
use rustc_ast_pretty::pprust;
|
||||
use rustc_data_structures::captures::Captures;
|
||||
@ -48,7 +47,7 @@ use rustc_data_structures::sync::Lrc;
|
||||
use rustc_errors::{struct_span_err, Applicability};
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::def::{DefKind, Namespace, PartialRes, PerNS, Res};
|
||||
use rustc_hir::def_id::{DefId, DefIdMap, DefPathHash, LocalDefId, CRATE_DEF_ID};
|
||||
use rustc_hir::def_id::{DefId, DefPathHash, LocalDefId, CRATE_DEF_ID};
|
||||
use rustc_hir::definitions::{DefKey, DefPathData, Definitions};
|
||||
use rustc_hir::intravisit;
|
||||
use rustc_hir::{ConstArg, GenericArg, InferKind, ParamName};
|
||||
@ -159,8 +158,6 @@ struct LoweringContext<'a, 'hir: 'a> {
|
||||
|
||||
current_module: LocalDefId,
|
||||
|
||||
type_def_lifetime_params: DefIdMap<usize>,
|
||||
|
||||
current_hir_id_owner: (LocalDefId, u32),
|
||||
item_local_id_counters: NodeMap<u32>,
|
||||
node_id_to_hir_id: IndexVec<NodeId, Option<hir::HirId>>,
|
||||
@ -172,7 +169,7 @@ struct LoweringContext<'a, 'hir: 'a> {
|
||||
pub trait ResolverAstLowering {
|
||||
fn def_key(&mut self, id: DefId) -> DefKey;
|
||||
|
||||
fn item_generics_num_lifetimes(&self, def: DefId, sess: &Session) -> usize;
|
||||
fn item_generics_num_lifetimes(&self, def: DefId) -> usize;
|
||||
|
||||
fn legacy_const_generic_args(&mut self, expr: &Expr) -> Option<Vec<usize>>;
|
||||
|
||||
@ -336,7 +333,6 @@ pub fn lower_crate<'a, 'hir>(
|
||||
is_in_trait_impl: false,
|
||||
is_in_dyn_type: false,
|
||||
anonymous_lifetime_mode: AnonymousLifetimeMode::PassThrough,
|
||||
type_def_lifetime_params: Default::default(),
|
||||
current_module: CRATE_DEF_ID,
|
||||
current_hir_id_owner: (CRATE_DEF_ID, 0),
|
||||
item_local_id_counters: Default::default(),
|
||||
@ -452,26 +448,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
fn visit_item(&mut self, item: &'tcx Item) {
|
||||
self.lctx.allocate_hir_id_counter(item.id);
|
||||
|
||||
match item.kind {
|
||||
ItemKind::Struct(_, ref generics)
|
||||
| ItemKind::Union(_, ref generics)
|
||||
| ItemKind::Enum(_, ref generics)
|
||||
| ItemKind::TyAlias(box TyAliasKind(_, ref generics, ..))
|
||||
| ItemKind::Trait(box TraitKind(_, _, ref generics, ..)) => {
|
||||
let def_id = self.lctx.resolver.local_def_id(item.id);
|
||||
let count = generics
|
||||
.params
|
||||
.iter()
|
||||
.filter(|param| {
|
||||
matches!(param.kind, ast::GenericParamKind::Lifetime { .. })
|
||||
})
|
||||
.count();
|
||||
self.lctx.type_def_lifetime_params.insert(def_id.to_def_id(), count);
|
||||
}
|
||||
ItemKind::Use(ref use_tree) => {
|
||||
self.allocate_use_tree_hir_id_counters(use_tree);
|
||||
}
|
||||
_ => {}
|
||||
if let ItemKind::Use(ref use_tree) = item.kind {
|
||||
self.allocate_use_tree_hir_id_counters(use_tree);
|
||||
}
|
||||
|
||||
visit::walk_item(self, item);
|
||||
@ -486,23 +464,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
self.lctx.allocate_hir_id_counter(item.id);
|
||||
visit::walk_foreign_item(self, item);
|
||||
}
|
||||
|
||||
fn visit_ty(&mut self, t: &'tcx Ty) {
|
||||
match t.kind {
|
||||
// Mirrors the case in visit::walk_ty
|
||||
TyKind::BareFn(ref f) => {
|
||||
walk_list!(self, visit_generic_param, &f.generic_params);
|
||||
// Mirrors visit::walk_fn_decl
|
||||
for parameter in &f.decl.inputs {
|
||||
// We don't lower the ids of argument patterns
|
||||
self.visit_pat(¶meter.pat);
|
||||
self.visit_ty(¶meter.ty)
|
||||
}
|
||||
self.visit_fn_ret_ty(&f.decl.output)
|
||||
}
|
||||
_ => visit::walk_ty(self, t),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.lower_node_id(CRATE_NODE_ID);
|
||||
|
@ -90,15 +90,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
_ => ParenthesizedGenericArgs::Err,
|
||||
};
|
||||
|
||||
let num_lifetimes = type_def_id.map_or(0, |def_id| {
|
||||
if let Some(&n) = self.type_def_lifetime_params.get(&def_id) {
|
||||
return n;
|
||||
}
|
||||
assert!(!def_id.is_local());
|
||||
let n = self.resolver.item_generics_num_lifetimes(def_id, self.sess);
|
||||
self.type_def_lifetime_params.insert(def_id, n);
|
||||
n
|
||||
});
|
||||
let num_lifetimes = type_def_id
|
||||
.map_or(0, |def_id| self.resolver.item_generics_num_lifetimes(def_id));
|
||||
self.lower_path_segment(
|
||||
p.span,
|
||||
segment,
|
||||
|
@ -952,6 +952,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|
||||
match item.kind {
|
||||
ItemKind::TyAlias(box TyAliasKind(_, ref generics, _, _))
|
||||
| ItemKind::Fn(box FnKind(_, _, ref generics, _)) => {
|
||||
self.compute_num_lifetime_params(item.id, generics);
|
||||
self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| {
|
||||
visit::walk_item(this, item)
|
||||
});
|
||||
@ -960,6 +961,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|
||||
ItemKind::Enum(_, ref generics)
|
||||
| ItemKind::Struct(_, ref generics)
|
||||
| ItemKind::Union(_, ref generics) => {
|
||||
self.compute_num_lifetime_params(item.id, generics);
|
||||
self.resolve_adt(item, generics);
|
||||
}
|
||||
|
||||
@ -970,10 +972,12 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|
||||
items: ref impl_items,
|
||||
..
|
||||
}) => {
|
||||
self.compute_num_lifetime_params(item.id, generics);
|
||||
self.resolve_implementation(generics, of_trait, &self_ty, item.id, impl_items);
|
||||
}
|
||||
|
||||
ItemKind::Trait(box TraitKind(.., ref generics, ref bounds, ref trait_items)) => {
|
||||
self.compute_num_lifetime_params(item.id, generics);
|
||||
// Create a new rib for the trait-wide type parameters.
|
||||
self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| {
|
||||
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> {
|
||||
}
|
||||
|
||||
ItemKind::TraitAlias(ref generics, ref bounds) => {
|
||||
self.compute_num_lifetime_params(item.id, generics);
|
||||
// Create a new rib for the trait-wide type parameters.
|
||||
self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| {
|
||||
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> {
|
||||
Some((ident.name, ns)),
|
||||
)
|
||||
}
|
||||
|
||||
fn compute_num_lifetime_params(&mut self, id: NodeId, generics: &Generics) {
|
||||
let def_id = self.r.local_def_id(id);
|
||||
let count = generics
|
||||
.params
|
||||
.iter()
|
||||
.filter(|param| matches!(param.kind, ast::GenericParamKind::Lifetime { .. }))
|
||||
.count();
|
||||
self.r.item_generics_num_lifetimes.insert(def_id, count);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Resolver<'a> {
|
||||
|
@ -1030,6 +1030,8 @@ pub struct Resolver<'a> {
|
||||
trait_impl_items: FxHashSet<LocalDefId>,
|
||||
|
||||
legacy_const_generic_args: FxHashMap<DefId, Option<Vec<usize>>>,
|
||||
/// Amount of lifetime parameters for each item in the crate.
|
||||
item_generics_num_lifetimes: FxHashMap<LocalDefId, usize>,
|
||||
|
||||
main_def: Option<MainDefinition>,
|
||||
}
|
||||
@ -1109,8 +1111,12 @@ impl ResolverAstLowering for Resolver<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
fn item_generics_num_lifetimes(&self, def_id: DefId, sess: &Session) -> usize {
|
||||
self.cstore().item_generics_num_lifetimes(def_id, sess)
|
||||
fn item_generics_num_lifetimes(&self, def_id: DefId) -> usize {
|
||||
if let Some(def_id) = def_id.as_local() {
|
||||
self.item_generics_num_lifetimes[&def_id]
|
||||
} else {
|
||||
self.cstore().item_generics_num_lifetimes(def_id, self.session)
|
||||
}
|
||||
}
|
||||
|
||||
fn legacy_const_generic_args(&mut self, expr: &Expr) -> Option<Vec<usize>> {
|
||||
@ -1390,6 +1396,7 @@ impl<'a> Resolver<'a> {
|
||||
next_disambiguator: Default::default(),
|
||||
trait_impl_items: Default::default(),
|
||||
legacy_const_generic_args: Default::default(),
|
||||
item_generics_num_lifetimes: Default::default(),
|
||||
main_def: Default::default(),
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user