forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix unsoundness when associated types dont actually come from supertr…
…aits
- Loading branch information
1 parent
8edf9b8
commit 172cf9b
Showing
3 changed files
with
269 additions
and
110 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
60 changes: 60 additions & 0 deletions
60
tests/ui/object-safety/almost-supertrait-associated-type.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,60 @@ | ||
// Test for fixed unsoundness in #126079. | ||
// Enforces that the associated types that are object safe | ||
|
||
use std::marker::PhantomData; | ||
|
||
fn transmute<T, U>(t: T) -> U { | ||
(&PhantomData::<T> as &dyn Foo<T, U>).transmute(t) | ||
//~^ ERROR the trait `Foo` cannot be made into an object | ||
//~| ERROR the trait `Foo` cannot be made into an object | ||
} | ||
|
||
struct ActuallySuper; | ||
struct NotActuallySuper; | ||
trait Super<Q> { | ||
type Assoc; | ||
} | ||
|
||
trait Dyn { | ||
type Out; | ||
} | ||
impl<T, U> Dyn for dyn Foo<T, U> + '_ { | ||
//~^ ERROR the trait `Foo` cannot be made into an object | ||
type Out = U; | ||
} | ||
impl<S: Dyn<Out = U> + ?Sized, U> Super<NotActuallySuper> for S { | ||
type Assoc = U; | ||
} | ||
|
||
trait Foo<T, U>: Super<ActuallySuper, Assoc = T> | ||
where | ||
<Self as Mirror>::Assoc: Super<NotActuallySuper> | ||
{ | ||
fn transmute(&self, t: T) -> <Self as Super<NotActuallySuper>>::Assoc; | ||
} | ||
|
||
trait Mirror { | ||
type Assoc: ?Sized; | ||
} | ||
impl<T: ?Sized> Mirror for T { | ||
type Assoc = T; | ||
} | ||
|
||
impl<T, U> Foo<T, U> for PhantomData<T> { | ||
fn transmute(&self, t: T) -> T { | ||
t | ||
} | ||
} | ||
impl<T> Super<ActuallySuper> for PhantomData<T> { | ||
type Assoc = T; | ||
} | ||
impl<T> Super<NotActuallySuper> for PhantomData<T> { | ||
type Assoc = T; | ||
} | ||
|
||
fn main() { | ||
let x = String::from("hello, world"); | ||
let s = transmute::<&str, &'static str>(x.as_str()); | ||
drop(x); | ||
println!("> {s}"); | ||
} |
Oops, something went wrong.