aboutsummaryrefslogtreecommitdiff
path: root/crates/hir
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-06-13 16:46:09 +0100
committerGitHub <[email protected]>2021-06-13 16:46:09 +0100
commit7bff76d8ae1e8c3fbada1ade9ccf5111a1c0547e (patch)
tree1e85ed9c3fdf144469d9766c43c5362f5a31530b /crates/hir
parent3d8df2aef87bca7ec3f0994d799462f08d1ad449 (diff)
parent4af7a35197a1cb159458694e69e17bd83dc9edff (diff)
Merge #9249
9249: internal: remove def-level diagnostics tests r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/hir')
-rw-r--r--crates/hir/src/diagnostics.rs92
-rw-r--r--crates/hir/src/lib.rs77
2 files changed, 45 insertions, 124 deletions
diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs
index 718c86b3a..28580eeb4 100644
--- a/crates/hir/src/diagnostics.rs
+++ b/crates/hir/src/diagnostics.rs
@@ -5,11 +5,10 @@
5//! be expressed in terms of hir types themselves. 5//! be expressed in terms of hir types themselves.
6use std::any::Any; 6use std::any::Any;
7 7
8use cfg::{CfgExpr, CfgOptions, DnfExpr}; 8use cfg::{CfgExpr, CfgOptions};
9use either::Either; 9use either::Either;
10use hir_def::path::ModPath; 10use hir_def::path::ModPath;
11use hir_expand::{name::Name, HirFileId, InFile}; 11use hir_expand::{name::Name, HirFileId, InFile};
12use stdx::format_to;
13use syntax::{ast, AstPtr, SyntaxNodePtr, TextRange}; 12use syntax::{ast, AstPtr, SyntaxNodePtr, TextRange};
14 13
15pub use crate::diagnostics_sink::{ 14pub use crate::diagnostics_sink::{
@@ -37,7 +36,10 @@ diagnostics![
37 UnresolvedExternCrate, 36 UnresolvedExternCrate,
38 UnresolvedImport, 37 UnresolvedImport,
39 UnresolvedMacroCall, 38 UnresolvedMacroCall,
39 UnresolvedProcMacro,
40 MacroError,
40 MissingFields, 41 MissingFields,
42 InactiveCode,
41]; 43];
42 44
43#[derive(Debug)] 45#[derive(Debug)]
@@ -62,108 +64,28 @@ pub struct UnresolvedMacroCall {
62 pub path: ModPath, 64 pub path: ModPath,
63} 65}
64 66
65// Diagnostic: inactive-code
66//
67// This diagnostic is shown for code with inactive `#[cfg]` attributes.
68#[derive(Debug, Clone, Eq, PartialEq)] 67#[derive(Debug, Clone, Eq, PartialEq)]
69pub struct InactiveCode { 68pub struct InactiveCode {
70 pub file: HirFileId, 69 pub node: InFile<SyntaxNodePtr>,
71 pub node: SyntaxNodePtr,
72 pub cfg: CfgExpr, 70 pub cfg: CfgExpr,
73 pub opts: CfgOptions, 71 pub opts: CfgOptions,
74} 72}
75 73
76impl Diagnostic for InactiveCode {
77 fn code(&self) -> DiagnosticCode {
78 DiagnosticCode("inactive-code")
79 }
80 fn message(&self) -> String {
81 let inactive = DnfExpr::new(self.cfg.clone()).why_inactive(&self.opts);
82 let mut buf = "code is inactive due to #[cfg] directives".to_string();
83
84 if let Some(inactive) = inactive {
85 format_to!(buf, ": {}", inactive);
86 }
87
88 buf
89 }
90 fn display_source(&self) -> InFile<SyntaxNodePtr> {
91 InFile::new(self.file, self.node.clone())
92 }
93 fn as_any(&self) -> &(dyn Any + Send + 'static) {
94 self
95 }
96}
97
98// Diagnostic: unresolved-proc-macro
99//
100// This diagnostic is shown when a procedural macro can not be found. This usually means that
101// procedural macro support is simply disabled (and hence is only a weak hint instead of an error),
102// but can also indicate project setup problems.
103//
104// If you are seeing a lot of "proc macro not expanded" warnings, you can add this option to the
105// `rust-analyzer.diagnostics.disabled` list to prevent them from showing. Alternatively you can
106// enable support for procedural macros (see `rust-analyzer.procMacro.enable`).
107#[derive(Debug, Clone, Eq, PartialEq)] 74#[derive(Debug, Clone, Eq, PartialEq)]
108pub struct UnresolvedProcMacro { 75pub struct UnresolvedProcMacro {
109 pub file: HirFileId, 76 pub node: InFile<SyntaxNodePtr>,
110 pub node: SyntaxNodePtr,
111 /// If the diagnostic can be pinpointed more accurately than via `node`, this is the `TextRange` 77 /// If the diagnostic can be pinpointed more accurately than via `node`, this is the `TextRange`
112 /// to use instead. 78 /// to use instead.
113 pub precise_location: Option<TextRange>, 79 pub precise_location: Option<TextRange>,
114 pub macro_name: Option<String>, 80 pub macro_name: Option<String>,
115} 81}
116 82
117impl Diagnostic for UnresolvedProcMacro {
118 fn code(&self) -> DiagnosticCode {
119 DiagnosticCode("unresolved-proc-macro")
120 }
121
122 fn message(&self) -> String {
123 match &self.macro_name {
124 Some(name) => format!("proc macro `{}` not expanded", name),
125 None => "proc macro not expanded".to_string(),
126 }
127 }
128
129 fn display_source(&self) -> InFile<SyntaxNodePtr> {
130 InFile::new(self.file, self.node.clone())
131 }
132
133 fn as_any(&self) -> &(dyn Any + Send + 'static) {
134 self
135 }
136}
137
138// Diagnostic: macro-error
139//
140// This diagnostic is shown for macro expansion errors.
141#[derive(Debug, Clone, Eq, PartialEq)] 83#[derive(Debug, Clone, Eq, PartialEq)]
142pub struct MacroError { 84pub struct MacroError {
143 pub file: HirFileId, 85 pub node: InFile<SyntaxNodePtr>,
144 pub node: SyntaxNodePtr,
145 pub message: String, 86 pub message: String,
146} 87}
147 88
148impl Diagnostic for MacroError {
149 fn code(&self) -> DiagnosticCode {
150 DiagnosticCode("macro-error")
151 }
152 fn message(&self) -> String {
153 self.message.clone()
154 }
155 fn display_source(&self) -> InFile<SyntaxNodePtr> {
156 InFile::new(self.file, self.node.clone())
157 }
158 fn as_any(&self) -> &(dyn Any + Send + 'static) {
159 self
160 }
161 fn is_experimental(&self) -> bool {
162 // Newly added and not very well-tested, might contain false positives.
163 true
164 }
165}
166
167#[derive(Debug)] 89#[derive(Debug)]
168pub struct UnimplementedBuiltinMacro { 90pub struct UnimplementedBuiltinMacro {
169 pub file: HirFileId, 91 pub file: HirFileId,
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index 0a9414013..d891d0ec1 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -506,20 +506,22 @@ impl Module {
506 506
507 DefDiagnosticKind::UnconfiguredCode { ast, cfg, opts } => { 507 DefDiagnosticKind::UnconfiguredCode { ast, cfg, opts } => {
508 let item = ast.to_node(db.upcast()); 508 let item = ast.to_node(db.upcast());
509 sink.push(InactiveCode { 509 acc.push(
510 file: ast.file_id, 510 InactiveCode {
511 node: AstPtr::new(&item).into(), 511 node: ast.with_value(AstPtr::new(&item).into()),
512 cfg: cfg.clone(), 512 cfg: cfg.clone(),
513 opts: opts.clone(), 513 opts: opts.clone(),
514 }); 514 }
515 .into(),
516 );
515 } 517 }
516 518
517 DefDiagnosticKind::UnresolvedProcMacro { ast } => { 519 DefDiagnosticKind::UnresolvedProcMacro { ast } => {
518 let mut precise_location = None; 520 let mut precise_location = None;
519 let (file, ast, name) = match ast { 521 let (node, name) = match ast {
520 MacroCallKind::FnLike { ast_id, .. } => { 522 MacroCallKind::FnLike { ast_id, .. } => {
521 let node = ast_id.to_node(db.upcast()); 523 let node = ast_id.to_node(db.upcast());
522 (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node)), None) 524 (ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node))), None)
523 } 525 }
524 MacroCallKind::Derive { ast_id, derive_name, .. } => { 526 MacroCallKind::Derive { ast_id, derive_name, .. } => {
525 let node = ast_id.to_node(db.upcast()); 527 let node = ast_id.to_node(db.upcast());
@@ -552,8 +554,7 @@ impl Module {
552 } 554 }
553 555
554 ( 556 (
555 ast_id.file_id, 557 ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node))),
556 SyntaxNodePtr::from(AstPtr::new(&node)),
557 Some(derive_name.clone()), 558 Some(derive_name.clone()),
558 ) 559 )
559 } 560 }
@@ -564,18 +565,14 @@ impl Module {
564 || panic!("cannot find attribute #{}", invoc_attr_index), 565 || panic!("cannot find attribute #{}", invoc_attr_index),
565 ); 566 );
566 ( 567 (
567 ast_id.file_id, 568 ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&attr))),
568 SyntaxNodePtr::from(AstPtr::new(&attr)),
569 Some(attr_name.clone()), 569 Some(attr_name.clone()),
570 ) 570 )
571 } 571 }
572 }; 572 };
573 sink.push(UnresolvedProcMacro { 573 acc.push(
574 file, 574 UnresolvedProcMacro { node, precise_location, macro_name: name }.into(),
575 node: ast, 575 );
576 precise_location,
577 macro_name: name,
578 });
579 } 576 }
580 577
581 DefDiagnosticKind::UnresolvedMacroCall { ast, path } => { 578 DefDiagnosticKind::UnresolvedMacroCall { ast, path } => {
@@ -590,19 +587,19 @@ impl Module {
590 } 587 }
591 588
592 DefDiagnosticKind::MacroError { ast, message } => { 589 DefDiagnosticKind::MacroError { ast, message } => {
593 let (file, ast) = match ast { 590 let node = match ast {
594 MacroCallKind::FnLike { ast_id, .. } => { 591 MacroCallKind::FnLike { ast_id, .. } => {
595 let node = ast_id.to_node(db.upcast()); 592 let node = ast_id.to_node(db.upcast());
596 (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node))) 593 ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node)))
597 } 594 }
598 MacroCallKind::Derive { ast_id, .. } 595 MacroCallKind::Derive { ast_id, .. }
599 | MacroCallKind::Attr { ast_id, .. } => { 596 | MacroCallKind::Attr { ast_id, .. } => {
600 // FIXME: point to the attribute instead, this creates very large diagnostics 597 // FIXME: point to the attribute instead, this creates very large diagnostics
601 let node = ast_id.to_node(db.upcast()); 598 let node = ast_id.to_node(db.upcast());
602 (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node))) 599 ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node)))
603 } 600 }
604 }; 601 };
605 sink.push(MacroError { file, node: ast, message: message.clone() }); 602 acc.push(MacroError { node, message: message.clone() }.into());
606 } 603 }
607 604
608 DefDiagnosticKind::UnimplementedBuiltinMacro { ast } => { 605 DefDiagnosticKind::UnimplementedBuiltinMacro { ast } => {
@@ -1045,23 +1042,25 @@ impl Function {
1045 let source_map = db.body_with_source_map(self.id.into()).1; 1042 let source_map = db.body_with_source_map(self.id.into()).1;
1046 for diag in source_map.diagnostics() { 1043 for diag in source_map.diagnostics() {
1047 match diag { 1044 match diag {
1048 BodyDiagnostic::InactiveCode { node, cfg, opts } => sink.push(InactiveCode { 1045 BodyDiagnostic::InactiveCode { node, cfg, opts } => acc.push(
1049 file: node.file_id, 1046 InactiveCode { node: node.clone(), cfg: cfg.clone(), opts: opts.clone() }
1050 node: node.value.clone(), 1047 .into(),
1051 cfg: cfg.clone(), 1048 ),
1052 opts: opts.clone(), 1049 BodyDiagnostic::MacroError { node, message } => acc.push(
1053 }), 1050 MacroError {
1054 BodyDiagnostic::MacroError { node, message } => sink.push(MacroError { 1051 node: node.clone().map(|it| it.into()),
1055 file: node.file_id, 1052 message: message.to_string(),
1056 node: node.value.clone().into(), 1053 }
1057 message: message.to_string(), 1054 .into(),
1058 }), 1055 ),
1059 BodyDiagnostic::UnresolvedProcMacro { node } => sink.push(UnresolvedProcMacro { 1056 BodyDiagnostic::UnresolvedProcMacro { node } => acc.push(
1060 file: node.file_id, 1057 UnresolvedProcMacro {
1061 node: node.value.clone().into(), 1058 node: node.clone().map(|it| it.into()),
1062 precise_location: None, 1059 precise_location: None,
1063 macro_name: None, 1060 macro_name: None,
1064 }), 1061 }
1062 .into(),
1063 ),
1065 BodyDiagnostic::UnresolvedMacroCall { node, path } => acc.push( 1064 BodyDiagnostic::UnresolvedMacroCall { node, path } => acc.push(
1066 UnresolvedMacroCall { macro_call: node.clone(), path: path.clone() }.into(), 1065 UnresolvedMacroCall { macro_call: node.clone(), path: path.clone() }.into(),
1067 ), 1066 ),