Look for macro names in all namespaces for diagnostics.

This commit is contained in:
Mara Bos 2021-08-22 15:02:35 +02:00
parent 9051c056ed
commit 7977cb43b0
2 changed files with 34 additions and 19 deletions

View file

@ -956,9 +956,42 @@ impl<'a> Resolver<'a> {
if macro_kind == MacroKind::Derive && (ident.name == sym::Send || ident.name == sym::Sync) {
let msg = format!("unsafe traits like `{}` should be implemented explicitly", ident);
err.span_note(ident.span, &msg);
return;
}
if self.macro_names.contains(&ident.normalize_to_macros_2_0()) {
err.help("have you added the `#[macro_use]` on the module/import?");
return;
}
for ns in [Namespace::MacroNS, Namespace::TypeNS, Namespace::ValueNS] {
if let Ok(binding) = self.early_resolve_ident_in_lexical_scope(
ident,
ScopeSet::All(ns, false),
&parent_scope,
false,
false,
ident.span,
) {
let it_is = match binding.macro_kind() {
Some(MacroKind::Bang) => "it is a function-like macro".to_string(),
Some(kind) => format!("it is {} {}", kind.article(), kind.descr_expected()),
None => format!(
"it is not {} {}",
macro_kind.article(),
macro_kind.descr_expected()
),
};
if let crate::NameBindingKind::Import { import, .. } = binding.kind {
if !import.span.is_dummy() {
err.span_note(
import.span,
&format!("`{}` is imported here, but {}", ident, it_is),
);
return;
}
}
err.note(&format!("`{}` is in scope, but {}", ident, it_is));
return;
}
}
}

View file

@ -19,7 +19,7 @@ use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind};
use rustc_expand::compile_declarative_macro;
use rustc_expand::expand::{AstFragment, Invocation, InvocationKind, SupportsMacroExpansion};
use rustc_feature::is_builtin_attr_name;
use rustc_hir::def::{self, DefKind, Namespace, NonMacroAttrKind};
use rustc_hir::def::{self, DefKind, NonMacroAttrKind};
use rustc_hir::def_id::{CrateNum, LocalDefId};
use rustc_hir::PrimTy;
use rustc_middle::middle::stability;
@ -1115,24 +1115,6 @@ impl<'a> Resolver<'a> {
let msg = format!("cannot find {} `{}` in this scope", expected, ident);
let mut err = self.session.struct_span_err(ident.span, &msg);
self.unresolved_macro_suggestions(&mut err, kind, &parent_scope, ident);
if let Ok(binding) = self.early_resolve_ident_in_lexical_scope(
ident,
ScopeSet::All(Namespace::TypeNS, false),
&parent_scope,
false,
false,
ident.span,
) {
if let crate::NameBindingKind::Import { import, .. } = binding.kind {
err.span_note(
import.span,
&format!(
"`{}` is imported here, but it is not a {}",
ident, expected
),
);
}
}
err.emit();
}
}