This commit is contained in:
uHOOCCOOHu 2019-10-03 01:38:56 +08:00
parent 43f09ad36c
commit e0100e63ae
No known key found for this signature in database
GPG Key ID: CED392DE0C483D00
5 changed files with 28 additions and 13 deletions

View File

@ -15,7 +15,7 @@ pub enum CfgExpr {
impl CfgExpr { impl CfgExpr {
/// Fold the cfg by querying all basic `Atom` and `KeyValue` predicates. /// Fold the cfg by querying all basic `Atom` and `KeyValue` predicates.
pub fn fold(&self, query: &impl Fn(&SmolStr, Option<&SmolStr>) -> bool) -> Option<bool> { pub fn fold(&self, query: &dyn Fn(&SmolStr, Option<&SmolStr>) -> bool) -> Option<bool> {
match self { match self {
CfgExpr::Invalid => None, CfgExpr::Invalid => None,
CfgExpr::Atom(name) => Some(query(name, None)), CfgExpr::Atom(name) => Some(query(name, None)),

View File

@ -45,10 +45,15 @@ impl Attr {
pub(crate) fn from_attrs_owner( pub(crate) fn from_attrs_owner(
file_id: HirFileId, file_id: HirFileId,
owner: &impl AttrsOwner, owner: &dyn AttrsOwner,
db: &impl AstDatabase, db: &impl AstDatabase,
) -> Arc<[Attr]> { ) -> Option<Arc<[Attr]>> {
owner.attrs().flat_map(|ast| Attr::from_src(Source { file_id, ast }, db)).collect() let mut attrs = owner.attrs().peekable();
if attrs.peek().is_none() {
// Avoid heap allocation
return None;
}
Some(attrs.flat_map(|ast| Attr::from_src(Source { file_id, ast }, db)).collect())
} }
pub(crate) fn is_simple_atom(&self, name: &str) -> bool { pub(crate) fn is_simple_atom(&self, name: &str) -> bool {

View File

@ -213,7 +213,9 @@ impl ModuleImplBlocks {
match item { match item {
ast::ItemOrMacro::Item(ast::ModuleItem::ImplBlock(impl_block_ast)) => { ast::ItemOrMacro::Item(ast::ModuleItem::ImplBlock(impl_block_ast)) => {
let attrs = Attr::from_attrs_owner(file_id, &impl_block_ast, db); let attrs = Attr::from_attrs_owner(file_id, &impl_block_ast, db);
if attrs.iter().any(|attr| attr.is_cfg_enabled(cfg_options) == Some(false)) { if attrs.map_or(false, |attrs| {
attrs.iter().any(|attr| attr.is_cfg_enabled(cfg_options) == Some(false))
}) {
continue; continue;
} }
@ -228,7 +230,9 @@ impl ModuleImplBlocks {
ast::ItemOrMacro::Item(_) => (), ast::ItemOrMacro::Item(_) => (),
ast::ItemOrMacro::Macro(macro_call) => { ast::ItemOrMacro::Macro(macro_call) => {
let attrs = Attr::from_attrs_owner(file_id, &macro_call, db); let attrs = Attr::from_attrs_owner(file_id, &macro_call, db);
if attrs.iter().any(|attr| attr.is_cfg_enabled(cfg_options) == Some(false)) { if attrs.map_or(false, |attrs| {
attrs.iter().any(|attr| attr.is_cfg_enabled(cfg_options) == Some(false))
}) {
continue; continue;
} }

View File

@ -7,7 +7,6 @@ use rustc_hash::FxHashMap;
use test_utils::tested_by; use test_utils::tested_by;
use crate::{ use crate::{
attr::Attr,
db::DefDatabase, db::DefDatabase,
ids::{AstItemDef, LocationCtx, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind}, ids::{AstItemDef, LocationCtx, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind},
name::MACRO_RULES, name::MACRO_RULES,
@ -715,8 +714,12 @@ where
} }
} }
fn is_cfg_enabled(&self, attrs: &[Attr]) -> bool { fn is_cfg_enabled(&self, attrs: &raw::Attrs) -> bool {
attrs.iter().all(|attr| attr.is_cfg_enabled(&self.def_collector.cfg_options) != Some(false)) attrs.as_ref().map_or(true, |attrs| {
attrs
.iter()
.all(|attr| attr.is_cfg_enabled(&self.def_collector.cfg_options) != Some(false))
})
} }
} }

View File

@ -120,9 +120,12 @@ impl Index<Macro> for RawItems {
} }
} }
// Avoid heap allocation on items without attributes.
pub(super) type Attrs = Option<Arc<[Attr]>>;
#[derive(Debug, PartialEq, Eq, Clone)] #[derive(Debug, PartialEq, Eq, Clone)]
pub(super) struct RawItem { pub(super) struct RawItem {
pub(super) attrs: Arc<[Attr]>, pub(super) attrs: Attrs,
pub(super) kind: RawItemKind, pub(super) kind: RawItemKind,
} }
@ -390,7 +393,7 @@ impl<DB: AstDatabase> RawItemsCollector<&DB> {
fn push_import( fn push_import(
&mut self, &mut self,
current_module: Option<Module>, current_module: Option<Module>,
attrs: Arc<[Attr]>, attrs: Attrs,
data: ImportData, data: ImportData,
source: ImportSourcePtr, source: ImportSourcePtr,
) { ) {
@ -399,7 +402,7 @@ impl<DB: AstDatabase> RawItemsCollector<&DB> {
self.push_item(current_module, attrs, RawItemKind::Import(import)) self.push_item(current_module, attrs, RawItemKind::Import(import))
} }
fn push_item(&mut self, current_module: Option<Module>, attrs: Arc<[Attr]>, kind: RawItemKind) { fn push_item(&mut self, current_module: Option<Module>, attrs: Attrs, kind: RawItemKind) {
match current_module { match current_module {
Some(module) => match &mut self.raw_items.modules[module] { Some(module) => match &mut self.raw_items.modules[module] {
ModuleData::Definition { items, .. } => items, ModuleData::Definition { items, .. } => items,
@ -410,7 +413,7 @@ impl<DB: AstDatabase> RawItemsCollector<&DB> {
.push(RawItem { attrs, kind }) .push(RawItem { attrs, kind })
} }
fn parse_attrs(&self, item: &impl ast::AttrsOwner) -> Arc<[Attr]> { fn parse_attrs(&self, item: &impl ast::AttrsOwner) -> Attrs {
Attr::from_attrs_owner(self.file_id, item, self.db) Attr::from_attrs_owner(self.file_id, item, self.db)
} }
} }