Show proc-macro loading errors in unresolved-proc-macro diagnostics

This commit is contained in:
Lukas Wirth 2022-06-15 17:33:55 +02:00
parent 15c63c4119
commit 7d51fc4640
14 changed files with 149 additions and 114 deletions

View file

@ -74,19 +74,27 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: DefMap, tree_id: T
}
let cfg_options = &krate.cfg_options;
let proc_macros = krate
.proc_macro
.iter()
.enumerate()
.map(|(idx, it)| {
// FIXME: a hacky way to create a Name from string.
let name = tt::Ident { text: it.name.clone(), id: tt::TokenId::unspecified() };
let (proc_macros, proc_macro_err) = match &krate.proc_macro {
Ok(proc_macros) => {
(
name.as_name(),
ProcMacroExpander::new(def_map.krate, base_db::ProcMacroId(idx as u32)),
proc_macros
.iter()
.enumerate()
.map(|(idx, it)| {
// FIXME: a hacky way to create a Name from string.
let name =
tt::Ident { text: it.name.clone(), id: tt::TokenId::unspecified() };
(
name.as_name(),
ProcMacroExpander::new(def_map.krate, base_db::ProcMacroId(idx as u32)),
)
})
.collect(),
None,
)
})
.collect();
}
Err(e) => (Vec::new(), Some(e.clone())),
};
let is_proc_macro = krate.is_proc_macro;
let mut collector = DefCollector {
@ -100,6 +108,7 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: DefMap, tree_id: T
mod_dirs: FxHashMap::default(),
cfg_options,
proc_macros,
proc_macro_err,
from_glob_import: Default::default(),
skip_attrs: Default::default(),
derive_helpers_in_scope: Default::default(),
@ -241,6 +250,7 @@ struct DefCollector<'a> {
/// empty when proc. macro support is disabled (in which case we still do name resolution for
/// them).
proc_macros: Vec<(Name, ProcMacroExpander)>,
proc_macro_err: Option<String>,
is_proc_macro: bool,
from_glob_import: PerNsGlobImports,
/// If we fail to resolve an attribute on a `ModItem`, we fall back to ignoring the attribute.
@ -1232,6 +1242,7 @@ impl DefCollector<'_> {
self.def_map.diagnostics.push(DefDiagnostic::unresolved_proc_macro(
directive.module_id,
loc.kind,
self.proc_macro_err.clone(),
));
return recollect_without(self);
@ -1283,7 +1294,11 @@ impl DefCollector<'_> {
let diag = match err {
hir_expand::ExpandError::UnresolvedProcMacro => {
// Missing proc macros are non-fatal, so they are handled specially.
DefDiagnostic::unresolved_proc_macro(module_id, loc.kind.clone())
DefDiagnostic::unresolved_proc_macro(
module_id,
loc.kind.clone(),
self.proc_macro_err.clone(),
)
}
_ => DefDiagnostic::macro_error(module_id, loc.kind.clone(), err.to_string()),
};
@ -2097,6 +2112,7 @@ mod tests {
mod_dirs: FxHashMap::default(),
cfg_options: &CfgOptions::default(),
proc_macros: Default::default(),
proc_macro_err: None,
from_glob_import: Default::default(),
skip_attrs: Default::default(),
derive_helpers_in_scope: Default::default(),

View file

@ -23,7 +23,7 @@ pub enum DefDiagnosticKind {
UnconfiguredCode { ast: AstId<ast::Item>, cfg: CfgExpr, opts: CfgOptions },
UnresolvedProcMacro { ast: MacroCallKind },
UnresolvedProcMacro { ast: MacroCallKind, proc_macro_err: Option<String> },
UnresolvedMacroCall { ast: MacroCallKind, path: ModPath },
@ -81,8 +81,15 @@ impl DefDiagnostic {
Self { in_module: container, kind: DefDiagnosticKind::UnconfiguredCode { ast, cfg, opts } }
}
pub(super) fn unresolved_proc_macro(container: LocalModuleId, ast: MacroCallKind) -> Self {
Self { in_module: container, kind: DefDiagnosticKind::UnresolvedProcMacro { ast } }
pub(super) fn unresolved_proc_macro(
container: LocalModuleId,
ast: MacroCallKind,
proc_macro_err: Option<String>,
) -> Self {
Self {
in_module: container,
kind: DefDiagnosticKind::UnresolvedProcMacro { ast, proc_macro_err },
}
}
pub(super) fn macro_error(