Skip to content

Commit cd937fa

Browse files
authored
Skip elidable constructors (#525)
1 parent 4cfd0cb commit cd937fa

11 files changed

Lines changed: 134 additions & 53 deletions

‎cpp2rust/converter/converter.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3804,6 +3804,11 @@ void Converter::ConvertCXXConstructExprArgs(clang::CXXConstructExpr *expr) {
38043804
bool Converter::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) {
38053805
PushSuppressIteratorClone push(*this, expr);
38063806

3807+
if (expr->isElidable()) {
3808+
Convert(expr->getArg(0));
3809+
return false;
3810+
}
3811+
38073812
if (auto str = GetMappedAsString(expr, expr->getArgs(), expr->getNumArgs());
38083813
!str.empty()) {
38093814
StrCat(str);

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2372,6 +2372,11 @@ std::string ConverterRefCount::ConvertStream(clang::Expr *expr) {
23722372
}
23732373

23742374
bool ConverterRefCount::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) {
2375+
if (expr->isElidable()) {
2376+
Convert(expr->getArg(0));
2377+
return false;
2378+
}
2379+
23752380
// An array is boxed unless it initializes an unboxed variable.
23762381
bool boxed_array = expr->getType()->isArrayType() &&
23772382
getConversionKind() != ConversionKind::Unboxed;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// ADDITIONAL_COMPILE_FLAGS: -std=c++11
2+
#include <algorithm>
3+
#include <cassert>
4+
#include <vector>
5+
6+
int main() {
7+
std::vector<int> v;
8+
v.push_back(4);
9+
v.push_back(7);
10+
std::vector<int>::iterator it = std::find(v.begin(), v.end(), 7);
11+
assert(it != v.end());
12+
assert(*it == 7);
13+
return 0;
14+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn main() {
10+
__cpp2rust_init_globals();
11+
std::process::exit(main_0());
12+
}
13+
fn main_0() -> i32 {
14+
let v: Value<Vec<i32>> = Rc::new(RefCell::new(Vec::new()));
15+
{
16+
let __a1 = 4;
17+
(*v.borrow_mut()).push(__a1)
18+
};
19+
{
20+
let __a1 = 7;
21+
(*v.borrow_mut()).push(__a1)
22+
};
23+
let it: Value<Ptr<i32>> = Rc::new(RefCell::new({
24+
let count = ((v.as_pointer() as Ptr<i32>).to_end().get_offset()
25+
- (v.as_pointer() as Ptr<i32>).get_offset()) as usize;
26+
(v.as_pointer() as Ptr<i32>).offset(
27+
(v.as_pointer() as Ptr<i32>)
28+
.clone()
29+
.into_iter()
30+
.take(count)
31+
.position(|value_0| value_0.read() == 7)
32+
.unwrap_or(count) as isize,
33+
)
34+
}));
35+
assert!((*it.borrow()) != (v.as_pointer() as Ptr<i32>).to_end());
36+
assert!((((*it.borrow()).read()) == 7));
37+
return 0;
38+
}
39+
pub fn __cpp2rust_init_globals() {}

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

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,23 +74,20 @@ impl MinHeap {
7474
}
7575
}
7676
pub fn AllocMinHeap_1(mut capacity: i32) -> Option<Value<MinHeap>> {
77-
let mut minHeap: Option<Value<MinHeap>> = Some(Rc::new(RefCell::new({
78-
let __tmp_0: Value<MinHeap> = Rc::new(RefCell::new(MinHeap {
79-
size: 0,
80-
capacity: capacity,
81-
arr: Some(Rc::new(RefCell::new(
82-
(0..(capacity as usize))
83-
.map(|_| <Ptr<MinHeapNode>>::default())
84-
.collect::<Box<[_]>>(),
85-
))),
86-
next: 0,
87-
alloc: Some(Rc::new(RefCell::new(
88-
(0..10000_usize)
89-
.map(|_| <MinHeapNode>::default())
90-
.collect::<Box<[_]>>(),
91-
))),
92-
}));
93-
MinHeap::move_from({ __tmp_0.as_pointer() })
77+
let mut minHeap: Option<Value<MinHeap>> = Some(Rc::new(RefCell::new(MinHeap {
78+
size: 0,
79+
capacity: capacity,
80+
arr: Some(Rc::new(RefCell::new(
81+
(0..(capacity as usize))
82+
.map(|_| <Ptr<MinHeapNode>>::default())
83+
.collect::<Box<[_]>>(),
84+
))),
85+
next: 0,
86+
alloc: Some(Rc::new(RefCell::new(
87+
(0..10000_usize)
88+
.map(|_| <MinHeapNode>::default())
89+
.collect::<Box<[_]>>(),
90+
))),
9491
})));
9592
return minHeap.take();
9693
}

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,8 @@ pub fn main() {
182182
fn main_0() -> i32 {
183183
let x: Value<Option<Value<i32>>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new(0)))));
184184
let safe_ptr: Value<Option<Value<SafePointer>>> =
185-
Rc::new(RefCell::new(Some(Rc::new(RefCell::new({
186-
let __tmp_0: Value<SafePointer> = Rc::new(RefCell::new(SafePointer {
187-
ptr: (*x.borrow_mut()).take(),
188-
}));
189-
SafePointer::move_from({ __tmp_0.as_pointer() })
185+
Rc::new(RefCell::new(Some(Rc::new(RefCell::new(SafePointer {
186+
ptr: (*x.borrow_mut()).take(),
190187
})))));
191188
({ DoStuffWithSafePointer_0(safe_ptr.as_pointer()) });
192189
assert!((({ Consume_1((*safe_ptr.borrow_mut()).take(),) }) == 60));

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,8 @@ pub fn main() {
3333
std::process::exit(main_0());
3434
}
3535
fn main_0() -> i32 {
36-
let mut o: Option<Value<Outer>> = Some(Rc::new(RefCell::new({
37-
let __tmp_0: Value<Outer> = Rc::new(RefCell::new(Outer {
38-
inner: Some(Rc::new(RefCell::new(Inner { x: 10, y: 20 }))),
39-
}));
40-
Outer::move_from({ __tmp_0.as_pointer() })
36+
let mut o: Option<Value<Outer>> = Some(Rc::new(RefCell::new(Outer {
37+
inner: Some(Rc::new(RefCell::new(Inner { x: 10, y: 20 }))),
4138
})));
4239
(*{ (*o.as_ref().unwrap().borrow()).inner.clone() }
4340
.as_ref()
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub fn main() {
10+
unsafe {
11+
__cpp2rust_init_globals();
12+
std::process::exit(main_0() as i32);
13+
}
14+
}
15+
unsafe fn main_0() -> i32 {
16+
let mut v: Vec<i32> = Vec::new();
17+
{
18+
let __a1 = 4;
19+
v.push(__a1)
20+
};
21+
{
22+
let __a1 = 7;
23+
v.push(__a1)
24+
};
25+
let mut it: *mut i32 = {
26+
let mut it = v.as_mut_ptr();
27+
while it != v.as_mut_ptr().add(v.len()) && *it != 7 {
28+
it = it.add(1);
29+
}
30+
it
31+
};
32+
assert!(it != v.as_mut_ptr().add(v.len()));
33+
assert!(((*it) == (7)));
34+
return 0;
35+
}
36+
pub unsafe fn __cpp2rust_init_globals() {}

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

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -145,23 +145,20 @@ impl MinHeap {
145145
}
146146
}
147147
pub unsafe fn AllocMinHeap_1(mut capacity: i32) -> Option<Box<MinHeap>> {
148-
let mut minHeap: Option<Box<MinHeap>> = Some(Box::new({
149-
let mut __tmp_0: MinHeap = MinHeap {
150-
size: 0,
151-
capacity: capacity,
152-
arr: Some(
153-
(0..(capacity as usize))
154-
.map(|_| <*mut MinHeapNode>::default())
155-
.collect::<Box<[_]>>(),
156-
),
157-
next: 0,
158-
alloc: Some(
159-
(0..10000_usize)
160-
.map(|_| <MinHeapNode>::default())
161-
.collect::<Box<[_]>>(),
162-
),
163-
};
164-
MinHeap::move_from({ &mut __tmp_0 })
148+
let mut minHeap: Option<Box<MinHeap>> = Some(Box::new(MinHeap {
149+
size: 0,
150+
capacity: capacity,
151+
arr: Some(
152+
(0..(capacity as usize))
153+
.map(|_| <*mut MinHeapNode>::default())
154+
.collect::<Box<[_]>>(),
155+
),
156+
next: 0,
157+
alloc: Some(
158+
(0..10000_usize)
159+
.map(|_| <MinHeapNode>::default())
160+
.collect::<Box<[_]>>(),
161+
),
165162
}));
166163
return minHeap.take();
167164
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,7 @@ pub fn main() {
160160
}
161161
unsafe fn main_0() -> i32 {
162162
let mut x: Option<Box<i32>> = Some(Box::new(0));
163-
let mut safe_ptr: Option<Box<SafePointer>> = Some(Box::new({
164-
let mut __tmp_0: SafePointer = SafePointer { ptr: x.take() };
165-
SafePointer::move_from({ &mut __tmp_0 })
166-
}));
163+
let mut safe_ptr: Option<Box<SafePointer>> = Some(Box::new(SafePointer { ptr: x.take() }));
167164
(unsafe { DoStuffWithSafePointer_0(&mut safe_ptr) });
168165
assert!(((unsafe { Consume_1(safe_ptr.take(),) }) == (60)));
169166
return 0;

0 commit comments

Comments
 (0)