7171: Emit diagnostics for unresolved item-level macros r=jonas-schievink a=jonas-schievink

We have been emitting macro *expansion* errors for macro calls in item position, as well as expansion and resolution errors for macro calls in bodies. This fills in the last gap, which is *resolution* errors for macro calls in item position (ie. those that name resolution cares about).

bors r+

Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
This commit is contained in:
bors[bot] 2021-01-05 14:46:47 +00:00 committed by GitHub
commit 16c544a497
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,7 +13,7 @@ use hir_expand::{
builtin_macro::find_builtin_macro,
name::{AsName, Name},
proc_macro::ProcMacroExpander,
HirFileId, MacroCallId, MacroDefId, MacroDefKind,
HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
};
use hir_expand::{InFile, MacroCallLoc};
use rustc_hash::{FxHashMap, FxHashSet};
@ -860,6 +860,37 @@ impl DefCollector<'_> {
}
fn finish(mut self) -> CrateDefMap {
// Emit diagnostics for all remaining unexpanded macros.
for directive in &self.unexpanded_macros {
let mut error = None;
directive.ast_id.as_call_id_with_errors(
self.db,
self.def_map.krate,
|path| {
let resolved_res = self.def_map.resolve_path_fp_with_macro(
self.db,
ResolveMode::Other,
directive.module_id,
&path,
BuiltinShadowMode::Module,
);
resolved_res.resolved_def.take_macros()
},
&mut |e| {
error.get_or_insert(e);
},
);
if let Some(err) = error {
self.def_map.diagnostics.push(DefDiagnostic::macro_error(
directive.module_id,
MacroCallKind::FnLike(directive.ast_id.ast_id),
err.to_string(),
));
}
}
// Emit diagnostics for all remaining unresolved imports.
// We'd like to avoid emitting a diagnostics avalanche when some `extern crate` doesn't