Skip to content

Commit c19f316

Browse files
committed
Introduce PlaceContext::may_observe_address.
1 parent 2a9a296 commit c19f316

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

compiler/rustc_middle/src/mir/visit.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,23 @@ impl PlaceContext {
14021402
)
14031403
}
14041404

1405+
/// Returns `true` if this place context may be used to know the address of the given place.
1406+
pub fn may_observe_address(self) -> bool {
1407+
matches!(
1408+
self,
1409+
PlaceContext::NonMutatingUse(
1410+
NonMutatingUseContext::SharedBorrow
1411+
| NonMutatingUseContext::RawBorrow
1412+
| NonMutatingUseContext::FakeBorrow
1413+
) | PlaceContext::MutatingUse(
1414+
MutatingUseContext::Drop
1415+
| MutatingUseContext::Borrow
1416+
| MutatingUseContext::RawBorrow
1417+
| MutatingUseContext::AsmOutput
1418+
)
1419+
)
1420+
}
1421+
14051422
/// Returns `true` if this place context represents a storage live or storage dead marker.
14061423
#[inline]
14071424
pub fn is_storage_marker(self) -> bool {

compiler/rustc_mir_dataflow/src/value_analysis.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_data_structures::fx::{FxHashMap, FxIndexSet, StdEntry};
66
use rustc_data_structures::stack::ensure_sufficient_stack;
77
use rustc_index::IndexVec;
88
use rustc_index::bit_set::DenseBitSet;
9-
use rustc_middle::mir::visit::{MutatingUseContext, PlaceContext, Visitor};
9+
use rustc_middle::mir::visit::{PlaceContext, Visitor};
1010
use rustc_middle::mir::*;
1111
use rustc_middle::ty::{self, Ty, TyCtxt};
1212
use tracing::debug;
@@ -917,12 +917,7 @@ pub fn excluded_locals(body: &Body<'_>) -> DenseBitSet<Local> {
917917

918918
impl<'tcx> Visitor<'tcx> for Collector {
919919
fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, _location: Location) {
920-
if (context.is_borrow()
921-
|| context.is_address_of()
922-
|| context.is_drop()
923-
|| context == PlaceContext::MutatingUse(MutatingUseContext::AsmOutput))
924-
&& !place.is_indirect()
925-
{
920+
if context.may_observe_address() && !place.is_indirect() {
926921
// A pointer to a place could be used to access other places with the same local,
927922
// hence we have to exclude the local completely.
928923
self.result.insert(place.local);

compiler/rustc_mir_transform/src/ssa.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ impl SsaVisitor<'_, '_> {
225225

226226
impl<'tcx> Visitor<'tcx> for SsaVisitor<'_, 'tcx> {
227227
fn visit_local(&mut self, local: Local, ctxt: PlaceContext, loc: Location) {
228+
if ctxt.may_observe_address() {
229+
self.borrowed_locals.insert(local);
230+
}
228231
match ctxt {
229232
PlaceContext::MutatingUse(MutatingUseContext::Projection)
230233
| PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection) => bug!(),
@@ -237,7 +240,6 @@ impl<'tcx> Visitor<'tcx> for SsaVisitor<'_, 'tcx> {
237240
PlaceContext::NonMutatingUse(
238241
NonMutatingUseContext::SharedBorrow | NonMutatingUseContext::FakeBorrow,
239242
) => {
240-
self.borrowed_locals.insert(local);
241243
self.check_dominates(local, loc);
242244
self.direct_uses[local] += 1;
243245
}

0 commit comments

Comments
 (0)