Skip to content

Commit 10b536f

Browse files
committed
ExprUseVisitor: treat ByValue use of Copy types as ImmBorrow
1 parent e9a387d commit 10b536f

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

compiler/rustc_typeck/src/check/upvar.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1535,10 +1535,9 @@ impl<'a, 'tcx> InferBorrowKind<'a, 'tcx> {
15351535
place_with_id, diag_expr_id, mode
15361536
);
15371537

1538-
// Copy type being used as ByValue are equivalent to ImmBorrow and don't require any
1539-
// escalation.
1538+
// Copy types in ByValue scenarios need should be treated as ImmBorrows
15401539
match mode {
1541-
euv::ConsumeMode::Copy => return,
1540+
euv::ConsumeMode::Copy => unreachable!(),
15421541
euv::ConsumeMode::Move => {}
15431542
};
15441543

compiler/rustc_typeck/src/expr_use_visitor.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,13 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
144144
debug!("delegate_consume(place_with_id={:?})", place_with_id);
145145

146146
let mode = copy_or_move(&self.mc, place_with_id);
147-
self.delegate.consume(place_with_id, diag_expr_id, mode);
147+
148+
match mode {
149+
ConsumeMode::Move => self.delegate.consume(place_with_id, diag_expr_id, mode),
150+
ConsumeMode::Copy => {
151+
self.delegate.borrow(place_with_id, diag_expr_id, ty::BorrowKind::ImmBorrow)
152+
}
153+
}
148154
}
149155

150156
fn consume_exprs(&mut self, exprs: &[hir::Expr<'_>]) {
@@ -653,9 +659,18 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
653659
delegate.borrow(place, discr_place.hir_id, bk);
654660
}
655661
ty::BindByValue(..) => {
656-
let mode = copy_or_move(mc, &place);
657662
debug!("walk_pat binding consuming pat");
658-
delegate.consume(place, discr_place.hir_id, mode);
663+
let mode = copy_or_move(mc, &place);
664+
match mode {
665+
ConsumeMode::Move => {
666+
delegate.consume(place, discr_place.hir_id, mode)
667+
}
668+
ConsumeMode::Copy => delegate.borrow(
669+
place,
670+
discr_place.hir_id,
671+
ty::BorrowKind::ImmBorrow,
672+
),
673+
}
659674
}
660675
}
661676
}
@@ -773,8 +788,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
773788

774789
match capture_info.capture_kind {
775790
ty::UpvarCapture::ByValue(_) => {
776-
let mode = copy_or_move(&self.mc, &place_with_id);
777-
self.delegate.consume(&place_with_id, place_with_id.hir_id, mode);
791+
self.delegate_consume(&place_with_id, place_with_id.hir_id);
778792
}
779793
ty::UpvarCapture::ByRef(upvar_borrow) => {
780794
self.delegate.borrow(

0 commit comments

Comments
 (0)