2021-06-14 05:15:05 -05:00
|
|
|
use crate::{Diagnostic, DiagnosticsContext, Severity};
|
2021-06-13 09:51:44 -05:00
|
|
|
|
|
|
|
// Diagnostic: unresolved-proc-macro
|
|
|
|
//
|
|
|
|
// This diagnostic is shown when a procedural macro can not be found. This usually means that
|
|
|
|
// procedural macro support is simply disabled (and hence is only a weak hint instead of an error),
|
|
|
|
// but can also indicate project setup problems.
|
|
|
|
//
|
|
|
|
// If you are seeing a lot of "proc macro not expanded" warnings, you can add this option to the
|
|
|
|
// `rust-analyzer.diagnostics.disabled` list to prevent them from showing. Alternatively you can
|
2022-06-12 11:44:46 -05:00
|
|
|
// enable support for procedural macros (see `rust-analyzer.procMacro.attributes.enable`).
|
2021-06-14 11:32:39 -05:00
|
|
|
pub(crate) fn unresolved_proc_macro(
|
2021-06-13 09:51:44 -05:00
|
|
|
ctx: &DiagnosticsContext<'_>,
|
|
|
|
d: &hir::UnresolvedProcMacro,
|
2022-06-14 03:40:57 -05:00
|
|
|
proc_macros_enabled: bool,
|
|
|
|
proc_attr_macros_enabled: bool,
|
2021-06-13 09:51:44 -05:00
|
|
|
) -> Diagnostic {
|
|
|
|
// Use more accurate position if available.
|
|
|
|
let display_range = d
|
|
|
|
.precise_location
|
|
|
|
.unwrap_or_else(|| ctx.sema.diagnostics_display_range(d.node.clone()).range);
|
2022-06-14 03:40:57 -05:00
|
|
|
|
|
|
|
let config_enabled = match d.kind {
|
|
|
|
hir::MacroKind::Attr => proc_macros_enabled && proc_attr_macros_enabled,
|
|
|
|
_ => proc_macros_enabled,
|
|
|
|
};
|
|
|
|
|
2021-06-13 09:51:44 -05:00
|
|
|
let message = match &d.macro_name {
|
|
|
|
Some(name) => format!("proc macro `{}` not expanded", name),
|
|
|
|
None => "proc macro not expanded".to_string(),
|
|
|
|
};
|
2022-06-14 03:40:57 -05:00
|
|
|
let (message, severity) = if config_enabled {
|
|
|
|
(message, Severity::Error)
|
|
|
|
} else {
|
|
|
|
let message = match d.kind {
|
|
|
|
hir::MacroKind::Attr if proc_macros_enabled => {
|
|
|
|
format!("{message}{}", " (attribute macro expansion is disabled)")
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
format!("{message}{}", " (proc-macro expansion is disabled)")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
(message, Severity::WeakWarning)
|
|
|
|
};
|
2021-06-13 09:51:44 -05:00
|
|
|
|
2022-06-14 03:40:57 -05:00
|
|
|
Diagnostic::new("unresolved-proc-macro", message, display_range).severity(severity)
|
2021-06-13 09:51:44 -05:00
|
|
|
}
|