Skip to content

Commit 5f6ad1d

Browse files
committed
Avoid clone on fresh function pointer
1 parent 0faa092 commit 5f6ad1d

2 files changed

Lines changed: 5 additions & 4 deletions

File tree

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,7 @@ void ConverterRefCount::ConvertFunctionToFunctionPointer(
12631263
ConvertFunctionPointerType(
12641264
fn_decl->getType()->getAs<clang::FunctionProtoType>()),
12651265
Mapper::MapFunctionName(fn_decl)));
1266+
computed_expr_type_ = ComputedExprType::FreshPointer;
12661267
}
12671268

12681269
void ConverterRefCount::ConvertEqualsNullPtr(clang::Expr *expr) {
@@ -1322,7 +1323,7 @@ bool ConverterRefCount::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
13221323
if (expr->getTypeAsWritten()->isVoidType()) {
13231324
PushExprKind push(*this, ExprKind::Void);
13241325
Convert(expr->getSubExpr());
1325-
if (!TypeIsCopyable(expr->getSubExpr()->getType())) {
1326+
if (!TypeIsCopyable(expr->getSubExpr()->getType()) && !isFresh()) {
13261327
StrCat(".clone()");
13271328
}
13281329
return false;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ fn main_0() -> i32 {
104104
}));
105105
assert!(((*err.borrow()) == 7));
106106
assert!(((*chosen.borrow()) == 123));
107-
bump_and_return_4.clone();
107+
bump_and_return_4;
108108
assert!(((*side_effect_counter_3.with(Value::clone).borrow()) == 2));
109-
(FnPtr::<fn() -> i32>::new(bump_and_return_4)).clone();
109+
(FnPtr::<fn() -> i32>::new(bump_and_return_4));
110110
assert!(((*side_effect_counter_3.with(Value::clone).borrow()) == 2));
111-
((FnPtr::<fn() -> i32>::new(bump_and_return_4)).cast::<fn() -> i32>(None)).clone();
111+
((FnPtr::<fn() -> i32>::new(bump_and_return_4)).cast::<fn() -> i32>(None));
112112
assert!(((*side_effect_counter_3.with(Value::clone).borrow()) == 2));
113113
let storage: Value<i32> = Rc::new(RefCell::new(11));
114114
let p: Value<Ptr<i32>> = Rc::new(RefCell::new((storage.as_pointer())));

0 commit comments

Comments
 (0)