Skip to content

Commit 9987396

Browse files
committed
Don't fake borrow below a deref pattern
1 parent 81560a4 commit 9987396

File tree

2 files changed

+14
-4
lines changed
  • compiler/rustc_mir_build/src/build/matches
  • tests/ui/pattern/deref-patterns

2 files changed

+14
-4
lines changed

compiler/rustc_mir_build/src/build/matches/mod.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
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};
99
use crate::build::scope::DropKind;
1010
use crate::build::ForGuard::{self, OutsideGuard, RefWithinGuard};
1111
use 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;

tests/ui/pattern/deref-patterns/bindings.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
fn simple_vec(vec: Vec<u32>) -> u32 {
66
match vec {
77
box [] => 100,
8-
// FIXME(deref_patterns): fake borrows break guards
9-
// box [x] if x == 4 => x + 4,
8+
box [x] if x == 4 => x + 4,
109
box [x] => x,
1110
box [1, x] => x + 200,
1211
box ref slice => slice.iter().sum(),
@@ -29,6 +28,7 @@ fn main() {
2928
assert_eq!(simple_vec(vec![1]), 1);
3029
assert_eq!(simple_vec(vec![1, 2]), 202);
3130
assert_eq!(simple_vec(vec![1, 2, 3]), 6);
31+
assert_eq!(simple_vec(vec![4]), 8);
3232

3333
assert_eq!(nested_vec(vec![vec![0, 42]]), 42);
3434
assert_eq!(nested_vec(vec![vec![1, 42]]), 42);

0 commit comments

Comments
 (0)