Skip to content

Commit 3876753

Browse files
committed
Add diagnostic in case of failed .try_into() method call pre-Edition 2021
1 parent e269e6b commit 3876753

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

compiler/rustc_typeck/src/check/method/suggest.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12031203
let mut candidates = valid_out_of_scope_traits;
12041204
candidates.sort();
12051205
candidates.dedup();
1206+
1207+
// `TryFrom` and `FromIterator` have no methods
1208+
let edition_fix = candidates
1209+
.iter()
1210+
.find(|did| self.tcx.is_diagnostic_item(sym::TryInto, **did))
1211+
.map(|&d| d);
1212+
12061213
err.help("items from traits can only be used if the trait is in scope");
12071214
let msg = format!(
12081215
"the following {traits_are} implemented but not in scope; \
@@ -1212,6 +1219,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12121219
);
12131220

12141221
self.suggest_use_candidates(err, msg, candidates);
1222+
if let Some(did) = edition_fix {
1223+
err.note(&format!(
1224+
"'{}' is included in the prelude starting in Edition 2021",
1225+
with_crate_prefix(|| self.tcx.def_path_str(did))
1226+
));
1227+
}
1228+
12151229
true
12161230
} else {
12171231
false

0 commit comments

Comments
 (0)