mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 06:11:35 +00:00
simplify storing generic parameter attributes in item tree
This commit is contained in:
parent
37a8493138
commit
3205ed7a41
2 changed files with 35 additions and 43 deletions
|
@ -21,7 +21,7 @@ use crate::{
|
||||||
db::DefDatabase,
|
db::DefDatabase,
|
||||||
dyn_map::{keys, DynMap},
|
dyn_map::{keys, DynMap},
|
||||||
expander::Expander,
|
expander::Expander,
|
||||||
item_tree::ItemTree,
|
item_tree::{AttrOwner, ItemTree},
|
||||||
lower::LowerCtx,
|
lower::LowerCtx,
|
||||||
nameres::{DefMap, MacroSubNs},
|
nameres::{DefMap, MacroSubNs},
|
||||||
src::{HasChildSource, HasSource},
|
src::{HasChildSource, HasSource},
|
||||||
|
@ -215,9 +215,14 @@ impl GenericParams {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn fill(&mut self, lower_ctx: &LowerCtx<'_>, node: &dyn HasGenericParams) {
|
pub(crate) fn fill(
|
||||||
|
&mut self,
|
||||||
|
lower_ctx: &LowerCtx<'_>,
|
||||||
|
node: &dyn HasGenericParams,
|
||||||
|
add_param_attrs: impl FnMut(AttrOwner, ast::GenericParam),
|
||||||
|
) {
|
||||||
if let Some(params) = node.generic_param_list() {
|
if let Some(params) = node.generic_param_list() {
|
||||||
self.fill_params(lower_ctx, params)
|
self.fill_params(lower_ctx, params, add_param_attrs)
|
||||||
}
|
}
|
||||||
if let Some(where_clause) = node.where_clause() {
|
if let Some(where_clause) = node.where_clause() {
|
||||||
self.fill_where_predicates(lower_ctx, where_clause);
|
self.fill_where_predicates(lower_ctx, where_clause);
|
||||||
|
@ -235,7 +240,12 @@ impl GenericParams {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fill_params(&mut self, lower_ctx: &LowerCtx<'_>, params: ast::GenericParamList) {
|
fn fill_params(
|
||||||
|
&mut self,
|
||||||
|
lower_ctx: &LowerCtx<'_>,
|
||||||
|
params: ast::GenericParamList,
|
||||||
|
mut add_param_attrs: impl FnMut(AttrOwner, ast::GenericParam),
|
||||||
|
) {
|
||||||
for type_or_const_param in params.type_or_const_params() {
|
for type_or_const_param in params.type_or_const_params() {
|
||||||
match type_or_const_param {
|
match type_or_const_param {
|
||||||
ast::TypeOrConstParam::Type(type_param) => {
|
ast::TypeOrConstParam::Type(type_param) => {
|
||||||
|
@ -249,13 +259,14 @@ impl GenericParams {
|
||||||
default,
|
default,
|
||||||
provenance: TypeParamProvenance::TypeParamList,
|
provenance: TypeParamProvenance::TypeParamList,
|
||||||
};
|
};
|
||||||
self.type_or_consts.alloc(param.into());
|
let idx = self.type_or_consts.alloc(param.into());
|
||||||
let type_ref = TypeRef::Path(name.into());
|
let type_ref = TypeRef::Path(name.into());
|
||||||
self.fill_bounds(
|
self.fill_bounds(
|
||||||
lower_ctx,
|
lower_ctx,
|
||||||
type_param.type_bound_list(),
|
type_param.type_bound_list(),
|
||||||
Either::Left(type_ref),
|
Either::Left(type_ref),
|
||||||
);
|
);
|
||||||
|
add_param_attrs(idx.into(), ast::GenericParam::TypeParam(type_param));
|
||||||
}
|
}
|
||||||
ast::TypeOrConstParam::Const(const_param) => {
|
ast::TypeOrConstParam::Const(const_param) => {
|
||||||
let name = const_param.name().map_or_else(Name::missing, |it| it.as_name());
|
let name = const_param.name().map_or_else(Name::missing, |it| it.as_name());
|
||||||
|
@ -267,7 +278,8 @@ impl GenericParams {
|
||||||
ty: Interned::new(ty),
|
ty: Interned::new(ty),
|
||||||
has_default: const_param.default_val().is_some(),
|
has_default: const_param.default_val().is_some(),
|
||||||
};
|
};
|
||||||
self.type_or_consts.alloc(param.into());
|
let idx = self.type_or_consts.alloc(param.into());
|
||||||
|
add_param_attrs(idx.into(), ast::GenericParam::ConstParam(const_param));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -275,13 +287,14 @@ impl GenericParams {
|
||||||
let name =
|
let name =
|
||||||
lifetime_param.lifetime().map_or_else(Name::missing, |lt| Name::new_lifetime(<));
|
lifetime_param.lifetime().map_or_else(Name::missing, |lt| Name::new_lifetime(<));
|
||||||
let param = LifetimeParamData { name: name.clone() };
|
let param = LifetimeParamData { name: name.clone() };
|
||||||
self.lifetimes.alloc(param);
|
let idx = self.lifetimes.alloc(param);
|
||||||
let lifetime_ref = LifetimeRef::new_name(name);
|
let lifetime_ref = LifetimeRef::new_name(name);
|
||||||
self.fill_bounds(
|
self.fill_bounds(
|
||||||
lower_ctx,
|
lower_ctx,
|
||||||
lifetime_param.type_bound_list(),
|
lifetime_param.type_bound_list(),
|
||||||
Either::Right(lifetime_ref),
|
Either::Right(lifetime_ref),
|
||||||
);
|
);
|
||||||
|
add_param_attrs(idx.into(), ast::GenericParam::LifetimeParam(lifetime_param));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -602,44 +602,23 @@ impl<'a> Ctx<'a> {
|
||||||
generics.fill_bounds(&self.body_ctx, bounds, Either::Left(self_param));
|
generics.fill_bounds(&self.body_ctx, bounds, Either::Left(self_param));
|
||||||
}
|
}
|
||||||
|
|
||||||
generics.fill(&self.body_ctx, node);
|
let add_param_attrs = |item, param| {
|
||||||
|
let attrs = RawAttrs::new(self.db.upcast(), ¶m, self.body_ctx.hygiene());
|
||||||
|
// This is identical to the body of `Ctx::add_attrs()` but we can't call that here
|
||||||
|
// because it requires `&mut self` and the call to `generics.fill()` below also
|
||||||
|
// references `self`.
|
||||||
|
match self.tree.attrs.entry(item) {
|
||||||
|
Entry::Occupied(mut entry) => {
|
||||||
|
*entry.get_mut() = entry.get().merge(attrs);
|
||||||
|
}
|
||||||
|
Entry::Vacant(entry) => {
|
||||||
|
entry.insert(attrs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
generics.fill(&self.body_ctx, node, add_param_attrs);
|
||||||
|
|
||||||
generics.shrink_to_fit();
|
generics.shrink_to_fit();
|
||||||
|
|
||||||
if let Some(params) = node.generic_param_list() {
|
|
||||||
let params_by_name: FxHashMap<_, _> = params
|
|
||||||
.generic_params()
|
|
||||||
.filter_map(|param| {
|
|
||||||
let name = match ¶m {
|
|
||||||
ast::GenericParam::ConstParam(param) => param.name()?.as_name(),
|
|
||||||
ast::GenericParam::LifetimeParam(param) => {
|
|
||||||
Name::new_lifetime(¶m.lifetime()?)
|
|
||||||
}
|
|
||||||
ast::GenericParam::TypeParam(param) => param.name()?.as_name(),
|
|
||||||
};
|
|
||||||
Some((name, param))
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
for (idx, param) in generics.type_or_consts.iter() {
|
|
||||||
if let Some(name) = param.name() {
|
|
||||||
if let Some(param) = params_by_name.get(name) {
|
|
||||||
self.add_attrs(
|
|
||||||
idx.into(),
|
|
||||||
RawAttrs::new(self.db.upcast(), param, self.hygiene()),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (idx, param) in generics.lifetimes.iter() {
|
|
||||||
if let Some(param) = params_by_name.get(¶m.name) {
|
|
||||||
self.add_attrs(
|
|
||||||
idx.into(),
|
|
||||||
RawAttrs::new(self.db.upcast(), param, self.hygiene()),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Interned::new(generics)
|
Interned::new(generics)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue