Skip to content

Commit 4e148ca

Browse files
authored
Use idiomatic S::new for ctor instead of S::S (#420)
1 parent 4e23c19 commit 4e148ca

55 files changed

Lines changed: 431 additions & 453 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: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,10 +1146,9 @@ std::string Converter::GetCtorName(clang::CXXConstructorDecl *decl) {
11461146
if (decl->isCopyOrMoveConstructor()) {
11471147
return GetOverloadedFunctionName(decl);
11481148
}
1149-
return GetRecordName(decl->getParent()) +
1150-
(GetNumberOfConvertingCtors(decl->getParent()) != 1
1151-
? std::to_string(GetCtorIndex(decl))
1152-
: "");
1149+
return GetNumberOfConvertingCtors(decl->getParent()) != 1
1150+
? std::format("new_{}", GetCtorIndex(decl))
1151+
: "new";
11531152
}
11541153

11551154
bool Converter::VisitCXXConstructorDecl(clang::CXXConstructorDecl *decl) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub struct S {
1212
pub n: Value<Box<[i32]>>,
1313
}
1414
impl S {
15-
pub fn S(x: i32) -> Self {
15+
pub fn new(x: i32) -> Self {
1616
let x: Value<i32> = Rc::new(RefCell::new(x));
1717
let __this: Value<S> = Rc::new(RefCell::new(Self {
1818
v: Rc::new(RefCell::new(vec![
@@ -76,17 +76,17 @@ pub fn main() {
7676
std::process::exit(main_0());
7777
}
7878
fn main_0() -> i32 {
79-
let s: Value<S> = Rc::new(RefCell::new(S::S({ 2 })));
79+
let s: Value<S> = Rc::new(RefCell::new(S::new({ 2 })));
8080
assert!((({ sum_0(s.as_pointer(),) }) == 7));
8181
assert!((({ shuffle_1(3,) }) == 10));
8282
return 0;
8383
}
8484
pub fn shuffle_1(x: i32) -> i32 {
8585
let x: Value<i32> = Rc::new(RefCell::new(x));
86-
let a: Value<S> = Rc::new(RefCell::new(S::S({ (*x.borrow()) })));
86+
let a: Value<S> = Rc::new(RefCell::new(S::new({ (*x.borrow()) })));
8787
let b: Value<S> = Rc::new(RefCell::new(S::S_pmutS_rv({ a.as_pointer() })));
8888
assert!((*(*a.borrow()).v.borrow()).is_empty());
89-
let c: Value<S> = Rc::new(RefCell::new(S::S({ 1 })));
89+
let c: Value<S> = Rc::new(RefCell::new(S::new({ 1 })));
9090
({ SImpl::operator_assign_pmutS_rv(&c.as_pointer(), b.as_pointer()) });
9191
assert!((*(*b.borrow()).v.borrow()).is_empty());
9292
return ({ sum_0(c.as_pointer()) });

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub struct S {
1313
pub n: [i32; 2],
1414
}
1515
impl S {
16-
pub unsafe fn S(mut x: i32) -> Self {
16+
pub unsafe fn new(mut x: i32) -> Self {
1717
let mut this = Self {
1818
v: vec![x; (x as usize) as usize],
1919
n: [x, ((x) + (1))],
@@ -60,16 +60,16 @@ pub fn main() {
6060
}
6161
}
6262
unsafe fn main_0() -> i32 {
63-
let mut s: S = S::S({ 2 });
63+
let mut s: S = S::new({ 2 });
6464
assert!(((unsafe { sum_0(&s,) }) == (7)));
6565
assert!(((unsafe { shuffle_1(3,) }) == (10)));
6666
return 0;
6767
}
6868
pub unsafe fn shuffle_1(mut x: i32) -> i32 {
69-
let mut a: S = S::S({ x });
69+
let mut a: S = S::new({ x });
7070
let mut b: S = S::S_pmutS_rv({ &mut a });
7171
assert!(a.v.is_empty());
72-
let mut c: S = S::S({ 1 });
72+
let mut c: S = S::new({ 1 });
7373
(unsafe { S::operator_assign_pmutS_rv(&mut c, &mut b) });
7474
assert!(b.v.is_empty());
7575
return (unsafe { sum_0(&c) });

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,20 @@ pub fn main() {
6868
std::process::exit(main_0());
6969
}
7070
fn main_0() -> i32 {
71-
let s: Value<S> = Rc::new(RefCell::new(S::S({ 1 })));
71+
let s: Value<S> = Rc::new(RefCell::new(S::new({ 1 })));
7272
assert!((({ SImpl::get(&s.as_pointer(),) }) == 1));
7373
({ SImpl::set(&s.as_pointer(), 4) });
7474
assert!((({ SImpl::get(&s.as_pointer(),) }) == 4));
7575
assert!((({ SImpl::add(&s.as_pointer(), 2,) }) == 6));
76-
let derived: Value<Derived> = Rc::new(RefCell::new(Derived::Derived({ 3 })));
76+
let derived: Value<Derived> = Rc::new(RefCell::new(Derived::new({ 3 })));
7777
let base: Value<PtrDyn<dyn Base>> = Rc::new(RefCell::new(
7878
(derived.as_pointer()).to_dyn::<dyn Base>(|w| w),
7979
));
8080
assert!((({ (*(*base.borrow()).upgrade().deref()).apply(5,) }) == 15));
8181
return 0;
8282
}
8383
impl S {
84-
pub fn S(x: i32) -> Self {
84+
pub fn new(x: i32) -> Self {
8585
let x: Value<i32> = Rc::new(RefCell::new(x));
8686
let __this: Value<S> = Rc::new(RefCell::new(Self {
8787
v: Rc::new(RefCell::new((*x.borrow()))),
@@ -91,7 +91,7 @@ impl S {
9191
}
9292
}
9393
impl Derived {
94-
pub fn Derived(factor: i32) -> Self {
94+
pub fn new(factor: i32) -> Self {
9595
let factor: Value<i32> = Rc::new(RefCell::new(factor));
9696
let __this: Value<Derived> = Rc::new(RefCell::new(Self {
9797
factor: Rc::new(RefCell::new((*factor.borrow()))),

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,24 @@ pub fn main() {
3232
}
3333
}
3434
unsafe fn main_0() -> i32 {
35-
let mut s: S = S::S({ 1 });
35+
let mut s: S = S::new({ 1 });
3636
assert!(((unsafe { S::get(&s,) }) == (1)));
3737
(unsafe { S::set(&mut s, 4) });
3838
assert!(((unsafe { S::get(&s,) }) == (4)));
3939
assert!(((unsafe { S::add(&mut s, 2,) }) == (6)));
40-
let mut derived: Derived = Derived::Derived({ 3 });
40+
let mut derived: Derived = Derived::new({ 3 });
4141
let mut base: *mut dyn Base = (&mut derived as *mut Derived);
4242
assert!(((unsafe { (*base).apply(5,) }) == (15)));
4343
return 0;
4444
}
4545
impl S {
46-
pub unsafe fn S(mut x: i32) -> Self {
46+
pub unsafe fn new(mut x: i32) -> Self {
4747
let mut this = Self { v: x };
4848
this
4949
}
5050
}
5151
impl Derived {
52-
pub unsafe fn Derived(mut factor: i32) -> Self {
52+
pub unsafe fn new(mut factor: i32) -> Self {
5353
let mut this = Self { factor: factor };
5454
this
5555
}

‎tests/ub/out/refcount/ctor_ref_member.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub struct S {
1111
pub r: Ptr<i32>,
1212
}
1313
impl S {
14-
pub fn S(x: Ptr<i32>) -> Self {
14+
pub fn new(x: Ptr<i32>) -> Self {
1515
let __this: Value<S> = Rc::new(RefCell::new(Self { r: (x).clone() }));
1616
let this: Ptr<S> = __this.as_pointer();
1717
Rc::try_unwrap(__this).ok().unwrap().into_inner()
@@ -23,7 +23,7 @@ pub fn main() {
2323
std::process::exit(main_0());
2424
}
2525
fn main_0() -> i32 {
26-
let s: Value<S> = Rc::new(RefCell::new(S::S({
26+
let s: Value<S> = Rc::new(RefCell::new(S::new({
2727
let __tmp_0: Value<i32> = Rc::new(RefCell::new(5));
2828
__tmp_0.as_pointer()
2929
})));

‎tests/ub/out/unsafe/ctor_ref_member.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub struct S {
1212
pub r: *const i32,
1313
}
1414
impl S {
15-
pub unsafe fn S(x: *const i32) -> Self {
15+
pub unsafe fn new(x: *const i32) -> Self {
1616
let mut this = Self { r: x };
1717
this
1818
}
@@ -24,7 +24,7 @@ pub fn main() {
2424
}
2525
}
2626
unsafe fn main_0() -> i32 {
27-
let mut s: S = S::S({
27+
let mut s: S = S::new({
2828
let mut __tmp_0: i32 = 5;
2929
&mut __tmp_0
3030
});

‎tests/unit/out/refcount/10_struct.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub struct Partial {
7171
pub p: Value<Ptr<i32>>,
7272
}
7373
impl Partial {
74-
pub fn Partial1(q: Ptr<i32>) -> Self {
74+
pub fn new_1(q: Ptr<i32>) -> Self {
7575
let q: Value<Ptr<i32>> = Rc::new(RefCell::new(q));
7676
let __this: Value<Partial> = Rc::new(RefCell::new(Self {
7777
p: Rc::new(RefCell::new((*q.borrow()).clone())),
@@ -145,7 +145,7 @@ fn main_0() -> i32 {
145145
adj: Rc::new(RefCell::new(Ptr::<Ptr<GraphNode>>::null())),
146146
}));
147147
let arr: Value<Box<[i32]>> = Rc::new(RefCell::new(Box::new([3, 1, 4])));
148-
let it: Value<Partial> = Rc::new(RefCell::new(Partial::Partial1({
148+
let it: Value<Partial> = Rc::new(RefCell::new(Partial::new_1({
149149
(arr.as_pointer() as Ptr<i32>)
150150
})));
151151
if {

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub struct S {
1414
pub v: Value<i32>,
1515
}
1616
impl S {
17-
pub fn S(init: i32) -> Self {
17+
pub fn new(init: i32) -> Self {
1818
let init: Value<i32> = Rc::new(RefCell::new(init));
1919
let __this: Value<S> = Rc::new(RefCell::new(Self {
2020
v: Rc::new(RefCell::new((*init.borrow()))),
@@ -53,7 +53,7 @@ pub struct Point {
5353
pub y: Value<i32>,
5454
}
5555
impl Point {
56-
pub fn Point1(x: i32, y: i32) -> Self {
56+
pub fn new_1(x: i32, y: i32) -> Self {
5757
let x: Value<i32> = Rc::new(RefCell::new(x));
5858
let y: Value<i32> = Rc::new(RefCell::new(y));
5959
let __this: Value<Point> = Rc::new(RefCell::new(Self {
@@ -63,17 +63,17 @@ impl Point {
6363
let this: Ptr<Point> = __this.as_pointer();
6464
Rc::try_unwrap(__this).ok().unwrap().into_inner()
6565
}
66-
pub fn Point2(v: i32) -> Self {
66+
pub fn new_2(v: i32) -> Self {
6767
let v: Value<i32> = Rc::new(RefCell::new(v));
68-
let __this: Value<Point> = Rc::new(RefCell::new(Point::Point1({ (*v.borrow()) }, {
68+
let __this: Value<Point> = Rc::new(RefCell::new(Point::new_1({ (*v.borrow()) }, {
6969
((*v.borrow()) + 1)
7070
})));
7171
let this: Ptr<Point> = __this.as_pointer();
7272
(*(*this.upgrade().deref()).y.borrow_mut()) *= 10;
7373
Rc::try_unwrap(__this).ok().unwrap().into_inner()
7474
}
75-
pub fn Point3() -> Self {
76-
let __this: Value<Point> = Rc::new(RefCell::new(Point::Point2({ 4 })));
75+
pub fn new_3() -> Self {
76+
let __this: Value<Point> = Rc::new(RefCell::new(Point::new_2({ 4 })));
7777
let this: Ptr<Point> = __this.as_pointer();
7878
(*(*this.upgrade().deref()).x.borrow_mut()) += 100;
7979
Rc::try_unwrap(__this).ok().unwrap().into_inner()
@@ -91,7 +91,7 @@ impl Clone for Point {
9191
}
9292
impl Default for Point {
9393
fn default() -> Self {
94-
{ Point::Point3() }
94+
{ Point::new_3() }
9595
}
9696
}
9797
impl ByteRepr for Point {
@@ -115,16 +115,16 @@ pub fn main() {
115115
}
116116
fn main_0() -> i32 {
117117
{
118-
let s: Value<S> = Rc::new(RefCell::new(S::S({ 3 })));
118+
let s: Value<S> = Rc::new(RefCell::new(S::new({ 3 })));
119119
let _dtor_s = ScopedDestructor::new(&s, |__p| __p.destructor());
120120
assert!(((*(*s.borrow()).v.borrow()) == 4));
121121
assert!((total_0.with(|rc| *rc.borrow()) == 8));
122122
}
123123
assert!((total_0.with(|rc| *rc.borrow()) == 18));
124-
let p: Value<Point> = Rc::new(RefCell::new(Point::Point3()));
124+
let p: Value<Point> = Rc::new(RefCell::new(Point::new_3()));
125125
assert!(((*(*p.borrow()).x.borrow()) == 104));
126126
assert!(((*(*p.borrow()).y.borrow()) == 50));
127-
let q: Value<Point> = Rc::new(RefCell::new(Point::Point2({ 7 })));
127+
let q: Value<Point> = Rc::new(RefCell::new(Point::new_2({ 7 })));
128128
assert!(((*(*q.borrow()).x.borrow()) == 7));
129129
assert!(((*(*q.borrow()).y.borrow()) == 80));
130130
return 0;

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct Partial {
1515
pub keep: Value<i32>,
1616
}
1717
impl Partial {
18-
pub fn Partial(v: i32, keep: i32) -> Self {
18+
pub fn new(v: i32, keep: i32) -> Self {
1919
let v: Value<i32> = Rc::new(RefCell::new(v));
2020
let keep: Value<i32> = Rc::new(RefCell::new(keep));
2121
let __this: Value<Partial> = Rc::new(RefCell::new(Self {
@@ -63,7 +63,7 @@ pub struct NonConstAssign {
6363
pub mark: Value<i32>,
6464
}
6565
impl NonConstAssign {
66-
pub fn NonConstAssign() -> Self {
66+
pub fn new() -> Self {
6767
let __this: Value<NonConstAssign> = Rc::new(RefCell::new(Self {
6868
mark: Rc::new(RefCell::new(0)),
6969
}));
@@ -82,7 +82,7 @@ impl Clone for NonConstAssign {
8282
}
8383
impl Default for NonConstAssign {
8484
fn default() -> Self {
85-
{ NonConstAssign::NonConstAssign() }
85+
{ NonConstAssign::new() }
8686
}
8787
}
8888
impl ByteRepr for NonConstAssign {
@@ -103,7 +103,7 @@ pub struct RefQualified {
103103
pub mark: Value<i32>,
104104
}
105105
impl RefQualified {
106-
pub fn RefQualified() -> Self {
106+
pub fn new() -> Self {
107107
let __this: Value<RefQualified> = Rc::new(RefCell::new(Self {
108108
mark: Rc::new(RefCell::new(0)),
109109
}));
@@ -122,7 +122,7 @@ impl Clone for RefQualified {
122122
}
123123
impl Default for RefQualified {
124124
fn default() -> Self {
125-
{ RefQualified::RefQualified() }
125+
{ RefQualified::new() }
126126
}
127127
}
128128
impl ByteRepr for RefQualified {
@@ -193,9 +193,9 @@ pub fn main() {
193193
std::process::exit(main_0());
194194
}
195195
fn main_0() -> i32 {
196-
let a: Value<Partial> = Rc::new(RefCell::new(Partial::Partial({ 1 }, { 100 })));
197-
let b: Value<Partial> = Rc::new(RefCell::new(Partial::Partial({ 2 }, { 200 })));
198-
let c: Value<Partial> = Rc::new(RefCell::new(Partial::Partial({ 3 }, { 300 })));
196+
let a: Value<Partial> = Rc::new(RefCell::new(Partial::new({ 1 }, { 100 })));
197+
let b: Value<Partial> = Rc::new(RefCell::new(Partial::new({ 2 }, { 200 })));
198+
let c: Value<Partial> = Rc::new(RefCell::new(Partial::new({ 3 }, { 300 })));
199199
({ PartialImpl::operator_assign(&a.as_pointer(), b.as_pointer()) });
200200
assert!(((*(*a.borrow()).v.borrow()) == 2) && ((*(*a.borrow()).keep.borrow()) == 100));
201201
assert!((assigns_0.with(|rc| *rc.borrow()) == 1));
@@ -213,7 +213,7 @@ fn main_0() -> i32 {
213213
});
214214
assert!((assigns_0.with(|rc| *rc.borrow()) == 3));
215215
({
216-
let _o: Value<Partial> = Rc::new(RefCell::new(Partial::Partial({ 9 }, { 900 })));
216+
let _o: Value<Partial> = Rc::new(RefCell::new(Partial::new({ 9 }, { 900 })));
217217
PartialImpl::operator_assign(&a.as_pointer(), _o.as_pointer())
218218
});
219219
assert!(((*(*a.borrow()).v.borrow()) == 9) && ((*(*a.borrow()).keep.borrow()) == 100));
@@ -232,10 +232,10 @@ fn main_0() -> i32 {
232232
assert!(((*(*a.borrow()).v.borrow()) == 2));
233233
assert!((assigns_0.with(|rc| *rc.borrow()) == 6));
234234
let h: Value<Holder> = Rc::new(RefCell::new(Holder {
235-
p: Rc::new(RefCell::new(Partial::Partial({ 4 }, { 40 }))),
235+
p: Rc::new(RefCell::new(Partial::new({ 4 }, { 40 }))),
236236
arr: Rc::new(RefCell::new(Box::new([
237-
Partial::Partial({ 5 }, { 50 }),
238-
Partial::Partial({ 6 }, { 60 }),
237+
Partial::new({ 5 }, { 50 }),
238+
Partial::new({ 6 }, { 60 }),
239239
]))),
240240
}));
241241
({ PartialImpl::operator_assign(&(*h.borrow()).p.as_pointer(), b.as_pointer()) });
@@ -254,18 +254,18 @@ fn main_0() -> i32 {
254254
&& ((*(*(*h.borrow()).arr.borrow())[(1) as usize].keep.borrow()) == 60)
255255
);
256256
assert!((assigns_0.with(|rc| *rc.borrow()) == 8));
257-
let n: Value<NonConstAssign> = Rc::new(RefCell::new(NonConstAssign::NonConstAssign()));
258-
let n1: Value<NonConstAssign> = Rc::new(RefCell::new(NonConstAssign::NonConstAssign()));
259-
let n2: Value<NonConstAssign> = Rc::new(RefCell::new(NonConstAssign::NonConstAssign()));
260-
let cn: Value<NonConstAssign> = Rc::new(RefCell::new(NonConstAssign::NonConstAssign()));
257+
let n: Value<NonConstAssign> = Rc::new(RefCell::new(NonConstAssign::new()));
258+
let n1: Value<NonConstAssign> = Rc::new(RefCell::new(NonConstAssign::new()));
259+
let n2: Value<NonConstAssign> = Rc::new(RefCell::new(NonConstAssign::new()));
260+
let cn: Value<NonConstAssign> = Rc::new(RefCell::new(NonConstAssign::new()));
261261
({ NonConstAssignImpl::operator_assign_pmutNonConstAssign(&n1.as_pointer(), n.as_pointer()) });
262262
({
263263
NonConstAssignImpl::operator_assign_pconstNonConstAssign(&n2.as_pointer(), cn.as_pointer())
264264
});
265265
assert!(((*(*n1.borrow()).mark.borrow()) == 1));
266266
assert!(((*(*n2.borrow()).mark.borrow()) == 10));
267-
let r: Value<RefQualified> = Rc::new(RefCell::new(RefQualified::RefQualified()));
268-
let r1: Value<RefQualified> = Rc::new(RefCell::new(RefQualified::RefQualified()));
267+
let r: Value<RefQualified> = Rc::new(RefCell::new(RefQualified::new()));
268+
let r1: Value<RefQualified> = Rc::new(RefCell::new(RefQualified::new()));
269269
({ RefQualifiedImpl::operator_assign(&r1.as_pointer(), r.as_pointer()) });
270270
assert!(((*(*r1.borrow()).mark.borrow()) == 1));
271271
return 0;

0 commit comments

Comments
 (0)