Skip to content

Commit 22e491a

Browse files
committed
Auto merge of #89861 - nbdd0121:closure, r=wesleywiser
Closure capture cleanup & refactor Follow up of #89648 Each commit is self-contained and the rationale/changes are documented in the commit message, so it's advisable to review commit by commit. The code is significantly cleaner (at least IMO), but that could have some perf implication, so I'd suggest a perf run. r? `@wesleywiser` cc `@arora-aman`
2 parents 256721e + c84cea9 commit 22e491a

File tree

23 files changed

+323
-418
lines changed

23 files changed

+323
-418
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -706,13 +706,12 @@ 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
}
715-
ty::UpvarCapture::ByValue(_) => {
714+
ty::UpvarCapture::ByValue => {
716715
capture_reason = format!("possible mutation of `{}`", upvar);
717716
}
718717
_ => bug!("upvar `{}` borrowed, but not mutably", upvar),

compiler/rustc_borrowck/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ fn do_mir_borrowck<'a, 'tcx>(
186186
.map(|captured_place| {
187187
let capture = captured_place.info.capture_kind;
188188
let by_ref = match capture {
189-
ty::UpvarCapture::ByValue(_) => false,
189+
ty::UpvarCapture::ByValue => false,
190190
ty::UpvarCapture::ByRef(..) => true,
191191
};
192192
Upvar { place: captured_place.clone(), by_ref }

compiler/rustc_middle/src/ty/closure.rs

+10-24
Original file line numberDiff line numberDiff line change
@@ -52,35 +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.
59-
///
60-
/// If the upvar was inferred to be captured by value (e.g. `move`
61-
/// was not used), then the `Span` points to a usage that
62-
/// required it. There may be more than one such usage
63-
/// (e.g. `|| { a; a; }`), in which case we pick an
64-
/// arbitrary one.
65-
ByValue(Option<Span>),
59+
ByValue,
6660

6761
/// Upvar is captured by reference.
68-
ByRef(UpvarBorrow<'tcx>),
69-
}
70-
71-
#[derive(PartialEq, Clone, Copy, TyEncodable, TyDecodable, TypeFoldable, HashStable)]
72-
pub struct UpvarBorrow<'tcx> {
73-
/// The kind of borrow: by-ref upvars have access to shared
74-
/// immutable borrows, which are not part of the normal language
75-
/// syntax.
76-
pub kind: BorrowKind,
77-
78-
/// Region of the resulting reference.
79-
pub region: ty::Region<'tcx>,
62+
ByRef(BorrowKind),
8063
}
8164

8265
pub type UpvarListMap = FxHashMap<DefId, FxIndexMap<hir::HirId, UpvarId>>;
83-
pub type UpvarCaptureMap<'tcx> = FxHashMap<UpvarId, UpvarCapture<'tcx>>;
66+
pub type UpvarCaptureMap = FxHashMap<UpvarId, UpvarCapture>;
8467

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

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

155138
/// Represents if `place` can be mutated or not.
156139
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>>,
157143
}
158144

159145
impl<'tcx> CapturedPlace<'tcx> {
@@ -287,7 +273,7 @@ pub fn is_ancestor_or_same_capture(
287273
/// for a particular capture as well as identifying the part of the source code
288274
/// that triggered this capture to occur.
289275
#[derive(PartialEq, Clone, Debug, Copy, TyEncodable, TyDecodable, TypeFoldable, HashStable)]
290-
pub struct CaptureInfo<'tcx> {
276+
pub struct CaptureInfo {
291277
/// Expr Id pointing to use that resulted in selecting the current capture kind
292278
///
293279
/// Eg:
@@ -325,7 +311,7 @@ pub struct CaptureInfo<'tcx> {
325311
pub path_expr_id: Option<hir::HirId>,
326312

327313
/// Capture mode that was selected
328-
pub capture_kind: UpvarCapture<'tcx>,
314+
pub capture_kind: UpvarCapture,
329315
}
330316

331317
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
@@ -57,8 +57,8 @@ pub use self::binding::BindingMode::*;
5757
pub use self::closure::{
5858
is_ancestor_or_same_capture, place_to_string_for_capture, BorrowKind, CaptureInfo,
5959
CapturedPlace, ClosureKind, MinCaptureInformationMap, MinCaptureList,
60-
RootVariableMinCaptureList, UpvarBorrow, UpvarCapture, UpvarCaptureMap, UpvarId, UpvarListMap,
61-
UpvarPath, CAPTURE_STRUCT_LOCAL,
60+
RootVariableMinCaptureList, UpvarCapture, UpvarCaptureMap, UpvarId, UpvarListMap, UpvarPath,
61+
CAPTURE_STRUCT_LOCAL,
6262
};
6363
pub use self::consts::{Const, ConstInt, ConstKind, InferConst, ScalarInt, Unevaluated, ValTree};
6464
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/build/expr/as_place.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ fn to_upvars_resolved_place_builder<'a, 'tcx>(
266266
// we need to deref it
267267
upvar_resolved_place_builder = match capture.info.capture_kind {
268268
ty::UpvarCapture::ByRef(_) => upvar_resolved_place_builder.deref(),
269-
ty::UpvarCapture::ByValue(_) => upvar_resolved_place_builder,
269+
ty::UpvarCapture::ByValue => upvar_resolved_place_builder,
270270
};
271271

272272
let next_projection = capture.place.projections.len();

compiler/rustc_mir_build/src/build/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
930930
let mut projs = closure_env_projs.clone();
931931
projs.push(ProjectionElem::Field(Field::new(i), ty));
932932
match capture {
933-
ty::UpvarCapture::ByValue(_) => {}
933+
ty::UpvarCapture::ByValue => {}
934934
ty::UpvarCapture::ByRef(..) => {
935935
projs.push(ProjectionElem::Deref);
936936
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1108,9 +1108,9 @@ impl<'tcx> Cx<'tcx> {
11081108
let temp_lifetime = self.region_scope_tree.temporary_scope(closure_expr.hir_id.local_id);
11091109

11101110
match upvar_capture {
1111-
ty::UpvarCapture::ByValue(_) => captured_place_expr,
1111+
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_passes/src/liveness.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
726726
);
727727
self.acc(self.exit_ln, var, ACC_READ | ACC_USE);
728728
}
729-
ty::UpvarCapture::ByValue(_) => {}
729+
ty::UpvarCapture::ByValue => {}
730730
}
731731
}
732732
}
@@ -1481,7 +1481,7 @@ impl<'tcx> Liveness<'_, 'tcx> {
14811481
for (&var_hir_id, min_capture_list) in closure_min_captures {
14821482
for captured_place in min_capture_list {
14831483
match captured_place.info.capture_kind {
1484-
ty::UpvarCapture::ByValue(_) => {}
1484+
ty::UpvarCapture::ByValue => {}
14851485
ty::UpvarCapture::ByRef(..) => continue,
14861486
};
14871487
let span = captured_place.get_capture_kind_span(self.ir.tcx);

compiler/rustc_typeck/src/check/regionck.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -859,15 +859,15 @@ 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;
868868
}
869869
}
870-
ty::UpvarCapture::ByValue(_) => {
870+
ty::UpvarCapture::ByValue => {
871871
all_captures_are_imm_borrow = false;
872872
}
873873
}

0 commit comments

Comments
 (0)