From 325ceaef19df2ab6a21fd2ed3796607d85e0de4c Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 14 Jun 2022 10:40:57 +0200 Subject: [PATCH] fix: Check for the correct proc-macro settings in missing proc-macro diagnostics --- crates/hir/src/diagnostics.rs | 3 +- crates/hir/src/lib.rs | 8 +++-- .../src/handlers/unresolved_proc_macro.rs | 29 ++++++++++++++----- crates/ide-diagnostics/src/lib.rs | 5 ++-- crates/ide/src/highlight_related.rs | 9 +++--- crates/rust-analyzer/src/config.rs | 3 +- 6 files changed, 40 insertions(+), 17 deletions(-) diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs index 08e239804c..0c88d15b08 100644 --- a/crates/hir/src/diagnostics.rs +++ b/crates/hir/src/diagnostics.rs @@ -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, pub macro_name: Option, + pub kind: MacroKind, } #[derive(Debug, Clone, Eq, PartialEq)] diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 193ef904de..4620d0c03a 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -628,13 +628,14 @@ fn emit_def_diagnostic(db: &dyn HirDatabase, acc: &mut Vec, 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, 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, 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(), ), diff --git a/crates/ide-diagnostics/src/handlers/unresolved_proc_macro.rs b/crates/ide-diagnostics/src/handlers/unresolved_proc_macro.rs index fea3a0e832..37350a7aaf 100644 --- a/crates/ide-diagnostics/src/handlers/unresolved_proc_macro.rs +++ b/crates/ide-diagnostics/src/handlers/unresolved_proc_macro.rs @@ -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) } diff --git a/crates/ide-diagnostics/src/lib.rs b/crates/ide-diagnostics/src/lib.rs index 1cbe8ad72b..eeddd36fb5 100644 --- a/crates/ide-diagnostics/src/lib.rs +++ b/crates/ide-diagnostics/src/lib.rs @@ -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, 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) { diff --git a/crates/ide/src/highlight_related.rs b/crates/ide/src/highlight_related.rs index 5b235e71ac..eae5fc8d17 100644 --- a/crates/ide/src/highlight_related.rs +++ b/crates/ide/src/highlight_related.rs @@ -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( diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index fcc4cbebc5..e5ac10556d 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -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 {