Skip to content

Commit 681d747

Browse files
authored
Rollup merge of #71993 - ecstatic-morse:cleanup-old-liveness, r=jonas-schievink
Remove old `util/liveness.rs` module The liveness dataflow analysis now lives in the `dataflow` module, so this one is no longer necessary. I've copied the relevant bits of the module docs for `util::liveness` to `MaybeLiveLocals`. The example in the docs is now a `mir-dataflow` test: https://github.com/rust-lang/rust/blob/a08c47310c7d49cbdc5d7afb38408ba519967ecd/src/test/ui/mir-dataflow/liveness-ptr.rs#L6-L26 The borrow-checker used the same notion of "defs" and "uses", so I've moved it into a submodule. I would have moved it to `util/def_use.rs`, since it seems generally useful, but there's already a slightly [different version](https://github.com/rust-lang/rust/blob/master/src/librustc_mir/util/def_use.rs) of the same abstraction needed for copy propagation.
2 parents 1e6c199 + 046848e commit 681d747

File tree

8 files changed

+92
-334
lines changed

8 files changed

+92
-334
lines changed
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use rustc_middle::mir::visit::{
2+
MutatingUseContext, NonMutatingUseContext, NonUseContext, PlaceContext,
3+
};
4+
5+
#[derive(Eq, PartialEq, Clone)]
6+
pub enum DefUse {
7+
Def,
8+
Use,
9+
Drop,
10+
}
11+
12+
pub fn categorize(context: PlaceContext) -> Option<DefUse> {
13+
match context {
14+
///////////////////////////////////////////////////////////////////////////
15+
// DEFS
16+
17+
PlaceContext::MutatingUse(MutatingUseContext::Store) |
18+
19+
// This is potentially both a def and a use...
20+
PlaceContext::MutatingUse(MutatingUseContext::AsmOutput) |
21+
22+
// We let Call define the result in both the success and
23+
// unwind cases. This is not really correct, however it
24+
// does not seem to be observable due to the way that we
25+
// generate MIR. To do things properly, we would apply
26+
// the def in call only to the input from the success
27+
// path and not the unwind path. -nmatsakis
28+
PlaceContext::MutatingUse(MutatingUseContext::Call) |
29+
PlaceContext::MutatingUse(MutatingUseContext::Yield) |
30+
31+
// Storage live and storage dead aren't proper defines, but we can ignore
32+
// values that come before them.
33+
PlaceContext::NonUse(NonUseContext::StorageLive) |
34+
PlaceContext::NonUse(NonUseContext::StorageDead) => Some(DefUse::Def),
35+
36+
///////////////////////////////////////////////////////////////////////////
37+
// REGULAR USES
38+
//
39+
// These are uses that occur *outside* of a drop. For the
40+
// purposes of NLL, these are special in that **all** the
41+
// lifetimes appearing in the variable must be live for each regular use.
42+
43+
PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection) |
44+
PlaceContext::MutatingUse(MutatingUseContext::Projection) |
45+
46+
// Borrows only consider their local used at the point of the borrow.
47+
// This won't affect the results since we use this analysis for generators
48+
// and we only care about the result at suspension points. Borrows cannot
49+
// cross suspension points so this behavior is unproblematic.
50+
PlaceContext::MutatingUse(MutatingUseContext::Borrow) |
51+
PlaceContext::NonMutatingUse(NonMutatingUseContext::SharedBorrow) |
52+
PlaceContext::NonMutatingUse(NonMutatingUseContext::ShallowBorrow) |
53+
PlaceContext::NonMutatingUse(NonMutatingUseContext::UniqueBorrow) |
54+
55+
PlaceContext::MutatingUse(MutatingUseContext::AddressOf) |
56+
PlaceContext::NonMutatingUse(NonMutatingUseContext::AddressOf) |
57+
PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect) |
58+
PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy) |
59+
PlaceContext::NonMutatingUse(NonMutatingUseContext::Move) |
60+
PlaceContext::NonUse(NonUseContext::AscribeUserTy) |
61+
PlaceContext::MutatingUse(MutatingUseContext::Retag) =>
62+
Some(DefUse::Use),
63+
64+
///////////////////////////////////////////////////////////////////////////
65+
// DROP USES
66+
//
67+
// These are uses that occur in a DROP (a MIR drop, not a
68+
// call to `std::mem::drop()`). For the purposes of NLL,
69+
// uses in drop are special because `#[may_dangle]`
70+
// attributes can affect whether lifetimes must be live.
71+
72+
PlaceContext::MutatingUse(MutatingUseContext::Drop) =>
73+
Some(DefUse::Drop),
74+
75+
// Debug info is neither def nor use.
76+
PlaceContext::NonUse(NonUseContext::VarDebugInfo) => None,
77+
}
78+
}

src/librustc_mir/borrow_check/diagnostics/find_use.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use std::collections::VecDeque;
22
use std::rc::Rc;
33

44
use crate::borrow_check::{
5+
def_use::{self, DefUse},
56
nll::ToRegionVid,
67
region_infer::{Cause, RegionInferenceContext},
78
};
8-
use crate::util::liveness::{self, DefUse};
99
use rustc_data_structures::fx::FxHashSet;
1010
use rustc_middle::mir::visit::{MirVisitable, PlaceContext, Visitor};
1111
use rustc_middle::mir::{Body, Local, Location};
@@ -117,7 +117,7 @@ impl<'cx, 'tcx> Visitor<'tcx> for DefUseVisitor<'cx, 'tcx> {
117117
});
118118

119119
if found_it {
120-
self.def_use_result = match liveness::categorize(context) {
120+
self.def_use_result = match def_use::categorize(context) {
121121
Some(DefUse::Def) => Some(DefUseResult::Def),
122122
Some(DefUse::Use) => Some(DefUseResult::UseLive { local }),
123123
Some(DefUse::Drop) => Some(DefUseResult::UseDrop { local }),

src/librustc_mir/borrow_check/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ use self::path_utils::*;
5151
mod borrow_set;
5252
mod constraint_generation;
5353
mod constraints;
54+
mod def_use;
5455
mod diagnostics;
5556
mod facts;
5657
mod invalidation;

src/librustc_mir/borrow_check/type_check/liveness/local_use_map.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ use rustc_index::vec::IndexVec;
33
use rustc_middle::mir::visit::{PlaceContext, Visitor};
44
use rustc_middle::mir::{Body, Local, Location};
55

6-
use crate::util::liveness::{categorize, DefUse};
7-
6+
use crate::borrow_check::def_use::{self, DefUse};
87
use crate::borrow_check::region_infer::values::{PointIndex, RegionValueElements};
98

109
/// A map that cross references each local with the locations where it
@@ -160,7 +159,7 @@ impl LocalUseMapBuild<'_> {
160159
impl Visitor<'tcx> for LocalUseMapBuild<'_> {
161160
fn visit_local(&mut self, &local: &Local, context: PlaceContext, location: Location) {
162161
if self.locals_with_use_data[local] {
163-
match categorize(context) {
162+
match def_use::categorize(context) {
164163
Some(DefUse::Def) => self.insert_def(local, location),
165164
Some(DefUse::Use) => self.insert_use(local, location),
166165
Some(DefUse::Drop) => self.insert_drop(local, location),

src/librustc_mir/borrow_check/type_check/liveness/polonius.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use crate::borrow_check::def_use::{self, DefUse};
12
use crate::borrow_check::location::{LocationIndex, LocationTable};
23
use crate::dataflow::indexes::MovePathIndex;
34
use crate::dataflow::move_paths::{LookupResult, MoveData};
4-
use crate::util::liveness::{categorize, DefUse};
55
use rustc_middle::mir::visit::{MutatingUseContext, PlaceContext, Visitor};
66
use rustc_middle::mir::{Body, Local, Location, Place};
77
use rustc_middle::ty::subst::GenericArg;
@@ -56,7 +56,7 @@ impl UseFactsExtractor<'_> {
5656

5757
impl Visitor<'tcx> for UseFactsExtractor<'_> {
5858
fn visit_local(&mut self, &local: &Local, context: PlaceContext, location: Location) {
59-
match categorize(context) {
59+
match def_use::categorize(context) {
6060
Some(DefUse::Def) => self.insert_def(local, location),
6161
Some(DefUse::Use) => self.insert_use(local, location),
6262
Some(DefUse::Drop) => self.insert_drop_use(local, location),

src/librustc_mir/dataflow/impls/liveness.rs

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ use crate::dataflow::{AnalysisDomain, Backward, BottomValue, GenKill, GenKillAna
66

77
/// A [live-variable dataflow analysis][liveness].
88
///
9+
/// This analysis considers references as being used only at the point of the
10+
/// borrow. In other words, this analysis does not track uses because of references that already
11+
/// exist. See [this `mir-datalow` test][flow-test] for an example. You almost never want to use
12+
/// this analysis without also looking at the results of [`MaybeBorrowedLocals`].
13+
///
14+
/// [`MaybeBorrowedLocals`]: ../struct.MaybeBorrowedLocals.html
15+
/// [flow-test]: https://github.com/rust-lang/rust/blob/a08c47310c7d49cbdc5d7afb38408ba519967ecd/src/test/ui/mir-dataflow/liveness-ptr.rs
916
/// [liveness]: https://en.wikipedia.org/wiki/Live_variable_analysis
1017
pub struct MaybeLiveLocals;
1118

0 commit comments

Comments
 (0)