From 13140c97bc15842668d2e7f5419596a4095b89df Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 27 Apr 2026 22:08:14 +0100 Subject: [PATCH 01/19] Handle anonymous structs inside records --- cpp2rust/converter/mapper.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/cpp2rust/converter/mapper.cpp b/cpp2rust/converter/mapper.cpp index 425cf805..f60abcc1 100644 --- a/cpp2rust/converter/mapper.cpp +++ b/cpp2rust/converter/mapper.cpp @@ -735,6 +735,17 @@ std::string ToString(clang::QualType qual_type) { return normalizeTranslationRule(type); } +static std::string synthesizeAnonRecordName(const clang::RecordDecl *record) { + std::string parent_name; + if (auto *parent = + clang::dyn_cast(record->getDeclContext())) { + parent_name = + parent->getIdentifier() ? parent->getIdentifier()->getName().str(); + : synthesizeAnonRecordName(parent); + } + return std::format("{}_anon_{}", parent_name, GetAnonIndex(record)); +} + std::string ToString(const clang::NamedDecl *decl) { if (auto *record = clang::dyn_cast(decl); record && !record->getIdentifier()) { From 796a4549eaf3c1ac0d6ba32fcee0e3ee2c891c9c Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 27 Apr 2026 22:21:11 +0100 Subject: [PATCH 02/19] Fix build --- cpp2rust/converter/mapper.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cpp2rust/converter/mapper.cpp b/cpp2rust/converter/mapper.cpp index f60abcc1..371d7d83 100644 --- a/cpp2rust/converter/mapper.cpp +++ b/cpp2rust/converter/mapper.cpp @@ -739,8 +739,8 @@ static std::string synthesizeAnonRecordName(const clang::RecordDecl *record) { std::string parent_name; if (auto *parent = clang::dyn_cast(record->getDeclContext())) { - parent_name = - parent->getIdentifier() ? parent->getIdentifier()->getName().str(); + parent_name = parent->getIdentifier() + ? parent->getIdentifier()->getName().str() : synthesizeAnonRecordName(parent); } return std::format("{}_anon_{}", parent_name, GetAnonIndex(record)); From f86279159d45439126d29d95855bbcfc28767a74 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 28 Apr 2026 09:17:02 +0100 Subject: [PATCH 03/19] Add anonymous struct tests --- tests/unit/out/refcount/anonymous-struct.rs | 30 ------------------- tests/unit/out/refcount/anonymous-struct_c.rs | 21 ------------- tests/unit/out/unsafe/anonymous-struct.rs | 20 ------------- tests/unit/out/unsafe/anonymous-struct_c.rs | 20 ------------- 4 files changed, 91 deletions(-) diff --git a/tests/unit/out/refcount/anonymous-struct.rs b/tests/unit/out/refcount/anonymous-struct.rs index 980aff17..4fad0525 100644 --- a/tests/unit/out/refcount/anonymous-struct.rs +++ b/tests/unit/out/refcount/anonymous-struct.rs @@ -197,35 +197,5 @@ fn main_0() -> i32 { .borrow()) == 11) ); - #[derive(Default)] - pub struct anon_0 { - pub x: Value, - pub z: Value, - } - impl Clone for anon_0 { - fn clone(&self) -> Self { - let mut this = Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - z: Rc::new(RefCell::new((*self.z.borrow()))), - }; - this - } - } - impl ByteRepr for anon_0 {}; - let s: Value = Rc::new(RefCell::new(::default())); - (*(*s.borrow()).x.borrow_mut()) = 1; - (*(*s.borrow()).z.borrow_mut()) = 2; - assert!( - ({ - (*(*s.borrow()).x.borrow_mut()) = 1; - (*(*s.borrow()).x.borrow()) - } != 0) - ); - assert!( - ({ - (*(*s.borrow()).z.borrow_mut()) = 2; - (*(*s.borrow()).z.borrow()) - } != 0) - ); return 0; } diff --git a/tests/unit/out/refcount/anonymous-struct_c.rs b/tests/unit/out/refcount/anonymous-struct_c.rs index ab870b0a..c4987572 100644 --- a/tests/unit/out/refcount/anonymous-struct_c.rs +++ b/tests/unit/out/refcount/anonymous-struct_c.rs @@ -106,26 +106,5 @@ fn main_0() -> i32 { .borrow()) == 11) ); - #[derive(Default)] - pub struct anon_0 { - pub x: Value, - pub z: Value, - } - impl ByteRepr for anon_0 {}; - let s: Value = >::default(); - (*(*s.borrow()).x.borrow_mut()) = 1; - (*(*s.borrow()).z.borrow_mut()) = 2; - assert!( - ({ - (*(*s.borrow()).x.borrow_mut()) = 1; - (*(*s.borrow()).x.borrow()) - } != 0) - ); - assert!( - ({ - (*(*s.borrow()).z.borrow_mut()) = 2; - (*(*s.borrow()).z.borrow()) - } != 0) - ); return 0; } diff --git a/tests/unit/out/unsafe/anonymous-struct.rs b/tests/unit/out/unsafe/anonymous-struct.rs index 6b95a9f3..ecbd9834 100644 --- a/tests/unit/out/unsafe/anonymous-struct.rs +++ b/tests/unit/out/unsafe/anonymous-struct.rs @@ -87,25 +87,5 @@ unsafe fn main_0() -> i32 { assert!(((o.anon_3.i) == (9))); assert!(((o.anon_3.inner_named.j) == (10))); assert!(((o.anon_3.anon_1.k) == (11))); - #[derive(Copy, Clone, Default)] - pub struct anon_0 { - pub x: i32, - pub z: i32, - }; - let mut s: anon_0 = ::default(); - s.x = 1; - s.z = 2; - assert!( - ({ - s.x = 1; - s.x - } != 0) - ); - assert!( - ({ - s.z = 2; - s.z - } != 0) - ); return 0; } diff --git a/tests/unit/out/unsafe/anonymous-struct_c.rs b/tests/unit/out/unsafe/anonymous-struct_c.rs index 578d1f81..1c2f72ed 100644 --- a/tests/unit/out/unsafe/anonymous-struct_c.rs +++ b/tests/unit/out/unsafe/anonymous-struct_c.rs @@ -83,25 +83,5 @@ unsafe fn main_0() -> i32 { assert!(((o.anon_3.i) == (9))); assert!(((o.anon_3.inner_named.j) == (10))); assert!(((o.anon_3.anon_1.k) == (11))); - #[derive(Copy, Clone, Default)] - pub struct anon_0 { - pub x: i32, - pub z: i32, - }; - let mut s: anon_0 = ::default(); - s.x = 1; - s.z = 2; - assert!( - ({ - s.x = 1; - s.x - } != 0) - ); - assert!( - ({ - s.z = 2; - s.z - } != 0) - ); return 0; } From d6f77488a01ad1fdcbf90a253bd7df5193d657b2 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 28 Apr 2026 09:45:48 +0100 Subject: [PATCH 04/19] Update tests --- tests/unit/out/refcount/anonymous-struct.rs | 30 +++++++++++++++++++ tests/unit/out/refcount/anonymous-struct_c.rs | 21 +++++++++++++ tests/unit/out/unsafe/anonymous-struct.rs | 20 +++++++++++++ tests/unit/out/unsafe/anonymous-struct_c.rs | 20 +++++++++++++ 4 files changed, 91 insertions(+) diff --git a/tests/unit/out/refcount/anonymous-struct.rs b/tests/unit/out/refcount/anonymous-struct.rs index 4fad0525..980aff17 100644 --- a/tests/unit/out/refcount/anonymous-struct.rs +++ b/tests/unit/out/refcount/anonymous-struct.rs @@ -197,5 +197,35 @@ fn main_0() -> i32 { .borrow()) == 11) ); + #[derive(Default)] + pub struct anon_0 { + pub x: Value, + pub z: Value, + } + impl Clone for anon_0 { + fn clone(&self) -> Self { + let mut this = Self { + x: Rc::new(RefCell::new((*self.x.borrow()))), + z: Rc::new(RefCell::new((*self.z.borrow()))), + }; + this + } + } + impl ByteRepr for anon_0 {}; + let s: Value = Rc::new(RefCell::new(::default())); + (*(*s.borrow()).x.borrow_mut()) = 1; + (*(*s.borrow()).z.borrow_mut()) = 2; + assert!( + ({ + (*(*s.borrow()).x.borrow_mut()) = 1; + (*(*s.borrow()).x.borrow()) + } != 0) + ); + assert!( + ({ + (*(*s.borrow()).z.borrow_mut()) = 2; + (*(*s.borrow()).z.borrow()) + } != 0) + ); return 0; } diff --git a/tests/unit/out/refcount/anonymous-struct_c.rs b/tests/unit/out/refcount/anonymous-struct_c.rs index c4987572..ab870b0a 100644 --- a/tests/unit/out/refcount/anonymous-struct_c.rs +++ b/tests/unit/out/refcount/anonymous-struct_c.rs @@ -106,5 +106,26 @@ fn main_0() -> i32 { .borrow()) == 11) ); + #[derive(Default)] + pub struct anon_0 { + pub x: Value, + pub z: Value, + } + impl ByteRepr for anon_0 {}; + let s: Value = >::default(); + (*(*s.borrow()).x.borrow_mut()) = 1; + (*(*s.borrow()).z.borrow_mut()) = 2; + assert!( + ({ + (*(*s.borrow()).x.borrow_mut()) = 1; + (*(*s.borrow()).x.borrow()) + } != 0) + ); + assert!( + ({ + (*(*s.borrow()).z.borrow_mut()) = 2; + (*(*s.borrow()).z.borrow()) + } != 0) + ); return 0; } diff --git a/tests/unit/out/unsafe/anonymous-struct.rs b/tests/unit/out/unsafe/anonymous-struct.rs index ecbd9834..6b95a9f3 100644 --- a/tests/unit/out/unsafe/anonymous-struct.rs +++ b/tests/unit/out/unsafe/anonymous-struct.rs @@ -87,5 +87,25 @@ unsafe fn main_0() -> i32 { assert!(((o.anon_3.i) == (9))); assert!(((o.anon_3.inner_named.j) == (10))); assert!(((o.anon_3.anon_1.k) == (11))); + #[derive(Copy, Clone, Default)] + pub struct anon_0 { + pub x: i32, + pub z: i32, + }; + let mut s: anon_0 = ::default(); + s.x = 1; + s.z = 2; + assert!( + ({ + s.x = 1; + s.x + } != 0) + ); + assert!( + ({ + s.z = 2; + s.z + } != 0) + ); return 0; } diff --git a/tests/unit/out/unsafe/anonymous-struct_c.rs b/tests/unit/out/unsafe/anonymous-struct_c.rs index 1c2f72ed..578d1f81 100644 --- a/tests/unit/out/unsafe/anonymous-struct_c.rs +++ b/tests/unit/out/unsafe/anonymous-struct_c.rs @@ -83,5 +83,25 @@ unsafe fn main_0() -> i32 { assert!(((o.anon_3.i) == (9))); assert!(((o.anon_3.inner_named.j) == (10))); assert!(((o.anon_3.anon_1.k) == (11))); + #[derive(Copy, Clone, Default)] + pub struct anon_0 { + pub x: i32, + pub z: i32, + }; + let mut s: anon_0 = ::default(); + s.x = 1; + s.z = 2; + assert!( + ({ + s.x = 1; + s.x + } != 0) + ); + assert!( + ({ + s.z = 2; + s.z + } != 0) + ); return 0; } From d84745a73515bbdebb2afff035b2cf57d97c11f0 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 28 Apr 2026 10:06:03 +0100 Subject: [PATCH 05/19] Update expected output --- .../unit/out/refcount/union_cross_arm_cast.rs | 80 +++++++++++++ .../out/refcount/union_field_alignment.rs | 30 +++++ .../out/refcount/union_flex_array_member.rs | 73 ++++++++++++ .../unit/out/refcount/union_memset_memcpy.rs | 107 ++++++++++++++++++ tests/unit/out/refcount/union_nested.rs | 78 +++++++++++++ .../out/refcount/union_pointer_pun_address.rs | 41 +++++++ .../out/refcount/union_struct_dual_use.rs | 68 +++++++++++ .../unit/out/unsafe/union_addrof_external.rs | 81 +++++++++++++ tests/unit/out/unsafe/union_cross_arm_cast.rs | 62 ++++++++++ .../unit/out/unsafe/union_field_alignment.rs | 30 +++++ .../out/unsafe/union_flex_array_member.rs | 52 +++++++++ tests/unit/out/unsafe/union_memset_memcpy.rs | 85 ++++++++++++++ tests/unit/out/unsafe/union_nested.rs | 60 ++++++++++ .../unit/out/unsafe/union_struct_dual_use.rs | 60 ++++++++++ .../out/unsafe/union_tagged_struct_arms.rs | 86 ++++++++++++++ 15 files changed, 993 insertions(+) create mode 100644 tests/unit/out/refcount/union_cross_arm_cast.rs create mode 100644 tests/unit/out/refcount/union_field_alignment.rs create mode 100644 tests/unit/out/refcount/union_flex_array_member.rs create mode 100644 tests/unit/out/refcount/union_memset_memcpy.rs create mode 100644 tests/unit/out/refcount/union_nested.rs create mode 100644 tests/unit/out/refcount/union_pointer_pun_address.rs create mode 100644 tests/unit/out/refcount/union_struct_dual_use.rs create mode 100644 tests/unit/out/unsafe/union_addrof_external.rs create mode 100644 tests/unit/out/unsafe/union_cross_arm_cast.rs create mode 100644 tests/unit/out/unsafe/union_field_alignment.rs create mode 100644 tests/unit/out/unsafe/union_flex_array_member.rs create mode 100644 tests/unit/out/unsafe/union_memset_memcpy.rs create mode 100644 tests/unit/out/unsafe/union_nested.rs create mode 100644 tests/unit/out/unsafe/union_struct_dual_use.rs create mode 100644 tests/unit/out/unsafe/union_tagged_struct_arms.rs diff --git a/tests/unit/out/refcount/union_cross_arm_cast.rs b/tests/unit/out/refcount/union_cross_arm_cast.rs new file mode 100644 index 00000000..20e0d2eb --- /dev/null +++ b/tests/unit/out/refcount/union_cross_arm_cast.rs @@ -0,0 +1,80 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +#[derive()] +pub struct shape_a { + pub code: Value, + pub pad: Value>, +} +impl ByteRepr for shape_a {} +#[derive()] +pub struct shape_b { + pub code: Value, + pub lo: Value, + pub mid: Value, + pub fill: Value>, + pub tail: Value, +} +impl ByteRepr for shape_b {} +#[derive()] +pub struct Container_anon_0 { + pub a: Value, + pub b: Value, + pub raw_: Value>, +} +impl ByteRepr for Container_anon_0 {} +#[derive(Default)] +pub struct Container { + pub len: Value, + pub u: Value, +} +impl ByteRepr for Container {} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let c: Value = >::default(); + { + ((c.as_pointer()) as Ptr).to_any().memset( + (0) as u8, + ::std::mem::size_of::() as u64 as usize, + ); + ((c.as_pointer()) as Ptr).to_any().clone() + }; + (*(*(*(*c.borrow()).u.borrow()).a.borrow()).code.borrow_mut()) = 10_u16; + (*(*c.borrow()).len.borrow_mut()) = (::std::mem::size_of::() as u64 as u32); + (*(*((((*(*c.borrow()).u.borrow()).a.as_pointer()) + .to_strong() + .as_pointer() as AnyPtr) + .cast::() + .expect("ub:wrong type")) + .upgrade() + .deref()) + .tail + .borrow_mut()) = 3735928559_u32; + assert!(((*(*(*(*c.borrow()).u.borrow()).b.borrow()).tail.borrow()) == 3735928559_u32)); + assert!((((*(*(*(*c.borrow()).u.borrow()).b.borrow()).code.borrow()) as i32) == 10)); + (*(*(*(*c.borrow()).u.borrow()).b.borrow()).lo.borrow_mut()) = 8080_u16; + assert!( + ((((((*(*c.borrow()).u.borrow()).raw_.as_pointer()) + .to_strong() + .as_pointer() as Ptr::) + .offset((2) as isize) + .read()) as i32) + == 144) + ); + assert!( + ((((((*(*c.borrow()).u.borrow()).raw_.as_pointer()) + .to_strong() + .as_pointer() as Ptr::) + .offset((3) as isize) + .read()) as i32) + == 31) + ); + return 0; +} diff --git a/tests/unit/out/refcount/union_field_alignment.rs b/tests/unit/out/refcount/union_field_alignment.rs new file mode 100644 index 00000000..3601f78e --- /dev/null +++ b/tests/unit/out/refcount/union_field_alignment.rs @@ -0,0 +1,30 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +#[derive()] +pub struct node_anon_0 { + pub bytes: Value>, + pub aligner: Value, +} +impl ByteRepr for node_anon_0 {} +#[derive(Default)] +pub struct node { + pub next: Value>, + pub x: Value, +} +impl ByteRepr for node {} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let n: Value = >::default(); + (*(*n.borrow()).next.borrow_mut()) = Default::default(); + (*(*(*n.borrow()).x.borrow()).bytes.borrow_mut())[(0) as usize] = 171_u8; + assert!((((*(*(*n.borrow()).x.borrow()).bytes.borrow())[(0) as usize] as i32) == 171)); + return 0; +} diff --git a/tests/unit/out/refcount/union_flex_array_member.rs b/tests/unit/out/refcount/union_flex_array_member.rs new file mode 100644 index 00000000..56d7ef0e --- /dev/null +++ b/tests/unit/out/refcount/union_flex_array_member.rs @@ -0,0 +1,73 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +#[derive()] +pub struct node_anon_0 { + pub bytes: Value>, + pub aligner: Value, +} +impl ByteRepr for node_anon_0 {} +#[derive(Default)] +pub struct node { + pub len: Value, + pub x: Value, +} +impl ByteRepr for node {} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let tail_size: Value = Rc::new(RefCell::new(32_u64)); + let n: Value> = Rc::new(RefCell::new( + ({ + let ___size: u64 = + (::std::mem::size_of::() as u64 as u64).wrapping_add((*tail_size.borrow())); + malloc_0(___size) + }) + .cast::() + .expect("ub:wrong type"), + )); + (*(*(*n.borrow()).upgrade().deref()).len.borrow_mut()) = (*tail_size.borrow()); + let i: Value = Rc::new(RefCell::new(0_u64)); + 'loop_: while ((*i.borrow()) < (*tail_size.borrow())) { + let __rhs = ((((*i.borrow()) & 255_u64) as u64) as u8); + (*(*(*(*n.borrow()).upgrade().deref()).x.borrow()) + .bytes + .borrow_mut())[(*i.borrow()) as usize] = __rhs; + (*i.borrow_mut()).postfix_inc(); + } + let i: Value = Rc::new(RefCell::new(0_u64)); + 'loop_: while ((*i.borrow()) < (*tail_size.borrow())) { + assert!({ + let _lhs = ((*(*(*(*n.borrow()).upgrade().deref()).x.borrow()) + .bytes + .borrow())[(*i.borrow()) as usize] as i32); + _lhs == (((((*i.borrow()) & 255_u64) as u64) as u8) as i32) + }); + (*i.borrow_mut()).postfix_inc(); + } + let p: Value> = Rc::new(RefCell::new( + (((*(*(*n.borrow()).upgrade().deref()).x.borrow()) + .bytes + .as_pointer() as Ptr) + .offset(10 as isize)), + )); + assert!(((((*p.borrow()).read()) as i32) == 10)); + (*p.borrow()).write(170_u8); + assert!( + (((*(*(*(*n.borrow()).upgrade().deref()).x.borrow()) + .bytes + .borrow())[(10) as usize] as i32) + == 170) + ); + ({ + let ___ptr: AnyPtr = ((*n.borrow()).clone() as Ptr).to_any(); + free_1(___ptr) + }); + return 0; +} diff --git a/tests/unit/out/refcount/union_memset_memcpy.rs b/tests/unit/out/refcount/union_memset_memcpy.rs new file mode 100644 index 00000000..fbb55430 --- /dev/null +++ b/tests/unit/out/refcount/union_memset_memcpy.rs @@ -0,0 +1,107 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +#[derive()] +pub struct shape_a { + pub code: Value, + pub pad: Value>, +} +impl ByteRepr for shape_a {} +#[derive()] +pub struct shape_b { + pub code: Value, + pub lo: Value, + pub hi: Value, + pub fill: Value>, +} +impl ByteRepr for shape_b {} +#[derive()] +pub struct Container_anon_0 { + pub a: Value, + pub b: Value, + pub raw_: Value>, +} +impl ByteRepr for Container_anon_0 {} +#[derive(Default)] +pub struct Container { + pub view: Value, +} +impl ByteRepr for Container {} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let c: Value = >::default(); + { + ((c.as_pointer()) as Ptr).to_any().memset( + (0) as u8, + ::std::mem::size_of::() as u64 as usize, + ); + ((c.as_pointer()) as Ptr).to_any().clone() + }; + assert!((((*(*(*(*c.borrow()).view.borrow()).a.borrow()).code.borrow()) as i32) == 0)); + assert!((((*(*(*(*c.borrow()).view.borrow()).b.borrow()).lo.borrow()) as i32) == 0)); + assert!((((*(*(*c.borrow()).view.borrow()).raw_.borrow())[(0) as usize] as i32) == 0)); + assert!((((*(*(*c.borrow()).view.borrow()).raw_.borrow())[(255) as usize] as i32) == 0)); + let src: Value> = Rc::new(RefCell::new(Box::new([ + 0_u8, + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ]))); + (*src.borrow_mut())[(0) as usize] = 2_u8; + (*src.borrow_mut())[(2) as usize] = 80_u8; + (*src.borrow_mut())[(3) as usize] = 0_u8; + (*src.borrow_mut())[(4) as usize] = 127_u8; + (*src.borrow_mut())[(5) as usize] = 0_u8; + (*src.borrow_mut())[(6) as usize] = 0_u8; + (*src.borrow_mut())[(7) as usize] = 1_u8; + let len: Value = Rc::new(RefCell::new(16_u64)); + assert!(((*len.borrow()) <= ::std::mem::size_of::<[u8; 256]>() as u64)); + { + (((*(*c.borrow()).view.borrow()).raw_.as_pointer()) as Ptr) + .to_any() + .memcpy( + &((src.as_pointer() as Ptr) as Ptr).to_any(), + (*len.borrow()) as usize, + ); + (((*(*c.borrow()).view.borrow()).raw_.as_pointer()) as Ptr) + .to_any() + .clone() + }; + assert!((((*(*(*(*c.borrow()).view.borrow()).b.borrow()).code.borrow()) as i32) == 2)); + assert!( + ((((((*(*(*c.borrow()).view.borrow()).b.borrow()).lo.as_pointer()) + .to_strong() + .as_pointer() as Ptr::) + .offset((0) as isize) + .read()) as i32) + == 80) + ); + { + ((c.as_pointer()) as Ptr).to_any().memset( + (0) as u8, + ::std::mem::size_of::() as u64 as usize, + ); + ((c.as_pointer()) as Ptr).to_any().clone() + }; + assert!((((*(*(*(*c.borrow()).view.borrow()).b.borrow()).code.borrow()) as i32) == 0)); + return 0; +} diff --git a/tests/unit/out/refcount/union_nested.rs b/tests/unit/out/refcount/union_nested.rs new file mode 100644 index 00000000..d8322734 --- /dev/null +++ b/tests/unit/out/refcount/union_nested.rs @@ -0,0 +1,78 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +#[derive()] +pub struct record { + pub code: Value, + pub pad: Value>, +} +impl ByteRepr for record {} +#[derive()] +pub struct inner_anon_0 { + pub h: Value, + pub raw_: Value>, +} +impl ByteRepr for inner_anon_0 {} +#[derive(Default)] +pub struct inner { + pub view: Value, +} +impl ByteRepr for inner {} +#[derive(Default)] +pub struct Outer_anon_0 { + pub h: Value, + pub nested: Value, +} +impl ByteRepr for Outer_anon_0 {} +#[derive(Default)] +pub struct Outer { + pub kind: Value, + pub level: Value, + pub variant: Value, + pub len: Value, + pub body: Value, +} +impl ByteRepr for Outer {} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let ex: Value = >::default(); + { + ((ex.as_pointer()) as Ptr) + .to_any() + .memset((0) as u8, ::std::mem::size_of::() as u64 as usize); + ((ex.as_pointer()) as Ptr).to_any().clone() + }; + (*(*ex.borrow()).kind.borrow_mut()) = 2; + (*(*ex.borrow()).level.borrow_mut()) = 1; + (*(*ex.borrow()).variant.borrow_mut()) = 6; + (*(*ex.borrow()).len.borrow_mut()) = (::std::mem::size_of::() as u64 as u32); + (*(*(*(*ex.borrow()).body.borrow()).h.borrow()) + .code + .borrow_mut()) = 2_u16; + (*(*(*(*ex.borrow()).body.borrow()).h.borrow()) + .pad + .borrow_mut())[(0) as usize] = (('X' as i32) as u8); + assert!((((*(*(*(*ex.borrow()).body.borrow()).h.borrow()).code.borrow()) as i32) == 2)); + assert!( + (((*(*(*(*ex.borrow()).body.borrow()).h.borrow()).pad.borrow())[(0) as usize] as i32) + == ('X' as i32)) + ); + assert!( + (((*(*(*(*(*(*ex.borrow()).body.borrow()).nested.borrow()) + .view + .borrow()) + .h + .borrow()) + .code + .borrow()) as i32) + == 2) + ); + return 0; +} diff --git a/tests/unit/out/refcount/union_pointer_pun_address.rs b/tests/unit/out/refcount/union_pointer_pun_address.rs new file mode 100644 index 00000000..bbe33b8a --- /dev/null +++ b/tests/unit/out/refcount/union_pointer_pun_address.rs @@ -0,0 +1,41 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +#[derive(Default)] +pub struct node_a { + pub n: Value, +} +impl ByteRepr for node_a {} +#[derive(Default)] +pub struct node_b { + pub data: Value, + pub next: Value>, +} +impl ByteRepr for node_b {} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let a: Value = Rc::new(RefCell::new(node_a { + n: Rc::new(RefCell::new(123)), + })); + #[derive(Default)] + pub struct anon_0 { + pub to_a: Value>, + pub to_b: Value>, + } + impl ByteRepr for anon_0 {}; + let ptr: Value = >::default(); + (*(*ptr.borrow()).to_a.borrow_mut()) = (a.as_pointer()); + let out: Value> = Rc::new(RefCell::new((*(*ptr.borrow()).to_b.borrow()).clone())); + assert!({ + let _lhs = ((*out.borrow()).to_strong().as_pointer() as AnyPtr).clone(); + _lhs == ((a.as_pointer()).to_strong().as_pointer() as AnyPtr) + }); + return 0; +} diff --git a/tests/unit/out/refcount/union_struct_dual_use.rs b/tests/unit/out/refcount/union_struct_dual_use.rs new file mode 100644 index 00000000..2e1989c3 --- /dev/null +++ b/tests/unit/out/refcount/union_struct_dual_use.rs @@ -0,0 +1,68 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +#[derive(Default)] +pub struct Inner { + pub a: Value, + pub b: Value, +} +impl ByteRepr for Inner {} +pub fn sum_inner_0(i: Ptr) -> i32 { + let i: Value> = Rc::new(RefCell::new(i)); + return { + let _lhs = (*(*(*i.borrow()).upgrade().deref()).a.borrow()); + _lhs + (*(*(*i.borrow()).upgrade().deref()).b.borrow()) + }; +} +#[derive()] +pub struct Outer_anon_0 { + pub inner: Value, + pub raw_: Value>, +} +impl ByteRepr for Outer_anon_0 {} +#[derive(Default)] +pub struct Outer { + pub u: Value, +} +impl ByteRepr for Outer {} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let standalone: Value = >::default(); + (*(*standalone.borrow()).a.borrow_mut()) = 3; + (*(*standalone.borrow()).b.borrow_mut()) = 4; + assert!( + (({ + let _i: Ptr = (standalone.as_pointer()); + sum_inner_0(_i) + }) == 7) + ); + let outer: Value = >::default(); + { + ((outer.as_pointer()) as Ptr) + .to_any() + .memset((0) as u8, ::std::mem::size_of::() as u64 as usize); + ((outer.as_pointer()) as Ptr).to_any().clone() + }; + (*(*(*(*outer.borrow()).u.borrow()).inner.borrow()) + .a + .borrow_mut()) = 3; + (*(*(*(*outer.borrow()).u.borrow()).inner.borrow()) + .b + .borrow_mut()) = 4; + assert!( + (({ + let _i: Ptr = ((*(*outer.borrow()).u.borrow()).inner.as_pointer()); + sum_inner_0(_i) + }) == 7) + ); + assert!(((((*(*(*outer.borrow()).u.borrow()).raw_.borrow())[(0) as usize] as u8) as i32) == 3)); + assert!(((((*(*(*outer.borrow()).u.borrow()).raw_.borrow())[(4) as usize] as u8) as i32) == 4)); + return 0; +} diff --git a/tests/unit/out/unsafe/union_addrof_external.rs b/tests/unit/out/unsafe/union_addrof_external.rs new file mode 100644 index 00000000..9181620f --- /dev/null +++ b/tests/unit/out/unsafe/union_addrof_external.rs @@ -0,0 +1,81 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +#[derive(Copy, Clone)] +pub struct record { + pub code: u16, + pub lo: u16, + pub hi: u32, + pub pad: [u8; 8], +} +#[derive(Copy, Clone)] +pub struct Container_anon_0 { + pub h: record, + pub raw_: [u8; 128], +} +#[derive(Copy, Clone, Default)] +pub struct Container { + pub view: Container_anon_0, +} +pub unsafe fn fill_0(mut out: *mut ::libc::c_void, mut cap: u64) { + let mut src: [u8; 16] = [ + 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, + 0_u8, + ]; + src[(0) as usize] = 2_u8; + src[(1) as usize] = 0_u8; + src[(2) as usize] = 0_u8; + src[(3) as usize] = 80_u8; + src[(4) as usize] = 127_u8; + src[(5) as usize] = 0_u8; + src[(6) as usize] = 0_u8; + src[(7) as usize] = 1_u8; + let mut n: u64 = if ((::std::mem::size_of::<[u8; 16]>() as u64) < (cap)) { + ::std::mem::size_of::<[u8; 16]>() as u64 + } else { + cap + }; + { + if n != 0 { + ::std::ptr::copy_nonoverlapping( + (src.as_mut_ptr() as *const u8 as *const ::libc::c_void), + out, + n as usize, + ) + } + out + }; +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut c: Container = ::default(); + { + let byte_0 = + ((&mut c as *mut Container) as *mut Container as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::() as u64 { + *byte_0.offset(offset as isize) = 0 as u8; + } + ((&mut c as *mut Container) as *mut Container as *mut ::libc::c_void) + }; + (unsafe { + let _out: *mut ::libc::c_void = + ((&mut c.view as *mut Container_anon_0) as *mut ::libc::c_void); + let _cap: u64 = ::std::mem::size_of::() as u64; + fill_0(_out, _cap) + }); + assert!(((c.view.h.code as i32) == (2))); + assert!((((*((&mut c.view.h.lo as *mut u16) as *mut u8).offset((0) as isize)) as i32) == (0))); + assert!((((*((&mut c.view.h.lo as *mut u16) as *mut u8).offset((1) as isize)) as i32) == (80))); + assert!(((c.view.raw_[(0) as usize] as i32) == (2))); + assert!((((c.view.raw_[(3) as usize] as u8) as i32) == (80))); + return 0; +} diff --git a/tests/unit/out/unsafe/union_cross_arm_cast.rs b/tests/unit/out/unsafe/union_cross_arm_cast.rs new file mode 100644 index 00000000..d93385ce --- /dev/null +++ b/tests/unit/out/unsafe/union_cross_arm_cast.rs @@ -0,0 +1,62 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +#[derive(Copy, Clone)] +pub struct shape_a { + pub code: u16, + pub pad: [u8; 14], +} +#[derive(Copy, Clone)] +pub struct shape_b { + pub code: u16, + pub lo: u16, + pub mid: u32, + pub fill: [u8; 16], + pub tail: u32, +} +#[derive(Copy, Clone)] +pub struct Container_anon_0 { + pub a: shape_a, + pub b: shape_b, + pub raw_: [u8; 64], +} +#[derive(Copy, Clone, Default)] +pub struct Container { + pub len: u32, + pub u: Container_anon_0, +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut c: Container = ::default(); + { + let byte_0 = + ((&mut c as *mut Container) as *mut Container as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::() as u64 { + *byte_0.offset(offset as isize) = 0 as u8; + } + ((&mut c as *mut Container) as *mut Container as *mut ::libc::c_void) + }; + c.u.a.code = 10_u16; + c.len = (::std::mem::size_of::() as u64 as u32); + (*(((&mut c.u.a as *mut shape_a) as *mut ::libc::c_void) as *mut shape_b)).tail = + 3735928559_u32; + assert!(((c.u.b.tail) == (3735928559_u32))); + assert!(((c.u.b.code as i32) == (10))); + c.u.b.lo = 8080_u16; + assert!( + (((*((&mut c.u.raw_ as *mut [u8; 64]) as *mut u8).offset((2) as isize)) as i32) == (144)) + ); + assert!( + (((*((&mut c.u.raw_ as *mut [u8; 64]) as *mut u8).offset((3) as isize)) as i32) == (31)) + ); + return 0; +} diff --git a/tests/unit/out/unsafe/union_field_alignment.rs b/tests/unit/out/unsafe/union_field_alignment.rs new file mode 100644 index 00000000..8441c7d4 --- /dev/null +++ b/tests/unit/out/unsafe/union_field_alignment.rs @@ -0,0 +1,30 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +#[derive(Copy, Clone)] +pub struct node_anon_0 { + pub bytes: [u8; 1], + pub aligner: *mut ::libc::c_void, +} +#[derive(Copy, Clone, Default)] +pub struct node { + pub next: *mut node, + pub x: node_anon_0, +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut n: node = ::default(); + n.next = Default::default(); + n.x.bytes[(0) as usize] = 171_u8; + assert!(((n.x.bytes[(0) as usize] as i32) == (171))); + return 0; +} diff --git a/tests/unit/out/unsafe/union_flex_array_member.rs b/tests/unit/out/unsafe/union_flex_array_member.rs new file mode 100644 index 00000000..fcf94b81 --- /dev/null +++ b/tests/unit/out/unsafe/union_flex_array_member.rs @@ -0,0 +1,52 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +#[derive(Copy, Clone)] +pub struct node_anon_0 { + pub bytes: [u8; 1], + pub aligner: *mut ::libc::c_void, +} +#[derive(Copy, Clone, Default)] +pub struct node { + pub len: u64, + pub x: node_anon_0, +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut tail_size: u64 = 32_u64; + let mut n: *mut node = ((unsafe { + let ___size: u64 = (::std::mem::size_of::() as u64 as u64).wrapping_add(tail_size); + malloc_0(___size) + }) as *mut node); + (*n).len = tail_size; + let mut i: u64 = 0_u64; + 'loop_: while ((i) < (tail_size)) { + (*n).x.bytes[(i) as usize] = ((((i) & (255_u64)) as u64) as u8); + i.postfix_inc(); + } + let mut i: u64 = 0_u64; + 'loop_: while ((i) < (tail_size)) { + assert!( + (((*n).x.bytes[(i) as usize] as i32) == (((((i) & (255_u64)) as u64) as u8) as i32)) + ); + i.postfix_inc(); + } + let mut p: *mut u8 = (&mut (*n).x.bytes[(10) as usize] as *mut u8); + assert!((((*p) as i32) == (10))); + (*p) = 170_u8; + assert!((((*n).x.bytes[(10) as usize] as i32) == (170))); + (unsafe { + let ___ptr: *mut ::libc::c_void = (n as *mut node as *mut ::libc::c_void); + free_1(___ptr) + }); + return 0; +} diff --git a/tests/unit/out/unsafe/union_memset_memcpy.rs b/tests/unit/out/unsafe/union_memset_memcpy.rs new file mode 100644 index 00000000..47354e9d --- /dev/null +++ b/tests/unit/out/unsafe/union_memset_memcpy.rs @@ -0,0 +1,85 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +#[derive(Copy, Clone)] +pub struct shape_a { + pub code: u16, + pub pad: [u8; 14], +} +#[derive(Copy, Clone)] +pub struct shape_b { + pub code: u16, + pub lo: u16, + pub hi: u32, + pub fill: [u8; 8], +} +#[derive(Copy, Clone)] +pub struct Container_anon_0 { + pub a: shape_a, + pub b: shape_b, + pub raw_: [u8; 256], +} +#[derive(Copy, Clone, Default)] +pub struct Container { + pub view: Container_anon_0, +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut c: Container = ::default(); + { + let byte_0 = + ((&mut c as *mut Container) as *mut Container as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::() as u64 { + *byte_0.offset(offset as isize) = 0 as u8; + } + ((&mut c as *mut Container) as *mut Container as *mut ::libc::c_void) + }; + assert!(((c.view.a.code as i32) == (0))); + assert!(((c.view.b.lo as i32) == (0))); + assert!(((c.view.raw_[(0) as usize] as i32) == (0))); + assert!(((c.view.raw_[(255) as usize] as i32) == (0))); + let mut src: [u8; 16] = [ + 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, + 0_u8, + ]; + src[(0) as usize] = 2_u8; + src[(2) as usize] = 80_u8; + src[(3) as usize] = 0_u8; + src[(4) as usize] = 127_u8; + src[(5) as usize] = 0_u8; + src[(6) as usize] = 0_u8; + src[(7) as usize] = 1_u8; + let mut len: u64 = 16_u64; + assert!(((len) <= (::std::mem::size_of::<[u8; 256]>() as u64))); + { + if len != 0 { + ::std::ptr::copy_nonoverlapping( + (src.as_mut_ptr() as *const u8 as *const ::libc::c_void), + ((&mut c.view.raw_ as *mut [u8; 256]) as *mut [u8; 256] as *mut ::libc::c_void), + len as usize, + ) + } + ((&mut c.view.raw_ as *mut [u8; 256]) as *mut [u8; 256] as *mut ::libc::c_void) + }; + assert!(((c.view.b.code as i32) == (2))); + assert!((((*((&mut c.view.b.lo as *mut u16) as *mut u8).offset((0) as isize)) as i32) == (80))); + { + let byte_0 = + ((&mut c as *mut Container) as *mut Container as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::() as u64 { + *byte_0.offset(offset as isize) = 0 as u8; + } + ((&mut c as *mut Container) as *mut Container as *mut ::libc::c_void) + }; + assert!(((c.view.b.code as i32) == (0))); + return 0; +} diff --git a/tests/unit/out/unsafe/union_nested.rs b/tests/unit/out/unsafe/union_nested.rs new file mode 100644 index 00000000..ba0822fb --- /dev/null +++ b/tests/unit/out/unsafe/union_nested.rs @@ -0,0 +1,60 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +#[derive(Copy, Clone)] +pub struct record { + pub code: u16, + pub pad: [u8; 14], +} +#[derive(Copy, Clone)] +pub struct inner_anon_0 { + pub h: record, + pub raw_: [u8; 128], +} +#[derive(Copy, Clone, Default)] +pub struct inner { + pub view: inner_anon_0, +} +#[derive(Copy, Clone, Default)] +pub struct Outer_anon_0 { + pub h: record, + pub nested: inner, +} +#[derive(Copy, Clone, Default)] +pub struct Outer { + pub kind: i32, + pub level: i32, + pub variant: i32, + pub len: u32, + pub body: Outer_anon_0, +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut ex: Outer = ::default(); + { + let byte_0 = ((&mut ex as *mut Outer) as *mut Outer as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::() as u64 { + *byte_0.offset(offset as isize) = 0 as u8; + } + ((&mut ex as *mut Outer) as *mut Outer as *mut ::libc::c_void) + }; + ex.kind = 2; + ex.level = 1; + ex.variant = 6; + ex.len = (::std::mem::size_of::() as u64 as u32); + ex.body.h.code = 2_u16; + ex.body.h.pad[(0) as usize] = (('X' as i32) as u8); + assert!(((ex.body.h.code as i32) == (2))); + assert!(((ex.body.h.pad[(0) as usize] as i32) == ('X' as i32))); + assert!(((ex.body.nested.view.h.code as i32) == (2))); + return 0; +} diff --git a/tests/unit/out/unsafe/union_struct_dual_use.rs b/tests/unit/out/unsafe/union_struct_dual_use.rs new file mode 100644 index 00000000..76beecf2 --- /dev/null +++ b/tests/unit/out/unsafe/union_struct_dual_use.rs @@ -0,0 +1,60 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +#[derive(Copy, Clone, Default)] +pub struct Inner { + pub a: i32, + pub b: i32, +} +pub unsafe fn sum_inner_0(mut i: *mut Inner) -> i32 { + return (((*i).a) + ((*i).b)); +} +#[derive(Copy, Clone)] +pub struct Outer_anon_0 { + pub inner: Inner, + pub raw_: [u8; 16], +} +#[derive(Copy, Clone, Default)] +pub struct Outer { + pub u: Outer_anon_0, +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut standalone: Inner = ::default(); + standalone.a = 3; + standalone.b = 4; + assert!( + ((unsafe { + let _i: *mut Inner = (&mut standalone as *mut Inner); + sum_inner_0(_i) + }) == (7)) + ); + let mut outer: Outer = ::default(); + { + let byte_0 = ((&mut outer as *mut Outer) as *mut Outer as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::() as u64 { + *byte_0.offset(offset as isize) = 0 as u8; + } + ((&mut outer as *mut Outer) as *mut Outer as *mut ::libc::c_void) + }; + outer.u.inner.a = 3; + outer.u.inner.b = 4; + assert!( + ((unsafe { + let _i: *mut Inner = (&mut outer.u.inner as *mut Inner); + sum_inner_0(_i) + }) == (7)) + ); + assert!((((outer.u.raw_[(0) as usize] as u8) as i32) == (3))); + assert!((((outer.u.raw_[(4) as usize] as u8) as i32) == (4))); + return 0; +} diff --git a/tests/unit/out/unsafe/union_tagged_struct_arms.rs b/tests/unit/out/unsafe/union_tagged_struct_arms.rs new file mode 100644 index 00000000..541a1395 --- /dev/null +++ b/tests/unit/out/unsafe/union_tagged_struct_arms.rs @@ -0,0 +1,86 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +#[derive(Clone, Copy, PartialEq, Debug, Default)] +enum Choice { + #[default] + C_LIST = 1, + C_LETTERS = 2, + C_INTEGERS = 3, +} +#[derive(Copy, Clone, Default)] +pub struct Branch_anon_0_anon_0 { + pub items: *mut *mut u8, + pub count: i64, + pub cursor: i64, +} +#[derive(Copy, Clone, Default)] +pub struct Branch_anon_0_anon_1 { + pub lo: i32, + pub hi: i32, + pub curr: i32, + pub step: u8, +} +#[derive(Copy, Clone, Default)] +pub struct Branch_anon_0_anon_2 { + pub lo: i64, + pub hi: i64, + pub curr: i64, + pub step: i64, + pub width: i32, +} +#[derive(Copy, Clone, Default)] +pub struct Branch_anon_0 { + pub list: Branch_anon_0_anon_0, + pub letters: Branch_anon_0_anon_1, + pub integers: Branch_anon_0_anon_2, +} +#[derive(Copy, Clone, Default)] +pub struct Branch { + pub choice: Choice, + pub index: i32, + pub v: Branch_anon_0, +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + static mut items: [*mut u8; 3] = [b"a\0".as_ptr(), b"b\0".as_ptr(), b"c\0".as_ptr()];; + let mut p_list: Branch = ::default(); + p_list.choice = (Choice::C_LIST as Choice); + p_list.index = 0; + p_list.v.list.items = items.as_mut_ptr(); + p_list.v.list.count = 3_i64; + p_list.v.list.cursor = 1_i64; + assert!(((p_list.v.list.count) == (3_i64))); + assert!( + (((*(*p_list.v.list.items.offset((1) as isize)).offset((0) as isize)) as i32) + == ('b' as i32)) + ); + let mut p_letters: Branch = ::default(); + p_letters.choice = (Choice::C_LETTERS as Choice); + p_letters.index = 1; + p_letters.v.letters.lo = ('a' as i32); + p_letters.v.letters.hi = ('z' as i32); + p_letters.v.letters.curr = ('m' as i32); + p_letters.v.letters.step = 1_u8; + assert!((((p_letters.v.letters.hi) - (p_letters.v.letters.lo)) == (25))); + let mut p_integers: Branch = ::default(); + p_integers.choice = (Choice::C_INTEGERS as Choice); + p_integers.index = 2; + p_integers.v.integers.lo = 1_i64; + p_integers.v.integers.hi = 100_i64; + p_integers.v.integers.curr = 1_i64; + p_integers.v.integers.step = 1_i64; + p_integers.v.integers.width = 3; + assert!(((p_integers.v.integers.hi) == (100_i64))); + assert!(((p_integers.v.integers.width) == (3))); + return 0; +} From 59548ad33ced43ead244d9c85240077da2a7052f Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 27 Apr 2026 21:05:35 +0100 Subject: [PATCH 06/19] Translate unsafe unions Rename EmitRustStruct as EmitRustStructOrUnion since the logic for emitting a struct or a union is very similar, the only thing that is really different is whether are are emitting the struct or union keyword. All structs and unions have now repr(C). The Rust compiler is allowed to reorder fields. This is undesirable for unions where order of fields is strict. Since a struct that is defined outside an union can be used inside an union, we have to be pessimstic and consider all structs as having repr(C). Unions cannot derive the Default trait, so implement Default as the 0 bit pattern, which should be enough for the current usages of union fields. --- cpp2rust/converter/converter.cpp | 66 +++++++++++++------ cpp2rust/converter/converter.h | 6 +- cpp2rust/converter/lex.h | 1 + .../converter/models/converter_refcount.cpp | 2 +- .../converter/models/converter_refcount.h | 4 +- 5 files changed, 54 insertions(+), 25 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 1b3ed81e..b008e62a 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -599,12 +599,12 @@ bool Converter::VisitRecordDecl(clang::RecordDecl *decl) { } Mapper::AddRuleForUserDefinedType(decl); - EmitRustStruct(decl); + EmitRustStructOrUnion(decl); return false; } -void Converter::EmitRustStruct(clang::RecordDecl *decl) { +void Converter::EmitRustStructOrUnion(clang::RecordDecl *decl) { // Enums and static variables. In rust they live outside the record for (auto *d : decl->decls()) { if (auto *enum_decl = llvm::dyn_cast(d)) { @@ -630,6 +630,9 @@ void Converter::EmitRustStruct(clang::RecordDecl *decl) { } // Derived traits + if (EmitsReprCForRecords()) { + StrCat("#[repr(C)]"); + } StrCat("#[derive("); for (auto *attr : GetStructAttributes(decl)) { StrCat(attr, ","); @@ -640,7 +643,7 @@ void Converter::EmitRustStruct(clang::RecordDecl *decl) { auto access = clang::dyn_cast(decl) ? AccessSpecifierAsString(decl->getAccess()) : keyword::kPub; - StrCat(access, keyword::kStruct, GetRecordName(decl)); + StrCat(access, decl->isUnion() ? keyword::kUnion : keyword::kStruct, GetRecordName(decl)); { PushBrace brace(*this); for (auto *field : decl->fields()) { @@ -681,8 +684,8 @@ void Converter::EmitRustStruct(clang::RecordDecl *decl) { AddOrdTrait(cxx); AddCloneTrait(cxx); AddDropTrait(cxx); - AddDefaultTrait(cxx); } + AddDefaultTrait(decl); AddByteReprTrait(decl); } @@ -728,10 +731,10 @@ bool Converter::VisitCXXRecordDecl(clang::CXXRecordDecl *decl) { } } - EmitRustStruct(decl); + EmitRustStructOrUnion(decl); } else { // FIXME: improve error handling - assert(0 && "unsupported union"); + assert(0 && "unsupported record kind"); } return false; @@ -2878,11 +2881,19 @@ std::string Converter::GetRecordName(const clang::NamedDecl *decl) const { if (auto it = inner_structs_.find(ID); it != inner_structs_.end()) { return it->second; } + if (auto *record = clang::dyn_cast(decl); + record && !record->getIdentifier()) { + return ID; + } return std::regex_replace(Mapper::ToString(decl), std::regex("::"), "_"); } std::vector Converter::GetStructAttributes(const clang::RecordDecl *decl) { + if (decl->isUnion()) { + return {"Copy", "Clone"}; + } + std::vector struct_attrs = {}; if (recordDerivesCopy(decl)) { @@ -3202,7 +3213,14 @@ void Converter::AddCloneTrait(const clang::CXXRecordDecl *decl) {} void Converter::AddDropTrait(const clang::CXXRecordDecl *decl) {} -void Converter::AddDefaultTrait(const clang::CXXRecordDecl *decl) { +void Converter::AddDefaultTrait(const clang::RecordDecl *decl) { + if (decl->isUnion()) { + StrCat( + std::format("impl Default for {} {{ fn default() -> Self {{ unsafe {{ " + "std::mem::zeroed() }} }} }}", + GetRecordName(decl))); + return; + } if (RecordDerivesDefault(decl)) { return; } @@ -3211,20 +3229,26 @@ void Converter::AddDefaultTrait(const clang::CXXRecordDecl *decl) { PushBrace impl_brace(*this); StrCat("fn default() -> Self"); PushBrace fn_brace(*this); - if (auto *default_ctor = GetUserDefinedDefaultConstructor(decl)) { - StrCat(keyword_unsafe_); - PushBrace unsafe_brace(*this); - Convert(clang::CXXConstructExpr::Create( - ctx_, ctx_.getCanonicalTagType(decl), clang::SourceLocation(), - default_ctor, - /*Elidable=*/false, llvm::ArrayRef(), - /*HadMultipleCandidates=*/false, - /*ListInitialization=*/false, - /*StdInitListInitialization=*/false, - /*ZeroInitialization=*/false, clang::CXXConstructionKind::Complete, - clang::SourceRange())); - } else { - StrCat(struct_name); + + if (auto *cxx = clang::dyn_cast(decl)) { + if (auto *default_ctor = GetUserDefinedDefaultConstructor(decl)) { + StrCat(keyword_unsafe_); + PushBrace unsafe_brace(*this); + Convert(clang::CXXConstructExpr::Create( + ctx_, ctx_.getCanonicalTagType(decl), clang::SourceLocation(), + default_ctor, + /*Elidable=*/false, llvm::ArrayRef(), + /*HadMultipleCandidates=*/false, + /*ListInitialization=*/false, + /*StdInitListInitialization=*/false, + /*ZeroInitialization=*/false, clang::CXXConstructionKind::Complete, + clang::SourceRange())); + return; + } + } + + StrCat(struct_name); + { PushBrace struct_brace(*this); for (auto *field : decl->fields()) { StrCat(GetNamedDeclAsString(field), token::kColon, diff --git a/cpp2rust/converter/converter.h b/cpp2rust/converter/converter.h index a948d1a9..b38080f1 100644 --- a/cpp2rust/converter/converter.h +++ b/cpp2rust/converter/converter.h @@ -99,7 +99,9 @@ class Converter : public clang::RecursiveASTVisitor { virtual bool VisitCXXRecordDecl(clang::CXXRecordDecl *decl); - void EmitRustStruct(clang::RecordDecl *decl); + virtual void EmitRustStructOrUnion(clang::RecordDecl *decl); + + virtual bool EmitsReprCForRecords() const { return true; } virtual bool VisitCXXMethodDecl(clang::CXXMethodDecl *decl); virtual std::string GetSelfMaybeWithMut(const clang::CXXMethodDecl *decl); @@ -441,7 +443,7 @@ class Converter : public clang::RecursiveASTVisitor { virtual void AddDropTrait(const clang::CXXRecordDecl *decl); - virtual void AddDefaultTrait(const clang::CXXRecordDecl *decl); + virtual void AddDefaultTrait(const clang::RecordDecl *decl); virtual void AddByteReprTrait(const clang::RecordDecl *decl); diff --git a/cpp2rust/converter/lex.h b/cpp2rust/converter/lex.h index db69958d..b7a9e3d2 100644 --- a/cpp2rust/converter/lex.h +++ b/cpp2rust/converter/lex.h @@ -51,6 +51,7 @@ inline constexpr const char *kReturn = "return"; inline constexpr const char *kSelfValue = "self"; inline constexpr const char *kStatic = "static"; inline constexpr const char *kStruct = "struct"; +inline constexpr const char *kUnion = "union"; inline constexpr const char *kTrue = "true"; inline constexpr const char *kWhile = "while"; inline constexpr const char *kFor = "for"; diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 0442ae75..7f636987 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -460,7 +460,7 @@ void ConverterRefCount::AddCloneTrait(const clang::CXXRecordDecl *decl) { StrCat("}"); } -void ConverterRefCount::AddDefaultTrait(const clang::CXXRecordDecl *decl) { +void ConverterRefCount::AddDefaultTrait(const clang::RecordDecl *decl) { PushConversionKind push(*this, ConversionKind::FullRefCount); Converter::AddDefaultTrait(decl); } diff --git a/cpp2rust/converter/models/converter_refcount.h b/cpp2rust/converter/models/converter_refcount.h index cfb22aa8..9211bd22 100644 --- a/cpp2rust/converter/models/converter_refcount.h +++ b/cpp2rust/converter/models/converter_refcount.h @@ -28,6 +28,8 @@ class ConverterRefCount final : public Converter { bool VisitCXXRecordDecl(clang::CXXRecordDecl *decl) override; + bool EmitsReprCForRecords() const override { return false; } + void ConvertOrdAndPartialOrdTraits(const clang::CXXRecordDecl *decl, const clang::FunctionDecl *op) override; @@ -37,7 +39,7 @@ class ConverterRefCount final : public Converter { void AddByteReprTrait(const clang::RecordDecl *decl) override; - void AddDefaultTrait(const clang::CXXRecordDecl *decl) override; + void AddDefaultTrait(const clang::RecordDecl *decl) override; std::string GetSelfMaybeWithMut(const clang::CXXMethodDecl *decl) override; From f459d054f17dc0e17b0b3caa66ef4e849a798cb6 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 27 Apr 2026 21:19:55 +0100 Subject: [PATCH 07/19] Change test expectations --- tests/unit/union_basic.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unit/union_basic.c b/tests/unit/union_basic.c index 5d26a336..86731cc2 100644 --- a/tests/unit/union_basic.c +++ b/tests/unit/union_basic.c @@ -1,3 +1,4 @@ +// no-compile: refcount #include union basic { From c0e2a74af90fdb26cc891dd4b365c8ed138371ea Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 27 Apr 2026 21:21:37 +0100 Subject: [PATCH 08/19] Update union generated files --- .../out/refcount/union_addrof_external.rs | 118 ++++++++++++++++++ tests/unit/out/refcount/union_basic.rs | 7 +- .../unit/out/refcount/union_cross_arm_cast.rs | 34 ++++- .../out/refcount/union_field_alignment.rs | 11 +- .../out/refcount/union_flex_array_member.rs | 11 +- .../unit/out/refcount/union_memset_memcpy.rs | 33 ++++- tests/unit/out/refcount/union_nested.rs | 32 ++++- .../out/refcount/union_pointer_pun_address.rs | 11 +- .../union_pointer_pun_writethrough.rs | 11 +- .../out/refcount/union_struct_dual_use.rs | 11 +- .../out/refcount/union_tagged_many_arms.rs | 11 +- .../unit/out/refcount/union_tagged_simple.rs | 11 +- .../out/refcount/union_tagged_struct_arms.rs | 29 +++-- .../refcount/union_void_ptr_sized_deref.rs | 11 +- .../unit/out/unsafe/union_addrof_external.rs | 26 +++- tests/unit/out/unsafe/union_basic.rs | 10 +- tests/unit/out/unsafe/union_cross_arm_cast.rs | 32 ++++- .../unit/out/unsafe/union_field_alignment.rs | 11 +- .../out/unsafe/union_flex_array_member.rs | 11 +- tests/unit/out/unsafe/union_memset_memcpy.rs | 31 ++++- tests/unit/out/unsafe/union_nested.rs | 33 ++++- .../out/unsafe/union_pointer_pun_address.rs | 14 ++- .../unsafe/union_pointer_pun_writethrough.rs | 12 +- .../unit/out/unsafe/union_struct_dual_use.rs | 12 +- .../unit/out/unsafe/union_tagged_many_arms.rs | 13 +- tests/unit/out/unsafe/union_tagged_simple.rs | 13 +- .../out/unsafe/union_tagged_struct_arms.rs | 28 +++-- .../out/unsafe/union_void_ptr_sized_deref.rs | 13 +- 28 files changed, 506 insertions(+), 94 deletions(-) create mode 100644 tests/unit/out/refcount/union_addrof_external.rs diff --git a/tests/unit/out/refcount/union_addrof_external.rs b/tests/unit/out/refcount/union_addrof_external.rs new file mode 100644 index 00000000..5306b87e --- /dev/null +++ b/tests/unit/out/refcount/union_addrof_external.rs @@ -0,0 +1,118 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +#[repr(C)] +#[derive()] +pub struct record { + pub code: Value, + pub lo: Value, + pub hi: Value, + pub pad: Value>, +} +impl ByteRepr for record {} +#[repr(C)] +#[derive(Copy, Clone)] +pub union Container_anon_15_3 { + pub h: Value, + pub raw_: Value>, +} +impl Default for Container_anon_15_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +#[repr(C)] +#[derive(Default)] +pub struct Container { + pub view: Value, +} +impl ByteRepr for Container {} +pub fn fill_0(out: AnyPtr, cap: u64) { + let out: Value = Rc::new(RefCell::new(out)); + let cap: Value = Rc::new(RefCell::new(cap)); + let src: Value> = Rc::new(RefCell::new(Box::new([ + 0_u8, + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ]))); + (*src.borrow_mut())[(0) as usize] = 2_u8; + (*src.borrow_mut())[(1) as usize] = 0_u8; + (*src.borrow_mut())[(2) as usize] = 0_u8; + (*src.borrow_mut())[(3) as usize] = 80_u8; + (*src.borrow_mut())[(4) as usize] = 127_u8; + (*src.borrow_mut())[(5) as usize] = 0_u8; + (*src.borrow_mut())[(6) as usize] = 0_u8; + (*src.borrow_mut())[(7) as usize] = 1_u8; + let n: Value = Rc::new(RefCell::new( + if ((::std::mem::size_of::<[u8; 16]>() as u64) < (*cap.borrow())) { + ::std::mem::size_of::<[u8; 16]>() as u64 + } else { + (*cap.borrow()) + }, + )); + { + (*out.borrow()).memcpy( + &((src.as_pointer() as Ptr) as Ptr).to_any(), + (*n.borrow()) as usize, + ); + (*out.borrow()).clone() + }; +} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let c: Value = >::default(); + { + ((c.as_pointer()) as Ptr).to_any().memset( + (0) as u8, + ::std::mem::size_of::() as u64 as usize, + ); + ((c.as_pointer()) as Ptr).to_any().clone() + }; + ({ + let _out: AnyPtr = (((*c.borrow()).view.as_pointer()).to_strong().as_pointer() as AnyPtr); + let _cap: u64 = ::std::mem::size_of::() as u64; + fill_0(_out, _cap) + }); + assert!((((*(*(*(*c.borrow()).view.borrow()).h.borrow()).code.borrow()) as i32) == (2))); + assert!( + ((((((*(*(*c.borrow()).view.borrow()).h.borrow()).lo.as_pointer()) + .to_strong() + .as_pointer() as Ptr::) + .offset((0) as isize) + .read()) as i32) + == (0)) + ); + assert!( + ((((((*(*(*c.borrow()).view.borrow()).h.borrow()).lo.as_pointer()) + .to_strong() + .as_pointer() as Ptr::) + .offset((1) as isize) + .read()) as i32) + == (80)) + ); + assert!((((*(*(*c.borrow()).view.borrow()).raw_.borrow())[(0) as usize] as i32) == (2))); + assert!( + ((((*(*(*c.borrow()).view.borrow()).raw_.borrow())[(3) as usize] as u8) as i32) == (80)) + ); + return 0; +} diff --git a/tests/unit/out/refcount/union_basic.rs b/tests/unit/out/refcount/union_basic.rs index c7255a39..8a005ac1 100644 --- a/tests/unit/out/refcount/union_basic.rs +++ b/tests/unit/out/refcount/union_basic.rs @@ -7,10 +7,15 @@ use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; #[derive(Default)] -pub struct basic { +pub union basic { pub i: Value, pub f: Value, } +impl Default for basic { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} impl ByteRepr for basic {} pub fn main() { std::process::exit(main_0()); diff --git a/tests/unit/out/refcount/union_cross_arm_cast.rs b/tests/unit/out/refcount/union_cross_arm_cast.rs index 20e0d2eb..06470059 100644 --- a/tests/unit/out/refcount/union_cross_arm_cast.rs +++ b/tests/unit/out/refcount/union_cross_arm_cast.rs @@ -11,6 +11,16 @@ pub struct shape_a { pub code: Value, pub pad: Value>, } +impl Default for shape_a { + fn default() -> Self { + shape_a { + code: >::default(), + pad: Rc::new(RefCell::new( + (0..14).map(|_| ::default()).collect::>(), + )), + } + } +} impl ByteRepr for shape_a {} #[derive()] pub struct shape_b { @@ -20,18 +30,36 @@ pub struct shape_b { pub fill: Value>, pub tail: Value, } +impl Default for shape_b { + fn default() -> Self { + shape_b { + code: >::default(), + lo: >::default(), + mid: >::default(), + fill: Rc::new(RefCell::new( + (0..16).map(|_| ::default()).collect::>(), + )), + tail: >::default(), + } + } +} impl ByteRepr for shape_b {} #[derive()] -pub struct Container_anon_0 { +pub union Container_anon_22_3 { pub a: Value, pub b: Value, pub raw_: Value>, } -impl ByteRepr for Container_anon_0 {} +impl Default for Container_anon_22_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +impl ByteRepr for Container_anon_22_3 {} #[derive(Default)] pub struct Container { pub len: Value, - pub u: Value, + pub u: Value, } impl ByteRepr for Container {} pub fn main() { diff --git a/tests/unit/out/refcount/union_field_alignment.rs b/tests/unit/out/refcount/union_field_alignment.rs index 3601f78e..24abc749 100644 --- a/tests/unit/out/refcount/union_field_alignment.rs +++ b/tests/unit/out/refcount/union_field_alignment.rs @@ -7,15 +7,20 @@ use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; #[derive()] -pub struct node_anon_0 { +pub union node_anon_10_3 { pub bytes: Value>, pub aligner: Value, } -impl ByteRepr for node_anon_0 {} +impl Default for node_anon_10_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +impl ByteRepr for node_anon_10_3 {} #[derive(Default)] pub struct node { pub next: Value>, - pub x: Value, + pub x: Value, } impl ByteRepr for node {} pub fn main() { diff --git a/tests/unit/out/refcount/union_flex_array_member.rs b/tests/unit/out/refcount/union_flex_array_member.rs index 56d7ef0e..d42f23ad 100644 --- a/tests/unit/out/refcount/union_flex_array_member.rs +++ b/tests/unit/out/refcount/union_flex_array_member.rs @@ -7,15 +7,20 @@ use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; #[derive()] -pub struct node_anon_0 { +pub union node_anon_10_3 { pub bytes: Value>, pub aligner: Value, } -impl ByteRepr for node_anon_0 {} +impl Default for node_anon_10_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +impl ByteRepr for node_anon_10_3 {} #[derive(Default)] pub struct node { pub len: Value, - pub x: Value, + pub x: Value, } impl ByteRepr for node {} pub fn main() { diff --git a/tests/unit/out/refcount/union_memset_memcpy.rs b/tests/unit/out/refcount/union_memset_memcpy.rs index fbb55430..309f6f52 100644 --- a/tests/unit/out/refcount/union_memset_memcpy.rs +++ b/tests/unit/out/refcount/union_memset_memcpy.rs @@ -11,6 +11,16 @@ pub struct shape_a { pub code: Value, pub pad: Value>, } +impl Default for shape_a { + fn default() -> Self { + shape_a { + code: >::default(), + pad: Rc::new(RefCell::new( + (0..14).map(|_| ::default()).collect::>(), + )), + } + } +} impl ByteRepr for shape_a {} #[derive()] pub struct shape_b { @@ -19,17 +29,34 @@ pub struct shape_b { pub hi: Value, pub fill: Value>, } +impl Default for shape_b { + fn default() -> Self { + shape_b { + code: >::default(), + lo: >::default(), + hi: >::default(), + fill: Rc::new(RefCell::new( + (0..8).map(|_| ::default()).collect::>(), + )), + } + } +} impl ByteRepr for shape_b {} #[derive()] -pub struct Container_anon_0 { +pub union Container_anon_20_3 { pub a: Value, pub b: Value, pub raw_: Value>, } -impl ByteRepr for Container_anon_0 {} +impl Default for Container_anon_20_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +impl ByteRepr for Container_anon_20_3 {} #[derive(Default)] pub struct Container { - pub view: Value, + pub view: Value, } impl ByteRepr for Container {} pub fn main() { diff --git a/tests/unit/out/refcount/union_nested.rs b/tests/unit/out/refcount/union_nested.rs index d8322734..a2359b77 100644 --- a/tests/unit/out/refcount/union_nested.rs +++ b/tests/unit/out/refcount/union_nested.rs @@ -11,31 +11,51 @@ pub struct record { pub code: Value, pub pad: Value>, } +impl Default for record { + fn default() -> Self { + record { + code: >::default(), + pad: Rc::new(RefCell::new( + (0..14).map(|_| ::default()).collect::>(), + )), + } + } +} impl ByteRepr for record {} #[derive()] -pub struct inner_anon_0 { +pub union inner_anon_12_3 { pub h: Value, pub raw_: Value>, } -impl ByteRepr for inner_anon_0 {} +impl Default for inner_anon_12_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +impl ByteRepr for inner_anon_12_3 {} #[derive(Default)] pub struct inner { - pub view: Value, + pub view: Value, } impl ByteRepr for inner {} #[derive(Default)] -pub struct Outer_anon_0 { +pub union Outer_anon_23_3 { pub h: Value, pub nested: Value, } -impl ByteRepr for Outer_anon_0 {} +impl Default for Outer_anon_23_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +impl ByteRepr for Outer_anon_23_3 {} #[derive(Default)] pub struct Outer { pub kind: Value, pub level: Value, pub variant: Value, pub len: Value, - pub body: Value, + pub body: Value, } impl ByteRepr for Outer {} pub fn main() { diff --git a/tests/unit/out/refcount/union_pointer_pun_address.rs b/tests/unit/out/refcount/union_pointer_pun_address.rs index bbe33b8a..249aeb9b 100644 --- a/tests/unit/out/refcount/union_pointer_pun_address.rs +++ b/tests/unit/out/refcount/union_pointer_pun_address.rs @@ -25,12 +25,17 @@ fn main_0() -> i32 { n: Rc::new(RefCell::new(123)), })); #[derive(Default)] - pub struct anon_0 { + pub union anon_16_3 { pub to_a: Value>, pub to_b: Value>, } - impl ByteRepr for anon_0 {}; - let ptr: Value = >::default(); + impl Default for anon_16_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } + } + impl ByteRepr for anon_16_3 {}; + let ptr: Value = >::default(); (*(*ptr.borrow()).to_a.borrow_mut()) = (a.as_pointer()); let out: Value> = Rc::new(RefCell::new((*(*ptr.borrow()).to_b.borrow()).clone())); assert!({ diff --git a/tests/unit/out/refcount/union_pointer_pun_writethrough.rs b/tests/unit/out/refcount/union_pointer_pun_writethrough.rs index c6c6960b..12054307 100644 --- a/tests/unit/out/refcount/union_pointer_pun_writethrough.rs +++ b/tests/unit/out/refcount/union_pointer_pun_writethrough.rs @@ -12,12 +12,17 @@ pub fn main() { fn main_0() -> i32 { let x: Value = Rc::new(RefCell::new((-1_i32 as i64))); #[derive(Default)] - pub struct anon_0 { + pub union anon_7_3 { pub as_unsigned: Value>, pub as_signed: Value>, } - impl ByteRepr for anon_0 {}; - let pp: Value = >::default(); + impl Default for anon_7_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } + } + impl ByteRepr for anon_7_3 {}; + let pp: Value = >::default(); (*(*pp.borrow()).as_signed.borrow_mut()) = (x.as_pointer()); (*(*pp.borrow()).as_unsigned.borrow()).write(42_u64); assert!(((*x.borrow()) == 42_i64)); diff --git a/tests/unit/out/refcount/union_struct_dual_use.rs b/tests/unit/out/refcount/union_struct_dual_use.rs index 2e1989c3..9100f8d8 100644 --- a/tests/unit/out/refcount/union_struct_dual_use.rs +++ b/tests/unit/out/refcount/union_struct_dual_use.rs @@ -20,14 +20,19 @@ pub fn sum_inner_0(i: Ptr) -> i32 { }; } #[derive()] -pub struct Outer_anon_0 { +pub union Outer_anon_13_3 { pub inner: Value, pub raw_: Value>, } -impl ByteRepr for Outer_anon_0 {} +impl Default for Outer_anon_13_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +impl ByteRepr for Outer_anon_13_3 {} #[derive(Default)] pub struct Outer { - pub u: Value, + pub u: Value, } impl ByteRepr for Outer {} pub fn main() { diff --git a/tests/unit/out/refcount/union_tagged_many_arms.rs b/tests/unit/out/refcount/union_tagged_many_arms.rs index 87d63663..17c690bf 100644 --- a/tests/unit/out/refcount/union_tagged_many_arms.rs +++ b/tests/unit/out/refcount/union_tagged_many_arms.rs @@ -16,18 +16,23 @@ enum Tag { T_REF = 4, } #[derive(Default)] -pub struct Slot_anon_0 { +pub union Slot_anon_15_3 { pub text: Value>, pub handle: Value, pub signed_n: Value, pub unsigned_n: Value, pub f: Value, } -impl ByteRepr for Slot_anon_0 {} +impl Default for Slot_anon_15_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +impl ByteRepr for Slot_anon_15_3 {} #[derive(Default)] pub struct Slot { pub tag: Value, - pub payload: Value, + pub payload: Value, } impl ByteRepr for Slot {} pub fn main() { diff --git a/tests/unit/out/refcount/union_tagged_simple.rs b/tests/unit/out/refcount/union_tagged_simple.rs index fcb54840..dd7baa33 100644 --- a/tests/unit/out/refcount/union_tagged_simple.rs +++ b/tests/unit/out/refcount/union_tagged_simple.rs @@ -13,16 +13,21 @@ enum Kind { KIND_DONE = 1, } #[derive(Default)] -pub struct Event_anon_0 { +pub union Event_anon_12_3 { pub obj: Value, pub code: Value, } -impl ByteRepr for Event_anon_0 {} +impl Default for Event_anon_12_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +impl ByteRepr for Event_anon_12_3 {} #[derive(Default)] pub struct Event { pub kind: Value, pub handle: Value, - pub payload: Value, + pub payload: Value, } impl ByteRepr for Event {} pub fn main() { diff --git a/tests/unit/out/refcount/union_tagged_struct_arms.rs b/tests/unit/out/refcount/union_tagged_struct_arms.rs index 52dc5c9a..9e94541e 100644 --- a/tests/unit/out/refcount/union_tagged_struct_arms.rs +++ b/tests/unit/out/refcount/union_tagged_struct_arms.rs @@ -14,41 +14,46 @@ enum Choice { C_INTEGERS = 3, } #[derive(Default)] -pub struct Branch_anon_0_anon_0 { +pub struct Branch_anon_14_3_anon_15_5 { pub items: Value>>, pub count: Value, pub cursor: Value, } -impl ByteRepr for Branch_anon_0_anon_0 {} +impl ByteRepr for Branch_anon_14_3_anon_15_5 {} #[derive(Default)] -pub struct Branch_anon_0_anon_1 { +pub struct Branch_anon_14_3_anon_20_5 { pub lo: Value, pub hi: Value, pub curr: Value, pub step: Value, } -impl ByteRepr for Branch_anon_0_anon_1 {} +impl ByteRepr for Branch_anon_14_3_anon_20_5 {} #[derive(Default)] -pub struct Branch_anon_0_anon_2 { +pub struct Branch_anon_14_3_anon_26_5 { pub lo: Value, pub hi: Value, pub curr: Value, pub step: Value, pub width: Value, } -impl ByteRepr for Branch_anon_0_anon_2 {} +impl ByteRepr for Branch_anon_14_3_anon_26_5 {} #[derive(Default)] -pub struct Branch_anon_0 { - pub list: Value, - pub letters: Value, - pub integers: Value, +pub union Branch_anon_14_3 { + pub list: Value, + pub letters: Value, + pub integers: Value, } -impl ByteRepr for Branch_anon_0 {} +impl Default for Branch_anon_14_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +impl ByteRepr for Branch_anon_14_3 {} #[derive(Default)] pub struct Branch { pub choice: Value, pub index: Value, - pub v: Value, + pub v: Value, } impl ByteRepr for Branch {} pub fn main() { diff --git a/tests/unit/out/refcount/union_void_ptr_sized_deref.rs b/tests/unit/out/refcount/union_void_ptr_sized_deref.rs index d07cc4a7..cd30a604 100644 --- a/tests/unit/out/refcount/union_void_ptr_sized_deref.rs +++ b/tests/unit/out/refcount/union_void_ptr_sized_deref.rs @@ -14,17 +14,22 @@ enum Width { W_16 = 2, } #[derive(Default)] -pub struct Sink_anon_0 { +pub union Sink_anon_14_3 { pub text: Value>, pub handle: Value, pub signed_n: Value, pub f: Value, } -impl ByteRepr for Sink_anon_0 {} +impl Default for Sink_anon_14_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +impl ByteRepr for Sink_anon_14_3 {} #[derive(Default)] pub struct Sink { pub width: Value, - pub out: Value, + pub out: Value, } impl ByteRepr for Sink {} pub fn write_count_0(s: Ptr, count: i64) { diff --git a/tests/unit/out/unsafe/union_addrof_external.rs b/tests/unit/out/unsafe/union_addrof_external.rs index 9181620f..bbaa44a8 100644 --- a/tests/unit/out/unsafe/union_addrof_external.rs +++ b/tests/unit/out/unsafe/union_addrof_external.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone)] pub struct record { pub code: u16, @@ -13,14 +14,31 @@ pub struct record { pub hi: u32, pub pad: [u8; 8], } +impl Default for record { + fn default() -> Self { + record { + code: 0_u16, + lo: 0_u16, + hi: 0_u32, + pad: [0_u8; 8], + } + } +} +#[repr(C)] #[derive(Copy, Clone)] -pub struct Container_anon_0 { +pub union Container_anon_15_3 { pub h: record, pub raw_: [u8; 128], } +impl Default for Container_anon_15_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Container { - pub view: Container_anon_0, + pub view: Container_anon_15_3, } pub unsafe fn fill_0(mut out: *mut ::libc::c_void, mut cap: u64) { let mut src: [u8; 16] = [ @@ -68,8 +86,8 @@ unsafe fn main_0() -> i32 { }; (unsafe { let _out: *mut ::libc::c_void = - ((&mut c.view as *mut Container_anon_0) as *mut ::libc::c_void); - let _cap: u64 = ::std::mem::size_of::() as u64; + ((&mut c.view as *mut Container_anon_15_3) as *mut ::libc::c_void); + let _cap: u64 = ::std::mem::size_of::() as u64; fill_0(_out, _cap) }); assert!(((c.view.h.code as i32) == (2))); diff --git a/tests/unit/out/unsafe/union_basic.rs b/tests/unit/out/unsafe/union_basic.rs index 1ddfcb9c..81e82df3 100644 --- a/tests/unit/out/unsafe/union_basic.rs +++ b/tests/unit/out/unsafe/union_basic.rs @@ -6,11 +6,17 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; -#[derive(Copy, Clone, Default)] -pub struct basic { +#[repr(C)] +#[derive(Copy, Clone)] +pub union basic { pub i: i32, pub f: f32, } +impl Default for basic { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} pub fn main() { unsafe { std::process::exit(main_0() as i32); diff --git a/tests/unit/out/unsafe/union_cross_arm_cast.rs b/tests/unit/out/unsafe/union_cross_arm_cast.rs index d93385ce..6d63269b 100644 --- a/tests/unit/out/unsafe/union_cross_arm_cast.rs +++ b/tests/unit/out/unsafe/union_cross_arm_cast.rs @@ -6,11 +6,21 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone)] pub struct shape_a { pub code: u16, pub pad: [u8; 14], } +impl Default for shape_a { + fn default() -> Self { + shape_a { + code: 0_u16, + pad: [0_u8; 14], + } + } +} +#[repr(C)] #[derive(Copy, Clone)] pub struct shape_b { pub code: u16, @@ -19,16 +29,34 @@ pub struct shape_b { pub fill: [u8; 16], pub tail: u32, } +impl Default for shape_b { + fn default() -> Self { + shape_b { + code: 0_u16, + lo: 0_u16, + mid: 0_u32, + fill: [0_u8; 16], + tail: 0_u32, + } + } +} +#[repr(C)] #[derive(Copy, Clone)] -pub struct Container_anon_0 { +pub union Container_anon_22_3 { pub a: shape_a, pub b: shape_b, pub raw_: [u8; 64], } +impl Default for Container_anon_22_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Container { pub len: u32, - pub u: Container_anon_0, + pub u: Container_anon_22_3, } pub fn main() { unsafe { diff --git a/tests/unit/out/unsafe/union_field_alignment.rs b/tests/unit/out/unsafe/union_field_alignment.rs index 8441c7d4..60518256 100644 --- a/tests/unit/out/unsafe/union_field_alignment.rs +++ b/tests/unit/out/unsafe/union_field_alignment.rs @@ -6,15 +6,22 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone)] -pub struct node_anon_0 { +pub union node_anon_10_3 { pub bytes: [u8; 1], pub aligner: *mut ::libc::c_void, } +impl Default for node_anon_10_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct node { pub next: *mut node, - pub x: node_anon_0, + pub x: node_anon_10_3, } pub fn main() { unsafe { diff --git a/tests/unit/out/unsafe/union_flex_array_member.rs b/tests/unit/out/unsafe/union_flex_array_member.rs index fcf94b81..41fe937a 100644 --- a/tests/unit/out/unsafe/union_flex_array_member.rs +++ b/tests/unit/out/unsafe/union_flex_array_member.rs @@ -6,15 +6,22 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone)] -pub struct node_anon_0 { +pub union node_anon_10_3 { pub bytes: [u8; 1], pub aligner: *mut ::libc::c_void, } +impl Default for node_anon_10_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct node { pub len: u64, - pub x: node_anon_0, + pub x: node_anon_10_3, } pub fn main() { unsafe { diff --git a/tests/unit/out/unsafe/union_memset_memcpy.rs b/tests/unit/out/unsafe/union_memset_memcpy.rs index 47354e9d..d23b83c9 100644 --- a/tests/unit/out/unsafe/union_memset_memcpy.rs +++ b/tests/unit/out/unsafe/union_memset_memcpy.rs @@ -6,11 +6,21 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone)] pub struct shape_a { pub code: u16, pub pad: [u8; 14], } +impl Default for shape_a { + fn default() -> Self { + shape_a { + code: 0_u16, + pad: [0_u8; 14], + } + } +} +#[repr(C)] #[derive(Copy, Clone)] pub struct shape_b { pub code: u16, @@ -18,15 +28,32 @@ pub struct shape_b { pub hi: u32, pub fill: [u8; 8], } +impl Default for shape_b { + fn default() -> Self { + shape_b { + code: 0_u16, + lo: 0_u16, + hi: 0_u32, + fill: [0_u8; 8], + } + } +} +#[repr(C)] #[derive(Copy, Clone)] -pub struct Container_anon_0 { +pub union Container_anon_20_3 { pub a: shape_a, pub b: shape_b, pub raw_: [u8; 256], } +impl Default for Container_anon_20_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Container { - pub view: Container_anon_0, + pub view: Container_anon_20_3, } pub fn main() { unsafe { diff --git a/tests/unit/out/unsafe/union_nested.rs b/tests/unit/out/unsafe/union_nested.rs index ba0822fb..25285226 100644 --- a/tests/unit/out/unsafe/union_nested.rs +++ b/tests/unit/out/unsafe/union_nested.rs @@ -6,32 +6,55 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone)] pub struct record { pub code: u16, pub pad: [u8; 14], } +impl Default for record { + fn default() -> Self { + record { + code: 0_u16, + pad: [0_u8; 14], + } + } +} +#[repr(C)] #[derive(Copy, Clone)] -pub struct inner_anon_0 { +pub union inner_anon_12_3 { pub h: record, pub raw_: [u8; 128], } +impl Default for inner_anon_12_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct inner { - pub view: inner_anon_0, + pub view: inner_anon_12_3, } -#[derive(Copy, Clone, Default)] -pub struct Outer_anon_0 { +#[repr(C)] +#[derive(Copy, Clone)] +pub union Outer_anon_23_3 { pub h: record, pub nested: inner, } +impl Default for Outer_anon_23_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer { pub kind: i32, pub level: i32, pub variant: i32, pub len: u32, - pub body: Outer_anon_0, + pub body: Outer_anon_23_3, } pub fn main() { unsafe { diff --git a/tests/unit/out/unsafe/union_pointer_pun_address.rs b/tests/unit/out/unsafe/union_pointer_pun_address.rs index ad93126d..f86e6b7b 100644 --- a/tests/unit/out/unsafe/union_pointer_pun_address.rs +++ b/tests/unit/out/unsafe/union_pointer_pun_address.rs @@ -6,10 +6,12 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct node_a { pub n: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct node_b { pub data: *mut ::libc::c_void, @@ -22,12 +24,18 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut a: node_a = node_a { n: 123 }; - #[derive(Copy, Clone, Default)] - pub struct anon_0 { + #[repr(C)] + #[derive(Copy, Clone)] + pub union anon_16_3 { pub to_a: *mut node_a, pub to_b: *mut node_b, + } + impl Default for anon_16_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } }; - let mut ptr: anon_0 = ::default(); + let mut ptr: anon_16_3 = ::default(); ptr.to_a = (&mut a as *mut node_a); let mut out: *mut node_b = ptr.to_b; assert!(((out as *mut ::libc::c_void) == ((&mut a as *mut node_a) as *mut ::libc::c_void))); diff --git a/tests/unit/out/unsafe/union_pointer_pun_writethrough.rs b/tests/unit/out/unsafe/union_pointer_pun_writethrough.rs index 40f7088e..aa9b9a25 100644 --- a/tests/unit/out/unsafe/union_pointer_pun_writethrough.rs +++ b/tests/unit/out/unsafe/union_pointer_pun_writethrough.rs @@ -13,12 +13,18 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut x: i64 = (-1_i32 as i64); - #[derive(Copy, Clone, Default)] - pub struct anon_0 { + #[repr(C)] + #[derive(Copy, Clone)] + pub union anon_7_3 { pub as_unsigned: *mut u64, pub as_signed: *mut i64, + } + impl Default for anon_7_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } }; - let mut pp: anon_0 = ::default(); + let mut pp: anon_7_3 = ::default(); pp.as_signed = (&mut x as *mut i64); (*pp.as_unsigned) = 42_u64; assert!(((x) == (42_i64))); diff --git a/tests/unit/out/unsafe/union_struct_dual_use.rs b/tests/unit/out/unsafe/union_struct_dual_use.rs index 76beecf2..6db039ba 100644 --- a/tests/unit/out/unsafe/union_struct_dual_use.rs +++ b/tests/unit/out/unsafe/union_struct_dual_use.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Inner { pub a: i32, @@ -14,14 +15,21 @@ pub struct Inner { pub unsafe fn sum_inner_0(mut i: *mut Inner) -> i32 { return (((*i).a) + ((*i).b)); } +#[repr(C)] #[derive(Copy, Clone)] -pub struct Outer_anon_0 { +pub union Outer_anon_13_3 { pub inner: Inner, pub raw_: [u8; 16], } +impl Default for Outer_anon_13_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer { - pub u: Outer_anon_0, + pub u: Outer_anon_13_3, } pub fn main() { unsafe { diff --git a/tests/unit/out/unsafe/union_tagged_many_arms.rs b/tests/unit/out/unsafe/union_tagged_many_arms.rs index 81817191..79924c08 100644 --- a/tests/unit/out/unsafe/union_tagged_many_arms.rs +++ b/tests/unit/out/unsafe/union_tagged_many_arms.rs @@ -15,18 +15,25 @@ enum Tag { T_FLOAT = 3, T_REF = 4, } -#[derive(Copy, Clone, Default)] -pub struct Slot_anon_0 { +#[repr(C)] +#[derive(Copy, Clone)] +pub union Slot_anon_15_3 { pub text: *const u8, pub handle: *mut ::libc::c_void, pub signed_n: i64, pub unsigned_n: u64, pub f: f64, } +impl Default for Slot_anon_15_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Slot { pub tag: Tag, - pub payload: Slot_anon_0, + pub payload: Slot_anon_15_3, } pub fn main() { unsafe { diff --git a/tests/unit/out/unsafe/union_tagged_simple.rs b/tests/unit/out/unsafe/union_tagged_simple.rs index 0468df97..96c1f9bc 100644 --- a/tests/unit/out/unsafe/union_tagged_simple.rs +++ b/tests/unit/out/unsafe/union_tagged_simple.rs @@ -12,16 +12,23 @@ enum Kind { KIND_NONE = 0, KIND_DONE = 1, } -#[derive(Copy, Clone, Default)] -pub struct Event_anon_0 { +#[repr(C)] +#[derive(Copy, Clone)] +pub union Event_anon_12_3 { pub obj: *mut ::libc::c_void, pub code: i32, } +impl Default for Event_anon_12_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Event { pub kind: Kind, pub handle: *mut ::libc::c_void, - pub payload: Event_anon_0, + pub payload: Event_anon_12_3, } pub fn main() { unsafe { diff --git a/tests/unit/out/unsafe/union_tagged_struct_arms.rs b/tests/unit/out/unsafe/union_tagged_struct_arms.rs index 541a1395..898f45a7 100644 --- a/tests/unit/out/unsafe/union_tagged_struct_arms.rs +++ b/tests/unit/out/unsafe/union_tagged_struct_arms.rs @@ -13,38 +13,48 @@ enum Choice { C_LETTERS = 2, C_INTEGERS = 3, } +#[repr(C)] #[derive(Copy, Clone, Default)] -pub struct Branch_anon_0_anon_0 { +pub struct Branch_anon_14_3_anon_15_5 { pub items: *mut *mut u8, pub count: i64, pub cursor: i64, } +#[repr(C)] #[derive(Copy, Clone, Default)] -pub struct Branch_anon_0_anon_1 { +pub struct Branch_anon_14_3_anon_20_5 { pub lo: i32, pub hi: i32, pub curr: i32, pub step: u8, } +#[repr(C)] #[derive(Copy, Clone, Default)] -pub struct Branch_anon_0_anon_2 { +pub struct Branch_anon_14_3_anon_26_5 { pub lo: i64, pub hi: i64, pub curr: i64, pub step: i64, pub width: i32, } -#[derive(Copy, Clone, Default)] -pub struct Branch_anon_0 { - pub list: Branch_anon_0_anon_0, - pub letters: Branch_anon_0_anon_1, - pub integers: Branch_anon_0_anon_2, +#[repr(C)] +#[derive(Copy, Clone)] +pub union Branch_anon_14_3 { + pub list: Branch_anon_14_3_anon_15_5, + pub letters: Branch_anon_14_3_anon_20_5, + pub integers: Branch_anon_14_3_anon_26_5, +} +impl Default for Branch_anon_14_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Branch { pub choice: Choice, pub index: i32, - pub v: Branch_anon_0, + pub v: Branch_anon_14_3, } pub fn main() { unsafe { diff --git a/tests/unit/out/unsafe/union_void_ptr_sized_deref.rs b/tests/unit/out/unsafe/union_void_ptr_sized_deref.rs index 6b542afb..323fd253 100644 --- a/tests/unit/out/unsafe/union_void_ptr_sized_deref.rs +++ b/tests/unit/out/unsafe/union_void_ptr_sized_deref.rs @@ -13,17 +13,24 @@ enum Width { W_32 = 1, W_16 = 2, } -#[derive(Copy, Clone, Default)] -pub struct Sink_anon_0 { +#[repr(C)] +#[derive(Copy, Clone)] +pub union Sink_anon_14_3 { pub text: *const u8, pub handle: *mut ::libc::c_void, pub signed_n: i64, pub f: f64, } +impl Default for Sink_anon_14_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Sink { pub width: Width, - pub out: Sink_anon_0, + pub out: Sink_anon_14_3, } pub unsafe fn write_count_0(mut s: *mut Sink, mut count: i64) { 'switch: { From f60879df57fa8d238003787ea6155762cba8dc7d Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 27 Apr 2026 21:22:19 +0100 Subject: [PATCH 09/19] Add repr(C) to all transalted unsafe structs --- tests/benchmarks/out/unsafe/bfs.rs | 5 +++++ tests/benchmarks/out/unsafe/bst.rs | 1 + tests/ub/out/unsafe/ub6.rs | 1 + tests/unit/out/unsafe/10_struct.rs | 2 ++ tests/unit/out/unsafe/bounded_struct_ptr.rs | 1 + tests/unit/out/unsafe/bst.rs | 1 + tests/unit/out/unsafe/c_struct.rs | 5 +++++ tests/unit/out/unsafe/class.rs | 2 ++ tests/unit/out/unsafe/clone_vs_move.rs | 2 ++ tests/unit/out/unsafe/complex_function.rs | 4 ++++ tests/unit/out/unsafe/default.rs | 1 + tests/unit/out/unsafe/exprs.rs | 2 ++ tests/unit/out/unsafe/fft.rs | 1 + tests/unit/out/unsafe/fn_ptr_cast.rs | 1 + tests/unit/out/unsafe/fn_ptr_stable_sort.rs | 1 + tests/unit/out/unsafe/fn_ptr_struct.rs | 1 + tests/unit/out/unsafe/fn_ptr_vtable.rs | 1 + tests/unit/out/unsafe/foreach_disjoint_field_borrow.rs | 1 + tests/unit/out/unsafe/function_overloading.rs | 1 + tests/unit/out/unsafe/huffman.rs | 2 ++ tests/unit/out/unsafe/immutable-deref-on-func-call.rs | 1 + tests/unit/out/unsafe/init.rs | 1 + tests/unit/out/unsafe/kruskal.rs | 3 +++ tests/unit/out/unsafe/linked_list.rs | 1 + tests/unit/out/unsafe/nested_structs.rs | 6 ++++++ tests/unit/out/unsafe/new_bst.rs | 1 + tests/unit/out/unsafe/new_struct.rs | 1 + tests/unit/out/unsafe/operator_less_than.rs | 1 + tests/unit/out/unsafe/pod.rs | 1 + tests/unit/out/unsafe/pointer_array.rs | 1 + tests/unit/out/unsafe/pointers.rs | 1 + tests/unit/out/unsafe/polymorphism.rs | 2 ++ tests/unit/out/unsafe/push_emplace_back.rs | 3 +++ tests/unit/out/unsafe/random.rs | 2 ++ tests/unit/out/unsafe/reinterpret_cast_struct.rs | 1 + tests/unit/out/unsafe/reinterpret_cast_struct_to_struct.rs | 2 ++ tests/unit/out/unsafe/reserved_keywords.rs | 1 + tests/unit/out/unsafe/struct_ctor.rs | 1 + tests/unit/out/unsafe/struct_default_ctor.rs | 1 + tests/unit/out/unsafe/struct_ptr.rs | 1 + tests/unit/out/unsafe/unique_ptr.rs | 2 ++ tests/unit/out/unsafe/unique_ptr_const_deref.rs | 1 + tests/unit/out/unsafe/unique_ptr_nested.rs | 2 ++ tests/unit/out/unsafe/unique_ptr_struct.rs | 1 + tests/unit/out/unsafe/va_arg_struct_ctx.rs | 1 + 45 files changed, 75 insertions(+) diff --git a/tests/benchmarks/out/unsafe/bfs.rs b/tests/benchmarks/out/unsafe/bfs.rs index 6fcf07ca..e1a44a4c 100644 --- a/tests/benchmarks/out/unsafe/bfs.rs +++ b/tests/benchmarks/out/unsafe/bfs.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Queue { pub elems: *mut u32, @@ -35,11 +36,13 @@ impl Queue { return ((self.back) == (0_u64)); } } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct GraphNode { pub vertex: u32, pub next: *mut GraphNode, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Graph { pub V: u32, @@ -108,6 +111,7 @@ pub unsafe fn BFS_0(graph: *const Graph, mut start_vertex: u32) -> *mut u32 { head = (*head).next; } } + ::std::mem::drop(Box::from_raw(::std::slice::from_raw_parts_mut( visited, libcc2rs::malloc_usable_size(visited as *mut ::libc::c_void) @@ -169,6 +173,7 @@ unsafe fn main_0() -> i32 { } i.prefix_inc(); } + ::std::mem::drop(Box::from_raw(::std::slice::from_raw_parts_mut( graph.adj, libcc2rs::malloc_usable_size(graph.adj as *mut ::libc::c_void) diff --git a/tests/benchmarks/out/unsafe/bst.rs b/tests/benchmarks/out/unsafe/bst.rs index f3810641..769f040a 100644 --- a/tests/benchmarks/out/unsafe/bst.rs +++ b/tests/benchmarks/out/unsafe/bst.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct node_t { pub left: *mut node_t, diff --git a/tests/ub/out/unsafe/ub6.rs b/tests/ub/out/unsafe/ub6.rs index 43b6731d..1d276b7c 100644 --- a/tests/ub/out/unsafe/ub6.rs +++ b/tests/ub/out/unsafe/ub6.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Pair { pub x1: *mut i32, diff --git a/tests/unit/out/unsafe/10_struct.rs b/tests/unit/out/unsafe/10_struct.rs index afb5f3bf..717ba9f5 100644 --- a/tests/unit/out/unsafe/10_struct.rs +++ b/tests/unit/out/unsafe/10_struct.rs @@ -6,11 +6,13 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct GraphNode { pub dst: u32, pub next: *mut GraphNode, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Graph { pub V: u32, diff --git a/tests/unit/out/unsafe/bounded_struct_ptr.rs b/tests/unit/out/unsafe/bounded_struct_ptr.rs index 10f11398..e5acc6a7 100644 --- a/tests/unit/out/unsafe/bounded_struct_ptr.rs +++ b/tests/unit/out/unsafe/bounded_struct_ptr.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Foo { pub x1: i32, diff --git a/tests/unit/out/unsafe/bst.rs b/tests/unit/out/unsafe/bst.rs index 1d9406ff..0067a0fe 100644 --- a/tests/unit/out/unsafe/bst.rs +++ b/tests/unit/out/unsafe/bst.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct node_t { pub left: *mut node_t, diff --git a/tests/unit/out/unsafe/c_struct.rs b/tests/unit/out/unsafe/c_struct.rs index 3d12a8fd..1d2fcd32 100644 --- a/tests/unit/out/unsafe/c_struct.rs +++ b/tests/unit/out/unsafe/c_struct.rs @@ -6,16 +6,19 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Point { pub x: i32, pub y: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Line { pub start: Point, pub end: Point, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Node { pub value: i32, @@ -28,11 +31,13 @@ enum Color { GREEN = 1, BLUE = 2, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Inner { pub a: i32, pub b: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Container { pub inner: Inner, diff --git a/tests/unit/out/unsafe/class.rs b/tests/unit/out/unsafe/class.rs index aacf2489..2e098d4f 100644 --- a/tests/unit/out/unsafe/class.rs +++ b/tests/unit/out/unsafe/class.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Pair { pub first: i32, @@ -42,6 +43,7 @@ impl Pair { })); } } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Route { pub path: Pair, diff --git a/tests/unit/out/unsafe/clone_vs_move.rs b/tests/unit/out/unsafe/clone_vs_move.rs index 5f782e3e..7dcb0938 100644 --- a/tests/unit/out/unsafe/clone_vs_move.rs +++ b/tests/unit/out/unsafe/clone_vs_move.rs @@ -6,10 +6,12 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Bar { pub w: i32, } +#[repr(C)] #[derive(Copy, Clone)] pub struct Foo { pub x: i32, diff --git a/tests/unit/out/unsafe/complex_function.rs b/tests/unit/out/unsafe/complex_function.rs index fe27d543..62b15075 100644 --- a/tests/unit/out/unsafe/complex_function.rs +++ b/tests/unit/out/unsafe/complex_function.rs @@ -15,10 +15,12 @@ pub unsafe fn ptr_1(mut x: *mut i32) -> *mut i32 { pub unsafe fn bar_2(x: *mut i32) -> *mut i32 { return x; } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct X1 { pub v: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct X2 { pub v: *mut X1, @@ -28,6 +30,7 @@ impl X2 { return self.v; } } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct X3 { pub v: *mut X2, @@ -37,6 +40,7 @@ impl X3 { return self.v; } } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct X4 { pub v: X3, diff --git a/tests/unit/out/unsafe/default.rs b/tests/unit/out/unsafe/default.rs index aa31c35c..6ccbb6af 100644 --- a/tests/unit/out/unsafe/default.rs +++ b/tests/unit/out/unsafe/default.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone)] pub struct Pointers { pub x1: *mut i32, diff --git a/tests/unit/out/unsafe/exprs.rs b/tests/unit/out/unsafe/exprs.rs index c6643731..c8161b85 100644 --- a/tests/unit/out/unsafe/exprs.rs +++ b/tests/unit/out/unsafe/exprs.rs @@ -6,10 +6,12 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct X { pub x: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Y { pub x: X, diff --git a/tests/unit/out/unsafe/fft.rs b/tests/unit/out/unsafe/fft.rs index 564131af..3235a4bd 100644 --- a/tests/unit/out/unsafe/fft.rs +++ b/tests/unit/out/unsafe/fft.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Complex { pub re: f64, diff --git a/tests/unit/out/unsafe/fn_ptr_cast.rs b/tests/unit/out/unsafe/fn_ptr_cast.rs index f9178b9c..9951cc7b 100644 --- a/tests/unit/out/unsafe/fn_ptr_cast.rs +++ b/tests/unit/out/unsafe/fn_ptr_cast.rs @@ -44,6 +44,7 @@ pub unsafe fn test_double_cast_2() { ); assert!(((fn2) == (fn_))); } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Command { pub data: *mut ::libc::c_void, diff --git a/tests/unit/out/unsafe/fn_ptr_stable_sort.rs b/tests/unit/out/unsafe/fn_ptr_stable_sort.rs index 4f8cb21e..f1fb136d 100644 --- a/tests/unit/out/unsafe/fn_ptr_stable_sort.rs +++ b/tests/unit/out/unsafe/fn_ptr_stable_sort.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Item { pub key: i32, diff --git a/tests/unit/out/unsafe/fn_ptr_struct.rs b/tests/unit/out/unsafe/fn_ptr_struct.rs index 4760d91c..6e4a0466 100644 --- a/tests/unit/out/unsafe/fn_ptr_struct.rs +++ b/tests/unit/out/unsafe/fn_ptr_struct.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Clone)] pub struct Handler { pub tag: i32, diff --git a/tests/unit/out/unsafe/fn_ptr_vtable.rs b/tests/unit/out/unsafe/fn_ptr_vtable.rs index 1beb5c33..25b91dca 100644 --- a/tests/unit/out/unsafe/fn_ptr_vtable.rs +++ b/tests/unit/out/unsafe/fn_ptr_vtable.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Clone)] pub struct Vtable { pub create: Option *mut ::libc::c_void>, diff --git a/tests/unit/out/unsafe/foreach_disjoint_field_borrow.rs b/tests/unit/out/unsafe/foreach_disjoint_field_borrow.rs index dd8f8325..93399ada 100644 --- a/tests/unit/out/unsafe/foreach_disjoint_field_borrow.rs +++ b/tests/unit/out/unsafe/foreach_disjoint_field_borrow.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Clone, Default)] pub struct S { pub v: Vec, diff --git a/tests/unit/out/unsafe/function_overloading.rs b/tests/unit/out/unsafe/function_overloading.rs index 4b0cb1fa..effb483d 100644 --- a/tests/unit/out/unsafe/function_overloading.rs +++ b/tests/unit/out/unsafe/function_overloading.rs @@ -21,6 +21,7 @@ pub unsafe fn foo_3(mut x: *mut i32, mut y: *mut i32, z: *mut i32) -> i32 { pub unsafe fn bar_4(x: *mut i32) -> i32 { return (*x); } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Foo {} impl Foo { diff --git a/tests/unit/out/unsafe/huffman.rs b/tests/unit/out/unsafe/huffman.rs index 962d5690..d9da3862 100644 --- a/tests/unit/out/unsafe/huffman.rs +++ b/tests/unit/out/unsafe/huffman.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct MinHeapNode { pub data: u8, @@ -40,6 +41,7 @@ pub unsafe fn Swap_0(a: *mut MinHeapNode, b: *mut MinHeapNode) { }) .clone(); } +#[repr(C)] #[derive(Default)] pub struct MinHeap { pub size: i32, diff --git a/tests/unit/out/unsafe/immutable-deref-on-func-call.rs b/tests/unit/out/unsafe/immutable-deref-on-func-call.rs index 85a4fcfd..3a917620 100644 --- a/tests/unit/out/unsafe/immutable-deref-on-func-call.rs +++ b/tests/unit/out/unsafe/immutable-deref-on-func-call.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Item { pub value: i32, diff --git a/tests/unit/out/unsafe/init.rs b/tests/unit/out/unsafe/init.rs index fe3cc58a..c43dbde4 100644 --- a/tests/unit/out/unsafe/init.rs +++ b/tests/unit/out/unsafe/init.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct X { pub x: i32, diff --git a/tests/unit/out/unsafe/kruskal.rs b/tests/unit/out/unsafe/kruskal.rs index d6b5f346..4ae16259 100644 --- a/tests/unit/out/unsafe/kruskal.rs +++ b/tests/unit/out/unsafe/kruskal.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Edge { pub u: i32, @@ -93,6 +94,7 @@ pub unsafe fn quicksort_1(arr: *mut Option>, mut start: i32, mut end quicksort_1(_arr, _start, _end) }); } +#[repr(C)] #[derive(Default)] pub struct DisjointSet { pub rank: Option>, @@ -144,6 +146,7 @@ impl DisjointSet { } } } +#[repr(C)] #[derive(Default)] pub struct Graph { pub edges: Option>, diff --git a/tests/unit/out/unsafe/linked_list.rs b/tests/unit/out/unsafe/linked_list.rs index 3c46970c..b9141c36 100644 --- a/tests/unit/out/unsafe/linked_list.rs +++ b/tests/unit/out/unsafe/linked_list.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Node { pub val: i32, diff --git a/tests/unit/out/unsafe/nested_structs.rs b/tests/unit/out/unsafe/nested_structs.rs index 6d018fd7..b93fed2d 100644 --- a/tests/unit/out/unsafe/nested_structs.rs +++ b/tests/unit/out/unsafe/nested_structs.rs @@ -6,28 +6,34 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Level0_Level1_1_Level2_1_Level3_1 { pub x1: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Level0_Level1_1_Level2_1_Level3_2 { pub x1: i32, pub x2: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Level0_Level1_1_Level2_1 { pub x1: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Level0_Level1_1 { pub x1: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Level0_Level1_2 { pub x1: i32, pub x2: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Level0 {} pub fn main() { diff --git a/tests/unit/out/unsafe/new_bst.rs b/tests/unit/out/unsafe/new_bst.rs index 441bd7a7..cb5a75cd 100644 --- a/tests/unit/out/unsafe/new_bst.rs +++ b/tests/unit/out/unsafe/new_bst.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct node_t { pub left: *mut node_t, diff --git a/tests/unit/out/unsafe/new_struct.rs b/tests/unit/out/unsafe/new_struct.rs index 5adf5151..ed91969b 100644 --- a/tests/unit/out/unsafe/new_struct.rs +++ b/tests/unit/out/unsafe/new_struct.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Pair { pub x: i32, diff --git a/tests/unit/out/unsafe/operator_less_than.rs b/tests/unit/out/unsafe/operator_less_than.rs index 0665ca14..d183314f 100644 --- a/tests/unit/out/unsafe/operator_less_than.rs +++ b/tests/unit/out/unsafe/operator_less_than.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Pair { pub x: i32, diff --git a/tests/unit/out/unsafe/pod.rs b/tests/unit/out/unsafe/pod.rs index 0039489f..337aadab 100644 --- a/tests/unit/out/unsafe/pod.rs +++ b/tests/unit/out/unsafe/pod.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct POD { pub x1: i32, diff --git a/tests/unit/out/unsafe/pointer_array.rs b/tests/unit/out/unsafe/pointer_array.rs index 2dd1090c..3d3d82e7 100644 --- a/tests/unit/out/unsafe/pointer_array.rs +++ b/tests/unit/out/unsafe/pointer_array.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone)] pub struct StackArray { pub arr: [*mut i32; 3], diff --git a/tests/unit/out/unsafe/pointers.rs b/tests/unit/out/unsafe/pointers.rs index 7bbc2eb5..0b5e1a45 100644 --- a/tests/unit/out/unsafe/pointers.rs +++ b/tests/unit/out/unsafe/pointers.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Test { pub x: i32, diff --git a/tests/unit/out/unsafe/polymorphism.rs b/tests/unit/out/unsafe/polymorphism.rs index ee123ce9..3152dceb 100644 --- a/tests/unit/out/unsafe/polymorphism.rs +++ b/tests/unit/out/unsafe/polymorphism.rs @@ -9,6 +9,7 @@ use std::rc::Rc; pub unsafe trait Animal { unsafe fn bark(&self) -> bool; } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Dog {} unsafe impl Animal for Dog { @@ -16,6 +17,7 @@ unsafe impl Animal for Dog { return true; } } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Cat {} impl Cat { diff --git a/tests/unit/out/unsafe/push_emplace_back.rs b/tests/unit/out/unsafe/push_emplace_back.rs index afa6f328..35c60983 100644 --- a/tests/unit/out/unsafe/push_emplace_back.rs +++ b/tests/unit/out/unsafe/push_emplace_back.rs @@ -6,15 +6,18 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Chunk { pub data: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Writer { pub output: *mut Vec, pub chunk: Chunk, } +#[repr(C)] #[derive(Clone, Default)] pub struct JPEGData { pub com_data: Vec>, diff --git a/tests/unit/out/unsafe/random.rs b/tests/unit/out/unsafe/random.rs index 773659dd..2ff83a5c 100644 --- a/tests/unit/out/unsafe/random.rs +++ b/tests/unit/out/unsafe/random.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone)] pub struct Pair { pub x: i32, @@ -52,6 +53,7 @@ impl Default for Pair { pub unsafe fn zero_0() -> i32 { return 0; } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct X1 {} pub unsafe fn foo_1(mut x1: i32, x2: *mut i32, mut x3: *mut i32, p2: *mut Pair, mut p3: *mut Pair) { diff --git a/tests/unit/out/unsafe/reinterpret_cast_struct.rs b/tests/unit/out/unsafe/reinterpret_cast_struct.rs index 04b9f91e..76cdd709 100644 --- a/tests/unit/out/unsafe/reinterpret_cast_struct.rs +++ b/tests/unit/out/unsafe/reinterpret_cast_struct.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Point { pub x: i32, diff --git a/tests/unit/out/unsafe/reinterpret_cast_struct_to_struct.rs b/tests/unit/out/unsafe/reinterpret_cast_struct_to_struct.rs index 5209888f..21852fbe 100644 --- a/tests/unit/out/unsafe/reinterpret_cast_struct_to_struct.rs +++ b/tests/unit/out/unsafe/reinterpret_cast_struct_to_struct.rs @@ -6,11 +6,13 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Point { pub x: u32, pub y: u32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Pair { pub first: u32, diff --git a/tests/unit/out/unsafe/reserved_keywords.rs b/tests/unit/out/unsafe/reserved_keywords.rs index 998bb849..d90eabda 100644 --- a/tests/unit/out/unsafe/reserved_keywords.rs +++ b/tests/unit/out/unsafe/reserved_keywords.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct S { pub as_: i32, diff --git a/tests/unit/out/unsafe/struct_ctor.rs b/tests/unit/out/unsafe/struct_ctor.rs index a6e534cd..ffcf6f82 100644 --- a/tests/unit/out/unsafe/struct_ctor.rs +++ b/tests/unit/out/unsafe/struct_ctor.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct StructWithCtor { x1_: i32, diff --git a/tests/unit/out/unsafe/struct_default_ctor.rs b/tests/unit/out/unsafe/struct_default_ctor.rs index accfa3af..bd1ccbf2 100644 --- a/tests/unit/out/unsafe/struct_default_ctor.rs +++ b/tests/unit/out/unsafe/struct_default_ctor.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Clone)] pub struct WOFF2Params { pub extended_metadata: Vec, diff --git a/tests/unit/out/unsafe/struct_ptr.rs b/tests/unit/out/unsafe/struct_ptr.rs index ee77901c..c0d49d77 100644 --- a/tests/unit/out/unsafe/struct_ptr.rs +++ b/tests/unit/out/unsafe/struct_ptr.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct XX { pub x: i32, diff --git a/tests/unit/out/unsafe/unique_ptr.rs b/tests/unit/out/unsafe/unique_ptr.rs index 1582c2c8..dba60cdd 100644 --- a/tests/unit/out/unsafe/unique_ptr.rs +++ b/tests/unit/out/unsafe/unique_ptr.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Default)] pub struct SafePointer { pub ptr: Option>, @@ -15,6 +16,7 @@ impl SafePointer { (*self.ptr.as_deref_mut().unwrap()).prefix_inc(); } } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Pair { pub x: i32, diff --git a/tests/unit/out/unsafe/unique_ptr_const_deref.rs b/tests/unit/out/unsafe/unique_ptr_const_deref.rs index 4c4c64a9..b7a96774 100644 --- a/tests/unit/out/unsafe/unique_ptr_const_deref.rs +++ b/tests/unit/out/unsafe/unique_ptr_const_deref.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Default)] pub struct Holder { pub val: Option>, diff --git a/tests/unit/out/unsafe/unique_ptr_nested.rs b/tests/unit/out/unsafe/unique_ptr_nested.rs index 44448ae3..e316f9f8 100644 --- a/tests/unit/out/unsafe/unique_ptr_nested.rs +++ b/tests/unit/out/unsafe/unique_ptr_nested.rs @@ -6,11 +6,13 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Inner { pub x: i32, pub y: i32, } +#[repr(C)] #[derive(Default)] pub struct Outer { pub inner: Option>, diff --git a/tests/unit/out/unsafe/unique_ptr_struct.rs b/tests/unit/out/unsafe/unique_ptr_struct.rs index e9da7738..726d5da2 100644 --- a/tests/unit/out/unsafe/unique_ptr_struct.rs +++ b/tests/unit/out/unsafe/unique_ptr_struct.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Point { pub x: i32, diff --git a/tests/unit/out/unsafe/va_arg_struct_ctx.rs b/tests/unit/out/unsafe/va_arg_struct_ctx.rs index edc3c758..1b7030b1 100644 --- a/tests/unit/out/unsafe/va_arg_struct_ctx.rs +++ b/tests/unit/out/unsafe/va_arg_struct_ctx.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct context { pub verbose: i32, From 97a00fa8e5dc2e7c3dc6e75cd018be90d3971656 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 27 Apr 2026 22:08:14 +0100 Subject: [PATCH 10/19] Handle anonymous structs inside records --- cpp2rust/converter/converter.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index b008e62a..1bf26900 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -2881,10 +2881,6 @@ std::string Converter::GetRecordName(const clang::NamedDecl *decl) const { if (auto it = inner_structs_.find(ID); it != inner_structs_.end()) { return it->second; } - if (auto *record = clang::dyn_cast(decl); - record && !record->getIdentifier()) { - return ID; - } return std::regex_replace(Mapper::ToString(decl), std::regex("::"), "_"); } From 4f55f7d35f62da2fb6a5d8e387e40cf764209e10 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 27 Apr 2026 22:08:39 +0100 Subject: [PATCH 11/19] Update tests --- .../unit/out/refcount/union_cross_arm_cast.rs | 8 +++--- .../out/refcount/union_field_alignment.rs | 8 +++--- .../out/refcount/union_flex_array_member.rs | 8 +++--- .../unit/out/refcount/union_memset_memcpy.rs | 8 +++--- tests/unit/out/refcount/union_nested.rs | 16 ++++++------ .../out/refcount/union_pointer_pun_address.rs | 8 +++--- .../union_pointer_pun_writethrough.rs | 8 +++--- .../out/refcount/union_struct_dual_use.rs | 8 +++--- .../out/refcount/union_tagged_many_arms.rs | 8 +++--- .../unit/out/refcount/union_tagged_simple.rs | 8 +++--- .../out/refcount/union_tagged_struct_arms.rs | 26 +++++++++---------- .../refcount/union_void_ptr_sized_deref.rs | 8 +++--- .../unit/out/unsafe/union_addrof_external.rs | 10 +++---- tests/unit/out/unsafe/union_cross_arm_cast.rs | 6 ++--- .../unit/out/unsafe/union_field_alignment.rs | 6 ++--- .../out/unsafe/union_flex_array_member.rs | 6 ++--- tests/unit/out/unsafe/union_memset_memcpy.rs | 6 ++--- tests/unit/out/unsafe/union_nested.rs | 12 ++++----- .../out/unsafe/union_pointer_pun_address.rs | 6 ++--- .../unsafe/union_pointer_pun_writethrough.rs | 6 ++--- .../unit/out/unsafe/union_struct_dual_use.rs | 6 ++--- .../unit/out/unsafe/union_tagged_many_arms.rs | 6 ++--- tests/unit/out/unsafe/union_tagged_simple.rs | 6 ++--- .../out/unsafe/union_tagged_struct_arms.rs | 18 ++++++------- .../out/unsafe/union_void_ptr_sized_deref.rs | 6 ++--- 25 files changed, 111 insertions(+), 111 deletions(-) diff --git a/tests/unit/out/refcount/union_cross_arm_cast.rs b/tests/unit/out/refcount/union_cross_arm_cast.rs index 06470059..e87e9009 100644 --- a/tests/unit/out/refcount/union_cross_arm_cast.rs +++ b/tests/unit/out/refcount/union_cross_arm_cast.rs @@ -45,21 +45,21 @@ impl Default for shape_b { } impl ByteRepr for shape_b {} #[derive()] -pub union Container_anon_22_3 { +pub union Container_anon_0 { pub a: Value, pub b: Value, pub raw_: Value>, } -impl Default for Container_anon_22_3 { +impl Default for Container_anon_0 { fn default() -> Self { unsafe { std::mem::zeroed() } } } -impl ByteRepr for Container_anon_22_3 {} +impl ByteRepr for Container_anon_0 {} #[derive(Default)] pub struct Container { pub len: Value, - pub u: Value, + pub u: Value, } impl ByteRepr for Container {} pub fn main() { diff --git a/tests/unit/out/refcount/union_field_alignment.rs b/tests/unit/out/refcount/union_field_alignment.rs index 24abc749..fa803699 100644 --- a/tests/unit/out/refcount/union_field_alignment.rs +++ b/tests/unit/out/refcount/union_field_alignment.rs @@ -7,20 +7,20 @@ use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; #[derive()] -pub union node_anon_10_3 { +pub union node_anon_0 { pub bytes: Value>, pub aligner: Value, } -impl Default for node_anon_10_3 { +impl Default for node_anon_0 { fn default() -> Self { unsafe { std::mem::zeroed() } } } -impl ByteRepr for node_anon_10_3 {} +impl ByteRepr for node_anon_0 {} #[derive(Default)] pub struct node { pub next: Value>, - pub x: Value, + pub x: Value, } impl ByteRepr for node {} pub fn main() { diff --git a/tests/unit/out/refcount/union_flex_array_member.rs b/tests/unit/out/refcount/union_flex_array_member.rs index d42f23ad..f3cf1a57 100644 --- a/tests/unit/out/refcount/union_flex_array_member.rs +++ b/tests/unit/out/refcount/union_flex_array_member.rs @@ -7,20 +7,20 @@ use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; #[derive()] -pub union node_anon_10_3 { +pub union node_anon_0 { pub bytes: Value>, pub aligner: Value, } -impl Default for node_anon_10_3 { +impl Default for node_anon_0 { fn default() -> Self { unsafe { std::mem::zeroed() } } } -impl ByteRepr for node_anon_10_3 {} +impl ByteRepr for node_anon_0 {} #[derive(Default)] pub struct node { pub len: Value, - pub x: Value, + pub x: Value, } impl ByteRepr for node {} pub fn main() { diff --git a/tests/unit/out/refcount/union_memset_memcpy.rs b/tests/unit/out/refcount/union_memset_memcpy.rs index 309f6f52..fa1190d1 100644 --- a/tests/unit/out/refcount/union_memset_memcpy.rs +++ b/tests/unit/out/refcount/union_memset_memcpy.rs @@ -43,20 +43,20 @@ impl Default for shape_b { } impl ByteRepr for shape_b {} #[derive()] -pub union Container_anon_20_3 { +pub union Container_anon_0 { pub a: Value, pub b: Value, pub raw_: Value>, } -impl Default for Container_anon_20_3 { +impl Default for Container_anon_0 { fn default() -> Self { unsafe { std::mem::zeroed() } } } -impl ByteRepr for Container_anon_20_3 {} +impl ByteRepr for Container_anon_0 {} #[derive(Default)] pub struct Container { - pub view: Value, + pub view: Value, } impl ByteRepr for Container {} pub fn main() { diff --git a/tests/unit/out/refcount/union_nested.rs b/tests/unit/out/refcount/union_nested.rs index a2359b77..67b635f1 100644 --- a/tests/unit/out/refcount/union_nested.rs +++ b/tests/unit/out/refcount/union_nested.rs @@ -23,39 +23,39 @@ impl Default for record { } impl ByteRepr for record {} #[derive()] -pub union inner_anon_12_3 { +pub union inner_anon_0 { pub h: Value, pub raw_: Value>, } -impl Default for inner_anon_12_3 { +impl Default for inner_anon_0 { fn default() -> Self { unsafe { std::mem::zeroed() } } } -impl ByteRepr for inner_anon_12_3 {} +impl ByteRepr for inner_anon_0 {} #[derive(Default)] pub struct inner { - pub view: Value, + pub view: Value, } impl ByteRepr for inner {} #[derive(Default)] -pub union Outer_anon_23_3 { +pub union Outer_anon_0 { pub h: Value, pub nested: Value, } -impl Default for Outer_anon_23_3 { +impl Default for Outer_anon_0 { fn default() -> Self { unsafe { std::mem::zeroed() } } } -impl ByteRepr for Outer_anon_23_3 {} +impl ByteRepr for Outer_anon_0 {} #[derive(Default)] pub struct Outer { pub kind: Value, pub level: Value, pub variant: Value, pub len: Value, - pub body: Value, + pub body: Value, } impl ByteRepr for Outer {} pub fn main() { diff --git a/tests/unit/out/refcount/union_pointer_pun_address.rs b/tests/unit/out/refcount/union_pointer_pun_address.rs index 249aeb9b..354ac36a 100644 --- a/tests/unit/out/refcount/union_pointer_pun_address.rs +++ b/tests/unit/out/refcount/union_pointer_pun_address.rs @@ -25,17 +25,17 @@ fn main_0() -> i32 { n: Rc::new(RefCell::new(123)), })); #[derive(Default)] - pub union anon_16_3 { + pub union anon_0 { pub to_a: Value>, pub to_b: Value>, } - impl Default for anon_16_3 { + impl Default for anon_0 { fn default() -> Self { unsafe { std::mem::zeroed() } } } - impl ByteRepr for anon_16_3 {}; - let ptr: Value = >::default(); + impl ByteRepr for anon_0 {}; + let ptr: Value = >::default(); (*(*ptr.borrow()).to_a.borrow_mut()) = (a.as_pointer()); let out: Value> = Rc::new(RefCell::new((*(*ptr.borrow()).to_b.borrow()).clone())); assert!({ diff --git a/tests/unit/out/refcount/union_pointer_pun_writethrough.rs b/tests/unit/out/refcount/union_pointer_pun_writethrough.rs index 12054307..ceaaea09 100644 --- a/tests/unit/out/refcount/union_pointer_pun_writethrough.rs +++ b/tests/unit/out/refcount/union_pointer_pun_writethrough.rs @@ -12,17 +12,17 @@ pub fn main() { fn main_0() -> i32 { let x: Value = Rc::new(RefCell::new((-1_i32 as i64))); #[derive(Default)] - pub union anon_7_3 { + pub union anon_0 { pub as_unsigned: Value>, pub as_signed: Value>, } - impl Default for anon_7_3 { + impl Default for anon_0 { fn default() -> Self { unsafe { std::mem::zeroed() } } } - impl ByteRepr for anon_7_3 {}; - let pp: Value = >::default(); + impl ByteRepr for anon_0 {}; + let pp: Value = >::default(); (*(*pp.borrow()).as_signed.borrow_mut()) = (x.as_pointer()); (*(*pp.borrow()).as_unsigned.borrow()).write(42_u64); assert!(((*x.borrow()) == 42_i64)); diff --git a/tests/unit/out/refcount/union_struct_dual_use.rs b/tests/unit/out/refcount/union_struct_dual_use.rs index 9100f8d8..a6b4b913 100644 --- a/tests/unit/out/refcount/union_struct_dual_use.rs +++ b/tests/unit/out/refcount/union_struct_dual_use.rs @@ -20,19 +20,19 @@ pub fn sum_inner_0(i: Ptr) -> i32 { }; } #[derive()] -pub union Outer_anon_13_3 { +pub union Outer_anon_0 { pub inner: Value, pub raw_: Value>, } -impl Default for Outer_anon_13_3 { +impl Default for Outer_anon_0 { fn default() -> Self { unsafe { std::mem::zeroed() } } } -impl ByteRepr for Outer_anon_13_3 {} +impl ByteRepr for Outer_anon_0 {} #[derive(Default)] pub struct Outer { - pub u: Value, + pub u: Value, } impl ByteRepr for Outer {} pub fn main() { diff --git a/tests/unit/out/refcount/union_tagged_many_arms.rs b/tests/unit/out/refcount/union_tagged_many_arms.rs index 17c690bf..97137bfc 100644 --- a/tests/unit/out/refcount/union_tagged_many_arms.rs +++ b/tests/unit/out/refcount/union_tagged_many_arms.rs @@ -16,23 +16,23 @@ enum Tag { T_REF = 4, } #[derive(Default)] -pub union Slot_anon_15_3 { +pub union Slot_anon_0 { pub text: Value>, pub handle: Value, pub signed_n: Value, pub unsigned_n: Value, pub f: Value, } -impl Default for Slot_anon_15_3 { +impl Default for Slot_anon_0 { fn default() -> Self { unsafe { std::mem::zeroed() } } } -impl ByteRepr for Slot_anon_15_3 {} +impl ByteRepr for Slot_anon_0 {} #[derive(Default)] pub struct Slot { pub tag: Value, - pub payload: Value, + pub payload: Value, } impl ByteRepr for Slot {} pub fn main() { diff --git a/tests/unit/out/refcount/union_tagged_simple.rs b/tests/unit/out/refcount/union_tagged_simple.rs index dd7baa33..6d7806c5 100644 --- a/tests/unit/out/refcount/union_tagged_simple.rs +++ b/tests/unit/out/refcount/union_tagged_simple.rs @@ -13,21 +13,21 @@ enum Kind { KIND_DONE = 1, } #[derive(Default)] -pub union Event_anon_12_3 { +pub union Event_anon_0 { pub obj: Value, pub code: Value, } -impl Default for Event_anon_12_3 { +impl Default for Event_anon_0 { fn default() -> Self { unsafe { std::mem::zeroed() } } } -impl ByteRepr for Event_anon_12_3 {} +impl ByteRepr for Event_anon_0 {} #[derive(Default)] pub struct Event { pub kind: Value, pub handle: Value, - pub payload: Value, + pub payload: Value, } impl ByteRepr for Event {} pub fn main() { diff --git a/tests/unit/out/refcount/union_tagged_struct_arms.rs b/tests/unit/out/refcount/union_tagged_struct_arms.rs index 9e94541e..f38467e2 100644 --- a/tests/unit/out/refcount/union_tagged_struct_arms.rs +++ b/tests/unit/out/refcount/union_tagged_struct_arms.rs @@ -14,46 +14,46 @@ enum Choice { C_INTEGERS = 3, } #[derive(Default)] -pub struct Branch_anon_14_3_anon_15_5 { +pub struct Branch_anon_0_anon_0 { pub items: Value>>, pub count: Value, pub cursor: Value, } -impl ByteRepr for Branch_anon_14_3_anon_15_5 {} +impl ByteRepr for Branch_anon_0_anon_0 {} #[derive(Default)] -pub struct Branch_anon_14_3_anon_20_5 { +pub struct Branch_anon_0_anon_1 { pub lo: Value, pub hi: Value, pub curr: Value, pub step: Value, } -impl ByteRepr for Branch_anon_14_3_anon_20_5 {} +impl ByteRepr for Branch_anon_0_anon_1 {} #[derive(Default)] -pub struct Branch_anon_14_3_anon_26_5 { +pub struct Branch_anon_0_anon_2 { pub lo: Value, pub hi: Value, pub curr: Value, pub step: Value, pub width: Value, } -impl ByteRepr for Branch_anon_14_3_anon_26_5 {} +impl ByteRepr for Branch_anon_0_anon_2 {} #[derive(Default)] -pub union Branch_anon_14_3 { - pub list: Value, - pub letters: Value, - pub integers: Value, +pub union Branch_anon_0 { + pub list: Value, + pub letters: Value, + pub integers: Value, } -impl Default for Branch_anon_14_3 { +impl Default for Branch_anon_0 { fn default() -> Self { unsafe { std::mem::zeroed() } } } -impl ByteRepr for Branch_anon_14_3 {} +impl ByteRepr for Branch_anon_0 {} #[derive(Default)] pub struct Branch { pub choice: Value, pub index: Value, - pub v: Value, + pub v: Value, } impl ByteRepr for Branch {} pub fn main() { diff --git a/tests/unit/out/refcount/union_void_ptr_sized_deref.rs b/tests/unit/out/refcount/union_void_ptr_sized_deref.rs index cd30a604..c3fe2bbe 100644 --- a/tests/unit/out/refcount/union_void_ptr_sized_deref.rs +++ b/tests/unit/out/refcount/union_void_ptr_sized_deref.rs @@ -14,22 +14,22 @@ enum Width { W_16 = 2, } #[derive(Default)] -pub union Sink_anon_14_3 { +pub union Sink_anon_0 { pub text: Value>, pub handle: Value, pub signed_n: Value, pub f: Value, } -impl Default for Sink_anon_14_3 { +impl Default for Sink_anon_0 { fn default() -> Self { unsafe { std::mem::zeroed() } } } -impl ByteRepr for Sink_anon_14_3 {} +impl ByteRepr for Sink_anon_0 {} #[derive(Default)] pub struct Sink { pub width: Value, - pub out: Value, + pub out: Value, } impl ByteRepr for Sink {} pub fn write_count_0(s: Ptr, count: i64) { diff --git a/tests/unit/out/unsafe/union_addrof_external.rs b/tests/unit/out/unsafe/union_addrof_external.rs index bbaa44a8..c53e5a37 100644 --- a/tests/unit/out/unsafe/union_addrof_external.rs +++ b/tests/unit/out/unsafe/union_addrof_external.rs @@ -26,11 +26,11 @@ impl Default for record { } #[repr(C)] #[derive(Copy, Clone)] -pub union Container_anon_15_3 { +pub union Container_anon_0 { pub h: record, pub raw_: [u8; 128], } -impl Default for Container_anon_15_3 { +impl Default for Container_anon_0 { fn default() -> Self { unsafe { std::mem::zeroed() } } @@ -38,7 +38,7 @@ impl Default for Container_anon_15_3 { #[repr(C)] #[derive(Copy, Clone, Default)] pub struct Container { - pub view: Container_anon_15_3, + pub view: Container_anon_0, } pub unsafe fn fill_0(mut out: *mut ::libc::c_void, mut cap: u64) { let mut src: [u8; 16] = [ @@ -86,8 +86,8 @@ unsafe fn main_0() -> i32 { }; (unsafe { let _out: *mut ::libc::c_void = - ((&mut c.view as *mut Container_anon_15_3) as *mut ::libc::c_void); - let _cap: u64 = ::std::mem::size_of::() as u64; + ((&mut c.view as *mut Container_anon_0) as *mut ::libc::c_void); + let _cap: u64 = ::std::mem::size_of::() as u64; fill_0(_out, _cap) }); assert!(((c.view.h.code as i32) == (2))); diff --git a/tests/unit/out/unsafe/union_cross_arm_cast.rs b/tests/unit/out/unsafe/union_cross_arm_cast.rs index 6d63269b..3a40ae36 100644 --- a/tests/unit/out/unsafe/union_cross_arm_cast.rs +++ b/tests/unit/out/unsafe/union_cross_arm_cast.rs @@ -42,12 +42,12 @@ impl Default for shape_b { } #[repr(C)] #[derive(Copy, Clone)] -pub union Container_anon_22_3 { +pub union Container_anon_0 { pub a: shape_a, pub b: shape_b, pub raw_: [u8; 64], } -impl Default for Container_anon_22_3 { +impl Default for Container_anon_0 { fn default() -> Self { unsafe { std::mem::zeroed() } } @@ -56,7 +56,7 @@ impl Default for Container_anon_22_3 { #[derive(Copy, Clone, Default)] pub struct Container { pub len: u32, - pub u: Container_anon_22_3, + pub u: Container_anon_0, } pub fn main() { unsafe { diff --git a/tests/unit/out/unsafe/union_field_alignment.rs b/tests/unit/out/unsafe/union_field_alignment.rs index 60518256..623efb1f 100644 --- a/tests/unit/out/unsafe/union_field_alignment.rs +++ b/tests/unit/out/unsafe/union_field_alignment.rs @@ -8,11 +8,11 @@ use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] #[derive(Copy, Clone)] -pub union node_anon_10_3 { +pub union node_anon_0 { pub bytes: [u8; 1], pub aligner: *mut ::libc::c_void, } -impl Default for node_anon_10_3 { +impl Default for node_anon_0 { fn default() -> Self { unsafe { std::mem::zeroed() } } @@ -21,7 +21,7 @@ impl Default for node_anon_10_3 { #[derive(Copy, Clone, Default)] pub struct node { pub next: *mut node, - pub x: node_anon_10_3, + pub x: node_anon_0, } pub fn main() { unsafe { diff --git a/tests/unit/out/unsafe/union_flex_array_member.rs b/tests/unit/out/unsafe/union_flex_array_member.rs index 41fe937a..a563b679 100644 --- a/tests/unit/out/unsafe/union_flex_array_member.rs +++ b/tests/unit/out/unsafe/union_flex_array_member.rs @@ -8,11 +8,11 @@ use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] #[derive(Copy, Clone)] -pub union node_anon_10_3 { +pub union node_anon_0 { pub bytes: [u8; 1], pub aligner: *mut ::libc::c_void, } -impl Default for node_anon_10_3 { +impl Default for node_anon_0 { fn default() -> Self { unsafe { std::mem::zeroed() } } @@ -21,7 +21,7 @@ impl Default for node_anon_10_3 { #[derive(Copy, Clone, Default)] pub struct node { pub len: u64, - pub x: node_anon_10_3, + pub x: node_anon_0, } pub fn main() { unsafe { diff --git a/tests/unit/out/unsafe/union_memset_memcpy.rs b/tests/unit/out/unsafe/union_memset_memcpy.rs index d23b83c9..1a54f254 100644 --- a/tests/unit/out/unsafe/union_memset_memcpy.rs +++ b/tests/unit/out/unsafe/union_memset_memcpy.rs @@ -40,12 +40,12 @@ impl Default for shape_b { } #[repr(C)] #[derive(Copy, Clone)] -pub union Container_anon_20_3 { +pub union Container_anon_0 { pub a: shape_a, pub b: shape_b, pub raw_: [u8; 256], } -impl Default for Container_anon_20_3 { +impl Default for Container_anon_0 { fn default() -> Self { unsafe { std::mem::zeroed() } } @@ -53,7 +53,7 @@ impl Default for Container_anon_20_3 { #[repr(C)] #[derive(Copy, Clone, Default)] pub struct Container { - pub view: Container_anon_20_3, + pub view: Container_anon_0, } pub fn main() { unsafe { diff --git a/tests/unit/out/unsafe/union_nested.rs b/tests/unit/out/unsafe/union_nested.rs index 25285226..d50dac02 100644 --- a/tests/unit/out/unsafe/union_nested.rs +++ b/tests/unit/out/unsafe/union_nested.rs @@ -22,11 +22,11 @@ impl Default for record { } #[repr(C)] #[derive(Copy, Clone)] -pub union inner_anon_12_3 { +pub union inner_anon_0 { pub h: record, pub raw_: [u8; 128], } -impl Default for inner_anon_12_3 { +impl Default for inner_anon_0 { fn default() -> Self { unsafe { std::mem::zeroed() } } @@ -34,15 +34,15 @@ impl Default for inner_anon_12_3 { #[repr(C)] #[derive(Copy, Clone, Default)] pub struct inner { - pub view: inner_anon_12_3, + pub view: inner_anon_0, } #[repr(C)] #[derive(Copy, Clone)] -pub union Outer_anon_23_3 { +pub union Outer_anon_0 { pub h: record, pub nested: inner, } -impl Default for Outer_anon_23_3 { +impl Default for Outer_anon_0 { fn default() -> Self { unsafe { std::mem::zeroed() } } @@ -54,7 +54,7 @@ pub struct Outer { pub level: i32, pub variant: i32, pub len: u32, - pub body: Outer_anon_23_3, + pub body: Outer_anon_0, } pub fn main() { unsafe { diff --git a/tests/unit/out/unsafe/union_pointer_pun_address.rs b/tests/unit/out/unsafe/union_pointer_pun_address.rs index f86e6b7b..3115e621 100644 --- a/tests/unit/out/unsafe/union_pointer_pun_address.rs +++ b/tests/unit/out/unsafe/union_pointer_pun_address.rs @@ -26,16 +26,16 @@ unsafe fn main_0() -> i32 { let mut a: node_a = node_a { n: 123 }; #[repr(C)] #[derive(Copy, Clone)] - pub union anon_16_3 { + pub union anon_0 { pub to_a: *mut node_a, pub to_b: *mut node_b, } - impl Default for anon_16_3 { + impl Default for anon_0 { fn default() -> Self { unsafe { std::mem::zeroed() } } }; - let mut ptr: anon_16_3 = ::default(); + let mut ptr: anon_0 = ::default(); ptr.to_a = (&mut a as *mut node_a); let mut out: *mut node_b = ptr.to_b; assert!(((out as *mut ::libc::c_void) == ((&mut a as *mut node_a) as *mut ::libc::c_void))); diff --git a/tests/unit/out/unsafe/union_pointer_pun_writethrough.rs b/tests/unit/out/unsafe/union_pointer_pun_writethrough.rs index aa9b9a25..908bde23 100644 --- a/tests/unit/out/unsafe/union_pointer_pun_writethrough.rs +++ b/tests/unit/out/unsafe/union_pointer_pun_writethrough.rs @@ -15,16 +15,16 @@ unsafe fn main_0() -> i32 { let mut x: i64 = (-1_i32 as i64); #[repr(C)] #[derive(Copy, Clone)] - pub union anon_7_3 { + pub union anon_0 { pub as_unsigned: *mut u64, pub as_signed: *mut i64, } - impl Default for anon_7_3 { + impl Default for anon_0 { fn default() -> Self { unsafe { std::mem::zeroed() } } }; - let mut pp: anon_7_3 = ::default(); + let mut pp: anon_0 = ::default(); pp.as_signed = (&mut x as *mut i64); (*pp.as_unsigned) = 42_u64; assert!(((x) == (42_i64))); diff --git a/tests/unit/out/unsafe/union_struct_dual_use.rs b/tests/unit/out/unsafe/union_struct_dual_use.rs index 6db039ba..c5761448 100644 --- a/tests/unit/out/unsafe/union_struct_dual_use.rs +++ b/tests/unit/out/unsafe/union_struct_dual_use.rs @@ -17,11 +17,11 @@ pub unsafe fn sum_inner_0(mut i: *mut Inner) -> i32 { } #[repr(C)] #[derive(Copy, Clone)] -pub union Outer_anon_13_3 { +pub union Outer_anon_0 { pub inner: Inner, pub raw_: [u8; 16], } -impl Default for Outer_anon_13_3 { +impl Default for Outer_anon_0 { fn default() -> Self { unsafe { std::mem::zeroed() } } @@ -29,7 +29,7 @@ impl Default for Outer_anon_13_3 { #[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer { - pub u: Outer_anon_13_3, + pub u: Outer_anon_0, } pub fn main() { unsafe { diff --git a/tests/unit/out/unsafe/union_tagged_many_arms.rs b/tests/unit/out/unsafe/union_tagged_many_arms.rs index 79924c08..1f9f7972 100644 --- a/tests/unit/out/unsafe/union_tagged_many_arms.rs +++ b/tests/unit/out/unsafe/union_tagged_many_arms.rs @@ -17,14 +17,14 @@ enum Tag { } #[repr(C)] #[derive(Copy, Clone)] -pub union Slot_anon_15_3 { +pub union Slot_anon_0 { pub text: *const u8, pub handle: *mut ::libc::c_void, pub signed_n: i64, pub unsigned_n: u64, pub f: f64, } -impl Default for Slot_anon_15_3 { +impl Default for Slot_anon_0 { fn default() -> Self { unsafe { std::mem::zeroed() } } @@ -33,7 +33,7 @@ impl Default for Slot_anon_15_3 { #[derive(Copy, Clone, Default)] pub struct Slot { pub tag: Tag, - pub payload: Slot_anon_15_3, + pub payload: Slot_anon_0, } pub fn main() { unsafe { diff --git a/tests/unit/out/unsafe/union_tagged_simple.rs b/tests/unit/out/unsafe/union_tagged_simple.rs index 96c1f9bc..ea3282b6 100644 --- a/tests/unit/out/unsafe/union_tagged_simple.rs +++ b/tests/unit/out/unsafe/union_tagged_simple.rs @@ -14,11 +14,11 @@ enum Kind { } #[repr(C)] #[derive(Copy, Clone)] -pub union Event_anon_12_3 { +pub union Event_anon_0 { pub obj: *mut ::libc::c_void, pub code: i32, } -impl Default for Event_anon_12_3 { +impl Default for Event_anon_0 { fn default() -> Self { unsafe { std::mem::zeroed() } } @@ -28,7 +28,7 @@ impl Default for Event_anon_12_3 { pub struct Event { pub kind: Kind, pub handle: *mut ::libc::c_void, - pub payload: Event_anon_12_3, + pub payload: Event_anon_0, } pub fn main() { unsafe { diff --git a/tests/unit/out/unsafe/union_tagged_struct_arms.rs b/tests/unit/out/unsafe/union_tagged_struct_arms.rs index 898f45a7..57a28bb7 100644 --- a/tests/unit/out/unsafe/union_tagged_struct_arms.rs +++ b/tests/unit/out/unsafe/union_tagged_struct_arms.rs @@ -15,14 +15,14 @@ enum Choice { } #[repr(C)] #[derive(Copy, Clone, Default)] -pub struct Branch_anon_14_3_anon_15_5 { +pub struct Branch_anon_0_anon_0 { pub items: *mut *mut u8, pub count: i64, pub cursor: i64, } #[repr(C)] #[derive(Copy, Clone, Default)] -pub struct Branch_anon_14_3_anon_20_5 { +pub struct Branch_anon_0_anon_1 { pub lo: i32, pub hi: i32, pub curr: i32, @@ -30,7 +30,7 @@ pub struct Branch_anon_14_3_anon_20_5 { } #[repr(C)] #[derive(Copy, Clone, Default)] -pub struct Branch_anon_14_3_anon_26_5 { +pub struct Branch_anon_0_anon_2 { pub lo: i64, pub hi: i64, pub curr: i64, @@ -39,12 +39,12 @@ pub struct Branch_anon_14_3_anon_26_5 { } #[repr(C)] #[derive(Copy, Clone)] -pub union Branch_anon_14_3 { - pub list: Branch_anon_14_3_anon_15_5, - pub letters: Branch_anon_14_3_anon_20_5, - pub integers: Branch_anon_14_3_anon_26_5, +pub union Branch_anon_0 { + pub list: Branch_anon_0_anon_0, + pub letters: Branch_anon_0_anon_1, + pub integers: Branch_anon_0_anon_2, } -impl Default for Branch_anon_14_3 { +impl Default for Branch_anon_0 { fn default() -> Self { unsafe { std::mem::zeroed() } } @@ -54,7 +54,7 @@ impl Default for Branch_anon_14_3 { pub struct Branch { pub choice: Choice, pub index: i32, - pub v: Branch_anon_14_3, + pub v: Branch_anon_0, } pub fn main() { unsafe { diff --git a/tests/unit/out/unsafe/union_void_ptr_sized_deref.rs b/tests/unit/out/unsafe/union_void_ptr_sized_deref.rs index 323fd253..785dec47 100644 --- a/tests/unit/out/unsafe/union_void_ptr_sized_deref.rs +++ b/tests/unit/out/unsafe/union_void_ptr_sized_deref.rs @@ -15,13 +15,13 @@ enum Width { } #[repr(C)] #[derive(Copy, Clone)] -pub union Sink_anon_14_3 { +pub union Sink_anon_0 { pub text: *const u8, pub handle: *mut ::libc::c_void, pub signed_n: i64, pub f: f64, } -impl Default for Sink_anon_14_3 { +impl Default for Sink_anon_0 { fn default() -> Self { unsafe { std::mem::zeroed() } } @@ -30,7 +30,7 @@ impl Default for Sink_anon_14_3 { #[derive(Copy, Clone, Default)] pub struct Sink { pub width: Width, - pub out: Sink_anon_14_3, + pub out: Sink_anon_0, } pub unsafe fn write_count_0(mut s: *mut Sink, mut count: i64) { 'switch: { From 6e1b596fdc7f01b52279b7f94f275d9ba274d40f Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 28 Apr 2026 09:38:30 +0100 Subject: [PATCH 12/19] Delete duplicate function definition --- cpp2rust/converter/mapper.cpp | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/cpp2rust/converter/mapper.cpp b/cpp2rust/converter/mapper.cpp index 371d7d83..425cf805 100644 --- a/cpp2rust/converter/mapper.cpp +++ b/cpp2rust/converter/mapper.cpp @@ -735,17 +735,6 @@ std::string ToString(clang::QualType qual_type) { return normalizeTranslationRule(type); } -static std::string synthesizeAnonRecordName(const clang::RecordDecl *record) { - std::string parent_name; - if (auto *parent = - clang::dyn_cast(record->getDeclContext())) { - parent_name = parent->getIdentifier() - ? parent->getIdentifier()->getName().str() - : synthesizeAnonRecordName(parent); - } - return std::format("{}_anon_{}", parent_name, GetAnonIndex(record)); -} - std::string ToString(const clang::NamedDecl *decl) { if (auto *record = clang::dyn_cast(decl); record && !record->getIdentifier()) { From 6db97bed3c4ecf1d639624cdb95ec79aeb9e1412 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 28 Apr 2026 10:32:38 +0100 Subject: [PATCH 13/19] Update test expectations --- tests/unit/out/unsafe/anonymous-struct.rs | 9 +++++++++ tests/unit/out/unsafe/anonymous-struct_c.rs | 9 +++++++++ tests/unit/union_addrof_external.c | 1 - tests/unit/union_cross_arm_cast.c | 2 +- tests/unit/union_field_alignment.c | 2 +- tests/unit/union_memset_memcpy.c | 2 +- tests/unit/union_nested.c | 2 +- tests/unit/union_pointer_pun_address.c | 1 - tests/unit/union_pointer_pun_writethrough.c | 3 +-- tests/unit/union_struct_dual_use.c | 2 +- tests/unit/union_tagged_many_arms.c | 1 + tests/unit/union_tagged_simple.c | 1 + tests/unit/union_tagged_struct_arms.c | 2 +- tests/unit/union_void_ptr_sized_deref.c | 1 + 14 files changed, 28 insertions(+), 10 deletions(-) diff --git a/tests/unit/out/unsafe/anonymous-struct.rs b/tests/unit/out/unsafe/anonymous-struct.rs index 6b95a9f3..8940f0ac 100644 --- a/tests/unit/out/unsafe/anonymous-struct.rs +++ b/tests/unit/out/unsafe/anonymous-struct.rs @@ -6,40 +6,48 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer_Named { pub a: i32, pub b: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer_anon_0 { pub c: i32, pub d: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer_anon_1 { pub g: i32, pub h: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer_anon_2 { pub e: i32, pub f: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer_anon_3_anon_0 { pub j: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer_anon_3_anon_1 { pub k: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer_anon_3 { pub i: i32, pub inner_named: Outer_anon_3_anon_0, pub anon_1: Outer_anon_3_anon_1, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer { pub named: Outer_Named, @@ -87,6 +95,7 @@ unsafe fn main_0() -> i32 { assert!(((o.anon_3.i) == (9))); assert!(((o.anon_3.inner_named.j) == (10))); assert!(((o.anon_3.anon_1.k) == (11))); + #[repr(C)] #[derive(Copy, Clone, Default)] pub struct anon_0 { pub x: i32, diff --git a/tests/unit/out/unsafe/anonymous-struct_c.rs b/tests/unit/out/unsafe/anonymous-struct_c.rs index 578d1f81..1da705e1 100644 --- a/tests/unit/out/unsafe/anonymous-struct_c.rs +++ b/tests/unit/out/unsafe/anonymous-struct_c.rs @@ -6,40 +6,48 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Named { pub a: i32, pub b: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer_anon_0 { pub c: i32, pub d: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer_anon_1 { pub g: i32, pub h: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer_anon_2 { pub e: i32, pub f: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer_anon_3_anon_0 { pub j: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer_anon_3_anon_1 { pub k: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer_anon_3 { pub i: i32, pub inner_named: Outer_anon_3_anon_0, pub anon_1: Outer_anon_3_anon_1, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer { pub named: Named, @@ -83,6 +91,7 @@ unsafe fn main_0() -> i32 { assert!(((o.anon_3.i) == (9))); assert!(((o.anon_3.inner_named.j) == (10))); assert!(((o.anon_3.anon_1.k) == (11))); + #[repr(C)] #[derive(Copy, Clone, Default)] pub struct anon_0 { pub x: i32, diff --git a/tests/unit/union_addrof_external.c b/tests/unit/union_addrof_external.c index 87a58268..9669e853 100644 --- a/tests/unit/union_addrof_external.c +++ b/tests/unit/union_addrof_external.c @@ -1,5 +1,4 @@ // translation-fail: refcount -// no-compile: unsafe #include #include #include diff --git a/tests/unit/union_cross_arm_cast.c b/tests/unit/union_cross_arm_cast.c index d9af701d..598de31e 100644 --- a/tests/unit/union_cross_arm_cast.c +++ b/tests/unit/union_cross_arm_cast.c @@ -1,4 +1,4 @@ -// no-compile +// no-compile: refcount #include #include #include diff --git a/tests/unit/union_field_alignment.c b/tests/unit/union_field_alignment.c index 4051ff9d..63c957d1 100644 --- a/tests/unit/union_field_alignment.c +++ b/tests/unit/union_field_alignment.c @@ -1,4 +1,4 @@ -// no-compile +// no-compile: refcount #include #include #include diff --git a/tests/unit/union_memset_memcpy.c b/tests/unit/union_memset_memcpy.c index 6311739b..b4a270b6 100644 --- a/tests/unit/union_memset_memcpy.c +++ b/tests/unit/union_memset_memcpy.c @@ -1,4 +1,4 @@ -// no-compile +// no-compile: refcount #include #include #include diff --git a/tests/unit/union_nested.c b/tests/unit/union_nested.c index d046dcc2..b056725e 100644 --- a/tests/unit/union_nested.c +++ b/tests/unit/union_nested.c @@ -1,4 +1,4 @@ -// no-compile +// no-compile: refcount #include #include #include diff --git a/tests/unit/union_pointer_pun_address.c b/tests/unit/union_pointer_pun_address.c index 50dfd767..421bb1f0 100644 --- a/tests/unit/union_pointer_pun_address.c +++ b/tests/unit/union_pointer_pun_address.c @@ -1,5 +1,4 @@ // no-compile: refcount -// panic: unsafe #include struct node_a { diff --git a/tests/unit/union_pointer_pun_writethrough.c b/tests/unit/union_pointer_pun_writethrough.c index 03fbc47f..d29cbc50 100644 --- a/tests/unit/union_pointer_pun_writethrough.c +++ b/tests/unit/union_pointer_pun_writethrough.c @@ -1,5 +1,4 @@ -// panic: refcount -// XFAIL: unsafe +// no-compile: refcount #include int main(void) { diff --git a/tests/unit/union_struct_dual_use.c b/tests/unit/union_struct_dual_use.c index a160e07b..2ddb14f4 100644 --- a/tests/unit/union_struct_dual_use.c +++ b/tests/unit/union_struct_dual_use.c @@ -1,4 +1,4 @@ -// no-compile +// no-compile: refcount #include #include diff --git a/tests/unit/union_tagged_many_arms.c b/tests/unit/union_tagged_many_arms.c index e3677004..c492c67d 100644 --- a/tests/unit/union_tagged_many_arms.c +++ b/tests/unit/union_tagged_many_arms.c @@ -1,3 +1,4 @@ +// no-compile: refcount #include #include diff --git a/tests/unit/union_tagged_simple.c b/tests/unit/union_tagged_simple.c index 6efa44de..590f34bb 100644 --- a/tests/unit/union_tagged_simple.c +++ b/tests/unit/union_tagged_simple.c @@ -1,3 +1,4 @@ +// no-compile: refcount #include typedef enum { diff --git a/tests/unit/union_tagged_struct_arms.c b/tests/unit/union_tagged_struct_arms.c index 52a4de99..3cd71f8a 100644 --- a/tests/unit/union_tagged_struct_arms.c +++ b/tests/unit/union_tagged_struct_arms.c @@ -1,4 +1,4 @@ -// no-compile: unsafe +// no-compile #include #include diff --git a/tests/unit/union_void_ptr_sized_deref.c b/tests/unit/union_void_ptr_sized_deref.c index 586a608e..3d957cfe 100644 --- a/tests/unit/union_void_ptr_sized_deref.c +++ b/tests/unit/union_void_ptr_sized_deref.c @@ -1,3 +1,4 @@ +// no-compile: refcount #include #include #include From d1db80b6e585caa2c2d91cacf4a71b7bccf37793 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 28 Apr 2026 11:23:25 +0100 Subject: [PATCH 14/19] Delete no-compile files --- tests/unit/out/refcount/union_basic.rs | 30 ---- .../unit/out/refcount/union_cross_arm_cast.rs | 108 ------------- .../out/refcount/union_field_alignment.rs | 35 ---- .../out/refcount/union_flex_array_member.rs | 78 --------- .../unit/out/refcount/union_memset_memcpy.rs | 134 --------------- tests/unit/out/refcount/union_nested.rs | 98 ----------- .../out/refcount/union_pointer_pun_address.rs | 46 ------ .../union_pointer_pun_writethrough.rs | 30 ---- .../out/refcount/union_struct_dual_use.rs | 73 --------- .../out/refcount/union_tagged_many_arms.rs | 74 --------- .../unit/out/refcount/union_tagged_simple.rs | 54 ------- .../out/refcount/union_tagged_struct_arms.rs | 153 ------------------ .../refcount/union_void_ptr_sized_deref.rs | 108 ------------- .../out/unsafe/union_flex_array_member.rs | 59 ------- .../out/unsafe/union_tagged_struct_arms.rs | 96 ----------- 15 files changed, 1176 deletions(-) delete mode 100644 tests/unit/out/refcount/union_basic.rs delete mode 100644 tests/unit/out/refcount/union_cross_arm_cast.rs delete mode 100644 tests/unit/out/refcount/union_field_alignment.rs delete mode 100644 tests/unit/out/refcount/union_flex_array_member.rs delete mode 100644 tests/unit/out/refcount/union_memset_memcpy.rs delete mode 100644 tests/unit/out/refcount/union_nested.rs delete mode 100644 tests/unit/out/refcount/union_pointer_pun_address.rs delete mode 100644 tests/unit/out/refcount/union_pointer_pun_writethrough.rs delete mode 100644 tests/unit/out/refcount/union_struct_dual_use.rs delete mode 100644 tests/unit/out/refcount/union_tagged_many_arms.rs delete mode 100644 tests/unit/out/refcount/union_tagged_simple.rs delete mode 100644 tests/unit/out/refcount/union_tagged_struct_arms.rs delete mode 100644 tests/unit/out/refcount/union_void_ptr_sized_deref.rs delete mode 100644 tests/unit/out/unsafe/union_flex_array_member.rs delete mode 100644 tests/unit/out/unsafe/union_tagged_struct_arms.rs diff --git a/tests/unit/out/refcount/union_basic.rs b/tests/unit/out/refcount/union_basic.rs deleted file mode 100644 index 8a005ac1..00000000 --- a/tests/unit/out/refcount/union_basic.rs +++ /dev/null @@ -1,30 +0,0 @@ -extern crate libcc2rs; -use libcc2rs::*; -use std::cell::RefCell; -use std::collections::BTreeMap; -use std::io::prelude::*; -use std::io::{Read, Seek, Write}; -use std::os::fd::AsFd; -use std::rc::{Rc, Weak}; -#[derive(Default)] -pub union basic { - pub i: Value, - pub f: Value, -} -impl Default for basic { - fn default() -> Self { - unsafe { std::mem::zeroed() } - } -} -impl ByteRepr for basic {} -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - let u: Value = >::default(); - (*(*u.borrow()).i.borrow_mut()) = 42; - assert!(((*(*u.borrow()).i.borrow()) == 42)); - (*(*u.borrow()).f.borrow_mut()) = 3.140000105E+0; - assert!(((*(*u.borrow()).f.borrow()) == 3.140000105E+0)); - return 0; -} diff --git a/tests/unit/out/refcount/union_cross_arm_cast.rs b/tests/unit/out/refcount/union_cross_arm_cast.rs deleted file mode 100644 index e87e9009..00000000 --- a/tests/unit/out/refcount/union_cross_arm_cast.rs +++ /dev/null @@ -1,108 +0,0 @@ -extern crate libcc2rs; -use libcc2rs::*; -use std::cell::RefCell; -use std::collections::BTreeMap; -use std::io::prelude::*; -use std::io::{Read, Seek, Write}; -use std::os::fd::AsFd; -use std::rc::{Rc, Weak}; -#[derive()] -pub struct shape_a { - pub code: Value, - pub pad: Value>, -} -impl Default for shape_a { - fn default() -> Self { - shape_a { - code: >::default(), - pad: Rc::new(RefCell::new( - (0..14).map(|_| ::default()).collect::>(), - )), - } - } -} -impl ByteRepr for shape_a {} -#[derive()] -pub struct shape_b { - pub code: Value, - pub lo: Value, - pub mid: Value, - pub fill: Value>, - pub tail: Value, -} -impl Default for shape_b { - fn default() -> Self { - shape_b { - code: >::default(), - lo: >::default(), - mid: >::default(), - fill: Rc::new(RefCell::new( - (0..16).map(|_| ::default()).collect::>(), - )), - tail: >::default(), - } - } -} -impl ByteRepr for shape_b {} -#[derive()] -pub union Container_anon_0 { - pub a: Value, - pub b: Value, - pub raw_: Value>, -} -impl Default for Container_anon_0 { - fn default() -> Self { - unsafe { std::mem::zeroed() } - } -} -impl ByteRepr for Container_anon_0 {} -#[derive(Default)] -pub struct Container { - pub len: Value, - pub u: Value, -} -impl ByteRepr for Container {} -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - let c: Value = >::default(); - { - ((c.as_pointer()) as Ptr).to_any().memset( - (0) as u8, - ::std::mem::size_of::() as u64 as usize, - ); - ((c.as_pointer()) as Ptr).to_any().clone() - }; - (*(*(*(*c.borrow()).u.borrow()).a.borrow()).code.borrow_mut()) = 10_u16; - (*(*c.borrow()).len.borrow_mut()) = (::std::mem::size_of::() as u64 as u32); - (*(*((((*(*c.borrow()).u.borrow()).a.as_pointer()) - .to_strong() - .as_pointer() as AnyPtr) - .cast::() - .expect("ub:wrong type")) - .upgrade() - .deref()) - .tail - .borrow_mut()) = 3735928559_u32; - assert!(((*(*(*(*c.borrow()).u.borrow()).b.borrow()).tail.borrow()) == 3735928559_u32)); - assert!((((*(*(*(*c.borrow()).u.borrow()).b.borrow()).code.borrow()) as i32) == 10)); - (*(*(*(*c.borrow()).u.borrow()).b.borrow()).lo.borrow_mut()) = 8080_u16; - assert!( - ((((((*(*c.borrow()).u.borrow()).raw_.as_pointer()) - .to_strong() - .as_pointer() as Ptr::) - .offset((2) as isize) - .read()) as i32) - == 144) - ); - assert!( - ((((((*(*c.borrow()).u.borrow()).raw_.as_pointer()) - .to_strong() - .as_pointer() as Ptr::) - .offset((3) as isize) - .read()) as i32) - == 31) - ); - return 0; -} diff --git a/tests/unit/out/refcount/union_field_alignment.rs b/tests/unit/out/refcount/union_field_alignment.rs deleted file mode 100644 index fa803699..00000000 --- a/tests/unit/out/refcount/union_field_alignment.rs +++ /dev/null @@ -1,35 +0,0 @@ -extern crate libcc2rs; -use libcc2rs::*; -use std::cell::RefCell; -use std::collections::BTreeMap; -use std::io::prelude::*; -use std::io::{Read, Seek, Write}; -use std::os::fd::AsFd; -use std::rc::{Rc, Weak}; -#[derive()] -pub union node_anon_0 { - pub bytes: Value>, - pub aligner: Value, -} -impl Default for node_anon_0 { - fn default() -> Self { - unsafe { std::mem::zeroed() } - } -} -impl ByteRepr for node_anon_0 {} -#[derive(Default)] -pub struct node { - pub next: Value>, - pub x: Value, -} -impl ByteRepr for node {} -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - let n: Value = >::default(); - (*(*n.borrow()).next.borrow_mut()) = Default::default(); - (*(*(*n.borrow()).x.borrow()).bytes.borrow_mut())[(0) as usize] = 171_u8; - assert!((((*(*(*n.borrow()).x.borrow()).bytes.borrow())[(0) as usize] as i32) == 171)); - return 0; -} diff --git a/tests/unit/out/refcount/union_flex_array_member.rs b/tests/unit/out/refcount/union_flex_array_member.rs deleted file mode 100644 index f3cf1a57..00000000 --- a/tests/unit/out/refcount/union_flex_array_member.rs +++ /dev/null @@ -1,78 +0,0 @@ -extern crate libcc2rs; -use libcc2rs::*; -use std::cell::RefCell; -use std::collections::BTreeMap; -use std::io::prelude::*; -use std::io::{Read, Seek, Write}; -use std::os::fd::AsFd; -use std::rc::{Rc, Weak}; -#[derive()] -pub union node_anon_0 { - pub bytes: Value>, - pub aligner: Value, -} -impl Default for node_anon_0 { - fn default() -> Self { - unsafe { std::mem::zeroed() } - } -} -impl ByteRepr for node_anon_0 {} -#[derive(Default)] -pub struct node { - pub len: Value, - pub x: Value, -} -impl ByteRepr for node {} -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - let tail_size: Value = Rc::new(RefCell::new(32_u64)); - let n: Value> = Rc::new(RefCell::new( - ({ - let ___size: u64 = - (::std::mem::size_of::() as u64 as u64).wrapping_add((*tail_size.borrow())); - malloc_0(___size) - }) - .cast::() - .expect("ub:wrong type"), - )); - (*(*(*n.borrow()).upgrade().deref()).len.borrow_mut()) = (*tail_size.borrow()); - let i: Value = Rc::new(RefCell::new(0_u64)); - 'loop_: while ((*i.borrow()) < (*tail_size.borrow())) { - let __rhs = ((((*i.borrow()) & 255_u64) as u64) as u8); - (*(*(*(*n.borrow()).upgrade().deref()).x.borrow()) - .bytes - .borrow_mut())[(*i.borrow()) as usize] = __rhs; - (*i.borrow_mut()).postfix_inc(); - } - let i: Value = Rc::new(RefCell::new(0_u64)); - 'loop_: while ((*i.borrow()) < (*tail_size.borrow())) { - assert!({ - let _lhs = ((*(*(*(*n.borrow()).upgrade().deref()).x.borrow()) - .bytes - .borrow())[(*i.borrow()) as usize] as i32); - _lhs == (((((*i.borrow()) & 255_u64) as u64) as u8) as i32) - }); - (*i.borrow_mut()).postfix_inc(); - } - let p: Value> = Rc::new(RefCell::new( - (((*(*(*n.borrow()).upgrade().deref()).x.borrow()) - .bytes - .as_pointer() as Ptr) - .offset(10 as isize)), - )); - assert!(((((*p.borrow()).read()) as i32) == 10)); - (*p.borrow()).write(170_u8); - assert!( - (((*(*(*(*n.borrow()).upgrade().deref()).x.borrow()) - .bytes - .borrow())[(10) as usize] as i32) - == 170) - ); - ({ - let ___ptr: AnyPtr = ((*n.borrow()).clone() as Ptr).to_any(); - free_1(___ptr) - }); - return 0; -} diff --git a/tests/unit/out/refcount/union_memset_memcpy.rs b/tests/unit/out/refcount/union_memset_memcpy.rs deleted file mode 100644 index fa1190d1..00000000 --- a/tests/unit/out/refcount/union_memset_memcpy.rs +++ /dev/null @@ -1,134 +0,0 @@ -extern crate libcc2rs; -use libcc2rs::*; -use std::cell::RefCell; -use std::collections::BTreeMap; -use std::io::prelude::*; -use std::io::{Read, Seek, Write}; -use std::os::fd::AsFd; -use std::rc::{Rc, Weak}; -#[derive()] -pub struct shape_a { - pub code: Value, - pub pad: Value>, -} -impl Default for shape_a { - fn default() -> Self { - shape_a { - code: >::default(), - pad: Rc::new(RefCell::new( - (0..14).map(|_| ::default()).collect::>(), - )), - } - } -} -impl ByteRepr for shape_a {} -#[derive()] -pub struct shape_b { - pub code: Value, - pub lo: Value, - pub hi: Value, - pub fill: Value>, -} -impl Default for shape_b { - fn default() -> Self { - shape_b { - code: >::default(), - lo: >::default(), - hi: >::default(), - fill: Rc::new(RefCell::new( - (0..8).map(|_| ::default()).collect::>(), - )), - } - } -} -impl ByteRepr for shape_b {} -#[derive()] -pub union Container_anon_0 { - pub a: Value, - pub b: Value, - pub raw_: Value>, -} -impl Default for Container_anon_0 { - fn default() -> Self { - unsafe { std::mem::zeroed() } - } -} -impl ByteRepr for Container_anon_0 {} -#[derive(Default)] -pub struct Container { - pub view: Value, -} -impl ByteRepr for Container {} -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - let c: Value = >::default(); - { - ((c.as_pointer()) as Ptr).to_any().memset( - (0) as u8, - ::std::mem::size_of::() as u64 as usize, - ); - ((c.as_pointer()) as Ptr).to_any().clone() - }; - assert!((((*(*(*(*c.borrow()).view.borrow()).a.borrow()).code.borrow()) as i32) == 0)); - assert!((((*(*(*(*c.borrow()).view.borrow()).b.borrow()).lo.borrow()) as i32) == 0)); - assert!((((*(*(*c.borrow()).view.borrow()).raw_.borrow())[(0) as usize] as i32) == 0)); - assert!((((*(*(*c.borrow()).view.borrow()).raw_.borrow())[(255) as usize] as i32) == 0)); - let src: Value> = Rc::new(RefCell::new(Box::new([ - 0_u8, - ::default(), - ::default(), - ::default(), - ::default(), - ::default(), - ::default(), - ::default(), - ::default(), - ::default(), - ::default(), - ::default(), - ::default(), - ::default(), - ::default(), - ::default(), - ]))); - (*src.borrow_mut())[(0) as usize] = 2_u8; - (*src.borrow_mut())[(2) as usize] = 80_u8; - (*src.borrow_mut())[(3) as usize] = 0_u8; - (*src.borrow_mut())[(4) as usize] = 127_u8; - (*src.borrow_mut())[(5) as usize] = 0_u8; - (*src.borrow_mut())[(6) as usize] = 0_u8; - (*src.borrow_mut())[(7) as usize] = 1_u8; - let len: Value = Rc::new(RefCell::new(16_u64)); - assert!(((*len.borrow()) <= ::std::mem::size_of::<[u8; 256]>() as u64)); - { - (((*(*c.borrow()).view.borrow()).raw_.as_pointer()) as Ptr) - .to_any() - .memcpy( - &((src.as_pointer() as Ptr) as Ptr).to_any(), - (*len.borrow()) as usize, - ); - (((*(*c.borrow()).view.borrow()).raw_.as_pointer()) as Ptr) - .to_any() - .clone() - }; - assert!((((*(*(*(*c.borrow()).view.borrow()).b.borrow()).code.borrow()) as i32) == 2)); - assert!( - ((((((*(*(*c.borrow()).view.borrow()).b.borrow()).lo.as_pointer()) - .to_strong() - .as_pointer() as Ptr::) - .offset((0) as isize) - .read()) as i32) - == 80) - ); - { - ((c.as_pointer()) as Ptr).to_any().memset( - (0) as u8, - ::std::mem::size_of::() as u64 as usize, - ); - ((c.as_pointer()) as Ptr).to_any().clone() - }; - assert!((((*(*(*(*c.borrow()).view.borrow()).b.borrow()).code.borrow()) as i32) == 0)); - return 0; -} diff --git a/tests/unit/out/refcount/union_nested.rs b/tests/unit/out/refcount/union_nested.rs deleted file mode 100644 index 67b635f1..00000000 --- a/tests/unit/out/refcount/union_nested.rs +++ /dev/null @@ -1,98 +0,0 @@ -extern crate libcc2rs; -use libcc2rs::*; -use std::cell::RefCell; -use std::collections::BTreeMap; -use std::io::prelude::*; -use std::io::{Read, Seek, Write}; -use std::os::fd::AsFd; -use std::rc::{Rc, Weak}; -#[derive()] -pub struct record { - pub code: Value, - pub pad: Value>, -} -impl Default for record { - fn default() -> Self { - record { - code: >::default(), - pad: Rc::new(RefCell::new( - (0..14).map(|_| ::default()).collect::>(), - )), - } - } -} -impl ByteRepr for record {} -#[derive()] -pub union inner_anon_0 { - pub h: Value, - pub raw_: Value>, -} -impl Default for inner_anon_0 { - fn default() -> Self { - unsafe { std::mem::zeroed() } - } -} -impl ByteRepr for inner_anon_0 {} -#[derive(Default)] -pub struct inner { - pub view: Value, -} -impl ByteRepr for inner {} -#[derive(Default)] -pub union Outer_anon_0 { - pub h: Value, - pub nested: Value, -} -impl Default for Outer_anon_0 { - fn default() -> Self { - unsafe { std::mem::zeroed() } - } -} -impl ByteRepr for Outer_anon_0 {} -#[derive(Default)] -pub struct Outer { - pub kind: Value, - pub level: Value, - pub variant: Value, - pub len: Value, - pub body: Value, -} -impl ByteRepr for Outer {} -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - let ex: Value = >::default(); - { - ((ex.as_pointer()) as Ptr) - .to_any() - .memset((0) as u8, ::std::mem::size_of::() as u64 as usize); - ((ex.as_pointer()) as Ptr).to_any().clone() - }; - (*(*ex.borrow()).kind.borrow_mut()) = 2; - (*(*ex.borrow()).level.borrow_mut()) = 1; - (*(*ex.borrow()).variant.borrow_mut()) = 6; - (*(*ex.borrow()).len.borrow_mut()) = (::std::mem::size_of::() as u64 as u32); - (*(*(*(*ex.borrow()).body.borrow()).h.borrow()) - .code - .borrow_mut()) = 2_u16; - (*(*(*(*ex.borrow()).body.borrow()).h.borrow()) - .pad - .borrow_mut())[(0) as usize] = (('X' as i32) as u8); - assert!((((*(*(*(*ex.borrow()).body.borrow()).h.borrow()).code.borrow()) as i32) == 2)); - assert!( - (((*(*(*(*ex.borrow()).body.borrow()).h.borrow()).pad.borrow())[(0) as usize] as i32) - == ('X' as i32)) - ); - assert!( - (((*(*(*(*(*(*ex.borrow()).body.borrow()).nested.borrow()) - .view - .borrow()) - .h - .borrow()) - .code - .borrow()) as i32) - == 2) - ); - return 0; -} diff --git a/tests/unit/out/refcount/union_pointer_pun_address.rs b/tests/unit/out/refcount/union_pointer_pun_address.rs deleted file mode 100644 index 354ac36a..00000000 --- a/tests/unit/out/refcount/union_pointer_pun_address.rs +++ /dev/null @@ -1,46 +0,0 @@ -extern crate libcc2rs; -use libcc2rs::*; -use std::cell::RefCell; -use std::collections::BTreeMap; -use std::io::prelude::*; -use std::io::{Read, Seek, Write}; -use std::os::fd::AsFd; -use std::rc::{Rc, Weak}; -#[derive(Default)] -pub struct node_a { - pub n: Value, -} -impl ByteRepr for node_a {} -#[derive(Default)] -pub struct node_b { - pub data: Value, - pub next: Value>, -} -impl ByteRepr for node_b {} -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - let a: Value = Rc::new(RefCell::new(node_a { - n: Rc::new(RefCell::new(123)), - })); - #[derive(Default)] - pub union anon_0 { - pub to_a: Value>, - pub to_b: Value>, - } - impl Default for anon_0 { - fn default() -> Self { - unsafe { std::mem::zeroed() } - } - } - impl ByteRepr for anon_0 {}; - let ptr: Value = >::default(); - (*(*ptr.borrow()).to_a.borrow_mut()) = (a.as_pointer()); - let out: Value> = Rc::new(RefCell::new((*(*ptr.borrow()).to_b.borrow()).clone())); - assert!({ - let _lhs = ((*out.borrow()).to_strong().as_pointer() as AnyPtr).clone(); - _lhs == ((a.as_pointer()).to_strong().as_pointer() as AnyPtr) - }); - return 0; -} diff --git a/tests/unit/out/refcount/union_pointer_pun_writethrough.rs b/tests/unit/out/refcount/union_pointer_pun_writethrough.rs deleted file mode 100644 index ceaaea09..00000000 --- a/tests/unit/out/refcount/union_pointer_pun_writethrough.rs +++ /dev/null @@ -1,30 +0,0 @@ -extern crate libcc2rs; -use libcc2rs::*; -use std::cell::RefCell; -use std::collections::BTreeMap; -use std::io::prelude::*; -use std::io::{Read, Seek, Write}; -use std::os::fd::AsFd; -use std::rc::{Rc, Weak}; -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - let x: Value = Rc::new(RefCell::new((-1_i32 as i64))); - #[derive(Default)] - pub union anon_0 { - pub as_unsigned: Value>, - pub as_signed: Value>, - } - impl Default for anon_0 { - fn default() -> Self { - unsafe { std::mem::zeroed() } - } - } - impl ByteRepr for anon_0 {}; - let pp: Value = >::default(); - (*(*pp.borrow()).as_signed.borrow_mut()) = (x.as_pointer()); - (*(*pp.borrow()).as_unsigned.borrow()).write(42_u64); - assert!(((*x.borrow()) == 42_i64)); - return 0; -} diff --git a/tests/unit/out/refcount/union_struct_dual_use.rs b/tests/unit/out/refcount/union_struct_dual_use.rs deleted file mode 100644 index a6b4b913..00000000 --- a/tests/unit/out/refcount/union_struct_dual_use.rs +++ /dev/null @@ -1,73 +0,0 @@ -extern crate libcc2rs; -use libcc2rs::*; -use std::cell::RefCell; -use std::collections::BTreeMap; -use std::io::prelude::*; -use std::io::{Read, Seek, Write}; -use std::os::fd::AsFd; -use std::rc::{Rc, Weak}; -#[derive(Default)] -pub struct Inner { - pub a: Value, - pub b: Value, -} -impl ByteRepr for Inner {} -pub fn sum_inner_0(i: Ptr) -> i32 { - let i: Value> = Rc::new(RefCell::new(i)); - return { - let _lhs = (*(*(*i.borrow()).upgrade().deref()).a.borrow()); - _lhs + (*(*(*i.borrow()).upgrade().deref()).b.borrow()) - }; -} -#[derive()] -pub union Outer_anon_0 { - pub inner: Value, - pub raw_: Value>, -} -impl Default for Outer_anon_0 { - fn default() -> Self { - unsafe { std::mem::zeroed() } - } -} -impl ByteRepr for Outer_anon_0 {} -#[derive(Default)] -pub struct Outer { - pub u: Value, -} -impl ByteRepr for Outer {} -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - let standalone: Value = >::default(); - (*(*standalone.borrow()).a.borrow_mut()) = 3; - (*(*standalone.borrow()).b.borrow_mut()) = 4; - assert!( - (({ - let _i: Ptr = (standalone.as_pointer()); - sum_inner_0(_i) - }) == 7) - ); - let outer: Value = >::default(); - { - ((outer.as_pointer()) as Ptr) - .to_any() - .memset((0) as u8, ::std::mem::size_of::() as u64 as usize); - ((outer.as_pointer()) as Ptr).to_any().clone() - }; - (*(*(*(*outer.borrow()).u.borrow()).inner.borrow()) - .a - .borrow_mut()) = 3; - (*(*(*(*outer.borrow()).u.borrow()).inner.borrow()) - .b - .borrow_mut()) = 4; - assert!( - (({ - let _i: Ptr = ((*(*outer.borrow()).u.borrow()).inner.as_pointer()); - sum_inner_0(_i) - }) == 7) - ); - assert!(((((*(*(*outer.borrow()).u.borrow()).raw_.borrow())[(0) as usize] as u8) as i32) == 3)); - assert!(((((*(*(*outer.borrow()).u.borrow()).raw_.borrow())[(4) as usize] as u8) as i32) == 4)); - return 0; -} diff --git a/tests/unit/out/refcount/union_tagged_many_arms.rs b/tests/unit/out/refcount/union_tagged_many_arms.rs deleted file mode 100644 index 97137bfc..00000000 --- a/tests/unit/out/refcount/union_tagged_many_arms.rs +++ /dev/null @@ -1,74 +0,0 @@ -extern crate libcc2rs; -use libcc2rs::*; -use std::cell::RefCell; -use std::collections::BTreeMap; -use std::io::prelude::*; -use std::io::{Read, Seek, Write}; -use std::os::fd::AsFd; -use std::rc::{Rc, Weak}; -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum Tag { - #[default] - T_NUM_S = 0, - T_NUM_U = 1, - T_TEXT = 2, - T_FLOAT = 3, - T_REF = 4, -} -#[derive(Default)] -pub union Slot_anon_0 { - pub text: Value>, - pub handle: Value, - pub signed_n: Value, - pub unsigned_n: Value, - pub f: Value, -} -impl Default for Slot_anon_0 { - fn default() -> Self { - unsafe { std::mem::zeroed() } - } -} -impl ByteRepr for Slot_anon_0 {} -#[derive(Default)] -pub struct Slot { - pub tag: Value, - pub payload: Value, -} -impl ByteRepr for Slot {} -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - let a: Value = >::default(); - (*(*a.borrow()).tag.borrow_mut()) = (Tag::T_NUM_S as Tag); - (*(*(*a.borrow()).payload.borrow()).signed_n.borrow_mut()) = (-7_i32 as i64); - assert!(((*(*(*a.borrow()).payload.borrow()).signed_n.borrow()) == (-7_i32 as i64))); - let b: Value = >::default(); - (*(*b.borrow()).tag.borrow_mut()) = (Tag::T_NUM_U as Tag); - (*(*(*b.borrow()).payload.borrow()).unsigned_n.borrow_mut()) = 3735928559_u64; - assert!(((*(*(*b.borrow()).payload.borrow()).unsigned_n.borrow()) == 3735928559_u64)); - let c: Value = >::default(); - (*(*c.borrow()).tag.borrow_mut()) = (Tag::T_TEXT as Tag); - (*(*(*c.borrow()).payload.borrow()).text.borrow_mut()) = - (Ptr::from_string_literal("hello")).clone(); - assert!( - ((((*(*(*c.borrow()).payload.borrow()).text.borrow()) - .offset((0) as isize) - .read()) as i32) - == ('h' as i32)) - ); - let d: Value = >::default(); - (*(*d.borrow()).tag.borrow_mut()) = (Tag::T_FLOAT as Tag); - (*(*(*d.borrow()).payload.borrow()).f.borrow_mut()) = 1.5E+0; - assert!(((*(*(*d.borrow()).payload.borrow()).f.borrow()) == 1.5E+0)); - let x: Value = Rc::new(RefCell::new(0)); - let e: Value = >::default(); - (*(*e.borrow()).tag.borrow_mut()) = (Tag::T_REF as Tag); - (*(*(*e.borrow()).payload.borrow()).handle.borrow_mut()) = - ((x.as_pointer()) as Ptr).to_any(); - assert!({ - let _lhs = (*(*(*e.borrow()).payload.borrow()).handle.borrow()).clone(); - _lhs == ((x.as_pointer()) as Ptr).to_any() - }); - return 0; -} diff --git a/tests/unit/out/refcount/union_tagged_simple.rs b/tests/unit/out/refcount/union_tagged_simple.rs deleted file mode 100644 index 6d7806c5..00000000 --- a/tests/unit/out/refcount/union_tagged_simple.rs +++ /dev/null @@ -1,54 +0,0 @@ -extern crate libcc2rs; -use libcc2rs::*; -use std::cell::RefCell; -use std::collections::BTreeMap; -use std::io::prelude::*; -use std::io::{Read, Seek, Write}; -use std::os::fd::AsFd; -use std::rc::{Rc, Weak}; -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum Kind { - #[default] - KIND_NONE = 0, - KIND_DONE = 1, -} -#[derive(Default)] -pub union Event_anon_0 { - pub obj: Value, - pub code: Value, -} -impl Default for Event_anon_0 { - fn default() -> Self { - unsafe { std::mem::zeroed() } - } -} -impl ByteRepr for Event_anon_0 {} -#[derive(Default)] -pub struct Event { - pub kind: Value, - pub handle: Value, - pub payload: Value, -} -impl ByteRepr for Event {} -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - let dummy: Value = Rc::new(RefCell::new(0)); - let m1: Value = >::default(); - (*(*m1.borrow()).kind.borrow_mut()) = (Kind::KIND_DONE as Kind); - (*(*m1.borrow()).handle.borrow_mut()) = ((dummy.as_pointer()) as Ptr).to_any(); - (*(*(*m1.borrow()).payload.borrow()).code.borrow_mut()) = 42; - assert!((((*(*m1.borrow()).kind.borrow()) as u32) == (Kind::KIND_DONE as u32))); - assert!(((*(*(*m1.borrow()).payload.borrow()).code.borrow()) == 42)); - let m2: Value = >::default(); - (*(*m2.borrow()).kind.borrow_mut()) = (Kind::KIND_NONE as Kind); - (*(*m2.borrow()).handle.borrow_mut()) = ((dummy.as_pointer()) as Ptr).to_any(); - (*(*(*m2.borrow()).payload.borrow()).obj.borrow_mut()) = - ((dummy.as_pointer()) as Ptr).to_any(); - assert!({ - let _lhs = (*(*(*m2.borrow()).payload.borrow()).obj.borrow()).clone(); - _lhs == ((dummy.as_pointer()) as Ptr).to_any() - }); - return 0; -} diff --git a/tests/unit/out/refcount/union_tagged_struct_arms.rs b/tests/unit/out/refcount/union_tagged_struct_arms.rs deleted file mode 100644 index f38467e2..00000000 --- a/tests/unit/out/refcount/union_tagged_struct_arms.rs +++ /dev/null @@ -1,153 +0,0 @@ -extern crate libcc2rs; -use libcc2rs::*; -use std::cell::RefCell; -use std::collections::BTreeMap; -use std::io::prelude::*; -use std::io::{Read, Seek, Write}; -use std::os::fd::AsFd; -use std::rc::{Rc, Weak}; -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum Choice { - #[default] - C_LIST = 1, - C_LETTERS = 2, - C_INTEGERS = 3, -} -#[derive(Default)] -pub struct Branch_anon_0_anon_0 { - pub items: Value>>, - pub count: Value, - pub cursor: Value, -} -impl ByteRepr for Branch_anon_0_anon_0 {} -#[derive(Default)] -pub struct Branch_anon_0_anon_1 { - pub lo: Value, - pub hi: Value, - pub curr: Value, - pub step: Value, -} -impl ByteRepr for Branch_anon_0_anon_1 {} -#[derive(Default)] -pub struct Branch_anon_0_anon_2 { - pub lo: Value, - pub hi: Value, - pub curr: Value, - pub step: Value, - pub width: Value, -} -impl ByteRepr for Branch_anon_0_anon_2 {} -#[derive(Default)] -pub union Branch_anon_0 { - pub list: Value, - pub letters: Value, - pub integers: Value, -} -impl Default for Branch_anon_0 { - fn default() -> Self { - unsafe { std::mem::zeroed() } - } -} -impl ByteRepr for Branch_anon_0 {} -#[derive(Default)] -pub struct Branch { - pub choice: Value, - pub index: Value, - pub v: Value, -} -impl ByteRepr for Branch {} -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - thread_local!( - static items: Value]>> = Rc::new(RefCell::new(Box::new([ - Ptr::from_string_literal("a"), - Ptr::from_string_literal("b"), - Ptr::from_string_literal("c"), - ]))); - ); - let p_list: Value = >::default(); - (*(*p_list.borrow()).choice.borrow_mut()) = (Choice::C_LIST as Choice); - (*(*p_list.borrow()).index.borrow_mut()) = 0; - (*(*(*(*p_list.borrow()).v.borrow()).list.borrow()) - .items - .borrow_mut()) = (items.with(Value::clone).as_pointer() as Ptr>); - (*(*(*(*p_list.borrow()).v.borrow()).list.borrow()) - .count - .borrow_mut()) = 3_i64; - (*(*(*(*p_list.borrow()).v.borrow()).list.borrow()) - .cursor - .borrow_mut()) = 1_i64; - assert!( - ((*(*(*(*p_list.borrow()).v.borrow()).list.borrow()) - .count - .borrow()) - == 3_i64) - ); - assert!( - (((((*(*(*(*p_list.borrow()).v.borrow()).list.borrow()) - .items - .borrow()) - .offset((1) as isize) - .read()) - .offset((0) as isize) - .read()) as i32) - == ('b' as i32)) - ); - let p_letters: Value = >::default(); - (*(*p_letters.borrow()).choice.borrow_mut()) = (Choice::C_LETTERS as Choice); - (*(*p_letters.borrow()).index.borrow_mut()) = 1; - (*(*(*(*p_letters.borrow()).v.borrow()).letters.borrow()) - .lo - .borrow_mut()) = ('a' as i32); - (*(*(*(*p_letters.borrow()).v.borrow()).letters.borrow()) - .hi - .borrow_mut()) = ('z' as i32); - (*(*(*(*p_letters.borrow()).v.borrow()).letters.borrow()) - .curr - .borrow_mut()) = ('m' as i32); - (*(*(*(*p_letters.borrow()).v.borrow()).letters.borrow()) - .step - .borrow_mut()) = 1_u8; - assert!( - (((*(*(*(*p_letters.borrow()).v.borrow()).letters.borrow()) - .hi - .borrow()) - - (*(*(*(*p_letters.borrow()).v.borrow()).letters.borrow()) - .lo - .borrow())) - == 25) - ); - let p_integers: Value = >::default(); - (*(*p_integers.borrow()).choice.borrow_mut()) = (Choice::C_INTEGERS as Choice); - (*(*p_integers.borrow()).index.borrow_mut()) = 2; - (*(*(*(*p_integers.borrow()).v.borrow()).integers.borrow()) - .lo - .borrow_mut()) = 1_i64; - (*(*(*(*p_integers.borrow()).v.borrow()).integers.borrow()) - .hi - .borrow_mut()) = 100_i64; - (*(*(*(*p_integers.borrow()).v.borrow()).integers.borrow()) - .curr - .borrow_mut()) = 1_i64; - (*(*(*(*p_integers.borrow()).v.borrow()).integers.borrow()) - .step - .borrow_mut()) = 1_i64; - (*(*(*(*p_integers.borrow()).v.borrow()).integers.borrow()) - .width - .borrow_mut()) = 3; - assert!( - ((*(*(*(*p_integers.borrow()).v.borrow()).integers.borrow()) - .hi - .borrow()) - == 100_i64) - ); - assert!( - ((*(*(*(*p_integers.borrow()).v.borrow()).integers.borrow()) - .width - .borrow()) - == 3) - ); - return 0; -} diff --git a/tests/unit/out/refcount/union_void_ptr_sized_deref.rs b/tests/unit/out/refcount/union_void_ptr_sized_deref.rs deleted file mode 100644 index c3fe2bbe..00000000 --- a/tests/unit/out/refcount/union_void_ptr_sized_deref.rs +++ /dev/null @@ -1,108 +0,0 @@ -extern crate libcc2rs; -use libcc2rs::*; -use std::cell::RefCell; -use std::collections::BTreeMap; -use std::io::prelude::*; -use std::io::{Read, Seek, Write}; -use std::os::fd::AsFd; -use std::rc::{Rc, Weak}; -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum Width { - #[default] - W_64 = 0, - W_32 = 1, - W_16 = 2, -} -#[derive(Default)] -pub union Sink_anon_0 { - pub text: Value>, - pub handle: Value, - pub signed_n: Value, - pub f: Value, -} -impl Default for Sink_anon_0 { - fn default() -> Self { - unsafe { std::mem::zeroed() } - } -} -impl ByteRepr for Sink_anon_0 {} -#[derive(Default)] -pub struct Sink { - pub width: Value, - pub out: Value, -} -impl ByteRepr for Sink {} -pub fn write_count_0(s: Ptr, count: i64) { - let s: Value> = Rc::new(RefCell::new(s)); - let count: Value = Rc::new(RefCell::new(count)); - 'switch: { - let __match_cond = ((*(*(*s.borrow()).upgrade().deref()).width.borrow()) as u32); - match __match_cond { - v if v == (Width::W_64 as u32) => { - (*(*(*(*s.borrow()).upgrade().deref()).out.borrow()) - .handle - .borrow()) - .cast::() - .expect("ub:wrong type") - .write((*count.borrow())); - break 'switch; - } - v if v == (Width::W_32 as u32) => { - (*(*(*(*s.borrow()).upgrade().deref()).out.borrow()) - .handle - .borrow()) - .cast::() - .expect("ub:wrong type") - .write(((*count.borrow()) as i32)); - break 'switch; - } - v if v == (Width::W_16 as u32) => { - (*(*(*(*s.borrow()).upgrade().deref()).out.borrow()) - .handle - .borrow()) - .cast::() - .expect("ub:wrong type") - .write(((*count.borrow()) as i16)); - break 'switch; - } - _ => {} - } - }; -} -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - let buf64: Value = Rc::new(RefCell::new(0_i64)); - let buf32: Value = Rc::new(RefCell::new(0)); - let buf16: Value = Rc::new(RefCell::new(0_i16)); - let s: Value = >::default(); - (*(*s.borrow()).width.borrow_mut()) = (Width::W_64 as Width); - (*(*(*s.borrow()).out.borrow()).handle.borrow_mut()) = - ((buf64.as_pointer()) as Ptr).to_any(); - ({ - let _s: Ptr = (s.as_pointer()); - let _count: i64 = 1234605616436508552_i64; - write_count_0(_s, _count) - }); - assert!(((*buf64.borrow()) == 1234605616436508552_i64)); - (*(*s.borrow()).width.borrow_mut()) = (Width::W_32 as Width); - (*(*(*s.borrow()).out.borrow()).handle.borrow_mut()) = - ((buf32.as_pointer()) as Ptr).to_any(); - ({ - let _s: Ptr = (s.as_pointer()); - let _count: i64 = 305419896_i64; - write_count_0(_s, _count) - }); - assert!(((*buf32.borrow()) == 305419896)); - (*(*s.borrow()).width.borrow_mut()) = (Width::W_16 as Width); - (*(*(*s.borrow()).out.borrow()).handle.borrow_mut()) = - ((buf16.as_pointer()) as Ptr).to_any(); - ({ - let _s: Ptr = (s.as_pointer()); - let _count: i64 = 4660_i64; - write_count_0(_s, _count) - }); - assert!((((*buf16.borrow()) as i32) == 4660)); - return 0; -} diff --git a/tests/unit/out/unsafe/union_flex_array_member.rs b/tests/unit/out/unsafe/union_flex_array_member.rs deleted file mode 100644 index a563b679..00000000 --- a/tests/unit/out/unsafe/union_flex_array_member.rs +++ /dev/null @@ -1,59 +0,0 @@ -extern crate libc; -use libc::*; -extern crate libcc2rs; -use libcc2rs::*; -use std::collections::BTreeMap; -use std::io::{Read, Seek, Write}; -use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; -use std::rc::Rc; -#[repr(C)] -#[derive(Copy, Clone)] -pub union node_anon_0 { - pub bytes: [u8; 1], - pub aligner: *mut ::libc::c_void, -} -impl Default for node_anon_0 { - fn default() -> Self { - unsafe { std::mem::zeroed() } - } -} -#[repr(C)] -#[derive(Copy, Clone, Default)] -pub struct node { - pub len: u64, - pub x: node_anon_0, -} -pub fn main() { - unsafe { - std::process::exit(main_0() as i32); - } -} -unsafe fn main_0() -> i32 { - let mut tail_size: u64 = 32_u64; - let mut n: *mut node = ((unsafe { - let ___size: u64 = (::std::mem::size_of::() as u64 as u64).wrapping_add(tail_size); - malloc_0(___size) - }) as *mut node); - (*n).len = tail_size; - let mut i: u64 = 0_u64; - 'loop_: while ((i) < (tail_size)) { - (*n).x.bytes[(i) as usize] = ((((i) & (255_u64)) as u64) as u8); - i.postfix_inc(); - } - let mut i: u64 = 0_u64; - 'loop_: while ((i) < (tail_size)) { - assert!( - (((*n).x.bytes[(i) as usize] as i32) == (((((i) & (255_u64)) as u64) as u8) as i32)) - ); - i.postfix_inc(); - } - let mut p: *mut u8 = (&mut (*n).x.bytes[(10) as usize] as *mut u8); - assert!((((*p) as i32) == (10))); - (*p) = 170_u8; - assert!((((*n).x.bytes[(10) as usize] as i32) == (170))); - (unsafe { - let ___ptr: *mut ::libc::c_void = (n as *mut node as *mut ::libc::c_void); - free_1(___ptr) - }); - return 0; -} diff --git a/tests/unit/out/unsafe/union_tagged_struct_arms.rs b/tests/unit/out/unsafe/union_tagged_struct_arms.rs deleted file mode 100644 index 57a28bb7..00000000 --- a/tests/unit/out/unsafe/union_tagged_struct_arms.rs +++ /dev/null @@ -1,96 +0,0 @@ -extern crate libc; -use libc::*; -extern crate libcc2rs; -use libcc2rs::*; -use std::collections::BTreeMap; -use std::io::{Read, Seek, Write}; -use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; -use std::rc::Rc; -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum Choice { - #[default] - C_LIST = 1, - C_LETTERS = 2, - C_INTEGERS = 3, -} -#[repr(C)] -#[derive(Copy, Clone, Default)] -pub struct Branch_anon_0_anon_0 { - pub items: *mut *mut u8, - pub count: i64, - pub cursor: i64, -} -#[repr(C)] -#[derive(Copy, Clone, Default)] -pub struct Branch_anon_0_anon_1 { - pub lo: i32, - pub hi: i32, - pub curr: i32, - pub step: u8, -} -#[repr(C)] -#[derive(Copy, Clone, Default)] -pub struct Branch_anon_0_anon_2 { - pub lo: i64, - pub hi: i64, - pub curr: i64, - pub step: i64, - pub width: i32, -} -#[repr(C)] -#[derive(Copy, Clone)] -pub union Branch_anon_0 { - pub list: Branch_anon_0_anon_0, - pub letters: Branch_anon_0_anon_1, - pub integers: Branch_anon_0_anon_2, -} -impl Default for Branch_anon_0 { - fn default() -> Self { - unsafe { std::mem::zeroed() } - } -} -#[repr(C)] -#[derive(Copy, Clone, Default)] -pub struct Branch { - pub choice: Choice, - pub index: i32, - pub v: Branch_anon_0, -} -pub fn main() { - unsafe { - std::process::exit(main_0() as i32); - } -} -unsafe fn main_0() -> i32 { - static mut items: [*mut u8; 3] = [b"a\0".as_ptr(), b"b\0".as_ptr(), b"c\0".as_ptr()];; - let mut p_list: Branch = ::default(); - p_list.choice = (Choice::C_LIST as Choice); - p_list.index = 0; - p_list.v.list.items = items.as_mut_ptr(); - p_list.v.list.count = 3_i64; - p_list.v.list.cursor = 1_i64; - assert!(((p_list.v.list.count) == (3_i64))); - assert!( - (((*(*p_list.v.list.items.offset((1) as isize)).offset((0) as isize)) as i32) - == ('b' as i32)) - ); - let mut p_letters: Branch = ::default(); - p_letters.choice = (Choice::C_LETTERS as Choice); - p_letters.index = 1; - p_letters.v.letters.lo = ('a' as i32); - p_letters.v.letters.hi = ('z' as i32); - p_letters.v.letters.curr = ('m' as i32); - p_letters.v.letters.step = 1_u8; - assert!((((p_letters.v.letters.hi) - (p_letters.v.letters.lo)) == (25))); - let mut p_integers: Branch = ::default(); - p_integers.choice = (Choice::C_INTEGERS as Choice); - p_integers.index = 2; - p_integers.v.integers.lo = 1_i64; - p_integers.v.integers.hi = 100_i64; - p_integers.v.integers.curr = 1_i64; - p_integers.v.integers.step = 1_i64; - p_integers.v.integers.width = 3; - assert!(((p_integers.v.integers.hi) == (100_i64))); - assert!(((p_integers.v.integers.width) == (3))); - return 0; -} From 492e703ae13fe4974159084b079f6e918b0fae3d Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 28 Apr 2026 14:25:24 +0100 Subject: [PATCH 15/19] Don't overwrite memset definition on macOS --- cpp2rust/compat/platform_flags.h | 1 + 1 file changed, 1 insertion(+) diff --git a/cpp2rust/compat/platform_flags.h b/cpp2rust/compat/platform_flags.h index 91e3680b..063449a3 100644 --- a/cpp2rust/compat/platform_flags.h +++ b/cpp2rust/compat/platform_flags.h @@ -10,6 +10,7 @@ static inline std::vector getPlatformClangFlags() { std::vector flags = { "-resource-dir=" CLANG_RESOURCE_DIR, "-I" COMPAT_INCLUDE_DIR, + "-D_FORTIFY_SOURCE=0", }; #ifdef MACOS_SDK_PATH flags.push_back("-isysroot" MACOS_SDK_PATH); From 243b4a50a04d6d1d56f7a6d56bdf567b6f55c9f0 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 28 Apr 2026 15:23:32 +0100 Subject: [PATCH 16/19] Fix call of GetUserDefinedDefaultConstructor --- cpp2rust/converter/converter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 1bf26900..d89c60d4 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -3227,7 +3227,7 @@ void Converter::AddDefaultTrait(const clang::RecordDecl *decl) { PushBrace fn_brace(*this); if (auto *cxx = clang::dyn_cast(decl)) { - if (auto *default_ctor = GetUserDefinedDefaultConstructor(decl)) { + if (auto *default_ctor = GetUserDefinedDefaultConstructor(cxx)) { StrCat(keyword_unsafe_); PushBrace unsafe_brace(*this); Convert(clang::CXXConstructExpr::Create( From 906ea4e9245277a2ff70cfe254ac997a3b9953ab Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 28 Apr 2026 15:23:47 +0100 Subject: [PATCH 17/19] clang-format --- cpp2rust/converter/converter.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index d89c60d4..adddd417 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -643,7 +643,8 @@ void Converter::EmitRustStructOrUnion(clang::RecordDecl *decl) { auto access = clang::dyn_cast(decl) ? AccessSpecifierAsString(decl->getAccess()) : keyword::kPub; - StrCat(access, decl->isUnion() ? keyword::kUnion : keyword::kStruct, GetRecordName(decl)); + StrCat(access, decl->isUnion() ? keyword::kUnion : keyword::kStruct, + GetRecordName(decl)); { PushBrace brace(*this); for (auto *field : decl->fields()) { From fe8078471a0de5e97a0f348d55c3f54d8e999d5e Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 28 Apr 2026 15:37:08 +0100 Subject: [PATCH 18/19] Update tests --- tests/benchmarks/out/unsafe/bfs.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/benchmarks/out/unsafe/bfs.rs b/tests/benchmarks/out/unsafe/bfs.rs index e1a44a4c..df11cfcf 100644 --- a/tests/benchmarks/out/unsafe/bfs.rs +++ b/tests/benchmarks/out/unsafe/bfs.rs @@ -111,7 +111,6 @@ pub unsafe fn BFS_0(graph: *const Graph, mut start_vertex: u32) -> *mut u32 { head = (*head).next; } } - ::std::mem::drop(Box::from_raw(::std::slice::from_raw_parts_mut( visited, libcc2rs::malloc_usable_size(visited as *mut ::libc::c_void) @@ -173,7 +172,6 @@ unsafe fn main_0() -> i32 { } i.prefix_inc(); } - ::std::mem::drop(Box::from_raw(::std::slice::from_raw_parts_mut( graph.adj, libcc2rs::malloc_usable_size(graph.adj as *mut ::libc::c_void) From b074d3ef274a471cadf4b0d225a1d67dfb14744a Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 28 Apr 2026 15:37:45 +0100 Subject: [PATCH 19/19] Generate unsafe Default impl for union only in unsafe model --- cpp2rust/converter/converter.cpp | 15 +++++++++++---- cpp2rust/converter/converter.h | 2 ++ cpp2rust/converter/models/converter_refcount.cpp | 3 +++ cpp2rust/converter/models/converter_refcount.h | 2 ++ 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index adddd417..11867c9d 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -3210,12 +3210,19 @@ void Converter::AddCloneTrait(const clang::CXXRecordDecl *decl) {} void Converter::AddDropTrait(const clang::CXXRecordDecl *decl) {} +void Converter::AddDefaultTraitForUnion(const clang::RecordDecl *decl) { + StrCat(std::format("impl Default for {}", GetRecordName(decl))); + PushBrace impl_brace(*this); + StrCat("fn default() -> Self"); + PushBrace fn_brace(*this); + StrCat("unsafe"); + PushBrace unsafe_brace(*this); + StrCat("std::mem::zeroed()"); +} + void Converter::AddDefaultTrait(const clang::RecordDecl *decl) { if (decl->isUnion()) { - StrCat( - std::format("impl Default for {} {{ fn default() -> Self {{ unsafe {{ " - "std::mem::zeroed() }} }} }}", - GetRecordName(decl))); + AddDefaultTraitForUnion(decl); return; } if (RecordDerivesDefault(decl)) { diff --git a/cpp2rust/converter/converter.h b/cpp2rust/converter/converter.h index b38080f1..4e6140e0 100644 --- a/cpp2rust/converter/converter.h +++ b/cpp2rust/converter/converter.h @@ -445,6 +445,8 @@ class Converter : public clang::RecursiveASTVisitor { virtual void AddDefaultTrait(const clang::RecordDecl *decl); + virtual void AddDefaultTraitForUnion(const clang::RecordDecl *decl); + virtual void AddByteReprTrait(const clang::RecordDecl *decl); virtual void diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 7f636987..d681d1e3 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -465,6 +465,9 @@ void ConverterRefCount::AddDefaultTrait(const clang::RecordDecl *decl) { Converter::AddDefaultTrait(decl); } +void ConverterRefCount::AddDefaultTraitForUnion(const clang::RecordDecl *decl) { +} + void ConverterRefCount::AddDropTrait(const clang::CXXRecordDecl *decl) { if (!decl->hasUserDeclaredDestructor()) { return; diff --git a/cpp2rust/converter/models/converter_refcount.h b/cpp2rust/converter/models/converter_refcount.h index 9211bd22..0ea89e68 100644 --- a/cpp2rust/converter/models/converter_refcount.h +++ b/cpp2rust/converter/models/converter_refcount.h @@ -41,6 +41,8 @@ class ConverterRefCount final : public Converter { void AddDefaultTrait(const clang::RecordDecl *decl) override; + void AddDefaultTraitForUnion(const clang::RecordDecl *decl) override; + std::string GetSelfMaybeWithMut(const clang::CXXMethodDecl *decl) override; bool VisitCXXConstructorDecl(clang::CXXConstructorDecl *decl) override;