Add comments with examples and tests
This commit is contained in:
parent
fb3b77a8c8
commit
22eaffe71a
32 changed files with 788 additions and 136 deletions
|
@ -248,8 +248,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
let final_tupled_upvars_type = self.tcx.mk_tup(final_upvar_tys.iter());
|
||||
self.demand_suptype(span, substs.tupled_upvars_ty(), final_tupled_upvars_type);
|
||||
|
||||
let fake_reads =
|
||||
delegate.fake_reads.into_iter().map(|(place, cause)| (place, cause)).collect();
|
||||
let fake_reads = delegate
|
||||
.fake_reads
|
||||
.into_iter()
|
||||
.map(|(place, cause, hir_id)| (place, cause, hir_id))
|
||||
.collect();
|
||||
self.typeck_results.borrow_mut().closure_fake_reads.insert(closure_def_id, fake_reads);
|
||||
|
||||
// If we are also inferred the closure kind here,
|
||||
|
@ -1154,7 +1157,7 @@ struct InferBorrowKind<'a, 'tcx> {
|
|||
/// Place { V1, [ProjectionKind::Field(Index=1, Variant=0)] } : CaptureKind { E2, MutableBorrow }
|
||||
/// ```
|
||||
capture_information: InferredCaptureInformation<'tcx>,
|
||||
fake_reads: Vec<(Place<'tcx>, FakeReadCause)>,
|
||||
fake_reads: Vec<(Place<'tcx>, FakeReadCause, hir::HirId)>,
|
||||
}
|
||||
|
||||
impl<'a, 'tcx> InferBorrowKind<'a, 'tcx> {
|
||||
|
@ -1416,9 +1419,9 @@ impl<'a, 'tcx> InferBorrowKind<'a, 'tcx> {
|
|||
}
|
||||
|
||||
impl<'a, 'tcx> euv::Delegate<'tcx> for InferBorrowKind<'a, 'tcx> {
|
||||
fn fake_read(&mut self, place: Place<'tcx>, cause: FakeReadCause) {
|
||||
fn fake_read(&mut self, place: Place<'tcx>, cause: FakeReadCause, diag_expr_id: hir::HirId) {
|
||||
if let PlaceBase::Upvar(_) = place.base {
|
||||
self.fake_reads.push((place, cause));
|
||||
self.fake_reads.push((place, cause, diag_expr_id));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -371,18 +371,18 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
|
|||
fn visit_fake_reads_map(&mut self) {
|
||||
let mut resolved_closure_fake_reads: FxHashMap<
|
||||
DefId,
|
||||
Vec<(HirPlace<'tcx>, FakeReadCause)>,
|
||||
Vec<(HirPlace<'tcx>, FakeReadCause, hir::HirId)>,
|
||||
> = Default::default();
|
||||
for (closure_def_id, fake_reads) in
|
||||
self.fcx.typeck_results.borrow().closure_fake_reads.iter()
|
||||
{
|
||||
let mut resolved_fake_reads = Vec::<(HirPlace<'tcx>, FakeReadCause)>::new();
|
||||
for (place, cause) in fake_reads.iter() {
|
||||
let mut resolved_fake_reads = Vec::<(HirPlace<'tcx>, FakeReadCause, hir::HirId)>::new();
|
||||
for (place, cause, hir_id) in fake_reads.iter() {
|
||||
let locatable =
|
||||
self.tcx().hir().local_def_id_to_hir_id(closure_def_id.expect_local());
|
||||
|
||||
let resolved_fake_read = self.resolve(place.clone(), &locatable);
|
||||
resolved_fake_reads.push((resolved_fake_read, *cause));
|
||||
resolved_fake_reads.push((resolved_fake_read, *cause, *hir_id));
|
||||
}
|
||||
resolved_closure_fake_reads.insert(*closure_def_id, resolved_fake_reads);
|
||||
}
|
||||
|
|
|
@ -7,11 +7,11 @@ pub use self::ConsumeMode::*;
|
|||
// Export these here so that Clippy can use them.
|
||||
pub use rustc_middle::hir::place::{Place, PlaceBase, PlaceWithHirId, Projection};
|
||||
|
||||
use rustc_data_structures::fx::FxIndexMap;
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::def::Res;
|
||||
use rustc_hir::def_id::LocalDefId;
|
||||
use rustc_hir::PatKind;
|
||||
//use rustc_hir::QPath;
|
||||
use rustc_index::vec::Idx;
|
||||
use rustc_infer::infer::InferCtxt;
|
||||
use rustc_middle::hir::place::ProjectionKind;
|
||||
|
@ -54,7 +54,8 @@ pub trait Delegate<'tcx> {
|
|||
// `diag_expr_id` is the id used for diagnostics (see `consume` for more details).
|
||||
fn mutate(&mut self, assignee_place: &PlaceWithHirId<'tcx>, diag_expr_id: hir::HirId);
|
||||
|
||||
fn fake_read(&mut self, place: Place<'tcx>, cause: FakeReadCause);
|
||||
// The `place` should be a fake read because of specified `cause`.
|
||||
fn fake_read(&mut self, place: Place<'tcx>, cause: FakeReadCause, diag_expr_id: hir::HirId);
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||
|
@ -241,20 +242,33 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
|
|||
let ExprUseVisitor { ref mc, body_owner: _, delegate: _ } = *self;
|
||||
let mut needs_to_be_read = false;
|
||||
for arm in arms.iter() {
|
||||
return_if_err!(mc.cat_pattern(discr_place.clone(), &arm.pat, |_place, pat| {
|
||||
return_if_err!(mc.cat_pattern(discr_place.clone(), &arm.pat, |place, pat| {
|
||||
match &pat.kind {
|
||||
PatKind::Binding(_, _, _, opt_sub_pat) => {
|
||||
PatKind::Binding(.., opt_sub_pat) => {
|
||||
// If the opt_sub_pat is None, than the binding does not count as
|
||||
// a wildcard for the purpose of borrowing discr
|
||||
if let None = opt_sub_pat {
|
||||
// a wildcard for the purpose of borrowing discr.
|
||||
if opt_sub_pat.is_none() {
|
||||
needs_to_be_read = true;
|
||||
}
|
||||
}
|
||||
PatKind::TupleStruct(_, _, _)
|
||||
| PatKind::Struct(_, _, _)
|
||||
| PatKind::Lit(_) => {
|
||||
// If the PatKind is a TupleStruct, Struct, or Lit then we want
|
||||
// to borrow discr
|
||||
PatKind::TupleStruct(..)
|
||||
| PatKind::Path(..)
|
||||
| PatKind::Struct(..)
|
||||
| PatKind::Tuple(..) => {
|
||||
// If the PatKind is a TupleStruct, Struct or Tuple then we want to check
|
||||
// whether the Variant is a MultiVariant or a SingleVariant. We only want
|
||||
// to borrow discr if it is a MultiVariant.
|
||||
// If it is a SingleVariant and creates a binding we will handle that when
|
||||
// this callback gets called again.
|
||||
if let ty::Adt(def, _) = place.place.base_ty.kind() {
|
||||
if def.variants.len() > 1 {
|
||||
needs_to_be_read = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
PatKind::Lit(_) => {
|
||||
// If the PatKind is a Lit then we want
|
||||
// to borrow discr.
|
||||
needs_to_be_read = true;
|
||||
}
|
||||
_ => {}
|
||||
|
@ -264,6 +278,16 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
|
|||
|
||||
if needs_to_be_read {
|
||||
self.borrow_expr(&discr, ty::ImmBorrow);
|
||||
} else {
|
||||
self.delegate.fake_read(
|
||||
discr_place.place.clone(),
|
||||
FakeReadCause::ForMatchedPlace,
|
||||
discr_place.hir_id,
|
||||
);
|
||||
|
||||
// We always want to walk the discriminant. We want to make sure, for instance,
|
||||
// that the discriminant has been initialized.
|
||||
self.walk_expr(&discr);
|
||||
}
|
||||
|
||||
// treatment of the discriminant is handled while walking the arms.
|
||||
|
@ -553,7 +577,11 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
|
|||
}
|
||||
|
||||
fn walk_arm(&mut self, discr_place: &PlaceWithHirId<'tcx>, arm: &hir::Arm<'_>) {
|
||||
self.delegate.fake_read(discr_place.place.clone(), FakeReadCause::ForMatchedPlace);
|
||||
self.delegate.fake_read(
|
||||
discr_place.place.clone(),
|
||||
FakeReadCause::ForMatchedPlace,
|
||||
discr_place.hir_id,
|
||||
);
|
||||
self.walk_pat(discr_place, &arm.pat);
|
||||
|
||||
if let Some(hir::Guard::If(ref e)) = arm.guard {
|
||||
|
@ -566,7 +594,11 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
|
|||
/// Walks a pat that occurs in isolation (i.e., top-level of fn argument or
|
||||
/// let binding, and *not* a match arm or nested pat.)
|
||||
fn walk_irrefutable_pat(&mut self, discr_place: &PlaceWithHirId<'tcx>, pat: &hir::Pat<'_>) {
|
||||
self.delegate.fake_read(discr_place.place.clone(), FakeReadCause::ForLet);
|
||||
self.delegate.fake_read(
|
||||
discr_place.place.clone(),
|
||||
FakeReadCause::ForLet,
|
||||
discr_place.hir_id,
|
||||
);
|
||||
self.walk_pat(discr_place, pat);
|
||||
}
|
||||
|
||||
|
@ -634,6 +666,14 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
|
|||
/// - When reporting the Place back to the Delegate, ensure that the UpvarId uses the enclosing
|
||||
/// closure as the DefId.
|
||||
fn walk_captures(&mut self, closure_expr: &hir::Expr<'_>) {
|
||||
fn upvar_is_local_variable(
|
||||
upvars: Option<&'tcx FxIndexMap<hir::HirId, hir::Upvar>>,
|
||||
upvar_id: &hir::HirId,
|
||||
body_owner_is_closure: bool,
|
||||
) -> bool {
|
||||
upvars.map(|upvars| !upvars.contains_key(upvar_id)).unwrap_or(body_owner_is_closure)
|
||||
}
|
||||
|
||||
debug!("walk_captures({:?})", closure_expr);
|
||||
|
||||
let closure_def_id = self.tcx().hir().local_def_id(closure_expr.hir_id).to_def_id();
|
||||
|
@ -645,16 +685,32 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
|
|||
ty::Closure(..) | ty::Generator(..)
|
||||
);
|
||||
|
||||
// If we have a nested closure, we want to include the fake reads present in the nested closure.
|
||||
if let Some(fake_reads) = self.mc.typeck_results.closure_fake_reads.get(&closure_def_id) {
|
||||
for (fake_read, cause) in fake_reads.iter() {
|
||||
for (fake_read, cause, hir_id) in fake_reads.iter() {
|
||||
match fake_read.base {
|
||||
PlaceBase::Upvar(upvar_id) => {
|
||||
if upvars.map_or(body_owner_is_closure, |upvars| {
|
||||
!upvars.contains_key(&upvar_id.var_path.hir_id)
|
||||
}) {
|
||||
if upvar_is_local_variable(
|
||||
upvars,
|
||||
&upvar_id.var_path.hir_id,
|
||||
body_owner_is_closure,
|
||||
) {
|
||||
// The nested closure might be fake reading the current (enclosing) closure's local variables.
|
||||
// We check if the root variable is ever mentioned within the enclosing closure, if not
|
||||
// then for the current body (if it's a closure) these do not require fake_read, we will ignore them.
|
||||
// The only places we want to fake read before creating the parent closure are the ones that
|
||||
// are not local to it/ defined by it.
|
||||
//
|
||||
// ```rust,ignore(cannot-test-this-because-pseduo-code)
|
||||
// let v1 = (0, 1);
|
||||
// let c = || { // fake reads: v1
|
||||
// let v2 = (0, 1);
|
||||
// let e = || { // fake reads: v1, v2
|
||||
// let (_, t1) = v1;
|
||||
// let (_, t2) = v2;
|
||||
// }
|
||||
// }
|
||||
// ```
|
||||
// This check is performed when visiting the body of the outermost closure (`c`) and ensures
|
||||
// that we don't add a fake read of v2 in c.
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
@ -665,7 +721,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
|
|||
);
|
||||
}
|
||||
};
|
||||
self.delegate.fake_read(fake_read.clone(), *cause);
|
||||
self.delegate.fake_read(fake_read.clone(), *cause, *hir_id);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue