@@ -29,6 +29,10 @@ pub trait Delegate<'tcx> {
29
29
// The value found at `place` is moved, depending
30
30
// on `mode`. Where `diag_expr_id` is the id used for diagnostics for `place`.
31
31
//
32
+ // Use of a `Copy` type in a ByValue context is considered a use
33
+ // by `ImmBorrow` and `borrow` is called instead.
34
+ //
35
+ //
32
36
// The parameter `diag_expr_id` indicates the HIR id that ought to be used for
33
37
// diagnostics. Around pattern matching such as `let pat = expr`, the diagnostic
34
38
// 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> {
134
138
}
135
139
136
140
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)
147
142
}
148
143
149
144
fn consume_exprs ( & mut self , exprs : & [ hir:: Expr < ' _ > ] ) {
@@ -653,15 +648,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
653
648
}
654
649
ty:: BindByValue ( ..) => {
655
650
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) ;
665
652
}
666
653
}
667
654
}
@@ -808,3 +795,23 @@ fn copy_or_move<'a, 'tcx>(
808
795
ConsumeMode :: Copy
809
796
}
810
797
}
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