Skip to content

Commit f3ec74d

Browse files
committed
Fix Arc::into_inner.
It should never run the regular Drop implementation, sincei t unconditionally runs fetch_sub.
1 parent 819d531 commit f3ec74d

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

library/alloc/src/sync.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1093,13 +1093,13 @@ impl<T, A: Allocator> Arc<T, A> {
10931093
#[inline]
10941094
#[stable(feature = "arc_into_inner", since = "1.70.0")]
10951095
pub fn into_inner(this: Self) -> Option<T> {
1096+
let mut this = mem::ManuallyDrop::new(this);
1097+
let alloc = unsafe { ptr::read(&this.alloc) };
10961098
match this.inner().strong.fetch_sub(ONE_STRONG, Release) {
10971099
ONE_STRONG => {
1098-
let mut this = mem::ManuallyDrop::new(this);
10991100
acquire!(this.inner().strong);
11001101
let value = unsafe { ptr::read(Self::get_mut_unchecked(&mut this)) };
11011102
unsafe { this.deallocate() };
1102-
drop(unsafe { ptr::read(&this.alloc) });
11031103
Some(value)
11041104
}
11051105
ONE_STRONG_WITH_WEAK
@@ -1109,9 +1109,7 @@ impl<T, A: Allocator> Arc<T, A> {
11091109
.compare_exchange(NO_STRONG_WITH_WEAK, STRONG_DROPPED, Acquire, Relaxed)
11101110
.is_ok() =>
11111111
{
1112-
let mut this = mem::ManuallyDrop::new(this);
11131112
let value = unsafe { ptr::read(Self::get_mut_unchecked(&mut this)) };
1114-
let alloc = unsafe { ptr::read(&this.alloc) };
11151113
drop(Weak { ptr: this.ptr, alloc });
11161114
Some(value)
11171115
}

0 commit comments

Comments
 (0)