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: 6 additions & 1 deletion cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1840,11 +1840,16 @@ bool Converter::ConvertSpecialCall(clang::CallExpr *expr) {
return false;
}

bool Converter::ExprMapsValueToPointer(const clang::CallExpr *expr) const {
return Mapper::Contains(ctx_, expr) && !expr->getType()->isPointerType() &&
RuleRegistry::MapsToPointer(ctx_, expr->getType());
}

void Converter::EmitCallResult(clang::CallExpr *expr, std::string str,
const TempMaterializationCtx *temps) {
bool deref = (GetReturnTypeOfFunction(expr)->isReferenceType() ||
(temps && IsReferenceType(expr))) &&
!isAddrOf() && !isVoid();
!isAddrOf() && !isVoid() && !ExprMapsValueToPointer(expr);
// The dereference is in the block of the temporaries, so they are still
// alive, as is the read of a field of the result.
if (deref && !(temps && Mapper::IsRefExpr(ctx_, expr))) {
Expand Down
2 changes: 2 additions & 0 deletions cpp2rust/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {

virtual bool IsReferenceType(const clang::Expr *expr) const;

bool ExprMapsValueToPointer(const clang::CallExpr *expr) const;

virtual bool RecordDerivesDefault(const clang::RecordDecl *decl);

virtual bool TypeDerivesDefault(clang::QualType qual_type);
Expand Down
2 changes: 1 addition & 1 deletion cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1237,7 +1237,7 @@ void ConverterRefCount::EmitCallResult(clang::CallExpr *expr, std::string str,
};
auto ty = GetReturnTypeOfFunction(expr);
if (auto *ref = ty->getAs<clang::ReferenceType>();
ref && !isAddrOf() && !isVoid()) {
ref && !isAddrOf() && !isVoid() && !ExprMapsValueToPointer(expr)) {
if (isLValue()) {
pending_deref_.set(wrap(std::move(str)), /*fresh=*/true);
return;
Expand Down
15 changes: 9 additions & 6 deletions rules/vector/tgt_refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,19 @@ fn f118<T1>(a0: Ptr<bool>, a1: usize) -> Ptr<bool> {
}

fn f119(a0: Ptr<bool>, a1: bool) -> Ptr<bool> {
a0.write(a1);
a0
let __a0 = a0;
__a0.write(a1);
__a0
}

fn f120(a0: Ptr<bool>, a1: bool) -> Ptr<bool> {
a0.write(a1);
a0
let __a0 = a0;
__a0.write(a1);
__a0
}

fn f121(a0: Ptr<bool>, a1: Ptr<bool>) -> Ptr<bool> {
a0.write(a1.read());
a0
let __a0 = a0;
__a0.write(a1.read());
__a0
}
15 changes: 9 additions & 6 deletions rules/vector/tgt_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,16 +267,19 @@ unsafe fn f118<T1>(a0: &mut Vec<bool>, a1: usize) -> *mut bool {
}

unsafe fn f119(a0: *mut bool, a1: bool) -> *mut bool {
*a0 = a1;
a0
let __a0 = a0;
*__a0 = a1;
__a0
}

unsafe fn f120(a0: *mut bool, a1: bool) -> *mut bool {
*a0 = a1;
a0
let __a0 = a0;
*__a0 = a1;
__a0
}

unsafe fn f121(a0: *mut bool, a1: *mut bool) -> *mut bool {
*a0 = *a1;
a0
let __a0 = a0;
*__a0 = *a1;
__a0
}
26 changes: 13 additions & 13 deletions tests/unit/out/refcount/vector_bool_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,33 @@ fn main_0() -> i32 {
let mut r1: Ptr<bool> = v1.decay().offset(0_usize as isize);
assert!((v1.decay().offset(0_usize as isize).read()) && (r1.read()));
{
r1.write(false);
(r1).clone()
let __a0 = (r1).clone();
__a0.write(false);
__a0
};
assert!(!(v1.decay().offset(0_usize as isize).read()));
let it: Value<RandomAccessIterator<bool>> = Rc::new(RefCell::new(v1.decay().begin()));
let mut r2: Ptr<bool> = (*it.borrow()).iter_deref();
{
r2.write(true);
(r2).clone()
let __a0 = (r2).clone();
__a0.write(true);
__a0
};
assert!(v1.decay().offset(0_usize as isize).read());
{
v1.decay().offset(0_usize as isize).write(false);
v1.decay().offset(0_usize as isize)
let __a0 = v1.decay().offset(0_usize as isize);
__a0.write(false);
__a0
};
assert!(!(v1.decay().offset(0_usize as isize).read()));
{
let v: Value<Vec<bool>> = Rc::new(RefCell::new(Vec::from([false, false, false])));
let mut i: i32 = 0;
let mut b: bool = (*{
v.decay()
.offset((i.postfix_inc() as usize) as isize)
.write(true);
v.decay().offset((i.postfix_inc() as usize) as isize)
let mut b: bool = {
let __a0 = v.decay().offset((i.postfix_inc() as usize) as isize);
__a0.write(true);
__a0
}
.upgrade()
.deref())
.read();
assert!(b);
}
Expand Down
24 changes: 14 additions & 10 deletions tests/unit/out/unsafe/vector_bool_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,33 @@ unsafe fn main_0() -> i32 {
let mut r1: *mut bool = (&mut (v1)[0_usize as usize] as *mut bool);
assert!((*(&mut (v1)[0_usize as usize] as *mut bool)) && (*r1));
{
*r1 = false;
r1
let __a0 = r1;
*__a0 = false;
__a0
};
assert!(!(*(&mut (v1)[0_usize as usize] as *mut bool)));
let mut it: UnsafeRandomAccessIterator<bool> = v1.begin();
let mut r2: *mut bool = it.iter_deref();
{
*r2 = true;
r2
let __a0 = r2;
*__a0 = true;
__a0
};
assert!(*(&mut (v1)[0_usize as usize] as *mut bool));
{
*(&mut (v1)[0_usize as usize] as *mut bool) = false;
(&mut (v1)[0_usize as usize] as *mut bool)
let __a0 = (&mut (v1)[0_usize as usize] as *mut bool);
*__a0 = false;
__a0
};
assert!(!(*(&mut (v1)[0_usize as usize] as *mut bool)));
{
let mut v: Vec<bool> = Vec::from([false, false, false]);
let mut i: i32 = 0;
let mut b: bool = *(*{
*(&mut (v)[(i.postfix_inc() as usize) as usize] as *mut bool) = true;
(&mut (v)[(i.postfix_inc() as usize) as usize] as *mut bool)
});
let mut b: bool = *{
let __a0 = (&mut (v)[(i.postfix_inc() as usize) as usize] as *mut bool);
*__a0 = true;
__a0
};
assert!(b);
}
return 0;
Expand Down
13 changes: 6 additions & 7 deletions tests/unit/vector_bool_reference.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// XFAIL: refcount, unsafe
#include <cassert>
#include <vector>

Expand All @@ -14,11 +13,11 @@ int main() {
assert(v1[0]);
v1[0] = false;
assert(!v1[0]);
{
std::vector<bool> v = {false, false, false };
int i = 0;
bool b = v[i++] = true;
assert(b);
}
{
std::vector<bool> v = {false, false, false};
int i = 0;
bool b = v[i++] = true;
assert(b);
}
return 0;
}
Loading