]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_borrowck/src/def_use.rs
Rollup merge of #92092 - saethlin:fix-sort-guards-sb, r=danielhenrymantilla
[rust.git] / compiler / rustc_borrowck / src / def_use.rs
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::LlvmAsmOutput) |
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::AsmOutput) |
30         PlaceContext::MutatingUse(MutatingUseContext::Yield) |
31
32         // Storage live and storage dead aren't proper defines, but we can ignore
33         // values that come before them.
34         PlaceContext::NonUse(NonUseContext::StorageLive) |
35         PlaceContext::NonUse(NonUseContext::StorageDead) => Some(DefUse::Def),
36
37         ///////////////////////////////////////////////////////////////////////////
38         // REGULAR USES
39         //
40         // These are uses that occur *outside* of a drop. For the
41         // purposes of NLL, these are special in that **all** the
42         // lifetimes appearing in the variable must be live for each regular use.
43
44         PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection) |
45         PlaceContext::MutatingUse(MutatingUseContext::Projection) |
46
47         // Borrows only consider their local used at the point of the borrow.
48         // This won't affect the results since we use this analysis for generators
49         // and we only care about the result at suspension points. Borrows cannot
50         // cross suspension points so this behavior is unproblematic.
51         PlaceContext::MutatingUse(MutatingUseContext::Borrow) |
52         PlaceContext::NonMutatingUse(NonMutatingUseContext::SharedBorrow) |
53         PlaceContext::NonMutatingUse(NonMutatingUseContext::ShallowBorrow) |
54         PlaceContext::NonMutatingUse(NonMutatingUseContext::UniqueBorrow) |
55
56         PlaceContext::MutatingUse(MutatingUseContext::AddressOf) |
57         PlaceContext::NonMutatingUse(NonMutatingUseContext::AddressOf) |
58         PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect) |
59         PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy) |
60         PlaceContext::NonMutatingUse(NonMutatingUseContext::Move) |
61         PlaceContext::NonUse(NonUseContext::AscribeUserTy) |
62         PlaceContext::MutatingUse(MutatingUseContext::Retag) =>
63             Some(DefUse::Use),
64
65         ///////////////////////////////////////////////////////////////////////////
66         // DROP USES
67         //
68         // These are uses that occur in a DROP (a MIR drop, not a
69         // call to `std::mem::drop()`). For the purposes of NLL,
70         // uses in drop are special because `#[may_dangle]`
71         // attributes can affect whether lifetimes must be live.
72
73         PlaceContext::MutatingUse(MutatingUseContext::Drop) =>
74             Some(DefUse::Drop),
75
76         // Debug info is neither def nor use.
77         PlaceContext::NonUse(NonUseContext::VarDebugInfo) => None,
78     }
79 }