Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 1ad6d32

Browse files
committed
Auto merge of rust-lang#12528 - Veykril:proc-diag, r=Veykril
fix: Check for the correct proc-macro settings in missing proc-macro diagnostics
2 parents 7db7387 + 325ceae commit 1ad6d32

File tree

6 files changed

+40
-17
lines changed

6 files changed

+40
-17
lines changed

crates/hir/src/diagnostics.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use hir_def::path::ModPath;
99
use hir_expand::{name::Name, HirFileId, InFile};
1010
use syntax::{ast, AstPtr, SyntaxNodePtr, TextRange};
1111

12-
use crate::Type;
12+
use crate::{MacroKind, Type};
1313

1414
macro_rules! diagnostics {
1515
($($diag:ident,)*) => {
@@ -86,6 +86,7 @@ pub struct UnresolvedProcMacro {
8686
/// to use instead.
8787
pub precise_location: Option<TextRange>,
8888
pub macro_name: Option<String>,
89+
pub kind: MacroKind,
8990
}
9091

9192
#[derive(Debug, Clone, Eq, PartialEq)]

crates/hir/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,13 +628,14 @@ fn emit_def_diagnostic(db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>, diag:
628628
}
629629

630630
DefDiagnosticKind::UnresolvedProcMacro { ast } => {
631-
let (node, precise_location, macro_name) = match ast {
631+
let (node, precise_location, macro_name, kind) = match ast {
632632
MacroCallKind::FnLike { ast_id, .. } => {
633633
let node = ast_id.to_node(db.upcast());
634634
(
635635
ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node))),
636636
node.path().map(|it| it.syntax().text_range()),
637637
node.path().and_then(|it| it.segment()).map(|it| it.to_string()),
638+
MacroKind::ProcMacro,
638639
)
639640
}
640641
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:
665666
ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node))),
666667
token.as_ref().map(|tok| tok.text_range()),
667668
token.as_ref().map(ToString::to_string),
669+
MacroKind::Derive,
668670
)
669671
}
670672
MacroCallKind::Attr { ast_id, invoc_attr_index, .. } => {
@@ -683,10 +685,11 @@ fn emit_def_diagnostic(db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>, diag:
683685
.and_then(|seg| seg.name_ref())
684686
.as_ref()
685687
.map(ToString::to_string),
688+
MacroKind::Attr,
686689
)
687690
}
688691
};
689-
acc.push(UnresolvedProcMacro { node, precise_location, macro_name }.into());
692+
acc.push(UnresolvedProcMacro { node, precise_location, macro_name, kind }.into());
690693
}
691694

692695
DefDiagnosticKind::UnresolvedMacroCall { ast, path } => {
@@ -1159,6 +1162,7 @@ impl DefWithBody {
11591162
node: node.clone().map(|it| it.into()),
11601163
precise_location: None,
11611164
macro_name: None,
1165+
kind: MacroKind::ProcMacro,
11621166
}
11631167
.into(),
11641168
),

crates/ide-diagnostics/src/handlers/unresolved_proc_macro.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,36 @@ use crate::{Diagnostic, DiagnosticsContext, Severity};
1212
pub(crate) fn unresolved_proc_macro(
1313
ctx: &DiagnosticsContext<'_>,
1414
d: &hir::UnresolvedProcMacro,
15-
attr_proc_macros_enabled: bool,
15+
proc_macros_enabled: bool,
16+
proc_attr_macros_enabled: bool,
1617
) -> Diagnostic {
1718
// Use more accurate position if available.
1819
let display_range = d
1920
.precise_location
2021
.unwrap_or_else(|| ctx.sema.diagnostics_display_range(d.node.clone()).range);
22+
23+
let config_enabled = match d.kind {
24+
hir::MacroKind::Attr => proc_macros_enabled && proc_attr_macros_enabled,
25+
_ => proc_macros_enabled,
26+
};
27+
2128
let message = match &d.macro_name {
2229
Some(name) => format!("proc macro `{}` not expanded", name),
2330
None => "proc macro not expanded".to_string(),
2431
};
25-
let message = format!(
26-
"{message}{}",
27-
if attr_proc_macros_enabled { "" } else { " (attribute macro expansion is disabled)" }
28-
);
32+
let (message, severity) = if config_enabled {
33+
(message, Severity::Error)
34+
} else {
35+
let message = match d.kind {
36+
hir::MacroKind::Attr if proc_macros_enabled => {
37+
format!("{message}{}", " (attribute macro expansion is disabled)")
38+
}
39+
_ => {
40+
format!("{message}{}", " (proc-macro expansion is disabled)")
41+
}
42+
};
43+
(message, Severity::WeakWarning)
44+
};
2945

30-
Diagnostic::new("unresolved-proc-macro", message, display_range)
31-
.severity(if attr_proc_macros_enabled { Severity::Error } else { Severity::WeakWarning })
46+
Diagnostic::new("unresolved-proc-macro", message, display_range).severity(severity)
3247
}

crates/ide-diagnostics/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ impl Default for ExprFillDefaultMode {
139139

140140
#[derive(Default, Debug, Clone)]
141141
pub struct DiagnosticsConfig {
142-
pub attr_proc_macros_enabled: bool,
142+
pub proc_macros_enabled: bool,
143+
pub proc_attr_macros_enabled: bool,
143144
pub disable_experimental: bool,
144145
pub disabled: FxHashSet<String>,
145146
pub expr_fill_default: ExprFillDefaultMode,
@@ -205,7 +206,7 @@ pub fn diagnostics(
205206
AnyDiagnostic::UnresolvedImport(d) => handlers::unresolved_import::unresolved_import(&ctx, &d),
206207
AnyDiagnostic::UnresolvedMacroCall(d) => handlers::unresolved_macro_call::unresolved_macro_call(&ctx, &d),
207208
AnyDiagnostic::UnresolvedModule(d) => handlers::unresolved_module::unresolved_module(&ctx, &d),
208-
AnyDiagnostic::UnresolvedProcMacro(d) => handlers::unresolved_proc_macro::unresolved_proc_macro(&ctx, &d, config.attr_proc_macros_enabled),
209+
AnyDiagnostic::UnresolvedProcMacro(d) => handlers::unresolved_proc_macro::unresolved_proc_macro(&ctx, &d, config.proc_macros_enabled, config.proc_attr_macros_enabled),
209210
AnyDiagnostic::InvalidDeriveTarget(d) => handlers::invalid_derive_target::invalid_derive_target(&ctx, &d),
210211

211212
AnyDiagnostic::InactiveCode(d) => match handlers::inactive_code::inactive_code(&ctx, &d) {

crates/ide/src/highlight_related.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ pub struct HighlightRelatedConfig {
3636
// Feature: Highlight Related
3737
//
3838
// Highlights constructs related to the thing under the cursor:
39-
// - if on an identifier, highlights all references to that identifier in the current file
40-
// - if on an `async` or `await token, highlights all yield points for that async context
41-
// - if on a `return` or `fn` keyword, `?` character or `->` return type arrow, highlights all exit points for that context
42-
// - if on a `break`, `loop`, `while` or `for` token, highlights all break points for that loop or block context
39+
//
40+
// . if on an identifier, highlights all references to that identifier in the current file
41+
// . if on an `async` or `await token, highlights all yield points for that async context
42+
// . if on a `return` or `fn` keyword, `?` character or `->` return type arrow, highlights all exit points for that context
43+
// . if on a `break`, `loop`, `while` or `for` token, highlights all break points for that loop or block context
4344
//
4445
// Note: `?` and `->` do not currently trigger this behavior in the VSCode editor.
4546
pub(crate) fn highlight_related(

crates/rust-analyzer/src/config.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,8 @@ impl Config {
856856

857857
pub fn diagnostics(&self) -> DiagnosticsConfig {
858858
DiagnosticsConfig {
859-
attr_proc_macros_enabled: self.expand_proc_attr_macros(),
859+
proc_attr_macros_enabled: self.expand_proc_attr_macros(),
860+
proc_macros_enabled: self.data.procMacro_enable,
860861
disable_experimental: !self.data.diagnostics_experimental_enable,
861862
disabled: self.data.diagnostics_disabled.clone(),
862863
expr_fill_default: match self.data.assist_expressionFillDefault {

0 commit comments

Comments
 (0)