Skip to content

Commit 958e645

Browse files
committed
Adds hint if a trait fails to resolve and a newly added one in Edition 2021 is suggested
1 parent e91d5ca commit 958e645

File tree

4 files changed

+134
-15
lines changed

4 files changed

+134
-15
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+50-7
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ crate struct ImportSuggestion {
6666
pub descr: &'static str,
6767
pub path: Path,
6868
pub accessible: bool,
69+
/// An extra note that should be issued if this item is suggested
70+
pub note: Option<String>,
6971
}
7072

7173
/// Adjust the impl span so that just the `impl` keyword is taken by removing
@@ -872,11 +874,38 @@ impl<'a> Resolver<'a> {
872874
}
873875

874876
if candidates.iter().all(|v: &ImportSuggestion| v.did != did) {
877+
// See if we're recommending TryFrom, TryInto, or FromIterator and add
878+
// a note about editions
879+
let note = if let Some(did) = did {
880+
let requires_note = !did.is_local()
881+
&& this.cstore().item_attrs(did, this.session).iter().any(
882+
|attr| {
883+
if attr.has_name(sym::rustc_diagnostic_item) {
884+
[sym::TryInto, sym::TryFrom, sym::FromIterator]
885+
.map(|x| Some(x))
886+
.contains(&attr.value_str())
887+
} else {
888+
false
889+
}
890+
},
891+
);
892+
893+
requires_note.then(|| {
894+
format!(
895+
"'{}' is included in the prelude starting in Edition 2021",
896+
path_names_to_string(&path)
897+
)
898+
})
899+
} else {
900+
None
901+
};
902+
875903
candidates.push(ImportSuggestion {
876904
did,
877905
descr: res.descr(),
878906
path,
879907
accessible: child_accessible,
908+
note,
880909
});
881910
}
882911
}
@@ -1764,12 +1793,14 @@ crate fn show_candidates(
17641793
return;
17651794
}
17661795

1767-
let mut accessible_path_strings: Vec<(String, &str, Option<DefId>)> = Vec::new();
1768-
let mut inaccessible_path_strings: Vec<(String, &str, Option<DefId>)> = Vec::new();
1796+
let mut accessible_path_strings: Vec<(String, &str, Option<DefId>, &Option<String>)> =
1797+
Vec::new();
1798+
let mut inaccessible_path_strings: Vec<(String, &str, Option<DefId>, &Option<String>)> =
1799+
Vec::new();
17691800

17701801
candidates.iter().for_each(|c| {
17711802
(if c.accessible { &mut accessible_path_strings } else { &mut inaccessible_path_strings })
1772-
.push((path_names_to_string(&c.path), c.descr, c.did))
1803+
.push((path_names_to_string(&c.path), c.descr, c.did, &c.note))
17731804
});
17741805

17751806
// we want consistent results across executions, but candidates are produced
@@ -1792,6 +1823,11 @@ crate fn show_candidates(
17921823
let instead = if instead { " instead" } else { "" };
17931824
let mut msg = format!("consider importing {} {}{}", determiner, kind, instead);
17941825

1826+
// Issue notes
1827+
for note in accessible_path_strings.iter().map(|cand| cand.3.as_ref()).flatten() {
1828+
err.note(note);
1829+
}
1830+
17951831
if let Some(span) = use_placement_span {
17961832
for candidate in &mut accessible_path_strings {
17971833
// produce an additional newline to separate the new use statement
@@ -1820,7 +1856,7 @@ crate fn show_candidates(
18201856
assert!(!inaccessible_path_strings.is_empty());
18211857

18221858
if inaccessible_path_strings.len() == 1 {
1823-
let (name, descr, def_id) = &inaccessible_path_strings[0];
1859+
let (name, descr, def_id, note) = &inaccessible_path_strings[0];
18241860
let msg = format!("{} `{}` exists but is inaccessible", descr, name);
18251861

18261862
if let Some(local_def_id) = def_id.and_then(|did| did.as_local()) {
@@ -1832,12 +1868,15 @@ crate fn show_candidates(
18321868
} else {
18331869
err.note(&msg);
18341870
}
1871+
if let Some(note) = (*note).as_deref() {
1872+
err.note(note);
1873+
}
18351874
} else {
1836-
let (_, descr_first, _) = &inaccessible_path_strings[0];
1875+
let (_, descr_first, _, _) = &inaccessible_path_strings[0];
18371876
let descr = if inaccessible_path_strings
18381877
.iter()
18391878
.skip(1)
1840-
.all(|(_, descr, _)| descr == descr_first)
1879+
.all(|(_, descr, _, _)| descr == descr_first)
18411880
{
18421881
descr_first.to_string()
18431882
} else {
@@ -1848,7 +1887,7 @@ crate fn show_candidates(
18481887
let mut has_colon = false;
18491888

18501889
let mut spans = Vec::new();
1851-
for (name, _, def_id) in &inaccessible_path_strings {
1890+
for (name, _, def_id, _) in &inaccessible_path_strings {
18521891
if let Some(local_def_id) = def_id.and_then(|did| did.as_local()) {
18531892
let span = definitions.def_span(local_def_id);
18541893
let span = session.source_map().guess_head_span(span);
@@ -1868,6 +1907,10 @@ crate fn show_candidates(
18681907
multi_span.push_span_label(span, format!("`{}`: not accessible", name));
18691908
}
18701909

1910+
for note in inaccessible_path_strings.iter().map(|cand| cand.3.as_ref()).flatten() {
1911+
err.note(note);
1912+
}
1913+
18711914
err.span_note(multi_span, &msg);
18721915
}
18731916
}

compiler/rustc_resolve/src/late/diagnostics.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1502,6 +1502,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
15021502
descr: "module",
15031503
path,
15041504
accessible: true,
1505+
note: None,
15051506
},
15061507
));
15071508
} else {
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
1-
// Make sure that calling `.try_into()` in pre-2021 mentions Edition 2021 change
1+
// Make sure that trying to access `TryInto`, `TryFrom`, `FromIterator` in pre-2021 mentions
2+
// Edition 2021 change
23
// edition:2018
34

45
fn test() {
5-
let i: i16 = 0_i32.try_into().unwrap();
6+
let _i: i16 = 0_i32.try_into().unwrap();
67
//~^ ERROR no method named `try_into` found for type `i32` in the current scope
78
//~| NOTE method not found in `i32`
89
//~| NOTE 'std::convert::TryInto' is included in the prelude starting in Edition 2021
10+
11+
let _i: i16 = TryFrom::try_from(0_i32).unwrap();
12+
//~^ ERROR failed to resolve: use of undeclared type
13+
//~| NOTE not found in this scope
14+
//~| NOTE 'std::convert::TryFrom' is included in the prelude starting in Edition 2021
15+
//~| NOTE 'core::convert::TryFrom' is included in the prelude starting in Edition 2021
16+
17+
let _i: i16 = TryInto::try_into(0_i32).unwrap();
18+
//~^ ERROR failed to resolve: use of undeclared type
19+
//~| NOTE not found in this scope
20+
//~| NOTE 'std::convert::TryInto' is included in the prelude starting in Edition 2021
21+
//~| NOTE 'core::convert::TryInto' is included in the prelude starting in Edition 2021
22+
23+
let _v: Vec<_> = FromIterator::from_iter(&[1]);
24+
//~^ ERROR failed to resolve: use of undeclared type
25+
//~| NOTE 'std::iter::FromIterator' is included in the prelude starting in Edition 2021
26+
//~| NOTE 'core::iter::FromIterator' is included in the prelude starting in Edition 2021
927
}
1028

11-
fn main() {}
29+
fn main() {
30+
test();
31+
}

src/test/ui/suggestions/suggest-tryinto-edition-change.stderr

+60-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,62 @@
1+
error[E0433]: failed to resolve: use of undeclared type `TryFrom`
2+
--> $DIR/suggest-tryinto-edition-change.rs:11:19
3+
|
4+
LL | let _i: i16 = TryFrom::try_from(0_i32).unwrap();
5+
| ^^^^^^^ not found in this scope
6+
|
7+
= note: 'std::convert::TryFrom' is included in the prelude starting in Edition 2021
8+
= note: 'core::convert::TryFrom' is included in the prelude starting in Edition 2021
9+
help: consider importing one of these items
10+
|
11+
LL | use std::convert::TryFrom;
12+
|
13+
LL | use core::convert::TryFrom;
14+
|
15+
16+
error[E0433]: failed to resolve: use of undeclared type `TryInto`
17+
--> $DIR/suggest-tryinto-edition-change.rs:17:19
18+
|
19+
LL | let _i: i16 = TryInto::try_into(0_i32).unwrap();
20+
| ^^^^^^^ not found in this scope
21+
|
22+
= note: 'std::convert::TryInto' is included in the prelude starting in Edition 2021
23+
= note: 'core::convert::TryInto' is included in the prelude starting in Edition 2021
24+
help: consider importing one of these items
25+
|
26+
LL | use std::convert::TryInto;
27+
|
28+
LL | use core::convert::TryInto;
29+
|
30+
31+
error[E0433]: failed to resolve: use of undeclared type `FromIterator`
32+
--> $DIR/suggest-tryinto-edition-change.rs:23:22
33+
|
34+
LL | let _v: Vec<_> = FromIterator::from_iter(&[1]);
35+
| ^^^^^^^^^^^^
36+
|
37+
::: $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
38+
|
39+
LL | pub trait IntoIterator {
40+
| ---------------------- similarly named trait `IntoIterator` defined here
41+
|
42+
= note: 'std::iter::FromIterator' is included in the prelude starting in Edition 2021
43+
= note: 'core::iter::FromIterator' is included in the prelude starting in Edition 2021
44+
help: a trait with a similar name exists
45+
|
46+
LL | let _v: Vec<_> = IntoIterator::from_iter(&[1]);
47+
| ~~~~~~~~~~~~
48+
help: consider importing one of these items
49+
|
50+
LL | use std::iter::FromIterator;
51+
|
52+
LL | use core::iter::FromIterator;
53+
|
54+
155
error[E0599]: no method named `try_into` found for type `i32` in the current scope
2-
--> $DIR/suggest-tryinto-edition-change.rs:5:24
56+
--> $DIR/suggest-tryinto-edition-change.rs:6:25
357
|
4-
LL | let i: i16 = 0_i32.try_into().unwrap();
5-
| ^^^^^^^^ method not found in `i32`
58+
LL | let _i: i16 = 0_i32.try_into().unwrap();
59+
| ^^^^^^^^ method not found in `i32`
660
|
761
::: $SRC_DIR/core/src/convert/mod.rs:LL:COL
862
|
@@ -16,6 +70,7 @@ help: the following trait is implemented but not in scope; perhaps add a `use` f
1670
LL | use std::convert::TryInto;
1771
|
1872

19-
error: aborting due to previous error
73+
error: aborting due to 4 previous errors
2074

21-
For more information about this error, try `rustc --explain E0599`.
75+
Some errors have detailed explanations: E0433, E0599.
76+
For more information about an error, try `rustc --explain E0433`.

0 commit comments

Comments
 (0)