2

Change lint message to be stronger for &T -> &mut T transmute by 5225...

 2 years ago
source link: https://github.com/rust-lang/rust/pull/92704
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Copy link

Contributor

5225225 commented 20 days ago

edited

The old message implied that it's only UB if you use the reference to mutate, which (as far as I know) is not true. As in, the following program has UB, and a &T -> &mut T transmute is effectively an unreachable_unchecked.

fn main() {
    #[allow(mutable_transmutes)]
    unsafe {
        let _ = std::mem::transmute::<&i32, &mut i32>(&0);
    }
}

In the future, it might be a good idea to use the edition system to make this a hard error, since I don't think it is ever defined behaviour? Unless we rule that &UnsafeCell<i32> -> &mut i32 is fine. (That, and you always could just use .get(), so you're not losing anything)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK