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
6 changes: 1 addition & 5 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5444,11 +5444,7 @@ std::string Converter::ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg,

// The rule takes the argument by value from an rvalue reference parameter,
// e.g., push_back(T &&), and so C++ moves it.
auto param_type = GetParamType(expr, ph_ctx.arg_idx);
bool moved = ph_ctx.access == TranslationRule::Access::kTake ||
(ph_ctx.access == TranslationRule::Access::kMove && param_type &&
(*param_type)->isRValueReferenceType());
if (moved && !clang::isa<clang::MaterializeTemporaryExpr>(arg)) {
if (MovesRuleArg(expr, ph_ctx.arg_idx, arg, ph_ctx.access)) {
auto *ref = clang::dyn_cast<clang::DeclRefExpr>(arg->IgnoreParenImpCasts());
auto *var = ref ? clang::dyn_cast<clang::VarDecl>(ref->getDecl()) : nullptr;
if (auto *record = arg->getType()->getAsCXXRecordDecl();
Expand Down
9 changes: 9 additions & 0 deletions cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,15 @@ bool IsConvertibleMoveConstructor(const clang::CXXConstructorDecl *ctor) {
return ctor->isUserProvided() || NeedsDefaultedMoveConstructor(parent);
}

bool MovesRuleArg(clang::Expr *expr, unsigned arg_idx, clang::Expr *arg,
TranslationRule::Access access) {
auto param_type = GetParamType(expr, arg_idx);
bool moved = access == TranslationRule::Access::kTake ||
(access == TranslationRule::Access::kMove && param_type &&
(*param_type)->isRValueReferenceType());
return moved && !clang::isa<clang::MaterializeTemporaryExpr>(arg);
}

static bool
IsImplicitConvertingConstructor(const clang::CXXConstructorDecl *ctor) {
return !ctor->isCopyOrMoveConstructor() && !ctor->isImplicit() &&
Expand Down
4 changes: 4 additions & 0 deletions cpp2rust/converter/converter_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <unordered_set>
#include <vector>

#include "converter/translation_rule.h"
#include "logging.h"

namespace clang {
Expand Down Expand Up @@ -110,6 +111,9 @@ bool IsDefaultedMoveConstructor(const clang::CXXConstructorDecl *ctor);

bool IsConvertibleMoveConstructor(const clang::CXXConstructorDecl *ctor);

bool MovesRuleArg(clang::Expr *expr, unsigned arg_idx, clang::Expr *arg,
TranslationRule::Access access);

// Whether decl has a move constructor converted to a move_from method.
bool HasMoveFromConstructor(const clang::RecordDecl *decl);

Expand Down
15 changes: 14 additions & 1 deletion cpp2rust/converter/models/boxed_vars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,19 @@ class AddressTakenVisitor
TraverseStmt(init);
continue;
}
auto *record = arg->getType()->getAsRecordDecl();
if (TranslationRule::AnyFragment(
rule->body, [&](const TranslationRule::BodyFragment &fragment) {
auto *ph = std::get_if<TranslationRule::PlaceholderFragment>(
&fragment);
return ph && ph->n == i &&
((ph->access == TranslationRule::Access::kMove &&
MovesFromArg(expr, i, all_args[i])) ||
(record && HasMoveFromConstructor(record) &&
MovesRuleArg(expr, i, all_args[i], ph->access)));
})) {
continue;
}
if (!rule->takesAddress(ctx_, i, arg) ||
rule->takesTemporaryAddress(ctx_, expr, i, arg)) {
AddValueUse(arg);
Expand All @@ -329,7 +342,7 @@ class AddressTakenVisitor
// An element of an array or vector, or a field of a struct, is accessed
// without making a pointer to it.
void AddValueUse(const clang::Expr *expr, bool write = false) {
expr = IgnoreNoOpCasts(expr->IgnoreParens());
expr = IgnoreNoOpCasts(IgnoreTransparentStdCall(expr)->IgnoreParens());
// The value of a conditional lvalue is the value of either operand; it is
// written through a pointer to the operand.
if (auto *cond = clang::dyn_cast<clang::ConditionalOperator>(expr)) {
Expand Down
6 changes: 3 additions & 3 deletions tests/std/utilities/pairs/out/refcount/pairs.pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ pub fn test_1() {
})
})
);
let values: Value<Vec<i32>> = Rc::new(RefCell::new(vec![7; 2_usize]));
let mut values: Vec<i32> = vec![7; 2_usize];
let copied: Value<(Value<Vec<i32>>, Value<i32>)> = Rc::new(RefCell::new((
Rc::new(RefCell::new(({ (*values.borrow()).clone() }))),
Rc::new(RefCell::new((values).clone())),
Rc::new(RefCell::new(3)),
)));
assert!(({ ((*values.borrow()).len() == 2_usize) }));
assert!((values.len() == 2_usize));
assert!(
({
((*(*copied.borrow()).0.borrow()).len() == 2_usize)
Expand Down
72 changes: 72 additions & 0 deletions tests/std/utilities/utility/forward.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// ADDITIONAL_COMPILE_FLAGS: -std=c++23
#include <cassert>
#include <utility>
#include <vector>

// N4950 [forward]/9: std::move(t) returns
// static_cast<remove_reference_t<T>&&>(t), so moving a scalar reads its value.
namespace p9_move_scalar {
void test() {
int source = 1;
int target = std::move(source);
assert(target == 1);
assert(source == 1);
}
} // namespace p9_move_scalar

// N4950 [forward]/9: moving a trivially copyable object copies its value.
namespace p9_move_trivially_copyable {
struct Point {
int x;
int y;
};

void test() {
Point source{1, 2};
Point target = std::move(source);
assert(target.x == 1 && target.y == 2);
assert(source.x == 1 && source.y == 2);

Point assigned{0, 0};
Point other{3, 4};
assigned = std::move(other);
assert(assigned.x == 3 && assigned.y == 4);
assert(other.x == 3 && other.y == 4);

std::vector<Point> points;
Point element{5, 6};
points.push_back(std::move(element));
assert(points[0].x == 5 && points[0].y == 6);
assert(element.x == 5 && element.y == 6);
}
} // namespace p9_move_trivially_copyable

// N4950 [forward]/10: std::move(a) binds to the constructor A(A&&), which
// moves the value from a.
namespace p10_move_constructor {
struct A {
int value;
A(int value) : value(value) {}
A(A &&other) : value(other.value) { other.value = 0; }
};

void test() {
A source(1);
A target = std::move(source);
assert(target.value == 1);
assert(source.value == 0);

std::vector<A> values;
A element(2);
values.push_back(std::move(element));
assert(values[0].value == 2);
assert(element.value == 0);
}
} // namespace p10_move_constructor

int main() {
p9_move_scalar::test();
p9_move_trivially_copyable::test();
p10_move_constructor::test();
return 0;
}
92 changes: 92 additions & 0 deletions tests/std/utilities/utility/out/refcount/forward.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
extern crate libcc2rs;
use libcc2rs::*;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn test_0() {
let mut source: i32 = 1;
let mut target: i32 = source;
assert!((target == 1));
assert!((source == 1));
}
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
#[byte_size(8)]
pub struct p9_move_trivially_copyable_Point {
#[offset(0)]
pub x: i32,
#[offset(4)]
pub y: i32,
}
pub fn test_1() {
let mut source: p9_move_trivially_copyable_Point =
p9_move_trivially_copyable_Point { x: 1, y: 2 };
let mut target: p9_move_trivially_copyable_Point = (source).clone();
assert!((target.x == 1) && (target.y == 2));
assert!((source.x == 1) && (source.y == 2));
let mut assigned: p9_move_trivially_copyable_Point =
p9_move_trivially_copyable_Point { x: 0, y: 0 };
let mut other: p9_move_trivially_copyable_Point =
p9_move_trivially_copyable_Point { x: 3, y: 4 };
assigned = (other).clone();
assert!((assigned.x == 3) && (assigned.y == 4));
assert!((other.x == 3) && (other.y == 4));
let mut points: Vec<p9_move_trivially_copyable_Point> = Vec::new();
let mut element: p9_move_trivially_copyable_Point =
p9_move_trivially_copyable_Point { x: 5, y: 6 };
{
let __a1 = (element).clone();
points.push(__a1)
};
assert!((points[0_usize].x == 5) && (points[0_usize].y == 6));
assert!((element.x == 5) && (element.y == 6));
}
#[derive(Record, ByteRepr, FnPtrArg, MoveCtor, CppFrom, Default)]
#[cpp_from(i32, new, value)]
#[byte_size(4)]
pub struct p10_move_constructor_A {
#[offset(0)]
pub value: i32,
}
impl p10_move_constructor_A {
pub fn new(mut value: i32) -> Self {
Self { value: value }
}
pub fn move_from(other: Ptr<p10_move_constructor_A>) -> Self {
let __this: p10_move_constructor_A = Self {
value: other.with(|__s| __s.value),
};
field!(other, value).write(0);
__this
}
}
pub fn test_2() {
let source: Value<p10_move_constructor_A> =
Rc::new(RefCell::new(p10_move_constructor_A::new({ 1 })));
let mut target: p10_move_constructor_A =
p10_move_constructor_A::move_from({ source.as_pointer() });
assert!((target.value == 1));
assert!(({ ({ (*source.borrow()).value } == 0) }));
let mut values: Vec<p10_move_constructor_A> = Vec::new();
let element: Value<p10_move_constructor_A> =
Rc::new(RefCell::new(p10_move_constructor_A::new({ 2 })));
{
let __a1 = p10_move_constructor_A::move_from({ element.as_pointer() });
values.push(__a1)
};
assert!((values[0_usize].value == 2));
assert!(({ ({ (*element.borrow()).value } == 0) }));
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
}
fn main_0() -> i32 {
({ test_0() });
({ test_1() });
({ test_2() });
return 0;
}
pub fn __cpp2rust_init_globals() {}
89 changes: 89 additions & 0 deletions tests/std/utilities/utility/out/unsafe/forward.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
extern crate libc;
use libc::*;
extern crate libcc2rs;
use libcc2rs::*;
use std::collections::BTreeMap;
use std::io::{Read, Seek, Write};
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
use std::rc::Rc;
pub unsafe fn test_0() {
let mut source: i32 = 1;
let mut target: i32 = source;
assert!(((target) == (1)));
assert!(((source) == (1)));
}
#[repr(C)]
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
pub struct p9_move_trivially_copyable_Point {
pub x: i32,
pub y: i32,
}
pub unsafe fn test_1() {
let mut source: p9_move_trivially_copyable_Point =
p9_move_trivially_copyable_Point { x: 1, y: 2 };
let mut target: p9_move_trivially_copyable_Point = source;
assert!(((target.x) == (1)) && ((target.y) == (2)));
assert!(((source.x) == (1)) && ((source.y) == (2)));
let mut assigned: p9_move_trivially_copyable_Point =
p9_move_trivially_copyable_Point { x: 0, y: 0 };
let mut other: p9_move_trivially_copyable_Point =
p9_move_trivially_copyable_Point { x: 3, y: 4 };
assigned = other;
assert!(((assigned.x) == (3)) && ((assigned.y) == (4)));
assert!(((other.x) == (3)) && ((other.y) == (4)));
let mut points: Vec<p9_move_trivially_copyable_Point> = Vec::new();
let mut element: p9_move_trivially_copyable_Point =
p9_move_trivially_copyable_Point { x: 5, y: 6 };
{
let __a1 = element;
points.push(__a1)
};
assert!(((points[(0_usize)].x) == (5)) && ((points[(0_usize)].y) == (6)));
assert!(((element.x) == (5)) && ((element.y) == (6)));
}
#[repr(C)]
#[derive(FnPtrArg, MoveCtorUnsafe, CppFromUnsafe, Default)]
#[cpp_from(i32, new, value)]
pub struct p10_move_constructor_A {
pub value: i32,
}
impl p10_move_constructor_A {
pub unsafe fn new(mut value: i32) -> Self {
let mut this = Self { value: value };
this
}
pub unsafe fn move_from(other: *mut p10_move_constructor_A) -> Self {
let mut this = Self {
value: (*other).value,
};
(*other).value = 0;
this
}
}
pub unsafe fn test_2() {
let mut source: p10_move_constructor_A = p10_move_constructor_A::new({ 1 });
let mut target: p10_move_constructor_A = p10_move_constructor_A::move_from({ &mut source });
assert!(((target.value) == (1)));
assert!(((source.value) == (0)));
let mut values: Vec<p10_move_constructor_A> = Vec::new();
let mut element: p10_move_constructor_A = p10_move_constructor_A::new({ 2 });
{
let __a1 = p10_move_constructor_A::move_from({ &mut element });
values.push(__a1)
};
assert!(((values[(0_usize)].value) == (2)));
assert!(((element.value) == (0)));
}
pub fn main() {
unsafe {
__cpp2rust_init_globals();
std::process::exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
(unsafe { test_0() });
(unsafe { test_1() });
(unsafe { test_2() });
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
6 changes: 3 additions & 3 deletions tests/unit/out/refcount/11_move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn change_0(n: Ptr<Option<Value<i32>>>) {
let m: Value<Option<Value<i32>>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new(20)))));
n.write((*m.borrow_mut()).take());
assert!(({ ((*m.borrow()).as_pointer()).is_null() }));
let mut m: Option<Value<i32>> = Some(Rc::new(RefCell::new(20)));
n.write(m.take());
assert!((m.as_pointer()).is_null());
}
pub fn main() {
__cpp2rust_init_globals();
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/out/refcount/alloc_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn All_0(arr: Ptr<Option<Value<Box<[i32]>>>>, mut N: i32, mut element: i32) {
let all: Value<Option<Value<Box<[i32]>>>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new(
let mut all: Option<Value<Box<[i32]>>> = Some(Rc::new(RefCell::new(
(0..(N as usize)).map(|_| 0_i32).collect::<Box<[_]>>(),
)))));
)));
{
let mut i: i32 = 0;
'loop_: while (i < N) {
(*all.borrow()).decay().offset((i as usize)).write(element);
all.decay().offset((i as usize)).write(element);
i.prefix_inc();
}
}
arr.write((*all.borrow_mut()).take());
arr.write(all.take());
}
pub fn Consume_1(mut arr: Option<Value<Box<[i32]>>>, mut N: i32) -> i32 {
let mut sum: i32 = 0;
Expand Down
5 changes: 2 additions & 3 deletions tests/unit/out/refcount/copy_ctor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,8 @@ pub struct Box_int_ {
pub val: i32,
}
impl Box_int_ {
pub fn new(v: i32) -> Self {
let v: Value<i32> = Rc::new(RefCell::new(v));
Self { val: (*v.borrow()) }
pub fn new(mut v: i32) -> Self {
Self { val: v }
}
pub fn copy_from(o: Ptr<Box_int_>) -> Self {
Self {
Expand Down
Loading
Loading