about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAman Arora <me@aman-arora.com>2021-07-11 16:26:29 -0400
committerAman Arora <me@aman-arora.com>2021-07-11 16:26:29 -0400
commit10b536fc7133eb6dcbde3fa25dfd0b6c89a5e32c (patch)
tree2e1e0a3420bfa6e3da71045df93a8f221cff3097
parente9a387d6cf5961a7f2dcb671da3147bd413355c4 (diff)
downloadrust-10b536fc7133eb6dcbde3fa25dfd0b6c89a5e32c.tar.gz
rust-10b536fc7133eb6dcbde3fa25dfd0b6c89a5e32c.zip
ExprUseVisitor: treat ByValue use of Copy types as ImmBorrow
-rw-r--r--compiler/rustc_typeck/src/check/upvar.rs5
-rw-r--r--compiler/rustc_typeck/src/expr_use_visitor.rs24
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 9a39a32f6d5..7e3bc785c07 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 f1749412794..bc34525662f 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(