Skip to content

Commit 5375575

Browse files
Moved UpVar* types to a separate file.
1 parent e4884c1 commit 5375575

File tree

2 files changed

+74
-64
lines changed

2 files changed

+74
-64
lines changed

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 2 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub use self::AssocItemContainer::*;
1515
pub use self::BorrowKind::*;
1616
pub use self::IntVarValue::*;
1717
pub use self::Variance::*;
18+
pub use upvar::*;
1819

1920
use crate::hir::exports::ExportMap;
2021
use crate::hir::place::{
@@ -115,6 +116,7 @@ mod instance;
115116
mod list;
116117
mod structural_impls;
117118
mod sty;
119+
mod upvar;
118120

119121
// Data types
120122

@@ -515,37 +517,6 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for TyS<'tcx> {
515517
#[rustc_diagnostic_item = "Ty"]
516518
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
517519

518-
#[derive(
519-
Clone,
520-
Copy,
521-
Debug,
522-
PartialEq,
523-
Eq,
524-
Hash,
525-
TyEncodable,
526-
TyDecodable,
527-
TypeFoldable,
528-
HashStable
529-
)]
530-
pub struct UpvarPath {
531-
pub hir_id: hir::HirId,
532-
}
533-
534-
/// Upvars do not get their own `NodeId`. Instead, we use the pair of
535-
/// the original var ID (that is, the root variable that is referenced
536-
/// by the upvar) and the ID of the closure expression.
537-
#[derive(Clone, Copy, PartialEq, Eq, Hash, TyEncodable, TyDecodable, TypeFoldable, HashStable)]
538-
pub struct UpvarId {
539-
pub var_path: UpvarPath,
540-
pub closure_expr_id: LocalDefId,
541-
}
542-
543-
impl UpvarId {
544-
pub fn new(var_hir_id: hir::HirId, closure_def_id: LocalDefId) -> UpvarId {
545-
UpvarId { var_path: UpvarPath { hir_id: var_hir_id }, closure_expr_id: closure_def_id }
546-
}
547-
}
548-
549520
#[derive(Clone, PartialEq, Debug, TyEncodable, TyDecodable, TypeFoldable, Copy, HashStable)]
550521
pub enum BorrowKind {
551522
/// Data must be immutable and is aliasable.
@@ -598,36 +569,6 @@ pub enum BorrowKind {
598569
MutBorrow,
599570
}
600571

601-
/// Information describing the capture of an upvar. This is computed
602-
/// during `typeck`, specifically by `regionck`.
603-
#[derive(PartialEq, Clone, Debug, Copy, TyEncodable, TyDecodable, TypeFoldable, HashStable)]
604-
pub enum UpvarCapture<'tcx> {
605-
/// Upvar is captured by value. This is always true when the
606-
/// closure is labeled `move`, but can also be true in other cases
607-
/// depending on inference.
608-
///
609-
/// If the upvar was inferred to be captured by value (e.g. `move`
610-
/// was not used), then the `Span` points to a usage that
611-
/// required it. There may be more than one such usage
612-
/// (e.g. `|| { a; a; }`), in which case we pick an
613-
/// arbitrary one.
614-
ByValue(Option<Span>),
615-
616-
/// Upvar is captured by reference.
617-
ByRef(UpvarBorrow<'tcx>),
618-
}
619-
620-
#[derive(PartialEq, Clone, Copy, TyEncodable, TyDecodable, TypeFoldable, HashStable)]
621-
pub struct UpvarBorrow<'tcx> {
622-
/// The kind of borrow: by-ref upvars have access to shared
623-
/// immutable borrows, which are not part of the normal language
624-
/// syntax.
625-
pub kind: BorrowKind,
626-
627-
/// Region of the resulting reference.
628-
pub region: ty::Region<'tcx>,
629-
}
630-
631572
/// Given the closure DefId this map provides a map of root variables to minimum
632573
/// set of `CapturedPlace`s that need to be tracked to support all captures of that closure.
633574
pub type MinCaptureInformationMap<'tcx> = FxHashMap<DefId, RootVariableMinCaptureList<'tcx>>;
@@ -749,9 +690,6 @@ pub struct CaptureInfo<'tcx> {
749690
pub capture_kind: UpvarCapture<'tcx>,
750691
}
751692

752-
pub type UpvarListMap = FxHashMap<DefId, FxIndexMap<hir::HirId, UpvarId>>;
753-
pub type UpvarCaptureMap<'tcx> = FxHashMap<UpvarId, UpvarCapture<'tcx>>;
754-
755693
impl ty::EarlyBoundRegion {
756694
/// Does this early bound region have a name? Early bound regions normally
757695
/// always have names except when using anonymous lifetimes (`'_`).

compiler/rustc_middle/src/ty/upvar.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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>>;

0 commit comments

Comments
 (0)