Skip to content

Commit da01bbc

Browse files
committed
refcount: structs should derive Copy whenever possible
1 parent 31a138f commit da01bbc

190 files changed

Lines changed: 519 additions & 503 deletions

File tree

Some content is hidden

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

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,22 @@ bool ConverterRefCount::RecordDerivesClone(const clang::RecordDecl *decl) {
696696
!GetUserDefinedCopyConstructor(cxx) && HasDefaultedBaseCopies(cxx));
697697
}
698698

699+
// A struct is Copy when its copy is a field-wise copy of fields that are all
700+
// Copy in Rust. Pointers (Ptr) and arrays (Box<[T]>) are not.
701+
bool ConverterRefCount::RecordDerivesCopyTrait(const clang::RecordDecl *decl) {
702+
if (decl->isUnion() || !RecordDerivesClone(decl) ||
703+
!HasDefaultedCopyConstructor(decl)) {
704+
return false;
705+
}
706+
if (auto *cxx = clang::dyn_cast<clang::CXXRecordDecl>(decl);
707+
cxx && RecordNeedsDestruction(cxx)) {
708+
return false;
709+
}
710+
return llvm::all_of(decl->fields(), [&](const clang::FieldDecl *field) {
711+
return TypeIsCopyable(field->getType());
712+
});
713+
}
714+
699715
void ConverterRefCount::AddCloneTrait(const clang::RecordDecl *decl) {
700716
if (RecordDerivesClone(decl) || !RecordImplementsClone(decl)) {
701717
return;
@@ -2346,6 +2362,10 @@ ConverterRefCount::GetStructAttributes(const clang::RecordDecl *decl) {
23462362
return {"VaArg", "FnPtrArg"};
23472363
}
23482364

2365+
if (RecordDerivesCopyTrait(decl)) {
2366+
attrs.emplace_back("Copy");
2367+
}
2368+
23492369
if (RecordDerivesClone(decl)) {
23502370
attrs.emplace_back("Clone");
23512371
}

‎cpp2rust/converter/models/converter_refcount.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class ConverterRefCount final : public Converter {
6868

6969
bool RecordDerivesClone(const clang::RecordDecl *decl);
7070

71+
bool RecordDerivesCopyTrait(const clang::RecordDecl *decl);
72+
7173
void EmitByteSizeAttr(const clang::RecordDecl *decl) override;
7274

7375
bool

‎tests/multi-file/cross_tu_tag_collision/out/refcount/cross_tu_tag_collision.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::io::prelude::*;
66
use std::io::{Read, Seek, Write};
77
use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
9-
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
9+
#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
1010
#[byte_size(4)]
1111
pub struct widget {
1212
#[offset(0)]

‎tests/multi-file/friend_operator/out/refcount/friend_operator.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::io::prelude::*;
66
use std::io::{Read, Seek, Write};
77
use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
9-
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
9+
#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
1010
#[byte_size(8)]
1111
pub struct S {
1212
#[offset(0)]

‎tests/multi-file/out_of_line_methods_cross_tu/out/refcount/out_of_line_methods_cross_tu.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::io::prelude::*;
66
use std::io::{Read, Seek, Write};
77
use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
9-
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
9+
#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
1010
#[byte_size(4)]
1111
pub struct S {
1212
#[offset(0)]
@@ -16,14 +16,14 @@ impl S {}
1616
pub trait Base {
1717
fn apply(&mut self, x: i32) -> i32;
1818
}
19-
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
19+
#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
2020
#[byte_size(16)]
2121
pub struct Derived {
2222
#[offset(8)]
2323
pub factor: i32,
2424
}
2525
impl Derived {}
26-
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
26+
#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
2727
#[byte_size(8)]
2828
pub struct Pair {
2929
#[offset(0)]

‎tests/multi-file/template_method_late_instantiation/out/refcount/template_method_late_instantiation.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::io::prelude::*;
66
use std::io::{Read, Seek, Write};
77
use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
9-
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)]
9+
#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)]
1010
#[byte_size(4)]
1111
pub struct S_int_ {
1212
#[offset(0)]

‎tests/std/basic/basic.def.odr/p14-class-in-two-files/out/refcount/p14-class-in-two-files.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ thread_local!(
8080
thread_local!(
8181
pub static shared_5: Value<LifetimeTracked> = Rc::new(RefCell::new(LifetimeTracked::new()));
8282
);
83-
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
83+
#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
8484
#[byte_size(1)]
8585
pub struct HasInlineMembers {}
8686
impl HasInlineMembers {

‎tests/std/basic/basic.def.odr/p15-class-template-static-member/out/refcount/p15-class-template-static-member.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ impl Default for LifetimeTracked {
7070
thread_local!(
7171
pub static member_4: Value<LifetimeTracked> = Rc::new(RefCell::new(LifetimeTracked::new()));
7272
);
73-
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
73+
#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
7474
#[byte_size(1)]
7575
pub struct HasStaticMember_int_ {}
7676
thread_local!(
7777
pub static member_5: Value<LifetimeTracked> = Rc::new(RefCell::new(LifetimeTracked::new()));
7878
);
79-
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
79+
#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
8080
#[byte_size(1)]
8181
pub struct HasStaticMember_long_ {}
8282
pub fn main() {

‎tests/std/basic/basic.def.odr/p15-function-template/out/refcount/p15-function-template.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ fn main_0() -> i32 {
122122
assert!((ctorCount_0.with(|rc| *rc.borrow()) == 4));
123123
return 0;
124124
}
125-
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
125+
#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
126126
#[byte_size(1)]
127127
pub struct S_14 {}
128128
pub fn count_18() -> i32 {
@@ -143,7 +143,7 @@ pub fn b_count_long_17() -> i32 {
143143
pub fn b_count_local_15() -> i32 {
144144
return ({ count_18() });
145145
}
146-
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
146+
#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
147147
#[byte_size(1)]
148148
pub struct S_21 {}
149149
pub trait AssertsDtorCountAtExitImpl {

‎tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/out/refcount/p16-default-argument-lambda-in-class.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub struct AssertsDtorCountAtExit {
7373
#[offset(0)]
7474
pub expected: i32,
7575
}
76-
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
76+
#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
7777
#[byte_size(1)]
7878
pub struct X {}
7979
thread_local!(

0 commit comments

Comments
 (0)