Skip to content

Commit 6407727

Browse files
committed
Auto merge of #12648 - flodiebold:proc-macro-errors-again, r=flodiebold
fix: Report proc macro errors in expressions correctly as well They didn't have a krate before, resulting in the generic "proc macro not found" error. Also improve error messages a bit more.
2 parents 9eaf96c + 8b3ec12 commit 6407727

File tree

12 files changed

+69
-69
lines changed

12 files changed

+69
-69
lines changed

crates/hir-def/src/body.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ pub struct SyntheticSyntax;
312312
pub enum BodyDiagnostic {
313313
InactiveCode { node: InFile<SyntaxNodePtr>, cfg: CfgExpr, opts: CfgOptions },
314314
MacroError { node: InFile<AstPtr<ast::MacroCall>>, message: String },
315-
UnresolvedProcMacro { node: InFile<AstPtr<ast::MacroCall>> },
315+
UnresolvedProcMacro { node: InFile<AstPtr<ast::MacroCall>>, krate: CrateId },
316316
UnresolvedMacroCall { node: InFile<AstPtr<ast::MacroCall>>, path: ModPath },
317317
}
318318

crates/hir-def/src/body/lower.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -572,9 +572,10 @@ impl ExprCollector<'_> {
572572

573573
if record_diagnostics {
574574
match &res.err {
575-
Some(ExpandError::UnresolvedProcMacro) => {
575+
Some(ExpandError::UnresolvedProcMacro(krate)) => {
576576
self.source_map.diagnostics.push(BodyDiagnostic::UnresolvedProcMacro {
577577
node: InFile::new(outer_file, syntax_ptr),
578+
krate: *krate,
578579
});
579580
}
580581
Some(err) => {

crates/hir-def/src/nameres/collector.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use itertools::Itertools;
2222
use la_arena::Idx;
2323
use limit::Limit;
2424
use rustc_hash::{FxHashMap, FxHashSet};
25+
use stdx::always;
2526
use syntax::{ast, SmolStr};
2627

2728
use crate::{
@@ -1234,7 +1235,7 @@ impl DefCollector<'_> {
12341235
self.def_map.diagnostics.push(DefDiagnostic::unresolved_proc_macro(
12351236
directive.module_id,
12361237
loc.kind,
1237-
Some(loc.def.krate),
1238+
loc.def.krate,
12381239
));
12391240
return recollect_without(self);
12401241
}
@@ -1258,7 +1259,7 @@ impl DefCollector<'_> {
12581259
self.def_map.diagnostics.push(DefDiagnostic::unresolved_proc_macro(
12591260
directive.module_id,
12601261
loc.kind,
1261-
Some(loc.def.krate),
1262+
loc.def.krate,
12621263
));
12631264

12641265
return recollect_without(self);
@@ -1308,13 +1309,10 @@ impl DefCollector<'_> {
13081309
let err = self.db.macro_expand_error(macro_call_id);
13091310
if let Some(err) = err {
13101311
let diag = match err {
1311-
hir_expand::ExpandError::UnresolvedProcMacro => {
1312+
hir_expand::ExpandError::UnresolvedProcMacro(krate) => {
1313+
always!(krate == loc.def.krate);
13121314
// Missing proc macros are non-fatal, so they are handled specially.
1313-
DefDiagnostic::unresolved_proc_macro(
1314-
module_id,
1315-
loc.kind.clone(),
1316-
Some(loc.def.krate),
1317-
)
1315+
DefDiagnostic::unresolved_proc_macro(module_id, loc.kind.clone(), loc.def.krate)
13181316
}
13191317
_ => DefDiagnostic::macro_error(module_id, loc.kind.clone(), err.to_string()),
13201318
};

crates/hir-def/src/nameres/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub enum DefDiagnosticKind {
2424

2525
UnconfiguredCode { ast: AstId<ast::Item>, cfg: CfgExpr, opts: CfgOptions },
2626

27-
UnresolvedProcMacro { ast: MacroCallKind, krate: Option<CrateId> },
27+
UnresolvedProcMacro { ast: MacroCallKind, krate: CrateId },
2828

2929
UnresolvedMacroCall { ast: MacroCallKind, path: ModPath },
3030

@@ -85,7 +85,7 @@ impl DefDiagnostic {
8585
pub(super) fn unresolved_proc_macro(
8686
container: LocalModuleId,
8787
ast: MacroCallKind,
88-
krate: Option<CrateId>,
88+
krate: CrateId,
8989
) -> Self {
9090
Self { in_module: container, kind: DefDiagnosticKind::UnresolvedProcMacro { ast, krate } }
9191
}

crates/hir-expand/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub type ExpandResult<T> = ValueResult<T, ExpandError>;
4343

4444
#[derive(Debug, PartialEq, Eq, Clone)]
4545
pub enum ExpandError {
46-
UnresolvedProcMacro,
46+
UnresolvedProcMacro(CrateId),
4747
Mbe(mbe::ExpandError),
4848
Other(Box<str>),
4949
}
@@ -57,7 +57,7 @@ impl From<mbe::ExpandError> for ExpandError {
5757
impl fmt::Display for ExpandError {
5858
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5959
match self {
60-
ExpandError::UnresolvedProcMacro => f.write_str("unresolved proc-macro"),
60+
ExpandError::UnresolvedProcMacro(_) => f.write_str("unresolved proc-macro"),
6161
ExpandError::Mbe(it) => it.fmt(f),
6262
ExpandError::Other(it) => f.write_str(it),
6363
}

crates/hir-expand/src/proc_macro.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl ProcMacroExpander {
7575
},
7676
}
7777
}
78-
None => ExpandResult::only_err(ExpandError::UnresolvedProcMacro),
78+
None => ExpandResult::only_err(ExpandError::UnresolvedProcMacro(self.krate)),
7979
}
8080
}
8181
}

crates/hir/src/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub struct UnresolvedProcMacro {
9090
pub macro_name: Option<String>,
9191
pub kind: MacroKind,
9292
/// The crate id of the proc-macro this macro belongs to, or `None` if the proc-macro can't be found.
93-
pub krate: Option<CrateId>,
93+
pub krate: CrateId,
9494
}
9595

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

crates/hir/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1159,13 +1159,13 @@ impl DefWithBody {
11591159
}
11601160
.into(),
11611161
),
1162-
BodyDiagnostic::UnresolvedProcMacro { node } => acc.push(
1162+
BodyDiagnostic::UnresolvedProcMacro { node, krate } => acc.push(
11631163
UnresolvedProcMacro {
11641164
node: node.clone().map(|it| it.into()),
11651165
precise_location: None,
11661166
macro_name: None,
11671167
kind: MacroKind::ProcMacro,
1168-
krate: None,
1168+
krate: *krate,
11691169
}
11701170
.into(),
11711171
),

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ pub(crate) fn unresolved_proc_macro(
3232
None => "proc macro not expanded".to_string(),
3333
};
3434
let severity = if config_enabled { Severity::Error } else { Severity::WeakWarning };
35-
let def_map = d.krate.map(|krate| ctx.sema.db.crate_def_map(krate));
35+
let def_map = ctx.sema.db.crate_def_map(d.krate);
3636
let message = format!(
3737
"{message}: {}",
3838
if config_enabled {
39-
match def_map.as_ref().and_then(|def_map| def_map.proc_macro_loading_error()) {
39+
match def_map.proc_macro_loading_error() {
4040
Some(e) => e,
41-
None => "proc macro not found",
41+
None => "proc macro not found in the built dylib",
4242
}
4343
} else {
4444
match d.kind {

0 commit comments

Comments
 (0)