Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alloc: add ToString specialization for &&str #128759

Merged
merged 4 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
notriddle committed Aug 7, 2024
commit 20c833c632d76ee78284441226f12b919318bc4b
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
compiler-errors marked this conversation as resolved.
Show resolved Hide resolved
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 \
Expand All @@ -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 \
Expand All @@ -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 \
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/coercion/coerce-expect-unsized-ascribed.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ LL | let _ = type_ascribe!(Box::new( if true { false } else { true }), Box<d
|
= note: expected struct `Box<dyn Debug>`
found struct `Box<bool>`
= help: `bool` implements `Debug` so you could box the found value and coerce it to the trait object `Box<dyn Debug>`, you will have to change the expected type as well

error[E0308]: mismatched types
--> $DIR/coerce-expect-unsized-ascribed.rs:16:27
Expand All @@ -51,6 +52,7 @@ LL | let _ = type_ascribe!(Box::new( match true { true => 'a', false => 'b'
|
= note: expected struct `Box<dyn Debug>`
found struct `Box<char>`
= help: `char` implements `Debug` so you could box the found value and coerce it to the trait object `Box<dyn Debug>`, you will have to change the expected type as well

error[E0308]: mismatched types
--> $DIR/coerce-expect-unsized-ascribed.rs:18:27
Expand Down Expand Up @@ -96,6 +98,7 @@ LL | let _ = type_ascribe!(&if true { false } else { true }, &dyn Debug);
|
= note: expected reference `&dyn Debug`
found reference `&bool`
= help: `bool` implements `Debug` so you could box the found value and coerce it to the trait object `Box<dyn Debug>`, you will have to change the expected type as well

error[E0308]: mismatched types
--> $DIR/coerce-expect-unsized-ascribed.rs:24:27
Expand All @@ -105,6 +108,7 @@ LL | let _ = type_ascribe!(&match true { true => 'a', false => 'b' }, &dyn D
|
= note: expected reference `&dyn Debug`
found reference `&char`
= help: `char` implements `Debug` so you could box the found value and coerce it to the trait object `Box<dyn Debug>`, you will have to change the expected type as well

error[E0308]: mismatched types
--> $DIR/coerce-expect-unsized-ascribed.rs:26:27
Expand Down
1 change: 0 additions & 1 deletion tests/ui/traits/upcast_soundness_bug.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ LL | let p = p as *const dyn Trait<u8, u16>; // <- this is bad!
|
= note: expected trait object `dyn Trait<u8, u8>`
found trait object `dyn Trait<u8, u16>`
= help: `dyn Trait<u8, u16>` implements `Trait` so you could box the found value and coerce it to the trait object `Box<dyn Trait>`, you will have to change the expected type as well

error: aborting due to 1 previous error

Expand Down
Loading