@@ -29,6 +29,10 @@ pub trait Delegate<'tcx> {
2929 // The value found at `place` is moved, depending
3030 // on `mode`. Where `diag_expr_id` is the id used for diagnostics for `place`.
3131 //
32+ // Use of a `Copy` type in a ByValue context is considered a use
33+ // by `ImmBorrow` and `borrow` is called instead.
34+ //
35+ //
3236 // The parameter `diag_expr_id` indicates the HIR id that ought to be used for
3337 // diagnostics. Around pattern matching such as `let pat = expr`, the diagnostic
3438 // id will be the id of the expression `expr` but the place itself will have
@@ -134,16 +138,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
134138 }
135139
136140 fn delegate_consume ( & mut self , place_with_id : & PlaceWithHirId < ' tcx > , diag_expr_id : hir:: HirId ) {
137- debug ! ( "delegate_consume(place_with_id={:?})" , place_with_id) ;
138-
139- let mode = copy_or_move ( & self . mc , place_with_id) ;
140-
141- match mode {
142- ConsumeMode :: Move => self . delegate . consume ( place_with_id, diag_expr_id) ,
143- ConsumeMode :: Copy => {
144- self . delegate . borrow ( place_with_id, diag_expr_id, ty:: BorrowKind :: ImmBorrow )
145- }
146- }
141+ delegate_consume ( & self . mc , self . delegate , place_with_id, diag_expr_id)
147142 }
148143
149144 fn consume_exprs ( & mut self , exprs : & [ hir:: Expr < ' _ > ] ) {
@@ -653,15 +648,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
653648 }
654649 ty:: BindByValue ( ..) => {
655650 debug!( "walk_pat binding consuming pat" ) ;
656- let mode = copy_or_move( mc, & place) ;
657- match mode {
658- ConsumeMode :: Move => delegate. consume( place, discr_place. hir_id) ,
659- ConsumeMode :: Copy => delegate. borrow(
660- place,
661- discr_place. hir_id,
662- ty:: BorrowKind :: ImmBorrow ,
663- ) ,
664- }
651+ delegate_consume( mc, * delegate, place, discr_place. hir_id) ;
665652 }
666653 }
667654 }
@@ -808,3 +795,23 @@ fn copy_or_move<'a, 'tcx>(
808795 ConsumeMode :: Copy
809796 }
810797}
798+
799+ // - If a place is used in a `ByValue` context then move it if it's not a `Copy` type.
800+ // - If the place that is a `Copy` type consider it a `ImmBorrow`.
801+ fn delegate_consume < ' a , ' tcx > (
802+ mc : & mc:: MemCategorizationContext < ' a , ' tcx > ,
803+ delegate : & mut ( dyn Delegate < ' tcx > + ' a ) ,
804+ place_with_id : & PlaceWithHirId < ' tcx > ,
805+ diag_expr_id : hir:: HirId ,
806+ ) {
807+ debug ! ( "delegate_consume(place_with_id={:?})" , place_with_id) ;
808+
809+ let mode = copy_or_move ( & mc, place_with_id) ;
810+
811+ match mode {
812+ ConsumeMode :: Move => delegate. consume ( place_with_id, diag_expr_id) ,
813+ ConsumeMode :: Copy => {
814+ delegate. borrow ( place_with_id, diag_expr_id, ty:: BorrowKind :: ImmBorrow )
815+ }
816+ }
817+ }
0 commit comments