Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4070,7 +4070,11 @@ bool Converter::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) {
return false;
}

if (auto str = GetMappedAsString(expr, expr->getArgs(), expr->getNumArgs());
auto *ctor = expr->getConstructor();
bool trivial_move = ctor->isMoveConstructor() && ctor->isTrivial();
if (auto str = trivial_move ? std::string()
: GetMappedAsString(expr, expr->getArgs(),
expr->getNumArgs());
!str.empty()) {
StrCat(str);
if (!IsPassThroughRule(expr)) {
Expand All @@ -4079,7 +4083,6 @@ bool Converter::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) {
return false;
}

auto *ctor = expr->getConstructor();
if (IsLambdaCopyOrMoveConstructor(ctor)) {
Convert(expr->getArg(0));
EmitFresh(std::format(".{}()", GetCopyOrMoveName(ctor)));
Expand Down
10 changes: 10 additions & 0 deletions tests/std/class/class.mem/class.ctor/class.copy.ctor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <cassert>
#include <initializer_list>
#include <utility>
#include <vector>

// N4950 [class.copy.ctor]/11: a copy/move constructor that is not
// user-provided is trivial.
Expand All @@ -24,11 +25,20 @@ void test() {
// N4950 [class.copy.ctor]/14: the implicitly-defined move constructor
// performs a memberwise move, so a trivial move leaves the source unchanged.
namespace p14_trivial_move_keeps_source {
int read_by_value(std::vector<int>::iterator it) { return *it; }

void test() {
std::initializer_list<int> source = {1, 2, 3};
std::initializer_list<int> moved = std::move(source);
assert(moved.size() == 3);
assert(source.size() == 3);
std::vector<int> values{7, 8};
std::vector<int>::iterator it = values.begin();
assert(read_by_value(std::move(it)) == 7);
assert(*it == 7);
std::vector<int>::iterator moved_it = std::move(it);
assert(*moved_it == 7);
assert(*it == 7);
}
} // namespace p14_trivial_move_keeps_source

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,30 @@ pub fn test_1() {
});
assert!((result.value == 7));
}
pub fn test_2() {
pub fn read_by_value_2(it: RandomAccessIterator<i32>) -> i32 {
let it: Value<RandomAccessIterator<i32>> = Rc::new(RefCell::new(it));
return ((*it.borrow()).iter_deref().read());
}
pub fn test_3() {
let source: Value<Vec<i32>> = Rc::new(RefCell::new(Vec::from([1, 2, 3])));
let moved: Value<Vec<i32>> = Rc::new(RefCell::new((*source.borrow_mut()).clone()));
assert!(({ ((*moved.borrow()).len() == 3_usize) }));
assert!(({ ((*source.borrow()).len() == 3_usize) }));
let values: Value<Vec<i32>> = Rc::new(RefCell::new(Vec::from([7, 8])));
let it: Value<RandomAccessIterator<i32>> = Rc::new(RefCell::new(values.decay().begin()));
assert!(({ (({ read_by_value_2((*it.borrow()).clone(),) }) == 7) }));
assert!(({ (((*it.borrow()).iter_deref().read()) == 7) }));
let moved_it: Value<RandomAccessIterator<i32>> = Rc::new(RefCell::new((*it.borrow()).clone()));
assert!(({ (((*moved_it.borrow()).iter_deref().read()) == 7) }));
assert!(({ (((*it.borrow()).iter_deref().read()) == 7) }));
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
}
fn main_0() -> i32 {
({ test_1() });
({ test_2() });
({ test_3() });
return 0;
}
pub fn __cpp2rust_init_globals() {}
14 changes: 12 additions & 2 deletions tests/std/class/class.mem/class.ctor/out/unsafe/class.copy.ctor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,21 @@ pub unsafe fn test_1() {
});
assert!(((result.value) == (7)));
}
pub unsafe fn test_2() {
pub unsafe fn read_by_value_2(mut it: UnsafeRandomAccessIterator<i32>) -> i32 {
return (*it.iter_deref());
}
pub unsafe fn test_3() {
let mut source: Vec<i32> = Vec::from([1, 2, 3]);
let mut moved: Vec<i32> = source.clone();
assert!(((moved.len()) == (3_usize)));
assert!(((source.len()) == (3_usize)));
let mut values: Vec<i32> = Vec::from([7, 8]);
let mut it: UnsafeRandomAccessIterator<i32> = values.begin();
assert!(((unsafe { read_by_value_2(it.clone(),) }) == (7)));
assert!(((*it.iter_deref()) == (7)));
let mut moved_it: UnsafeRandomAccessIterator<i32> = it.clone();
assert!(((*moved_it.iter_deref()) == (7)));
assert!(((*it.iter_deref()) == (7)));
}
pub fn main() {
unsafe {
Expand All @@ -51,7 +61,7 @@ pub fn main() {
}
unsafe fn main_0() -> i32 {
(unsafe { test_1() });
(unsafe { test_2() });
(unsafe { test_3() });
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
2 changes: 1 addition & 1 deletion tests/unit/out/unsafe/inline_container_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl State {
grid: std::array::from_fn::<_, 2, _>(|__i: usize| {
std::array::from_fn::<_, 3, _>(|__i2: usize| (*_a0).grid[(__i)][(__i2)])
}),
arr: std::array::from_fn(|__i| (*&mut (&mut (*_a0)).arr[__i])),
arr: (*_a0).arr.clone(),
elems: std::mem::take(&mut (*_a0).elems),
rows: std::mem::take(&mut (*_a0).rows),
};
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/unsafe/libc_struct_without_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct UserDefined {
impl UserDefined {
pub unsafe fn move_from(_a0: *mut UserDefined) -> Self {
let mut this = Self {
a: std::array::from_fn(|__i| (*&mut (&mut (*_a0)).a[__i])),
a: (*_a0).a.clone(),
v: std::mem::take(&mut (*_a0).v),
};
this
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/unsafe/pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ unsafe fn main_0() -> i32 {
assert!(((*f) == (3)));
let mut a: (i32, i32) = (1, 2);
let mut pa: *mut i32 = (&mut a.0 as *mut i32);
let mut b: (i32, i32) = (cpp_move_unsafe!(&mut a.0), cpp_move_unsafe!(&mut a.1));
let mut b: (i32, i32) = a.clone();
(*pa) = 9;
assert!(((b.0) == (1)));
assert!(((a.0) == (9)));
Expand Down
Loading