Skip to content

Commit 96b11a5

Browse files
Test that we allow non-static lifetime transmutes
1 parent 2a606b5 commit 96b11a5

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

tests/ui/transmute.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,21 @@ fn bytes_to_str(b: &[u8], mb: &mut [u8]) {
140140
let _: &mut str = unsafe { std::mem::transmute(mb) };
141141
}
142142

143+
// Make sure we can modify lifetimes, which is one of the recommended uses
144+
// of transmute
145+
146+
// Make sure we can do static lifetime transmutes
147+
#[warn(transmute_ptr_to_ptr)]
148+
unsafe fn transmute_lifetime_to_static<'a, T>(t: &'a T) -> &'static T {
149+
std::mem::transmute::<&'a T, &'static T>(t)
150+
}
151+
152+
// Make sure we can do non-static lifetime transmutes
153+
#[warn(transmute_ptr_to_ptr)]
154+
unsafe fn transmute_lifetime<'a, 'b, T>(t: &'a T, u: &'b T) -> &'b T {
155+
std::mem::transmute::<&'a T, &'b T>(t)
156+
}
157+
143158
#[warn(transmute_ptr_to_ptr)]
144159
fn transmute_ptr_to_ptr() {
145160
let ptr = &1u32 as *const u32;
@@ -159,10 +174,6 @@ fn transmute_ptr_to_ptr() {
159174
let _ = mut_ptr as *mut f32;
160175
let _ = unsafe { &*(&1u32 as *const u32 as *const f32) };
161176
let _ = unsafe { &mut *(&mut 1u32 as *mut u32 as *mut f32) };
162-
// This is just modifying the lifetime, and is one of the recommended uses
163-
// of transmute
164-
let n = 1u32;
165-
let _ = unsafe { std::mem::transmute::<&'_ u32, &'static u32>(&n) };
166177
}
167178

168179
fn main() { }

tests/ui/transmute.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,29 +205,29 @@ error: transmute from a `&mut [u8]` to a `&mut str`
205205
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8_mut(mb).unwrap()`
206206

207207
error: transmute from a pointer to a pointer
208-
--> $DIR/transmute.rs:149:29
208+
--> $DIR/transmute.rs:164:29
209209
|
210-
149 | let _: *const f32 = std::mem::transmute(ptr);
210+
164 | let _: *const f32 = std::mem::transmute(ptr);
211211
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr as *const f32`
212212
|
213213
= note: `-D transmute-ptr-to-ptr` implied by `-D warnings`
214214

215215
error: transmute from a pointer to a pointer
216-
--> $DIR/transmute.rs:150:27
216+
--> $DIR/transmute.rs:165:27
217217
|
218-
150 | let _: *mut f32 = std::mem::transmute(mut_ptr);
218+
165 | let _: *mut f32 = std::mem::transmute(mut_ptr);
219219
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `mut_ptr as *mut f32`
220220

221221
error: transmute from a reference to a reference
222-
--> $DIR/transmute.rs:152:23
222+
--> $DIR/transmute.rs:167:23
223223
|
224-
152 | let _: &f32 = std::mem::transmute(&1u32);
224+
167 | let _: &f32 = std::mem::transmute(&1u32);
225225
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(&1u32 as *const u32 as *const f32)`
226226

227227
error: transmute from a reference to a reference
228-
--> $DIR/transmute.rs:153:27
228+
--> $DIR/transmute.rs:168:27
229229
|
230-
153 | let _: &mut f32 = std::mem::transmute(&mut 1u32);
230+
168 | let _: &mut f32 = std::mem::transmute(&mut 1u32);
231231
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut *(&mut 1u32 as *mut u32 as *mut f32)`
232232

233233
error: aborting due to 36 previous errors

0 commit comments

Comments
 (0)