Skip to content

Commit 1e4aaee

Browse files
committed
internal: refactor unresolved proc macro diagnostic
1 parent f85e383 commit 1e4aaee

File tree

5 files changed

+59
-61
lines changed

5 files changed

+59
-61
lines changed

crates/hir/src/diagnostics.rs

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ diagnostics![
3636
UnresolvedExternCrate,
3737
UnresolvedImport,
3838
UnresolvedMacroCall,
39+
UnresolvedProcMacro,
3940
MissingFields,
4041
InactiveCode,
4142
];
@@ -69,46 +70,15 @@ pub struct InactiveCode {
6970
pub opts: CfgOptions,
7071
}
7172

72-
// Diagnostic: unresolved-proc-macro
73-
//
74-
// This diagnostic is shown when a procedural macro can not be found. This usually means that
75-
// procedural macro support is simply disabled (and hence is only a weak hint instead of an error),
76-
// but can also indicate project setup problems.
77-
//
78-
// If you are seeing a lot of "proc macro not expanded" warnings, you can add this option to the
79-
// `rust-analyzer.diagnostics.disabled` list to prevent them from showing. Alternatively you can
80-
// enable support for procedural macros (see `rust-analyzer.procMacro.enable`).
8173
#[derive(Debug, Clone, Eq, PartialEq)]
8274
pub struct UnresolvedProcMacro {
83-
pub file: HirFileId,
84-
pub node: SyntaxNodePtr,
75+
pub node: InFile<SyntaxNodePtr>,
8576
/// If the diagnostic can be pinpointed more accurately than via `node`, this is the `TextRange`
8677
/// to use instead.
8778
pub precise_location: Option<TextRange>,
8879
pub macro_name: Option<String>,
8980
}
9081

91-
impl Diagnostic for UnresolvedProcMacro {
92-
fn code(&self) -> DiagnosticCode {
93-
DiagnosticCode("unresolved-proc-macro")
94-
}
95-
96-
fn message(&self) -> String {
97-
match &self.macro_name {
98-
Some(name) => format!("proc macro `{}` not expanded", name),
99-
None => "proc macro not expanded".to_string(),
100-
}
101-
}
102-
103-
fn display_source(&self) -> InFile<SyntaxNodePtr> {
104-
InFile::new(self.file, self.node.clone())
105-
}
106-
107-
fn as_any(&self) -> &(dyn Any + Send + 'static) {
108-
self
109-
}
110-
}
111-
11282
// Diagnostic: macro-error
11383
//
11484
// This diagnostic is shown for macro expansion errors.

crates/hir/src/lib.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -518,10 +518,10 @@ impl Module {
518518

519519
DefDiagnosticKind::UnresolvedProcMacro { ast } => {
520520
let mut precise_location = None;
521-
let (file, ast, name) = match ast {
521+
let (node, name) = match ast {
522522
MacroCallKind::FnLike { ast_id, .. } => {
523523
let node = ast_id.to_node(db.upcast());
524-
(ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node)), None)
524+
(ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node))), None)
525525
}
526526
MacroCallKind::Derive { ast_id, derive_name, .. } => {
527527
let node = ast_id.to_node(db.upcast());
@@ -554,8 +554,7 @@ impl Module {
554554
}
555555

556556
(
557-
ast_id.file_id,
558-
SyntaxNodePtr::from(AstPtr::new(&node)),
557+
ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node))),
559558
Some(derive_name.clone()),
560559
)
561560
}
@@ -566,18 +565,14 @@ impl Module {
566565
|| panic!("cannot find attribute #{}", invoc_attr_index),
567566
);
568567
(
569-
ast_id.file_id,
570-
SyntaxNodePtr::from(AstPtr::new(&attr)),
568+
ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&attr))),
571569
Some(attr_name.clone()),
572570
)
573571
}
574572
};
575-
sink.push(UnresolvedProcMacro {
576-
file,
577-
node: ast,
578-
precise_location,
579-
macro_name: name,
580-
});
573+
acc.push(
574+
UnresolvedProcMacro { node, precise_location, macro_name: name }.into(),
575+
);
581576
}
582577

583578
DefDiagnosticKind::UnresolvedMacroCall { ast, path } => {
@@ -1056,12 +1051,14 @@ impl Function {
10561051
node: node.value.clone().into(),
10571052
message: message.to_string(),
10581053
}),
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-
}),
1054+
BodyDiagnostic::UnresolvedProcMacro { node } => acc.push(
1055+
UnresolvedProcMacro {
1056+
node: node.clone().map(|it| it.into()),
1057+
precise_location: None,
1058+
macro_name: None,
1059+
}
1060+
.into(),
1061+
),
10651062
BodyDiagnostic::UnresolvedMacroCall { node, path } => acc.push(
10661063
UnresolvedMacroCall { macro_call: node.clone(), path: path.clone() }.into(),
10671064
),

crates/ide/src/diagnostics.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod unresolved_module;
88
mod unresolved_extern_crate;
99
mod unresolved_import;
1010
mod unresolved_macro_call;
11+
mod unresolved_proc_macro;
1112
mod inactive_code;
1213
mod missing_fields;
1314

@@ -68,6 +69,11 @@ impl Diagnostic {
6869
self
6970
}
7071

72+
fn severity(mut self, severity: Severity) -> Diagnostic {
73+
self.severity = severity;
74+
self
75+
}
76+
7177
fn error(range: TextRange, message: String) -> Self {
7278
Self {
7379
message,
@@ -178,16 +184,6 @@ pub(crate) fn diagnostics(
178184
.with_code(Some(d.code())),
179185
);
180186
})
181-
.on::<hir::diagnostics::UnresolvedProcMacro, _>(|d| {
182-
// Use more accurate position if available.
183-
let display_range = d
184-
.precise_location
185-
.unwrap_or_else(|| sema.diagnostics_display_range(d.display_source()).range);
186-
187-
// FIXME: it would be nice to tell the user whether proc macros are currently disabled
188-
res.borrow_mut()
189-
.push(Diagnostic::hint(display_range, d.message()).with_code(Some(d.code())));
190-
})
191187
.on::<hir::diagnostics::UnimplementedBuiltinMacro, _>(|d| {
192188
let display_range = sema.diagnostics_display_range(d.display_source()).range;
193189
res.borrow_mut()
@@ -231,6 +227,7 @@ pub(crate) fn diagnostics(
231227
AnyDiagnostic::UnresolvedExternCrate(d) => unresolved_extern_crate::unresolved_extern_crate(&ctx, &d),
232228
AnyDiagnostic::UnresolvedImport(d) => unresolved_import::unresolved_import(&ctx, &d),
233229
AnyDiagnostic::UnresolvedMacroCall(d) => unresolved_macro_call::unresolved_macro_call(&ctx, &d),
230+
AnyDiagnostic::UnresolvedProcMacro(d) => unresolved_proc_macro::unresolved_proc_macro(&ctx, &d),
234231
AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d),
235232

236233
AnyDiagnostic::InactiveCode(d) => match inactive_code::inactive_code(&ctx, &d) {

crates/ide/src/diagnostics/inactive_code.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use cfg::DnfExpr;
22
use stdx::format_to;
33

4-
use crate::diagnostics::{Diagnostic, DiagnosticsContext};
4+
use crate::{
5+
diagnostics::{Diagnostic, DiagnosticsContext},
6+
Severity,
7+
};
58

69
// Diagnostic: inactive-code
710
//
@@ -27,6 +30,7 @@ pub(super) fn inactive_code(
2730
message,
2831
ctx.sema.diagnostics_display_range(d.node.clone()).range,
2932
)
33+
.severity(Severity::WeakWarning)
3034
.with_unused(true);
3135
Some(res)
3236
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use crate::{
2+
diagnostics::{Diagnostic, DiagnosticsContext},
3+
Severity,
4+
};
5+
6+
// Diagnostic: unresolved-proc-macro
7+
//
8+
// This diagnostic is shown when a procedural macro can not be found. This usually means that
9+
// procedural macro support is simply disabled (and hence is only a weak hint instead of an error),
10+
// but can also indicate project setup problems.
11+
//
12+
// If you are seeing a lot of "proc macro not expanded" warnings, you can add this option to the
13+
// `rust-analyzer.diagnostics.disabled` list to prevent them from showing. Alternatively you can
14+
// enable support for procedural macros (see `rust-analyzer.procMacro.enable`).
15+
pub(super) fn unresolved_proc_macro(
16+
ctx: &DiagnosticsContext<'_>,
17+
d: &hir::UnresolvedProcMacro,
18+
) -> Diagnostic {
19+
// Use more accurate position if available.
20+
let display_range = d
21+
.precise_location
22+
.unwrap_or_else(|| ctx.sema.diagnostics_display_range(d.node.clone()).range);
23+
// FIXME: it would be nice to tell the user whether proc macros are currently disabled
24+
let message = match &d.macro_name {
25+
Some(name) => format!("proc macro `{}` not expanded", name),
26+
None => "proc macro not expanded".to_string(),
27+
};
28+
29+
Diagnostic::new("unresolved-proc-macro", message, display_range).severity(Severity::WeakWarning)
30+
}

0 commit comments

Comments
 (0)