From e42f9627664cc3c44094e1c4f985270fbfd592b1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 22 Nov 2019 11:27:47 +0300 Subject: Encapsulate Attrs --- crates/ra_hir_def/src/nameres/collector.rs | 20 ++++++++------------ crates/ra_hir_def/src/nameres/raw.rs | 18 +++++++----------- 2 files changed, 15 insertions(+), 23 deletions(-) (limited to 'crates/ra_hir_def/src/nameres') diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index aae3dcadf..7902293e8 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -12,7 +12,7 @@ use rustc_hash::FxHashMap; use test_utils::tested_by; use crate::{ - attr::Attr, + attr::Attrs, db::DefDatabase2, nameres::{ diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint, @@ -549,7 +549,7 @@ where // `#[macro_use] extern crate` is hoisted to imports macros before collecting // any other items. for item in items { - if self.is_cfg_enabled(item.attrs()) { + if self.is_cfg_enabled(&item.attrs) { if let raw::RawItemKind::Import(import_id) = item.kind { let import = self.raw_items[import_id].clone(); if import.is_extern_crate && import.is_macro_use { @@ -560,10 +560,10 @@ where } for item in items { - if self.is_cfg_enabled(item.attrs()) { + if self.is_cfg_enabled(&item.attrs) { match item.kind { raw::RawItemKind::Module(m) => { - self.collect_module(&self.raw_items[m], item.attrs()) + self.collect_module(&self.raw_items[m], &item.attrs) } raw::RawItemKind::Import(import_id) => self .def_collector @@ -585,9 +585,9 @@ where } } - fn collect_module(&mut self, module: &raw::ModuleData, attrs: &[Attr]) { + fn collect_module(&mut self, module: &raw::ModuleData, attrs: &Attrs) { let path_attr = self.path_attr(attrs); - let is_macro_use = self.is_macro_use(attrs); + let is_macro_use = attrs.has_atom("macro_use"); match module { // inline module, just recurse raw::ModuleData::Definition { name, items, ast_id } => { @@ -779,17 +779,13 @@ where } } - fn is_cfg_enabled(&self, attrs: &[Attr]) -> bool { + fn is_cfg_enabled(&self, attrs: &Attrs) -> bool { attrs.iter().all(|attr| attr.is_cfg_enabled(&self.def_collector.cfg_options) != Some(false)) } - fn path_attr<'a>(&self, attrs: &'a [Attr]) -> Option<&'a SmolStr> { + fn path_attr<'a>(&self, attrs: &'a Attrs) -> Option<&'a SmolStr> { attrs.iter().find_map(|attr| attr.as_path()) } - - fn is_macro_use<'a>(&self, attrs: &'a [Attr]) -> bool { - attrs.iter().any(|attr| attr.is_simple_atom("macro_use")) - } } fn is_macro_rules(path: &Path) -> bool { diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 7c68fd638..55a9634f8 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -16,7 +16,12 @@ use ra_syntax::{ }; use test_utils::tested_by; -use crate::{attr::Attr, db::DefDatabase2, path::Path, FileAstId, HirFileId, ModuleSource, Source}; +use crate::{ + attr::{Attr, Attrs}, + db::DefDatabase2, + path::Path, + FileAstId, HirFileId, ModuleSource, Source, +}; /// `RawItems` is a set of top-level items in a file (except for impls). /// @@ -129,21 +134,12 @@ impl Index for RawItems { } } -// Avoid heap allocation on items without attributes. -type Attrs = Option>; - #[derive(Debug, PartialEq, Eq, Clone)] pub(super) struct RawItem { - attrs: Attrs, + pub(super) attrs: Attrs, pub(super) kind: RawItemKind, } -impl RawItem { - pub(super) fn attrs(&self) -> &[Attr] { - self.attrs.as_ref().map_or(&[], |it| &*it) - } -} - #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub(super) enum RawItemKind { Module(Module), -- cgit v1.2.3