Skip to content

Commit de0e4d7

Browse files
committed
fix translation of Brunsli
1 parent 8f93176 commit de0e4d7

8 files changed

Lines changed: 159 additions & 8 deletions

File tree

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -642,11 +642,12 @@ static bool NeedsDefaultedMoveConstructor(const clang::CXXRecordDecl *decl) {
642642

643643
bool IsConvertibleMoveConstructor(const clang::CXXConstructorDecl *ctor) {
644644
auto *parent = ctor->getParent();
645-
if (!ctor->isMoveConstructor() || ctor->isDeleted() || !ctor->hasBody() ||
645+
if (!ctor->isMoveConstructor() || ctor->isDeleted() ||
646646
!IsConvertibleImplicitMemberParent(parent)) {
647647
return false;
648648
}
649-
return ctor->isUserProvided() || NeedsDefaultedMoveConstructor(parent);
649+
return ctor->isUserProvided() ||
650+
(ctor->hasBody() && NeedsDefaultedMoveConstructor(parent));
650651
}
651652

652653
bool MovesRuleArg(clang::Expr *expr, unsigned arg_idx, clang::Expr *arg,
@@ -811,7 +812,8 @@ bool IsConvertedConstructor(const clang::CXXConstructorDecl *decl) {
811812
(decl->isImplicit() && !IsConvertibleImplicitMember(decl))) {
812813
return false;
813814
}
814-
return !decl->isCopyOrMoveConstructor() || decl->hasBody();
815+
return !decl->isCopyOrMoveConstructor() || decl->isUserProvided() ||
816+
decl->hasBody();
815817
}
816818

817819
bool IsDefaultedMoveConstructor(const clang::CXXConstructorDecl *ctor) {
@@ -1916,7 +1918,7 @@ bool IsEmittableMethod(clang::CXXMethodDecl *method) {
19161918
return method->hasBody();
19171919
}
19181920
if (IsConvertibleImplicitMember(method)) {
1919-
return method->hasBody();
1921+
return method->hasBody() || method->isUserProvided();
19201922
}
19211923
// Compiler-generated members are covered by derived traits
19221924
if (method->isImplicit()) {

‎tests/multi-file/out_of_line_methods_cross_tu/a.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ int main() {
99
assert(s.get() == 4);
1010
assert(s.add(2) == 6);
1111

12+
M m(2);
13+
M n(static_cast<M &&>(m));
14+
assert(n.v == 2);
15+
M c(n);
16+
assert(c.v == 3);
17+
assert(move_m(5) == 5);
18+
1219
Derived derived(3);
1320
Base *base = &derived;
1421
assert(base->apply(5) == 15);

‎tests/multi-file/out_of_line_methods_cross_tu/b.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ int S::add(int x) {
1111
return v;
1212
}
1313

14+
M::M(int x) : v(x) {}
15+
16+
M::M(M &&) = default;
17+
18+
M::M(const M &o) : v(o.v + 1) {}
19+
20+
int move_m(int x) {
21+
M m(x);
22+
M n(static_cast<M &&>(m));
23+
return n.v;
24+
}
25+
1426
Derived::Derived(int factor) : factor(factor) {}
1527

1628
int Derived::apply(int x) { return factor * x; }

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

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ pub struct S {
1212
#[offset(0)]
1313
pub v: i32,
1414
}
15+
#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg, MoveCtor, Default)]
16+
#[byte_size(4)]
17+
pub struct M {
18+
#[offset(0)]
19+
pub v: i32,
20+
}
1521
pub trait Base {
1622
fn apply(&mut self, x: i32) -> i32;
1723
}
@@ -46,6 +52,24 @@ fn main_0() -> i32 {
4652
({ SImpl::set(&s.as_pointer(), 4) });
4753
assert!((({ SImpl::get(&s.as_pointer(),) }) == 4));
4854
assert!((({ SImpl::add(&s.as_pointer(), 2,) }) == 6));
55+
let m: Value<M> = Rc::new(RefCell::new({
56+
let __v: Value<M> = Rc::new(RefCell::new(<M>::zero_init()));
57+
MImpl::construct(&__v.as_pointer(), { 2 });
58+
Rc::try_unwrap(__v).ok().unwrap().into_inner()
59+
}));
60+
let n: Value<M> = Rc::new(RefCell::new({
61+
let __v: Value<M> = Rc::new(RefCell::new(<M>::zero_init()));
62+
MImpl::move_from(&__v.as_pointer(), { m.as_pointer() });
63+
Rc::try_unwrap(__v).ok().unwrap().into_inner()
64+
}));
65+
assert!(({ (*n.borrow()).v } == 2));
66+
let mut c: M = {
67+
let __v: Value<M> = Rc::new(RefCell::new(<M>::zero_init()));
68+
MImpl::copy_from(&__v.as_pointer(), { n.as_pointer() });
69+
Rc::try_unwrap(__v).ok().unwrap().into_inner()
70+
};
71+
assert!((c.v == 3));
72+
assert!((({ move_m_1(5,) }) == 5));
4973
let derived: Value<Derived> = Rc::new(RefCell::new({
5074
let __v: Value<Derived> = Rc::new(RefCell::new(<Derived>::zero_init()));
5175
DerivedImpl::construct(&__v.as_pointer(), { 3 });
@@ -57,13 +81,29 @@ fn main_0() -> i32 {
5781
first: 7,
5882
second: 3,
5983
}));
60-
assert!((({ pair_sum_1((pair.as_pointer()),) }) == 10));
84+
assert!((({ pair_sum_2((pair.as_pointer()),) }) == 10));
6185
assert!((({ pair_diff_0((pair.as_pointer()),) }) == 4));
6286
return 0;
6387
}
6488
impl S {}
89+
impl M {}
90+
impl M {}
91+
impl M {}
92+
pub fn move_m_1(mut x: i32) -> i32 {
93+
let m: Value<M> = Rc::new(RefCell::new({
94+
let __v: Value<M> = Rc::new(RefCell::new(<M>::zero_init()));
95+
MImpl::construct(&__v.as_pointer(), { x });
96+
Rc::try_unwrap(__v).ok().unwrap().into_inner()
97+
}));
98+
let mut n: M = {
99+
let __v: Value<M> = Rc::new(RefCell::new(<M>::zero_init()));
100+
MImpl::move_from(&__v.as_pointer(), { m.as_pointer() });
101+
Rc::try_unwrap(__v).ok().unwrap().into_inner()
102+
};
103+
return n.v;
104+
}
65105
impl Derived {}
66-
pub fn pair_sum_1(mut p: Ptr<Pair>) -> i32 {
106+
pub fn pair_sum_2(mut p: Ptr<Pair>) -> i32 {
67107
return ({ p.with(|__s| __s.first) } + { p.with(|__s| __s.second) });
68108
}
69109
impl Base for Derived {
@@ -81,6 +121,28 @@ impl DerivedImpl for Ptr<Derived> {
81121
field!((*self), factor).write(factor);
82122
}
83123
}
124+
pub trait MImpl {
125+
fn construct(&self, mut x: i32) {
126+
unimplemented!()
127+
}
128+
fn move_from(&self, _a0: Ptr<M>) {
129+
unimplemented!()
130+
}
131+
fn copy_from(&self, _a0: Ptr<M>) {
132+
unimplemented!()
133+
}
134+
}
135+
impl MImpl for Ptr<M> {
136+
fn construct(&self, mut x: i32) {
137+
field!((*self), v).write(x);
138+
}
139+
fn move_from(&self, _a0: Ptr<M>) {
140+
field!((*self), v).write({ (*_a0.upgrade().deref()).v });
141+
}
142+
fn copy_from(&self, o: Ptr<M>) {
143+
field!((*self), v).write((o.with(|__s| __s.v) + 1));
144+
}
145+
}
84146
pub trait SImpl {
85147
fn construct(&self, mut x: i32) {
86148
unimplemented!()

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

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ impl S {
1616
return self.v;
1717
}
1818
}
19+
#[repr(C)]
20+
#[derive(FnPtrArg, ZeroInitializationUnsafe, MoveCtorUnsafe, Default)]
21+
pub struct M {
22+
pub v: i32,
23+
}
24+
impl M {}
1925
pub unsafe trait Base {
2026
unsafe fn apply(&mut self, x: i32) -> i32;
2127
}
@@ -50,6 +56,24 @@ unsafe fn main_0() -> i32 {
5056
(unsafe { S::set(&mut s, 4) });
5157
assert!(((unsafe { S::get(&s,) }) == (4)));
5258
assert!(((unsafe { S::add(&mut s, 2,) }) == (6)));
59+
let mut m: M = {
60+
let mut __v: M = <M>::zero_init();
61+
M::construct(&mut __v, { 2 });
62+
__v
63+
};
64+
let mut n: M = {
65+
let mut __v: M = <M>::zero_init();
66+
M::move_from(&mut __v, { &mut m });
67+
__v
68+
};
69+
assert!(((n.v) == (2)));
70+
let mut c: M = {
71+
let mut __v: M = <M>::zero_init();
72+
M::copy_from(&mut __v, { &n });
73+
__v
74+
};
75+
assert!(((c.v) == (3)));
76+
assert!(((unsafe { move_m_1(5,) }) == (5)));
5377
let mut derived: Derived = {
5478
let mut __v: Derived = <Derived>::zero_init();
5579
Derived::construct(&mut __v, { 3 });
@@ -61,7 +85,7 @@ unsafe fn main_0() -> i32 {
6185
first: 7,
6286
second: 3,
6387
};
64-
assert!(((unsafe { pair_sum_1((&mut pair as *mut Pair).cast_const(),) }) == (10)));
88+
assert!(((unsafe { pair_sum_2((&mut pair as *mut Pair).cast_const(),) }) == (10)));
6589
assert!(((unsafe { pair_diff_0((&mut pair as *mut Pair).cast_const(),) }) == (4)));
6690
return 0;
6791
}
@@ -70,6 +94,17 @@ impl S {
7094
(&raw mut self.v).write(x);
7195
}
7296
}
97+
impl M {
98+
pub unsafe fn construct(&mut self, mut x: i32) {
99+
(&raw mut self.v).write(x);
100+
}
101+
pub unsafe fn move_from(&mut self, _a0: *mut M) {
102+
(&raw mut self.v).write((*_a0).v);
103+
}
104+
pub unsafe fn copy_from(&mut self, o: *const M) {
105+
(&raw mut self.v).write((((*o).v) + (1)));
106+
}
107+
}
73108
impl Derived {
74109
pub unsafe fn construct(&mut self, mut factor: i32) {
75110
(&raw mut self.factor).write(factor);
@@ -90,8 +125,24 @@ impl S {
90125
return self.v;
91126
}
92127
}
128+
impl M {}
129+
impl M {}
130+
impl M {}
131+
pub unsafe fn move_m_1(mut x: i32) -> i32 {
132+
let mut m: M = {
133+
let mut __v: M = <M>::zero_init();
134+
M::construct(&mut __v, { x });
135+
__v
136+
};
137+
let mut n: M = {
138+
let mut __v: M = <M>::zero_init();
139+
M::move_from(&mut __v, { &mut m });
140+
__v
141+
};
142+
return n.v;
143+
}
93144
impl Derived {}
94-
pub unsafe fn pair_sum_1(mut p: *const Pair) -> i32 {
145+
pub unsafe fn pair_sum_2(mut p: *const Pair) -> i32 {
95146
return (((*p).first) + ((*p).second));
96147
}
97148
unsafe impl Base for Derived {

‎tests/multi-file/out_of_line_methods_cross_tu/s.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ class S {
1313
int v;
1414
};
1515

16+
class M {
17+
public:
18+
M(int x);
19+
M(M &&);
20+
M(const M &);
21+
22+
int v;
23+
};
24+
25+
int move_m(int x);
26+
1627
class Base {
1728
public:
1829
virtual ~Base() {}

‎tests/std/basic/out/refcount/basic.def.odr.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,9 @@ impl p9_destructor_not_potentially_invoked_HasUndefinedDestructorImpl
963963
}
964964
pub trait p9_unused_copy_constructor_HasUndefinedCopyCtorImpl {
965965
fn construct(&self);
966+
fn copy_from(&self, _a0: Ptr<p9_unused_copy_constructor_HasUndefinedCopyCtor>) {
967+
unimplemented!()
968+
}
966969
}
967970
impl p9_unused_copy_constructor_HasUndefinedCopyCtorImpl
968971
for Ptr<p9_unused_copy_constructor_HasUndefinedCopyCtor>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ impl NoCopyImpl for Ptr<NoCopy> {
184184
}
185185
}
186186
pub trait PrivateCopyImpl {
187+
fn copy_from(&self, _a0: Ptr<PrivateCopy>) {
188+
unimplemented!()
189+
}
187190
fn copy_assign(&self, _a0: Ptr<PrivateCopy>) -> Ptr<PrivateCopy> {
188191
unimplemented!()
189192
}

0 commit comments

Comments
 (0)