diff options
Diffstat (limited to 'crates/hir_def')
-rw-r--r-- | crates/hir_def/src/body.rs | 6 | ||||
-rw-r--r-- | crates/hir_def/src/diagnostics.rs | 34 | ||||
-rw-r--r-- | crates/hir_def/src/import_map.rs | 6 | ||||
-rw-r--r-- | crates/hir_def/src/item_tree.rs | 18 | ||||
-rw-r--r-- | crates/hir_def/src/nameres.rs | 21 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/collector.rs | 13 |
6 files changed, 87 insertions, 11 deletions
diff --git a/crates/hir_def/src/body.rs b/crates/hir_def/src/body.rs index 9a9a605dd..d51036e4f 100644 --- a/crates/hir_def/src/body.rs +++ b/crates/hir_def/src/body.rs | |||
@@ -105,14 +105,16 @@ impl Expander { | |||
105 | 105 | ||
106 | let macro_call = InFile::new(self.current_file_id, ¯o_call); | 106 | let macro_call = InFile::new(self.current_file_id, ¯o_call); |
107 | 107 | ||
108 | if let Some(call_id) = macro_call.as_call_id(db, self.crate_def_map.krate, |path| { | 108 | let resolver = |path: ModPath| -> Option<MacroDefId> { |
109 | if let Some(local_scope) = local_scope { | 109 | if let Some(local_scope) = local_scope { |
110 | if let Some(def) = path.as_ident().and_then(|n| local_scope.get_legacy_macro(n)) { | 110 | if let Some(def) = path.as_ident().and_then(|n| local_scope.get_legacy_macro(n)) { |
111 | return Some(def); | 111 | return Some(def); |
112 | } | 112 | } |
113 | } | 113 | } |
114 | self.resolve_path_as_macro(db, &path) | 114 | self.resolve_path_as_macro(db, &path) |
115 | }) { | 115 | }; |
116 | |||
117 | if let Some(call_id) = macro_call.as_call_id(db, self.crate_def_map.krate, resolver) { | ||
116 | let file_id = call_id.as_file(); | 118 | let file_id = call_id.as_file(); |
117 | if let Some(node) = db.parse_or_expand(file_id) { | 119 | if let Some(node) = db.parse_or_expand(file_id) { |
118 | if let Some(expr) = T::cast(node) { | 120 | if let Some(expr) = T::cast(node) { |
diff --git a/crates/hir_def/src/diagnostics.rs b/crates/hir_def/src/diagnostics.rs index 001b3c5db..c9c08b01f 100644 --- a/crates/hir_def/src/diagnostics.rs +++ b/crates/hir_def/src/diagnostics.rs | |||
@@ -7,6 +7,9 @@ use syntax::{ast, AstPtr, SyntaxNodePtr}; | |||
7 | 7 | ||
8 | use hir_expand::{HirFileId, InFile}; | 8 | use hir_expand::{HirFileId, InFile}; |
9 | 9 | ||
10 | // Diagnostic: unresolved-module | ||
11 | // | ||
12 | // This diagnostic is triggered if rust-analyzer is unable to discover referred module. | ||
10 | #[derive(Debug)] | 13 | #[derive(Debug)] |
11 | pub struct UnresolvedModule { | 14 | pub struct UnresolvedModule { |
12 | pub file: HirFileId, | 15 | pub file: HirFileId, |
@@ -29,6 +32,9 @@ impl Diagnostic for UnresolvedModule { | |||
29 | } | 32 | } |
30 | } | 33 | } |
31 | 34 | ||
35 | // Diagnostic: unresolved-extern-crate | ||
36 | // | ||
37 | // This diagnostic is triggered if rust-analyzer is unable to discover referred extern crate. | ||
32 | #[derive(Debug)] | 38 | #[derive(Debug)] |
33 | pub struct UnresolvedExternCrate { | 39 | pub struct UnresolvedExternCrate { |
34 | pub file: HirFileId, | 40 | pub file: HirFileId, |
@@ -50,6 +56,9 @@ impl Diagnostic for UnresolvedExternCrate { | |||
50 | } | 56 | } |
51 | } | 57 | } |
52 | 58 | ||
59 | // Diagnostic: unresolved-import | ||
60 | // | ||
61 | // This diagnostic is triggered if rust-analyzer is unable to discover imported module. | ||
53 | #[derive(Debug)] | 62 | #[derive(Debug)] |
54 | pub struct UnresolvedImport { | 63 | pub struct UnresolvedImport { |
55 | pub file: HirFileId, | 64 | pub file: HirFileId, |
@@ -77,3 +86,28 @@ impl Diagnostic for UnresolvedImport { | |||
77 | true | 86 | true |
78 | } | 87 | } |
79 | } | 88 | } |
89 | |||
90 | // Diagnostic: unconfigured-code | ||
91 | // | ||
92 | // This diagnostic is shown for code with inactive `#[cfg]` attributes. | ||
93 | #[derive(Debug)] | ||
94 | pub struct InactiveCode { | ||
95 | pub file: HirFileId, | ||
96 | pub node: SyntaxNodePtr, | ||
97 | } | ||
98 | |||
99 | impl Diagnostic for InactiveCode { | ||
100 | fn code(&self) -> DiagnosticCode { | ||
101 | DiagnosticCode("inactive-code") | ||
102 | } | ||
103 | fn message(&self) -> String { | ||
104 | // FIXME: say *why* it is configured out | ||
105 | "code is inactive due to #[cfg] directives".to_string() | ||
106 | } | ||
107 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
108 | InFile::new(self.file, self.node.clone()) | ||
109 | } | ||
110 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
111 | self | ||
112 | } | ||
113 | } | ||
diff --git a/crates/hir_def/src/import_map.rs b/crates/hir_def/src/import_map.rs index 028cae2e7..1e24f29a8 100644 --- a/crates/hir_def/src/import_map.rs +++ b/crates/hir_def/src/import_map.rs | |||
@@ -356,7 +356,7 @@ mod tests { | |||
356 | let krate = crate_graph | 356 | let krate = crate_graph |
357 | .iter() | 357 | .iter() |
358 | .find(|krate| { | 358 | .find(|krate| { |
359 | crate_graph[*krate].declaration_name.as_ref().map(|n| n.to_string()) | 359 | crate_graph[*krate].display_name.as_ref().map(|n| n.to_string()) |
360 | == Some(crate_name.to_string()) | 360 | == Some(crate_name.to_string()) |
361 | }) | 361 | }) |
362 | .unwrap(); | 362 | .unwrap(); |
@@ -375,7 +375,7 @@ mod tests { | |||
375 | let path = map.path_of(item).unwrap(); | 375 | let path = map.path_of(item).unwrap(); |
376 | format!( | 376 | format!( |
377 | "{}::{} ({})\n", | 377 | "{}::{} ({})\n", |
378 | crate_graph[krate].declaration_name.as_ref().unwrap(), | 378 | crate_graph[krate].display_name.as_ref().unwrap(), |
379 | path, | 379 | path, |
380 | mark | 380 | mark |
381 | ) | 381 | ) |
@@ -416,7 +416,7 @@ mod tests { | |||
416 | .iter() | 416 | .iter() |
417 | .filter_map(|krate| { | 417 | .filter_map(|krate| { |
418 | let cdata = &crate_graph[krate]; | 418 | let cdata = &crate_graph[krate]; |
419 | let name = cdata.declaration_name.as_ref()?; | 419 | let name = cdata.display_name.as_ref()?; |
420 | 420 | ||
421 | let map = db.import_map(krate); | 421 | let map = db.import_map(krate); |
422 | 422 | ||
diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs index 8a1121bbd..7eb388bae 100644 --- a/crates/hir_def/src/item_tree.rs +++ b/crates/hir_def/src/item_tree.rs | |||
@@ -672,6 +672,24 @@ impl ModItem { | |||
672 | pub fn downcast<N: ItemTreeNode>(self) -> Option<FileItemTreeId<N>> { | 672 | pub fn downcast<N: ItemTreeNode>(self) -> Option<FileItemTreeId<N>> { |
673 | N::id_from_mod_item(self) | 673 | N::id_from_mod_item(self) |
674 | } | 674 | } |
675 | |||
676 | pub fn ast_id(&self, tree: &ItemTree) -> FileAstId<ast::Item> { | ||
677 | match self { | ||
678 | ModItem::Import(it) => tree[it.index].ast_id().upcast(), | ||
679 | ModItem::ExternCrate(it) => tree[it.index].ast_id().upcast(), | ||
680 | ModItem::Function(it) => tree[it.index].ast_id().upcast(), | ||
681 | ModItem::Struct(it) => tree[it.index].ast_id().upcast(), | ||
682 | ModItem::Union(it) => tree[it.index].ast_id().upcast(), | ||
683 | ModItem::Enum(it) => tree[it.index].ast_id().upcast(), | ||
684 | ModItem::Const(it) => tree[it.index].ast_id().upcast(), | ||
685 | ModItem::Static(it) => tree[it.index].ast_id().upcast(), | ||
686 | ModItem::Trait(it) => tree[it.index].ast_id().upcast(), | ||
687 | ModItem::Impl(it) => tree[it.index].ast_id().upcast(), | ||
688 | ModItem::TypeAlias(it) => tree[it.index].ast_id().upcast(), | ||
689 | ModItem::Mod(it) => tree[it.index].ast_id().upcast(), | ||
690 | ModItem::MacroCall(it) => tree[it.index].ast_id().upcast(), | ||
691 | } | ||
692 | } | ||
675 | } | 693 | } |
676 | 694 | ||
677 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] | 695 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
diff --git a/crates/hir_def/src/nameres.rs b/crates/hir_def/src/nameres.rs index 464ffef21..01a28aeeb 100644 --- a/crates/hir_def/src/nameres.rs +++ b/crates/hir_def/src/nameres.rs | |||
@@ -172,11 +172,7 @@ pub struct ModuleData { | |||
172 | impl CrateDefMap { | 172 | impl CrateDefMap { |
173 | pub(crate) fn crate_def_map_query(db: &dyn DefDatabase, krate: CrateId) -> Arc<CrateDefMap> { | 173 | pub(crate) fn crate_def_map_query(db: &dyn DefDatabase, krate: CrateId) -> Arc<CrateDefMap> { |
174 | let _p = profile::span("crate_def_map_query").detail(|| { | 174 | let _p = profile::span("crate_def_map_query").detail(|| { |
175 | db.crate_graph()[krate] | 175 | db.crate_graph()[krate].display_name.as_deref().unwrap_or_default().to_string() |
176 | .declaration_name | ||
177 | .as_ref() | ||
178 | .map(ToString::to_string) | ||
179 | .unwrap_or_default() | ||
180 | }); | 176 | }); |
181 | let def_map = { | 177 | let def_map = { |
182 | let edition = db.crate_graph()[krate].edition; | 178 | let edition = db.crate_graph()[krate].edition; |
@@ -290,7 +286,7 @@ mod diagnostics { | |||
290 | use hir_expand::diagnostics::DiagnosticSink; | 286 | use hir_expand::diagnostics::DiagnosticSink; |
291 | use hir_expand::hygiene::Hygiene; | 287 | use hir_expand::hygiene::Hygiene; |
292 | use hir_expand::InFile; | 288 | use hir_expand::InFile; |
293 | use syntax::{ast, AstPtr}; | 289 | use syntax::{ast, AstPtr, SyntaxNodePtr}; |
294 | 290 | ||
295 | use crate::path::ModPath; | 291 | use crate::path::ModPath; |
296 | use crate::{db::DefDatabase, diagnostics::*, nameres::LocalModuleId, AstId}; | 292 | use crate::{db::DefDatabase, diagnostics::*, nameres::LocalModuleId, AstId}; |
@@ -302,6 +298,8 @@ mod diagnostics { | |||
302 | UnresolvedExternCrate { ast: AstId<ast::ExternCrate> }, | 298 | UnresolvedExternCrate { ast: AstId<ast::ExternCrate> }, |
303 | 299 | ||
304 | UnresolvedImport { ast: AstId<ast::Use>, index: usize }, | 300 | UnresolvedImport { ast: AstId<ast::Use>, index: usize }, |
301 | |||
302 | UnconfiguredCode { ast: InFile<SyntaxNodePtr> }, | ||
305 | } | 303 | } |
306 | 304 | ||
307 | #[derive(Debug, PartialEq, Eq)] | 305 | #[derive(Debug, PartialEq, Eq)] |
@@ -340,6 +338,13 @@ mod diagnostics { | |||
340 | Self { in_module: container, kind: DiagnosticKind::UnresolvedImport { ast, index } } | 338 | Self { in_module: container, kind: DiagnosticKind::UnresolvedImport { ast, index } } |
341 | } | 339 | } |
342 | 340 | ||
341 | pub(super) fn unconfigured_code( | ||
342 | container: LocalModuleId, | ||
343 | ast: InFile<SyntaxNodePtr>, | ||
344 | ) -> Self { | ||
345 | Self { in_module: container, kind: DiagnosticKind::UnconfiguredCode { ast } } | ||
346 | } | ||
347 | |||
343 | pub(super) fn add_to( | 348 | pub(super) fn add_to( |
344 | &self, | 349 | &self, |
345 | db: &dyn DefDatabase, | 350 | db: &dyn DefDatabase, |
@@ -389,6 +394,10 @@ mod diagnostics { | |||
389 | sink.push(UnresolvedImport { file: ast.file_id, node: AstPtr::new(&tree) }); | 394 | sink.push(UnresolvedImport { file: ast.file_id, node: AstPtr::new(&tree) }); |
390 | } | 395 | } |
391 | } | 396 | } |
397 | |||
398 | DiagnosticKind::UnconfiguredCode { ast } => { | ||
399 | sink.push(InactiveCode { file: ast.file_id, node: ast.value.clone() }); | ||
400 | } | ||
392 | } | 401 | } |
393 | } | 402 | } |
394 | } | 403 | } |
diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index c8cd04264..bff8edb62 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs | |||
@@ -913,6 +913,7 @@ impl ModCollector<'_, '_> { | |||
913 | for &item in items { | 913 | for &item in items { |
914 | let attrs = self.item_tree.attrs(item.into()); | 914 | let attrs = self.item_tree.attrs(item.into()); |
915 | if !self.is_cfg_enabled(attrs) { | 915 | if !self.is_cfg_enabled(attrs) { |
916 | self.emit_unconfigured_diagnostic(item); | ||
916 | continue; | 917 | continue; |
917 | } | 918 | } |
918 | let module = | 919 | let module = |
@@ -1323,6 +1324,18 @@ impl ModCollector<'_, '_> { | |||
1323 | fn is_cfg_enabled(&self, attrs: &Attrs) -> bool { | 1324 | fn is_cfg_enabled(&self, attrs: &Attrs) -> bool { |
1324 | attrs.is_cfg_enabled(self.def_collector.cfg_options) | 1325 | attrs.is_cfg_enabled(self.def_collector.cfg_options) |
1325 | } | 1326 | } |
1327 | |||
1328 | fn emit_unconfigured_diagnostic(&mut self, item: ModItem) { | ||
1329 | let ast_id = item.ast_id(self.item_tree); | ||
1330 | let id_map = self.def_collector.db.ast_id_map(self.file_id); | ||
1331 | let syntax_ptr = id_map.get(ast_id).syntax_node_ptr(); | ||
1332 | |||
1333 | let ast_node = InFile::new(self.file_id, syntax_ptr); | ||
1334 | self.def_collector | ||
1335 | .def_map | ||
1336 | .diagnostics | ||
1337 | .push(DefDiagnostic::unconfigured_code(self.module_id, ast_node)); | ||
1338 | } | ||
1326 | } | 1339 | } |
1327 | 1340 | ||
1328 | fn is_macro_rules(path: &ModPath) -> bool { | 1341 | fn is_macro_rules(path: &ModPath) -> bool { |