Auto merge of #12528 - Veykril:proc-diag, r=Veykril

fix: Check for the correct proc-macro settings in missing proc-macro diagnostics
This commit is contained in:
bors 2022-06-14 09:00:21 +00:00
commit 1ad6d324b2
6 changed files with 40 additions and 17 deletions

View File

@ -9,7 +9,7 @@
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 @@ pub fn diagnostics(self, db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>) {
node: node.clone().map(|it| it.into()),
precise_location: None,
macro_name: None,
kind: MacroKind::ProcMacro,
}
.into(),
),

View File

@ -12,21 +12,36 @@
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 @@ fn default() -> Self {
#[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 @@ pub fn publish_diagnostics(&self) -> bool {
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 {