Skip to content

Commit a4f46de

Browse files
committed
Drop useless clones for receiver
1 parent ec164bc commit a4f46de

18 files changed

Lines changed: 37 additions & 36 deletions

‎cpp2rust/converter/converter.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5130,6 +5130,9 @@ std::string Converter::ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg,
51305130

51315131
if (ph_ctx.needs_pointer_receiver()) {
51325132
auto param_type = Mapper::GetParamType(*this, expr, ph_ctx.arg_idx);
5133+
bool borrowed = ph_ctx.access == TranslationRule::Access::kBorrow ||
5134+
ph_ctx.access == TranslationRule::Access::kBorrowMut;
5135+
PushExprKind push(*this, borrowed ? ExprKind::Object : ExprKind::XValue);
51335136
return ConvertPointerReceiver(arg, param_type);
51345137
}
51355138

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ std::string ConverterRefCount::AsPointerMethod(clang::QualType type) const {
401401

402402
std::string ConverterRefCount::ConvertPointerReceiver(clang::Expr *arg,
403403
std::string_view) {
404-
return ConvertFreshObject(arg);
404+
return isObject() ? ConvertObject(arg) : ConvertFreshObject(arg);
405405
}
406406

407407
std::string ConverterRefCount::ConvertFreshObject(clang::Expr *expr) {

‎rule-preprocessor/src/syntactic.rs‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ impl<'a> FragmentCtx<'a> {
216216
});
217217
return;
218218
}
219-
let mut access = self.builder.classify_access(token);
219+
let mut access = self
220+
.builder
221+
.classify_access(token, param.is_refcount_pointer);
220222
if param.is_mut_ref && self.text_buf.ends_with('*') {
221223
self.text_buf.pop();
222224
}
@@ -392,7 +394,7 @@ impl<'a> FnIrBuilder<'a> {
392394
generics
393395
}
394396

395-
fn classify_rw_from_name_ref(&self, name_ref: &ast::NameRef) -> Access {
397+
fn classify_rw_from_name_ref(&self, name_ref: &ast::NameRef, is_ptr: bool) -> Access {
396398
name_ref
397399
.syntax()
398400
.ancestors()
@@ -425,7 +427,8 @@ impl<'a> FnIrBuilder<'a> {
425427
{
426428
return None;
427429
}
428-
Some(Access::Unknown)
430+
// Every Ptr method takes &self.
431+
Some(if is_ptr { Access::Borrow } else { Access::Unknown })
429432
},
430433
ast::ReturnExpr(_) => {
431434
panic!("return statements are not supported in rule bodies");
@@ -461,12 +464,12 @@ impl<'a> FnIrBuilder<'a> {
461464
.is_some_and(|ty| matches!(ty, ast::Type::RefType(r) if r.mut_token().is_some()))
462465
}
463466

464-
fn classify_access(&self, token: &ra_ap_syntax::SyntaxToken) -> Access {
467+
fn classify_access(&self, token: &ra_ap_syntax::SyntaxToken, is_ptr: bool) -> Access {
465468
let Some(name_ref) = token.parent().and_then(ast::NameRef::cast) else {
466469
// Inside a macro invocation. Phase 2 will resolve this.
467470
return Access::Unknown;
468471
};
469-
self.classify_rw_from_name_ref(&name_ref)
472+
self.classify_rw_from_name_ref(&name_ref, is_ptr)
470473
}
471474

472475
fn trim_whitespaces(fragments: &mut Vec<BodyFragment>) {

‎tests/unit/out/refcount/11_move.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
99
pub fn change_0(n: Ptr<Option<Value<i32>>>) {
1010
let m: Value<Option<Value<i32>>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new(20)))));
11-
(n).clone().write((*m.borrow_mut()).take());
11+
n.write((*m.borrow_mut()).take());
1212
assert!(((*m.borrow()).as_pointer()).is_null());
1313
}
1414
pub fn main() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub fn All_0(arr: Ptr<Option<Value<Box<[i32]>>>>, mut N: i32, mut element: i32)
1919
.write(element);
2020
i.prefix_inc();
2121
}
22-
(arr).clone().write((*all.borrow_mut()).take());
22+
arr.write((*all.borrow_mut()).take());
2323
}
2424
pub fn Consume_1(mut arr: Option<Value<Box<[i32]>>>, mut N: i32) -> i32 {
2525
let mut sum: i32 = 0;

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ fn main_0() -> i32 {
8080
'loop_: for row in IterRange::new(m.as_pointer().decay().begin(), m.as_pointer().decay().end())
8181
{
8282
let row: Ptr<Vec<i32>> = row.iter_deref();
83-
'loop_: for col in
84-
IterRange::new((row).clone().decay().begin(), (row).clone().decay().end())
85-
{
83+
'loop_: for col in IterRange::new(row.decay().begin(), row.decay().end()) {
8684
let col: Ptr<i32> = col.iter_deref();
8785
square += { (col.read()) };
8886
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn main_0() -> i32 {
4949
assert!(((index!(p, 1_usize).read()) == 30));
5050
assert!((b == 40));
5151
assert!(((vec_elem!((hp), v, 1_usize).read()) == 60));
52-
({ write_through_0(((p).clone().decay().offset(0_usize as isize))) });
52+
({ write_through_0((p.decay().offset(0_usize as isize))) });
5353
assert!(((index!(p, 0_usize).read()) == 42));
5454
return 0;
5555
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,7 @@ fn main_0() -> i32 {
423423
!= m.as_pointer()
424424
.with(|__m: &Map<i16, (Value<i16>, Value<u32>)>| __m.end())
425425
);
426-
(r).clone()
427-
.with_mut(|__m: &mut Map<i16, (Value<i16>, Value<u32>)>| {
428-
__m.erase(&(*it4.borrow()).clone())
429-
});
426+
r.with_mut(|__m: &mut Map<i16, (Value<i16>, Value<u32>)>| __m.erase(&(*it4.borrow()).clone()));
430427
assert!(((*r.upgrade().deref()).len() == 3_usize));
431428
assert!(
432429
m.as_pointer()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ pub fn nested_emplace_move_5(mut bw: Ptr<Writer>) {
9999
}
100100
pub fn self_ref_push_6(mut comps: Ptr<Vec<Chunk>>) {
101101
{
102-
let a0_clone = (*(comps).clone().decay().upgrade().deref()).clone();
102+
let a0_clone = (*comps.decay().upgrade().deref()).clone();
103103
comps.with_mut(|__v: &mut Vec<Chunk>| __v.push(a0_clone))
104104
};
105105
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
99
pub fn change_0(p: Ptr<Option<Value<i32>>>) {
1010
let q: Value<Option<Value<i32>>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new(7)))));
11-
(p).clone().write((*q.borrow_mut()).take());
11+
p.write((*q.borrow_mut()).take());
1212
}
1313
pub fn main() {
1414
__cpp2rust_init_globals();

0 commit comments

Comments
 (0)