From 5bd8ba84931628fc2fa3e1c75cc9b9762c449125 Mon Sep 17 00:00:00 2001 From: Mu001999 Date: Sat, 10 Jun 2023 00:06:34 +0800 Subject: [PATCH] Make "consider importing" consistent for macros --- compiler/rustc_resolve/src/diagnostics.rs | 9 +++++++-- compiler/rustc_resolve/src/lib.rs | 4 +++- compiler/rustc_resolve/src/macros.rs | 6 +++--- tests/ui/empty/empty-macro-use.stderr | 6 ++++-- tests/ui/hygiene/no_implicit_prelude-2018.stderr | 6 ++++-- tests/ui/macros/issue-88228.rs | 5 ++--- tests/ui/macros/issue-88228.stderr | 14 +++++++++----- tests/ui/macros/macro-use-wrong-name.stderr | 12 +++++++++--- tests/ui/missing/missing-macro-use.stderr | 6 ++++-- tests/ui/proc-macro/derive-helper-shadowing.stderr | 12 ++++++++---- 10 files changed, 53 insertions(+), 27 deletions(-) diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 15c8a690530e7..ca811c9ed7d98 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -1352,6 +1352,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { macro_kind: MacroKind, parent_scope: &ParentScope<'a>, ident: Ident, + krate: &Crate, ) { let is_expected = &|res: Res| res.macro_kind() == Some(macro_kind); let suggestion = self.early_lookup_typo_candidate( @@ -1364,13 +1365,17 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { let import_suggestions = self.lookup_import_candidates(ident, Namespace::MacroNS, parent_scope, is_expected); + let (span, found_use) = match parent_scope.module.nearest_parent_mod().as_local() { + Some(def_id) => UsePlacementFinder::check(krate, self.def_id_to_node_id[def_id]), + None => (None, FoundUse::No), + }; show_candidates( self.tcx, err, - None, + span, &import_suggestions, Instead::No, - FoundUse::Yes, + found_use, DiagnosticMode::Normal, vec![], "", diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index dd8d01e35e5de..db97039b907c4 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1522,7 +1522,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { self.tcx.sess.time("check_hidden_glob_reexports", || { self.check_hidden_glob_reexports(exported_ambiguities) }); - self.tcx.sess.time("finalize_macro_resolutions", || self.finalize_macro_resolutions()); + self.tcx + .sess + .time("finalize_macro_resolutions", || self.finalize_macro_resolutions(krate)); self.tcx.sess.time("late_resolve_crate", || self.late_resolve_crate(krate)); self.tcx.sess.time("resolve_main", || self.resolve_main()); self.tcx.sess.time("resolve_check_unused", || self.check_unused(krate)); diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index 805c804e5759f..ca4f3331b9a2d 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -7,7 +7,7 @@ use crate::{BuiltinMacroState, Determinacy}; use crate::{DeriveData, Finalize, ParentScope, ResolutionError, Resolver, ScopeSet}; use crate::{ModuleKind, ModuleOrUniformRoot, NameBinding, PathResult, Segment}; use rustc_ast::expand::StrippedCfgItem; -use rustc_ast::{self as ast, attr, Inline, ItemKind, ModKind, NodeId}; +use rustc_ast::{self as ast, attr, Crate, Inline, ItemKind, ModKind, NodeId}; use rustc_ast_pretty::pprust; use rustc_attr::StabilityLevel; use rustc_data_structures::intern::Interned; @@ -674,7 +674,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { res.map(|res| (self.get_macro(res).map(|macro_data| macro_data.ext), res)) } - pub(crate) fn finalize_macro_resolutions(&mut self) { + pub(crate) fn finalize_macro_resolutions(&mut self, krate: &Crate) { let check_consistency = |this: &mut Self, path: &[Segment], span, @@ -795,7 +795,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { let expected = kind.descr_expected(); let msg = format!("cannot find {} `{}` in this scope", expected, ident); let mut err = self.tcx.sess.struct_span_err(ident.span, msg); - self.unresolved_macro_suggestions(&mut err, kind, &parent_scope, ident); + self.unresolved_macro_suggestions(&mut err, kind, &parent_scope, ident, krate); err.emit(); } } diff --git a/tests/ui/empty/empty-macro-use.stderr b/tests/ui/empty/empty-macro-use.stderr index e0b3b8685d6eb..5d552e4c40846 100644 --- a/tests/ui/empty/empty-macro-use.stderr +++ b/tests/ui/empty/empty-macro-use.stderr @@ -4,8 +4,10 @@ error: cannot find macro `macro_two` in this scope LL | macro_two!(); | ^^^^^^^^^ | - = help: consider importing this macro: - two_macros::macro_two +help: consider importing this macro + | +LL + use two_macros::macro_two; + | error: aborting due to previous error diff --git a/tests/ui/hygiene/no_implicit_prelude-2018.stderr b/tests/ui/hygiene/no_implicit_prelude-2018.stderr index 3f31b041b6203..b22f3e75b6f4f 100644 --- a/tests/ui/hygiene/no_implicit_prelude-2018.stderr +++ b/tests/ui/hygiene/no_implicit_prelude-2018.stderr @@ -4,8 +4,10 @@ error: cannot find macro `print` in this scope LL | print!(); | ^^^^^ | - = help: consider importing this macro: - std::print +help: consider importing this macro + | +LL + use std::print; + | error: aborting due to previous error diff --git a/tests/ui/macros/issue-88228.rs b/tests/ui/macros/issue-88228.rs index 60ba2eab7a7bf..ec55a262509aa 100644 --- a/tests/ui/macros/issue-88228.rs +++ b/tests/ui/macros/issue-88228.rs @@ -1,14 +1,14 @@ // compile-flags: -Z deduplicate-diagnostics=yes // edition:2018 -mod hey { +mod hey { //~ HELP consider importing this derive macro + //~^ HELP consider importing this macro pub use Copy as Bla; pub use std::println as bla; } #[derive(Bla)] //~^ ERROR cannot find derive macro `Bla` -//~| HELP consider importing this derive macro struct A; #[derive(println)] @@ -19,5 +19,4 @@ struct B; fn main() { bla!(); //~^ ERROR cannot find macro `bla` - //~| HELP consider importing this macro } diff --git a/tests/ui/macros/issue-88228.stderr b/tests/ui/macros/issue-88228.stderr index fe8a1deaedd77..1dbe2b77be2d9 100644 --- a/tests/ui/macros/issue-88228.stderr +++ b/tests/ui/macros/issue-88228.stderr @@ -4,8 +4,10 @@ error: cannot find macro `bla` in this scope LL | bla!(); | ^^^ | - = help: consider importing this macro: - crate::hey::bla +help: consider importing this macro + | +LL + use crate::hey::bla; + | error: cannot find derive macro `println` in this scope --> $DIR/issue-88228.rs:14:10 @@ -16,13 +18,15 @@ LL | #[derive(println)] = note: `println` is in scope, but it is a function-like macro error: cannot find derive macro `Bla` in this scope - --> $DIR/issue-88228.rs:9:10 + --> $DIR/issue-88228.rs:10:10 | LL | #[derive(Bla)] | ^^^ | - = help: consider importing this derive macro: - crate::hey::Bla +help: consider importing this derive macro + | +LL + use crate::hey::Bla; + | error: aborting due to 3 previous errors diff --git a/tests/ui/macros/macro-use-wrong-name.stderr b/tests/ui/macros/macro-use-wrong-name.stderr index ca5f0f190e8ba..36339542ac61f 100644 --- a/tests/ui/macros/macro-use-wrong-name.stderr +++ b/tests/ui/macros/macro-use-wrong-name.stderr @@ -2,15 +2,21 @@ error: cannot find macro `macro_two` in this scope --> $DIR/macro-use-wrong-name.rs:7:5 | LL | macro_two!(); - | ^^^^^^^^^ help: a macro with a similar name exists: `macro_one` + | ^^^^^^^^^ | ::: $DIR/auxiliary/two_macros.rs:2:1 | LL | macro_rules! macro_one { () => ("one") } | ---------------------- similarly named macro `macro_one` defined here | - = help: consider importing this macro: - two_macros::macro_two +help: a macro with a similar name exists + | +LL | macro_one!(); + | ~~~~~~~~~ +help: consider importing this macro + | +LL + use two_macros::macro_two; + | error: aborting due to previous error diff --git a/tests/ui/missing/missing-macro-use.stderr b/tests/ui/missing/missing-macro-use.stderr index 99e291cda0377..e1d80f52daddc 100644 --- a/tests/ui/missing/missing-macro-use.stderr +++ b/tests/ui/missing/missing-macro-use.stderr @@ -4,8 +4,10 @@ error: cannot find macro `macro_two` in this scope LL | macro_two!(); | ^^^^^^^^^ | - = help: consider importing this macro: - two_macros::macro_two +help: consider importing this macro + | +LL + use two_macros::macro_two; + | error: aborting due to previous error diff --git a/tests/ui/proc-macro/derive-helper-shadowing.stderr b/tests/ui/proc-macro/derive-helper-shadowing.stderr index de2c27a878c67..7e7870b295127 100644 --- a/tests/ui/proc-macro/derive-helper-shadowing.stderr +++ b/tests/ui/proc-macro/derive-helper-shadowing.stderr @@ -16,9 +16,11 @@ error: cannot find attribute `empty_helper` in this scope LL | #[derive(GenHelperUse)] | ^^^^^^^^^^^^ | - = help: consider importing this attribute macro: - empty_helper = note: this error originates in the derive macro `GenHelperUse` (in Nightly builds, run with -Z macro-backtrace for more info) +help: consider importing this attribute macro + | +LL + use empty_helper; + | error: cannot find attribute `empty_helper` in this scope --> $DIR/derive-helper-shadowing.rs:14:11 @@ -29,9 +31,11 @@ LL | #[empty_helper] LL | gen_helper_use!(); | ----------------- in this macro invocation | - = help: consider importing this attribute macro: - crate::empty_helper = note: this error originates in the macro `gen_helper_use` (in Nightly builds, run with -Z macro-backtrace for more info) +help: consider importing this attribute macro + | +LL + use crate::empty_helper; + | error[E0659]: `empty_helper` is ambiguous --> $DIR/derive-helper-shadowing.rs:26:13