Skip to content

Commit c919de7

Browse files
authored
Rollup merge of rust-lang#134797 - spastorino:ergonomic-ref-counting-1, r=nikomatsakis
Ergonomic ref counting This is an experimental first version of ergonomic ref counting. This first version implements most of the RFC but doesn't implement any of the optimizations. This was left for following iterations. RFC: rust-lang/rfcs#3680 Tracking issue: rust-lang#132290 Project goal: rust-lang/rust-project-goals#107 r? ```@nikomatsakis```
2 parents e5c1d1c + d52ac1b commit c919de7

17 files changed

+44
-5
lines changed

clippy_lints/src/escape.rs

+2
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ impl<'tcx> Delegate<'tcx> for EscapeDelegate<'_, 'tcx> {
150150
}
151151
}
152152

153+
fn use_cloned(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {}
154+
153155
fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, _: HirId, _: ty::BorrowKind) {
154156
if cmt.place.projections.is_empty() {
155157
if let PlaceBase::Local(lid) = cmt.place.base {

clippy_lints/src/loops/mut_range_bound.rs

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ struct MutatePairDelegate<'a, 'tcx> {
7979
impl<'tcx> Delegate<'tcx> for MutatePairDelegate<'_, 'tcx> {
8080
fn consume(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {}
8181

82+
fn use_cloned(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {}
83+
8284
fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, diag_expr_id: HirId, bk: ty::BorrowKind) {
8385
if bk == ty::BorrowKind::Mutable {
8486
if let PlaceBase::Local(id) = cmt.place.base {

clippy_lints/src/loops/never_loop.rs

+1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ fn never_loop_expr<'tcx>(
160160
| ExprKind::UnsafeBinderCast(_, e, _) => never_loop_expr(cx, e, local_labels, main_loop_id),
161161
ExprKind::Let(let_expr) => never_loop_expr(cx, let_expr.init, local_labels, main_loop_id),
162162
ExprKind::Array(es) | ExprKind::Tup(es) => never_loop_expr_all(cx, es.iter(), local_labels, main_loop_id),
163+
ExprKind::Use(expr, _) => never_loop_expr(cx, expr, local_labels, main_loop_id),
163164
ExprKind::MethodCall(_, receiver, es, _) => {
164165
never_loop_expr_all(cx, once(receiver).chain(es.iter()), local_labels, main_loop_id)
165166
},

clippy_lints/src/matches/manual_utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ where
9999
});
100100
if let ExprKind::Path(QPath::Resolved(None, Path { res: Res::Local(l), .. })) = e.kind {
101101
match captures.get(l) {
102-
Some(CaptureKind::Value | CaptureKind::Ref(Mutability::Mut)) => return None,
102+
Some(CaptureKind::Value | CaptureKind::Use | CaptureKind::Ref(Mutability::Mut)) => return None,
103103
Some(CaptureKind::Ref(Mutability::Not)) if binding_ref_mutability == Mutability::Mut => {
104104
return None;
105105
},

clippy_lints/src/methods/iter_overeager_cloned.rs

+2
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ impl<'tcx> Delegate<'tcx> for MoveDelegate {
147147
}
148148
}
149149

150+
fn use_cloned(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {}
151+
150152
fn borrow(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId, _: BorrowKind) {}
151153

152154
fn mutate(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {}

clippy_lints/src/needless_pass_by_ref_mut.rs

+2
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ impl<'tcx> euv::Delegate<'tcx> for MutablyUsedVariablesCtxt<'tcx> {
396396
}
397397
}
398398

399+
fn use_cloned(&mut self, _: &euv::PlaceWithHirId<'tcx>, _: HirId) {}
400+
399401
#[allow(clippy::if_same_then_else)]
400402
fn borrow(&mut self, cmt: &euv::PlaceWithHirId<'tcx>, id: HirId, borrow: ty::BorrowKind) {
401403
self.prev_bind = None;

clippy_lints/src/needless_pass_by_value.rs

+2
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@ impl<'tcx> euv::Delegate<'tcx> for MovedVariablesCtxt {
326326
self.move_common(cmt);
327327
}
328328

329+
fn use_cloned(&mut self, _: &euv::PlaceWithHirId<'tcx>, _: HirId) {}
330+
329331
fn borrow(&mut self, _: &euv::PlaceWithHirId<'tcx>, _: HirId, _: ty::BorrowKind) {}
330332

331333
fn mutate(&mut self, _: &euv::PlaceWithHirId<'tcx>, _: HirId) {}

clippy_lints/src/operators/assign_op_pattern.rs

+2
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ fn imm_borrows_in_expr(cx: &LateContext<'_>, e: &hir::Expr<'_>) -> HirIdSet {
112112
}
113113

114114
fn consume(&mut self, _: &PlaceWithHirId<'_>, _: HirId) {}
115+
fn use_cloned(&mut self, _: &PlaceWithHirId<'_>, _: HirId) {}
115116
fn mutate(&mut self, _: &PlaceWithHirId<'_>, _: HirId) {}
116117
fn fake_read(&mut self, _: &PlaceWithHirId<'_>, _: FakeReadCause, _: HirId) {}
117118
fn copy(&mut self, _: &PlaceWithHirId<'_>, _: HirId) {}
@@ -137,6 +138,7 @@ fn mut_borrows_in_expr(cx: &LateContext<'_>, e: &hir::Expr<'_>) -> HirIdSet {
137138
}
138139

139140
fn consume(&mut self, _: &PlaceWithHirId<'_>, _: HirId) {}
141+
fn use_cloned(&mut self, _: &PlaceWithHirId<'_>, _: HirId) {}
140142
fn mutate(&mut self, _: &PlaceWithHirId<'_>, _: HirId) {}
141143
fn fake_read(&mut self, _: &PlaceWithHirId<'_>, _: FakeReadCause, _: HirId) {}
142144
fn copy(&mut self, _: &PlaceWithHirId<'_>, _: HirId) {}

clippy_lints/src/option_if_let_else.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ fn try_get_option_occurrence<'tcx>(
177177
.then_some(())
178178
.and_then(|()| none_captures.get(local_id))
179179
}) {
180-
Some(CaptureKind::Value | CaptureKind::Ref(Mutability::Mut)) => return None,
180+
Some(CaptureKind::Value | CaptureKind::Use | CaptureKind::Ref(Mutability::Mut)) => return None,
181181
Some(CaptureKind::Ref(Mutability::Not)) if as_mut => return None,
182182
Some(CaptureKind::Ref(Mutability::Not)) | None => (),
183183
}

clippy_lints/src/unwrap.rs

+2
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ impl<'tcx> Delegate<'tcx> for MutationVisitor<'tcx> {
230230

231231
fn consume(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {}
232232

233+
fn use_cloned(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {}
234+
233235
fn fake_read(&mut self, _: &PlaceWithHirId<'tcx>, _: FakeReadCause, _: HirId) {}
234236
}
235237

clippy_lints/src/utils/author.rs

+6
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,11 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
426426
kind!("Tup({elements})");
427427
self.slice(elements, |e| self.expr(e));
428428
},
429+
ExprKind::Use(expr, _) => {
430+
bind!(self, expr);
431+
kind!("Use({expr})");
432+
self.expr(expr);
433+
},
429434
ExprKind::Binary(op, left, right) => {
430435
bind!(self, op, left, right);
431436
kind!("Binary({op}, {left}, {right})");
@@ -488,6 +493,7 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
488493
}) => {
489494
let capture_clause = match capture_clause {
490495
CaptureBy::Value { .. } => "Value { .. }",
496+
CaptureBy::Use { .. } => "Use { .. }",
491497
CaptureBy::Ref => "Ref",
492498
};
493499

clippy_utils/src/eager_or_lazy.rs

+1
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ fn expr_eagerness<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> EagernessS
291291
ExprKind::ConstBlock(_)
292292
| ExprKind::Array(_)
293293
| ExprKind::Tup(_)
294+
| ExprKind::Use(..)
294295
| ExprKind::Lit(_)
295296
| ExprKind::Cast(..)
296297
| ExprKind::Type(..)

clippy_utils/src/hir_utils.rs

+5
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ impl HirEqInterExpr<'_, '_, '_> {
393393
&& over(lf, rf, |l, r| self.eq_expr_field(l, r))
394394
},
395395
(&ExprKind::Tup(l_tup), &ExprKind::Tup(r_tup)) => self.eq_exprs(l_tup, r_tup),
396+
(&ExprKind::Use(l_expr, _), &ExprKind::Use(r_expr, _)) => self.eq_expr(l_expr, r_expr),
396397
(&ExprKind::Type(le, lt), &ExprKind::Type(re, rt)) => self.eq_expr(le, re) && self.eq_ty(lt, rt),
397398
(&ExprKind::Unary(l_op, le), &ExprKind::Unary(r_op, re)) => l_op == r_op && self.eq_expr(le, re),
398399
(&ExprKind::Yield(le, _), &ExprKind::Yield(re, _)) => return self.eq_expr(le, re),
@@ -425,6 +426,7 @@ impl HirEqInterExpr<'_, '_, '_> {
425426
| &ExprKind::Ret(..)
426427
| &ExprKind::Struct(..)
427428
| &ExprKind::Tup(..)
429+
| &ExprKind::Use(..)
428430
| &ExprKind::Type(..)
429431
| &ExprKind::Unary(..)
430432
| &ExprKind::Yield(..)
@@ -1053,6 +1055,9 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
10531055
ExprKind::Tup(tup) => {
10541056
self.hash_exprs(tup);
10551057
},
1058+
ExprKind::Use(expr, _) => {
1059+
self.hash_expr(expr);
1060+
},
10561061
ExprKind::Unary(lop, le) => {
10571062
std::mem::discriminant(&lop).hash(&mut self.s);
10581063
self.hash_expr(le);

clippy_utils/src/lib.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,7 @@ pub fn can_move_expr_to_closure_no_visit<'tcx>(
11291129
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11301130
pub enum CaptureKind {
11311131
Value,
1132+
Use,
11321133
Ref(Mutability),
11331134
}
11341135
impl CaptureKind {
@@ -1141,6 +1142,7 @@ impl std::ops::BitOr for CaptureKind {
11411142
fn bitor(self, rhs: Self) -> Self::Output {
11421143
match (self, rhs) {
11431144
(CaptureKind::Value, _) | (_, CaptureKind::Value) => CaptureKind::Value,
1145+
(CaptureKind::Use, _) | (_, CaptureKind::Use) => CaptureKind::Use,
11441146
(CaptureKind::Ref(Mutability::Mut), CaptureKind::Ref(_))
11451147
| (CaptureKind::Ref(_), CaptureKind::Ref(Mutability::Mut)) => CaptureKind::Ref(Mutability::Mut),
11461148
(CaptureKind::Ref(Mutability::Not), CaptureKind::Ref(Mutability::Not)) => CaptureKind::Ref(Mutability::Not),
@@ -1220,7 +1222,7 @@ pub fn capture_local_usage(cx: &LateContext<'_>, e: &Expr<'_>) -> CaptureKind {
12201222
},
12211223
ExprKind::Let(let_expr) => {
12221224
let mutability = match pat_capture_kind(cx, let_expr.pat) {
1223-
CaptureKind::Value => Mutability::Not,
1225+
CaptureKind::Value | CaptureKind::Use => Mutability::Not,
12241226
CaptureKind::Ref(m) => m,
12251227
};
12261228
return CaptureKind::Ref(mutability);
@@ -1229,7 +1231,7 @@ pub fn capture_local_usage(cx: &LateContext<'_>, e: &Expr<'_>) -> CaptureKind {
12291231
let mut mutability = Mutability::Not;
12301232
for capture in arms.iter().map(|arm| pat_capture_kind(cx, arm.pat)) {
12311233
match capture {
1232-
CaptureKind::Value => break,
1234+
CaptureKind::Value | CaptureKind::Use => break,
12331235
CaptureKind::Ref(Mutability::Mut) => mutability = Mutability::Mut,
12341236
CaptureKind::Ref(Mutability::Not) => (),
12351237
}
@@ -1239,7 +1241,7 @@ pub fn capture_local_usage(cx: &LateContext<'_>, e: &Expr<'_>) -> CaptureKind {
12391241
_ => break,
12401242
},
12411243
Node::LetStmt(l) => match pat_capture_kind(cx, l.pat) {
1242-
CaptureKind::Value => break,
1244+
CaptureKind::Value | CaptureKind::Use => break,
12431245
capture @ CaptureKind::Ref(_) => return capture,
12441246
},
12451247
_ => break,
@@ -1294,6 +1296,7 @@ pub fn can_move_expr_to_closure<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'
12941296
if !self.locals.contains(&local_id) {
12951297
let capture = match capture.info.capture_kind {
12961298
UpvarCapture::ByValue => CaptureKind::Value,
1299+
UpvarCapture::ByUse => CaptureKind::Use,
12971300
UpvarCapture::ByRef(kind) => match kind {
12981301
BorrowKind::Immutable => CaptureKind::Ref(Mutability::Not),
12991302
BorrowKind::UniqueImmutable | BorrowKind::Mutable => {

clippy_utils/src/sugg.rs

+4
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ impl<'a> Sugg<'a> {
147147
| ExprKind::Become(..)
148148
| ExprKind::Struct(..)
149149
| ExprKind::Tup(..)
150+
| ExprKind::Use(..)
150151
| ExprKind::Err(_)
151152
| ExprKind::UnsafeBinderCast(..) => Sugg::NonParen(get_snippet(expr.span)),
152153
ExprKind::DropTemps(inner) => Self::hir_from_snippet(inner, get_snippet),
@@ -217,6 +218,7 @@ impl<'a> Sugg<'a> {
217218
| ast::ExprKind::Try(..)
218219
| ast::ExprKind::TryBlock(..)
219220
| ast::ExprKind::Tup(..)
221+
| ast::ExprKind::Use(..)
220222
| ast::ExprKind::Array(..)
221223
| ast::ExprKind::While(..)
222224
| ast::ExprKind::Await(..)
@@ -835,6 +837,8 @@ impl<'tcx> DerefDelegate<'_, 'tcx> {
835837
impl<'tcx> Delegate<'tcx> for DerefDelegate<'_, 'tcx> {
836838
fn consume(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {}
837839

840+
fn use_cloned(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {}
841+
838842
fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, _: HirId, _: ty::BorrowKind) {
839843
if let PlaceBase::Local(id) = cmt.place.base {
840844
let map = self.cx.tcx.hir();

clippy_utils/src/usage.rs

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ impl MutVarsDelegate {
6666
impl<'tcx> Delegate<'tcx> for MutVarsDelegate {
6767
fn consume(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {}
6868

69+
fn use_cloned(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {}
70+
6971
fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, _: HirId, bk: ty::BorrowKind) {
7072
if bk == ty::BorrowKind::Mutable {
7173
self.update(cmt);

clippy_utils/src/visitors.rs

+3
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,9 @@ pub fn for_each_unconsumed_temporary<'tcx, B>(
648648
helper(typeck, true, arg, f)?;
649649
}
650650
},
651+
ExprKind::Use(expr, _) => {
652+
helper(typeck, true, expr, f)?;
653+
},
651654
ExprKind::Index(borrowed, consumed, _)
652655
| ExprKind::Assign(borrowed, consumed, _)
653656
| ExprKind::AssignOp(_, borrowed, consumed) => {

0 commit comments

Comments
 (0)