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

Commit 7d51fc4

Browse files
committed
Show proc-macro loading errors in unresolved-proc-macro diagnostics
1 parent 15c63c4 commit 7d51fc4

File tree

14 files changed

+149
-114
lines changed

14 files changed

+149
-114
lines changed

crates/base-db/src/fixture.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl ChangeFixture {
159159
meta.cfg.clone(),
160160
meta.cfg,
161161
meta.env,
162-
Default::default(),
162+
Ok(Vec::new()),
163163
false,
164164
origin,
165165
);
@@ -194,7 +194,7 @@ impl ChangeFixture {
194194
default_cfg.clone(),
195195
default_cfg,
196196
Env::default(),
197-
Default::default(),
197+
Ok(Vec::new()),
198198
false,
199199
CrateOrigin::CratesIo { repo: None },
200200
);
@@ -231,7 +231,7 @@ impl ChangeFixture {
231231
CfgOptions::default(),
232232
CfgOptions::default(),
233233
Env::default(),
234-
Vec::new(),
234+
Ok(Vec::new()),
235235
false,
236236
CrateOrigin::Lang(LangCrateOrigin::Core),
237237
);
@@ -268,7 +268,7 @@ impl ChangeFixture {
268268
CfgOptions::default(),
269269
CfgOptions::default(),
270270
Env::default(),
271-
proc_macro,
271+
Ok(proc_macro),
272272
true,
273273
CrateOrigin::CratesIo { repo: None },
274274
);

crates/base-db/src/input.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ pub enum ProcMacroExpansionError {
231231
System(String),
232232
}
233233

234+
pub type ProcMacroLoadResult = Result<Vec<ProcMacro>, String>;
235+
234236
#[derive(Debug, Clone)]
235237
pub struct ProcMacro {
236238
pub name: SmolStr,
@@ -254,7 +256,7 @@ pub struct CrateData {
254256
pub potential_cfg_options: CfgOptions,
255257
pub env: Env,
256258
pub dependencies: Vec<Dependency>,
257-
pub proc_macro: Vec<ProcMacro>,
259+
pub proc_macro: ProcMacroLoadResult,
258260
pub origin: CrateOrigin,
259261
pub is_proc_macro: bool,
260262
}
@@ -300,19 +302,19 @@ impl Dependency {
300302
impl CrateGraph {
301303
pub fn add_crate_root(
302304
&mut self,
303-
file_id: FileId,
305+
root_file_id: FileId,
304306
edition: Edition,
305307
display_name: Option<CrateDisplayName>,
306308
version: Option<String>,
307309
cfg_options: CfgOptions,
308310
potential_cfg_options: CfgOptions,
309311
env: Env,
310-
proc_macro: Vec<ProcMacro>,
312+
proc_macro: ProcMacroLoadResult,
311313
is_proc_macro: bool,
312314
origin: CrateOrigin,
313315
) -> CrateId {
314316
let data = CrateData {
315-
root_file_id: file_id,
317+
root_file_id,
316318
edition,
317319
version,
318320
display_name,
@@ -628,7 +630,7 @@ mod tests {
628630
CfgOptions::default(),
629631
CfgOptions::default(),
630632
Env::default(),
631-
Default::default(),
633+
Ok(Vec::new()),
632634
false,
633635
CrateOrigin::CratesIo { repo: None },
634636
);
@@ -640,7 +642,7 @@ mod tests {
640642
CfgOptions::default(),
641643
CfgOptions::default(),
642644
Env::default(),
643-
Default::default(),
645+
Ok(Vec::new()),
644646
false,
645647
CrateOrigin::CratesIo { repo: None },
646648
);
@@ -652,7 +654,7 @@ mod tests {
652654
CfgOptions::default(),
653655
CfgOptions::default(),
654656
Env::default(),
655-
Default::default(),
657+
Ok(Vec::new()),
656658
false,
657659
CrateOrigin::CratesIo { repo: None },
658660
);
@@ -678,7 +680,7 @@ mod tests {
678680
CfgOptions::default(),
679681
CfgOptions::default(),
680682
Env::default(),
681-
Default::default(),
683+
Ok(Vec::new()),
682684
false,
683685
CrateOrigin::CratesIo { repo: None },
684686
);
@@ -690,7 +692,7 @@ mod tests {
690692
CfgOptions::default(),
691693
CfgOptions::default(),
692694
Env::default(),
693-
Default::default(),
695+
Ok(Vec::new()),
694696
false,
695697
CrateOrigin::CratesIo { repo: None },
696698
);
@@ -713,7 +715,7 @@ mod tests {
713715
CfgOptions::default(),
714716
CfgOptions::default(),
715717
Env::default(),
716-
Default::default(),
718+
Ok(Vec::new()),
717719
false,
718720
CrateOrigin::CratesIo { repo: None },
719721
);
@@ -725,7 +727,7 @@ mod tests {
725727
CfgOptions::default(),
726728
CfgOptions::default(),
727729
Env::default(),
728-
Default::default(),
730+
Ok(Vec::new()),
729731
false,
730732
CrateOrigin::CratesIo { repo: None },
731733
);
@@ -737,7 +739,7 @@ mod tests {
737739
CfgOptions::default(),
738740
CfgOptions::default(),
739741
Env::default(),
740-
Default::default(),
742+
Ok(Vec::new()),
741743
false,
742744
CrateOrigin::CratesIo { repo: None },
743745
);
@@ -760,7 +762,7 @@ mod tests {
760762
CfgOptions::default(),
761763
CfgOptions::default(),
762764
Env::default(),
763-
Default::default(),
765+
Ok(Vec::new()),
764766
false,
765767
CrateOrigin::CratesIo { repo: None },
766768
);
@@ -772,7 +774,7 @@ mod tests {
772774
CfgOptions::default(),
773775
CfgOptions::default(),
774776
Env::default(),
775-
Default::default(),
777+
Ok(Vec::new()),
776778
false,
777779
CrateOrigin::CratesIo { repo: None },
778780
);

crates/base-db/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub use crate::{
1313
input::{
1414
CrateData, CrateDisplayName, CrateGraph, CrateId, CrateName, CrateOrigin, Dependency,
1515
Edition, Env, LangCrateOrigin, ProcMacro, ProcMacroExpander, ProcMacroExpansionError,
16-
ProcMacroId, ProcMacroKind, SourceRoot, SourceRootId,
16+
ProcMacroId, ProcMacroKind, ProcMacroLoadResult, SourceRoot, SourceRootId,
1717
},
1818
};
1919
pub use salsa::{self, Cancelled};

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

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,27 @@ 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 = krate
78-
.proc_macro
79-
.iter()
80-
.enumerate()
81-
.map(|(idx, it)| {
82-
// FIXME: a hacky way to create a Name from string.
83-
let name = tt::Ident { text: it.name.clone(), id: tt::TokenId::unspecified() };
77+
let (proc_macros, proc_macro_err) = match &krate.proc_macro {
78+
Ok(proc_macros) => {
8479
(
85-
name.as_name(),
86-
ProcMacroExpander::new(def_map.krate, base_db::ProcMacroId(idx as u32)),
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,
8794
)
88-
})
89-
.collect();
95+
}
96+
Err(e) => (Vec::new(), Some(e.clone())),
97+
};
9098
let is_proc_macro = krate.is_proc_macro;
9199

92100
let mut collector = DefCollector {
@@ -100,6 +108,7 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: DefMap, tree_id: T
100108
mod_dirs: FxHashMap::default(),
101109
cfg_options,
102110
proc_macros,
111+
proc_macro_err,
103112
from_glob_import: Default::default(),
104113
skip_attrs: Default::default(),
105114
derive_helpers_in_scope: Default::default(),
@@ -241,6 +250,7 @@ struct DefCollector<'a> {
241250
/// empty when proc. macro support is disabled (in which case we still do name resolution for
242251
/// them).
243252
proc_macros: Vec<(Name, ProcMacroExpander)>,
253+
proc_macro_err: Option<String>,
244254
is_proc_macro: bool,
245255
from_glob_import: PerNsGlobImports,
246256
/// If we fail to resolve an attribute on a `ModItem`, we fall back to ignoring the attribute.
@@ -1232,6 +1242,7 @@ impl DefCollector<'_> {
12321242
self.def_map.diagnostics.push(DefDiagnostic::unresolved_proc_macro(
12331243
directive.module_id,
12341244
loc.kind,
1245+
self.proc_macro_err.clone(),
12351246
));
12361247

12371248
return recollect_without(self);
@@ -1283,7 +1294,11 @@ impl DefCollector<'_> {
12831294
let diag = match err {
12841295
hir_expand::ExpandError::UnresolvedProcMacro => {
12851296
// Missing proc macros are non-fatal, so they are handled specially.
1286-
DefDiagnostic::unresolved_proc_macro(module_id, loc.kind.clone())
1297+
DefDiagnostic::unresolved_proc_macro(
1298+
module_id,
1299+
loc.kind.clone(),
1300+
self.proc_macro_err.clone(),
1301+
)
12871302
}
12881303
_ => DefDiagnostic::macro_error(module_id, loc.kind.clone(), err.to_string()),
12891304
};
@@ -2097,6 +2112,7 @@ mod tests {
20972112
mod_dirs: FxHashMap::default(),
20982113
cfg_options: &CfgOptions::default(),
20992114
proc_macros: Default::default(),
2115+
proc_macro_err: None,
21002116
from_glob_import: Default::default(),
21012117
skip_attrs: Default::default(),
21022118
derive_helpers_in_scope: Default::default(),

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub enum DefDiagnosticKind {
2323

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

26-
UnresolvedProcMacro { ast: MacroCallKind },
26+
UnresolvedProcMacro { ast: MacroCallKind, proc_macro_err: Option<String> },
2727

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

@@ -81,8 +81,15 @@ impl DefDiagnostic {
8181
Self { in_module: container, kind: DefDiagnosticKind::UnconfiguredCode { ast, cfg, opts } }
8282
}
8383

84-
pub(super) fn unresolved_proc_macro(container: LocalModuleId, ast: MacroCallKind) -> Self {
85-
Self { in_module: container, kind: DefDiagnosticKind::UnresolvedProcMacro { ast } }
84+
pub(super) fn unresolved_proc_macro(
85+
container: LocalModuleId,
86+
ast: MacroCallKind,
87+
proc_macro_err: Option<String>,
88+
) -> Self {
89+
Self {
90+
in_module: container,
91+
kind: DefDiagnosticKind::UnresolvedProcMacro { ast, proc_macro_err },
92+
}
8693
}
8794

8895
pub(super) fn macro_error(

crates/hir-expand/src/proc_macro.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,15 @@ impl ProcMacroExpander {
3434
match self.proc_macro_id {
3535
Some(id) => {
3636
let krate_graph = db.crate_graph();
37-
let proc_macro = match krate_graph[self.krate].proc_macro.get(id.0 as usize) {
37+
let proc_macros = match &krate_graph[self.krate].proc_macro {
38+
Ok(proc_macros) => proc_macros,
39+
Err(e) => {
40+
return ExpandResult::only_err(ExpandError::Other(
41+
e.clone().into_boxed_str(),
42+
))
43+
}
44+
};
45+
let proc_macro = match proc_macros.get(id.0 as usize) {
3846
Some(proc_macro) => proc_macro,
3947
None => {
4048
return ExpandResult::only_err(ExpandError::Other(

crates/hir/src/diagnostics.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ pub struct UnresolvedProcMacro {
8787
pub precise_location: Option<TextRange>,
8888
pub macro_name: Option<String>,
8989
pub kind: MacroKind,
90+
pub proc_macro_err: Option<String>,
9091
}
9192

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

crates/hir/src/lib.rs

Lines changed: 12 additions & 2 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 } => {
630+
DefDiagnosticKind::UnresolvedProcMacro { ast, proc_macro_err } => {
631631
let (node, precise_location, macro_name, kind) = match ast {
632632
MacroCallKind::FnLike { ast_id, .. } => {
633633
let node = ast_id.to_node(db.upcast());
@@ -689,7 +689,16 @@ fn emit_def_diagnostic(db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>, diag:
689689
)
690690
}
691691
};
692-
acc.push(UnresolvedProcMacro { node, precise_location, macro_name, kind }.into());
692+
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(),
701+
);
693702
}
694703

695704
DefDiagnosticKind::UnresolvedMacroCall { ast, path } => {
@@ -1163,6 +1172,7 @@ impl DefWithBody {
11631172
precise_location: None,
11641173
macro_name: None,
11651174
kind: MacroKind::ProcMacro,
1175+
proc_macro_err: None,
11661176
}
11671177
.into(),
11681178
),

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,23 @@ pub(crate) fn unresolved_proc_macro(
2929
Some(name) => format!("proc macro `{}` not expanded", name),
3030
None => "proc macro not expanded".to_string(),
3131
};
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)")
32+
let severity = if config_enabled { Severity::Error } else { Severity::WeakWarning };
33+
let message = format!(
34+
"{message}: {}",
35+
if config_enabled {
36+
match &d.proc_macro_err {
37+
Some(e) => e,
38+
None => "proc macro not found",
3839
}
39-
_ => {
40-
format!("{message}{}", " (proc-macro expansion is disabled)")
40+
} else {
41+
match d.kind {
42+
hir::MacroKind::Attr if proc_macros_enabled => {
43+
"attribute macro expansion is disabled"
44+
}
45+
_ => "proc-macro expansion is disabled",
4146
}
42-
};
43-
(message, Severity::WeakWarning)
44-
};
47+
},
48+
);
4549

4650
Diagnostic::new("unresolved-proc-macro", message, display_range).severity(severity)
4751
}

crates/ide/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ impl Analysis {
233233
cfg_options.clone(),
234234
cfg_options,
235235
Env::default(),
236-
Default::default(),
236+
Ok(Vec::new()),
237237
false,
238238
CrateOrigin::CratesIo { repo: None },
239239
);

0 commit comments

Comments
 (0)