-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
elaborate why dropping principal in
*dyn
casts is non-trivial
- Loading branch information
Lukas Markeffsky
committed
Oct 20, 2024
1 parent
b89751b
commit 46cc5e9
Showing
3 changed files
with
61 additions
and
2 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`. |