Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5e474f7

Browse files
committed
allocate before calling T::default in <Arc<T>>::default()
Same rationale as in the previous commit.
1 parent dd0620b commit 5e474f7

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

‎library/alloc/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
#![feature(async_closure)]
105105
#![feature(async_fn_traits)]
106106
#![feature(async_iterator)]
107+
#![feature(box_uninit_write)]
107108
#![feature(clone_to_uninit)]
108109
#![feature(coerce_unsized)]
109110
#![feature(const_align_of_val)]

‎library/alloc/src/sync.rs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3447,7 +3447,13 @@ impl<T: Default> Default for Arc<T> {
34473447
/// assert_eq!(*x, 0);
34483448
/// ```
34493449
fn default() -> Arc<T> {
3450-
Arc::new(Default::default())
3450+
let x = Box::into_raw(Box::write(Box::new_uninit(), ArcInner {
3451+
strong: atomic::AtomicUsize::new(1),
3452+
weak: atomic::AtomicUsize::new(1),
3453+
data: T::default(),
3454+
}));
3455+
// SAFETY: `Box::into_raw` consumes the `Box` and never returns null
3456+
unsafe { Self::from_inner(NonNull::new_unchecked(x)) }
34513457
}
34523458
}
34533459

‎tests/codegen/placement-new.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ pub fn box_default_inplace() -> Box<(String, String)> {
1919
// CHECK-LABEL: @arc_default_inplace
2020
#[no_mangle]
2121
pub fn arc_default_inplace() -> Arc<(String, String)> {
22-
// CHECK: [[ALLOCA:%.*]] = alloca
22+
// CHECK-NOT: alloca
2323
// CHECK: [[ARC:%.*]] = {{.*}}call {{.*}}__rust_alloc(
24-
// CHECK: call void @llvm.memcpy
24+
// CHECK-NOT: call void @llvm.memcpy
2525
// CHECK: ret ptr [[ARC]]
2626
Arc::default()
2727
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /