]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_borrowck/src/def_use.rs
Rollup merge of #101765 - GuillaumeGomez:tyctxt-visibility-doc, r=jyn514
[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         // We let Call define the result in both the success and
20         // unwind cases. This is not really correct, however it
21         // does not seem to be observable due to the way that we
22         // generate MIR. To do things properly, we would apply
23         // the def in call only to the input from the success
24         // path and not the unwind path. -nmatsakis
25         PlaceContext::MutatingUse(MutatingUseContext::Call) |
26         PlaceContext::MutatingUse(MutatingUseContext::AsmOutput) |
27         PlaceContext::MutatingUse(MutatingUseContext::Yield) |
28
29         // Storage live and storage dead aren't proper defines, but we can ignore
30         // values that come before them.
31         PlaceContext::NonUse(NonUseContext::StorageLive) |
32         PlaceContext::NonUse(NonUseContext::StorageDead) => Some(DefUse::Def),
33
34         ///////////////////////////////////////////////////////////////////////////
35         // REGULAR USES
36         //
37         // These are uses that occur *outside* of a drop. For the
38         // purposes of NLL, these are special in that **all** the
39         // lifetimes appearing in the variable must be live for each regular use.
40
41         PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection) |
42         PlaceContext::MutatingUse(MutatingUseContext::Projection) |
43
44         // Borrows only consider their local used at the point of the borrow.
45         // This won't affect the results since we use this analysis for generators
46         // and we only care about the result at suspension points. Borrows cannot
47         // cross suspension points so this behavior is unproblematic.
48         PlaceContext::MutatingUse(MutatingUseContext::Borrow) |
49         PlaceContext::NonMutatingUse(NonMutatingUseContext::SharedBorrow) |
50         PlaceContext::NonMutatingUse(NonMutatingUseContext::ShallowBorrow) |
51         PlaceContext::NonMutatingUse(NonMutatingUseContext::UniqueBorrow) |
52
53         PlaceContext::MutatingUse(MutatingUseContext::AddressOf) |
54         PlaceContext::NonMutatingUse(NonMutatingUseContext::AddressOf) |
55         PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect) |
56         PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy) |
57         PlaceContext::NonMutatingUse(NonMutatingUseContext::Move) |
58         PlaceContext::NonUse(NonUseContext::AscribeUserTy) |
59         PlaceContext::MutatingUse(MutatingUseContext::Retag) =>
60             Some(DefUse::Use),
61
62         ///////////////////////////////////////////////////////////////////////////
63         // DROP USES
64         //
65         // These are uses that occur in a DROP (a MIR drop, not a
66         // call to `std::mem::drop()`). For the purposes of NLL,
67         // uses in drop are special because `#[may_dangle]`
68         // attributes can affect whether lifetimes must be live.
69
70         PlaceContext::MutatingUse(MutatingUseContext::Drop) =>
71             Some(DefUse::Drop),
72
73         // Debug info is neither def nor use.
74         PlaceContext::NonUse(NonUseContext::VarDebugInfo) => None,
75
76         PlaceContext::MutatingUse(MutatingUseContext::Deinit | MutatingUseContext::SetDiscriminant) => {
77             bug!("These statements are not allowed in this MIR phase")
78         }
79     }
80 }