Skip to content

Commit b1499a5

Browse files
committed
Don't fake borrow inside a deref pattern
1 parent be61862 commit b1499a5

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

+12-2
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};
@@ -438,7 +438,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
438438
}
439439

440440
if let Some(ref borrows) = fake_borrows {
441-
self.calculate_fake_borrows(borrows, scrutinee_span)
441+
self.calculate_fake_borrows(borrows, scrutinee_place_builder.base(), scrutinee_span)
442442
} else {
443443
Vec::new()
444444
}
@@ -1941,6 +1941,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
19411941
fn calculate_fake_borrows<'b>(
19421942
&mut self,
19431943
fake_borrows: &'b FxIndexSet<Place<'tcx>>,
1944+
scrutinee_base: PlaceBase,
19441945
temp_span: Span,
19451946
) -> Vec<(Place<'tcx>, Local)> {
19461947
let tcx = self.tcx;
@@ -1951,6 +1952,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
19511952

19521953
// Insert a Shallow borrow of the prefixes of any fake borrows.
19531954
for place in fake_borrows {
1955+
if let PlaceBase::Local(l) = scrutinee_base
1956+
&& l != place.local
1957+
{
1958+
// The base of this place is a temporary created for deref patterns. We don't emit
1959+
// fake borrows for these as they are not initialized in all branches.
1960+
// FIXME(deref_patterns): is this sound?
1961+
continue;
1962+
}
1963+
19541964
let mut cursor = place.projection.as_ref();
19551965
while let [proj_base @ .., elem] = cursor {
19561966
cursor = proj_base;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
fn simple_vec(vec: Vec<u32>) -> u32 {
66
match vec {
77
deref!([]) => 100,
8-
// FIXME(deref_patterns): fake borrows break guards
9-
// deref!([x]) if x == 4 => x + 4,
8+
deref!([x]) if x == 4 => x + 4,
109
deref!([x]) => x,
1110
deref!([1, x]) => x + 200,
1211
deref!(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)