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 hir_expand::{name::Name, HirFileId, InFile};
use syntax::{ast, AstPtr, SyntaxNodePtr, TextRange}; use syntax::{ast, AstPtr, SyntaxNodePtr, TextRange};
use crate::Type; use crate::{MacroKind, Type};
macro_rules! diagnostics { macro_rules! diagnostics {
($($diag:ident,)*) => { ($($diag:ident,)*) => {
@ -86,6 +86,7 @@ pub struct UnresolvedProcMacro {
/// to use instead. /// to use instead.
pub precise_location: Option<TextRange>, pub precise_location: Option<TextRange>,
pub macro_name: Option<String>, pub macro_name: Option<String>,
pub kind: MacroKind,
} }
#[derive(Debug, Clone, Eq, PartialEq)] #[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 } => { DefDiagnosticKind::UnresolvedProcMacro { ast } => {
let (node, precise_location, macro_name) = match ast { let (node, precise_location, macro_name, kind) = match ast {
MacroCallKind::FnLike { ast_id, .. } => { MacroCallKind::FnLike { ast_id, .. } => {
let node = ast_id.to_node(db.upcast()); let node = ast_id.to_node(db.upcast());
( (
ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node))), ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node))),
node.path().map(|it| it.syntax().text_range()), node.path().map(|it| it.syntax().text_range()),
node.path().and_then(|it| it.segment()).map(|it| it.to_string()), node.path().and_then(|it| it.segment()).map(|it| it.to_string()),
MacroKind::ProcMacro,
) )
} }
MacroCallKind::Derive { ast_id, derive_attr_index, derive_index } => { 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))), ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node))),
token.as_ref().map(|tok| tok.text_range()), token.as_ref().map(|tok| tok.text_range()),
token.as_ref().map(ToString::to_string), token.as_ref().map(ToString::to_string),
MacroKind::Derive,
) )
} }
MacroCallKind::Attr { ast_id, invoc_attr_index, .. } => { 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()) .and_then(|seg| seg.name_ref())
.as_ref() .as_ref()
.map(ToString::to_string), .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 } => { DefDiagnosticKind::UnresolvedMacroCall { ast, path } => {
@ -1159,6 +1162,7 @@ impl DefWithBody {
node: node.clone().map(|it| it.into()), node: node.clone().map(|it| it.into()),
precise_location: None, precise_location: None,
macro_name: None, macro_name: None,
kind: MacroKind::ProcMacro,
} }
.into(), .into(),
), ),

View file

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

View file

@ -139,7 +139,8 @@ impl Default for ExprFillDefaultMode {
#[derive(Default, Debug, Clone)] #[derive(Default, Debug, Clone)]
pub struct DiagnosticsConfig { 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 disable_experimental: bool,
pub disabled: FxHashSet<String>, pub disabled: FxHashSet<String>,
pub expr_fill_default: ExprFillDefaultMode, pub expr_fill_default: ExprFillDefaultMode,
@ -205,7 +206,7 @@ pub fn diagnostics(
AnyDiagnostic::UnresolvedImport(d) => handlers::unresolved_import::unresolved_import(&ctx, &d), AnyDiagnostic::UnresolvedImport(d) => handlers::unresolved_import::unresolved_import(&ctx, &d),
AnyDiagnostic::UnresolvedMacroCall(d) => handlers::unresolved_macro_call::unresolved_macro_call(&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::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::InvalidDeriveTarget(d) => handlers::invalid_derive_target::invalid_derive_target(&ctx, &d),
AnyDiagnostic::InactiveCode(d) => match handlers::inactive_code::inactive_code(&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 // Feature: Highlight Related
// //
// Highlights constructs related to the thing under the cursor: // 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 an identifier, highlights all references to that identifier in the current file
// - if on a `return` or `fn` keyword, `?` character or `->` return type arrow, highlights all exit points for that context // . if on an `async` or `await token, highlights all yield points for that async context
// - if on a `break`, `loop`, `while` or `for` token, highlights all break points for that loop or block 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. // Note: `?` and `->` do not currently trigger this behavior in the VSCode editor.
pub(crate) fn highlight_related( pub(crate) fn highlight_related(

View file

@ -856,7 +856,8 @@ impl Config {
pub fn diagnostics(&self) -> DiagnosticsConfig { pub fn diagnostics(&self) -> DiagnosticsConfig {
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, disable_experimental: !self.data.diagnostics_experimental_enable,
disabled: self.data.diagnostics_disabled.clone(), disabled: self.data.diagnostics_disabled.clone(),
expr_fill_default: match self.data.assist_expressionFillDefault { expr_fill_default: match self.data.assist_expressionFillDefault {