mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-28 10:39:45 +00:00
Remove AttrOwner
This commit is contained in:
parent
f27268f319
commit
83a7ecdead
6 changed files with 60 additions and 69 deletions
|
|
@ -26,7 +26,6 @@ use crate::{
|
|||
AdtId, AstIdLoc, AttrDefId, GenericParamId, HasModule, LocalFieldId, Lookup, MacroId,
|
||||
VariantId,
|
||||
db::DefDatabase,
|
||||
item_tree::AttrOwner,
|
||||
lang_item::LangItem,
|
||||
nameres::{ModuleOrigin, ModuleSource},
|
||||
src::{HasChildSource, HasSource},
|
||||
|
|
@ -526,23 +525,22 @@ impl AttrsWithOwner {
|
|||
ModuleOrigin::File { definition, declaration_tree_id, declaration, .. } => {
|
||||
let decl_attrs = declaration_tree_id
|
||||
.item_tree(db)
|
||||
.raw_attrs(AttrOwner::Item(declaration.erase()))
|
||||
.raw_attrs(declaration.upcast())
|
||||
.clone();
|
||||
let tree = db.file_item_tree(definition.into());
|
||||
let def_attrs = tree.raw_attrs(AttrOwner::TopLevel).clone();
|
||||
let def_attrs = tree.top_level_raw_attrs().clone();
|
||||
decl_attrs.merge(def_attrs)
|
||||
}
|
||||
ModuleOrigin::CrateRoot { definition } => {
|
||||
let tree = db.file_item_tree(definition.into());
|
||||
tree.raw_attrs(AttrOwner::TopLevel).clone()
|
||||
tree.top_level_raw_attrs().clone()
|
||||
}
|
||||
ModuleOrigin::Inline { definition_tree_id, definition } => {
|
||||
definition_tree_id.item_tree(db).raw_attrs(definition.upcast()).clone()
|
||||
}
|
||||
ModuleOrigin::Inline { definition_tree_id, definition } => definition_tree_id
|
||||
.item_tree(db)
|
||||
.raw_attrs(AttrOwner::Item(definition.erase()))
|
||||
.clone(),
|
||||
ModuleOrigin::BlockExpr { id, .. } => {
|
||||
let tree = db.block_item_tree(id);
|
||||
tree.raw_attrs(AttrOwner::TopLevel).clone()
|
||||
tree.top_level_raw_attrs().clone()
|
||||
}
|
||||
};
|
||||
Attrs::expand_cfg_attr(db, module.krate, raw_attrs)
|
||||
|
|
|
|||
|
|
@ -376,7 +376,7 @@ fn include_macro_invoc(
|
|||
fn crate_supports_no_std(db: &dyn DefDatabase, crate_id: Crate) -> bool {
|
||||
let file = crate_id.data(db).root_file_id(db);
|
||||
let item_tree = db.file_item_tree(file.into());
|
||||
let attrs = item_tree.raw_attrs(crate::item_tree::AttrOwner::TopLevel);
|
||||
let attrs = item_tree.top_level_raw_attrs();
|
||||
for attr in &**attrs {
|
||||
match attr.path().as_ident() {
|
||||
Some(ident) if *ident == sym::no_std => return true,
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ use hir_expand::{
|
|||
use intern::Interned;
|
||||
use la_arena::Idx;
|
||||
use rustc_hash::FxHashMap;
|
||||
use span::{AstIdNode, Edition, ErasedFileAstId, FileAstId, SyntaxContext};
|
||||
use span::{AstIdNode, Edition, FileAstId, SyntaxContext};
|
||||
use stdx::never;
|
||||
use syntax::{SyntaxKind, ast, match_ast};
|
||||
use triomphe::Arc;
|
||||
|
|
@ -89,9 +89,8 @@ impl fmt::Debug for RawVisibilityId {
|
|||
#[derive(Debug, Default, Eq, PartialEq)]
|
||||
pub struct ItemTree {
|
||||
top_level: Box<[ModItemId]>,
|
||||
// Consider splitting this into top level RawAttrs and the map?
|
||||
attrs: FxHashMap<AttrOwner, RawAttrs>,
|
||||
|
||||
top_attrs: RawAttrs,
|
||||
attrs: FxHashMap<FileAstId<ast::Item>, RawAttrs>,
|
||||
vis: ItemVisibilities,
|
||||
// FIXME: They values store the key, turn this into a FxHashSet<ModItem> instead?
|
||||
data: FxHashMap<FileAstId<ast::Item>, ModItem>,
|
||||
|
|
@ -104,12 +103,13 @@ impl ItemTree {
|
|||
|
||||
let ctx = lower::Ctx::new(db, file_id);
|
||||
let syntax = db.parse_or_expand(file_id);
|
||||
let mut top_attrs = None;
|
||||
let mut item_tree = match_ast! {
|
||||
match syntax {
|
||||
ast::SourceFile(file) => {
|
||||
top_attrs = Some(RawAttrs::new(db, &file, ctx.span_map()));
|
||||
ctx.lower_module_items(&file)
|
||||
let top_attrs = RawAttrs::new(db, &file, ctx.span_map());
|
||||
let mut item_tree = ctx.lower_module_items(&file);
|
||||
item_tree.top_attrs = top_attrs;
|
||||
item_tree
|
||||
},
|
||||
ast::MacroItems(items) => {
|
||||
ctx.lower_module_items(&items)
|
||||
|
|
@ -128,10 +128,10 @@ impl ItemTree {
|
|||
}
|
||||
};
|
||||
|
||||
if let Some(attrs) = top_attrs {
|
||||
item_tree.attrs.insert(AttrOwner::TopLevel, attrs);
|
||||
}
|
||||
if item_tree.data.is_empty() && item_tree.top_level.is_empty() && item_tree.attrs.is_empty()
|
||||
if item_tree.data.is_empty()
|
||||
&& item_tree.top_level.is_empty()
|
||||
&& item_tree.attrs.is_empty()
|
||||
&& item_tree.top_attrs.is_empty()
|
||||
{
|
||||
EMPTY
|
||||
.get_or_init(|| {
|
||||
|
|
@ -139,6 +139,7 @@ impl ItemTree {
|
|||
top_level: Box::new([]),
|
||||
attrs: FxHashMap::default(),
|
||||
data: FxHashMap::default(),
|
||||
top_attrs: RawAttrs::EMPTY,
|
||||
vis: ItemVisibilities { arena: Box::new([]) },
|
||||
})
|
||||
})
|
||||
|
|
@ -158,7 +159,10 @@ impl ItemTree {
|
|||
|
||||
let ctx = lower::Ctx::new(db, loc.ast_id.file_id);
|
||||
let mut item_tree = ctx.lower_block(&block);
|
||||
if item_tree.data.is_empty() && item_tree.top_level.is_empty() && item_tree.attrs.is_empty()
|
||||
if item_tree.data.is_empty()
|
||||
&& item_tree.top_level.is_empty()
|
||||
&& item_tree.attrs.is_empty()
|
||||
&& item_tree.top_attrs.is_empty()
|
||||
{
|
||||
EMPTY
|
||||
.get_or_init(|| {
|
||||
|
|
@ -166,6 +170,7 @@ impl ItemTree {
|
|||
top_level: Box::new([]),
|
||||
attrs: FxHashMap::default(),
|
||||
data: FxHashMap::default(),
|
||||
top_attrs: RawAttrs::EMPTY,
|
||||
vis: ItemVisibilities { arena: Box::new([]) },
|
||||
})
|
||||
})
|
||||
|
|
@ -183,19 +188,25 @@ impl ItemTree {
|
|||
}
|
||||
|
||||
/// Returns the inner attributes of the source file.
|
||||
pub fn top_level_attrs(&self, db: &dyn DefDatabase, krate: Crate) -> Attrs {
|
||||
Attrs::expand_cfg_attr(
|
||||
db,
|
||||
krate,
|
||||
self.attrs.get(&AttrOwner::TopLevel).unwrap_or(&RawAttrs::EMPTY).clone(),
|
||||
)
|
||||
pub fn top_level_raw_attrs(&self) -> &RawAttrs {
|
||||
&self.top_attrs
|
||||
}
|
||||
|
||||
pub(crate) fn raw_attrs(&self, of: AttrOwner) -> &RawAttrs {
|
||||
/// Returns the inner attributes of the source file.
|
||||
pub fn top_level_attrs(&self, db: &dyn DefDatabase, krate: Crate) -> Attrs {
|
||||
Attrs::expand_cfg_attr(db, krate, self.top_attrs.clone())
|
||||
}
|
||||
|
||||
pub(crate) fn raw_attrs(&self, of: FileAstId<ast::Item>) -> &RawAttrs {
|
||||
self.attrs.get(&of).unwrap_or(&RawAttrs::EMPTY)
|
||||
}
|
||||
|
||||
pub(crate) fn attrs(&self, db: &dyn DefDatabase, krate: Crate, of: AttrOwner) -> Attrs {
|
||||
pub(crate) fn attrs(
|
||||
&self,
|
||||
db: &dyn DefDatabase,
|
||||
krate: Crate,
|
||||
of: FileAstId<ast::Item>,
|
||||
) -> Attrs {
|
||||
Attrs::expand_cfg_attr(db, krate, self.raw_attrs(of).clone())
|
||||
}
|
||||
|
||||
|
|
@ -226,7 +237,7 @@ impl ItemTree {
|
|||
}
|
||||
|
||||
fn shrink_to_fit(&mut self) {
|
||||
let ItemTree { top_level: _, attrs, data, vis: _ } = self;
|
||||
let ItemTree { top_level: _, attrs, data, vis: _, top_attrs: _ } = self;
|
||||
attrs.shrink_to_fit();
|
||||
data.shrink_to_fit();
|
||||
}
|
||||
|
|
@ -266,21 +277,6 @@ pub struct ItemTreeDataStats {
|
|||
pub macro_rules: usize,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
||||
pub enum AttrOwner {
|
||||
/// Attributes on an item.
|
||||
Item(ErasedFileAstId),
|
||||
/// Inner attributes of the source file.
|
||||
TopLevel,
|
||||
}
|
||||
|
||||
impl From<ModItemId> for AttrOwner {
|
||||
#[inline]
|
||||
fn from(value: ModItemId) -> Self {
|
||||
AttrOwner::Item(value.ast_id().erase())
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait implemented by all nodes in the item tree.
|
||||
pub trait ItemTreeNode: Clone {
|
||||
type Source: AstIdNode;
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ use hir_expand::{
|
|||
span_map::{SpanMap, SpanMapRef},
|
||||
};
|
||||
use la_arena::Arena;
|
||||
use span::{AstIdMap, SyntaxContext};
|
||||
use span::{AstIdMap, FileAstId, SyntaxContext};
|
||||
use syntax::{
|
||||
AstNode,
|
||||
ast::{self, HasModuleItem, HasName},
|
||||
|
|
@ -20,10 +20,10 @@ use triomphe::Arc;
|
|||
use crate::{
|
||||
db::DefDatabase,
|
||||
item_tree::{
|
||||
AttrOwner, Const, Enum, ExternBlock, ExternCrate, FieldsShape, Function, Impl, ImportAlias,
|
||||
Interned, ItemTree, ItemTreeAstId, Macro2, MacroCall, MacroRules, Mod, ModItem, ModItemId,
|
||||
ModKind, ModPath, RawAttrs, RawVisibility, RawVisibilityId, Static, Struct, StructKind,
|
||||
Trait, TraitAlias, TypeAlias, Union, Use, UseTree, UseTreeKind, VisibilityExplicitness,
|
||||
Const, Enum, ExternBlock, ExternCrate, FieldsShape, Function, Impl, ImportAlias, Interned,
|
||||
ItemTree, ItemTreeAstId, Macro2, MacroCall, MacroRules, Mod, ModItem, ModItemId, ModKind,
|
||||
ModPath, RawAttrs, RawVisibility, RawVisibilityId, Static, Struct, StructKind, Trait,
|
||||
TraitAlias, TypeAlias, Union, Use, UseTree, UseTreeKind, VisibilityExplicitness,
|
||||
},
|
||||
};
|
||||
|
||||
|
|
@ -97,7 +97,7 @@ impl<'a> Ctx<'a> {
|
|||
}
|
||||
|
||||
pub(super) fn lower_block(mut self, block: &ast::BlockExpr) -> ItemTree {
|
||||
self.tree.attrs.insert(AttrOwner::TopLevel, RawAttrs::new(self.db, block, self.span_map()));
|
||||
self.tree.top_attrs = RawAttrs::new(self.db, block, self.span_map());
|
||||
self.top_level = block
|
||||
.statements()
|
||||
.filter_map(|stmt| match stmt {
|
||||
|
|
@ -144,12 +144,12 @@ impl<'a> Ctx<'a> {
|
|||
ast::Item::ExternBlock(ast) => self.lower_extern_block(ast).into(),
|
||||
};
|
||||
let attrs = RawAttrs::new(self.db, item, self.span_map());
|
||||
self.add_attrs(mod_item.into(), attrs);
|
||||
self.add_attrs(mod_item.ast_id(), attrs);
|
||||
|
||||
Some(mod_item)
|
||||
}
|
||||
|
||||
fn add_attrs(&mut self, item: AttrOwner, attrs: RawAttrs) {
|
||||
fn add_attrs(&mut self, item: FileAstId<ast::Item>, attrs: RawAttrs) {
|
||||
if !attrs.is_empty() {
|
||||
match self.tree.attrs.entry(item) {
|
||||
Entry::Occupied(mut entry) => {
|
||||
|
|
@ -365,7 +365,7 @@ impl<'a> Ctx<'a> {
|
|||
ast::ExternItem::MacroCall(call) => self.lower_macro_call(call)?.into(),
|
||||
};
|
||||
let attrs = RawAttrs::new(self.db, &item, self.span_map());
|
||||
self.add_attrs(mod_item.into(), attrs);
|
||||
self.add_attrs(mod_item.ast_id(), attrs);
|
||||
Some(mod_item)
|
||||
})
|
||||
.collect()
|
||||
|
|
|
|||
|
|
@ -6,10 +6,9 @@ use span::{Edition, ErasedFileAstId};
|
|||
|
||||
use crate::{
|
||||
item_tree::{
|
||||
AttrOwner, Const, DefDatabase, Enum, ExternBlock, ExternCrate, FieldsShape, Function, Impl,
|
||||
ItemTree, Macro2, MacroCall, MacroRules, Mod, ModItemId, ModKind, RawAttrs,
|
||||
RawVisibilityId, Static, Struct, Trait, TraitAlias, TypeAlias, Union, Use, UseTree,
|
||||
UseTreeKind,
|
||||
Const, DefDatabase, Enum, ExternBlock, ExternCrate, FieldsShape, Function, Impl, ItemTree,
|
||||
Macro2, MacroCall, MacroRules, Mod, ModItemId, ModKind, RawAttrs, RawVisibilityId, Static,
|
||||
Struct, Trait, TraitAlias, TypeAlias, Union, Use, UseTree, UseTreeKind,
|
||||
},
|
||||
visibility::RawVisibility,
|
||||
};
|
||||
|
|
@ -18,9 +17,7 @@ pub(super) fn print_item_tree(db: &dyn DefDatabase, tree: &ItemTree, edition: Ed
|
|||
let mut p =
|
||||
Printer { db, tree, buf: String::new(), indent_level: 0, needs_indent: true, edition };
|
||||
|
||||
if let Some(attrs) = tree.attrs.get(&AttrOwner::TopLevel) {
|
||||
p.print_attrs(attrs, true, "\n");
|
||||
}
|
||||
p.print_attrs(&tree.top_attrs, true, "\n");
|
||||
p.blank();
|
||||
|
||||
for item in tree.top_level_items() {
|
||||
|
|
@ -102,8 +99,8 @@ impl Printer<'_> {
|
|||
}
|
||||
}
|
||||
|
||||
fn print_attrs_of(&mut self, of: impl Into<AttrOwner>, separated_by: &str) {
|
||||
if let Some(attrs) = self.tree.attrs.get(&of.into()) {
|
||||
fn print_attrs_of(&mut self, of: ModItemId, separated_by: &str) {
|
||||
if let Some(attrs) = self.tree.attrs.get(&of.ast_id()) {
|
||||
self.print_attrs(attrs, false, separated_by);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,8 +35,8 @@ use crate::{
|
|||
db::DefDatabase,
|
||||
item_scope::{GlobId, ImportId, ImportOrExternCrate, PerNsGlobImports},
|
||||
item_tree::{
|
||||
self, AttrOwner, FieldsShape, ImportAlias, ImportKind, ItemTree, ItemTreeAstId,
|
||||
ItemTreeNode, Macro2, MacroCall, MacroRules, Mod, ModItemId, ModKind, TreeId, UseTreeKind,
|
||||
self, FieldsShape, ImportAlias, ImportKind, ItemTree, ItemTreeAstId, ItemTreeNode, Macro2,
|
||||
MacroCall, MacroRules, Mod, ModItemId, ModKind, TreeId, UseTreeKind,
|
||||
},
|
||||
macro_call_as_call_id,
|
||||
nameres::{
|
||||
|
|
@ -1727,7 +1727,7 @@ impl ModCollector<'_, '_> {
|
|||
};
|
||||
|
||||
let mut process_mod_item = |item: ModItemId| {
|
||||
let attrs = self.item_tree.attrs(db, krate, item.into());
|
||||
let attrs = self.item_tree.attrs(db, krate, item.ast_id());
|
||||
if let Some(cfg) = attrs.cfg() {
|
||||
if !self.is_cfg_enabled(&cfg) {
|
||||
let ast_id = item.ast_id().erase();
|
||||
|
|
@ -2298,7 +2298,7 @@ impl ModCollector<'_, '_> {
|
|||
fn collect_macro_rules(&mut self, id: ItemTreeAstId<MacroRules>, module: ModuleId) {
|
||||
let krate = self.def_collector.def_map.krate;
|
||||
let mac = &self.item_tree[id];
|
||||
let attrs = self.item_tree.attrs(self.def_collector.db, krate, AttrOwner::Item(id.erase()));
|
||||
let attrs = self.item_tree.attrs(self.def_collector.db, krate, id.upcast());
|
||||
let ast_id = InFile::new(self.file_id(), mac.ast_id.upcast());
|
||||
|
||||
let export_attr = || attrs.by_key(sym::macro_export);
|
||||
|
|
@ -2387,7 +2387,7 @@ impl ModCollector<'_, '_> {
|
|||
|
||||
// Case 1: builtin macros
|
||||
let mut helpers_opt = None;
|
||||
let attrs = self.item_tree.attrs(self.def_collector.db, krate, AttrOwner::Item(id.erase()));
|
||||
let attrs = self.item_tree.attrs(self.def_collector.db, krate, id.upcast());
|
||||
let expander = if attrs.by_key(sym::rustc_builtin_macro).exists() {
|
||||
if let Some(expander) = find_builtin_macro(&mac.name) {
|
||||
match expander {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue