Skip to content

Commit 6c65edf

Browse files
committed
Auto merge of rust-lang#12514 - Veykril:proc-mac-err, r=Veykril
More precise proc-macro errors
2 parents 4f2a67b + 0e4eb64 commit 6c65edf

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

crates/hir/src/lib.rs

+19-10
Original file line numberDiff line numberDiff line change
@@ -628,35 +628,42 @@ fn emit_def_diagnostic(db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>, diag:
628628
}
629629

630630
DefDiagnosticKind::UnresolvedProcMacro { ast } => {
631-
let mut precise_location = None;
632-
let (node, macro_name) = match ast {
631+
let (node, precise_location, macro_name) = match ast {
633632
MacroCallKind::FnLike { ast_id, .. } => {
634633
let node = ast_id.to_node(db.upcast());
635-
(ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node))), None)
634+
(
635+
ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node))),
636+
node.path().map(|it| it.syntax().text_range()),
637+
node.path().and_then(|it| it.segment()).map(|it| it.to_string()),
638+
)
636639
}
637640
MacroCallKind::Derive { ast_id, derive_attr_index, derive_index } => {
638641
let node = ast_id.to_node(db.upcast());
639-
640642
// Compute the precise location of the macro name's token in the derive
641643
// list.
642644
let token = (|| {
643-
let derive_attr = node.attrs().nth(*derive_attr_index as usize)?;
644-
derive_attr
645+
let derive_attr = node
646+
.doc_comments_and_attrs()
647+
.nth(*derive_attr_index as usize)
648+
.and_then(Either::left)?;
649+
let token_tree = derive_attr.meta()?.token_tree()?;
650+
let group_by = token_tree
645651
.syntax()
646652
.children_with_tokens()
647653
.filter_map(|elem| match elem {
648654
syntax::NodeOrToken::Token(tok) => Some(tok),
649655
_ => None,
650656
})
651-
.group_by(|t| t.kind() == T![,])
657+
.group_by(|t| t.kind() == T![,]);
658+
let (_, mut group) = group_by
652659
.into_iter()
653660
.filter(|&(comma, _)| !comma)
654-
.nth(*derive_index as usize)
655-
.and_then(|(_, mut g)| g.find(|t| t.kind() == T![ident]))
661+
.nth(*derive_index as usize)?;
662+
group.find(|t| t.kind() == T![ident])
656663
})();
657-
precise_location = token.as_ref().map(|tok| tok.text_range());
658664
(
659665
ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node))),
666+
token.as_ref().map(|tok| tok.text_range()),
660667
token.as_ref().map(ToString::to_string),
661668
)
662669
}
@@ -667,8 +674,10 @@ fn emit_def_diagnostic(db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>, diag:
667674
.nth((*invoc_attr_index) as usize)
668675
.and_then(Either::left)
669676
.unwrap_or_else(|| panic!("cannot find attribute #{}", invoc_attr_index));
677+
670678
(
671679
ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&attr))),
680+
Some(attr.syntax().text_range()),
672681
attr.path()
673682
.and_then(|path| path.segment())
674683
.and_then(|seg| seg.name_ref())

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,25 @@ use crate::{Diagnostic, DiagnosticsContext, Severity};
88
//
99
// If you are seeing a lot of "proc macro not expanded" warnings, you can add this option to the
1010
// `rust-analyzer.diagnostics.disabled` list to prevent them from showing. Alternatively you can
11-
// enable support for procedural macros (see `rust-analyzer.procMacro.enable`).
11+
// enable support for procedural macros (see `rust-analyzer.procMacro.attributes.enable`).
1212
pub(crate) fn unresolved_proc_macro(
1313
ctx: &DiagnosticsContext<'_>,
1414
d: &hir::UnresolvedProcMacro,
15+
attr_proc_macros_enabled: bool,
1516
) -> Diagnostic {
1617
// Use more accurate position if available.
1718
let display_range = d
1819
.precise_location
1920
.unwrap_or_else(|| ctx.sema.diagnostics_display_range(d.node.clone()).range);
20-
// FIXME: it would be nice to tell the user whether proc macros are currently disabled
2121
let message = match &d.macro_name {
2222
Some(name) => format!("proc macro `{}` not expanded", name),
2323
None => "proc macro not expanded".to_string(),
2424
};
25+
let message = format!(
26+
"{message}{}",
27+
if attr_proc_macros_enabled { "" } else { " (attribute macro expansion is disabled)" }
28+
);
2529

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

crates/ide-diagnostics/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ impl Default for ExprFillDefaultMode {
139139

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

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

crates/rust-analyzer/src/config.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,7 @@ impl Config {
856856

857857
pub fn diagnostics(&self) -> DiagnosticsConfig {
858858
DiagnosticsConfig {
859+
attr_proc_macros_enabled: self.expand_proc_attr_macros(),
859860
disable_experimental: !self.data.diagnostics_experimental_enable,
860861
disabled: self.data.diagnostics_disabled.clone(),
861862
expr_fill_default: match self.data.assist_expressionFillDefault {
@@ -893,7 +894,7 @@ impl Config {
893894
}
894895

895896
pub fn expand_proc_attr_macros(&self) -> bool {
896-
self.data.procMacro_attributes_enable
897+
self.data.procMacro_enable && self.data.procMacro_attributes_enable
897898
}
898899

899900
pub fn files(&self) -> FilesConfig {

0 commit comments

Comments
 (0)