Skip to content

Commit 1702c00

Browse files
committed
Make captures in ByUse context be always ty::UpvarCapture::ByUse
1 parent 65d65e5 commit 1702c00

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

compiler/rustc_hir_typeck/src/expr_use_visitor.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,18 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
324324
}
325325
}
326326

327+
pub fn consume_clone_or_copy(&self, place_with_id: &PlaceWithHirId<'tcx>, diag_expr_id: HirId) {
328+
debug!("delegate_consume_or_clone(place_with_id={:?})", place_with_id);
329+
330+
if self.cx.type_is_copy_modulo_regions(place_with_id.place.ty()) {
331+
self.delegate.borrow_mut().copy(place_with_id, diag_expr_id);
332+
} else if self.cx.type_is_use_cloned_modulo_regions(place_with_id.place.ty()) {
333+
self.delegate.borrow_mut().use_cloned(place_with_id, diag_expr_id);
334+
} else {
335+
self.delegate.borrow_mut().consume(place_with_id, diag_expr_id);
336+
}
337+
}
338+
327339
fn consume_exprs(&self, exprs: &[hir::Expr<'_>]) -> Result<(), Cx::Error> {
328340
for expr in exprs {
329341
self.consume_expr(expr)?;
@@ -346,15 +358,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
346358
debug!("consume_or_clone_expr(expr={:?})", expr);
347359

348360
let place_with_id = self.cat_expr(expr)?;
349-
350-
if self.cx.type_is_copy_modulo_regions(place_with_id.place.ty()) {
351-
self.delegate.borrow_mut().copy(&place_with_id, place_with_id.hir_id);
352-
} else if self.cx.type_is_use_cloned_modulo_regions(place_with_id.place.ty()) {
353-
self.delegate.borrow_mut().use_cloned(&place_with_id, place_with_id.hir_id);
354-
} else {
355-
self.delegate.borrow_mut().consume(&place_with_id, place_with_id.hir_id);
356-
}
357-
361+
self.consume_clone_or_copy(&place_with_id, place_with_id.hir_id);
358362
self.walk_expr(expr)?;
359363
Ok(())
360364
}
@@ -1132,9 +1136,12 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
11321136
);
11331137

11341138
match capture_info.capture_kind {
1135-
ty::UpvarCapture::ByValue | ty::UpvarCapture::ByUse => {
1139+
ty::UpvarCapture::ByValue => {
11361140
self.consume_or_copy(&place_with_id, place_with_id.hir_id);
11371141
}
1142+
ty::UpvarCapture::ByUse => {
1143+
self.consume_clone_or_copy(&place_with_id, place_with_id.hir_id);
1144+
}
11381145
ty::UpvarCapture::ByRef(upvar_borrow) => {
11391146
self.delegate.borrow_mut().borrow(
11401147
&place_with_id,

compiler/rustc_hir_typeck/src/upvar.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,11 +1709,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17091709
// Otherwise you'd get an error in 2021 immediately because you'd be trying to take
17101710
// ownership of the (borrowed) String or else you'd take ownership of b, as in 2018 and
17111711
// before, which is also an error.
1712-
hir::CaptureBy::Value { .. } | hir::CaptureBy::Use { .. }
1713-
if !place.deref_tys().any(Ty::is_ref) =>
1714-
{
1712+
hir::CaptureBy::Value { .. } if !place.deref_tys().any(Ty::is_ref) => {
17151713
ty::UpvarCapture::ByValue
17161714
}
1715+
hir::CaptureBy::Use { .. } if !place.deref_tys().any(Ty::is_ref) => {
1716+
ty::UpvarCapture::ByUse
1717+
}
17171718
hir::CaptureBy::Value { .. } | hir::CaptureBy::Use { .. } | hir::CaptureBy::Ref => {
17181719
ty::UpvarCapture::ByRef(BorrowKind::Immutable)
17191720
}
@@ -2404,10 +2405,18 @@ fn determine_capture_info(
24042405
// We select the CaptureKind which ranks higher based the following priority order:
24052406
// (ByUse | ByValue) > MutBorrow > UniqueImmBorrow > ImmBorrow
24062407
match (capture_info_a.capture_kind, capture_info_b.capture_kind) {
2407-
(ty::UpvarCapture::ByUse, _) => capture_info_a,
2408-
(_, ty::UpvarCapture::ByUse) => capture_info_b,
2409-
(ty::UpvarCapture::ByValue, _) => capture_info_a,
2410-
(_, ty::UpvarCapture::ByValue) => capture_info_b,
2408+
(ty::UpvarCapture::ByUse, ty::UpvarCapture::ByValue)
2409+
| (ty::UpvarCapture::ByValue, ty::UpvarCapture::ByUse) => {
2410+
bug!("Same capture can't be ByUse and ByValue at the same time")
2411+
}
2412+
(ty::UpvarCapture::ByValue, ty::UpvarCapture::ByValue)
2413+
| (ty::UpvarCapture::ByUse, ty::UpvarCapture::ByUse)
2414+
| (ty::UpvarCapture::ByValue | ty::UpvarCapture::ByUse, ty::UpvarCapture::ByRef(_)) => {
2415+
capture_info_a
2416+
}
2417+
(ty::UpvarCapture::ByRef(_), ty::UpvarCapture::ByValue | ty::UpvarCapture::ByUse) => {
2418+
capture_info_b
2419+
}
24112420
(ty::UpvarCapture::ByRef(ref_a), ty::UpvarCapture::ByRef(ref_b)) => {
24122421
match (ref_a, ref_b) {
24132422
// Take LHS:

0 commit comments

Comments
 (0)