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.
Rollup merge of rust-lang#131898 - lukas-code:ptr-cast-cleanup, r=compiler-errors minor `*dyn` cast cleanup Small follow-up to rust-lang#130234 to remove a redundant check and clean up comments. No functional changes. Also, explain why casts cannot drop the principal even though coercions can, and add a test because apparently we didn't have one already. r? `@WaffleLapkin` or `@compiler-errors`
- Loading branch information
Showing
3 changed files
with
69 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//! Test that non-coercion casts aren't allowed to drop the principal, | ||
//! because they cannot modify the pointer metadata. | ||
//! | ||
//! We test this in a const context to guard against UB if this is allowed | ||
//! in the future. | ||
|
||
trait Trait {} | ||
impl Trait for () {} | ||
|
||
struct Wrapper<T: ?Sized>(T); | ||
|
||
const OBJECT: *const (dyn Trait + Send) = &(); | ||
|
||
// coercions are allowed | ||
const _: *const dyn Send = OBJECT as _; | ||
|
||
// casts are **not** allowed | ||
const _: *const Wrapper<dyn Send> = OBJECT as _; | ||
//~^ ERROR casting `*const (dyn Trait + Send + 'static)` as `*const Wrapper<dyn Send>` is invalid | ||
|
||
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,11 @@ | ||
error[E0606]: casting `*const (dyn Trait + Send + 'static)` as `*const Wrapper<dyn Send>` is invalid | ||
--> $DIR/ptr-to-trait-obj-drop-principal.rs:18:37 | ||
| | ||
LL | const _: *const Wrapper<dyn Send> = OBJECT as _; | ||
| ^^^^^^^^^^^ | ||
| | ||
= note: the trait objects may have different vtables | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0606`. |