This repository was archived by the owner on May 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +36
-2
lines changed Expand file tree Collapse file tree 4 files changed +36
-2
lines changed Original file line number Diff line number Diff line change @@ -1688,7 +1688,7 @@ impl<T: Default> Default for Box<T> {
16881688 /// Creates a `Box<T>`, with the `Default` value for T.
16891689 #[ inline]
16901690 fn default ( ) -> Self {
1691- Box :: new ( T :: default ( ) )
1691+ Box :: write ( Box :: new_uninit ( ) , T :: default ( ) )
16921692 }
16931693}
16941694
Original file line number Diff line number Diff line change 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) ]
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 1+ //@ compile-flags: -O
2+ #![ crate_type = "lib" ]
3+
4+ // Test to check that types with "complex" destructors, but trivial `Default` impls
5+ // are constructed directly into the allocation in `Box::default` and `Arc::default`.
6+
7+ use std:: sync:: Arc ;
8+
9+ // CHECK-LABEL: @box_default_inplace
10+ #[ no_mangle]
11+ pub fn box_default_inplace ( ) -> Box < ( String , String ) > {
12+ // CHECK-NOT: alloca
13+ // CHECK: [[BOX:%.*]] = {{.*}}call {{.*}}__rust_alloc(
14+ // CHECK-NOT: call void @llvm.memcpy.p0.p0.i64(ptr {{.*}}[[BOX]],
15+ // CHECK: ret ptr [[BOX]]
16+ Box :: default ( )
17+ }
18+
19+ // CHECK-LABEL: @arc_default_inplace
20+ #[ no_mangle]
21+ pub fn arc_default_inplace ( ) -> Arc < ( String , String ) > {
22+ // CHECK-NOT: alloca
23+ // CHECK: [[ARC:%.*]] = {{.*}}call {{.*}}__rust_alloc(
24+ // CHECK-NOT: call void @llvm.memcpy.p0.p0.i64(
25+ // CHECK: ret ptr [[ARC]]
26+ Arc :: default ( )
27+ }
You can’t perform that action at this time.
0 commit comments