Skip to content

Commit ad72247

Browse files
Rollup merge of #85605 - ptrojahn:closure_struct, r=matthewjasper
Replace Local::new(1) with CAPTURE_STRUCT_LOCAL
2 parents 6b0b81b + 0a80cc4 commit ad72247

File tree

6 files changed

+20
-19
lines changed

6 files changed

+20
-19
lines changed

compiler/rustc_middle/src/ty/closure.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::hir::place::{
22
Place as HirPlace, PlaceBase as HirPlaceBase, ProjectionKind as HirProjectionKind,
33
};
4-
use crate::ty;
4+
use crate::{mir, ty};
55

66
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
77
use rustc_hir as hir;
@@ -12,6 +12,10 @@ use super::{Ty, TyCtxt};
1212

1313
use self::BorrowKind::*;
1414

15+
// Captures are represented using fields inside a structure.
16+
// This represents accessing self in the closure structure
17+
pub const CAPTURE_STRUCT_LOCAL: mir::Local = mir::Local::from_u32(1);
18+
1519
#[derive(
1620
Clone,
1721
Copy,

compiler/rustc_mir/src/borrow_check/diagnostics/conflict_errors.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ use rustc_errors::{Applicability, DiagnosticBuilder};
44
use rustc_hir as hir;
55
use rustc_hir::def_id::DefId;
66
use rustc_hir::{AsyncGeneratorKind, GeneratorKind};
7-
use rustc_index::vec::Idx;
87
use rustc_middle::mir::{
98
self, AggregateKind, BindingForm, BorrowKind, ClearCrossCrate, ConstraintCategory,
10-
FakeReadCause, Local, LocalDecl, LocalInfo, LocalKind, Location, Operand, Place, PlaceRef,
9+
FakeReadCause, LocalDecl, LocalInfo, LocalKind, Location, Operand, Place, PlaceRef,
1110
ProjectionElem, Rvalue, Statement, StatementKind, Terminator, TerminatorKind, VarBindingForm,
1211
};
1312
use rustc_middle::ty::{self, suggest_constraining_type_param, Ty, TypeFoldable};
@@ -1274,7 +1273,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
12741273
bug!("temporary or return pointer with a name")
12751274
}
12761275
LocalKind::Var => "local variable ",
1277-
LocalKind::Arg if !self.upvars.is_empty() && local == Local::new(1) => {
1276+
LocalKind::Arg
1277+
if !self.upvars.is_empty() && local == ty::CAPTURE_STRUCT_LOCAL =>
1278+
{
12781279
"variable captured by `move` "
12791280
}
12801281
LocalKind::Arg => "function parameter ",

compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use rustc_hir as hir;
22
use rustc_hir::Node;
3-
use rustc_index::vec::Idx;
43
use rustc_middle::hir::map::Map;
54
use rustc_middle::mir::{Mutability, Place, PlaceRef, ProjectionElem};
65
use rustc_middle::ty::{self, Ty, TyCtxt};
@@ -115,12 +114,14 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
115114
}
116115
}
117116
PlaceRef { local: _, projection: [proj_base @ .., ProjectionElem::Deref] } => {
118-
if the_place_err.local == Local::new(1)
117+
if the_place_err.local == ty::CAPTURE_STRUCT_LOCAL
119118
&& proj_base.is_empty()
120119
&& !self.upvars.is_empty()
121120
{
122121
item_msg = format!("`{}`", access_place_desc.unwrap());
123-
debug_assert!(self.body.local_decls[Local::new(1)].ty.is_region_ptr());
122+
debug_assert!(
123+
self.body.local_decls[ty::CAPTURE_STRUCT_LOCAL].ty.is_region_ptr()
124+
);
124125
debug_assert!(is_closure_or_generator(
125126
Place::ty_from(
126127
the_place_err.local,
@@ -478,11 +479,9 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
478479
}
479480
}
480481

481-
PlaceRef {
482-
local,
483-
projection: [ProjectionElem::Deref],
484-
// FIXME document what is this 1 magic number about
485-
} if local == Local::new(1) && !self.upvars.is_empty() => {
482+
PlaceRef { local, projection: [ProjectionElem::Deref] }
483+
if local == ty::CAPTURE_STRUCT_LOCAL && !self.upvars.is_empty() =>
484+
{
486485
self.expected_fn_found_fn_mut_call(&mut err, span, act);
487486
}
488487

compiler/rustc_mir_build/src/build/expr/as_place.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,7 @@ fn to_upvars_resolved_place_builder<'a, 'tcx>(
209209
match from_builder.base {
210210
PlaceBase::Local(_) => Ok(from_builder),
211211
PlaceBase::Upvar { var_hir_id, closure_def_id, closure_kind } => {
212-
// Captures are represented using fields inside a structure.
213-
// This represents accessing self in the closure structure
214-
let mut upvar_resolved_place_builder = PlaceBuilder::from(Local::new(1));
212+
let mut upvar_resolved_place_builder = PlaceBuilder::from(ty::CAPTURE_STRUCT_LOCAL);
215213
match closure_kind {
216214
ty::ClosureKind::Fn | ty::ClosureKind::FnMut => {
217215
upvar_resolved_place_builder = upvar_resolved_place_builder.deref();

compiler/rustc_mir_build/src/build/expr/as_rvalue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
446446
} => {
447447
// Not in a closure
448448
debug_assert!(
449-
local == Local::new(1),
449+
local == ty::CAPTURE_STRUCT_LOCAL,
450450
"Expected local to be Local(1), found {:?}",
451451
local
452452
);

compiler/rustc_mir_build/src/build/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -953,9 +953,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
953953
// the given closure and use the necessary information to create upvar
954954
// debuginfo and to fill `self.upvar_mutbls`.
955955
if hir_typeck_results.closure_min_captures.get(&fn_def_id).is_some() {
956-
let closure_env_arg = Local::new(1);
957956
let mut closure_env_projs = vec![];
958-
let mut closure_ty = self.local_decls[closure_env_arg].ty;
957+
let mut closure_ty = self.local_decls[ty::CAPTURE_STRUCT_LOCAL].ty;
959958
if let ty::Ref(_, ty, _) = closure_ty.kind() {
960959
closure_env_projs.push(ProjectionElem::Deref);
961960
closure_ty = ty;
@@ -1001,7 +1000,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
10011000
name,
10021001
source_info: SourceInfo::outermost(tcx_hir.span(var_id)),
10031002
value: VarDebugInfoContents::Place(Place {
1004-
local: closure_env_arg,
1003+
local: ty::CAPTURE_STRUCT_LOCAL,
10051004
projection: tcx.intern_place_elems(&projs),
10061005
}),
10071006
});

0 commit comments

Comments
 (0)