diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_hir_def/src/body.rs | 17 | ||||
-rw-r--r-- | crates/ra_hir_def/src/body/lower.rs | 8 | ||||
-rw-r--r-- | crates/ra_hir_def/src/data.rs | 10 |
3 files changed, 24 insertions, 11 deletions
diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index ff0758da0..48b797dd6 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs | |||
@@ -24,9 +24,11 @@ use crate::{ | |||
24 | src::HasSource, | 24 | src::HasSource, |
25 | AsMacroCall, DefWithBodyId, HasModule, Lookup, ModuleId, | 25 | AsMacroCall, DefWithBodyId, HasModule, Lookup, ModuleId, |
26 | }; | 26 | }; |
27 | use ra_cfg::CfgOptions; | ||
27 | 28 | ||
28 | pub(crate) struct Expander { | 29 | pub(crate) struct Expander { |
29 | crate_def_map: Arc<CrateDefMap>, | 30 | crate_def_map: Arc<CrateDefMap>, |
31 | cfg_options: CfgOptions, | ||
30 | current_file_id: HirFileId, | 32 | current_file_id: HirFileId, |
31 | hygiene: Hygiene, | 33 | hygiene: Hygiene, |
32 | ast_id_map: Arc<AstIdMap>, | 34 | ast_id_map: Arc<AstIdMap>, |
@@ -43,7 +45,16 @@ impl Expander { | |||
43 | let crate_def_map = db.crate_def_map(module.krate); | 45 | let crate_def_map = db.crate_def_map(module.krate); |
44 | let hygiene = Hygiene::new(db.upcast(), current_file_id); | 46 | let hygiene = Hygiene::new(db.upcast(), current_file_id); |
45 | let ast_id_map = db.ast_id_map(current_file_id); | 47 | let ast_id_map = db.ast_id_map(current_file_id); |
46 | Expander { crate_def_map, current_file_id, hygiene, ast_id_map, module, recursive_limit: 0 } | 48 | let cfg_options = db.crate_graph()[module.krate].cfg_options.clone(); |
49 | Expander { | ||
50 | crate_def_map, | ||
51 | cfg_options, | ||
52 | current_file_id, | ||
53 | hygiene, | ||
54 | ast_id_map, | ||
55 | module, | ||
56 | recursive_limit: 0, | ||
57 | } | ||
47 | } | 58 | } |
48 | 59 | ||
49 | pub(crate) fn enter_expand<T: ast::AstNode>( | 60 | pub(crate) fn enter_expand<T: ast::AstNode>( |
@@ -107,6 +118,10 @@ impl Expander { | |||
107 | Attrs::new(owner, &self.hygiene) | 118 | Attrs::new(owner, &self.hygiene) |
108 | } | 119 | } |
109 | 120 | ||
121 | pub(crate) fn check_cfg(&self, attrs: &Attrs) -> bool { | ||
122 | attrs.is_cfg_enabled(&self.cfg_options) | ||
123 | } | ||
124 | |||
110 | fn parse_path(&mut self, path: ast::Path) -> Option<Path> { | 125 | fn parse_path(&mut self, path: ast::Path) -> Option<Path> { |
111 | Path::from_src(path, &self.hygiene) | 126 | Path::from_src(path, &self.hygiene) |
112 | } | 127 | } |
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index 06df88a34..6f56d3d7a 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs | |||
@@ -29,8 +29,8 @@ use crate::{ | |||
29 | path::GenericArgs, | 29 | path::GenericArgs, |
30 | path::Path, | 30 | path::Path, |
31 | type_ref::{Mutability, TypeRef}, | 31 | type_ref::{Mutability, TypeRef}, |
32 | AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, HasModule, Intern, | 32 | AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern, ModuleDefId, |
33 | ModuleDefId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, | 33 | StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, |
34 | }; | 34 | }; |
35 | 35 | ||
36 | use super::{ExprSource, PatSource}; | 36 | use super::{ExprSource, PatSource}; |
@@ -298,7 +298,6 @@ impl ExprCollector<'_> { | |||
298 | self.alloc_expr(Expr::Return { expr }, syntax_ptr) | 298 | self.alloc_expr(Expr::Return { expr }, syntax_ptr) |
299 | } | 299 | } |
300 | ast::Expr::RecordLit(e) => { | 300 | ast::Expr::RecordLit(e) => { |
301 | let crate_graph = self.db.crate_graph(); | ||
302 | let path = e.path().and_then(|path| self.expander.parse_path(path)); | 301 | let path = e.path().and_then(|path| self.expander.parse_path(path)); |
303 | let mut field_ptrs = Vec::new(); | 302 | let mut field_ptrs = Vec::new(); |
304 | let record_lit = if let Some(nfl) = e.record_field_list() { | 303 | let record_lit = if let Some(nfl) = e.record_field_list() { |
@@ -306,10 +305,9 @@ impl ExprCollector<'_> { | |||
306 | .fields() | 305 | .fields() |
307 | .inspect(|field| field_ptrs.push(AstPtr::new(field))) | 306 | .inspect(|field| field_ptrs.push(AstPtr::new(field))) |
308 | .filter_map(|field| { | 307 | .filter_map(|field| { |
309 | let module_id = ContainerId::DefWithBodyId(self.def).module(self.db); | ||
310 | let attrs = self.expander.parse_attrs(&field); | 308 | let attrs = self.expander.parse_attrs(&field); |
311 | 309 | ||
312 | if !attrs.is_cfg_enabled(&crate_graph[module_id.krate].cfg_options) { | 310 | if !self.expander.check_cfg(&attrs) { |
313 | return None; | 311 | return None; |
314 | } | 312 | } |
315 | 313 | ||
diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs index b8fbf0ed4..dd0e679e8 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs | |||
@@ -20,7 +20,7 @@ use crate::{ | |||
20 | type_ref::{Mutability, TypeBound, TypeRef}, | 20 | type_ref::{Mutability, TypeBound, TypeRef}, |
21 | visibility::RawVisibility, | 21 | visibility::RawVisibility, |
22 | AssocContainerId, AssocItemId, ConstId, ConstLoc, Expander, FunctionId, FunctionLoc, HasModule, | 22 | AssocContainerId, AssocItemId, ConstId, ConstLoc, Expander, FunctionId, FunctionLoc, HasModule, |
23 | ImplId, Intern, Lookup, ModuleId, StaticId, TraitId, TypeAliasId, TypeAliasLoc, | 23 | ImplId, Intern, Lookup, StaticId, TraitId, TypeAliasId, TypeAliasLoc, |
24 | }; | 24 | }; |
25 | 25 | ||
26 | #[derive(Debug, Clone, PartialEq, Eq)] | 26 | #[derive(Debug, Clone, PartialEq, Eq)] |
@@ -218,10 +218,11 @@ impl ImplData { | |||
218 | let mut items = Vec::new(); | 218 | let mut items = Vec::new(); |
219 | 219 | ||
220 | if let Some(item_list) = src.value.item_list() { | 220 | if let Some(item_list) = src.value.item_list() { |
221 | let mut expander = Expander::new(db, impl_loc.ast_id.file_id, module_id); | ||
221 | items.extend(collect_impl_items(db, item_list.impl_items(), src.file_id, id)); | 222 | items.extend(collect_impl_items(db, item_list.impl_items(), src.file_id, id)); |
222 | items.extend(collect_impl_items_in_macros( | 223 | items.extend(collect_impl_items_in_macros( |
223 | db, | 224 | db, |
224 | module_id, | 225 | &mut expander, |
225 | &src.with_value(item_list), | 226 | &src.with_value(item_list), |
226 | id, | 227 | id, |
227 | )); | 228 | )); |
@@ -268,18 +269,17 @@ impl ConstData { | |||
268 | 269 | ||
269 | fn collect_impl_items_in_macros( | 270 | fn collect_impl_items_in_macros( |
270 | db: &dyn DefDatabase, | 271 | db: &dyn DefDatabase, |
271 | module_id: ModuleId, | 272 | expander: &mut Expander, |
272 | impl_def: &InFile<ast::ItemList>, | 273 | impl_def: &InFile<ast::ItemList>, |
273 | id: ImplId, | 274 | id: ImplId, |
274 | ) -> Vec<AssocItemId> { | 275 | ) -> Vec<AssocItemId> { |
275 | let mut expander = Expander::new(db, impl_def.file_id, module_id); | ||
276 | let mut res = Vec::new(); | 276 | let mut res = Vec::new(); |
277 | 277 | ||
278 | // We set a limit to protect against infinite recursion | 278 | // We set a limit to protect against infinite recursion |
279 | let limit = 100; | 279 | let limit = 100; |
280 | 280 | ||
281 | for m in impl_def.value.syntax().children().filter_map(ast::MacroCall::cast) { | 281 | for m in impl_def.value.syntax().children().filter_map(ast::MacroCall::cast) { |
282 | res.extend(collect_impl_items_in_macro(db, &mut expander, m, id, limit)) | 282 | res.extend(collect_impl_items_in_macro(db, expander, m, id, limit)) |
283 | } | 283 | } |
284 | 284 | ||
285 | res | 285 | res |