Content-Length: 24623 | pFad | https://doc.rust-lang.org/std/string/../error/../string/../vec/../../error_codes/./E0804.html

E0804 - Error codes index

Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Error code E0804

An auto trait cannot be added to the bounds of a dyn Trait type via a pointer cast.

Erroneous code example:

#![allow(unused)]
fn main() {
let ptr: *const dyn core::any::Any = &();
_ = ptr as *const (dyn core::any::Any + Send);
}

Adding an auto trait can make the vtable invalid, potentially causing UB in safe code afterwards. For example:

use core::{mem::transmute, ptr::NonNull};

trait Trait {
    fn f(&self)
    where
        Self: Send;
}

impl Trait for NonNull<()> {
    fn f(&self) {
        unreachable!()
    }
}

fn main() {
    let unsend: &dyn Trait = &NonNull::dangling();
    let bad: &(dyn Trait + Send) = unsafe { transmute(unsend) };
    // This crashes, since the vtable for `NonNull as dyn Trait` does
    // not have an entry for `Trait::f`.
    bad.f();
}

To fix this error, you can use transmute rather than pointer casts, but you must ensure that the vtable is valid for the pointer’s type before calling a method on the trait object or allowing other code to do so.









ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://doc.rust-lang.org/std/string/../error/../string/../vec/../../error_codes/./E0804.html

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy