From 0a80cc4d83e8f38bbb771e4d7b3493229e1d9c55 Mon Sep 17 00:00:00 2001 From: Paul Trojahn Date: Fri, 21 May 2021 21:01:27 +0200 Subject: [PATCH] Replace Local::new(1) with CAPTURE_STRUCT_LOCAL --- compiler/rustc_middle/src/ty/closure.rs | 6 +++++- .../borrow_check/diagnostics/conflict_errors.rs | 7 ++++--- .../borrow_check/diagnostics/mutability_errors.rs | 15 +++++++-------- .../rustc_mir_build/src/build/expr/as_place.rs | 4 +--- .../rustc_mir_build/src/build/expr/as_rvalue.rs | 2 +- compiler/rustc_mir_build/src/build/mod.rs | 5 ++--- 6 files changed, 20 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_middle/src/ty/closure.rs b/compiler/rustc_middle/src/ty/closure.rs index 7790369af7f..0706a057dd0 100644 --- a/compiler/rustc_middle/src/ty/closure.rs +++ b/compiler/rustc_middle/src/ty/closure.rs @@ -1,7 +1,7 @@ use crate::hir::place::{ Place as HirPlace, PlaceBase as HirPlaceBase, ProjectionKind as HirProjectionKind, }; -use crate::ty; +use crate::{mir, ty}; use rustc_data_structures::fx::{FxHashMap, FxIndexMap}; use rustc_hir as hir; @@ -12,6 +12,10 @@ use super::{Ty, TyCtxt}; use self::BorrowKind::*; +// Captures are represented using fields inside a structure. +// This represents accessing self in the closure structure +pub const CAPTURE_STRUCT_LOCAL: mir::Local = mir::Local::from_u32(1); + #[derive( Clone, Copy, diff --git a/compiler/rustc_mir/src/borrow_check/diagnostics/conflict_errors.rs b/compiler/rustc_mir/src/borrow_check/diagnostics/conflict_errors.rs index 30e0b293ffb..8b0761889b8 100644 --- a/compiler/rustc_mir/src/borrow_check/diagnostics/conflict_errors.rs +++ b/compiler/rustc_mir/src/borrow_check/diagnostics/conflict_errors.rs @@ -4,10 +4,9 @@ use rustc_errors::{Applicability, DiagnosticBuilder}; use rustc_hir as hir; use rustc_hir::def_id::DefId; use rustc_hir::{AsyncGeneratorKind, GeneratorKind}; -use rustc_index::vec::Idx; use rustc_middle::mir::{ self, AggregateKind, BindingForm, BorrowKind, ClearCrossCrate, ConstraintCategory, - FakeReadCause, Local, LocalDecl, LocalInfo, LocalKind, Location, Operand, Place, PlaceRef, + FakeReadCause, LocalDecl, LocalInfo, LocalKind, Location, Operand, Place, PlaceRef, ProjectionElem, Rvalue, Statement, StatementKind, Terminator, TerminatorKind, VarBindingForm, }; use rustc_middle::ty::{self, suggest_constraining_type_param, Ty, TypeFoldable}; @@ -1274,7 +1273,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { bug!("temporary or return pointer with a name") } LocalKind::Var => "local variable ", - LocalKind::Arg if !self.upvars.is_empty() && local == Local::new(1) => { + LocalKind::Arg + if !self.upvars.is_empty() && local == ty::CAPTURE_STRUCT_LOCAL => + { "variable captured by `move` " } LocalKind::Arg => "function parameter ", diff --git a/compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs b/compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs index 1e2714a2c1b..d2b15661047 100644 --- a/compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs +++ b/compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs @@ -1,6 +1,5 @@ use rustc_hir as hir; use rustc_hir::Node; -use rustc_index::vec::Idx; use rustc_middle::hir::map::Map; use rustc_middle::mir::{Mutability, Place, PlaceRef, ProjectionElem}; use rustc_middle::ty::{self, Ty, TyCtxt}; @@ -115,12 +114,14 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { } } PlaceRef { local: _, projection: [proj_base @ .., ProjectionElem::Deref] } => { - if the_place_err.local == Local::new(1) + if the_place_err.local == ty::CAPTURE_STRUCT_LOCAL && proj_base.is_empty() && !self.upvars.is_empty() { item_msg = format!("`{}`", access_place_desc.unwrap()); - debug_assert!(self.body.local_decls[Local::new(1)].ty.is_region_ptr()); + debug_assert!( + self.body.local_decls[ty::CAPTURE_STRUCT_LOCAL].ty.is_region_ptr() + ); debug_assert!(is_closure_or_generator( Place::ty_from( the_place_err.local, @@ -478,11 +479,9 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { } } - PlaceRef { - local, - projection: [ProjectionElem::Deref], - // FIXME document what is this 1 magic number about - } if local == Local::new(1) && !self.upvars.is_empty() => { + PlaceRef { local, projection: [ProjectionElem::Deref] } + if local == ty::CAPTURE_STRUCT_LOCAL && !self.upvars.is_empty() => + { self.expected_fn_found_fn_mut_call(&mut err, span, act); } diff --git a/compiler/rustc_mir_build/src/build/expr/as_place.rs b/compiler/rustc_mir_build/src/build/expr/as_place.rs index 1053890e618..6ae62276e4b 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_place.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_place.rs @@ -209,9 +209,7 @@ fn to_upvars_resolved_place_builder<'a, 'tcx>( match from_builder.base { PlaceBase::Local(_) => Ok(from_builder), PlaceBase::Upvar { var_hir_id, closure_def_id, closure_kind } => { - // Captures are represented using fields inside a structure. - // This represents accessing self in the closure structure - let mut upvar_resolved_place_builder = PlaceBuilder::from(Local::new(1)); + let mut upvar_resolved_place_builder = PlaceBuilder::from(ty::CAPTURE_STRUCT_LOCAL); match closure_kind { ty::ClosureKind::Fn | ty::ClosureKind::FnMut => { upvar_resolved_place_builder = upvar_resolved_place_builder.deref(); diff --git a/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs b/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs index 822fbd91c94..f7adacdb791 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs @@ -433,7 +433,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { } => { // Not in a closure debug_assert!( - local == Local::new(1), + local == ty::CAPTURE_STRUCT_LOCAL, "Expected local to be Local(1), found {:?}", local ); diff --git a/compiler/rustc_mir_build/src/build/mod.rs b/compiler/rustc_mir_build/src/build/mod.rs index f944e5f8f04..963964f404c 100644 --- a/compiler/rustc_mir_build/src/build/mod.rs +++ b/compiler/rustc_mir_build/src/build/mod.rs @@ -896,9 +896,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // the given closure and use the necessary information to create upvar // debuginfo and to fill `self.upvar_mutbls`. if hir_typeck_results.closure_min_captures.get(&fn_def_id).is_some() { - let closure_env_arg = Local::new(1); let mut closure_env_projs = vec![]; - let mut closure_ty = self.local_decls[closure_env_arg].ty; + let mut closure_ty = self.local_decls[ty::CAPTURE_STRUCT_LOCAL].ty; if let ty::Ref(_, ty, _) = closure_ty.kind() { closure_env_projs.push(ProjectionElem::Deref); closure_ty = ty; @@ -944,7 +943,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { name, source_info: SourceInfo::outermost(tcx_hir.span(var_id)), value: VarDebugInfoContents::Place(Place { - local: closure_env_arg, + local: ty::CAPTURE_STRUCT_LOCAL, projection: tcx.intern_place_elems(&projs), }), });