about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2022-01-20 17:10:37 +0100
committerGitHub <noreply@github.com>2022-01-20 17:10:37 +0100
commit5c10dbd85f962e4fc1ed16f4e157f00e5e7b6a8a (patch)
tree028faa77af90be6df66d96c3cba5b28c6638ef62 /src
parent1839829f0a88a5835d3b85043d69b12d056ea191 (diff)
parent36a11417fafcbfae0500884df2d4b6dd2c5e5a0f (diff)
downloadrust-5c10dbd85f962e4fc1ed16f4e157f00e5e7b6a8a.tar.gz
rust-5c10dbd85f962e4fc1ed16f4e157f00e5e7b6a8a.zip
Rollup merge of #92704 - 5225225:std_mem_transmute_ref_t_mut_t, r=michaelwoerister
Change lint message to be stronger for &T -> &mut T transmute

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`.

```rust
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)
Diffstat (limited to 'src')
-rw-r--r--src/test/ui/transmute/transmute-imut-to-mut.rs2
-rw-r--r--src/test/ui/transmute/transmute-imut-to-mut.stderr2
2 files changed, 2 insertions, 2 deletions
diff --git a/src/test/ui/transmute/transmute-imut-to-mut.rs b/src/test/ui/transmute/transmute-imut-to-mut.rs
index 8e34e0ae8c7..9f3f76c1ef3 100644
--- a/src/test/ui/transmute/transmute-imut-to-mut.rs
+++ b/src/test/ui/transmute/transmute-imut-to-mut.rs
@@ -4,5 +4,5 @@ use std::mem::transmute;
 
 fn main() {
     let _a: &mut u8 = unsafe { transmute(&1u8) };
-    //~^ ERROR mutating transmuted &mut T from &T may cause undefined behavior
+    //~^ ERROR transmuting &T to &mut T is undefined behavior, even if the reference is unused, consider instead using an UnsafeCell
 }
diff --git a/src/test/ui/transmute/transmute-imut-to-mut.stderr b/src/test/ui/transmute/transmute-imut-to-mut.stderr
index d323c1a73b7..1e9dff3ce89 100644
--- a/src/test/ui/transmute/transmute-imut-to-mut.stderr
+++ b/src/test/ui/transmute/transmute-imut-to-mut.stderr
@@ -1,4 +1,4 @@
-error: mutating transmuted &mut T from &T may cause undefined behavior, consider instead using an UnsafeCell
+error: transmuting &T to &mut T is undefined behavior, even if the reference is unused, consider instead using an UnsafeCell
   --> $DIR/transmute-imut-to-mut.rs:6:32
    |
 LL |     let _a: &mut u8 = unsafe { transmute(&1u8) };