about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSimon Sapin <simon.sapin@exyr.org>2019-07-03 12:23:26 +0200
committerOliver Scherer <github35764891676564198441@oli-obk.de>2019-10-11 10:43:54 +0200
commit0a08841bb075959874e2e29c538150c826a1401a (patch)
tree05259ab9e66ac72e052409aef8a0f6aedad58138
parent000d90b11f7be70ffb7812680f7abc6deb52ec88 (diff)
downloadrust-0a08841bb075959874e2e29c538150c826a1401a.tar.gz
rust-0a08841bb075959874e2e29c538150c826a1401a.zip
Remove uses of `allow(unions_with_drop_fields)` in the standard library
-rw-r--r--src/libstd/panicking.rs15
1 files changed, 7 insertions, 8 deletions
diff --git a/src/libstd/panicking.rs b/src/libstd/panicking.rs
index 638ce1679b8..296f2a59bb8 100644
--- a/src/libstd/panicking.rs
+++ b/src/libstd/panicking.rs
@@ -12,7 +12,7 @@ use core::panic::{BoxMeUp, PanicInfo, Location};
 use crate::any::Any;
 use crate::fmt;
 use crate::intrinsics;
-use crate::mem;
+use crate::mem::{self, ManuallyDrop};
 use crate::ptr;
 use crate::raw;
 use crate::sync::atomic::{AtomicBool, Ordering};
@@ -227,10 +227,9 @@ pub use realstd::rt::update_panic_count;
 
 /// Invoke a closure, capturing the cause of an unwinding panic if one occurs.
 pub unsafe fn r#try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<dyn Any + Send>> {
-    #[allow(unions_with_drop_fields)]
     union Data<F, R> {
-        f: F,
-        r: R,
+        f: ManuallyDrop<F>,
+        r: ManuallyDrop<R>,
     }
 
     // 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>>
     let mut any_data = 0;
     let mut any_vtable = 0;
     let mut data = Data {
-        f,
+        f: ManuallyDrop::new(f)
     };
 
     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>>
 
     return if r == 0 {
         debug_assert!(update_panic_count(0) == 0);
-        Ok(data.r)
+        Ok(ManuallyDrop::into_inner(data.r))
     } else {
         update_panic_count(-1);
         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>>
     fn do_call<F: FnOnce() -> R, R>(data: *mut u8) {
         unsafe {
             let data = data as *mut Data<F, R>;
-            let f = ptr::read(&mut (*data).f);
-            ptr::write(&mut (*data).r, f());
+            let f = ptr::read(&mut *(*data).f);
+            ptr::write(&mut *(*data).r, f());
         }
     }
 }