Skip to content

Commit 7977cb4

Browse files
committed
Look for macro names in all namespaces for diagnostics.
1 parent 9051c05 commit 7977cb4

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,9 +956,42 @@ impl<'a> Resolver<'a> {
956956
if macro_kind == MacroKind::Derive && (ident.name == sym::Send || ident.name == sym::Sync) {
957957
let msg = format!("unsafe traits like `{}` should be implemented explicitly", ident);
958958
err.span_note(ident.span, &msg);
959+
return;
959960
}
960961
if self.macro_names.contains(&ident.normalize_to_macros_2_0()) {
961962
err.help("have you added the `#[macro_use]` on the module/import?");
963+
return;
964+
}
965+
for ns in [Namespace::MacroNS, Namespace::TypeNS, Namespace::ValueNS] {
966+
if let Ok(binding) = self.early_resolve_ident_in_lexical_scope(
967+
ident,
968+
ScopeSet::All(ns, false),
969+
&parent_scope,
970+
false,
971+
false,
972+
ident.span,
973+
) {
974+
let it_is = match binding.macro_kind() {
975+
Some(MacroKind::Bang) => "it is a function-like macro".to_string(),
976+
Some(kind) => format!("it is {} {}", kind.article(), kind.descr_expected()),
977+
None => format!(
978+
"it is not {} {}",
979+
macro_kind.article(),
980+
macro_kind.descr_expected()
981+
),
982+
};
983+
if let crate::NameBindingKind::Import { import, .. } = binding.kind {
984+
if !import.span.is_dummy() {
985+
err.span_note(
986+
import.span,
987+
&format!("`{}` is imported here, but {}", ident, it_is),
988+
);
989+
return;
990+
}
991+
}
992+
err.note(&format!("`{}` is in scope, but {}", ident, it_is));
993+
return;
994+
}
962995
}
963996
}
964997

compiler/rustc_resolve/src/macros.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind};
1919
use rustc_expand::compile_declarative_macro;
2020
use rustc_expand::expand::{AstFragment, Invocation, InvocationKind, SupportsMacroExpansion};
2121
use rustc_feature::is_builtin_attr_name;
22-
use rustc_hir::def::{self, DefKind, Namespace, NonMacroAttrKind};
22+
use rustc_hir::def::{self, DefKind, NonMacroAttrKind};
2323
use rustc_hir::def_id::{CrateNum, LocalDefId};
2424
use rustc_hir::PrimTy;
2525
use rustc_middle::middle::stability;
@@ -1115,24 +1115,6 @@ impl<'a> Resolver<'a> {
11151115
let msg = format!("cannot find {} `{}` in this scope", expected, ident);
11161116
let mut err = self.session.struct_span_err(ident.span, &msg);
11171117
self.unresolved_macro_suggestions(&mut err, kind, &parent_scope, ident);
1118-
if let Ok(binding) = self.early_resolve_ident_in_lexical_scope(
1119-
ident,
1120-
ScopeSet::All(Namespace::TypeNS, false),
1121-
&parent_scope,
1122-
false,
1123-
false,
1124-
ident.span,
1125-
) {
1126-
if let crate::NameBindingKind::Import { import, .. } = binding.kind {
1127-
err.span_note(
1128-
import.span,
1129-
&format!(
1130-
"`{}` is imported here, but it is not a {}",
1131-
ident, expected
1132-
),
1133-
);
1134-
}
1135-
}
11361118
err.emit();
11371119
}
11381120
}

0 commit comments

Comments
 (0)