Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 13de583

Browse files
authoredAug 6, 2023
Rollup merge of #114505 - ouz-a:cleanup_mir, r=RalfJung
Add documentation to has_deref Documentation of `has_deref` needed some polish to be more clear about where it should be used and what's it's purpose. cc #114401 r? `@RalfJung`
2 parents 92c0421 + 6df5462 commit 13de583

File tree

7 files changed

+23
-17
lines changed

7 files changed

+23
-17
lines changed
 

‎compiler/rustc_codegen_ssa/src/mir/place.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
441441
LocalRef::Place(place) => place,
442442
LocalRef::UnsizedPlace(place) => bx.load_operand(place).deref(cx),
443443
LocalRef::Operand(..) => {
444-
if place_ref.has_deref() {
444+
if place_ref.is_indirect_first_projection() {
445445
base = 1;
446446
let cg_base = self.codegen_consume(
447447
bx,

‎compiler/rustc_const_eval/src/interpret/step.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
247247

248248
AddressOf(_, place) => {
249249
// Figure out whether this is an addr_of of an already raw place.
250-
let place_base_raw = if place.has_deref() {
250+
let place_base_raw = if place.is_indirect_first_projection() {
251251
let ty = self.frame().body.local_decls[place.local].ty;
252252
ty.is_unsafe_ptr()
253253
} else {

‎compiler/rustc_middle/src/mir/mod.rs

+17-11
Original file line numberDiff line numberDiff line change
@@ -1592,14 +1592,13 @@ impl<'tcx> Place<'tcx> {
15921592
self.projection.iter().any(|elem| elem.is_indirect())
15931593
}
15941594

1595-
/// If MirPhase >= Derefered and if projection contains Deref,
1596-
/// It's guaranteed to be in the first place
1597-
pub fn has_deref(&self) -> bool {
1598-
// To make sure this is not accidentally used in wrong mir phase
1599-
debug_assert!(
1600-
self.projection.is_empty() || !self.projection[1..].contains(&PlaceElem::Deref)
1601-
);
1602-
self.projection.first() == Some(&PlaceElem::Deref)
1595+
/// Returns `true` if this `Place`'s first projection is `Deref`.
1596+
///
1597+
/// This is useful because for MIR phases `AnalysisPhase::PostCleanup` and later,
1598+
/// `Deref` projections can only occur as the first projection. In that case this method
1599+
/// is equivalent to `is_indirect`, but faster.
1600+
pub fn is_indirect_first_projection(&self) -> bool {
1601+
self.as_ref().is_indirect_first_projection()
16031602
}
16041603

16051604
/// Finds the innermost `Local` from this `Place`, *if* it is either a local itself or
@@ -1672,9 +1671,16 @@ impl<'tcx> PlaceRef<'tcx> {
16721671
self.projection.iter().any(|elem| elem.is_indirect())
16731672
}
16741673

1675-
/// If MirPhase >= Derefered and if projection contains Deref,
1676-
/// It's guaranteed to be in the first place
1677-
pub fn has_deref(&self) -> bool {
1674+
/// Returns `true` if this `Place`'s first projection is `Deref`.
1675+
///
1676+
/// This is useful because for MIR phases `AnalysisPhase::PostCleanup` and later,
1677+
/// `Deref` projections can only occur as the first projection. In that case this method
1678+
/// is equivalent to `is_indirect`, but faster.
1679+
pub fn is_indirect_first_projection(&self) -> bool {
1680+
// To make sure this is not accidentally used in wrong mir phase
1681+
debug_assert!(
1682+
self.projection.is_empty() || !self.projection[1..].contains(&PlaceElem::Deref)
1683+
);
16781684
self.projection.first() == Some(&PlaceElem::Deref)
16791685
}
16801686

‎compiler/rustc_mir_dataflow/src/value_analysis.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@ impl Map {
839839
tail_elem: Option<TrackElem>,
840840
f: &mut impl FnMut(ValueIndex),
841841
) {
842-
if place.has_deref() {
842+
if place.is_indirect_first_projection() {
843843
// We do not track indirect places.
844844
return;
845845
}

‎compiler/rustc_mir_transform/src/add_retag.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl<'tcx> MirPass<'tcx> for AddRetag {
6060
let basic_blocks = body.basic_blocks.as_mut();
6161
let local_decls = &body.local_decls;
6262
let needs_retag = |place: &Place<'tcx>| {
63-
!place.has_deref() // we're not really interested in stores to "outside" locations, they are hard to keep track of anyway
63+
!place.is_indirect_first_projection() // we're not really interested in stores to "outside" locations, they are hard to keep track of anyway
6464
&& may_contain_reference(place.ty(&*local_decls, tcx).ty, /*depth*/ 3, tcx)
6565
&& !local_decls[place.local].is_deref_temp()
6666
};

‎compiler/rustc_mir_transform/src/copy_prop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl<'tcx> MutVisitor<'tcx> for Replacer<'_, 'tcx> {
154154
fn visit_operand(&mut self, operand: &mut Operand<'tcx>, loc: Location) {
155155
if let Operand::Move(place) = *operand
156156
// A move out of a projection of a copy is equivalent to a copy of the original projection.
157-
&& !place.has_deref()
157+
&& !place.is_indirect_first_projection()
158158
&& !self.fully_moved.contains(place.local)
159159
{
160160
*operand = Operand::Copy(place);

‎src/tools/clippy/clippy_lints/src/dereference.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,7 @@ fn referent_used_exactly_once<'tcx>(
11701170
&& let [location] = *local_assignments(mir, local).as_slice()
11711171
&& let Some(statement) = mir.basic_blocks[location.block].statements.get(location.statement_index)
11721172
&& let StatementKind::Assign(box (_, Rvalue::Ref(_, _, place))) = statement.kind
1173-
&& !place.has_deref()
1173+
&& !place.is_indirect_first_projection()
11741174
// Ensure not in a loop (https://github.com/rust-lang/rust-clippy/issues/9710)
11751175
&& TriColorDepthFirstSearch::new(&mir.basic_blocks).run_from(location.block, &mut CycleDetector).is_none()
11761176
{

0 commit comments

Comments
 (0)