diff options
| author | Michael Howell <michael@notriddle.com> | 2024-08-06 18:18:15 -0700 |
|---|---|---|
| committer | Michael Howell <michael@notriddle.com> | 2024-08-06 18:24:17 -0700 |
| commit | 20c833c632d76ee78284441226f12b919318bc4b (patch) | |
| tree | 50a707aee18f62580856f65cd7525ba743ddd787 /compiler/rustc_trait_selection/src | |
| parent | 1b587a6e76200fdd8364ef910246efa11c973e7b (diff) | |
| download | rust-20c833c632d76ee78284441226f12b919318bc4b.tar.gz rust-20c833c632d76ee78284441226f12b919318bc4b.zip | |
diagnostics: `Box<dyn Trait>` suggestion with multiple matching impl
The two altered expectation messages both seem like improvements: - `coerce-expect-unsized-ascribed.stderr` says you can go `Box<char> -> Box<dyn Debug>`, which you can. - `upcast_soundness_bug.stderr` used to say that you could go `Box<dyn Trait<u8, u8>> -> Box<dyn Trait>`, which you can't, because the type parameters are missing in the destination and the only ones that work aren't what's needed.
Diffstat (limited to 'compiler/rustc_trait_selection/src')
| -rw-r--r-- | compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs | 24 |
1 files changed, 9 insertions, 15 deletions
diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs index 864510bb650..0c140f93d94 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs @@ -313,11 +313,9 @@ impl<T> Trait<T> for X { (ty::Dynamic(t, _, ty::DynKind::Dyn), _) if let Some(def_id) = t.principal_def_id() => { - let mut impl_def_ids = vec![]; - tcx.for_each_relevant_impl(def_id, values.found, |did| { - impl_def_ids.push(did) - }); - if let [_] = &impl_def_ids[..] { + let has_non_blanket_impl = + tcx.non_blanket_impls_for_ty(def_id, values.found).next().is_some(); + if has_non_blanket_impl { let trait_name = tcx.item_name(def_id); diag.help(format!( "`{}` implements `{trait_name}` so you could box the found value \ @@ -330,11 +328,9 @@ impl<T> Trait<T> for X { (_, ty::Dynamic(t, _, ty::DynKind::Dyn)) if let Some(def_id) = t.principal_def_id() => { - let mut impl_def_ids = vec![]; - tcx.for_each_relevant_impl(def_id, values.expected, |did| { - impl_def_ids.push(did) - }); - if let [_] = &impl_def_ids[..] { + let has_non_blanket_impl = + tcx.non_blanket_impls_for_ty(def_id, values.expected).next().is_some(); + if has_non_blanket_impl { let trait_name = tcx.item_name(def_id); diag.help(format!( "`{}` implements `{trait_name}` so you could change the expected \ @@ -346,11 +342,9 @@ impl<T> Trait<T> for X { (ty::Dynamic(t, _, ty::DynKind::DynStar), _) if let Some(def_id) = t.principal_def_id() => { - let mut impl_def_ids = vec![]; - tcx.for_each_relevant_impl(def_id, values.found, |did| { - impl_def_ids.push(did) - }); - if let [_] = &impl_def_ids[..] { + let has_non_blanket_impl = + tcx.non_blanket_impls_for_ty(def_id, values.found).next().is_some(); + if has_non_blanket_impl { let trait_name = tcx.item_name(def_id); diag.help(format!( "`{}` implements `{trait_name}`, `#[feature(dyn_star)]` is likely \ |
