Skip to content

Commit 06c3c82

Browse files
authored
Don't call mem::take for move of trivially copyable types (#635)
1 parent 48cb4ff commit 06c3c82

4 files changed

Lines changed: 45 additions & 1 deletion

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5476,7 +5476,8 @@ std::string Converter::ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg,
54765476
}
54775477

54785478
if (ph_ctx.access == TranslationRule::Access::kTake) {
5479-
if (clang::isa<clang::MaterializeTemporaryExpr>(arg)) {
5479+
if (clang::isa<clang::MaterializeTemporaryExpr>(arg) ||
5480+
arg->getType().isTriviallyCopyableType(ctx_)) {
54805481
return ConvertFreshRValue(arg);
54815482
}
54825483
return ConvertTake(arg);

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,23 @@ fn main_0() -> i32 {
466466
&& ({ (*v10.borrow())[1_usize] } == 7)
467467
})
468468
);
469+
let mut v12: Vec<i32> = Vec::new();
470+
let moved: Value<i32> = Rc::new(RefCell::new(5));
471+
{
472+
let __a1 = ({ (*moved.borrow()) });
473+
v12.push(__a1)
474+
};
475+
assert!(({ (v12[0_usize] == 5) && ((*moved.borrow()) == 5) }));
476+
let mut v13: Vec<Ptr<i32>> = Vec::new();
477+
let mut moved_ptr: Ptr<i32> = (moved.as_pointer());
478+
{
479+
let __a1 = (moved_ptr).clone();
480+
v13.push(__a1)
481+
};
482+
assert!(
483+
(v13[0_usize] == (moved.as_pointer()))
484+
&& ({ (moved_ptr).clone() } == { (moved.as_pointer()) })
485+
);
469486
return 0;
470487
}
471488
pub trait FooImpl {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,22 @@ unsafe fn main_0() -> i32 {
362362
assert!(
363363
(((foo.a.len()) == (1_usize)) && ((foo.a[(0_usize)]) == (4))) && ((v10[(1_usize)]) == (7))
364364
);
365+
let mut v12: Vec<i32> = Vec::new();
366+
let mut moved: i32 = 5;
367+
{
368+
let __a1 = moved;
369+
v12.push(__a1)
370+
};
371+
assert!(((v12[(0_usize)]) == (5)) && ((moved) == (5)));
372+
let mut v13: Vec<*mut i32> = Vec::new();
373+
let mut moved_ptr: *mut i32 = (&mut moved as *mut i32);
374+
{
375+
let __a1 = moved_ptr;
376+
v13.push(__a1)
377+
};
378+
assert!(
379+
((v13[(0_usize)]) == (&mut moved as *mut i32)) && ((moved_ptr) == (&mut moved as *mut i32))
380+
);
365381
return 0;
366382
}
367383
pub unsafe fn __cpp2rust_init_globals() {}

‎tests/unit/vector.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <assert.h>
2+
#include <utility>
23
#include <vector>
34

45
struct S {
@@ -177,5 +178,14 @@ int main() {
177178
assert(foo.a.size() == 2 && foo.a[1] == 7 && foo.b[0] == 5);
178179
exchange(foo.a, v10);
179180
assert(foo.a.size() == 1 && foo.a[0] == 4 && v10[1] == 7);
181+
182+
std::vector<int> v12;
183+
int moved = 5;
184+
v12.push_back(std::move(moved));
185+
assert(v12[0] == 5 && moved == 5);
186+
std::vector<int *> v13;
187+
int *moved_ptr = &moved;
188+
v13.push_back(std::move(moved_ptr));
189+
assert(v13[0] == &moved && moved_ptr == &moved);
180190
return 0;
181191
}

0 commit comments

Comments
 (0)