mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-03 17:35:28 +03:00
Don't fake borrow inside a deref pattern
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
//! This also includes code for pattern bindings in `let` statements and
|
||||
//! function parameters.
|
||||
|
||||
use crate::build::expr::as_place::PlaceBuilder;
|
||||
use crate::build::expr::as_place::{PlaceBase, PlaceBuilder};
|
||||
use crate::build::scope::DropKind;
|
||||
use crate::build::ForGuard::{self, OutsideGuard, RefWithinGuard};
|
||||
use crate::build::{BlockAnd, BlockAndExtension, Builder};
|
||||
@@ -438,7 +438,7 @@ fn lower_match_tree<'pat>(
|
||||
}
|
||||
|
||||
if let Some(ref borrows) = fake_borrows {
|
||||
self.calculate_fake_borrows(borrows, scrutinee_span)
|
||||
self.calculate_fake_borrows(borrows, scrutinee_place_builder.base(), scrutinee_span)
|
||||
} else {
|
||||
Vec::new()
|
||||
}
|
||||
@@ -1936,6 +1936,7 @@ fn test_candidates<'pat, 'b, 'c>(
|
||||
fn calculate_fake_borrows<'b>(
|
||||
&mut self,
|
||||
fake_borrows: &'b FxIndexSet<Place<'tcx>>,
|
||||
scrutinee_base: PlaceBase,
|
||||
temp_span: Span,
|
||||
) -> Vec<(Place<'tcx>, Local)> {
|
||||
let tcx = self.tcx;
|
||||
@@ -1946,6 +1947,15 @@ fn calculate_fake_borrows<'b>(
|
||||
|
||||
// Insert a Shallow borrow of the prefixes of any fake borrows.
|
||||
for place in fake_borrows {
|
||||
if let PlaceBase::Local(l) = scrutinee_base
|
||||
&& l != place.local
|
||||
{
|
||||
// The base of this place is a temporary created for deref patterns. We don't emit
|
||||
// fake borrows for these as they are not initialized in all branches.
|
||||
// FIXME(deref_patterns): is this sound?
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut cursor = place.projection.as_ref();
|
||||
while let [proj_base @ .., elem] = cursor {
|
||||
cursor = proj_base;
|
||||
|
||||
@@ -5,8 +5,7 @@
|
||||
fn simple_vec(vec: Vec<u32>) -> u32 {
|
||||
match vec {
|
||||
deref!([]) => 100,
|
||||
// FIXME(deref_patterns): fake borrows break guards
|
||||
// deref!([x]) if x == 4 => x + 4,
|
||||
deref!([x]) if x == 4 => x + 4,
|
||||
deref!([x]) => x,
|
||||
deref!([1, x]) => x + 200,
|
||||
deref!(ref slice) => slice.iter().sum(),
|
||||
@@ -29,6 +28,7 @@ fn main() {
|
||||
assert_eq!(simple_vec(vec![1]), 1);
|
||||
assert_eq!(simple_vec(vec![1, 2]), 202);
|
||||
assert_eq!(simple_vec(vec![1, 2, 3]), 6);
|
||||
assert_eq!(simple_vec(vec![4]), 8);
|
||||
|
||||
assert_eq!(nested_vec(vec![vec![0, 42]]), 42);
|
||||
assert_eq!(nested_vec(vec![vec![1, 42]]), 42);
|
||||
|
||||
Reference in New Issue
Block a user