Skip to content

Commit 5cd8408

Browse files
committed
Translate nameless copy constructor agrument as _a0 instead of self
1 parent 02cdd79 commit 5cd8408

4 files changed

Lines changed: 84 additions & 1 deletion

File tree

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ std::string GetNamedDeclAsString(const clang::NamedDecl *decl) {
732732
llvm::dyn_cast<clang::FunctionDecl>(pdecl->getDeclContext());
733733
const auto *ctor = llvm::dyn_cast_or_null<clang::CXXConstructorDecl>(fn);
734734
if (pdecl->isExplicitObjectParameter() ||
735-
(ctor && ctor->isCopyConstructor())) {
735+
(ctor && ctor->isCopyConstructor() && ctor->isDefaulted())) {
736736
name = "self";
737737
} else {
738738
name = std::format("_a{}", pdecl->getFunctionScopeIndex());

‎tests/unit/copy_ctor.cpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ struct NonConst {
1717
NonConst(const NonConst &o) : mark(o.mark + 10) {}
1818
};
1919

20+
struct Ignored {
21+
int v;
22+
Ignored(int v) : v(v) {}
23+
Ignored(const Ignored &) : v(-1) { ++copies; }
24+
};
25+
2026
struct Holder {
2127
Counted c;
2228
Counted arr[2];
@@ -63,6 +69,11 @@ int main() {
6369
assert(vec[0].v == 1);
6470
assert(copies == 10);
6571

72+
Ignored i1(1);
73+
Ignored i2(i1);
74+
assert(i1.v == 1 && i2.v == -1);
75+
assert(copies == 11);
76+
6677
NonConst n;
6778
NonConst n1(n);
6879
const NonConst cn;

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,47 @@ impl ByteRepr for NonConst {
103103
}
104104
}
105105
}
106+
#[derive(Default)]
107+
pub struct Ignored {
108+
pub v: Value<i32>,
109+
}
110+
impl Ignored {
111+
pub fn Ignored(v: i32) -> Self {
112+
let v: Value<i32> = Rc::new(RefCell::new(v));
113+
let __this: Value<Ignored> = Rc::new(RefCell::new(Self {
114+
v: Rc::new(RefCell::new((*v.borrow()))),
115+
}));
116+
let this: Ptr<Ignored> = __this.as_pointer();
117+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
118+
}
119+
pub fn Ignored_pconstIgnored(_a0: Ptr<Ignored>) -> Self {
120+
let __this: Value<Ignored> = Rc::new(RefCell::new(Self {
121+
v: Rc::new(RefCell::new(-1_i32)),
122+
}));
123+
let this: Ptr<Ignored> = __this.as_pointer();
124+
(*copies_0.with(Value::clone).borrow_mut()).prefix_inc();
125+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
126+
}
127+
}
128+
impl Clone for Ignored {
129+
fn clone(&self) -> Self {
130+
let __src: Value<Ignored> = Rc::new(RefCell::new(Ignored { v: self.v.clone() }));
131+
Ignored::Ignored_pconstIgnored(__src.as_pointer())
132+
}
133+
}
134+
impl ByteRepr for Ignored {
135+
fn byte_size() -> usize {
136+
4
137+
}
138+
fn to_bytes(&self, buf: &mut [u8]) {
139+
(*self.v.borrow()).to_bytes(&mut buf[0..4]);
140+
}
141+
fn from_bytes(buf: &[u8]) -> Self {
142+
Self {
143+
v: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
144+
}
145+
}
146+
}
106147
#[derive()]
107148
pub struct Holder {
108149
pub c: Value<Counted>,
@@ -225,6 +266,12 @@ fn main_0() -> i32 {
225266
== 1)
226267
);
227268
assert!((copies_0.with(|rc| *rc.borrow()) == 10));
269+
let i1: Value<Ignored> = Rc::new(RefCell::new(Ignored::Ignored({ 1 })));
270+
let i2: Value<Ignored> = Rc::new(RefCell::new(Ignored::Ignored_pconstIgnored({
271+
i1.as_pointer()
272+
})));
273+
assert!(((*(*i1.borrow()).v.borrow()) == 1) && ((*(*i2.borrow()).v.borrow()) == -1_i32));
274+
assert!((copies_0.with(|rc| *rc.borrow()) == 11));
228275
let n: Value<NonConst> = Rc::new(RefCell::new(NonConst::NonConst()));
229276
let n1: Value<NonConst> = Rc::new(RefCell::new(NonConst::NonConst_pmutNonConst({
230277
n.as_pointer()

‎tests/unit/out/unsafe/copy_ctor.rs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,27 @@ impl Default for NonConst {
6262
}
6363
}
6464
#[repr(C)]
65+
#[derive(Default)]
66+
pub struct Ignored {
67+
pub v: i32,
68+
}
69+
impl Ignored {
70+
pub unsafe fn Ignored(mut v: i32) -> Self {
71+
let mut this = Self { v: v };
72+
this
73+
}
74+
pub unsafe fn Ignored_pconstIgnored(_a0: *const Ignored) -> Self {
75+
let mut this = Self { v: -1_i32 };
76+
(*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)).prefix_inc();
77+
this
78+
}
79+
}
80+
impl Clone for Ignored {
81+
fn clone(&self) -> Self {
82+
unsafe { Ignored::Ignored_pconstIgnored(self as *const Ignored) }
83+
}
84+
}
85+
#[repr(C)]
6586
#[derive(Clone)]
6687
pub struct Holder {
6788
pub c: Counted,
@@ -124,6 +145,10 @@ unsafe fn main_0() -> i32 {
124145
};
125146
assert!(((vec_[(0_usize)].v) == (1)));
126147
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)) == (10)));
148+
let mut i1: Ignored = Ignored::Ignored({ 1 });
149+
let mut i2: Ignored = Ignored::Ignored_pconstIgnored({ &i1 });
150+
assert!(((i1.v) == (1)) && ((i2.v) == (-1_i32)));
151+
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)) == (11)));
127152
let mut n: NonConst = NonConst::NonConst();
128153
let mut n1: NonConst = NonConst::NonConst_pmutNonConst({ &mut n });
129154
let cn: NonConst = NonConst::NonConst();

0 commit comments

Comments
 (0)