forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#135228 - compiler-errors:normalizes-ur-dispatch, r=BoxyUwU Improve `DispatchFromDyn` and `CoerceUnsized` impl validation * Disallow arbitrary 1-ZST fields in `DispatchFromDyn` -- only `PhantomData`, and 1-ZSTs that mention no params (which is needed to support, e.g., the `Global` alloctor in `Box<T, U = Global>`). * Don't allow coercing between non-ZSTs to ZSTs (since the previous check wasn't actually checking the field tys were the same before checking the layout...) * Normalize the field before checking it's `PhantomData`. Fixes rust-lang#135215 Fixes rust-lang#135214 Fixes rust-lang#135220 r? ```@BoxyUwU``` or reassign
- Loading branch information
Showing
8 changed files
with
153 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
tests/ui/self/dispatch-from-dyn-zst-transmute-zst-nonzst.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// We used to allow erroneous `DispatchFromDyn` impls whose RHS type contained | ||
// fields that weren't ZSTs. I don't believe this was possible to abuse, but | ||
// it's at least nice to give users better errors. | ||
|
||
#![feature(arbitrary_self_types)] | ||
#![feature(unsize)] | ||
#![feature(dispatch_from_dyn)] | ||
|
||
use std::marker::Unsize; | ||
use std::ops::DispatchFromDyn; | ||
|
||
struct Dispatchable<T: ?Sized, Z> { | ||
_ptr: Box<T>, | ||
z: Z, | ||
} | ||
|
||
impl<T, U> DispatchFromDyn<Dispatchable<U, i32>> for Dispatchable<T, ()> | ||
//~^ ERROR implementing the `DispatchFromDyn` trait requires multiple coercions | ||
where | ||
T: Unsize<U> + ?Sized, | ||
U: ?Sized, | ||
{ | ||
} | ||
|
||
fn main() {} |
16 changes: 16 additions & 0 deletions
16
tests/ui/self/dispatch-from-dyn-zst-transmute-zst-nonzst.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
error[E0378]: implementing the `DispatchFromDyn` trait requires multiple coercions | ||
--> $DIR/dispatch-from-dyn-zst-transmute-zst-nonzst.rs:17:1 | ||
| | ||
LL | / impl<T, U> DispatchFromDyn<Dispatchable<U, i32>> for Dispatchable<T, ()> | ||
LL | | | ||
LL | | where | ||
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`) | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0378`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#![feature(arbitrary_self_types)] | ||
#![feature(unsize)] | ||
#![feature(dispatch_from_dyn)] | ||
|
||
use std::marker::PhantomData; | ||
use std::marker::Unsize; | ||
use std::ops::DispatchFromDyn; | ||
use std::ops::Deref; | ||
|
||
struct IsSendToken<T: ?Sized>(PhantomData<fn(T) -> T>); | ||
|
||
struct Foo<'a, U: ?Sized> { | ||
token: IsSendToken<U>, | ||
ptr: &'a U, | ||
} | ||
|
||
impl<'a, T, U> DispatchFromDyn<Foo<'a, U>> for Foo<'a, T> | ||
//~^ ERROR implementing the `DispatchFromDyn` trait requires multiple coercions | ||
where | ||
T: Unsize<U> + ?Sized, | ||
U: ?Sized {} | ||
|
||
trait Bar { | ||
fn f(self: Foo<'_, Self>); | ||
} | ||
|
||
impl<U: ?Sized> Deref for Foo<'_, U> { | ||
type Target = U; | ||
fn deref(&self) -> &U { | ||
self.ptr | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
error[E0378]: implementing the `DispatchFromDyn` trait requires multiple coercions | ||
--> $DIR/dispatch-from-dyn-zst-transmute.rs:17:1 | ||
| | ||
LL | / impl<'a, T, U> DispatchFromDyn<Foo<'a, U>> for Foo<'a, T> | ||
LL | | | ||
LL | | where | ||
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`) | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0378`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//@ check-pass | ||
|
||
#![feature(coerce_unsized, dispatch_from_dyn, unsize)] | ||
|
||
use std::marker::Unsize; | ||
use std::ops::{CoerceUnsized, DispatchFromDyn}; | ||
use std::marker::PhantomData; | ||
|
||
trait Mirror { | ||
type Assoc; | ||
} | ||
impl<T> Mirror for T { | ||
type Assoc = T; | ||
} | ||
|
||
struct W<T: 'static> { | ||
t: &'static T, | ||
f: <PhantomData<T> as Mirror>::Assoc, | ||
} | ||
|
||
impl<T, U> CoerceUnsized<W<U>> for W<T> where T: Unsize<U> {} | ||
|
||
impl<T, U> DispatchFromDyn<W<U>> for W<T> where T: Unsize<U> {} | ||
|
||
fn main() {} |