File tree 4 files changed +36
-2
lines changed
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> {
1688
1688
/// Creates a `Box<T>`, with the `Default` value for T.
1689
1689
#[ inline]
1690
1690
fn default ( ) -> Self {
1691
- Box :: new ( T :: default ( ) )
1691
+ Box :: write ( Box :: new_uninit ( ) , T :: default ( ) )
1692
1692
}
1693
1693
}
1694
1694
Original file line number Diff line number Diff line change 104
104
#![ feature( async_closure) ]
105
105
#![ feature( async_fn_traits) ]
106
106
#![ feature( async_iterator) ]
107
+ #![ feature( box_uninit_write) ]
107
108
#![ feature( clone_to_uninit) ]
108
109
#![ feature( coerce_unsized) ]
109
110
#![ 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> {
3447
3447
/// assert_eq!(*x, 0);
3448
3448
/// ```
3449
3449
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) ) }
3451
3457
}
3452
3458
}
3453
3459
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
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
25
+ // CHECK: ret ptr [[ARC]]
26
+ Arc :: default ( )
27
+ }
You can’t perform that action at this time.
0 commit comments