Skip to content

Commit eace240

Browse files
use option<PlaceRef<'tcx>> to clean up mir code a little
1 parent 1279b3b commit eace240

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

compiler/rustc_middle/src/mir/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -962,8 +962,7 @@ impl<'tcx> LocalDecl<'tcx> {
962962
opt_ty_info: _,
963963
opt_match_place: _,
964964
pat_span: _,
965-
})
966-
| BindingForm::ImplicitSelf(ImplicitSelfKind::Imm),
965+
}) | BindingForm::ImplicitSelf(ImplicitSelfKind::Imm),
967966
)))
968967
)
969968
}
@@ -980,8 +979,7 @@ impl<'tcx> LocalDecl<'tcx> {
980979
opt_ty_info: _,
981980
opt_match_place: _,
982981
pat_span: _,
983-
})
984-
| BindingForm::ImplicitSelf(_),
982+
}) | BindingForm::ImplicitSelf(_),
985983
)))
986984
)
987985
}

compiler/rustc_mir/src/transform/check_consts/validation.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
492492
// Special-case reborrows to be more like a copy of a reference.
493493
match *rvalue {
494494
Rvalue::Ref(_, kind, place) => {
495-
if let Some(reborrowed_proj) = place_as_reborrow(self.tcx, self.body, place) {
495+
if let Some(place_ref) = place_as_reborrow(self.tcx, self.body, place) {
496496
let ctx = match kind {
497497
BorrowKind::Shared => {
498498
PlaceContext::NonMutatingUse(NonMutatingUseContext::SharedBorrow)
@@ -508,20 +508,20 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
508508
}
509509
};
510510
self.visit_local(&place.local, ctx, location);
511-
self.visit_projection(place.local, reborrowed_proj, ctx, location);
511+
self.visit_projection(place.local, place_ref.projection, ctx, location);
512512
return;
513513
}
514514
}
515515
Rvalue::AddressOf(mutbl, place) => {
516-
if let Some(reborrowed_proj) = place_as_reborrow(self.tcx, self.body, place) {
516+
if let Some(place_ref) = place_as_reborrow(self.tcx, self.body, place) {
517517
let ctx = match mutbl {
518518
Mutability::Not => {
519519
PlaceContext::NonMutatingUse(NonMutatingUseContext::AddressOf)
520520
}
521521
Mutability::Mut => PlaceContext::MutatingUse(MutatingUseContext::AddressOf),
522522
};
523523
self.visit_local(&place.local, ctx, location);
524-
self.visit_projection(place.local, reborrowed_proj, ctx, location);
524+
self.visit_projection(place.local, place_ref.projection, ctx, location);
525525
return;
526526
}
527527
}
@@ -1016,7 +1016,7 @@ fn place_as_reborrow(
10161016
tcx: TyCtxt<'tcx>,
10171017
body: &Body<'tcx>,
10181018
place: Place<'tcx>,
1019-
) -> Option<&'a [PlaceElem<'tcx>]> {
1019+
) -> Option<PlaceRef<'tcx>> {
10201020
match place.as_ref().last_projection() {
10211021
Some((place_base, ProjectionElem::Deref)) => {
10221022
// A borrow of a `static` also looks like `&(*_1)` in the MIR, but `_1` is a `const`
@@ -1025,13 +1025,14 @@ fn place_as_reborrow(
10251025
None
10261026
} else {
10271027
// Ensure the type being derefed is a reference and not a raw pointer.
1028-
//
10291028
// This is sufficient to prevent an access to a `static mut` from being marked as a
10301029
// reborrow, even if the check above were to disappear.
10311030
let inner_ty = place_base.ty(body, tcx).ty;
1032-
match inner_ty.kind() {
1033-
ty::Ref(..) => Some(place_base.projection),
1034-
_ => None,
1031+
1032+
if let ty::Ref(..) = inner_ty.kind() {
1033+
return Some(place_base);
1034+
} else {
1035+
return None;
10351036
}
10361037
}
10371038
}

0 commit comments

Comments
 (0)