Use precise places when lowering Closures in THIR
- Closures now use closure_min_captures to figure out captured paths - Build upvar_mutbls using closure_min_captures - Change logic in limit_capture_mutability to differentiate b/w capturing parent's local variable or capturing a variable that is captured by the parent (in case of nested closure) using PlaceBase. Co-authored-by: Roxane Fruytier <roxane.fruytier@hotmail.com>
This commit is contained in:
@@ -18,7 +18,7 @@ use rustc_index::vec::Idx;
|
||||
|
||||
/// The "outermost" place that holds this value.
|
||||
#[derive(Copy, Clone)]
|
||||
pub enum PlaceBase {
|
||||
crate enum PlaceBase {
|
||||
/// Denotes the start of a `Place`.
|
||||
Local(Local),
|
||||
|
||||
@@ -67,7 +67,7 @@ pub enum PlaceBase {
|
||||
/// This is used internally when building a place for an expression like `a.b.c`. The fields `b`
|
||||
/// and `c` can be progressively pushed onto the place builder that is created when converting `a`.
|
||||
#[derive(Clone)]
|
||||
struct PlaceBuilder<'tcx> {
|
||||
crate struct PlaceBuilder<'tcx> {
|
||||
base: PlaceBase,
|
||||
projection: Vec<PlaceElem<'tcx>>,
|
||||
}
|
||||
@@ -279,7 +279,7 @@ fn to_upvars_resolved_place_builder<'a, 'tcx>(
|
||||
}
|
||||
|
||||
impl<'tcx> PlaceBuilder<'tcx> {
|
||||
fn into_place<'a>(
|
||||
crate fn into_place<'a>(
|
||||
self,
|
||||
tcx: TyCtxt<'tcx>,
|
||||
typeck_results: &'a ty::TypeckResults<'tcx>,
|
||||
@@ -299,6 +299,10 @@ impl<'tcx> PlaceBuilder<'tcx> {
|
||||
to_upvars_resolved_place_builder(self, tcx, typeck_results).unwrap()
|
||||
}
|
||||
|
||||
crate fn base(&self) -> PlaceBase {
|
||||
self.base
|
||||
}
|
||||
|
||||
fn field(self, f: Field, ty: Ty<'tcx>) -> Self {
|
||||
self.project(PlaceElem::Field(f, ty))
|
||||
}
|
||||
@@ -352,7 +356,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||
|
||||
/// This is used when constructing a compound `Place`, so that we can avoid creating
|
||||
/// intermediate `Place` values until we know the full set of projections.
|
||||
fn as_place_builder<M>(&mut self, block: BasicBlock, expr: M) -> BlockAnd<PlaceBuilder<'tcx>>
|
||||
crate fn as_place_builder<M>(&mut self, block: BasicBlock, expr: M) -> BlockAnd<PlaceBuilder<'tcx>>
|
||||
where
|
||||
M: Mirror<'tcx, Output = Expr<'tcx>>,
|
||||
{
|
||||
|
||||
@@ -4,6 +4,7 @@ use rustc_index::vec::Idx;
|
||||
|
||||
use crate::build::expr::category::{Category, RvalueFunc};
|
||||
use crate::build::{BlockAnd, BlockAndExtension, Builder};
|
||||
use crate::build::expr::as_place::PlaceBase;
|
||||
use crate::thir::*;
|
||||
use rustc_middle::middle::region;
|
||||
use rustc_middle::mir::AssertKind;
|
||||
@@ -393,44 +394,43 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||
|
||||
this.cfg.push(block, Statement { source_info, kind: StatementKind::StorageLive(temp) });
|
||||
|
||||
let arg_place = unpack!(block = this.as_place(block, arg));
|
||||
let arg_place_builder = unpack!(block = this.as_place_builder(block, arg));
|
||||
|
||||
let mutability = match arg_place.as_ref() {
|
||||
PlaceRef { local, projection: &[] } => this.local_decls[local].mutability,
|
||||
PlaceRef { local, projection: &[ProjectionElem::Deref] } => {
|
||||
debug_assert!(
|
||||
this.local_decls[local].is_ref_for_guard(),
|
||||
"Unexpected capture place",
|
||||
);
|
||||
this.local_decls[local].mutability
|
||||
}
|
||||
PlaceRef {
|
||||
local,
|
||||
projection: &[ref proj_base @ .., ProjectionElem::Field(upvar_index, _)],
|
||||
}
|
||||
| PlaceRef {
|
||||
local,
|
||||
projection:
|
||||
&[ref proj_base @ .., ProjectionElem::Field(upvar_index, _), ProjectionElem::Deref],
|
||||
} => {
|
||||
let place = PlaceRef { local, projection: proj_base };
|
||||
let mutability = match arg_place_builder.base() {
|
||||
// We are capturing a path that starts off a local variable in the parent.
|
||||
// The mutability of the current capture is same as the mutability
|
||||
// of the local declaration in the parent.
|
||||
PlaceBase::Local(local) => this.local_decls[local].mutability,
|
||||
// Parent is a closure and we are capturing a path that is captured
|
||||
// by the parent itself. The mutability of the current capture
|
||||
// is same as that of the capture in the parent closure.
|
||||
PlaceBase::Upvar { .. } => {
|
||||
let enclosing_upvars_resolved = arg_place_builder.clone().into_place(
|
||||
this.hir.tcx(),
|
||||
this.hir.typeck_results());
|
||||
|
||||
// Not projected from the implicit `self` in a closure.
|
||||
debug_assert!(
|
||||
match place.local_or_deref_local() {
|
||||
Some(local) => local == Local::new(1),
|
||||
None => false,
|
||||
},
|
||||
"Unexpected capture place"
|
||||
);
|
||||
// Not in a closure
|
||||
debug_assert!(
|
||||
this.upvar_mutbls.len() > upvar_index.index(),
|
||||
"Unexpected capture place"
|
||||
);
|
||||
this.upvar_mutbls[upvar_index.index()]
|
||||
match enclosing_upvars_resolved.as_ref() {
|
||||
PlaceRef { local, projection: &[ProjectionElem::Field(upvar_index, _), ..] }
|
||||
| PlaceRef {
|
||||
local,
|
||||
projection: &[ProjectionElem::Deref, ProjectionElem::Field(upvar_index, _), ..] } => {
|
||||
// Not in a closure
|
||||
debug_assert!(
|
||||
local == Local::new(1),
|
||||
"Expected local to be Local(1), found {:?}",
|
||||
local
|
||||
);
|
||||
// Not in a closure
|
||||
debug_assert!(
|
||||
this.upvar_mutbls.len() > upvar_index.index(),
|
||||
"Unexpected capture place, upvar_mutbls={:#?}, upvar_index={:?}",
|
||||
this.upvar_mutbls, upvar_index
|
||||
);
|
||||
this.upvar_mutbls[upvar_index.index()]
|
||||
}
|
||||
_ => bug!("Unexpected capture place"),
|
||||
}
|
||||
}
|
||||
_ => bug!("Unexpected capture place"),
|
||||
};
|
||||
|
||||
let borrow_kind = match mutability {
|
||||
@@ -438,6 +438,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||
Mutability::Mut => BorrowKind::Mut { allow_two_phase_borrow: false },
|
||||
};
|
||||
|
||||
let arg_place = arg_place_builder.into_place(
|
||||
this.hir.tcx(),
|
||||
this.hir.typeck_results());
|
||||
|
||||
this.cfg.push_assign(
|
||||
block,
|
||||
source_info,
|
||||
|
||||
@@ -10,6 +10,7 @@ use rustc_hir::lang_items::LangItem;
|
||||
use rustc_hir::{GeneratorKind, HirIdMap, Node};
|
||||
use rustc_index::vec::{Idx, IndexVec};
|
||||
use rustc_infer::infer::TyCtxtInferExt;
|
||||
use rustc_middle::hir::place::PlaceBase as HirPlaceBase;
|
||||
use rustc_middle::middle::region;
|
||||
use rustc_middle::mir::*;
|
||||
use rustc_middle::ty::subst::Subst;
|
||||
@@ -823,7 +824,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||
// with the closure's DefId. Here, we run through that vec of UpvarIds for
|
||||
// the given closure and use the necessary information to create upvar
|
||||
// debuginfo and to fill `self.upvar_mutbls`.
|
||||
if let Some(upvars) = hir_typeck_results.closure_captures.get(&fn_def_id) {
|
||||
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;
|
||||
@@ -836,14 +837,23 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||
ty::Generator(_, substs, _) => ty::UpvarSubsts::Generator(substs),
|
||||
_ => span_bug!(self.fn_span, "upvars with non-closure env ty {:?}", closure_ty),
|
||||
};
|
||||
let upvar_tys = upvar_substs.upvar_tys();
|
||||
let upvars_with_tys = upvars.iter().zip(upvar_tys);
|
||||
self.upvar_mutbls = upvars_with_tys
|
||||
let capture_tys = upvar_substs.upvar_tys();
|
||||
let captures_with_tys = hir_typeck_results
|
||||
.closure_min_captures_flattened(fn_def_id)
|
||||
.zip(capture_tys);
|
||||
|
||||
self.upvar_mutbls = captures_with_tys
|
||||
.enumerate()
|
||||
.map(|(i, ((&var_id, &upvar_id), ty))| {
|
||||
let capture = hir_typeck_results.upvar_capture(upvar_id);
|
||||
.map(|(i, (captured_place, ty))| {
|
||||
let capture = captured_place.info.capture_kind;
|
||||
let var_id = match captured_place.place.base {
|
||||
HirPlaceBase::Upvar(upvar_id) => upvar_id.var_path.hir_id,
|
||||
_ => bug!("Expected an upvar")
|
||||
};
|
||||
|
||||
let mut mutability = Mutability::Not;
|
||||
|
||||
// FIXME(project-rfc-2229#8): Store more precise information
|
||||
let mut name = kw::Invalid;
|
||||
if let Some(Node::Binding(pat)) = tcx_hir.find(var_id) {
|
||||
if let hir::PatKind::Binding(_, _, ident, _) = pat.kind {
|
||||
|
||||
Reference in New Issue
Block a user