aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/impl_block.rs
diff options
context:
space:
mode:
authoruHOOCCOOHu <[email protected]>2019-09-30 10:47:17 +0100
committeruHOOCCOOHu <[email protected]>2019-10-02 19:28:02 +0100
commita49ad47e5afa5950f92b77badc6679295101328a (patch)
treedfb48fb37c1db1ecbcfb7bf75f5acf0f9023d29d /crates/ra_hir/src/impl_block.rs
parentd2ea776b8fbb5286a04dde75a9a8f8b14f12bfe9 (diff)
Support cfg attribute on impl blocks
Diffstat (limited to 'crates/ra_hir/src/impl_block.rs')
-rw-r--r--crates/ra_hir/src/impl_block.rs30
1 files changed, 26 insertions, 4 deletions
diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs
index 8cf74ddc7..7877c3171 100644
--- a/crates/ra_hir/src/impl_block.rs
+++ b/crates/ra_hir/src/impl_block.rs
@@ -4,12 +4,14 @@ use rustc_hash::FxHashMap;
4use std::sync::Arc; 4use std::sync::Arc;
5 5
6use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId}; 6use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId};
7use ra_cfg::CfgOptions;
7use ra_syntax::{ 8use ra_syntax::{
8 ast::{self, AstNode}, 9 ast::{self, AstNode},
9 AstPtr, 10 AstPtr,
10}; 11};
11 12
12use crate::{ 13use crate::{
14 attr::Attr,
13 code_model::{Module, ModuleSource}, 15 code_model::{Module, ModuleSource},
14 db::{AstDatabase, DefDatabase, HirDatabase}, 16 db::{AstDatabase, DefDatabase, HirDatabase},
15 generics::HasGenericParams, 17 generics::HasGenericParams,
@@ -176,6 +178,7 @@ pub struct ModuleImplBlocks {
176impl ModuleImplBlocks { 178impl ModuleImplBlocks {
177 fn collect( 179 fn collect(
178 db: &(impl DefDatabase + AstDatabase), 180 db: &(impl DefDatabase + AstDatabase),
181 cfg_options: &CfgOptions,
179 module: Module, 182 module: Module,
180 source_map: &mut ImplSourceMap, 183 source_map: &mut ImplSourceMap,
181 ) -> Self { 184 ) -> Self {
@@ -188,11 +191,11 @@ impl ModuleImplBlocks {
188 let src = m.module.definition_source(db); 191 let src = m.module.definition_source(db);
189 match &src.ast { 192 match &src.ast {
190 ModuleSource::SourceFile(node) => { 193 ModuleSource::SourceFile(node) => {
191 m.collect_from_item_owner(db, source_map, node, src.file_id) 194 m.collect_from_item_owner(db, cfg_options, source_map, node, src.file_id)
192 } 195 }
193 ModuleSource::Module(node) => { 196 ModuleSource::Module(node) => {
194 let item_list = node.item_list().expect("inline module should have item list"); 197 let item_list = node.item_list().expect("inline module should have item list");
195 m.collect_from_item_owner(db, source_map, &item_list, src.file_id) 198 m.collect_from_item_owner(db, cfg_options, source_map, &item_list, src.file_id)
196 } 199 }
197 }; 200 };
198 m 201 m
@@ -201,6 +204,7 @@ impl ModuleImplBlocks {
201 fn collect_from_item_owner( 204 fn collect_from_item_owner(
202 &mut self, 205 &mut self,
203 db: &(impl DefDatabase + AstDatabase), 206 db: &(impl DefDatabase + AstDatabase),
207 cfg_options: &CfgOptions,
204 source_map: &mut ImplSourceMap, 208 source_map: &mut ImplSourceMap,
205 owner: &dyn ast::ModuleItemOwner, 209 owner: &dyn ast::ModuleItemOwner,
206 file_id: HirFileId, 210 file_id: HirFileId,
@@ -208,6 +212,11 @@ impl ModuleImplBlocks {
208 for item in owner.items_with_macros() { 212 for item in owner.items_with_macros() {
209 match item { 213 match item {
210 ast::ItemOrMacro::Item(ast::ModuleItem::ImplBlock(impl_block_ast)) => { 214 ast::ItemOrMacro::Item(ast::ModuleItem::ImplBlock(impl_block_ast)) => {
215 let attrs = Attr::from_attrs_owner(file_id, &impl_block_ast, db);
216 if attrs.iter().any(|attr| attr.is_cfg_enabled(cfg_options) == Some(false)) {
217 continue;
218 }
219
211 let impl_block = ImplData::from_ast(db, file_id, self.module, &impl_block_ast); 220 let impl_block = ImplData::from_ast(db, file_id, self.module, &impl_block_ast);
212 let id = self.impls.alloc(impl_block); 221 let id = self.impls.alloc(impl_block);
213 for &impl_item in &self.impls[id].items { 222 for &impl_item in &self.impls[id].items {
@@ -218,6 +227,11 @@ impl ModuleImplBlocks {
218 } 227 }
219 ast::ItemOrMacro::Item(_) => (), 228 ast::ItemOrMacro::Item(_) => (),
220 ast::ItemOrMacro::Macro(macro_call) => { 229 ast::ItemOrMacro::Macro(macro_call) => {
230 let attrs = Attr::from_attrs_owner(file_id, &macro_call, db);
231 if attrs.iter().any(|attr| attr.is_cfg_enabled(cfg_options) == Some(false)) {
232 continue;
233 }
234
221 //FIXME: we should really cut down on the boilerplate required to process a macro 235 //FIXME: we should really cut down on the boilerplate required to process a macro
222 let ast_id = db.ast_id_map(file_id).ast_id(&macro_call).with_file_id(file_id); 236 let ast_id = db.ast_id_map(file_id).ast_id(&macro_call).with_file_id(file_id);
223 if let Some(path) = macro_call 237 if let Some(path) = macro_call
@@ -231,7 +245,13 @@ impl ModuleImplBlocks {
231 if let Some(item_list) = 245 if let Some(item_list) =
232 db.parse_or_expand(file_id).and_then(ast::MacroItems::cast) 246 db.parse_or_expand(file_id).and_then(ast::MacroItems::cast)
233 { 247 {
234 self.collect_from_item_owner(db, source_map, &item_list, file_id) 248 self.collect_from_item_owner(
249 db,
250 cfg_options,
251 source_map,
252 &item_list,
253 file_id,
254 )
235 } 255 }
236 } 256 }
237 } 257 }
@@ -246,8 +266,10 @@ pub(crate) fn impls_in_module_with_source_map_query(
246 module: Module, 266 module: Module,
247) -> (Arc<ModuleImplBlocks>, Arc<ImplSourceMap>) { 267) -> (Arc<ModuleImplBlocks>, Arc<ImplSourceMap>) {
248 let mut source_map = ImplSourceMap::default(); 268 let mut source_map = ImplSourceMap::default();
269 let crate_graph = db.crate_graph();
270 let cfg_options = crate_graph.cfg_options(module.krate.crate_id());
249 271
250 let result = ModuleImplBlocks::collect(db, module, &mut source_map); 272 let result = ModuleImplBlocks::collect(db, cfg_options, module, &mut source_map);
251 (Arc::new(result), Arc::new(source_map)) 273 (Arc::new(result), Arc::new(source_map))
252} 274}
253 275