Skip to content

Commit 5c10dbd

Browse files
authored
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)
2 parents 1839829 + 36a1141 commit 5c10dbd

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

compiler/rustc_lint/src/builtin.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,7 @@ declare_lint! {
12471247
/// [`UnsafeCell`]: https://doc.rust-lang.org/std/cell/struct.UnsafeCell.html
12481248
MUTABLE_TRANSMUTES,
12491249
Deny,
1250-
"mutating transmuted &mut T from &T may cause undefined behavior"
1250+
"transmuting &T to &mut T is undefined behavior, even if the reference is unused"
12511251
}
12521252

12531253
declare_lint_pass!(MutableTransmutes => [MUTABLE_TRANSMUTES]);
@@ -1259,8 +1259,8 @@ impl<'tcx> LateLintPass<'tcx> for MutableTransmutes {
12591259
get_transmute_from_to(cx, expr).map(|(ty1, ty2)| (ty1.kind(), ty2.kind()))
12601260
{
12611261
if to_mt == hir::Mutability::Mut && from_mt == hir::Mutability::Not {
1262-
let msg = "mutating transmuted &mut T from &T may cause undefined behavior, \
1263-
consider instead using an UnsafeCell";
1262+
let msg = "transmuting &T to &mut T is undefined behavior, \
1263+
even if the reference is unused, consider instead using an UnsafeCell";
12641264
cx.struct_span_lint(MUTABLE_TRANSMUTES, expr.span, |lint| lint.build(msg).emit());
12651265
}
12661266
}

src/test/ui/transmute/transmute-imut-to-mut.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ use std::mem::transmute;
44

55
fn main() {
66
let _a: &mut u8 = unsafe { transmute(&1u8) };
7-
//~^ ERROR mutating transmuted &mut T from &T may cause undefined behavior
7+
//~^ ERROR transmuting &T to &mut T is undefined behavior, even if the reference is unused, consider instead using an UnsafeCell
88
}

src/test/ui/transmute/transmute-imut-to-mut.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: mutating transmuted &mut T from &T may cause undefined behavior, consider instead using an UnsafeCell
1+
error: transmuting &T to &mut T is undefined behavior, even if the reference is unused, consider instead using an UnsafeCell
22
--> $DIR/transmute-imut-to-mut.rs:6:32
33
|
44
LL | let _a: &mut u8 = unsafe { transmute(&1u8) };

0 commit comments

Comments
 (0)