fix: Check for the correct proc-macro settings in missing proc-macro diagnostics

This commit is contained in:
Lukas Wirth 2022-06-14 10:40:57 +02:00
parent 7db73875ac
commit 325ceaef19
6 changed files with 40 additions and 17 deletions

View file

@ -9,7 +9,7 @@ use hir_def::path::ModPath;
use hir_expand::{name::Name, HirFileId, InFile};
use syntax::{ast, AstPtr, SyntaxNodePtr, TextRange};
use crate::Type;
use crate::{MacroKind, Type};
macro_rules! diagnostics {
($($diag:ident,)*) => {
@ -86,6 +86,7 @@ pub struct UnresolvedProcMacro {
/// to use instead.
pub precise_location: Option<TextRange>,
pub macro_name: Option<String>,
pub kind: MacroKind,
}
#[derive(Debug, Clone, Eq, PartialEq)]

View file

@ -628,13 +628,14 @@ fn emit_def_diagnostic(db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>, diag:
}
DefDiagnosticKind::UnresolvedProcMacro { ast } => {
let (node, precise_location, macro_name) = match ast {
let (node, precise_location, macro_name, kind) = match ast {
MacroCallKind::FnLike { ast_id, .. } => {
let node = ast_id.to_node(db.upcast());
(
ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node))),
node.path().map(|it| it.syntax().text_range()),
node.path().and_then(|it| it.segment()).map(|it| it.to_string()),
MacroKind::ProcMacro,
)
}
MacroCallKind::Derive { ast_id, derive_attr_index, derive_index } => {
@ -665,6 +666,7 @@ fn emit_def_diagnostic(db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>, diag:
ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node))),
token.as_ref().map(|tok| tok.text_range()),
token.as_ref().map(ToString::to_string),
MacroKind::Derive,
)
}
MacroCallKind::Attr { ast_id, invoc_attr_index, .. } => {
@ -683,10 +685,11 @@ fn emit_def_diagnostic(db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>, diag:
.and_then(|seg| seg.name_ref())
.as_ref()
.map(ToString::to_string),
MacroKind::Attr,
)
}
};
acc.push(UnresolvedProcMacro { node, precise_location, macro_name }.into());
acc.push(UnresolvedProcMacro { node, precise_location, macro_name, kind }.into());
}
DefDiagnosticKind::UnresolvedMacroCall { ast, path } => {
@ -1159,6 +1162,7 @@ impl DefWithBody {
node: node.clone().map(|it| it.into()),
precise_location: None,
macro_name: None,
kind: MacroKind::ProcMacro,
}
.into(),
),

View file

@ -12,21 +12,36 @@ use crate::{Diagnostic, DiagnosticsContext, Severity};
pub(crate) fn unresolved_proc_macro(
ctx: &DiagnosticsContext<'_>,
d: &hir::UnresolvedProcMacro,
attr_proc_macros_enabled: bool,
proc_macros_enabled: bool,
proc_attr_macros_enabled: bool,
) -> 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);
let config_enabled = match d.kind {
hir::MacroKind::Attr => proc_macros_enabled && proc_attr_macros_enabled,
_ => proc_macros_enabled,
};
let message = match &d.macro_name {
Some(name) => format!("proc macro `{}` not expanded", name),
None => "proc macro not expanded".to_string(),
};
let message = format!(
"{message}{}",
if attr_proc_macros_enabled { "" } else { " (attribute macro expansion is disabled)" }
);
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)
};
Diagnostic::new("unresolved-proc-macro", message, display_range)
.severity(if attr_proc_macros_enabled { Severity::Error } else { Severity::WeakWarning })
Diagnostic::new("unresolved-proc-macro", message, display_range).severity(severity)
}

View file

@ -139,7 +139,8 @@ impl Default for ExprFillDefaultMode {
#[derive(Default, Debug, Clone)]
pub struct DiagnosticsConfig {
pub attr_proc_macros_enabled: bool,
pub proc_macros_enabled: bool,
pub proc_attr_macros_enabled: bool,
pub disable_experimental: bool,
pub disabled: FxHashSet<String>,
pub expr_fill_default: ExprFillDefaultMode,
@ -205,7 +206,7 @@ pub fn diagnostics(
AnyDiagnostic::UnresolvedImport(d) => handlers::unresolved_import::unresolved_import(&ctx, &d),
AnyDiagnostic::UnresolvedMacroCall(d) => handlers::unresolved_macro_call::unresolved_macro_call(&ctx, &d),
AnyDiagnostic::UnresolvedModule(d) => handlers::unresolved_module::unresolved_module(&ctx, &d),
AnyDiagnostic::UnresolvedProcMacro(d) => handlers::unresolved_proc_macro::unresolved_proc_macro(&ctx, &d, config.attr_proc_macros_enabled),
AnyDiagnostic::UnresolvedProcMacro(d) => handlers::unresolved_proc_macro::unresolved_proc_macro(&ctx, &d, config.proc_macros_enabled, config.proc_attr_macros_enabled),
AnyDiagnostic::InvalidDeriveTarget(d) => handlers::invalid_derive_target::invalid_derive_target(&ctx, &d),
AnyDiagnostic::InactiveCode(d) => match handlers::inactive_code::inactive_code(&ctx, &d) {

View file

@ -36,10 +36,11 @@ pub struct HighlightRelatedConfig {
// Feature: Highlight Related
//
// Highlights constructs related to the thing under the cursor:
// - if on an identifier, highlights all references to that identifier in the current file
// - if on an `async` or `await token, highlights all yield points for that async context
// - if on a `return` or `fn` keyword, `?` character or `->` return type arrow, highlights all exit points for that context
// - if on a `break`, `loop`, `while` or `for` token, highlights all break points for that loop or block context
//
// . if on an identifier, highlights all references to that identifier in the current file
// . if on an `async` or `await token, highlights all yield points for that async context
// . if on a `return` or `fn` keyword, `?` character or `->` return type arrow, highlights all exit points for that context
// . if on a `break`, `loop`, `while` or `for` token, highlights all break points for that loop or block context
//
// Note: `?` and `->` do not currently trigger this behavior in the VSCode editor.
pub(crate) fn highlight_related(

View file

@ -856,7 +856,8 @@ impl Config {
pub fn diagnostics(&self) -> DiagnosticsConfig {
DiagnosticsConfig {
attr_proc_macros_enabled: self.expand_proc_attr_macros(),
proc_attr_macros_enabled: self.expand_proc_attr_macros(),
proc_macros_enabled: self.data.procMacro_enable,
disable_experimental: !self.data.diagnostics_experimental_enable,
disabled: self.data.diagnostics_disabled.clone(),
expr_fill_default: match self.data.assist_expressionFillDefault {