Skip to content

Commit e2b679d

Browse files
committed
fix translation of string copy constructor
1 parent e421df0 commit e2b679d

4 files changed

Lines changed: 16 additions & 4 deletions

File tree

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,14 +2035,15 @@ bool ConverterRefCount::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) {
20352035

20362036
if (auto str = GetMappedAsString(expr, expr->getArgs(), expr->getNumArgs());
20372037
!str.empty()) {
2038+
if (!IsPassThroughRule(expr)) {
2039+
MarkFresh();
2040+
}
20382041
if (isAddrOf()) {
2039-
EmitFresh(std::format("Rc::new(RefCell::new({})){}", std::move(str),
2042+
EmitFresh(std::format("Rc::new(RefCell::new({})){}",
2043+
MakeFresh(std::move(str)),
20402044
AsPointerMethod(expr->getType())));
20412045
} else {
20422046
StrCat(str);
2043-
if (!IsPassThroughRule(expr)) {
2044-
MarkFresh();
2045-
}
20462047
}
20472048
return false;
20482049
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,10 @@ fn main_0() -> i32 {
427427
target.decay(),
428428
(*ref_calls.borrow())
429429
);
430+
println!(
431+
"copy: {}",
432+
Rc::new(RefCell::new((*target.borrow()).clone())).decay()
433+
);
430434
let targets: Value<Box<[Vec<i8>]>> = Rc::new(RefCell::new(Box::new([
431435
{
432436
let mut __bytes = Ptr::<i8>::from_string_literal(b"cd").to_c_bytes();

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,12 @@ unsafe fn main_0() -> i32 {
490490
(ref_calls),
491491
)
492492
});
493+
(unsafe {
494+
libc::printf(
495+
c"copy: %s\n".as_ptr() as *const libc::c_char,
496+
(target.clone().as_ptr()),
497+
)
498+
});
493499
let mut targets: [Vec<libc::c_char>; 2] = [
494500
std::slice::from_raw_parts(
495501
c"cd".as_ptr(),

‎tests/unit/string.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ int main() {
168168
counted_ref(target, ref_calls).resize(1);
169169
counted_ref(target, ref_calls).at(0) = 'A';
170170
printf("reference receiver: %s %d\n", target.c_str(), ref_calls);
171+
printf("copy: %s\n", std::string(target).c_str());
171172
std::string targets[2] = {"cd", "ef"};
172173
int target_index = 0;
173174
targets[target_index++].resize(1);

0 commit comments

Comments
 (0)