Skip to content

Commit 422da40

Browse files
authored
Rollup merge of #126371 - estebank:suggest-core, r=fmease
Tweak output of import suggestions When both `std::` and `core::` items are available, only suggest the `std::` ones. We ensure that in `no_std` crates we suggest `core::` items. Ensure that the list of items suggested to be imported are always in the order of local crate items, `std`/`core` items and finally foreign crate items. Tweak wording of import suggestion: if there are multiple items but they are all of the same kind, we use the kind name and not the generic "items". Fix #83564.
2 parents 4dd8813 + 5de8e6e commit 422da40

30 files changed

+159
-80
lines changed

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2783,33 +2783,65 @@ fn show_candidates(
27832783
// by iterating through a hash map, so make sure they are ordered:
27842784
for path_strings in [&mut accessible_path_strings, &mut inaccessible_path_strings] {
27852785
path_strings.sort_by(|a, b| a.0.cmp(&b.0));
2786+
path_strings.dedup_by(|a, b| a.0 == b.0);
27862787
let core_path_strings =
27872788
path_strings.extract_if(|p| p.0.starts_with("core::")).collect::<Vec<_>>();
2788-
path_strings.extend(core_path_strings);
2789-
path_strings.dedup_by(|a, b| a.0 == b.0);
2789+
let std_path_strings =
2790+
path_strings.extract_if(|p| p.0.starts_with("std::")).collect::<Vec<_>>();
2791+
let foreign_crate_path_strings =
2792+
path_strings.extract_if(|p| !p.0.starts_with("crate::")).collect::<Vec<_>>();
2793+
2794+
// We list the `crate` local paths first.
2795+
// Then we list the `std`/`core` paths.
2796+
if std_path_strings.len() == core_path_strings.len() {
2797+
// Do not list `core::` paths if we are already listing the `std::` ones.
2798+
path_strings.extend(std_path_strings);
2799+
} else {
2800+
path_strings.extend(std_path_strings);
2801+
path_strings.extend(core_path_strings);
2802+
}
2803+
// List all paths from foreign crates last.
2804+
path_strings.extend(foreign_crate_path_strings);
27902805
}
2791-
accessible_path_strings.sort();
27922806

27932807
if !accessible_path_strings.is_empty() {
2794-
let (determiner, kind, name, through) =
2808+
let (determiner, kind, s, name, through) =
27952809
if let [(name, descr, _, _, via_import)] = &accessible_path_strings[..] {
27962810
(
27972811
"this",
27982812
*descr,
2813+
"",
27992814
format!(" `{name}`"),
28002815
if *via_import { " through its public re-export" } else { "" },
28012816
)
28022817
} else {
2803-
("one of these", "items", String::new(), "")
2818+
// Get the unique item kinds and if there's only one, we use the right kind name
2819+
// instead of the more generic "items".
2820+
let mut kinds = accessible_path_strings
2821+
.iter()
2822+
.map(|(_, descr, _, _, _)| *descr)
2823+
.collect::<FxHashSet<&str>>()
2824+
.into_iter();
2825+
let kind = if let Some(kind) = kinds.next()
2826+
&& let None = kinds.next()
2827+
{
2828+
kind
2829+
} else {
2830+
"item"
2831+
};
2832+
let s = if kind.ends_with('s') { "es" } else { "s" };
2833+
2834+
("one of these", kind, s, String::new(), "")
28042835
};
28052836

28062837
let instead = if let Instead::Yes = instead { " instead" } else { "" };
28072838
let mut msg = if let DiagMode::Pattern = mode {
28082839
format!(
2809-
"if you meant to match on {kind}{instead}{name}, use the full path in the pattern",
2840+
"if you meant to match on {kind}{s}{instead}{name}, use the full path in the \
2841+
pattern",
28102842
)
28112843
} else {
2812-
format!("consider importing {determiner} {kind}{through}{instead}")
2844+
format!("consider importing {determiner} {kind}{s}{through}{instead}")
28132845
};
28142846

28152847
for note in accessible_path_strings.iter().flat_map(|cand| cand.3.as_ref()) {

src/tools/clippy/tests/ui/crashes/ice-6252.stderr

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ error[E0412]: cannot find type `PhantomData` in this scope
44
LL | _n: PhantomData,
55
| ^^^^^^^^^^^ not found in this scope
66
|
7-
help: consider importing one of these items
8-
|
9-
LL + use core::marker::PhantomData;
7+
help: consider importing this struct
108
|
119
LL + use std::marker::PhantomData;
1210
|

tests/ui/const-generics/issues/issue-82956.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0433]: failed to resolve: use of undeclared type `IntoIter`
44
LL | let mut iter = IntoIter::new(self);
55
| ^^^^^^^^ use of undeclared type `IntoIter`
66
|
7-
help: consider importing one of these items
7+
help: consider importing one of these structs
88
|
99
LL + use std::array::IntoIter;
1010
|

tests/ui/consts/const_refs_to_static-ice-121413.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
// ignore-tidy-linelength
66
#![feature(const_refs_to_static)]
77
const REF_INTERIOR_MUT: &usize = {
8+
//~^ HELP consider importing this struct
89
static FOO: Sync = AtomicUsize::new(0);
910
//~^ ERROR failed to resolve: use of undeclared type `AtomicUsize`
1011
//~| WARN trait objects without an explicit `dyn` are deprecated
1112
//~| ERROR the size for values of type `(dyn Sync + 'static)` cannot be known at compilation time
1213
//~| ERROR the size for values of type `(dyn Sync + 'static)` cannot be known at compilation time
1314
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
15+
//~| HELP if this is an object-safe trait, use `dyn`
16+
//~| HELP the trait `Sized` is not implemented for `(dyn Sync + 'static)`
17+
//~| HELP the trait `Sized` is not implemented for `(dyn Sync + 'static)`
1418
unsafe { &*(&FOO as *const _ as *const usize) }
1519
};
1620
pub fn main() {}

tests/ui/consts/const_refs_to_static-ice-121413.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0433]: failed to resolve: use of undeclared type `AtomicUsize`
2-
--> $DIR/const_refs_to_static-ice-121413.rs:8:24
2+
--> $DIR/const_refs_to_static-ice-121413.rs:9:24
33
|
44
LL | static FOO: Sync = AtomicUsize::new(0);
55
| ^^^^^^^^^^^ use of undeclared type `AtomicUsize`
@@ -10,7 +10,7 @@ LL + use std::sync::atomic::AtomicUsize;
1010
|
1111

1212
warning: trait objects without an explicit `dyn` are deprecated
13-
--> $DIR/const_refs_to_static-ice-121413.rs:8:17
13+
--> $DIR/const_refs_to_static-ice-121413.rs:9:17
1414
|
1515
LL | static FOO: Sync = AtomicUsize::new(0);
1616
| ^^^^
@@ -24,15 +24,15 @@ LL | static FOO: dyn Sync = AtomicUsize::new(0);
2424
| +++
2525

2626
error[E0277]: the size for values of type `(dyn Sync + 'static)` cannot be known at compilation time
27-
--> $DIR/const_refs_to_static-ice-121413.rs:8:17
27+
--> $DIR/const_refs_to_static-ice-121413.rs:9:17
2828
|
2929
LL | static FOO: Sync = AtomicUsize::new(0);
3030
| ^^^^ doesn't have a size known at compile-time
3131
|
3232
= help: the trait `Sized` is not implemented for `(dyn Sync + 'static)`
3333

3434
error[E0277]: the size for values of type `(dyn Sync + 'static)` cannot be known at compilation time
35-
--> $DIR/const_refs_to_static-ice-121413.rs:8:24
35+
--> $DIR/const_refs_to_static-ice-121413.rs:9:24
3636
|
3737
LL | static FOO: Sync = AtomicUsize::new(0);
3838
| ^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time

tests/ui/did_you_mean/issue-56028-there-is-an-enum-variant.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ error[E0425]: cannot find value `Set` in this scope
2222
LL | fn setup() -> Set { Set }
2323
| ^^^ not found in this scope
2424
|
25-
help: consider importing one of these items
25+
help: consider importing one of these unit variants
2626
|
2727
LL + use AffixHeart::Set;
2828
|

tests/ui/hygiene/extern-prelude-from-opaque-fail-2018.stderr

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ LL | fn f() { my_core::mem::drop(0); }
2424
LL | a!();
2525
| ---- in this macro invocation
2626
|
27-
= help: consider importing one of these items:
28-
core::mem
27+
= help: consider importing this module:
2928
std::mem
3029
= note: this error originates in the macro `a` (in Nightly builds, run with -Z macro-backtrace for more info)
3130

@@ -35,9 +34,7 @@ error[E0433]: failed to resolve: use of undeclared crate or module `my_core`
3534
LL | fn f() { my_core::mem::drop(0); }
3635
| ^^^^^^^ use of undeclared crate or module `my_core`
3736
|
38-
help: consider importing one of these items
39-
|
40-
LL + use core::mem;
37+
help: consider importing this module
4138
|
4239
LL + use std::mem;
4340
|

tests/ui/imports/cycle-import-in-std-1.stderr

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ error[E0432]: unresolved import `ops`
44
LL | use ops::{self as std};
55
| ^^^^^^^^^^^ no external crate `ops`
66
|
7-
= help: consider importing one of these items instead:
8-
core::ops
7+
= help: consider importing this module instead:
98
std::ops
109

1110
error: aborting due to 1 previous error

tests/ui/imports/cycle-import-in-std-2.stderr

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ error[E0432]: unresolved import `ops`
44
LL | use ops::{self as std};
55
| ^^^^^^^^^^^ no external crate `ops`
66
|
7-
= help: consider importing one of these items instead:
8-
core::ops
7+
= help: consider importing this module instead:
98
std::ops
109

1110
error: aborting due to 1 previous error

tests/ui/imports/import-alias-issue-121168.edition2018.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0412]: cannot find type `Foo` in this scope
44
LL | let _: Foo<i32> = todo!();
55
| ^^^ not found in this scope
66
|
7-
help: consider importing one of these items
7+
help: consider importing one of these structs
88
|
99
LL + use crate::nice_crate_name::Foo;
1010
|

0 commit comments

Comments
 (0)