forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#120248 - WaffleLapkin:bonk-ptr-object-casts, …
…r=<try> Make casts of pointers to trait objects stricter This is an attempt to `fix` rust-lang#120222 and rust-lang#120217. cc `@oli-obk` `@compiler-errors` `@lcnr`
- Loading branch information
Showing
8 changed files
with
146 additions
and
31 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// check-fail | ||
// | ||
// issue: <https://github.com/rust-lang/rust/issues/120222> | ||
|
||
|
||
trait A {} | ||
impl<T> A for T {} | ||
trait B {} | ||
impl<T> B for T {} | ||
|
||
trait Trait<G> {} | ||
struct X; | ||
impl<T> Trait<X> for T {} | ||
struct Y; | ||
impl<T> Trait<Y> for T {} | ||
|
||
fn main() { | ||
let a: *const dyn A = &(); | ||
let b: *const dyn B = a as _; //~ error: casting `*const dyn A` as `*const dyn B` is invalid | ||
|
||
let x: *const dyn Trait<X> = &(); | ||
let y: *const dyn Trait<Y> = x as _; //~ error: casting `*const dyn Trait<X>` as `*const dyn Trait<Y>` is invalid | ||
|
||
_ = (b, y); | ||
} | ||
|
||
fn generic<T>(x: *const dyn Trait<X>, t: *const dyn Trait<T>) { | ||
let _: *const dyn Trait<T> = x as _; //~ error: casting `*const (dyn Trait<X> + 'static)` as `*const dyn Trait<T>` is invalid | ||
let _: *const dyn Trait<X> = t as _; //~ error: casting `*const (dyn Trait<T> + 'static)` as `*const dyn Trait<X>` is invalid | ||
} |
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,35 @@ | ||
error[E0606]: casting `*const dyn A` as `*const dyn B` is invalid | ||
--> $DIR/ptr-to-trait-obj-different-args.rs:19:27 | ||
| | ||
LL | let b: *const dyn B = a as _; | ||
| ^^^^^^ | ||
| | ||
= note: vtable kinds may not match | ||
|
||
error[E0606]: casting `*const dyn Trait<X>` as `*const dyn Trait<Y>` is invalid | ||
--> $DIR/ptr-to-trait-obj-different-args.rs:22:34 | ||
| | ||
LL | let y: *const dyn Trait<Y> = x as _; | ||
| ^^^^^^ | ||
| | ||
= note: vtable kinds may not match | ||
|
||
error[E0606]: casting `*const (dyn Trait<X> + 'static)` as `*const dyn Trait<T>` is invalid | ||
--> $DIR/ptr-to-trait-obj-different-args.rs:28:34 | ||
| | ||
LL | let _: *const dyn Trait<T> = x as _; | ||
| ^^^^^^ | ||
| | ||
= note: vtable kinds may not match | ||
|
||
error[E0606]: casting `*const (dyn Trait<T> + 'static)` as `*const dyn Trait<X>` is invalid | ||
--> $DIR/ptr-to-trait-obj-different-args.rs:29:34 | ||
| | ||
LL | let _: *const dyn Trait<X> = t as _; | ||
| ^^^^^^ | ||
| | ||
= note: vtable kinds may not match | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0606`. |
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,31 @@ | ||
// check-fail | ||
// | ||
// issue: <https://github.com/rust-lang/rust/issues/120217> | ||
|
||
#![feature(arbitrary_self_types)] | ||
|
||
trait Static<'a> { | ||
fn proof(self: *const Self, s: &'a str) -> &'static str; | ||
} | ||
|
||
fn bad_cast<'a>(x: *const dyn Static<'static>) -> *const dyn Static<'a> { | ||
x as _ //~ error: lifetime may not live long enough | ||
} | ||
|
||
impl Static<'static> for () { | ||
fn proof(self: *const Self, s: &'static str) -> &'static str { | ||
s | ||
} | ||
} | ||
|
||
fn extend_lifetime(s: &str) -> &'static str { | ||
bad_cast(&()).proof(s) | ||
} | ||
|
||
fn main() { | ||
let s = String::from("Hello World"); | ||
let slice = extend_lifetime(&s); | ||
println!("Now it exists: {slice}"); | ||
drop(s); | ||
println!("Now it’s gone: {slice}"); | ||
} |
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,10 @@ | ||
error: lifetime may not live long enough | ||
--> $DIR/ptr-to-trait-obj-different-regions.rs:12:5 | ||
| | ||
LL | fn bad_cast<'a>(x: *const dyn Static<'static>) -> *const dyn Static<'a> { | ||
| -- lifetime `'a` defined here | ||
LL | x as _ | ||
| ^^^^^^ returning this value requires that `'a` must outlive `'static` | ||
|
||
error: aborting due to 1 previous error | ||
|