Skip to content

Commit 0a08841

Browse files
SimonSapinoli-obk
authored andcommitted
Remove uses of allow(unions_with_drop_fields) in the standard library
1 parent 000d90b commit 0a08841

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/libstd/panicking.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use core::panic::{BoxMeUp, PanicInfo, Location};
1212
use crate::any::Any;
1313
use crate::fmt;
1414
use crate::intrinsics;
15-
use crate::mem;
15+
use crate::mem::{self, ManuallyDrop};
1616
use crate::ptr;
1717
use crate::raw;
1818
use crate::sync::atomic::{AtomicBool, Ordering};
@@ -227,10 +227,9 @@ pub use realstd::rt::update_panic_count;
227227

228228
/// Invoke a closure, capturing the cause of an unwinding panic if one occurs.
229229
pub unsafe fn r#try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<dyn Any + Send>> {
230-
#[allow(unions_with_drop_fields)]
231230
union Data<F, R> {
232-
f: F,
233-
r: R,
231+
f: ManuallyDrop<F>,
232+
r: ManuallyDrop<R>,
234233
}
235234

236235
// We do some sketchy operations with ownership here for the sake of
@@ -261,7 +260,7 @@ pub unsafe fn r#try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<dyn Any + Send>>
261260
let mut any_data = 0;
262261
let mut any_vtable = 0;
263262
let mut data = Data {
264-
f,
263+
f: ManuallyDrop::new(f)
265264
};
266265

267266
let r = __rust_maybe_catch_panic(do_call::<F, R>,
@@ -271,7 +270,7 @@ pub unsafe fn r#try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<dyn Any + Send>>
271270

272271
return if r == 0 {
273272
debug_assert!(update_panic_count(0) == 0);
274-
Ok(data.r)
273+
Ok(ManuallyDrop::into_inner(data.r))
275274
} else {
276275
update_panic_count(-1);
277276
debug_assert!(update_panic_count(0) == 0);
@@ -284,8 +283,8 @@ pub unsafe fn r#try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<dyn Any + Send>>
284283
fn do_call<F: FnOnce() -> R, R>(data: *mut u8) {
285284
unsafe {
286285
let data = data as *mut Data<F, R>;
287-
let f = ptr::read(&mut (*data).f);
288-
ptr::write(&mut (*data).r, f());
286+
let f = ptr::read(&mut *(*data).f);
287+
ptr::write(&mut *(*data).r, f());
289288
}
290289
}
291290
}

0 commit comments

Comments
 (0)