Skip to content

Commit 48258ff

Browse files
committed
Remove region from UpvarCapture and move it to CapturedPlace
Region info is completely unnecessary for upvar capture kind computation and is only needed to create the final upvar tuple ty. Doing so makes creation of UpvarCapture very cheap and expose further cleanup opportunity.
1 parent 3698e03 commit 48258ff

File tree

9 files changed

+103
-116
lines changed

9 files changed

+103
-116
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -706,10 +706,9 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
706706
&origin_projection,
707707
) {
708708
match captured_place.info.capture_kind {
709-
ty::UpvarCapture::ByRef(ty::UpvarBorrow {
710-
kind: ty::BorrowKind::MutBorrow | ty::BorrowKind::UniqueImmBorrow,
711-
..
712-
}) => {
709+
ty::UpvarCapture::ByRef(
710+
ty::BorrowKind::MutBorrow | ty::BorrowKind::UniqueImmBorrow,
711+
) => {
713712
capture_reason = format!("mutable borrow of `{}`", upvar);
714713
}
715714
ty::UpvarCapture::ByValue => {

compiler/rustc_middle/src/ty/closure.rs

+9-17
Original file line numberDiff line numberDiff line change
@@ -52,29 +52,18 @@ impl UpvarId {
5252
/// Information describing the capture of an upvar. This is computed
5353
/// during `typeck`, specifically by `regionck`.
5454
#[derive(PartialEq, Clone, Debug, Copy, TyEncodable, TyDecodable, TypeFoldable, HashStable)]
55-
pub enum UpvarCapture<'tcx> {
55+
pub enum UpvarCapture {
5656
/// Upvar is captured by value. This is always true when the
5757
/// closure is labeled `move`, but can also be true in other cases
5858
/// depending on inference.
5959
ByValue,
6060

6161
/// Upvar is captured by reference.
62-
ByRef(UpvarBorrow<'tcx>),
63-
}
64-
65-
#[derive(PartialEq, Clone, Copy, TyEncodable, TyDecodable, TypeFoldable, HashStable)]
66-
pub struct UpvarBorrow<'tcx> {
67-
/// The kind of borrow: by-ref upvars have access to shared
68-
/// immutable borrows, which are not part of the normal language
69-
/// syntax.
70-
pub kind: BorrowKind,
71-
72-
/// Region of the resulting reference.
73-
pub region: ty::Region<'tcx>,
62+
ByRef(BorrowKind),
7463
}
7564

7665
pub type UpvarListMap = FxHashMap<DefId, FxIndexMap<hir::HirId, UpvarId>>;
77-
pub type UpvarCaptureMap<'tcx> = FxHashMap<UpvarId, UpvarCapture<'tcx>>;
66+
pub type UpvarCaptureMap = FxHashMap<UpvarId, UpvarCapture>;
7867

7968
/// Given the closure DefId this map provides a map of root variables to minimum
8069
/// set of `CapturedPlace`s that need to be tracked to support all captures of that closure.
@@ -144,10 +133,13 @@ pub struct CapturedPlace<'tcx> {
144133
pub place: HirPlace<'tcx>,
145134

146135
/// `CaptureKind` and expression(s) that resulted in such capture of `place`.
147-
pub info: CaptureInfo<'tcx>,
136+
pub info: CaptureInfo,
148137

149138
/// Represents if `place` can be mutated or not.
150139
pub mutability: hir::Mutability,
140+
141+
/// Region of the resulting reference if the upvar is captured by ref.
142+
pub region: Option<ty::Region<'tcx>>,
151143
}
152144

153145
impl<'tcx> CapturedPlace<'tcx> {
@@ -281,7 +273,7 @@ pub fn is_ancestor_or_same_capture(
281273
/// for a particular capture as well as identifying the part of the source code
282274
/// that triggered this capture to occur.
283275
#[derive(PartialEq, Clone, Debug, Copy, TyEncodable, TyDecodable, TypeFoldable, HashStable)]
284-
pub struct CaptureInfo<'tcx> {
276+
pub struct CaptureInfo {
285277
/// Expr Id pointing to use that resulted in selecting the current capture kind
286278
///
287279
/// Eg:
@@ -319,7 +311,7 @@ pub struct CaptureInfo<'tcx> {
319311
pub path_expr_id: Option<hir::HirId>,
320312

321313
/// Capture mode that was selected
322-
pub capture_kind: UpvarCapture<'tcx>,
314+
pub capture_kind: UpvarCapture,
323315
}
324316

325317
pub fn place_to_string_for_capture<'tcx>(tcx: TyCtxt<'tcx>, place: &HirPlace<'tcx>) -> String {

compiler/rustc_middle/src/ty/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ pub use self::binding::BindingMode::*;
5656
pub use self::closure::{
5757
is_ancestor_or_same_capture, place_to_string_for_capture, BorrowKind, CaptureInfo,
5858
CapturedPlace, ClosureKind, MinCaptureInformationMap, MinCaptureList,
59-
RootVariableMinCaptureList, UpvarBorrow, UpvarCapture, UpvarCaptureMap, UpvarId, UpvarListMap,
60-
UpvarPath, CAPTURE_STRUCT_LOCAL,
59+
RootVariableMinCaptureList, UpvarCapture, UpvarCaptureMap, UpvarId, UpvarListMap, UpvarPath,
60+
CAPTURE_STRUCT_LOCAL,
6161
};
6262
pub use self::consts::{Const, ConstInt, ConstKind, InferConst, ScalarInt, Unevaluated, ValTree};
6363
pub use self::context::{

compiler/rustc_middle/src/ty/structural_impls.rs

-6
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,6 @@ impl fmt::Debug for ty::UpvarId {
4747
}
4848
}
4949

50-
impl<'tcx> fmt::Debug for ty::UpvarBorrow<'tcx> {
51-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52-
write!(f, "UpvarBorrow({:?}, {:?})", self.kind, self.region)
53-
}
54-
}
55-
5650
impl<'tcx> fmt::Debug for ty::ExistentialTraitRef<'tcx> {
5751
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5852
with_no_trimmed_paths(|| fmt::Display::fmt(self, f))

compiler/rustc_mir_build/src/thir/cx/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,7 @@ impl<'tcx> Cx<'tcx> {
11101110
match upvar_capture {
11111111
ty::UpvarCapture::ByValue => captured_place_expr,
11121112
ty::UpvarCapture::ByRef(upvar_borrow) => {
1113-
let borrow_kind = match upvar_borrow.kind {
1113+
let borrow_kind = match upvar_borrow {
11141114
ty::BorrowKind::ImmBorrow => BorrowKind::Shared,
11151115
ty::BorrowKind::UniqueImmBorrow => BorrowKind::Unique,
11161116
ty::BorrowKind::MutBorrow => BorrowKind::Mut { allow_two_phase_borrow: false },

compiler/rustc_typeck/src/check/regionck.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -859,9 +859,9 @@ impl<'a, 'tcx> RegionCtxt<'a, 'tcx> {
859859
self.sub_regions(
860860
infer::ReborrowUpvar(span, upvar_id),
861861
borrow_region,
862-
upvar_borrow.region,
862+
captured_place.region.unwrap(),
863863
);
864-
if let ty::ImmBorrow = upvar_borrow.kind {
864+
if let ty::ImmBorrow = upvar_borrow {
865865
debug!("link_upvar_region: capture by shared ref");
866866
} else {
867867
all_captures_are_imm_borrow = false;

0 commit comments

Comments
 (0)