|
| 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 | +} |
0 commit comments