Skip to content

Commit bfac2f7

Browse files
committed
Update libcore/std for union fields with drop
We now use ManuallyDrop for equivalent effect.
1 parent acf82c7 commit bfac2f7

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

src/libcore/slice/rotate.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use ptr;
88
union RawArray<T> {
99
/// Ensure this is appropriately aligned for T, and is big
1010
/// enough for two elements even if T is enormous.
11-
typed: [T; 2],
11+
typed: [MaybeUninit<T>; 2],
1212
/// For normally-sized types, especially things like u8, having more
1313
/// than 2 in the buffer is necessary for usefulness, so pad it out
1414
/// enough to be helpful, but not so big as to risk overflow.
@@ -73,7 +73,8 @@ pub unsafe fn ptr_rotate<T>(mut left: usize, mid: *mut T, mut right: usize) {
7373
}
7474

7575
let mut rawarray = MaybeUninit::<RawArray<T>>::uninitialized();
76-
let buf = &mut (*rawarray.as_mut_ptr()).typed as *mut [T; 2] as *mut T;
76+
let buf = &mut (*rawarray.as_mut_ptr()).typed as *mut [MaybeUninit<T>; 2]
77+
as *mut [T; 2] as *mut T;
7778

7879
let dim = mid.sub(left).add(right);
7980
if left <= right {

src/libstd/panicking.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use cell::RefCell;
1616
use core::panic::{PanicInfo, Location};
1717
use fmt;
1818
use intrinsics;
19-
use mem;
19+
use mem::{self, ManuallyDrop};
2020
use ptr;
2121
use raw;
2222
use sys::stdio::panic_output;
@@ -238,8 +238,8 @@ pub use realstd::rt::update_panic_count;
238238
pub unsafe fn try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<dyn Any + Send>> {
239239
#[allow(unions_with_drop_fields)]
240240
union Data<F, R> {
241-
f: F,
242-
r: R,
241+
f: ManuallyDrop<F>,
242+
r: ManuallyDrop<R>,
243243
}
244244

245245
// We do some sketchy operations with ownership here for the sake of
@@ -270,7 +270,7 @@ pub unsafe fn try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<dyn Any + Send>> {
270270
let mut any_data = 0;
271271
let mut any_vtable = 0;
272272
let mut data = Data {
273-
f,
273+
f: ManuallyDrop::new(f)
274274
};
275275

276276
let r = __rust_maybe_catch_panic(do_call::<F, R>,
@@ -280,7 +280,7 @@ pub unsafe fn try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<dyn Any + Send>> {
280280

281281
return if r == 0 {
282282
debug_assert!(update_panic_count(0) == 0);
283-
Ok(data.r)
283+
Ok(ManuallyDrop::into_inner(data.r))
284284
} else {
285285
update_panic_count(-1);
286286
debug_assert!(update_panic_count(0) == 0);
@@ -293,8 +293,8 @@ pub unsafe fn try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<dyn Any + Send>> {
293293
fn do_call<F: FnOnce() -> R, R>(data: *mut u8) {
294294
unsafe {
295295
let data = data as *mut Data<F, R>;
296-
let f = ptr::read(&mut (*data).f);
297-
ptr::write(&mut (*data).r, f());
296+
let f = ptr::read(&mut *(*data).f);
297+
ptr::write(&mut *(*data).r, f());
298298
}
299299
}
300300
}

0 commit comments

Comments
 (0)