@@ -1000,13 +1000,15 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
10001000 // determines whether to borrow *at the level of the deref pattern* rather than
10011001 // borrowing the bound place (since that inner place is inside the temporary that
10021002 // stores the result of calling `deref()`/`deref_mut()` so can't be captured).
1003+ // Deref patterns on boxes don't borrow, so we ignore them here.
10031004 // HACK: this could be a fake pattern corresponding to a deref inserted by match
10041005 // ergonomics, in which case `pat.hir_id` will be the id of the subpattern.
1005- let mutable = self . cx . typeck_results ( ) . pat_has_ref_mut_binding ( subpattern) ;
1006- let mutability =
1007- if mutable { hir:: Mutability :: Mut } else { hir:: Mutability :: Not } ;
1008- let bk = ty:: BorrowKind :: from_mutbl ( mutability) ;
1009- self . delegate . borrow_mut ( ) . borrow ( place, discr_place. hir_id , bk) ;
1006+ if let hir:: ByRef :: Yes ( mutability) =
1007+ self . cx . typeck_results ( ) . deref_pat_borrow_mode ( place. place . ty ( ) , subpattern)
1008+ {
1009+ let bk = ty:: BorrowKind :: from_mutbl ( mutability) ;
1010+ self . delegate . borrow_mut ( ) . borrow ( place, discr_place. hir_id , bk) ;
1011+ }
10101012 }
10111013 PatKind :: Never => {
10121014 // A `!` pattern always counts as an immutable read of the discriminant,
@@ -1691,18 +1693,19 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
16911693 place_with_id = match adjust. kind {
16921694 adjustment:: PatAdjust :: BuiltinDeref => self . cat_deref ( pat. hir_id , place_with_id) ?,
16931695 adjustment:: PatAdjust :: OverloadedDeref => {
1694- // This adjustment corresponds to an overloaded deref; it borrows the scrutinee to
1695- // call `Deref::deref` or `DerefMut::deref_mut`. Invoke the callback before setting
1696- // `place_with_id` to the temporary storing the result of the deref.
1696+ // This adjustment corresponds to an overloaded deref; unless it's on a box, it
1697+ // borrows the scrutinee to call `Deref::deref` or `DerefMut::deref_mut`. Invoke
1698+ // the callback before setting `place_with_id` to the temporary storing the
1699+ // result of the deref.
16971700 // HACK(dianne): giving the callback a fake deref pattern makes sure it behaves the
1698- // same as it would if this were an explicit deref pattern.
1701+ // same as it would if this were an explicit deref pattern (including for boxes) .
16991702 op ( & place_with_id, & hir:: Pat { kind : PatKind :: Deref ( pat) , ..* pat } ) ?;
17001703 let target_ty = match adjusts. peek ( ) {
17011704 Some ( & & next_adjust) => next_adjust. source ,
17021705 // At the end of the deref chain, we get `pat`'s scrutinee.
17031706 None => self . pat_ty_unadjusted ( pat) ?,
17041707 } ;
1705- self . pat_deref_temp ( pat. hir_id , pat, target_ty) ?
1708+ self . pat_deref_place ( pat. hir_id , place_with_id , pat, target_ty) ?
17061709 }
17071710 } ;
17081711 }
@@ -1810,7 +1813,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
18101813 }
18111814 PatKind :: Deref ( subpat) => {
18121815 let ty = self . pat_ty_adjusted ( subpat) ?;
1813- let place = self . pat_deref_temp ( pat. hir_id , subpat, ty) ?;
1816+ let place = self . pat_deref_place ( pat. hir_id , place_with_id , subpat, ty) ?;
18141817 self . cat_pattern ( place, subpat, op) ?;
18151818 }
18161819
@@ -1863,21 +1866,27 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
18631866 Ok ( ( ) )
18641867 }
18651868
1866- /// Represents the place of the temp that stores the scrutinee of a deref pattern's interior.
1867- fn pat_deref_temp (
1869+ /// Represents the place matched on by a deref pattern's interior.
1870+ fn pat_deref_place (
18681871 & self ,
18691872 hir_id : HirId ,
1873+ base_place : PlaceWithHirId < ' tcx > ,
18701874 inner : & hir:: Pat < ' _ > ,
18711875 target_ty : Ty < ' tcx > ,
18721876 ) -> Result < PlaceWithHirId < ' tcx > , Cx :: Error > {
1873- let mutable = self . cx . typeck_results ( ) . pat_has_ref_mut_binding ( inner) ;
1874- let mutability = if mutable { hir:: Mutability :: Mut } else { hir:: Mutability :: Not } ;
1875- let re_erased = self . cx . tcx ( ) . lifetimes . re_erased ;
1876- let ty = Ty :: new_ref ( self . cx . tcx ( ) , re_erased, target_ty, mutability) ;
1877- // A deref pattern stores the result of `Deref::deref` or `DerefMut::deref_mut` ...
1878- let base = self . cat_rvalue ( hir_id, ty) ;
1879- // ... and the inner pattern matches on the place behind that reference.
1880- self . cat_deref ( hir_id, base)
1877+ match self . cx . typeck_results ( ) . deref_pat_borrow_mode ( base_place. place . ty ( ) , inner) {
1878+ // Deref patterns on boxes are lowered using a built-in deref.
1879+ hir:: ByRef :: No => self . cat_deref ( hir_id, base_place) ,
1880+ // For other types, we create a temporary to match on.
1881+ hir:: ByRef :: Yes ( mutability) => {
1882+ let re_erased = self . cx . tcx ( ) . lifetimes . re_erased ;
1883+ let ty = Ty :: new_ref ( self . cx . tcx ( ) , re_erased, target_ty, mutability) ;
1884+ // A deref pattern stores the result of `Deref::deref` or `DerefMut::deref_mut` ...
1885+ let base = self . cat_rvalue ( hir_id, ty) ;
1886+ // ... and the inner pattern matches on the place behind that reference.
1887+ self . cat_deref ( hir_id, base)
1888+ }
1889+ }
18811890 }
18821891
18831892 fn is_multivariant_adt ( & self , ty : Ty < ' tcx > , span : Span ) -> bool {
0 commit comments