1 use hir
::db
::DefDatabase
;
3 use crate::{Diagnostic, DiagnosticsContext, Severity}
;
5 // Diagnostic: unresolved-proc-macro
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.
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
,
20 // Use more accurate position if available.
21 let display_range
= ctx
.resolve_precise_location(&d
.node
, d
.precise_location
);
23 let config_enabled
= match d
.kind
{
24 hir
::MacroKind
::Attr
=> proc_macros_enabled
&& proc_attr_macros_enabled
,
25 _
=> proc_macros_enabled
,
28 let message
= match &d
.macro_name
{
29 Some(name
) => format
!("proc macro `{name}` not expanded"),
30 None
=> "proc macro not expanded".to_string(),
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
!(
37 match def_map
.proc_macro_loading_error() {
39 None
=> "proc macro not found in the built dylib",
43 hir
::MacroKind
::Attr
if proc_macros_enabled
=> {
44 "attribute macro expansion is disabled"
46 _
=> "proc-macro expansion is disabled",
51 Diagnostic
::new("unresolved-proc-macro", message
, display_range
).severity(severity
)