Skip to content

Commit 5bd8ba8

Browse files
committed
Make "consider importing" consistent for macros
1 parent 8b35c0b commit 5bd8ba8

10 files changed

+53
-27
lines changed

Diff for: compiler/rustc_resolve/src/diagnostics.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1352,6 +1352,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13521352
macro_kind: MacroKind,
13531353
parent_scope: &ParentScope<'a>,
13541354
ident: Ident,
1355+
krate: &Crate,
13551356
) {
13561357
let is_expected = &|res: Res| res.macro_kind() == Some(macro_kind);
13571358
let suggestion = self.early_lookup_typo_candidate(
@@ -1364,13 +1365,17 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13641365

13651366
let import_suggestions =
13661367
self.lookup_import_candidates(ident, Namespace::MacroNS, parent_scope, is_expected);
1368+
let (span, found_use) = match parent_scope.module.nearest_parent_mod().as_local() {
1369+
Some(def_id) => UsePlacementFinder::check(krate, self.def_id_to_node_id[def_id]),
1370+
None => (None, FoundUse::No),
1371+
};
13671372
show_candidates(
13681373
self.tcx,
13691374
err,
1370-
None,
1375+
span,
13711376
&import_suggestions,
13721377
Instead::No,
1373-
FoundUse::Yes,
1378+
found_use,
13741379
DiagnosticMode::Normal,
13751380
vec![],
13761381
"",

Diff for: compiler/rustc_resolve/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1522,7 +1522,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15221522
self.tcx.sess.time("check_hidden_glob_reexports", || {
15231523
self.check_hidden_glob_reexports(exported_ambiguities)
15241524
});
1525-
self.tcx.sess.time("finalize_macro_resolutions", || self.finalize_macro_resolutions());
1525+
self.tcx
1526+
.sess
1527+
.time("finalize_macro_resolutions", || self.finalize_macro_resolutions(krate));
15261528
self.tcx.sess.time("late_resolve_crate", || self.late_resolve_crate(krate));
15271529
self.tcx.sess.time("resolve_main", || self.resolve_main());
15281530
self.tcx.sess.time("resolve_check_unused", || self.check_unused(krate));

Diff for: compiler/rustc_resolve/src/macros.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{BuiltinMacroState, Determinacy};
77
use crate::{DeriveData, Finalize, ParentScope, ResolutionError, Resolver, ScopeSet};
88
use crate::{ModuleKind, ModuleOrUniformRoot, NameBinding, PathResult, Segment};
99
use rustc_ast::expand::StrippedCfgItem;
10-
use rustc_ast::{self as ast, attr, Inline, ItemKind, ModKind, NodeId};
10+
use rustc_ast::{self as ast, attr, Crate, Inline, ItemKind, ModKind, NodeId};
1111
use rustc_ast_pretty::pprust;
1212
use rustc_attr::StabilityLevel;
1313
use rustc_data_structures::intern::Interned;
@@ -674,7 +674,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
674674
res.map(|res| (self.get_macro(res).map(|macro_data| macro_data.ext), res))
675675
}
676676

677-
pub(crate) fn finalize_macro_resolutions(&mut self) {
677+
pub(crate) fn finalize_macro_resolutions(&mut self, krate: &Crate) {
678678
let check_consistency = |this: &mut Self,
679679
path: &[Segment],
680680
span,
@@ -795,7 +795,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
795795
let expected = kind.descr_expected();
796796
let msg = format!("cannot find {} `{}` in this scope", expected, ident);
797797
let mut err = self.tcx.sess.struct_span_err(ident.span, msg);
798-
self.unresolved_macro_suggestions(&mut err, kind, &parent_scope, ident);
798+
self.unresolved_macro_suggestions(&mut err, kind, &parent_scope, ident, krate);
799799
err.emit();
800800
}
801801
}

Diff for: tests/ui/empty/empty-macro-use.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ error: cannot find macro `macro_two` in this scope
44
LL | macro_two!();
55
| ^^^^^^^^^
66
|
7-
= help: consider importing this macro:
8-
two_macros::macro_two
7+
help: consider importing this macro
8+
|
9+
LL + use two_macros::macro_two;
10+
|
911

1012
error: aborting due to previous error
1113

Diff for: tests/ui/hygiene/no_implicit_prelude-2018.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ error: cannot find macro `print` in this scope
44
LL | print!();
55
| ^^^^^
66
|
7-
= help: consider importing this macro:
8-
std::print
7+
help: consider importing this macro
8+
|
9+
LL + use std::print;
10+
|
911

1012
error: aborting due to previous error
1113

Diff for: tests/ui/macros/issue-88228.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// compile-flags: -Z deduplicate-diagnostics=yes
22
// edition:2018
33

4-
mod hey {
4+
mod hey { //~ HELP consider importing this derive macro
5+
//~^ HELP consider importing this macro
56
pub use Copy as Bla;
67
pub use std::println as bla;
78
}
89

910
#[derive(Bla)]
1011
//~^ ERROR cannot find derive macro `Bla`
11-
//~| HELP consider importing this derive macro
1212
struct A;
1313

1414
#[derive(println)]
@@ -19,5 +19,4 @@ struct B;
1919
fn main() {
2020
bla!();
2121
//~^ ERROR cannot find macro `bla`
22-
//~| HELP consider importing this macro
2322
}

Diff for: tests/ui/macros/issue-88228.stderr

+9-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ error: cannot find macro `bla` in this scope
44
LL | bla!();
55
| ^^^
66
|
7-
= help: consider importing this macro:
8-
crate::hey::bla
7+
help: consider importing this macro
8+
|
9+
LL + use crate::hey::bla;
10+
|
911

1012
error: cannot find derive macro `println` in this scope
1113
--> $DIR/issue-88228.rs:14:10
@@ -16,13 +18,15 @@ LL | #[derive(println)]
1618
= note: `println` is in scope, but it is a function-like macro
1719

1820
error: cannot find derive macro `Bla` in this scope
19-
--> $DIR/issue-88228.rs:9:10
21+
--> $DIR/issue-88228.rs:10:10
2022
|
2123
LL | #[derive(Bla)]
2224
| ^^^
2325
|
24-
= help: consider importing this derive macro:
25-
crate::hey::Bla
26+
help: consider importing this derive macro
27+
|
28+
LL + use crate::hey::Bla;
29+
|
2630

2731
error: aborting due to 3 previous errors
2832

Diff for: tests/ui/macros/macro-use-wrong-name.stderr

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@ error: cannot find macro `macro_two` in this scope
22
--> $DIR/macro-use-wrong-name.rs:7:5
33
|
44
LL | macro_two!();
5-
| ^^^^^^^^^ help: a macro with a similar name exists: `macro_one`
5+
| ^^^^^^^^^
66
|
77
::: $DIR/auxiliary/two_macros.rs:2:1
88
|
99
LL | macro_rules! macro_one { () => ("one") }
1010
| ---------------------- similarly named macro `macro_one` defined here
1111
|
12-
= help: consider importing this macro:
13-
two_macros::macro_two
12+
help: a macro with a similar name exists
13+
|
14+
LL | macro_one!();
15+
| ~~~~~~~~~
16+
help: consider importing this macro
17+
|
18+
LL + use two_macros::macro_two;
19+
|
1420

1521
error: aborting due to previous error
1622

Diff for: tests/ui/missing/missing-macro-use.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ error: cannot find macro `macro_two` in this scope
44
LL | macro_two!();
55
| ^^^^^^^^^
66
|
7-
= help: consider importing this macro:
8-
two_macros::macro_two
7+
help: consider importing this macro
8+
|
9+
LL + use two_macros::macro_two;
10+
|
911

1012
error: aborting due to previous error
1113

Diff for: tests/ui/proc-macro/derive-helper-shadowing.stderr

+8-4
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ error: cannot find attribute `empty_helper` in this scope
1616
LL | #[derive(GenHelperUse)]
1717
| ^^^^^^^^^^^^
1818
|
19-
= help: consider importing this attribute macro:
20-
empty_helper
2119
= note: this error originates in the derive macro `GenHelperUse` (in Nightly builds, run with -Z macro-backtrace for more info)
20+
help: consider importing this attribute macro
21+
|
22+
LL + use empty_helper;
23+
|
2224

2325
error: cannot find attribute `empty_helper` in this scope
2426
--> $DIR/derive-helper-shadowing.rs:14:11
@@ -29,9 +31,11 @@ LL | #[empty_helper]
2931
LL | gen_helper_use!();
3032
| ----------------- in this macro invocation
3133
|
32-
= help: consider importing this attribute macro:
33-
crate::empty_helper
3434
= note: this error originates in the macro `gen_helper_use` (in Nightly builds, run with -Z macro-backtrace for more info)
35+
help: consider importing this attribute macro
36+
|
37+
LL + use crate::empty_helper;
38+
|
3539

3640
error[E0659]: `empty_helper` is ambiguous
3741
--> $DIR/derive-helper-shadowing.rs:26:13

0 commit comments

Comments
 (0)