]> git.proxmox.com Git - rustc.git/blob - src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/unresolved_proc_macro.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / src / tools / rust-analyzer / crates / ide-diagnostics / src / handlers / unresolved_proc_macro.rs
1 use hir::db::DefDatabase;
2
3 use crate::{Diagnostic, DiagnosticsContext, Severity};
4
5 // Diagnostic: unresolved-proc-macro
6 //
7 // This diagnostic is shown when a procedural macro can not be found. This usually means that
8 // procedural macro support is simply disabled (and hence is only a weak hint instead of an error),
9 // but can also indicate project setup problems.
10 //
11 // If you are seeing a lot of "proc macro not expanded" warnings, you can add this option to the
12 // `rust-analyzer.diagnostics.disabled` list to prevent them from showing. Alternatively you can
13 // enable support for procedural macros (see `rust-analyzer.procMacro.attributes.enable`).
14 pub(crate) fn unresolved_proc_macro(
15 ctx: &DiagnosticsContext<'_>,
16 d: &hir::UnresolvedProcMacro,
17 proc_macros_enabled: bool,
18 proc_attr_macros_enabled: bool,
19 ) -> Diagnostic {
20 // Use more accurate position if available.
21 let display_range = ctx.resolve_precise_location(&d.node, d.precise_location);
22
23 let config_enabled = match d.kind {
24 hir::MacroKind::Attr => proc_macros_enabled && proc_attr_macros_enabled,
25 _ => proc_macros_enabled,
26 };
27
28 let message = match &d.macro_name {
29 Some(name) => format!("proc macro `{name}` not expanded"),
30 None => "proc macro not expanded".to_string(),
31 };
32 let severity = if config_enabled { Severity::Error } else { Severity::WeakWarning };
33 let def_map = ctx.sema.db.crate_def_map(d.krate);
34 let message = format!(
35 "{message}: {}",
36 if config_enabled {
37 match def_map.proc_macro_loading_error() {
38 Some(e) => e,
39 None => "proc macro not found in the built dylib",
40 }
41 } else {
42 match d.kind {
43 hir::MacroKind::Attr if proc_macros_enabled => {
44 "attribute macro expansion is disabled"
45 }
46 _ => "proc-macro expansion is disabled",
47 }
48 },
49 );
50
51 Diagnostic::new("unresolved-proc-macro", message, display_range).severity(severity)
52 }