Remove uses of `allow(unions_with_drop_fields)` in the standard library

This commit is contained in:
Simon Sapin 2019-07-03 12:23:26 +02:00 committed by Oliver Scherer
parent 000d90b11f
commit 0a08841bb0
1 changed files with 7 additions and 8 deletions

View File

@ -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());
}
}
}