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