55//! This also includes code for pattern bindings in `let` statements and
66//! function parameters.
77
8- use crate :: build:: expr:: as_place:: PlaceBuilder ;
8+ use crate :: build:: expr:: as_place:: { PlaceBase , PlaceBuilder } ;
99use crate :: build:: scope:: DropKind ;
1010use crate :: build:: ForGuard :: { self , OutsideGuard , RefWithinGuard } ;
1111use crate :: build:: { BlockAnd , BlockAndExtension , Builder } ;
@@ -361,7 +361,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
361361 }
362362
363363 if let Some ( ref borrows) = fake_borrows {
364- self . calculate_fake_borrows ( borrows, scrutinee_span)
364+ self . calculate_fake_borrows ( borrows, scrutinee_place_builder . base ( ) , scrutinee_span)
365365 } else {
366366 Vec :: new ( )
367367 }
@@ -1862,6 +1862,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
18621862 fn calculate_fake_borrows < ' b > (
18631863 & mut self ,
18641864 fake_borrows : & ' b FxIndexSet < Place < ' tcx > > ,
1865+ scrutinee_base : PlaceBase ,
18651866 temp_span : Span ,
18661867 ) -> Vec < ( Place < ' tcx > , Local ) > {
18671868 let tcx = self . tcx ;
@@ -1872,6 +1873,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
18721873
18731874 // Insert a Shallow borrow of the prefixes of any fake borrows.
18741875 for place in fake_borrows {
1876+ if let PlaceBase :: Local ( l) = scrutinee_base
1877+ && l != place. local
1878+ {
1879+ // The base of this place is a temporary created for deref patterns. We don't emit
1880+ // fake borrows for these as they are not initialized in all branches.
1881+ // FIXME(deref_patterns): does this allow for subtle bugs?
1882+ continue ;
1883+ }
1884+
18751885 let mut cursor = place. projection . as_ref ( ) ;
18761886 while let [ proj_base @ .., elem] = cursor {
18771887 cursor = proj_base;
0 commit comments