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
3 changes: 2 additions & 1 deletion cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5476,7 +5476,8 @@ std::string Converter::ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg,
}

if (ph_ctx.access == TranslationRule::Access::kTake) {
if (clang::isa<clang::MaterializeTemporaryExpr>(arg)) {
if (clang::isa<clang::MaterializeTemporaryExpr>(arg) ||
arg->getType().isTriviallyCopyableType(ctx_)) {
return ConvertFreshRValue(arg);
}
return ConvertTake(arg);
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/out/refcount/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,23 @@ fn main_0() -> i32 {
&& ({ (*v10.borrow())[1_usize] } == 7)
})
);
let mut v12: Vec<i32> = Vec::new();
let moved: Value<i32> = Rc::new(RefCell::new(5));
{
let __a1 = ({ (*moved.borrow()) });
v12.push(__a1)
};
assert!(({ (v12[0_usize] == 5) && ((*moved.borrow()) == 5) }));
let mut v13: Vec<Ptr<i32>> = Vec::new();
let mut moved_ptr: Ptr<i32> = (moved.as_pointer());
{
let __a1 = (moved_ptr).clone();
v13.push(__a1)
};
assert!(
(v13[0_usize] == (moved.as_pointer()))
&& ({ (moved_ptr).clone() } == { (moved.as_pointer()) })
);
return 0;
}
pub trait FooImpl {
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/out/unsafe/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,22 @@ unsafe fn main_0() -> i32 {
assert!(
(((foo.a.len()) == (1_usize)) && ((foo.a[(0_usize)]) == (4))) && ((v10[(1_usize)]) == (7))
);
let mut v12: Vec<i32> = Vec::new();
let mut moved: i32 = 5;
{
let __a1 = moved;
v12.push(__a1)
};
assert!(((v12[(0_usize)]) == (5)) && ((moved) == (5)));
let mut v13: Vec<*mut i32> = Vec::new();
let mut moved_ptr: *mut i32 = (&mut moved as *mut i32);
{
let __a1 = moved_ptr;
v13.push(__a1)
};
assert!(
((v13[(0_usize)]) == (&mut moved as *mut i32)) && ((moved_ptr) == (&mut moved as *mut i32))
);
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
10 changes: 10 additions & 0 deletions tests/unit/vector.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <assert.h>
#include <utility>
#include <vector>

struct S {
Expand Down Expand Up @@ -177,5 +178,14 @@ int main() {
assert(foo.a.size() == 2 && foo.a[1] == 7 && foo.b[0] == 5);
exchange(foo.a, v10);
assert(foo.a.size() == 1 && foo.a[0] == 4 && v10[1] == 7);

std::vector<int> v12;
int moved = 5;
v12.push_back(std::move(moved));
assert(v12[0] == 5 && moved == 5);
std::vector<int *> v13;
int *moved_ptr = &moved;
v13.push_back(std::move(moved_ptr));
assert(v13[0] == &moved && moved_ptr == &moved);
return 0;
}
Loading