Skip to content

Commit 90cbb39

Browse files
Moved more types into upvar.rs (now named closure.rs)
1 parent 0ba5a6b commit 90cbb39

File tree

3 files changed

+170
-169
lines changed

3 files changed

+170
-169
lines changed
+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
use crate::hir::place::{Place as HirPlace, PlaceBase as HirPlaceBase};
2+
use crate::ty;
3+
4+
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
5+
use rustc_hir as hir;
6+
use rustc_hir::def_id::{DefId, LocalDefId};
7+
use rustc_hir::lang_items::LangItem;
8+
use rustc_span::Span;
9+
10+
use super::{BorrowKind, CaptureInfo, Ty, TyCtxt};
11+
12+
#[derive(
13+
Clone,
14+
Copy,
15+
Debug,
16+
PartialEq,
17+
Eq,
18+
Hash,
19+
TyEncodable,
20+
TyDecodable,
21+
TypeFoldable,
22+
HashStable
23+
)]
24+
pub struct UpvarPath {
25+
pub hir_id: hir::HirId,
26+
}
27+
28+
/// Upvars do not get their own `NodeId`. Instead, we use the pair of
29+
/// the original var ID (that is, the root variable that is referenced
30+
/// by the upvar) and the ID of the closure expression.
31+
#[derive(Clone, Copy, PartialEq, Eq, Hash, TyEncodable, TyDecodable, TypeFoldable, HashStable)]
32+
pub struct UpvarId {
33+
pub var_path: UpvarPath,
34+
pub closure_expr_id: LocalDefId,
35+
}
36+
37+
impl UpvarId {
38+
pub fn new(var_hir_id: hir::HirId, closure_def_id: LocalDefId) -> UpvarId {
39+
UpvarId { var_path: UpvarPath { hir_id: var_hir_id }, closure_expr_id: closure_def_id }
40+
}
41+
}
42+
43+
/// Information describing the capture of an upvar. This is computed
44+
/// during `typeck`, specifically by `regionck`.
45+
#[derive(PartialEq, Clone, Debug, Copy, TyEncodable, TyDecodable, TypeFoldable, HashStable)]
46+
pub enum UpvarCapture<'tcx> {
47+
/// Upvar is captured by value. This is always true when the
48+
/// closure is labeled `move`, but can also be true in other cases
49+
/// depending on inference.
50+
///
51+
/// If the upvar was inferred to be captured by value (e.g. `move`
52+
/// was not used), then the `Span` points to a usage that
53+
/// required it. There may be more than one such usage
54+
/// (e.g. `|| { a; a; }`), in which case we pick an
55+
/// arbitrary one.
56+
ByValue(Option<Span>),
57+
58+
/// Upvar is captured by reference.
59+
ByRef(UpvarBorrow<'tcx>),
60+
}
61+
62+
#[derive(PartialEq, Clone, Copy, TyEncodable, TyDecodable, TypeFoldable, HashStable)]
63+
pub struct UpvarBorrow<'tcx> {
64+
/// The kind of borrow: by-ref upvars have access to shared
65+
/// immutable borrows, which are not part of the normal language
66+
/// syntax.
67+
pub kind: BorrowKind,
68+
69+
/// Region of the resulting reference.
70+
pub region: ty::Region<'tcx>,
71+
}
72+
73+
pub type UpvarListMap = FxHashMap<DefId, FxIndexMap<hir::HirId, UpvarId>>;
74+
pub type UpvarCaptureMap<'tcx> = FxHashMap<UpvarId, UpvarCapture<'tcx>>;
75+
76+
/// Given the closure DefId this map provides a map of root variables to minimum
77+
/// set of `CapturedPlace`s that need to be tracked to support all captures of that closure.
78+
pub type MinCaptureInformationMap<'tcx> = FxHashMap<DefId, RootVariableMinCaptureList<'tcx>>;
79+
80+
/// Part of `MinCaptureInformationMap`; Maps a root variable to the list of `CapturedPlace`.
81+
/// Used to track the minimum set of `Place`s that need to be captured to support all
82+
/// Places captured by the closure starting at a given root variable.
83+
///
84+
/// This provides a convenient and quick way of checking if a variable being used within
85+
/// a closure is a capture of a local variable.
86+
pub type RootVariableMinCaptureList<'tcx> = FxIndexMap<hir::HirId, MinCaptureList<'tcx>>;
87+
88+
/// Part of `MinCaptureInformationMap`; List of `CapturePlace`s.
89+
pub type MinCaptureList<'tcx> = Vec<CapturedPlace<'tcx>>;
90+
91+
/// Represents the various closure traits in the language. This
92+
/// will determine the type of the environment (`self`, in the
93+
/// desugaring) argument that the closure expects.
94+
///
95+
/// You can get the environment type of a closure using
96+
/// `tcx.closure_env_ty()`.
97+
#[derive(Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash, Debug, TyEncodable, TyDecodable)]
98+
#[derive(HashStable)]
99+
pub enum ClosureKind {
100+
// Warning: Ordering is significant here! The ordering is chosen
101+
// because the trait Fn is a subtrait of FnMut and so in turn, and
102+
// hence we order it so that Fn < FnMut < FnOnce.
103+
Fn,
104+
FnMut,
105+
FnOnce,
106+
}
107+
108+
impl<'tcx> ClosureKind {
109+
// This is the initial value used when doing upvar inference.
110+
pub const LATTICE_BOTTOM: ClosureKind = ClosureKind::Fn;
111+
112+
pub fn trait_did(&self, tcx: TyCtxt<'tcx>) -> DefId {
113+
match *self {
114+
ClosureKind::Fn => tcx.require_lang_item(LangItem::Fn, None),
115+
ClosureKind::FnMut => tcx.require_lang_item(LangItem::FnMut, None),
116+
ClosureKind::FnOnce => tcx.require_lang_item(LangItem::FnOnce, None),
117+
}
118+
}
119+
120+
/// Returns `true` if a type that impls this closure kind
121+
/// must also implement `other`.
122+
pub fn extends(self, other: ty::ClosureKind) -> bool {
123+
matches!(
124+
(self, other),
125+
(ClosureKind::Fn, ClosureKind::Fn)
126+
| (ClosureKind::Fn, ClosureKind::FnMut)
127+
| (ClosureKind::Fn, ClosureKind::FnOnce)
128+
| (ClosureKind::FnMut, ClosureKind::FnMut)
129+
| (ClosureKind::FnMut, ClosureKind::FnOnce)
130+
| (ClosureKind::FnOnce, ClosureKind::FnOnce)
131+
)
132+
}
133+
134+
/// Returns the representative scalar type for this closure kind.
135+
/// See `TyS::to_opt_closure_kind` for more details.
136+
pub fn to_ty(self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
137+
match self {
138+
ty::ClosureKind::Fn => tcx.types.i8,
139+
ty::ClosureKind::FnMut => tcx.types.i16,
140+
ty::ClosureKind::FnOnce => tcx.types.i32,
141+
}
142+
}
143+
}
144+
145+
/// A composite describing a `Place` that is captured by a closure.
146+
#[derive(PartialEq, Clone, Debug, TyEncodable, TyDecodable, TypeFoldable, HashStable)]
147+
pub struct CapturedPlace<'tcx> {
148+
/// The `Place` that is captured.
149+
pub place: HirPlace<'tcx>,
150+
151+
/// `CaptureKind` and expression(s) that resulted in such capture of `place`.
152+
pub info: CaptureInfo<'tcx>,
153+
154+
/// Represents if `place` can be mutated or not.
155+
pub mutability: hir::Mutability,
156+
}
157+
158+
impl CapturedPlace<'tcx> {
159+
/// Returns the hir-id of the root variable for the captured place.
160+
/// e.g., if `a.b.c` was captured, would return the hir-id for `a`.
161+
pub fn get_root_variable(&self) -> hir::HirId {
162+
match self.place.base {
163+
HirPlaceBase::Upvar(upvar_id) => upvar_id.var_path.hir_id,
164+
base => bug!("Expected upvar, found={:?}", base),
165+
}
166+
}
167+
}

compiler/rustc_middle/src/ty/mod.rs

+3-97
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ pub use self::IntVarValue::*;
1616
pub use self::Variance::*;
1717
pub use adt::*;
1818
pub use assoc::*;
19+
pub use closure::*;
1920
pub use generics::*;
20-
pub use upvar::*;
2121

2222
use crate::hir::exports::ExportMap;
2323
use crate::hir::place::{
@@ -33,14 +33,13 @@ use crate::ty::util::Discr;
3333
use rustc_ast as ast;
3434
use rustc_attr as attr;
3535
use rustc_data_structures::captures::Captures;
36-
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
36+
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
3737
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
3838
use rustc_data_structures::sync::{self, par_iter, ParallelIterator};
3939
use rustc_data_structures::tagged_ptr::CopyTaggedPtr;
4040
use rustc_hir as hir;
4141
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
4242
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, CRATE_DEF_INDEX};
43-
use rustc_hir::lang_items::LangItem;
4443
use rustc_hir::{Constness, Node};
4544
use rustc_macros::HashStable;
4645
use rustc_span::hygiene::ExpnId;
@@ -103,6 +102,7 @@ pub mod walk;
103102

104103
mod adt;
105104
mod assoc;
105+
mod closure;
106106
mod consts;
107107
mod context;
108108
mod diagnostics;
@@ -112,7 +112,6 @@ mod instance;
112112
mod list;
113113
mod structural_impls;
114114
mod sty;
115-
mod upvar;
116115

117116
// Data types
118117

@@ -405,45 +404,6 @@ pub enum BorrowKind {
405404
MutBorrow,
406405
}
407406

408-
/// Given the closure DefId this map provides a map of root variables to minimum
409-
/// set of `CapturedPlace`s that need to be tracked to support all captures of that closure.
410-
pub type MinCaptureInformationMap<'tcx> = FxHashMap<DefId, RootVariableMinCaptureList<'tcx>>;
411-
412-
/// Part of `MinCaptureInformationMap`; Maps a root variable to the list of `CapturedPlace`.
413-
/// Used to track the minimum set of `Place`s that need to be captured to support all
414-
/// Places captured by the closure starting at a given root variable.
415-
///
416-
/// This provides a convenient and quick way of checking if a variable being used within
417-
/// a closure is a capture of a local variable.
418-
pub type RootVariableMinCaptureList<'tcx> = FxIndexMap<hir::HirId, MinCaptureList<'tcx>>;
419-
420-
/// Part of `MinCaptureInformationMap`; List of `CapturePlace`s.
421-
pub type MinCaptureList<'tcx> = Vec<CapturedPlace<'tcx>>;
422-
423-
/// A composite describing a `Place` that is captured by a closure.
424-
#[derive(PartialEq, Clone, Debug, TyEncodable, TyDecodable, TypeFoldable, HashStable)]
425-
pub struct CapturedPlace<'tcx> {
426-
/// The `Place` that is captured.
427-
pub place: HirPlace<'tcx>,
428-
429-
/// `CaptureKind` and expression(s) that resulted in such capture of `place`.
430-
pub info: CaptureInfo<'tcx>,
431-
432-
/// Represents if `place` can be mutated or not.
433-
pub mutability: hir::Mutability,
434-
}
435-
436-
impl CapturedPlace<'tcx> {
437-
/// Returns the hir-id of the root variable for the captured place.
438-
/// e.g., if `a.b.c` was captured, would return the hir-id for `a`.
439-
pub fn get_root_variable(&self) -> hir::HirId {
440-
match self.place.base {
441-
HirPlaceBase::Upvar(upvar_id) => upvar_id.var_path.hir_id,
442-
base => bug!("Expected upvar, found={:?}", base),
443-
}
444-
}
445-
}
446-
447407
pub fn place_to_string_for_capture(tcx: TyCtxt<'tcx>, place: &HirPlace<'tcx>) -> String {
448408
let name = match place.base {
449409
HirPlaceBase::Upvar(upvar_id) => tcx.hir().name(upvar_id.var_path.hir_id).to_string(),
@@ -1695,60 +1655,6 @@ impl<'tcx> FieldDef {
16951655
}
16961656
}
16971657

1698-
/// Represents the various closure traits in the language. This
1699-
/// will determine the type of the environment (`self`, in the
1700-
/// desugaring) argument that the closure expects.
1701-
///
1702-
/// You can get the environment type of a closure using
1703-
/// `tcx.closure_env_ty()`.
1704-
#[derive(Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash, Debug, TyEncodable, TyDecodable)]
1705-
#[derive(HashStable)]
1706-
pub enum ClosureKind {
1707-
// Warning: Ordering is significant here! The ordering is chosen
1708-
// because the trait Fn is a subtrait of FnMut and so in turn, and
1709-
// hence we order it so that Fn < FnMut < FnOnce.
1710-
Fn,
1711-
FnMut,
1712-
FnOnce,
1713-
}
1714-
1715-
impl<'tcx> ClosureKind {
1716-
// This is the initial value used when doing upvar inference.
1717-
pub const LATTICE_BOTTOM: ClosureKind = ClosureKind::Fn;
1718-
1719-
pub fn trait_did(&self, tcx: TyCtxt<'tcx>) -> DefId {
1720-
match *self {
1721-
ClosureKind::Fn => tcx.require_lang_item(LangItem::Fn, None),
1722-
ClosureKind::FnMut => tcx.require_lang_item(LangItem::FnMut, None),
1723-
ClosureKind::FnOnce => tcx.require_lang_item(LangItem::FnOnce, None),
1724-
}
1725-
}
1726-
1727-
/// Returns `true` if a type that impls this closure kind
1728-
/// must also implement `other`.
1729-
pub fn extends(self, other: ty::ClosureKind) -> bool {
1730-
matches!(
1731-
(self, other),
1732-
(ClosureKind::Fn, ClosureKind::Fn)
1733-
| (ClosureKind::Fn, ClosureKind::FnMut)
1734-
| (ClosureKind::Fn, ClosureKind::FnOnce)
1735-
| (ClosureKind::FnMut, ClosureKind::FnMut)
1736-
| (ClosureKind::FnMut, ClosureKind::FnOnce)
1737-
| (ClosureKind::FnOnce, ClosureKind::FnOnce)
1738-
)
1739-
}
1740-
1741-
/// Returns the representative scalar type for this closure kind.
1742-
/// See `TyS::to_opt_closure_kind` for more details.
1743-
pub fn to_ty(self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
1744-
match self {
1745-
ty::ClosureKind::Fn => tcx.types.i8,
1746-
ty::ClosureKind::FnMut => tcx.types.i16,
1747-
ty::ClosureKind::FnOnce => tcx.types.i32,
1748-
}
1749-
}
1750-
}
1751-
17521658
impl BorrowKind {
17531659
pub fn from_mutbl(m: hir::Mutability) -> BorrowKind {
17541660
match m {

compiler/rustc_middle/src/ty/upvar.rs

-72
This file was deleted.

0 commit comments

Comments
 (0)