Skip to content

Commit 1376ccd

Browse files
authored
Rollup merge of #64518 - spastorino:while-let-to-iterate-over-proj-slice, r=oli-obk
Use while let slice_pattern instead of carrying an index around r? @oli-obk
2 parents a5a7ed6 + d1f763f commit 1376ccd

File tree

6 files changed

+24
-16
lines changed

6 files changed

+24
-16
lines changed

src/librustc_mir/borrow_check/conflict_errors.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
614614
projection,
615615
} = first_borrowed_place;
616616

617-
for (i, elem) in projection.iter().enumerate().rev() {
618-
let proj_base = &projection[..i];
617+
let mut cursor = &**projection;
618+
while let [proj_base @ .., elem] = cursor {
619+
cursor = proj_base;
619620

620621
match elem {
621622
ProjectionElem::Field(field, _) if union_ty(base, proj_base).is_some() => {
@@ -637,8 +638,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
637638
projection,
638639
} = second_borrowed_place;
639640

640-
for (i, elem) in projection.iter().enumerate().rev() {
641-
let proj_base = &projection[..i];
641+
let mut cursor = &**projection;
642+
while let [proj_base @ .., elem] = cursor {
643+
cursor = proj_base;
642644

643645
if let ProjectionElem::Field(field, _) = elem {
644646
if let Some(union_ty) = union_ty(base, proj_base) {

src/librustc_mir/borrow_check/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1758,7 +1758,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
17581758
debug!("check_if_assigned_path_is_moved place: {:?}", place);
17591759

17601760
// None case => assigning to `x` does not require `x` be initialized.
1761-
for (i, elem) in place.projection.iter().enumerate().rev() {
1761+
let mut cursor = &*place.projection;
1762+
while let [proj_base @ .., elem] = cursor {
1763+
cursor = proj_base;
1764+
17621765
match elem {
17631766
ProjectionElem::Index(_/*operand*/) |
17641767
ProjectionElem::ConstantIndex { .. } |
@@ -1771,8 +1774,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
17711774

17721775
// assigning to (*P) requires P to be initialized
17731776
ProjectionElem::Deref => {
1774-
let proj_base = &place.projection[..i];
1775-
17761777
self.check_if_full_path_is_moved(
17771778
location, InitializationRequiringAction::Use,
17781779
(PlaceRef {
@@ -1790,7 +1791,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
17901791
}
17911792

17921793
ProjectionElem::Field(..) => {
1793-
let proj_base = &place.projection[..i];
17941794
// if type of `P` has a dtor, then
17951795
// assigning to `P.f` requires `P` itself
17961796
// be already initialized

src/librustc_mir/borrow_check/nll/type_check/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -2417,9 +2417,12 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
24172417
"add_reborrow_constraint({:?}, {:?}, {:?})",
24182418
location, borrow_region, borrowed_place
24192419
);
2420-
for (i, elem) in borrowed_place.projection.iter().enumerate().rev() {
2420+
2421+
let mut cursor = &*borrowed_place.projection;
2422+
while let [proj_base @ .., elem] = cursor {
2423+
cursor = proj_base;
2424+
24212425
debug!("add_reborrow_constraint - iteration {:?}", elem);
2422-
let proj_base = &borrowed_place.projection[..i];
24232426

24242427
match elem {
24252428
ProjectionElem::Deref => {

src/librustc_mir/build/matches/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1296,8 +1296,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
12961296
// Insert a Shallow borrow of the prefixes of any fake borrows.
12971297
for place in fake_borrows
12981298
{
1299-
for (i, elem) in place.projection.iter().enumerate().rev() {
1300-
let proj_base = &place.projection[..i];
1299+
let mut cursor = &*place.projection;
1300+
while let [proj_base @ .., elem] = cursor {
1301+
cursor = proj_base;
13011302

13021303
if let ProjectionElem::Deref = elem {
13031304
// Insert a shallow borrow after a deref. For other

src/librustc_mir/transform/check_unsafety.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,9 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
407407
place: &Place<'tcx>,
408408
is_mut_use: bool,
409409
) {
410-
for (i, elem) in place.projection.iter().enumerate().rev() {
411-
let proj_base = &place.projection[..i];
410+
let mut cursor = &*place.projection;
411+
while let [proj_base @ .., elem] = cursor {
412+
cursor = proj_base;
412413

413414
match elem {
414415
ProjectionElem::Field(..) => {

src/librustc_mir/util/alignment.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ fn is_within_packed<'tcx, L>(tcx: TyCtxt<'tcx>, local_decls: &L, place: &Place<'
3838
where
3939
L: HasLocalDecls<'tcx>,
4040
{
41-
for (i, elem) in place.projection.iter().enumerate().rev() {
42-
let proj_base = &place.projection[..i];
41+
let mut cursor = &*place.projection;
42+
while let [proj_base @ .., elem] = cursor {
43+
cursor = proj_base;
4344

4445
match elem {
4546
// encountered a Deref, which is ABI-aligned

0 commit comments

Comments
 (0)