rust/tests/ui/cast/ptr-to-trait-obj-drop-principal.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

22 lines
595 B
Rust
Raw Normal View History

//! 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() {}