mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-03 12:13:43 +00:00
Simplify
This commit is contained in:
parent
7bd2e305d6
commit
10c7ee7068
@ -1,8 +1,9 @@
|
|||||||
//! Contains basic data about various HIR declarations.
|
//! Contains basic data about various HIR declarations.
|
||||||
|
|
||||||
use std::{mem, sync::Arc};
|
use std::sync::Arc;
|
||||||
|
|
||||||
use hir_expand::{name::Name, AstId, ExpandResult, HirFileId, InFile, MacroCallId, MacroDefKind};
|
use hir_expand::{name::Name, AstId, ExpandResult, HirFileId, MacroCallId, MacroDefKind};
|
||||||
|
use smallvec::SmallVec;
|
||||||
use syntax::ast;
|
use syntax::ast;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
@ -10,13 +11,13 @@ use crate::{
|
|||||||
body::{Expander, Mark},
|
body::{Expander, Mark},
|
||||||
db::DefDatabase,
|
db::DefDatabase,
|
||||||
intern::Interned,
|
intern::Interned,
|
||||||
item_tree::{self, AssocItem, FnFlags, ItemTreeId, ModItem, Param, TreeId},
|
item_tree::{self, AssocItem, FnFlags, ItemTree, ItemTreeId, ModItem, Param, TreeId},
|
||||||
nameres::{attr_resolution::ResolvedAttr, DefMap},
|
nameres::{attr_resolution::ResolvedAttr, DefMap},
|
||||||
type_ref::{TraitRef, TypeBound, TypeRef},
|
type_ref::{TraitRef, TypeBound, TypeRef},
|
||||||
visibility::RawVisibility,
|
visibility::RawVisibility,
|
||||||
AssocItemId, AstIdWithPath, ConstId, ConstLoc, FunctionId, FunctionLoc, HasModule, ImplId,
|
AssocItemId, AstIdWithPath, ConstId, ConstLoc, FunctionId, FunctionLoc, HasModule, ImplId,
|
||||||
Intern, ItemContainerId, Lookup, Macro2Id, MacroRulesId, ModuleId, ProcMacroId, StaticId,
|
Intern, ItemContainerId, ItemLoc, Lookup, Macro2Id, MacroRulesId, ModuleId, ProcMacroId,
|
||||||
TraitId, TypeAliasId, TypeAliasLoc,
|
StaticId, TraitId, TypeAliasId, TypeAliasLoc,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
@ -209,9 +210,9 @@ pub struct TraitData {
|
|||||||
|
|
||||||
impl TraitData {
|
impl TraitData {
|
||||||
pub(crate) fn trait_data_query(db: &dyn DefDatabase, tr: TraitId) -> Arc<TraitData> {
|
pub(crate) fn trait_data_query(db: &dyn DefDatabase, tr: TraitId) -> Arc<TraitData> {
|
||||||
let tr_loc = tr.lookup(db);
|
let tr_loc @ ItemLoc { container: module_id, id: tree_id } = tr.lookup(db);
|
||||||
let item_tree = tr_loc.id.item_tree(db);
|
let item_tree = tree_id.item_tree(db);
|
||||||
let tr_def = &item_tree[tr_loc.id.value];
|
let tr_def = &item_tree[tree_id.value];
|
||||||
let _cx = stdx::panic_context::enter(format!(
|
let _cx = stdx::panic_context::enter(format!(
|
||||||
"trait_data_query({:?} -> {:?} -> {:?})",
|
"trait_data_query({:?} -> {:?} -> {:?})",
|
||||||
tr, tr_loc, tr_def
|
tr, tr_loc, tr_def
|
||||||
@ -219,25 +220,21 @@ impl TraitData {
|
|||||||
let name = tr_def.name.clone();
|
let name = tr_def.name.clone();
|
||||||
let is_auto = tr_def.is_auto;
|
let is_auto = tr_def.is_auto;
|
||||||
let is_unsafe = tr_def.is_unsafe;
|
let is_unsafe = tr_def.is_unsafe;
|
||||||
let module_id = tr_loc.container;
|
|
||||||
let visibility = item_tree[tr_def.visibility].clone();
|
let visibility = item_tree[tr_def.visibility].clone();
|
||||||
let skip_array_during_method_dispatch = item_tree
|
let skip_array_during_method_dispatch = item_tree
|
||||||
.attrs(db, tr_loc.container.krate(), ModItem::from(tr_loc.id.value).into())
|
.attrs(db, module_id.krate(), ModItem::from(tree_id.value).into())
|
||||||
.by_key("rustc_skip_array_during_method_dispatch")
|
.by_key("rustc_skip_array_during_method_dispatch")
|
||||||
.exists();
|
.exists();
|
||||||
|
|
||||||
let mut collector = AssocItemCollector::new(
|
let mut collector =
|
||||||
db,
|
AssocItemCollector::new(db, module_id, tree_id.file_id(), ItemContainerId::TraitId(tr));
|
||||||
module_id,
|
collector.collect(&item_tree, tree_id.tree_id(), &tr_def.items);
|
||||||
tr_loc.id.file_id(),
|
let (items, attribute_calls) = collector.finish();
|
||||||
ItemContainerId::TraitId(tr),
|
|
||||||
);
|
|
||||||
collector.collect(tr_loc.id.tree_id(), &tr_def.items);
|
|
||||||
|
|
||||||
Arc::new(TraitData {
|
Arc::new(TraitData {
|
||||||
name,
|
name,
|
||||||
attribute_calls: collector.take_attr_calls(),
|
attribute_calls,
|
||||||
items: collector.items,
|
items,
|
||||||
is_auto,
|
is_auto,
|
||||||
is_unsafe,
|
is_unsafe,
|
||||||
visibility,
|
visibility,
|
||||||
@ -284,25 +281,20 @@ pub struct ImplData {
|
|||||||
impl ImplData {
|
impl ImplData {
|
||||||
pub(crate) fn impl_data_query(db: &dyn DefDatabase, id: ImplId) -> Arc<ImplData> {
|
pub(crate) fn impl_data_query(db: &dyn DefDatabase, id: ImplId) -> Arc<ImplData> {
|
||||||
let _p = profile::span("impl_data_query");
|
let _p = profile::span("impl_data_query");
|
||||||
let impl_loc = id.lookup(db);
|
let ItemLoc { container: module_id, id: tree_id } = id.lookup(db);
|
||||||
|
|
||||||
let item_tree = impl_loc.id.item_tree(db);
|
let item_tree = tree_id.item_tree(db);
|
||||||
let impl_def = &item_tree[impl_loc.id.value];
|
let impl_def = &item_tree[tree_id.value];
|
||||||
let target_trait = impl_def.target_trait.clone();
|
let target_trait = impl_def.target_trait.clone();
|
||||||
let self_ty = impl_def.self_ty.clone();
|
let self_ty = impl_def.self_ty.clone();
|
||||||
let is_negative = impl_def.is_negative;
|
let is_negative = impl_def.is_negative;
|
||||||
let module_id = impl_loc.container;
|
|
||||||
|
|
||||||
let mut collector = AssocItemCollector::new(
|
let mut collector =
|
||||||
db,
|
AssocItemCollector::new(db, module_id, tree_id.file_id(), ItemContainerId::ImplId(id));
|
||||||
module_id,
|
collector.collect(&item_tree, tree_id.tree_id(), &impl_def.items);
|
||||||
impl_loc.id.file_id(),
|
|
||||||
ItemContainerId::ImplId(id),
|
|
||||||
);
|
|
||||||
collector.collect(impl_loc.id.tree_id(), &impl_def.items);
|
|
||||||
|
|
||||||
let attribute_calls = collector.take_attr_calls();
|
let (items, attribute_calls) = collector.finish();
|
||||||
let items = collector.items.into_iter().map(|(_, item)| item).collect();
|
let items = items.into_iter().map(|(_, item)| item).collect();
|
||||||
|
|
||||||
Arc::new(ImplData { target_trait, self_ty, items, is_negative, attribute_calls })
|
Arc::new(ImplData { target_trait, self_ty, items, is_negative, attribute_calls })
|
||||||
}
|
}
|
||||||
@ -463,18 +455,19 @@ impl<'a> AssocItemCollector<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn take_attr_calls(&mut self) -> Option<Box<Vec<(AstId<ast::Item>, MacroCallId)>>> {
|
fn finish(
|
||||||
let attribute_calls = mem::take(&mut self.attr_calls);
|
self,
|
||||||
if attribute_calls.is_empty() {
|
) -> (Vec<(Name, AssocItemId)>, Option<Box<Vec<(AstId<ast::Item>, MacroCallId)>>>) {
|
||||||
None
|
(
|
||||||
} else {
|
self.items,
|
||||||
Some(Box::new(attribute_calls))
|
if self.attr_calls.is_empty() { None } else { Some(Box::new(self.attr_calls)) },
|
||||||
}
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: proc-macro diagnostics
|
// FIXME: proc-macro diagnostics
|
||||||
fn collect(&mut self, tree_id: TreeId, assoc_items: &[AssocItem]) {
|
fn collect(&mut self, item_tree: &ItemTree, tree_id: TreeId, assoc_items: &[AssocItem]) {
|
||||||
let item_tree = tree_id.item_tree(self.db);
|
let container = self.container;
|
||||||
|
self.items.reserve(assoc_items.len());
|
||||||
|
|
||||||
'items: for &item in assoc_items {
|
'items: for &item in assoc_items {
|
||||||
let attrs = item_tree.attrs(self.db, self.module_id.krate, ModItem::from(item).into());
|
let attrs = item_tree.attrs(self.db, self.module_id.krate, ModItem::from(item).into());
|
||||||
@ -509,9 +502,9 @@ impl<'a> AssocItemCollector<'a> {
|
|||||||
continue 'attrs;
|
continue 'attrs;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
match self.expander.enter_expand_id(self.db, call_id) {
|
match self.expander.enter_expand_id::<ast::MacroItems>(self.db, call_id) {
|
||||||
ExpandResult { value: Some((mark, mac)), .. } => {
|
ExpandResult { value: Some((mark, _)), .. } => {
|
||||||
self.collect_macro_items(mark, mac);
|
self.collect_macro_items(mark);
|
||||||
continue 'items;
|
continue 'items;
|
||||||
}
|
}
|
||||||
ExpandResult { .. } => {}
|
ExpandResult { .. } => {}
|
||||||
@ -522,44 +515,43 @@ impl<'a> AssocItemCollector<'a> {
|
|||||||
match item {
|
match item {
|
||||||
AssocItem::Function(id) => {
|
AssocItem::Function(id) => {
|
||||||
let item = &item_tree[id];
|
let item = &item_tree[id];
|
||||||
|
|
||||||
let def =
|
let def =
|
||||||
FunctionLoc { container: self.container, id: ItemTreeId::new(tree_id, id) }
|
FunctionLoc { container, id: ItemTreeId::new(tree_id, id) }.intern(self.db);
|
||||||
.intern(self.db);
|
|
||||||
self.items.push((item.name.clone(), def.into()));
|
self.items.push((item.name.clone(), def.into()));
|
||||||
}
|
}
|
||||||
AssocItem::Const(id) => {
|
AssocItem::Const(id) => {
|
||||||
let item = &item_tree[id];
|
let item = &item_tree[id];
|
||||||
|
|
||||||
let name = match item.name.clone() {
|
let name = match item.name.clone() {
|
||||||
Some(name) => name,
|
Some(name) => name,
|
||||||
None => continue,
|
None => continue,
|
||||||
};
|
};
|
||||||
let def =
|
let def =
|
||||||
ConstLoc { container: self.container, id: ItemTreeId::new(tree_id, id) }
|
ConstLoc { container, id: ItemTreeId::new(tree_id, id) }.intern(self.db);
|
||||||
.intern(self.db);
|
|
||||||
self.items.push((name, def.into()));
|
self.items.push((name, def.into()));
|
||||||
}
|
}
|
||||||
AssocItem::TypeAlias(id) => {
|
AssocItem::TypeAlias(id) => {
|
||||||
let item = &item_tree[id];
|
let item = &item_tree[id];
|
||||||
let def = TypeAliasLoc {
|
|
||||||
container: self.container,
|
let def = TypeAliasLoc { container, id: ItemTreeId::new(tree_id, id) }
|
||||||
id: ItemTreeId::new(tree_id, id),
|
.intern(self.db);
|
||||||
}
|
|
||||||
.intern(self.db);
|
|
||||||
self.items.push((item.name.clone(), def.into()));
|
self.items.push((item.name.clone(), def.into()));
|
||||||
}
|
}
|
||||||
AssocItem::MacroCall(call) => {
|
AssocItem::MacroCall(call) => {
|
||||||
let call = &item_tree[call];
|
|
||||||
let ast_id_map = self.db.ast_id_map(self.expander.current_file_id());
|
|
||||||
if let Some(root) = self.db.parse_or_expand(self.expander.current_file_id()) {
|
if let Some(root) = self.db.parse_or_expand(self.expander.current_file_id()) {
|
||||||
|
let call = &item_tree[call];
|
||||||
|
|
||||||
|
let ast_id_map = self.db.ast_id_map(self.expander.current_file_id());
|
||||||
let call = ast_id_map.get(call.ast_id).to_node(&root);
|
let call = ast_id_map.get(call.ast_id).to_node(&root);
|
||||||
let _cx = stdx::panic_context::enter(format!(
|
let _cx = stdx::panic_context::enter(format!(
|
||||||
"collect_items MacroCall: {}",
|
"collect_items MacroCall: {}",
|
||||||
call
|
call
|
||||||
));
|
));
|
||||||
let res = self.expander.enter_expand(self.db, call);
|
let res = self.expander.enter_expand::<ast::MacroItems>(self.db, call);
|
||||||
|
|
||||||
if let Ok(ExpandResult { value: Some((mark, mac)), .. }) = res {
|
if let Ok(ExpandResult { value: Some((mark, _)), .. }) = res {
|
||||||
self.collect_macro_items(mark, mac);
|
self.collect_macro_items(mark);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -567,14 +559,13 @@ impl<'a> AssocItemCollector<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn collect_macro_items(&mut self, mark: Mark, mac: ast::MacroItems) {
|
fn collect_macro_items(&mut self, mark: Mark) {
|
||||||
let src: InFile<ast::MacroItems> = self.expander.to_source(mac);
|
let tree_id = item_tree::TreeId::new(self.expander.current_file_id(), None);
|
||||||
let tree_id = item_tree::TreeId::new(src.file_id, None);
|
|
||||||
let item_tree = tree_id.item_tree(self.db);
|
let item_tree = tree_id.item_tree(self.db);
|
||||||
let iter: Vec<_> =
|
let iter: SmallVec<[_; 2]> =
|
||||||
item_tree.top_level_items().iter().filter_map(ModItem::as_assoc_item).collect();
|
item_tree.top_level_items().iter().filter_map(ModItem::as_assoc_item).collect();
|
||||||
|
|
||||||
self.collect(tree_id, &iter);
|
self.collect(&item_tree, tree_id, &iter);
|
||||||
|
|
||||||
self.expander.exit(self.db, mark);
|
self.expander.exit(self.db, mark);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user