From 10b536fc7133eb6dcbde3fa25dfd0b6c89a5e32c Mon Sep 17 00:00:00 2001 From: Aman Arora Date: Sun, 11 Jul 2021 16:26:29 -0400 Subject: [PATCH] ExprUseVisitor: treat ByValue use of Copy types as ImmBorrow --- compiler/rustc_typeck/src/check/upvar.rs | 5 ++-- compiler/rustc_typeck/src/expr_use_visitor.rs | 24 +++++++++++++++---- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_typeck/src/check/upvar.rs b/compiler/rustc_typeck/src/check/upvar.rs index 9a39a32f6d5d..7e3bc785c07c 100644 --- a/compiler/rustc_typeck/src/check/upvar.rs +++ b/compiler/rustc_typeck/src/check/upvar.rs @@ -1535,10 +1535,9 @@ impl<'a, 'tcx> InferBorrowKind<'a, 'tcx> { place_with_id, diag_expr_id, mode ); - // Copy type being used as ByValue are equivalent to ImmBorrow and don't require any - // escalation. + // Copy types in ByValue scenarios need should be treated as ImmBorrows match mode { - euv::ConsumeMode::Copy => return, + euv::ConsumeMode::Copy => unreachable!(), euv::ConsumeMode::Move => {} }; diff --git a/compiler/rustc_typeck/src/expr_use_visitor.rs b/compiler/rustc_typeck/src/expr_use_visitor.rs index f17494127948..bc34525662f5 100644 --- a/compiler/rustc_typeck/src/expr_use_visitor.rs +++ b/compiler/rustc_typeck/src/expr_use_visitor.rs @@ -144,7 +144,13 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> { debug!("delegate_consume(place_with_id={:?})", place_with_id); let mode = copy_or_move(&self.mc, place_with_id); - self.delegate.consume(place_with_id, diag_expr_id, mode); + + match mode { + ConsumeMode::Move => self.delegate.consume(place_with_id, diag_expr_id, mode), + ConsumeMode::Copy => { + self.delegate.borrow(place_with_id, diag_expr_id, ty::BorrowKind::ImmBorrow) + } + } } fn consume_exprs(&mut self, exprs: &[hir::Expr<'_>]) { @@ -653,9 +659,18 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> { delegate.borrow(place, discr_place.hir_id, bk); } ty::BindByValue(..) => { - let mode = copy_or_move(mc, &place); debug!("walk_pat binding consuming pat"); - delegate.consume(place, discr_place.hir_id, mode); + let mode = copy_or_move(mc, &place); + match mode { + ConsumeMode::Move => { + delegate.consume(place, discr_place.hir_id, mode) + } + ConsumeMode::Copy => delegate.borrow( + place, + discr_place.hir_id, + ty::BorrowKind::ImmBorrow, + ), + } } } } @@ -773,8 +788,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> { match capture_info.capture_kind { ty::UpvarCapture::ByValue(_) => { - let mode = copy_or_move(&self.mc, &place_with_id); - self.delegate.consume(&place_with_id, place_with_id.hir_id, mode); + self.delegate_consume(&place_with_id, place_with_id.hir_id); } ty::UpvarCapture::ByRef(upvar_borrow) => { self.delegate.borrow(