diff options
| author | Jakob Degen <jakob@degen.com> | 2021-10-22 23:21:46 -0400 |
|---|---|---|
| committer | Jakob Degen <jakob@degen.com> | 2021-10-26 22:17:01 -0400 |
| commit | 387675366868e19fe1a9542afa2738b8f9ca23de (patch) | |
| tree | 1f15da490a60f62c6375f2ac40694ba9cc870c65 | |
| parent | e269e6bf47f40c9046cd44ab787881d700099252 (diff) | |
| download | rust-387675366868e19fe1a9542afa2738b8f9ca23de.tar.gz rust-387675366868e19fe1a9542afa2738b8f9ca23de.zip | |
Add diagnostic in case of failed `.try_into()` method call pre-Edition 2021
| -rw-r--r-- | compiler/rustc_typeck/src/check/method/suggest.rs | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/compiler/rustc_typeck/src/check/method/suggest.rs b/compiler/rustc_typeck/src/check/method/suggest.rs index 183ebc559ae..28b19981c2d 100644 --- a/compiler/rustc_typeck/src/check/method/suggest.rs +++ b/compiler/rustc_typeck/src/check/method/suggest.rs @@ -1203,6 +1203,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let mut candidates = valid_out_of_scope_traits; candidates.sort(); candidates.dedup(); + + // `TryFrom` and `FromIterator` have no methods + let edition_fix = candidates + .iter() + .find(|did| self.tcx.is_diagnostic_item(sym::TryInto, **did)) + .map(|&d| d); + err.help("items from traits can only be used if the trait is in scope"); let msg = format!( "the following {traits_are} implemented but not in scope; \ @@ -1212,6 +1219,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ); self.suggest_use_candidates(err, msg, candidates); + if let Some(did) = edition_fix { + err.note(&format!( + "'{}' is included in the prelude starting in Edition 2021", + with_crate_prefix(|| self.tcx.def_path_str(did)) + )); + } + true } else { false |
