Skip to content

Commit 7bff76d

Browse files
bors[bot]matklad
andauthored
Merge #9249
9249: internal: remove def-level diagnostics tests r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents 3d8df2a + 4af7a35 commit 7bff76d

File tree

11 files changed

+398
-562
lines changed

11 files changed

+398
-562
lines changed

crates/hir/src/diagnostics.rs

Lines changed: 7 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
//! be expressed in terms of hir types themselves.
66
use std::any::Any;
77

8-
use cfg::{CfgExpr, CfgOptions, DnfExpr};
8+
use cfg::{CfgExpr, CfgOptions};
99
use either::Either;
1010
use hir_def::path::ModPath;
1111
use hir_expand::{name::Name, HirFileId, InFile};
12-
use stdx::format_to;
1312
use syntax::{ast, AstPtr, SyntaxNodePtr, TextRange};
1413

1514
pub use crate::diagnostics_sink::{
@@ -37,7 +36,10 @@ diagnostics![
3736
UnresolvedExternCrate,
3837
UnresolvedImport,
3938
UnresolvedMacroCall,
39+
UnresolvedProcMacro,
40+
MacroError,
4041
MissingFields,
42+
InactiveCode,
4143
];
4244

4345
#[derive(Debug)]
@@ -62,108 +64,28 @@ pub struct UnresolvedMacroCall {
6264
pub path: ModPath,
6365
}
6466

65-
// Diagnostic: inactive-code
66-
//
67-
// This diagnostic is shown for code with inactive `#[cfg]` attributes.
6867
#[derive(Debug, Clone, Eq, PartialEq)]
6968
pub struct InactiveCode {
70-
pub file: HirFileId,
71-
pub node: SyntaxNodePtr,
69+
pub node: InFile<SyntaxNodePtr>,
7270
pub cfg: CfgExpr,
7371
pub opts: CfgOptions,
7472
}
7573

76-
impl 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`).
10774
#[derive(Debug, Clone, Eq, PartialEq)]
10875
pub struct UnresolvedProcMacro {
109-
pub file: HirFileId,
110-
pub node: SyntaxNodePtr,
76+
pub node: InFile<SyntaxNodePtr>,
11177
/// If the diagnostic can be pinpointed more accurately than via `node`, this is the `TextRange`
11278
/// to use instead.
11379
pub precise_location: Option<TextRange>,
11480
pub macro_name: Option<String>,
11581
}
11682

117-
impl 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.
14183
#[derive(Debug, Clone, Eq, PartialEq)]
14284
pub struct MacroError {
143-
pub file: HirFileId,
144-
pub node: SyntaxNodePtr,
85+
pub node: InFile<SyntaxNodePtr>,
14586
pub message: String,
14687
}
14788

148-
impl 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-
16789
#[derive(Debug)]
16890
pub struct UnimplementedBuiltinMacro {
16991
pub file: HirFileId,

crates/hir/src/lib.rs

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -506,20 +506,22 @@ impl Module {
506506

507507
DefDiagnosticKind::UnconfiguredCode { ast, cfg, opts } => {
508508
let item = ast.to_node(db.upcast());
509-
sink.push(InactiveCode {
510-
file: ast.file_id,
511-
node: AstPtr::new(&item).into(),
512-
cfg: cfg.clone(),
513-
opts: opts.clone(),
514-
});
509+
acc.push(
510+
InactiveCode {
511+
node: ast.with_value(AstPtr::new(&item).into()),
512+
cfg: cfg.clone(),
513+
opts: opts.clone(),
514+
}
515+
.into(),
516+
);
515517
}
516518

517519
DefDiagnosticKind::UnresolvedProcMacro { ast } => {
518520
let mut precise_location = None;
519-
let (file, ast, name) = match ast {
521+
let (node, name) = match ast {
520522
MacroCallKind::FnLike { ast_id, .. } => {
521523
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)
523525
}
524526
MacroCallKind::Derive { ast_id, derive_name, .. } => {
525527
let node = ast_id.to_node(db.upcast());
@@ -552,8 +554,7 @@ impl Module {
552554
}
553555

554556
(
555-
ast_id.file_id,
556-
SyntaxNodePtr::from(AstPtr::new(&node)),
557+
ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node))),
557558
Some(derive_name.clone()),
558559
)
559560
}
@@ -564,18 +565,14 @@ impl Module {
564565
|| panic!("cannot find attribute #{}", invoc_attr_index),
565566
);
566567
(
567-
ast_id.file_id,
568-
SyntaxNodePtr::from(AstPtr::new(&attr)),
568+
ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&attr))),
569569
Some(attr_name.clone()),
570570
)
571571
}
572572
};
573-
sink.push(UnresolvedProcMacro {
574-
file,
575-
node: ast,
576-
precise_location,
577-
macro_name: name,
578-
});
573+
acc.push(
574+
UnresolvedProcMacro { node, precise_location, macro_name: name }.into(),
575+
);
579576
}
580577

581578
DefDiagnosticKind::UnresolvedMacroCall { ast, path } => {
@@ -590,19 +587,19 @@ impl Module {
590587
}
591588

592589
DefDiagnosticKind::MacroError { ast, message } => {
593-
let (file, ast) = match ast {
590+
let node = match ast {
594591
MacroCallKind::FnLike { ast_id, .. } => {
595592
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)))
597594
}
598595
MacroCallKind::Derive { ast_id, .. }
599596
| MacroCallKind::Attr { ast_id, .. } => {
600597
// FIXME: point to the attribute instead, this creates very large diagnostics
601598
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)))
603600
}
604601
};
605-
sink.push(MacroError { file, node: ast, message: message.clone() });
602+
acc.push(MacroError { node, message: message.clone() }.into());
606603
}
607604

608605
DefDiagnosticKind::UnimplementedBuiltinMacro { ast } => {
@@ -1045,23 +1042,25 @@ impl Function {
10451042
let source_map = db.body_with_source_map(self.id.into()).1;
10461043
for diag in source_map.diagnostics() {
10471044
match diag {
1048-
BodyDiagnostic::InactiveCode { node, cfg, opts } => sink.push(InactiveCode {
1049-
file: node.file_id,
1050-
node: node.value.clone(),
1051-
cfg: cfg.clone(),
1052-
opts: opts.clone(),
1053-
}),
1054-
BodyDiagnostic::MacroError { node, message } => sink.push(MacroError {
1055-
file: node.file_id,
1056-
node: node.value.clone().into(),
1057-
message: message.to_string(),
1058-
}),
1059-
BodyDiagnostic::UnresolvedProcMacro { node } => sink.push(UnresolvedProcMacro {
1060-
file: node.file_id,
1061-
node: node.value.clone().into(),
1062-
precise_location: None,
1063-
macro_name: None,
1064-
}),
1045+
BodyDiagnostic::InactiveCode { node, cfg, opts } => acc.push(
1046+
InactiveCode { node: node.clone(), cfg: cfg.clone(), opts: opts.clone() }
1047+
.into(),
1048+
),
1049+
BodyDiagnostic::MacroError { node, message } => acc.push(
1050+
MacroError {
1051+
node: node.clone().map(|it| it.into()),
1052+
message: message.to_string(),
1053+
}
1054+
.into(),
1055+
),
1056+
BodyDiagnostic::UnresolvedProcMacro { node } => acc.push(
1057+
UnresolvedProcMacro {
1058+
node: node.clone().map(|it| it.into()),
1059+
precise_location: None,
1060+
macro_name: None,
1061+
}
1062+
.into(),
1063+
),
10651064
BodyDiagnostic::UnresolvedMacroCall { node, path } => acc.push(
10661065
UnresolvedMacroCall { macro_call: node.clone(), path: path.clone() }.into(),
10671066
),

0 commit comments

Comments
 (0)