]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/ty/upvar.rs
Moved all Adt* types to adt.rs
[rust.git] / compiler / rustc_middle / src / ty / upvar.rs
1 use crate::ty;
2
3 use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
4 use rustc_hir as hir;
5 use rustc_hir::def_id::{DefId, LocalDefId};
6 use rustc_span::Span;
7
8 use super::BorrowKind;
9
10 #[derive(
11     Clone,
12     Copy,
13     Debug,
14     PartialEq,
15     Eq,
16     Hash,
17     TyEncodable,
18     TyDecodable,
19     TypeFoldable,
20     HashStable
21 )]
22 pub struct UpvarPath {
23     pub hir_id: hir::HirId,
24 }
25
26 /// Upvars do not get their own `NodeId`. Instead, we use the pair of
27 /// the original var ID (that is, the root variable that is referenced
28 /// by the upvar) and the ID of the closure expression.
29 #[derive(Clone, Copy, PartialEq, Eq, Hash, TyEncodable, TyDecodable, TypeFoldable, HashStable)]
30 pub struct UpvarId {
31     pub var_path: UpvarPath,
32     pub closure_expr_id: LocalDefId,
33 }
34
35 impl UpvarId {
36     pub fn new(var_hir_id: hir::HirId, closure_def_id: LocalDefId) -> UpvarId {
37         UpvarId { var_path: UpvarPath { hir_id: var_hir_id }, closure_expr_id: closure_def_id }
38     }
39 }
40
41 /// Information describing the capture of an upvar. This is computed
42 /// during `typeck`, specifically by `regionck`.
43 #[derive(PartialEq, Clone, Debug, Copy, TyEncodable, TyDecodable, TypeFoldable, HashStable)]
44 pub enum UpvarCapture<'tcx> {
45     /// Upvar is captured by value. This is always true when the
46     /// closure is labeled `move`, but can also be true in other cases
47     /// depending on inference.
48     ///
49     /// If the upvar was inferred to be captured by value (e.g. `move`
50     /// was not used), then the `Span` points to a usage that
51     /// required it. There may be more than one such usage
52     /// (e.g. `|| { a; a; }`), in which case we pick an
53     /// arbitrary one.
54     ByValue(Option<Span>),
55
56     /// Upvar is captured by reference.
57     ByRef(UpvarBorrow<'tcx>),
58 }
59
60 #[derive(PartialEq, Clone, Copy, TyEncodable, TyDecodable, TypeFoldable, HashStable)]
61 pub struct UpvarBorrow<'tcx> {
62     /// The kind of borrow: by-ref upvars have access to shared
63     /// immutable borrows, which are not part of the normal language
64     /// syntax.
65     pub kind: BorrowKind,
66
67     /// Region of the resulting reference.
68     pub region: ty::Region<'tcx>,
69 }
70
71 pub type UpvarListMap = FxHashMap<DefId, FxIndexMap<hir::HirId, UpvarId>>;
72 pub type UpvarCaptureMap<'tcx> = FxHashMap<UpvarId, UpvarCapture<'tcx>>;