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

Commit 0e41d15

Browse files
committed
Use the correct crates proc-macro loading error message
1 parent 1d34cdc commit 0e41d15

File tree

6 files changed

+42
-42
lines changed

6 files changed

+42
-42
lines changed

crates/hir-def/src/nameres.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ pub struct DefMap {
103103
/// Side table for resolving derive helpers.
104104
exported_derives: FxHashMap<MacroDefId, Box<[Name]>>,
105105
fn_proc_macro_mapping: FxHashMap<FunctionId, ProcMacroId>,
106+
/// The error that occurred when failing to load the proc-macro dll.
107+
proc_macro_loading_error: Option<Box<str>>,
106108

107109
/// Custom attributes registered with `#![register_attr]`.
108110
registered_attrs: Vec<SmolStr>,
@@ -273,6 +275,7 @@ impl DefMap {
273275
extern_prelude: FxHashMap::default(),
274276
exported_derives: FxHashMap::default(),
275277
fn_proc_macro_mapping: FxHashMap::default(),
278+
proc_macro_loading_error: None,
276279
prelude: None,
277280
root,
278281
modules,
@@ -305,6 +308,9 @@ impl DefMap {
305308
pub fn fn_as_proc_macro(&self, id: FunctionId) -> Option<ProcMacroId> {
306309
self.fn_proc_macro_mapping.get(&id).copied()
307310
}
311+
pub fn proc_macro_loading_error(&self) -> Option<&str> {
312+
self.proc_macro_loading_error.as_deref()
313+
}
308314

309315
pub(crate) fn krate(&self) -> CrateId {
310316
self.krate
@@ -460,6 +466,7 @@ impl DefMap {
460466
registered_attrs,
461467
registered_tools,
462468
fn_proc_macro_mapping,
469+
proc_macro_loading_error: _,
463470
block: _,
464471
edition: _,
465472
recursion_limit: _,

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

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -74,26 +74,25 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: DefMap, tree_id: T
7474
}
7575

7676
let cfg_options = &krate.cfg_options;
77-
let (proc_macros, proc_macro_err) = match &krate.proc_macro {
77+
let proc_macros = match &krate.proc_macro {
7878
Ok(proc_macros) => {
79-
(
80-
proc_macros
81-
.iter()
82-
.enumerate()
83-
.map(|(idx, it)| {
84-
// FIXME: a hacky way to create a Name from string.
85-
let name =
86-
tt::Ident { text: it.name.clone(), id: tt::TokenId::unspecified() };
87-
(
88-
name.as_name(),
89-
ProcMacroExpander::new(def_map.krate, base_db::ProcMacroId(idx as u32)),
90-
)
91-
})
92-
.collect(),
93-
None,
94-
)
79+
proc_macros
80+
.iter()
81+
.enumerate()
82+
.map(|(idx, it)| {
83+
// FIXME: a hacky way to create a Name from string.
84+
let name = tt::Ident { text: it.name.clone(), id: tt::TokenId::unspecified() };
85+
(
86+
name.as_name(),
87+
ProcMacroExpander::new(def_map.krate, base_db::ProcMacroId(idx as u32)),
88+
)
89+
})
90+
.collect()
91+
}
92+
Err(e) => {
93+
def_map.proc_macro_loading_error = Some(e.clone().into_boxed_str());
94+
Vec::new()
9595
}
96-
Err(e) => (Vec::new(), Some(e.clone())),
9796
};
9897
let is_proc_macro = krate.is_proc_macro;
9998

@@ -108,7 +107,6 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: DefMap, tree_id: T
108107
mod_dirs: FxHashMap::default(),
109108
cfg_options,
110109
proc_macros,
111-
proc_macro_err,
112110
from_glob_import: Default::default(),
113111
skip_attrs: Default::default(),
114112
derive_helpers_in_scope: Default::default(),
@@ -250,7 +248,6 @@ struct DefCollector<'a> {
250248
/// empty when proc. macro support is disabled (in which case we still do name resolution for
251249
/// them).
252250
proc_macros: Vec<(Name, ProcMacroExpander)>,
253-
proc_macro_err: Option<String>,
254251
is_proc_macro: bool,
255252
from_glob_import: PerNsGlobImports,
256253
/// If we fail to resolve an attribute on a `ModItem`, we fall back to ignoring the attribute.
@@ -1147,7 +1144,7 @@ impl DefCollector<'_> {
11471144
invoc_attr_index: attr.id.ast_index,
11481145
is_derive: false,
11491146
},
1150-
self.proc_macro_err.clone(),
1147+
None,
11511148
));
11521149
return true;
11531150
}
@@ -1254,7 +1251,7 @@ impl DefCollector<'_> {
12541251
self.def_map.diagnostics.push(DefDiagnostic::unresolved_proc_macro(
12551252
directive.module_id,
12561253
loc.kind,
1257-
self.proc_macro_err.clone(),
1254+
Some(loc.def.krate),
12581255
));
12591256

12601257
return recollect_without(self);
@@ -1309,7 +1306,7 @@ impl DefCollector<'_> {
13091306
DefDiagnostic::unresolved_proc_macro(
13101307
module_id,
13111308
loc.kind.clone(),
1312-
self.proc_macro_err.clone(),
1309+
Some(loc.def.krate),
13131310
)
13141311
}
13151312
_ => DefDiagnostic::macro_error(module_id, loc.kind.clone(), err.to_string()),
@@ -2124,7 +2121,6 @@ mod tests {
21242121
mod_dirs: FxHashMap::default(),
21252122
cfg_options: &CfgOptions::default(),
21262123
proc_macros: Default::default(),
2127-
proc_macro_err: None,
21282124
from_glob_import: Default::default(),
21292125
skip_attrs: Default::default(),
21302126
derive_helpers_in_scope: Default::default(),

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Diagnostics emitted during DefMap construction.
22
3+
use base_db::CrateId;
34
use cfg::{CfgExpr, CfgOptions};
45
use hir_expand::MacroCallKind;
56
use la_arena::Idx;
@@ -23,7 +24,7 @@ pub enum DefDiagnosticKind {
2324

2425
UnconfiguredCode { ast: AstId<ast::Item>, cfg: CfgExpr, opts: CfgOptions },
2526

26-
UnresolvedProcMacro { ast: MacroCallKind, proc_macro_err: Option<String> },
27+
UnresolvedProcMacro { ast: MacroCallKind, krate: Option<CrateId> },
2728

2829
UnresolvedMacroCall { ast: MacroCallKind, path: ModPath },
2930

@@ -84,12 +85,9 @@ impl DefDiagnostic {
8485
pub(super) fn unresolved_proc_macro(
8586
container: LocalModuleId,
8687
ast: MacroCallKind,
87-
proc_macro_err: Option<String>,
88+
krate: Option<CrateId>,
8889
) -> Self {
89-
Self {
90-
in_module: container,
91-
kind: DefDiagnosticKind::UnresolvedProcMacro { ast, proc_macro_err },
92-
}
90+
Self { in_module: container, kind: DefDiagnosticKind::UnresolvedProcMacro { ast, krate } }
9391
}
9492

9593
pub(super) fn macro_error(

crates/hir/src/diagnostics.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//!
44
//! This probably isn't the best way to do this -- ideally, diagnistics should
55
//! be expressed in terms of hir types themselves.
6+
use base_db::CrateId;
67
use cfg::{CfgExpr, CfgOptions};
78
use either::Either;
89
use hir_def::path::ModPath;
@@ -87,7 +88,8 @@ pub struct UnresolvedProcMacro {
8788
pub precise_location: Option<TextRange>,
8889
pub macro_name: Option<String>,
8990
pub kind: MacroKind,
90-
pub proc_macro_err: Option<String>,
91+
/// The crate id of the proc-macro this macro belongs to, or `None` if the proc-macro can't be found.
92+
pub krate: Option<CrateId>,
9193
}
9294

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

crates/hir/src/lib.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ fn emit_def_diagnostic(db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>, diag:
627627
);
628628
}
629629

630-
DefDiagnosticKind::UnresolvedProcMacro { ast, proc_macro_err } => {
630+
DefDiagnosticKind::UnresolvedProcMacro { ast, krate } => {
631631
let (node, precise_location, macro_name, kind) = match ast {
632632
MacroCallKind::FnLike { ast_id, .. } => {
633633
let node = ast_id.to_node(db.upcast());
@@ -690,14 +690,8 @@ fn emit_def_diagnostic(db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>, diag:
690690
}
691691
};
692692
acc.push(
693-
UnresolvedProcMacro {
694-
node,
695-
precise_location,
696-
macro_name,
697-
kind,
698-
proc_macro_err: proc_macro_err.clone(),
699-
}
700-
.into(),
693+
UnresolvedProcMacro { node, precise_location, macro_name, kind, krate: *krate }
694+
.into(),
701695
);
702696
}
703697

@@ -1172,7 +1166,7 @@ impl DefWithBody {
11721166
precise_location: None,
11731167
macro_name: None,
11741168
kind: MacroKind::ProcMacro,
1175-
proc_macro_err: None,
1169+
krate: None,
11761170
}
11771171
.into(),
11781172
),

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use hir::db::DefDatabase;
2+
13
use crate::{Diagnostic, DiagnosticsContext, Severity};
24

35
// Diagnostic: unresolved-proc-macro
@@ -30,10 +32,11 @@ pub(crate) fn unresolved_proc_macro(
3032
None => "proc macro not expanded".to_string(),
3133
};
3234
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));
3336
let message = format!(
3437
"{message}: {}",
3538
if config_enabled {
36-
match &d.proc_macro_err {
39+
match def_map.as_ref().and_then(|def_map| def_map.proc_macro_loading_error()) {
3740
Some(e) => e,
3841
None => "proc macro not found",
3942
}

0 commit comments

Comments
 (0)