Skip to content

Commit 73e86a2

Browse files
committed
Rewrite place workflow in rustc_mir/build
1 parent 244fbb4 commit 73e86a2

File tree

2 files changed

+57
-51
lines changed

2 files changed

+57
-51
lines changed

src/librustc_mir/build/expr/as_rvalue.rs

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,6 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
428428
) -> BlockAnd<Operand<'tcx>> {
429429
let this = self;
430430

431-
let tcx = this.hir.tcx();
432431
let source_info = this.source_info(upvar_span);
433432
let temp = this.local_decls.push(LocalDecl::new_temp(upvar_ty, upvar_span));
434433

@@ -439,26 +438,32 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
439438

440439
let arg_place = unpack!(block = this.as_place(block, arg));
441440

442-
let mutability = if let (base_place, Some(projection)) = arg_place.final_projection(tcx) {
443-
match projection {
444-
ProjectionElem::Deref => {
445-
if let PlaceBase::Local(local) = base_place.base {
446-
debug_assert!(
447-
if let Some(ClearCrossCrate::Set(BindingForm::RefForGuard))
448-
= this.local_decls[local].is_user_variable {
449-
true
450-
} else {
451-
false
452-
},
453-
"Unexpected capture place",
454-
);
455-
this.local_decls[local].mutability
456-
} else if let (base_place, Some(projection)) = base_place.final_projection(tcx)
457-
{
458-
if let ProjectionElem::Field(upvar_index, _) = projection {
441+
let mut mutability = if let PlaceBase::Local(local) = arg_place.base {
442+
this.local_decls[local].mutability
443+
} else {
444+
bug!("Unexpected capture place");
445+
};
446+
if !arg_place.has_no_projection() {
447+
let mut proj_iter = arg_place.elems.iter().cloned().rev();
448+
if let Some(projection) = proj_iter.next() {
449+
match projection {
450+
ProjectionElem::Deref => {
451+
if let PlaceBase::Local(local) = arg_place.base {
452+
debug_assert!(
453+
if let Some(ClearCrossCrate::Set(BindingForm::RefForGuard))
454+
= this.local_decls[local].is_user_variable {
455+
true
456+
} else {
457+
false
458+
},
459+
"Unexpected capture place",
460+
);
461+
}
462+
463+
if let Some(ProjectionElem::Field(upvar_index, _)) = proj_iter.next() {
459464
// Not projected from the implicit `self` in a closure.
460465
debug_assert!(
461-
match base_place.base {
466+
match arg_place.base {
462467
PlaceBase::Local(local) => local == Local::new(1),
463468
_ => false,
464469
},
@@ -469,41 +474,34 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
469474
this.upvar_decls.len() > upvar_index.index(),
470475
"Unexpected capture place"
471476
);
472-
this.upvar_decls[upvar_index.index()].mutability
477+
mutability = this.upvar_decls[upvar_index.index()].mutability;
473478
} else {
474479
bug!("Unexpected capture place");
475480
}
476-
} else {
481+
},
482+
ProjectionElem::Field(upvar_index, _) => {
483+
// Not projected from the implicit `self` in a closure.
484+
debug_assert!(
485+
match arg_place.base {
486+
PlaceBase::Local(local) => local == Local::new(1),
487+
_ => false,
488+
},
489+
"Unexpected capture place"
490+
);
491+
// Not in a closure
492+
debug_assert!(
493+
this.upvar_decls.len() > upvar_index.index(),
494+
"Unexpected capture place"
495+
);
496+
mutability = this.upvar_decls[upvar_index.index()].mutability;
497+
},
498+
_ => {
477499
bug!("Unexpected capture place");
478500
}
479-
},
480-
ProjectionElem::Field(upvar_index, _) => {
481-
// Not projected from the implicit `self` in a closure.
482-
debug_assert!(
483-
match base_place.base {
484-
PlaceBase::Local(local) => local == Local::new(1),
485-
_ => false,
486-
},
487-
"Unexpected capture place"
488-
);
489-
// Not in a closure
490-
debug_assert!(
491-
this.upvar_decls.len() > upvar_index.index(),
492-
"Unexpected capture place"
493-
);
494-
this.upvar_decls[upvar_index.index()].mutability
495-
}
496-
_ => {
497-
bug!("Unexpected capture place");
498501
}
499502
}
500-
} else {
501-
if let PlaceBase::Local(local) = arg_place.base {
502-
this.local_decls[local].mutability
503-
} else {
504-
bug!("Unexpected capture place");
505-
}
506-
};
503+
}
504+
507505
let borrow_kind = match mutability {
508506
Mutability::Not => BorrowKind::Unique,
509507
Mutability::Mut => BorrowKind::Mut { allow_two_phase_borrow: false },

src/librustc_mir/build/scope.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -660,8 +660,12 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
660660
match drop_kind {
661661
DropKind::Value { .. } => if !needs_drop { return },
662662
DropKind::Storage => {
663-
match place.base {
664-
PlaceBase::Local(index) => if index.index() <= self.arg_count {
663+
match place {
664+
Place {
665+
base: PlaceBase::Local(index),
666+
elems,
667+
} => if index.index() <= self.arg_count
668+
&& elems.is_empty() {
665669
span_bug!(
666670
span,
667671
"`schedule_drop` called with index {} and arg_count {}",
@@ -938,8 +942,12 @@ fn build_scope_drops<'tcx>(cfg: &mut CFG<'tcx>,
938942

939943
// Drop the storage for both value and storage drops.
940944
// Only temps and vars need their storage dead.
941-
match drop_data.location.base {
942-
PlaceBase::Local(index) if index.index() > arg_count => {
945+
match drop_data.location {
946+
Place {
947+
base: PlaceBase::Local(index),
948+
elems,
949+
} if index.index() > arg_count
950+
&& elems.is_empty() => {
943951
cfg.push(block, Statement {
944952
source_info,
945953
kind: StatementKind::StorageDead(index)

0 commit comments

Comments
 (0)