diff options
| author | Michael Goulet <michael@errs.io> | 2025-02-24 19:21:45 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-24 19:21:45 -0500 |
| commit | 0bb00e2085b169bfc21e58b299f67f44c906d2fa (patch) | |
| tree | f5126f1db3325a493fdca88f16e7630b2107618b /tests/ui/self | |
| parent | 748af6b3e441fb624ee96d48d6e3935642427a91 (diff) | |
| parent | b46acc01916ba3e8b8f8ab9d89608861d1d4cb87 (diff) | |
| download | rust-0bb00e2085b169bfc21e58b299f67f44c906d2fa.tar.gz rust-0bb00e2085b169bfc21e58b299f67f44c906d2fa.zip | |
Rollup merge of #137289 - compiler-errors:coerce-unsized-errors, r=oli-obk
Consolidate and improve error messaging for `CoerceUnsized` and `DispatchFromDyn` Firstly, this PR consolidates and reworks the error diagnostics for `CoercePointee` and `DispatchFromDyn`. There was a ton of duplication for no reason -- this reworks both the errors and also the error codes, since they can be shared between both traits since they report the same thing. Secondly, when encountering a struct with multiple fields that must be coerced, point out the field spans, rather than mentioning the fields by name. This makes the error message clearer, but also means that we don't mention the `__S` dummy parameter for `derive(CoercePointee)`. Thirdly, emit a custom error message when we encounter a trait error that comes from the recursive field `CoerceUnsized`/`DispatchFromDyn` trait check. **Note:** This is the only one I'm not too satisfied with -- I think it could use some more refinement, but ideally it explains that the field must be an unsize-able pointer... Feedback welcome. Finally, don't emit `DispatchFromDyn` validity errors if we detect `CoerceUnsized` validity errors from an impl of the same ADT. This is best reviewed per commit. r? `@oli-obk` perhaps? cc `@dingxiangfei2009` -- sorry for making my own attempt at this PR, but I wanted to see if I could implement a fix for #136796 in a less complicated way, since communicating over github review comments can be a bit slow. I'll leave comments inline to explain my thinking about the diagnostics changes.
Diffstat (limited to 'tests/ui/self')
4 files changed, 20 insertions, 10 deletions
diff --git a/tests/ui/self/dispatch-from-dyn-zst-transmute-zst-nonzst.rs b/tests/ui/self/dispatch-from-dyn-zst-transmute-zst-nonzst.rs index 71f198f7dc7..94b76fe9685 100644 --- a/tests/ui/self/dispatch-from-dyn-zst-transmute-zst-nonzst.rs +++ b/tests/ui/self/dispatch-from-dyn-zst-transmute-zst-nonzst.rs @@ -15,7 +15,7 @@ struct Dispatchable<T: ?Sized, Z> { } impl<T, U> DispatchFromDyn<Dispatchable<U, i32>> for Dispatchable<T, ()> -//~^ ERROR implementing the `DispatchFromDyn` trait requires multiple coercions +//~^ ERROR implementing `DispatchFromDyn` does not allow multiple fields to be coerced where T: Unsize<U> + ?Sized, U: ?Sized, diff --git a/tests/ui/self/dispatch-from-dyn-zst-transmute-zst-nonzst.stderr b/tests/ui/self/dispatch-from-dyn-zst-transmute-zst-nonzst.stderr index 1f13c51f679..91760b9e2ea 100644 --- a/tests/ui/self/dispatch-from-dyn-zst-transmute-zst-nonzst.stderr +++ b/tests/ui/self/dispatch-from-dyn-zst-transmute-zst-nonzst.stderr @@ -1,4 +1,4 @@ -error[E0378]: implementing the `DispatchFromDyn` trait requires multiple coercions +error[E0375]: implementing `DispatchFromDyn` does not allow multiple fields to be coerced --> $DIR/dispatch-from-dyn-zst-transmute-zst-nonzst.rs:17:1 | LL | / impl<T, U> DispatchFromDyn<Dispatchable<U, i32>> for Dispatchable<T, ()> @@ -8,9 +8,14 @@ LL | | T: Unsize<U> + ?Sized, LL | | U: ?Sized, | |______________^ | - = note: the trait `DispatchFromDyn` may only be implemented for a coercion between structures with a single field being coerced - = note: currently, 2 fields need coercions: `_ptr` (`Box<T>` to `Box<U>`), `z` (`()` to `i32`) +note: the trait `DispatchFromDyn` may only be implemented when a single field is being coerced + --> $DIR/dispatch-from-dyn-zst-transmute-zst-nonzst.rs:13:5 + | +LL | _ptr: Box<T>, + | ^^^^^^^^^^^^ +LL | z: Z, + | ^^^^ error: aborting due to 1 previous error -For more information about this error, try `rustc --explain E0378`. +For more information about this error, try `rustc --explain E0375`. diff --git a/tests/ui/self/dispatch-from-dyn-zst-transmute.rs b/tests/ui/self/dispatch-from-dyn-zst-transmute.rs index 57c255b4d7b..967958ab486 100644 --- a/tests/ui/self/dispatch-from-dyn-zst-transmute.rs +++ b/tests/ui/self/dispatch-from-dyn-zst-transmute.rs @@ -15,7 +15,7 @@ struct Foo<'a, U: ?Sized> { } impl<'a, T, U> DispatchFromDyn<Foo<'a, U>> for Foo<'a, T> -//~^ ERROR implementing the `DispatchFromDyn` trait requires multiple coercions +//~^ ERROR implementing `DispatchFromDyn` does not allow multiple fields to be coerced where T: Unsize<U> + ?Sized, U: ?Sized {} diff --git a/tests/ui/self/dispatch-from-dyn-zst-transmute.stderr b/tests/ui/self/dispatch-from-dyn-zst-transmute.stderr index 5a8ae88b5f1..cc8be45e99d 100644 --- a/tests/ui/self/dispatch-from-dyn-zst-transmute.stderr +++ b/tests/ui/self/dispatch-from-dyn-zst-transmute.stderr @@ -1,4 +1,4 @@ -error[E0378]: implementing the `DispatchFromDyn` trait requires multiple coercions +error[E0375]: implementing `DispatchFromDyn` does not allow multiple fields to be coerced --> $DIR/dispatch-from-dyn-zst-transmute.rs:17:1 | LL | / impl<'a, T, U> DispatchFromDyn<Foo<'a, U>> for Foo<'a, T> @@ -8,9 +8,14 @@ LL | | T: Unsize<U> + ?Sized, LL | | U: ?Sized {} | |_____________^ | - = note: the trait `DispatchFromDyn` may only be implemented for a coercion between structures with a single field being coerced - = note: currently, 2 fields need coercions: `token` (`IsSendToken<T>` to `IsSendToken<U>`), `ptr` (`&'a T` to `&'a U`) +note: the trait `DispatchFromDyn` may only be implemented when a single field is being coerced + --> $DIR/dispatch-from-dyn-zst-transmute.rs:13:5 + | +LL | token: IsSendToken<U>, + | ^^^^^^^^^^^^^^^^^^^^^ +LL | ptr: &'a U, + | ^^^^^^^^^^ error: aborting due to 1 previous error -For more information about this error, try `rustc --explain E0378`. +For more information about this error, try `rustc --explain E0375`. |
