diff options
Diffstat (limited to 'crates/ra_hir_def/src/data.rs')
-rw-r--r-- | crates/ra_hir_def/src/data.rs | 37 |
1 files changed, 29 insertions, 8 deletions
diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs index 04bd4a305..689bb6c5c 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs | |||
@@ -3,15 +3,18 @@ | |||
3 | use std::sync::Arc; | 3 | use std::sync::Arc; |
4 | 4 | ||
5 | use hir_expand::{ | 5 | use hir_expand::{ |
6 | hygiene::Hygiene, | ||
6 | name::{name, AsName, Name}, | 7 | name::{name, AsName, Name}, |
7 | AstId, InFile, | 8 | AstId, InFile, |
8 | }; | 9 | }; |
10 | use ra_cfg::CfgOptions; | ||
9 | use ra_prof::profile; | 11 | use ra_prof::profile; |
10 | use ra_syntax::ast::{ | 12 | use ra_syntax::ast::{ |
11 | self, AstNode, ImplItem, ModuleItemOwner, NameOwner, TypeAscriptionOwner, VisibilityOwner, | 13 | self, AstNode, ImplItem, ModuleItemOwner, NameOwner, TypeAscriptionOwner, VisibilityOwner, |
12 | }; | 14 | }; |
13 | 15 | ||
14 | use crate::{ | 16 | use crate::{ |
17 | attr::Attrs, | ||
15 | db::DefDatabase, | 18 | db::DefDatabase, |
16 | path::{path, GenericArgs, Path}, | 19 | path::{path, GenericArgs, Path}, |
17 | src::HasSource, | 20 | src::HasSource, |
@@ -26,6 +29,7 @@ pub struct FunctionData { | |||
26 | pub name: Name, | 29 | pub name: Name, |
27 | pub params: Vec<TypeRef>, | 30 | pub params: Vec<TypeRef>, |
28 | pub ret_type: TypeRef, | 31 | pub ret_type: TypeRef, |
32 | pub attrs: Attrs, | ||
29 | /// True if the first param is `self`. This is relevant to decide whether this | 33 | /// True if the first param is `self`. This is relevant to decide whether this |
30 | /// can be called as a method. | 34 | /// can be called as a method. |
31 | pub has_self_param: bool, | 35 | pub has_self_param: bool, |
@@ -63,13 +67,15 @@ impl FunctionData { | |||
63 | params.push(type_ref); | 67 | params.push(type_ref); |
64 | } | 68 | } |
65 | } | 69 | } |
70 | let attrs = Attrs::new(&src.value, &Hygiene::new(db.upcast(), src.file_id)); | ||
71 | |||
66 | let ret_type = if let Some(type_ref) = src.value.ret_type().and_then(|rt| rt.type_ref()) { | 72 | let ret_type = if let Some(type_ref) = src.value.ret_type().and_then(|rt| rt.type_ref()) { |
67 | TypeRef::from_ast(type_ref) | 73 | TypeRef::from_ast(type_ref) |
68 | } else { | 74 | } else { |
69 | TypeRef::unit() | 75 | TypeRef::unit() |
70 | }; | 76 | }; |
71 | 77 | ||
72 | let ret_type = if src.value.is_async() { | 78 | let ret_type = if src.value.async_kw_token().is_some() { |
73 | let future_impl = desugar_future_path(ret_type); | 79 | let future_impl = desugar_future_path(ret_type); |
74 | let ty_bound = TypeBound::Path(future_impl); | 80 | let ty_bound = TypeBound::Path(future_impl); |
75 | TypeRef::ImplTrait(vec![ty_bound]) | 81 | TypeRef::ImplTrait(vec![ty_bound]) |
@@ -81,7 +87,7 @@ impl FunctionData { | |||
81 | let visibility = | 87 | let visibility = |
82 | RawVisibility::from_ast_with_default(db, vis_default, src.map(|s| s.visibility())); | 88 | RawVisibility::from_ast_with_default(db, vis_default, src.map(|s| s.visibility())); |
83 | 89 | ||
84 | let sig = FunctionData { name, params, ret_type, has_self_param, visibility }; | 90 | let sig = FunctionData { name, params, ret_type, has_self_param, visibility, attrs }; |
85 | Arc::new(sig) | 91 | Arc::new(sig) |
86 | } | 92 | } |
87 | } | 93 | } |
@@ -130,7 +136,7 @@ impl TraitData { | |||
130 | pub(crate) fn trait_data_query(db: &dyn DefDatabase, tr: TraitId) -> Arc<TraitData> { | 136 | pub(crate) fn trait_data_query(db: &dyn DefDatabase, tr: TraitId) -> Arc<TraitData> { |
131 | let src = tr.lookup(db).source(db); | 137 | let src = tr.lookup(db).source(db); |
132 | let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); | 138 | let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); |
133 | let auto = src.value.is_auto(); | 139 | let auto = src.value.auto_kw_token().is_some(); |
134 | let ast_id_map = db.ast_id_map(src.file_id); | 140 | let ast_id_map = db.ast_id_map(src.file_id); |
135 | 141 | ||
136 | let container = AssocContainerId::TraitId(tr); | 142 | let container = AssocContainerId::TraitId(tr); |
@@ -207,10 +213,11 @@ impl ImplData { | |||
207 | 213 | ||
208 | let target_trait = src.value.target_trait().map(TypeRef::from_ast); | 214 | let target_trait = src.value.target_trait().map(TypeRef::from_ast); |
209 | let target_type = TypeRef::from_ast_opt(src.value.target_type()); | 215 | let target_type = TypeRef::from_ast_opt(src.value.target_type()); |
210 | let is_negative = src.value.is_negative(); | 216 | let is_negative = src.value.excl_token().is_some(); |
211 | let module_id = impl_loc.container.module(db); | 217 | let module_id = impl_loc.container.module(db); |
212 | 218 | ||
213 | let mut items = Vec::new(); | 219 | let mut items = Vec::new(); |
220 | |||
214 | if let Some(item_list) = src.value.item_list() { | 221 | if let Some(item_list) = src.value.item_list() { |
215 | 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)); |
216 | items.extend(collect_impl_items_in_macros( | 223 | items.extend(collect_impl_items_in_macros( |
@@ -311,6 +318,10 @@ fn collect_impl_items_in_macro( | |||
311 | } | 318 | } |
312 | } | 319 | } |
313 | 320 | ||
321 | fn is_cfg_enabled(cfg_options: &CfgOptions, attrs: &Attrs) -> bool { | ||
322 | attrs.by_key("cfg").tt_values().all(|tt| cfg_options.is_cfg_enabled(tt) != Some(false)) | ||
323 | } | ||
324 | |||
314 | fn collect_impl_items( | 325 | fn collect_impl_items( |
315 | db: &dyn DefDatabase, | 326 | db: &dyn DefDatabase, |
316 | impl_items: impl Iterator<Item = ImplItem>, | 327 | impl_items: impl Iterator<Item = ImplItem>, |
@@ -318,16 +329,26 @@ fn collect_impl_items( | |||
318 | id: ImplId, | 329 | id: ImplId, |
319 | ) -> Vec<AssocItemId> { | 330 | ) -> Vec<AssocItemId> { |
320 | let items = db.ast_id_map(file_id); | 331 | let items = db.ast_id_map(file_id); |
332 | let crate_graph = db.crate_graph(); | ||
333 | let module_id = id.lookup(db).container.module(db); | ||
321 | 334 | ||
322 | impl_items | 335 | impl_items |
323 | .map(|item_node| match item_node { | 336 | .filter_map(|item_node| match item_node { |
324 | ast::ImplItem::FnDef(it) => { | 337 | ast::ImplItem::FnDef(it) => { |
325 | let def = FunctionLoc { | 338 | let def = FunctionLoc { |
326 | container: AssocContainerId::ImplId(id), | 339 | container: AssocContainerId::ImplId(id), |
327 | ast_id: AstId::new(file_id, items.ast_id(&it)), | 340 | ast_id: AstId::new(file_id, items.ast_id(&it)), |
328 | } | 341 | } |
329 | .intern(db); | 342 | .intern(db); |
330 | def.into() | 343 | |
344 | if !is_cfg_enabled( | ||
345 | &crate_graph[module_id.krate].cfg_options, | ||
346 | &db.function_data(def).attrs, | ||
347 | ) { | ||
348 | None | ||
349 | } else { | ||
350 | Some(def.into()) | ||
351 | } | ||
331 | } | 352 | } |
332 | ast::ImplItem::ConstDef(it) => { | 353 | ast::ImplItem::ConstDef(it) => { |
333 | let def = ConstLoc { | 354 | let def = ConstLoc { |
@@ -335,7 +356,7 @@ fn collect_impl_items( | |||
335 | ast_id: AstId::new(file_id, items.ast_id(&it)), | 356 | ast_id: AstId::new(file_id, items.ast_id(&it)), |
336 | } | 357 | } |
337 | .intern(db); | 358 | .intern(db); |
338 | def.into() | 359 | Some(def.into()) |
339 | } | 360 | } |
340 | ast::ImplItem::TypeAliasDef(it) => { | 361 | ast::ImplItem::TypeAliasDef(it) => { |
341 | let def = TypeAliasLoc { | 362 | let def = TypeAliasLoc { |
@@ -343,7 +364,7 @@ fn collect_impl_items( | |||
343 | ast_id: AstId::new(file_id, items.ast_id(&it)), | 364 | ast_id: AstId::new(file_id, items.ast_id(&it)), |
344 | } | 365 | } |
345 | .intern(db); | 366 | .intern(db); |
346 | def.into() | 367 | Some(def.into()) |
347 | } | 368 | } |
348 | }) | 369 | }) |
349 | .collect() | 370 | .collect() |