Skip to content

Commit 3751b5b

Browse files
committed
Merge branch 'master' into mismatched-pointers
2 parents 2669686 + 09a4a4e commit 3751b5b

46 files changed

Lines changed: 195 additions & 494 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎cpp2rust/converter/converter.cpp‎

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -645,33 +645,35 @@ bool Converter::RecordDerivesDefault(const clang::RecordDecl *decl) {
645645
}
646646

647647
for (auto f : decl->fields()) {
648-
if (f->hasInClassInitializer()) {
648+
if (f->hasInClassInitializer() || !TypeDerivesDefault(f->getType())) {
649649
return false;
650650
}
651+
}
651652

652-
// Records that contain function pointer do not derive Default
653-
if (auto ptr_ty = f->getType()->getAs<clang::PointerType>()) {
654-
if (ptr_ty->getPointeeType()->isFunctionType()) {
655-
return false;
656-
}
657-
}
653+
return true;
654+
}
658655

659-
// Records that contain std::array do not derive Default
660-
if (Printer::ToString(ctx_, f->getType()).contains("std::array")) {
661-
return false;
662-
}
656+
bool Converter::TypeDerivesDefault(clang::QualType qual_type) {
657+
// Rust only implements Default for arrays of up to 32 elements
658+
if (auto *array_type = ctx_.getAsConstantArrayType(qual_type)) {
659+
return array_type->getSize().ule(32) &&
660+
TypeDerivesDefault(array_type->getElementType());
661+
}
662+
if (qual_type->isArrayType()) {
663+
return false;
664+
}
663665

664-
// Records that contain C arrays do not derive Default
665-
if (f->getType()->isArrayType()) {
666-
return false;
667-
}
666+
// Types that need a custom array initializer (e.g., std::array, which is
667+
// translated to Vec)
668+
if (!GetArrayDefaultAsString(qual_type).empty()) {
669+
return false;
670+
}
668671

669-
// Records that contain libc types do not derive Default
670-
if (auto record = f->getType()->getAsRecordDecl()) {
671-
if (ctx_.getSourceManager().isInSystemHeader(record->getLocation()) &&
672-
f->getType().isPODType(ctx_)) {
673-
return false;
674-
}
672+
// libc types do not implement Default
673+
if (auto record = qual_type->getAsRecordDecl()) {
674+
if (ctx_.getSourceManager().isInSystemHeader(record->getLocation()) &&
675+
qual_type.isPODType(ctx_)) {
676+
return false;
675677
}
676678
}
677679

‎cpp2rust/converter/converter.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
730730

731731
virtual bool RecordDerivesDefault(const clang::RecordDecl *decl);
732732

733+
virtual bool TypeDerivesDefault(clang::QualType qual_type);
734+
733735
bool RecordDerivesCopy(const clang::RecordDecl *decl) const;
734736

735737
bool IsPassThroughRule(clang::Expr *expr) const;

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2311,6 +2311,11 @@ ConverterRefCount::GetStructAttributes(const clang::RecordDecl *decl) {
23112311
return attrs;
23122312
}
23132313

2314+
bool ConverterRefCount::TypeDerivesDefault(clang::QualType qual_type) {
2315+
// Arrays are translated to Value<Box<[T]>>, whose Default is empty
2316+
return !qual_type->isArrayType() && Converter::TypeDerivesDefault(qual_type);
2317+
}
2318+
23142319
std::string ConverterRefCount::ConvertVarInitValue(clang::QualType qual_type,
23152320
clang::Expr *expr) {
23162321
if (auto lambda = clang::dyn_cast<clang::LambdaExpr>(

‎cpp2rust/converter/models/converter_refcount.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ class ConverterRefCount final : public Converter {
227227
std::vector<const char *>
228228
GetStructAttributes(const clang::RecordDecl *decl) override;
229229

230+
bool TypeDerivesDefault(clang::QualType qual_type) override;
231+
230232
bool Convert(clang::QualType qual_type) override;
231233
bool
232234
Convert(clang::Expr *expr,

‎tests/multi-file/defaulted_move_cross_tu/out/unsafe/defaulted_move_cross_tu.rs‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write};
77
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
88
use std::rc::Rc;
99
#[repr(C)]
10-
#[derive()]
10+
#[derive(Default)]
1111
pub struct S {
1212
pub v: Vec<i32>,
1313
pub n: [i32; 2],
@@ -42,14 +42,6 @@ impl S {
4242
return &mut (*(self as *mut S));
4343
}
4444
}
45-
impl Default for S {
46-
fn default() -> Self {
47-
S {
48-
v: Default::default(),
49-
n: [0_i32; 2],
50-
}
51-
}
52-
}
5345
pub unsafe fn sum_0(s: *const S) -> i32 {
5446
return ((((*s).v.len() as i32) + ((*s).n[(0) as usize])) + ((*s).n[(1) as usize]));
5547
}

‎tests/unit/default.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,23 @@ struct Pointers {
66
int x5;
77
};
88

9+
struct SmallArrays {
10+
int a[32];
11+
int (*f)(int);
12+
int (*fs[2])(int);
13+
Pointers p[2];
14+
};
15+
16+
struct BigArray {
17+
int a[33];
18+
};
19+
920
int main() {
1021
Pointers *default_pointers = new Pointers[10];
1122
delete[] default_pointers;
23+
SmallArrays *small = new SmallArrays[2];
24+
delete[] small;
25+
BigArray *big = new BigArray[2];
26+
delete[] big;
1227
return 0;
1328
}

‎tests/unit/fft.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ADDITIONAL_COMPILE_FLAGS: -fmath-errno
2+
13
#include <cassert>
24
#include <cmath>
35
#include <memory>

‎tests/unit/out/refcount/default.rs‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,54 @@ impl Default for Pointers {
4343
}
4444
}
4545
}
46+
#[derive(DeepClone, Record, ByteRepr, VaArg, FnPtrArg)]
47+
#[byte_size(440)]
48+
pub struct SmallArrays {
49+
#[offset(0)]
50+
#[byte_size(128)]
51+
pub a: Value<Box<[i32]>>,
52+
#[offset(128)]
53+
#[byte_size(8)]
54+
pub f: FnPtr<fn(i32) -> i32>,
55+
#[offset(136)]
56+
#[byte_size(16)]
57+
pub fs: Value<Box<[FnPtr<fn(i32) -> i32>]>>,
58+
#[offset(152)]
59+
#[byte_size(288)]
60+
pub p: Value<Box<[Pointers]>>,
61+
}
62+
impl Default for SmallArrays {
63+
fn default() -> Self {
64+
SmallArrays {
65+
a: Rc::new(RefCell::new((0..32).map(|_| 0_i32).collect::<Box<[i32]>>())),
66+
f: FnPtr::<fn(i32) -> i32>::null(),
67+
fs: Rc::new(RefCell::new(
68+
(0..2)
69+
.map(|_| FnPtr::<fn(i32) -> i32>::null())
70+
.collect::<Box<[FnPtr<fn(i32) -> i32>]>>(),
71+
)),
72+
p: Rc::new(RefCell::new(
73+
(0..2)
74+
.map(|_| <Pointers>::default())
75+
.collect::<Box<[Pointers]>>(),
76+
)),
77+
}
78+
}
79+
}
80+
#[derive(DeepClone, Record, ByteRepr, VaArg, FnPtrArg)]
81+
#[byte_size(132)]
82+
pub struct BigArray {
83+
#[offset(0)]
84+
#[byte_size(132)]
85+
pub a: Value<Box<[i32]>>,
86+
}
87+
impl Default for BigArray {
88+
fn default() -> Self {
89+
BigArray {
90+
a: Rc::new(RefCell::new((0..33).map(|_| 0_i32).collect::<Box<[i32]>>())),
91+
}
92+
}
93+
}
4694
pub fn main() {
4795
__cpp2rust_init_globals();
4896
std::process::exit(main_0());
@@ -54,6 +102,18 @@ fn main_0() -> i32 {
54102
.collect::<Box<[Pointers]>>(),
55103
)));
56104
(*default_pointers.borrow()).delete();
105+
let small: Value<Ptr<SmallArrays>> = Rc::new(RefCell::new(Ptr::alloc_array(
106+
(0..2_usize)
107+
.map(|_| <SmallArrays>::default())
108+
.collect::<Box<[SmallArrays]>>(),
109+
)));
110+
(*small.borrow()).delete();
111+
let big: Value<Ptr<BigArray>> = Rc::new(RefCell::new(Ptr::alloc_array(
112+
(0..2_usize)
113+
.map(|_| <BigArray>::default())
114+
.collect::<Box<[BigArray]>>(),
115+
)));
116+
(*big.borrow()).delete();
57117
return 0;
58118
}
59119
pub fn __cpp2rust_init_globals() {}

‎tests/unit/out/refcount/default_in_statics.rs‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl Default for Outer {
6060
}
6161
}
6262
}
63-
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)]
63+
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
6464
#[byte_size(40)]
6565
pub struct Foo {
6666
#[offset(0)]
@@ -78,17 +78,6 @@ pub struct Foo {
7878
#[offset(32)]
7979
pub n: i32,
8080
}
81-
impl Default for Foo {
82-
fn default() -> Self {
83-
Foo {
84-
s1: Ptr::<u8>::null(),
85-
s2: Ptr::<u8>::null(),
86-
fn1: FnPtr::<fn(i32) -> i32>::null(),
87-
fn2: FnPtr::<fn(i32) -> i32>::null(),
88-
n: 0_i32,
89-
}
90-
}
91-
}
9281
thread_local!(
9382
pub static static_fn_0: Value<FnPtr<fn(i32) -> i32>> =
9483
Rc::new(RefCell::new(FnPtr::<fn(i32) -> i32>::null()));

‎tests/unit/out/refcount/fn_ptr.rs‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub fn identity_hash_6(v: bool) -> usize {
3636
let v: Value<bool> = Rc::new(RefCell::new(v));
3737
return ((*v.borrow()) as usize);
3838
}
39-
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)]
39+
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
4040
#[byte_size(8)]
4141
pub struct HashHolder_unsigned_long__ptr__bool__ {
4242
#[offset(0)]
@@ -52,13 +52,6 @@ impl HashHolder_unsigned_long__ptr__bool__ {
5252
Rc::try_unwrap(__this).ok().unwrap().into_inner()
5353
}
5454
}
55-
impl Default for HashHolder_unsigned_long__ptr__bool__ {
56-
fn default() -> Self {
57-
HashHolder_unsigned_long__ptr__bool__ {
58-
h: FnPtr::<fn(bool) -> u64>::null(),
59-
}
60-
}
61-
}
6255
pub fn main() {
6356
__cpp2rust_init_globals();
6457
std::process::exit(main_0());

0 commit comments

Comments
 (0)