From 640b9b81c397db0d270a8626e516f2ae8b247f9b Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Fri, 9 Oct 2026 13:13:37 +0100 Subject: [PATCH 1/2] Translate unsafe Default in unsafe context --- cpp2rust/converter/converter.cpp | 4 ++-- tests/unit/struct_default_ctor.cpp | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index b63c7fcb3..e2e2bf801 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -4917,11 +4917,11 @@ void Converter::AddDefaultTrait(const clang::RecordDecl *decl) { PushBrace impl_brace(*this); StrCat("fn default() -> Self"); PushBrace fn_brace(*this); + StrCat(keyword_unsafe_); + PushBrace unsafe_brace(*this); if (auto *cxx = clang::dyn_cast(decl)) { if (auto *default_ctor = GetUserDefinedDefaultConstructor(cxx)) { - StrCat(keyword_unsafe_); - PushBrace unsafe_brace(*this); Convert(MakeConstructExpr(ctx_, ctx_.getCanonicalTagType(decl), default_ctor, {})); return; diff --git a/tests/unit/struct_default_ctor.cpp b/tests/unit/struct_default_ctor.cpp index 07973b1ae..28ef7db75 100644 --- a/tests/unit/struct_default_ctor.cpp +++ b/tests/unit/struct_default_ctor.cpp @@ -12,11 +12,27 @@ struct Declared { Declared(); }; +struct Holder { + S items[2]; +}; + +struct FromStatic { + static constexpr int kInit = 3; + int v = kInit; +}; + int main() { Declared *d = nullptr; assert(d == nullptr); S s; assert(s.a == 11); assert(s.b == true); + + Holder *h = new Holder[1]; + assert(h[0].items[1].a == 11); + delete[] h; + FromStatic *fs = new FromStatic[2]; + assert(fs[1].v == 3); + delete[] fs; return 0; } From 124fffff92d2ac7c5100126e8912b1f25e3a515c Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Fri, 9 Oct 2026 18:45:47 +0100 Subject: [PATCH 2/2] Update tests --- .../out/refcount/defaulted_move_cross_tu.rs | 8 ++- .../template_method_late_instantiation.rs | 2 +- .../template_method_late_instantiation.rs | 2 +- tests/unit/out/refcount/10_struct.rs | 6 +- tests/unit/out/refcount/array_const_init.rs | 10 +-- tests/unit/out/refcount/array_field_addr.rs | 10 +-- .../out/refcount/array_of_noncopy_struct.rs | 8 ++- tests/unit/out/refcount/clone_vs_move.rs | 14 ++-- tests/unit/out/refcount/copy_assign.rs | 16 +++-- tests/unit/out/refcount/copy_ctor.rs | 16 +++-- .../unit/out/refcount/copy_move_defaulted.rs | 44 +++++++------ tests/unit/out/refcount/default.rs | 64 ++++++++++--------- tests/unit/out/refcount/default_in_statics.rs | 28 ++++---- tests/unit/out/refcount/destructor.rs | 10 +-- tests/unit/out/refcount/field_aliasing.rs | 12 ++-- .../unit/out/refcount/field_array_elem_ptr.rs | 16 +++-- .../out/refcount/field_method_receiver.rs | 20 +++--- tests/unit/out/refcount/field_pointers.rs | 28 ++++---- .../field_reinterpret_write_through.rs | 14 ++-- .../refcount/flex_array_member_incomplete.rs | 8 ++- .../flex_array_member_sibling_write.rs | 22 ++++--- .../refcount/in_class_field_initializer.rs | 22 ++++--- tests/unit/out/refcount/init_list.rs | 6 +- .../refcount/libc_struct_without_default.rs | 18 ++++-- .../out/refcount/memcpy_struct_layouts.rs | 8 ++- tests/unit/out/refcount/memset_struct_tail.rs | 14 ++-- tests/unit/out/refcount/offsetof.rs | 8 ++- .../refcount/operator_member_pointer_free.rs | 8 ++- .../operator_member_pointer_member.rs | 8 ++- tests/unit/out/refcount/pointer_array.rs | 14 ++-- tests/unit/out/refcount/random.rs | 26 ++++---- tests/unit/out/refcount/rule_of_five.rs | 8 ++- tests/unit/out/refcount/rule_of_three.rs | 8 ++- .../refcount/self_referencing_initializer.rs | 8 ++- .../out/refcount/struct_2d_array_field.rs | 18 ++++-- .../unit/out/refcount/struct_default_ctor.rs | 64 ++++++++++++++++++- .../refcount/struct_member_array_adjacency.rs | 8 ++- tests/unit/out/refcount/unboxed_locals.rs | 2 +- tests/unit/out/refcount/unboxed_records.rs | 16 +++-- .../out/refcount/union_addrof_external.rs | 12 ++-- .../unit/out/refcount/union_cross_arm_cast.rs | 22 ++++--- .../unit/out/refcount/union_memset_memcpy.rs | 20 +++--- tests/unit/out/refcount/union_nested.rs | 8 ++- tests/unit/out/unsafe/10_struct.rs | 6 +- .../out/unsafe/array_of_noncopy_struct.rs | 8 ++- tests/unit/out/unsafe/default.rs | 2 +- .../unsafe/flex_array_member_incomplete.rs | 2 +- .../out/unsafe/in_class_field_initializer.rs | 22 ++++--- tests/unit/out/unsafe/init_list.rs | 6 +- .../out/unsafe/libc_struct_without_default.rs | 14 ++-- tests/unit/out/unsafe/offsetof.rs | 8 ++- .../unsafe/self_referencing_initializer.rs | 8 ++- tests/unit/out/unsafe/struct_default_ctor.rs | 58 ++++++++++++++++- tests/unit/out/unsafe/unboxed_locals.rs | 2 +- tests/unit/out/unsafe/unboxed_records.rs | 2 +- 55 files changed, 530 insertions(+), 292 deletions(-) diff --git a/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs b/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs index 0126940ae..d84a4f987 100644 --- a/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs +++ b/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs @@ -36,9 +36,11 @@ impl S { } impl Default for S { fn default() -> Self { - S { - v: Rc::new(RefCell::new(Default::default())), - n: Rc::new(RefCell::new((0..2).map(|_| 0_i32).collect::>())), + { + S { + v: Rc::new(RefCell::new(Default::default())), + n: Rc::new(RefCell::new((0..2).map(|_| 0_i32).collect::>())), + } } } } diff --git a/tests/multi-file/template_method_late_instantiation/out/refcount/template_method_late_instantiation.rs b/tests/multi-file/template_method_late_instantiation/out/refcount/template_method_late_instantiation.rs index 86f3cc993..6ba3c3a10 100644 --- a/tests/multi-file/template_method_late_instantiation/out/refcount/template_method_late_instantiation.rs +++ b/tests/multi-file/template_method_late_instantiation/out/refcount/template_method_late_instantiation.rs @@ -14,7 +14,7 @@ pub struct S_int_ { } impl Default for S_int_ { fn default() -> Self { - S_int_ { x: 0 } + { S_int_ { x: 0 } } } } pub fn main() { diff --git a/tests/multi-file/template_method_late_instantiation/out/unsafe/template_method_late_instantiation.rs b/tests/multi-file/template_method_late_instantiation/out/unsafe/template_method_late_instantiation.rs index 7104da99a..d70135863 100644 --- a/tests/multi-file/template_method_late_instantiation/out/unsafe/template_method_late_instantiation.rs +++ b/tests/multi-file/template_method_late_instantiation/out/unsafe/template_method_late_instantiation.rs @@ -18,7 +18,7 @@ impl S_int_ { } impl Default for S_int_ { fn default() -> Self { - S_int_ { x: 0 } + unsafe { S_int_ { x: 0 } } } } pub fn main() { diff --git a/tests/unit/out/refcount/10_struct.rs b/tests/unit/out/refcount/10_struct.rs index d235b5719..a419f3930 100644 --- a/tests/unit/out/refcount/10_struct.rs +++ b/tests/unit/out/refcount/10_struct.rs @@ -38,8 +38,10 @@ impl Partial { } impl Default for Partial { fn default() -> Self { - Partial { - p: Ptr::::null(), + { + Partial { + p: Ptr::::null(), + } } } } diff --git a/tests/unit/out/refcount/array_const_init.rs b/tests/unit/out/refcount/array_const_init.rs index 34a5c1857..41c8bc2c9 100644 --- a/tests/unit/out/refcount/array_const_init.rs +++ b/tests/unit/out/refcount/array_const_init.rs @@ -20,10 +20,12 @@ pub struct S { } impl Default for S { fn default() -> Self { - S { - head: 0_i32, - tail: Rc::new(RefCell::new((0..3).map(|_| 0_i32).collect::>())), - buf: Rc::new(RefCell::new((0..4).map(|_| 0_i8).collect::>())), + { + S { + head: 0_i32, + tail: Rc::new(RefCell::new((0..3).map(|_| 0_i32).collect::>())), + buf: Rc::new(RefCell::new((0..4).map(|_| 0_i8).collect::>())), + } } } } diff --git a/tests/unit/out/refcount/array_field_addr.rs b/tests/unit/out/refcount/array_field_addr.rs index a15d56fe1..b3690f520 100644 --- a/tests/unit/out/refcount/array_field_addr.rs +++ b/tests/unit/out/refcount/array_field_addr.rs @@ -19,10 +19,12 @@ pub struct S { } impl Default for S { fn default() -> Self { - S { - before: 0_i32, - mask: Rc::new(RefCell::new((0..4).map(|_| 0_u8).collect::>())), - after: 0_i32, + { + S { + before: 0_i32, + mask: Rc::new(RefCell::new((0..4).map(|_| 0_u8).collect::>())), + after: 0_i32, + } } } } diff --git a/tests/unit/out/refcount/array_of_noncopy_struct.rs b/tests/unit/out/refcount/array_of_noncopy_struct.rs index c0b593b8a..ab6ae3662 100644 --- a/tests/unit/out/refcount/array_of_noncopy_struct.rs +++ b/tests/unit/out/refcount/array_of_noncopy_struct.rs @@ -17,9 +17,11 @@ pub struct NonCopy { } impl Default for NonCopy { fn default() -> Self { - NonCopy { - data: Rc::new(RefCell::new(Default::default())), - tag: 0, + { + NonCopy { + data: Rc::new(RefCell::new(Default::default())), + tag: 0, + } } } } diff --git a/tests/unit/out/refcount/clone_vs_move.rs b/tests/unit/out/refcount/clone_vs_move.rs index 190579b41..5c77efd60 100644 --- a/tests/unit/out/refcount/clone_vs_move.rs +++ b/tests/unit/out/refcount/clone_vs_move.rs @@ -32,12 +32,14 @@ pub struct Foo { } impl Default for Foo { fn default() -> Self { - Foo { - x: 0_i32, - y: >::default(), - z: Ptr::::null(), - a: Rc::new(RefCell::new((0..3).map(|_| 0_i32).collect::>())), - bar: ::default(), + { + Foo { + x: 0_i32, + y: >::default(), + z: Ptr::::null(), + a: Rc::new(RefCell::new((0..3).map(|_| 0_i32).collect::>())), + bar: ::default(), + } } } } diff --git a/tests/unit/out/refcount/copy_assign.rs b/tests/unit/out/refcount/copy_assign.rs index 0060fb43d..488c83406 100644 --- a/tests/unit/out/refcount/copy_assign.rs +++ b/tests/unit/out/refcount/copy_assign.rs @@ -81,13 +81,15 @@ pub struct Holder { } impl Default for Holder { fn default() -> Self { - Holder { - p: ::default(), - arr: Rc::new(RefCell::new( - (0..2) - .map(|_| ::default()) - .collect::>(), - )), + { + Holder { + p: ::default(), + arr: Rc::new(RefCell::new( + (0..2) + .map(|_| ::default()) + .collect::>(), + )), + } } } } diff --git a/tests/unit/out/refcount/copy_ctor.rs b/tests/unit/out/refcount/copy_ctor.rs index 566469fd1..b81e4466d 100644 --- a/tests/unit/out/refcount/copy_ctor.rs +++ b/tests/unit/out/refcount/copy_ctor.rs @@ -101,13 +101,15 @@ pub struct Holder { } impl Default for Holder { fn default() -> Self { - Holder { - c: ::default(), - arr: Rc::new(RefCell::new( - (0..2) - .map(|_| ::default()) - .collect::>(), - )), + { + Holder { + c: ::default(), + arr: Rc::new(RefCell::new( + (0..2) + .map(|_| ::default()) + .collect::>(), + )), + } } } } diff --git a/tests/unit/out/refcount/copy_move_defaulted.rs b/tests/unit/out/refcount/copy_move_defaulted.rs index 98aac76bf..6841629f9 100644 --- a/tests/unit/out/refcount/copy_move_defaulted.rs +++ b/tests/unit/out/refcount/copy_move_defaulted.rs @@ -35,10 +35,12 @@ impl Explicit { } impl Default for Explicit { fn default() -> Self { - Explicit { - v: 0_i32, - inner: ::default(), - arr: Rc::new(RefCell::new((0..2).map(|_| 0_i32).collect::>())), + { + Explicit { + v: 0_i32, + inner: ::default(), + arr: Rc::new(RefCell::new((0..2).map(|_| 0_i32).collect::>())), + } } } } @@ -56,10 +58,12 @@ pub struct Implicit { } impl Default for Implicit { fn default() -> Self { - Implicit { - v: 0_i32, - inner: ::default(), - arr: Rc::new(RefCell::new((0..2).map(|_| 0_i32).collect::>())), + { + Implicit { + v: 0_i32, + inner: ::default(), + arr: Rc::new(RefCell::new((0..2).map(|_| 0_i32).collect::>())), + } } } } @@ -159,11 +163,13 @@ impl Buffer { } impl Default for Buffer { fn default() -> Self { - Buffer { - data: Rc::new(RefCell::new(Default::default())), - rows: Rc::new(RefCell::new(Vec::new())), - n: 0_i32, - arr: Rc::new(RefCell::new((0..2).map(|_| 0_i32).collect::>())), + { + Buffer { + data: Rc::new(RefCell::new(Default::default())), + rows: Rc::new(RefCell::new(Vec::new())), + n: 0_i32, + arr: Rc::new(RefCell::new((0..2).map(|_| 0_i32).collect::>())), + } } } } @@ -198,11 +204,13 @@ impl Owner { } impl Default for Owner { fn default() -> Self { - Owner { - data: Rc::new(RefCell::new(Default::default())), - n: 0_i32, - arr: Rc::new(RefCell::new((0..2).map(|_| 0_i32).collect::>())), - p: None, + { + Owner { + data: Rc::new(RefCell::new(Default::default())), + n: 0_i32, + arr: Rc::new(RefCell::new((0..2).map(|_| 0_i32).collect::>())), + p: None, + } } } } diff --git a/tests/unit/out/refcount/default.rs b/tests/unit/out/refcount/default.rs index b9f5f9001..eb530ded8 100644 --- a/tests/unit/out/refcount/default.rs +++ b/tests/unit/out/refcount/default.rs @@ -26,20 +26,22 @@ pub struct Pointers { } impl Default for Pointers { fn default() -> Self { - Pointers { - x1: Ptr::::null(), - x2: Ptr::::null(), - x3: Rc::new(RefCell::new( - (0..5) - .map(|_| Ptr::::null()) - .collect::]>>(), - )), - x4: Rc::new(RefCell::new( - (0..10) - .map(|_| Ptr::::null()) - .collect::]>>(), - )), - x5: 0_i32, + { + Pointers { + x1: Ptr::::null(), + x2: Ptr::::null(), + x3: Rc::new(RefCell::new( + (0..5) + .map(|_| Ptr::::null()) + .collect::]>>(), + )), + x4: Rc::new(RefCell::new( + (0..10) + .map(|_| Ptr::::null()) + .collect::]>>(), + )), + x5: 0_i32, + } } } } @@ -61,19 +63,21 @@ pub struct SmallArrays { } impl Default for SmallArrays { fn default() -> Self { - SmallArrays { - a: Rc::new(RefCell::new((0..32).map(|_| 0_i32).collect::>())), - f: FnPtr:: i32>::null(), - fs: Rc::new(RefCell::new( - (0..2) - .map(|_| FnPtr:: i32>::null()) - .collect:: i32>]>>(), - )), - p: Rc::new(RefCell::new( - (0..2) - .map(|_| ::default()) - .collect::>(), - )), + { + SmallArrays { + a: Rc::new(RefCell::new((0..32).map(|_| 0_i32).collect::>())), + f: FnPtr:: i32>::null(), + fs: Rc::new(RefCell::new( + (0..2) + .map(|_| FnPtr:: i32>::null()) + .collect:: i32>]>>(), + )), + p: Rc::new(RefCell::new( + (0..2) + .map(|_| ::default()) + .collect::>(), + )), + } } } } @@ -86,8 +90,10 @@ pub struct BigArray { } impl Default for BigArray { fn default() -> Self { - BigArray { - a: Rc::new(RefCell::new((0..33).map(|_| 0_i32).collect::>())), + { + BigArray { + a: Rc::new(RefCell::new((0..33).map(|_| 0_i32).collect::>())), + } } } } diff --git a/tests/unit/out/refcount/default_in_statics.rs b/tests/unit/out/refcount/default_in_statics.rs index 4ebb80a2c..00425919c 100644 --- a/tests/unit/out/refcount/default_in_statics.rs +++ b/tests/unit/out/refcount/default_in_statics.rs @@ -44,19 +44,21 @@ pub struct Outer { } impl Default for Outer { fn default() -> Self { - Outer { - p1: Ptr::::null(), - p2: Ptr::::null(), - arr: Rc::new(RefCell::new( - (0..3) - .map(|_| Ptr::::null()) - .collect::]>>(), - )), - cp: Ptr::::null(), - pp: Ptr::>::null(), - inner: ::default(), - x: 0_i32, - fn_: FnPtr:: i32>::null(), + { + Outer { + p1: Ptr::::null(), + p2: Ptr::::null(), + arr: Rc::new(RefCell::new( + (0..3) + .map(|_| Ptr::::null()) + .collect::]>>(), + )), + cp: Ptr::::null(), + pp: Ptr::>::null(), + inner: ::default(), + x: 0_i32, + fn_: FnPtr:: i32>::null(), + } } } } diff --git a/tests/unit/out/refcount/destructor.rs b/tests/unit/out/refcount/destructor.rs index 1eb4f7b62..47bc4234a 100644 --- a/tests/unit/out/refcount/destructor.rs +++ b/tests/unit/out/refcount/destructor.rs @@ -42,10 +42,12 @@ pub struct ArrayMember { } impl Default for ArrayMember { fn default() -> Self { - ArrayMember { - items: Rc::new(RefCell::new( - (0..3).map(|_| ::default()).collect::>(), - )), + { + ArrayMember { + items: Rc::new(RefCell::new( + (0..3).map(|_| ::default()).collect::>(), + )), + } } } } diff --git a/tests/unit/out/refcount/field_aliasing.rs b/tests/unit/out/refcount/field_aliasing.rs index a25b740db..26603ae12 100644 --- a/tests/unit/out/refcount/field_aliasing.rs +++ b/tests/unit/out/refcount/field_aliasing.rs @@ -30,11 +30,13 @@ pub struct S { } impl Default for S { fn default() -> Self { - S { - in_: ::default(), - total: 0_i32, - n: 0_i32, - arr: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), + { + S { + in_: ::default(), + total: 0_i32, + n: 0_i32, + arr: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), + } } } } diff --git a/tests/unit/out/refcount/field_array_elem_ptr.rs b/tests/unit/out/refcount/field_array_elem_ptr.rs index 6920c715a..b3c6a8dac 100644 --- a/tests/unit/out/refcount/field_array_elem_ptr.rs +++ b/tests/unit/out/refcount/field_array_elem_ptr.rs @@ -30,13 +30,15 @@ pub struct Shape { } impl Default for Shape { fn default() -> Self { - Shape { - id: 0_i32, - coords: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), - points: Rc::new(RefCell::new( - (0..3).map(|_| ::default()).collect::>(), - )), - tail: 0_i32, + { + Shape { + id: 0_i32, + coords: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), + points: Rc::new(RefCell::new( + (0..3).map(|_| ::default()).collect::>(), + )), + tail: 0_i32, + } } } } diff --git a/tests/unit/out/refcount/field_method_receiver.rs b/tests/unit/out/refcount/field_method_receiver.rs index 77dabbb73..3740cd6bb 100644 --- a/tests/unit/out/refcount/field_method_receiver.rs +++ b/tests/unit/out/refcount/field_method_receiver.rs @@ -29,15 +29,17 @@ pub struct S { } impl Default for S { fn default() -> Self { - S { - tag: 0_i32, - c: ::default(), - arr: Rc::new(RefCell::new( - (0..2) - .map(|_| ::default()) - .collect::>(), - )), - v: Rc::new(RefCell::new(Default::default())), + { + S { + tag: 0_i32, + c: ::default(), + arr: Rc::new(RefCell::new( + (0..2) + .map(|_| ::default()) + .collect::>(), + )), + v: Rc::new(RefCell::new(Default::default())), + } } } } diff --git a/tests/unit/out/refcount/field_pointers.rs b/tests/unit/out/refcount/field_pointers.rs index 7c3274216..56dcd2a85 100644 --- a/tests/unit/out/refcount/field_pointers.rs +++ b/tests/unit/out/refcount/field_pointers.rs @@ -17,9 +17,11 @@ pub struct Inner { } impl Default for Inner { fn default() -> Self { - Inner { - a: 0_i32, - name: Rc::new(RefCell::new((0..8).map(|_| 0_i8).collect::>())), + { + Inner { + a: 0_i32, + name: Rc::new(RefCell::new((0..8).map(|_| 0_i8).collect::>())), + } } } } @@ -54,15 +56,17 @@ pub struct Outer { } impl Default for Outer { fn default() -> Self { - Outer { - x: 0_i32, - inner: ::default(), - items: Rc::new(RefCell::new( - (0..3).map(|_| ::default()).collect::>(), - )), - v: Rc::new(RefCell::new(Default::default())), - cursor: Ptr::::null(), - buf: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), + { + Outer { + x: 0_i32, + inner: ::default(), + items: Rc::new(RefCell::new( + (0..3).map(|_| ::default()).collect::>(), + )), + v: Rc::new(RefCell::new(Default::default())), + cursor: Ptr::::null(), + buf: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), + } } } } diff --git a/tests/unit/out/refcount/field_reinterpret_write_through.rs b/tests/unit/out/refcount/field_reinterpret_write_through.rs index a584d579f..ad6883874 100644 --- a/tests/unit/out/refcount/field_reinterpret_write_through.rs +++ b/tests/unit/out/refcount/field_reinterpret_write_through.rs @@ -33,12 +33,14 @@ pub struct S { } impl Default for S { fn default() -> Self { - S { - x: 0_i32, - in_: ::default(), - bytes: Rc::new(RefCell::new((0..4).map(|_| 0_u8).collect::>())), - arr: Rc::new(RefCell::new((0..3).map(|_| 0_i32).collect::>())), - tail: 0_i64, + { + S { + x: 0_i32, + in_: ::default(), + bytes: Rc::new(RefCell::new((0..4).map(|_| 0_u8).collect::>())), + arr: Rc::new(RefCell::new((0..3).map(|_| 0_i32).collect::>())), + tail: 0_i64, + } } } } diff --git a/tests/unit/out/refcount/flex_array_member_incomplete.rs b/tests/unit/out/refcount/flex_array_member_incomplete.rs index 61d23bfd8..54ee0abd8 100644 --- a/tests/unit/out/refcount/flex_array_member_incomplete.rs +++ b/tests/unit/out/refcount/flex_array_member_incomplete.rs @@ -17,9 +17,11 @@ pub struct F { } impl Default for F { fn default() -> Self { - F { - n: 0_i32, - tail: Rc::new(RefCell::new(Box::<[i8]>::default())), + { + F { + n: 0_i32, + tail: Rc::new(RefCell::new(Box::<[i8]>::default())), + } } } } diff --git a/tests/unit/out/refcount/flex_array_member_sibling_write.rs b/tests/unit/out/refcount/flex_array_member_sibling_write.rs index ee89839dc..05bae984a 100644 --- a/tests/unit/out/refcount/flex_array_member_sibling_write.rs +++ b/tests/unit/out/refcount/flex_array_member_sibling_write.rs @@ -17,9 +17,11 @@ pub struct S { } impl Default for S { fn default() -> Self { - S { - n: 0_i32, - name: Rc::new(RefCell::new((0..1).map(|_| 0_i8).collect::>())), + { + S { + n: 0_i32, + name: Rc::new(RefCell::new((0..1).map(|_| 0_i8).collect::>())), + } } } } @@ -44,12 +46,14 @@ pub struct T { } impl Default for T { fn default() -> Self { - T { - n: 0_i32, - cap: 0_i32, - a: Rc::new(RefCell::new( - (0..1).map(|_| ::default()).collect::>(), - )), + { + T { + n: 0_i32, + cap: 0_i32, + a: Rc::new(RefCell::new( + (0..1).map(|_| ::default()).collect::>(), + )), + } } } } diff --git a/tests/unit/out/refcount/in_class_field_initializer.rs b/tests/unit/out/refcount/in_class_field_initializer.rs index e7b633524..22463a37e 100644 --- a/tests/unit/out/refcount/in_class_field_initializer.rs +++ b/tests/unit/out/refcount/in_class_field_initializer.rs @@ -16,7 +16,7 @@ pub struct Inner { } impl Default for Inner { fn default() -> Self { - Inner { x: 3, y: 4 } + { Inner { x: 3, y: 4 } } } } #[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] @@ -35,11 +35,13 @@ pub struct S { } impl Default for S { fn default() -> Self { - S { - a: 1, - b: 2_i8, - c: ::default(), - d: ::default(), + { + S { + a: 1, + b: 2_i8, + c: ::default(), + d: ::default(), + } } } } @@ -58,9 +60,11 @@ impl Boxed_int_ { } impl Default for Boxed_int_ { fn default() -> Self { - Boxed_int_ { - v: 0_i32, - tag: 0_i32, + { + Boxed_int_ { + v: 0_i32, + tag: 0_i32, + } } } } diff --git a/tests/unit/out/refcount/init_list.rs b/tests/unit/out/refcount/init_list.rs index ce029c520..153950fbc 100644 --- a/tests/unit/out/refcount/init_list.rs +++ b/tests/unit/out/refcount/init_list.rs @@ -26,8 +26,10 @@ pub struct Holder { } impl Default for Holder { fn default() -> Self { - Holder { - p: Pair { a: 0, b: 0 }, + { + Holder { + p: Pair { a: 0, b: 0 }, + } } } } diff --git a/tests/unit/out/refcount/libc_struct_without_default.rs b/tests/unit/out/refcount/libc_struct_without_default.rs index 4e3be5c1e..b4d157d1b 100644 --- a/tests/unit/out/refcount/libc_struct_without_default.rs +++ b/tests/unit/out/refcount/libc_struct_without_default.rs @@ -18,11 +18,13 @@ pub struct UserDefined { } impl Default for UserDefined { fn default() -> Self { - UserDefined { - a: Rc::new(RefCell::new( - std::array::from_fn::<_, 1, _>(|_| Default::default()).to_vec(), - )), - v: Rc::new(RefCell::new(Default::default())), + { + UserDefined { + a: Rc::new(RefCell::new( + std::array::from_fn::<_, 1, _>(|_| Default::default()).to_vec(), + )), + v: Rc::new(RefCell::new(Default::default())), + } } } } @@ -35,8 +37,10 @@ pub struct FieldIsLibcType { } impl Default for FieldIsLibcType { fn default() -> Self { - FieldIsLibcType { - addr: Default::default(), + { + FieldIsLibcType { + addr: Default::default(), + } } } } diff --git a/tests/unit/out/refcount/memcpy_struct_layouts.rs b/tests/unit/out/refcount/memcpy_struct_layouts.rs index dbfd7e83f..ecb56b236 100644 --- a/tests/unit/out/refcount/memcpy_struct_layouts.rs +++ b/tests/unit/out/refcount/memcpy_struct_layouts.rs @@ -56,9 +56,11 @@ pub struct array { } impl Default for array { fn default() -> Self { - array { - name: Rc::new(RefCell::new((0..3).map(|_| 0_i8).collect::>())), - x: 0_i32, + { + array { + name: Rc::new(RefCell::new((0..3).map(|_| 0_i8).collect::>())), + x: 0_i32, + } } } } diff --git a/tests/unit/out/refcount/memset_struct_tail.rs b/tests/unit/out/refcount/memset_struct_tail.rs index e8513f1e6..e280539ab 100644 --- a/tests/unit/out/refcount/memset_struct_tail.rs +++ b/tests/unit/out/refcount/memset_struct_tail.rs @@ -23,12 +23,14 @@ pub struct S { } impl Default for S { fn default() -> Self { - S { - keep: 0_i32, - a: 0_i32, - b: 0_i64, - c: Rc::new(RefCell::new((0..5).map(|_| 0_i8).collect::>())), - last: 0_i32, + { + S { + keep: 0_i32, + a: 0_i32, + b: 0_i64, + c: Rc::new(RefCell::new((0..5).map(|_| 0_i8).collect::>())), + last: 0_i32, + } } } } diff --git a/tests/unit/out/refcount/offsetof.rs b/tests/unit/out/refcount/offsetof.rs index 04579b34d..54cf3fdc4 100644 --- a/tests/unit/out/refcount/offsetof.rs +++ b/tests/unit/out/refcount/offsetof.rs @@ -27,9 +27,11 @@ pub struct Frame { } impl Default for Frame { fn default() -> Self { - Frame { - tag: 0_u16, - body: Rc::new(RefCell::new((0..64).map(|_| 0_i8).collect::>())), + { + Frame { + tag: 0_u16, + body: Rc::new(RefCell::new((0..64).map(|_| 0_i8).collect::>())), + } } } } diff --git a/tests/unit/out/refcount/operator_member_pointer_free.rs b/tests/unit/out/refcount/operator_member_pointer_free.rs index 40c891f47..f4e7805a4 100644 --- a/tests/unit/out/refcount/operator_member_pointer_free.rs +++ b/tests/unit/out/refcount/operator_member_pointer_free.rs @@ -24,9 +24,11 @@ pub struct S { } impl Default for S { fn default() -> Self { - S { - data: Rc::new(RefCell::new((0..3).map(|_| 0_i32).collect::>())), - inner: ::default(), + { + S { + data: Rc::new(RefCell::new((0..3).map(|_| 0_i32).collect::>())), + inner: ::default(), + } } } } diff --git a/tests/unit/out/refcount/operator_member_pointer_member.rs b/tests/unit/out/refcount/operator_member_pointer_member.rs index 5b0739931..2cd8e505a 100644 --- a/tests/unit/out/refcount/operator_member_pointer_member.rs +++ b/tests/unit/out/refcount/operator_member_pointer_member.rs @@ -35,9 +35,11 @@ pub struct S { } impl Default for S { fn default() -> Self { - S { - data: Rc::new(RefCell::new((0..3).map(|_| 0_i32).collect::>())), - inner: ::default(), + { + S { + data: Rc::new(RefCell::new((0..3).map(|_| 0_i32).collect::>())), + inner: ::default(), + } } } } diff --git a/tests/unit/out/refcount/pointer_array.rs b/tests/unit/out/refcount/pointer_array.rs index 477195900..416721b1a 100644 --- a/tests/unit/out/refcount/pointer_array.rs +++ b/tests/unit/out/refcount/pointer_array.rs @@ -15,12 +15,14 @@ pub struct StackArray { } impl Default for StackArray { fn default() -> Self { - StackArray { - arr: Rc::new(RefCell::new( - (0..3) - .map(|_| Ptr::::null()) - .collect::]>>(), - )), + { + StackArray { + arr: Rc::new(RefCell::new( + (0..3) + .map(|_| Ptr::::null()) + .collect::]>>(), + )), + } } } } diff --git a/tests/unit/out/refcount/random.rs b/tests/unit/out/refcount/random.rs index c14961764..f86d55609 100644 --- a/tests/unit/out/refcount/random.rs +++ b/tests/unit/out/refcount/random.rs @@ -31,18 +31,20 @@ pub struct Pair { } impl Default for Pair { fn default() -> Self { - Pair { - x: 0_i32, - y: 0_i32, - a: Rc::new(RefCell::new((0..5).map(|_| 0_i32).collect::>())), - r: >::default(), - p: Ptr::::null(), - pair: Ptr::::null(), - ap: Rc::new(RefCell::new( - (0..2) - .map(|_| Ptr::::null()) - .collect::]>>(), - )), + { + Pair { + x: 0_i32, + y: 0_i32, + a: Rc::new(RefCell::new((0..5).map(|_| 0_i32).collect::>())), + r: >::default(), + p: Ptr::::null(), + pair: Ptr::::null(), + ap: Rc::new(RefCell::new( + (0..2) + .map(|_| Ptr::::null()) + .collect::]>>(), + )), + } } } } diff --git a/tests/unit/out/refcount/rule_of_five.rs b/tests/unit/out/refcount/rule_of_five.rs index 673227d7a..b587e1514 100644 --- a/tests/unit/out/refcount/rule_of_five.rs +++ b/tests/unit/out/refcount/rule_of_five.rs @@ -86,9 +86,11 @@ impl Clone for Buffer { } impl Default for Buffer { fn default() -> Self { - Buffer { - data: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), - size: 0_i32, + { + Buffer { + data: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), + size: 0_i32, + } } } } diff --git a/tests/unit/out/refcount/rule_of_three.rs b/tests/unit/out/refcount/rule_of_three.rs index 52b852338..db6994d19 100644 --- a/tests/unit/out/refcount/rule_of_three.rs +++ b/tests/unit/out/refcount/rule_of_three.rs @@ -65,9 +65,11 @@ impl Clone for Buffer { } impl Default for Buffer { fn default() -> Self { - Buffer { - data: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), - size: 0_i32, + { + Buffer { + data: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), + size: 0_i32, + } } } } diff --git a/tests/unit/out/refcount/self_referencing_initializer.rs b/tests/unit/out/refcount/self_referencing_initializer.rs index 80797122f..8eae7b50a 100644 --- a/tests/unit/out/refcount/self_referencing_initializer.rs +++ b/tests/unit/out/refcount/self_referencing_initializer.rs @@ -33,9 +33,11 @@ pub struct node_with_defaults { } impl Default for node_with_defaults { fn default() -> Self { - node_with_defaults { - next: Ptr::::null(), - value: 3, + { + node_with_defaults { + next: Ptr::::null(), + value: 3, + } } } } diff --git a/tests/unit/out/refcount/struct_2d_array_field.rs b/tests/unit/out/refcount/struct_2d_array_field.rs index 856c531b6..52f5b18e2 100644 --- a/tests/unit/out/refcount/struct_2d_array_field.rs +++ b/tests/unit/out/refcount/struct_2d_array_field.rs @@ -17,13 +17,17 @@ pub struct table { } impl Default for table { fn default() -> Self { - table { - rows: Rc::new(RefCell::new( - (0..3) - .map(|_| Rc::new(RefCell::new((0..10).map(|_| 0_i8).collect::>()))) - .collect::>]>>(), - )), - count: 0_usize, + { + table { + rows: Rc::new(RefCell::new( + (0..3) + .map(|_| { + Rc::new(RefCell::new((0..10).map(|_| 0_i8).collect::>())) + }) + .collect::>]>>(), + )), + count: 0_usize, + } } } } diff --git a/tests/unit/out/refcount/struct_default_ctor.rs b/tests/unit/out/refcount/struct_default_ctor.rs index 2392cba14..359ecbac4 100644 --- a/tests/unit/out/refcount/struct_default_ctor.rs +++ b/tests/unit/out/refcount/struct_default_ctor.rs @@ -31,6 +31,42 @@ pub struct Declared { pub v: i32, } impl Declared {} +#[derive(DeepClone, Record, ByteRepr, VaArg, FnPtrArg)] +#[byte_size(16)] +pub struct Holder { + #[offset(0)] + #[byte_size(16)] + pub items: Value>, +} +impl Default for Holder { + fn default() -> Self { + { + Holder { + items: Rc::new(RefCell::new( + (0..2).map(|_| ::default()).collect::>(), + )), + } + } + } +} +thread_local!( + pub static kInit_0: Value = Rc::new(RefCell::new(3)); +); +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[byte_size(4)] +pub struct FromStatic { + #[offset(0)] + pub v: i32, +} +impl Default for FromStatic { + fn default() -> Self { + { + FromStatic { + v: kInit_0.with(|rc| *rc.borrow()), + } + } + } +} pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); @@ -41,6 +77,32 @@ fn main_0() -> i32 { let mut s: S = S::new(); assert!((s.a == 11)); assert!(((s.b as i32) == (true as i32))); + let mut h: Ptr = Ptr::alloc_array( + (0..1_usize) + .map(|_| ::default()) + .collect::>(), + ); + assert!( + ({ + (*elem!( + (array_field_ptr!(h.offset((0) as isize), items) as Ptr), + 1 + ) + .upgrade() + .deref()) + .a + } == 11) + ); + h.delete(); + let mut fs: Ptr = Ptr::alloc_array( + (0..2_usize) + .map(|_| ::default()) + .collect::>(), + ); + assert!(({ (*elem!(fs, 1).upgrade().deref()).v } == 3)); + fs.delete(); return 0; } -pub fn __cpp2rust_init_globals() {} +pub fn __cpp2rust_init_globals() { + let _ = kInit_0.with(|_| ()); +} diff --git a/tests/unit/out/refcount/struct_member_array_adjacency.rs b/tests/unit/out/refcount/struct_member_array_adjacency.rs index 4e8444cae..c9e2a3365 100644 --- a/tests/unit/out/refcount/struct_member_array_adjacency.rs +++ b/tests/unit/out/refcount/struct_member_array_adjacency.rs @@ -18,9 +18,11 @@ pub struct pair { } impl Default for pair { fn default() -> Self { - pair { - a: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), - b: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), + { + pair { + a: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), + b: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), + } } } } diff --git a/tests/unit/out/refcount/unboxed_locals.rs b/tests/unit/out/refcount/unboxed_locals.rs index 2ad5f6dcc..c26605f2e 100644 --- a/tests/unit/out/refcount/unboxed_locals.rs +++ b/tests/unit/out/refcount/unboxed_locals.rs @@ -57,7 +57,7 @@ pub struct Square { } impl Default for Square { fn default() -> Self { - Square { side: 2 } + { Square { side: 2 } } } } pub fn main() { diff --git a/tests/unit/out/refcount/unboxed_records.rs b/tests/unit/out/refcount/unboxed_records.rs index 155c04588..86773f30e 100644 --- a/tests/unit/out/refcount/unboxed_records.rs +++ b/tests/unit/out/refcount/unboxed_records.rs @@ -34,12 +34,14 @@ pub struct Cookie { } impl Default for Cookie { fn default() -> Self { - Cookie { - x: 0_i32, - data: Ptr::::null(), - in_: Ptr::::null(), - inner: ::default(), - arr: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), + { + Cookie { + x: 0_i32, + data: Ptr::::null(), + in_: Ptr::::null(), + inner: ::default(), + arr: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), + } } } } @@ -51,7 +53,7 @@ pub struct Counter { } impl Default for Counter { fn default() -> Self { - Counter { n: 0 } + { Counter { n: 0 } } } } pub fn set_0(mut p: Ptr, mut v: i32) { diff --git a/tests/unit/out/refcount/union_addrof_external.rs b/tests/unit/out/refcount/union_addrof_external.rs index 8d27e5a75..c97409ecf 100644 --- a/tests/unit/out/refcount/union_addrof_external.rs +++ b/tests/unit/out/refcount/union_addrof_external.rs @@ -21,11 +21,13 @@ pub struct record { } impl Default for record { fn default() -> Self { - record { - code: 0_u16, - lo: 0_u16, - hi: 0_u32, - pad: Rc::new(RefCell::new((0..8).map(|_| 0_i8).collect::>())), + { + record { + code: 0_u16, + lo: 0_u16, + hi: 0_u32, + pad: Rc::new(RefCell::new((0..8).map(|_| 0_i8).collect::>())), + } } } } diff --git a/tests/unit/out/refcount/union_cross_arm_cast.rs b/tests/unit/out/refcount/union_cross_arm_cast.rs index 4ab7d30b5..50894c063 100644 --- a/tests/unit/out/refcount/union_cross_arm_cast.rs +++ b/tests/unit/out/refcount/union_cross_arm_cast.rs @@ -17,9 +17,11 @@ pub struct shape_a { } impl Default for shape_a { fn default() -> Self { - shape_a { - code: 0_u16, - pad: Rc::new(RefCell::new((0..14).map(|_| 0_i8).collect::>())), + { + shape_a { + code: 0_u16, + pad: Rc::new(RefCell::new((0..14).map(|_| 0_i8).collect::>())), + } } } } @@ -40,12 +42,14 @@ pub struct shape_b { } impl Default for shape_b { fn default() -> Self { - shape_b { - code: 0_u16, - lo: 0_u16, - mid: 0_u32, - fill: Rc::new(RefCell::new((0..16).map(|_| 0_u8).collect::>())), - tail: 0_u32, + { + shape_b { + code: 0_u16, + lo: 0_u16, + mid: 0_u32, + fill: Rc::new(RefCell::new((0..16).map(|_| 0_u8).collect::>())), + tail: 0_u32, + } } } } diff --git a/tests/unit/out/refcount/union_memset_memcpy.rs b/tests/unit/out/refcount/union_memset_memcpy.rs index 4ee2e6d5c..a212ba06f 100644 --- a/tests/unit/out/refcount/union_memset_memcpy.rs +++ b/tests/unit/out/refcount/union_memset_memcpy.rs @@ -17,9 +17,11 @@ pub struct shape_a { } impl Default for shape_a { fn default() -> Self { - shape_a { - code: 0_u16, - pad: Rc::new(RefCell::new((0..14).map(|_| 0_i8).collect::>())), + { + shape_a { + code: 0_u16, + pad: Rc::new(RefCell::new((0..14).map(|_| 0_i8).collect::>())), + } } } } @@ -38,11 +40,13 @@ pub struct shape_b { } impl Default for shape_b { fn default() -> Self { - shape_b { - code: 0_u16, - lo: 0_u16, - hi: 0_u32, - fill: Rc::new(RefCell::new((0..8).map(|_| 0_i8).collect::>())), + { + shape_b { + code: 0_u16, + lo: 0_u16, + hi: 0_u32, + fill: Rc::new(RefCell::new((0..8).map(|_| 0_i8).collect::>())), + } } } } diff --git a/tests/unit/out/refcount/union_nested.rs b/tests/unit/out/refcount/union_nested.rs index 708c07d7d..5a37154fd 100644 --- a/tests/unit/out/refcount/union_nested.rs +++ b/tests/unit/out/refcount/union_nested.rs @@ -17,9 +17,11 @@ pub struct record { } impl Default for record { fn default() -> Self { - record { - code: 0_u16, - pad: Rc::new(RefCell::new((0..14).map(|_| 0_i8).collect::>())), + { + record { + code: 0_u16, + pad: Rc::new(RefCell::new((0..14).map(|_| 0_i8).collect::>())), + } } } } diff --git a/tests/unit/out/unsafe/10_struct.rs b/tests/unit/out/unsafe/10_struct.rs index 8363a3f4c..1d782298c 100644 --- a/tests/unit/out/unsafe/10_struct.rs +++ b/tests/unit/out/unsafe/10_struct.rs @@ -43,8 +43,10 @@ impl Partial { } impl Default for Partial { fn default() -> Self { - Partial { - p: std::ptr::null_mut(), + unsafe { + Partial { + p: std::ptr::null_mut(), + } } } } diff --git a/tests/unit/out/unsafe/array_of_noncopy_struct.rs b/tests/unit/out/unsafe/array_of_noncopy_struct.rs index aa67257f1..5aee9679f 100644 --- a/tests/unit/out/unsafe/array_of_noncopy_struct.rs +++ b/tests/unit/out/unsafe/array_of_noncopy_struct.rs @@ -14,9 +14,11 @@ pub struct NonCopy { } impl Default for NonCopy { fn default() -> Self { - NonCopy { - data: Default::default(), - tag: 0, + unsafe { + NonCopy { + data: Default::default(), + tag: 0, + } } } } diff --git a/tests/unit/out/unsafe/default.rs b/tests/unit/out/unsafe/default.rs index 38a116fab..d473f9efb 100644 --- a/tests/unit/out/unsafe/default.rs +++ b/tests/unit/out/unsafe/default.rs @@ -30,7 +30,7 @@ pub struct BigArray { } impl Default for BigArray { fn default() -> Self { - BigArray { a: [0_i32; 33] } + unsafe { BigArray { a: [0_i32; 33] } } } } pub fn main() { diff --git a/tests/unit/out/unsafe/flex_array_member_incomplete.rs b/tests/unit/out/unsafe/flex_array_member_incomplete.rs index c97a36649..7f9423be6 100644 --- a/tests/unit/out/unsafe/flex_array_member_incomplete.rs +++ b/tests/unit/out/unsafe/flex_array_member_incomplete.rs @@ -14,7 +14,7 @@ pub struct F { } impl Default for F { fn default() -> Self { - F { n: 0_i32, tail: [] } + unsafe { F { n: 0_i32, tail: [] } } } } pub fn main() { diff --git a/tests/unit/out/unsafe/in_class_field_initializer.rs b/tests/unit/out/unsafe/in_class_field_initializer.rs index 9f17cc01c..7a87ba211 100644 --- a/tests/unit/out/unsafe/in_class_field_initializer.rs +++ b/tests/unit/out/unsafe/in_class_field_initializer.rs @@ -14,7 +14,7 @@ pub struct Inner { } impl Default for Inner { fn default() -> Self { - Inner { x: 3, y: 4 } + unsafe { Inner { x: 3, y: 4 } } } } #[repr(C)] @@ -27,11 +27,13 @@ pub struct S { } impl Default for S { fn default() -> Self { - S { - a: 1, - b: (2 as libc::c_char), - c: ::default(), - d: ::default(), + unsafe { + S { + a: 1, + b: (2 as libc::c_char), + c: ::default(), + d: ::default(), + } } } } @@ -49,9 +51,11 @@ impl Boxed_int_ { } impl Default for Boxed_int_ { fn default() -> Self { - Boxed_int_ { - v: 0_i32, - tag: 0_i32, + unsafe { + Boxed_int_ { + v: 0_i32, + tag: 0_i32, + } } } } diff --git a/tests/unit/out/unsafe/init_list.rs b/tests/unit/out/unsafe/init_list.rs index 87a6532e5..f5cd6a11e 100644 --- a/tests/unit/out/unsafe/init_list.rs +++ b/tests/unit/out/unsafe/init_list.rs @@ -26,8 +26,10 @@ impl Holder { } impl Default for Holder { fn default() -> Self { - Holder { - p: Pair { a: 0, b: 0 }, + unsafe { + Holder { + p: Pair { a: 0, b: 0 }, + } } } } diff --git a/tests/unit/out/unsafe/libc_struct_without_default.rs b/tests/unit/out/unsafe/libc_struct_without_default.rs index 46bdece10..907ab0a9c 100644 --- a/tests/unit/out/unsafe/libc_struct_without_default.rs +++ b/tests/unit/out/unsafe/libc_struct_without_default.rs @@ -14,9 +14,11 @@ pub struct UserDefined { } impl Default for UserDefined { fn default() -> Self { - UserDefined { - a: std::array::from_fn::<_, 1, _>(|_| Default::default()).to_vec(), - v: Default::default(), + unsafe { + UserDefined { + a: std::array::from_fn::<_, 1, _>(|_| Default::default()).to_vec(), + v: Default::default(), + } } } } @@ -27,8 +29,10 @@ pub struct FieldIsLibcType { } impl Default for FieldIsLibcType { fn default() -> Self { - FieldIsLibcType { - addr: unsafe { std::mem::zeroed() }, + unsafe { + FieldIsLibcType { + addr: unsafe { std::mem::zeroed() }, + } } } } diff --git a/tests/unit/out/unsafe/offsetof.rs b/tests/unit/out/unsafe/offsetof.rs index 3f743411d..5d26dfe97 100644 --- a/tests/unit/out/unsafe/offsetof.rs +++ b/tests/unit/out/unsafe/offsetof.rs @@ -21,9 +21,11 @@ pub struct Frame { } impl Default for Frame { fn default() -> Self { - Frame { - tag: 0_u16, - body: [(0 as libc::c_char); 64], + unsafe { + Frame { + tag: 0_u16, + body: [(0 as libc::c_char); 64], + } } } } diff --git a/tests/unit/out/unsafe/self_referencing_initializer.rs b/tests/unit/out/unsafe/self_referencing_initializer.rs index c851c493b..fd8e7e538 100644 --- a/tests/unit/out/unsafe/self_referencing_initializer.rs +++ b/tests/unit/out/unsafe/self_referencing_initializer.rs @@ -25,9 +25,11 @@ pub struct node_with_defaults { } impl Default for node_with_defaults { fn default() -> Self { - node_with_defaults { - next: std::ptr::null_mut(), - value: 3, + unsafe { + node_with_defaults { + next: std::ptr::null_mut(), + value: 3, + } } } } diff --git a/tests/unit/out/unsafe/struct_default_ctor.rs b/tests/unit/out/unsafe/struct_default_ctor.rs index 05d75ce9d..bdcc7d4a6 100644 --- a/tests/unit/out/unsafe/struct_default_ctor.rs +++ b/tests/unit/out/unsafe/struct_default_ctor.rs @@ -29,6 +29,26 @@ pub struct Declared { pub v: i32, } impl Declared {} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +pub struct Holder { + pub items: [S; 2], +} +pub static mut kInit_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 3 }); +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg)] +pub struct FromStatic { + pub v: i32, +} +impl Default for FromStatic { + fn default() -> Self { + unsafe { + FromStatic { + v: (*std::cell::LazyCell::force_mut(&mut *&raw mut kInit_0)), + } + } + } +} pub fn main() { unsafe { __cpp2rust_init_globals(); @@ -41,6 +61,42 @@ unsafe fn main_0() -> i32 { let mut s: S = S::new(); assert!(((s.a) == (11))); assert!(((s.b as i32) == (true as i32))); + let mut h: *mut Holder = Box::leak( + (0..1_usize) + .map(|_| ::default()) + .collect::>(), + ) + .as_mut_ptr(); + assert!((((*h.offset((0) as isize)).items[(1) as usize].a) == (11))); + { + let __p = h; + if !__p.is_null() { + ::std::mem::drop(Box::from_raw(::std::slice::from_raw_parts_mut( + __p, + libcc2rs::malloc_usable_size(__p as *mut ::libc::c_void) + / ::std::mem::size_of::(), + ))) + } + }; + let mut fs: *mut FromStatic = Box::leak( + (0..2_usize) + .map(|_| ::default()) + .collect::>(), + ) + .as_mut_ptr(); + assert!((((*fs.offset((1) as isize)).v) == (3))); + { + let __p = fs; + if !__p.is_null() { + ::std::mem::drop(Box::from_raw(::std::slice::from_raw_parts_mut( + __p, + libcc2rs::malloc_usable_size(__p as *mut ::libc::c_void) + / ::std::mem::size_of::(), + ))) + } + }; return 0; } -pub unsafe fn __cpp2rust_init_globals() {} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const kInit_0); +} diff --git a/tests/unit/out/unsafe/unboxed_locals.rs b/tests/unit/out/unsafe/unboxed_locals.rs index 885ce0db3..3cb5a7c08 100644 --- a/tests/unit/out/unsafe/unboxed_locals.rs +++ b/tests/unit/out/unsafe/unboxed_locals.rs @@ -54,7 +54,7 @@ pub struct Square { } impl Default for Square { fn default() -> Self { - Square { side: 2 } + unsafe { Square { side: 2 } } } } pub fn main() { diff --git a/tests/unit/out/unsafe/unboxed_records.rs b/tests/unit/out/unsafe/unboxed_records.rs index 8dde2ec53..b26a093eb 100644 --- a/tests/unit/out/unsafe/unboxed_records.rs +++ b/tests/unit/out/unsafe/unboxed_records.rs @@ -33,7 +33,7 @@ impl Counter { } impl Default for Counter { fn default() -> Self { - Counter { n: 0 } + unsafe { Counter { n: 0 } } } } pub unsafe fn set_0(mut p: *mut i32, mut v: i32) {