Skip to content

Commit f5d8117

Browse files
committed
Auto merge of #82536 - sexxi-goose:handle-patterns-take-2, r=nikomatsakis
2229: Handle patterns within closures correctly when `capture_disjoint_fields` is enabled This PR fixes several issues related to handling patterns within closures when `capture_disjoint_fields` is enabled. 1. Matching is always considered a use of the place, even with `_` patterns 2. Compiler ICE when capturing fields in closures through `let` assignments To do so, we - Introduced new Fake Reads - Delayed use of `Place` in favor of `PlaceBuilder` - Ensured that `PlaceBuilder` can be resolved before attempting to extract `Place` in any of the pattern matching code Closes rust-lang/project-rfc-2229/issues/27 Closes rust-lang/project-rfc-2229/issues/24 r? `@nikomatsakis`
2 parents 1d6754d + 189d206 commit f5d8117

40 files changed

+1473
-127
lines changed

compiler/rustc_middle/src/ty/context.rs

+28
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ use rustc_hir::{
4747
};
4848
use rustc_index::vec::{Idx, IndexVec};
4949
use rustc_macros::HashStable;
50+
use rustc_middle::mir::FakeReadCause;
5051
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
5152
use rustc_session::config::{BorrowckMode, CrateType, OutputFilenames};
5253
use rustc_session::lint::{Level, Lint};
@@ -430,6 +431,30 @@ pub struct TypeckResults<'tcx> {
430431
/// see `MinCaptureInformationMap` for more details.
431432
pub closure_min_captures: ty::MinCaptureInformationMap<'tcx>,
432433

434+
/// Tracks the fake reads required for a closure and the reason for the fake read.
435+
/// When performing pattern matching for closures, there are times we don't end up
436+
/// reading places that are mentioned in a closure (because of _ patterns). However,
437+
/// to ensure the places are initialized, we introduce fake reads.
438+
/// Consider these two examples:
439+
/// ``` (discriminant matching with only wildcard arm)
440+
/// let x: u8;
441+
/// let c = || match x { _ => () };
442+
/// ```
443+
/// In this example, we don't need to actually read/borrow `x` in `c`, and so we don't
444+
/// want to capture it. However, we do still want an error here, because `x` should have
445+
/// to be initialized at the point where c is created. Therefore, we add a "fake read"
446+
/// instead.
447+
/// ``` (destructured assignments)
448+
/// let c = || {
449+
/// let (t1, t2) = t;
450+
/// }
451+
/// ```
452+
/// In the second example, we capture the disjoint fields of `t` (`t.0` & `t.1`), but
453+
/// we never capture `t`. This becomes an issue when we build MIR as we require
454+
/// information on `t` in order to create place `t.0` and `t.1`. We can solve this
455+
/// issue by fake reading `t`.
456+
pub closure_fake_reads: FxHashMap<DefId, Vec<(HirPlace<'tcx>, FakeReadCause, hir::HirId)>>,
457+
433458
/// Stores the type, expression, span and optional scope span of all types
434459
/// that are live across the yield of this generator (if a generator).
435460
pub generator_interior_types: ty::Binder<Vec<GeneratorInteriorTypeCause<'tcx>>>,
@@ -464,6 +489,7 @@ impl<'tcx> TypeckResults<'tcx> {
464489
concrete_opaque_types: Default::default(),
465490
closure_captures: Default::default(),
466491
closure_min_captures: Default::default(),
492+
closure_fake_reads: Default::default(),
467493
generator_interior_types: ty::Binder::dummy(Default::default()),
468494
treat_byte_string_as_slice: Default::default(),
469495
}
@@ -715,6 +741,7 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for TypeckResults<'tcx> {
715741
ref concrete_opaque_types,
716742
ref closure_captures,
717743
ref closure_min_captures,
744+
ref closure_fake_reads,
718745
ref generator_interior_types,
719746
ref treat_byte_string_as_slice,
720747
} = *self;
@@ -750,6 +777,7 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for TypeckResults<'tcx> {
750777
concrete_opaque_types.hash_stable(hcx, hasher);
751778
closure_captures.hash_stable(hcx, hasher);
752779
closure_min_captures.hash_stable(hcx, hasher);
780+
closure_fake_reads.hash_stable(hcx, hasher);
753781
generator_interior_types.hash_stable(hcx, hasher);
754782
treat_byte_string_as_slice.hash_stable(hcx, hasher);
755783
})

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

+54-26
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ use rustc_middle::hir::place::ProjectionKind as HirProjectionKind;
1010
use rustc_middle::middle::region;
1111
use rustc_middle::mir::AssertKind::BoundsCheck;
1212
use rustc_middle::mir::*;
13+
use rustc_middle::ty::AdtDef;
1314
use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty, TyCtxt, Variance};
1415
use rustc_span::Span;
1516
use rustc_target::abi::VariantIdx;
1617

1718
use rustc_index::vec::Idx;
1819

1920
/// The "outermost" place that holds this value.
20-
#[derive(Copy, Clone)]
21+
#[derive(Copy, Clone, Debug, PartialEq)]
2122
crate enum PlaceBase {
2223
/// Denotes the start of a `Place`.
2324
Local(Local),
@@ -67,7 +68,7 @@ crate enum PlaceBase {
6768
///
6869
/// This is used internally when building a place for an expression like `a.b.c`. The fields `b`
6970
/// and `c` can be progressively pushed onto the place builder that is created when converting `a`.
70-
#[derive(Clone)]
71+
#[derive(Clone, Debug, PartialEq)]
7172
crate struct PlaceBuilder<'tcx> {
7273
base: PlaceBase,
7374
projection: Vec<PlaceElem<'tcx>>,
@@ -83,20 +84,23 @@ fn convert_to_hir_projections_and_truncate_for_capture<'tcx>(
8384
mir_projections: &[PlaceElem<'tcx>],
8485
) -> Vec<HirProjectionKind> {
8586
let mut hir_projections = Vec::new();
87+
let mut variant = None;
8688

8789
for mir_projection in mir_projections {
8890
let hir_projection = match mir_projection {
8991
ProjectionElem::Deref => HirProjectionKind::Deref,
9092
ProjectionElem::Field(field, _) => {
91-
// We will never encouter this for multivariant enums,
92-
// read the comment for `Downcast`.
93-
HirProjectionKind::Field(field.index() as u32, VariantIdx::new(0))
93+
let variant = variant.unwrap_or(VariantIdx::new(0));
94+
HirProjectionKind::Field(field.index() as u32, variant)
9495
}
95-
ProjectionElem::Downcast(..) => {
96-
// This projections exist only for enums that have
97-
// multiple variants. Since such enums that are captured
98-
// completely, we can stop here.
99-
break;
96+
ProjectionElem::Downcast(.., idx) => {
97+
// We don't expect to see multi-variant enums here, as earlier
98+
// phases will have truncated them already. However, there can
99+
// still be downcasts, thanks to single-variant enums.
100+
// We keep track of VariantIdx so we can use this information
101+
// if the next ProjectionElem is a Field.
102+
variant = Some(*idx);
103+
continue;
100104
}
101105
ProjectionElem::Index(..)
102106
| ProjectionElem::ConstantIndex { .. }
@@ -106,7 +110,7 @@ fn convert_to_hir_projections_and_truncate_for_capture<'tcx>(
106110
break;
107111
}
108112
};
109-
113+
variant = None;
110114
hir_projections.push(hir_projection);
111115
}
112116

@@ -194,12 +198,12 @@ fn find_capture_matching_projections<'a, 'tcx>(
194198
/// Takes a PlaceBuilder and resolves the upvar (if any) within it, so that the
195199
/// `PlaceBuilder` now starts from `PlaceBase::Local`.
196200
///
197-
/// Returns a Result with the error being the HirId of the Upvar that was not found.
201+
/// Returns a Result with the error being the PlaceBuilder (`from_builder`) that was not found.
198202
fn to_upvars_resolved_place_builder<'a, 'tcx>(
199203
from_builder: PlaceBuilder<'tcx>,
200204
tcx: TyCtxt<'tcx>,
201205
typeck_results: &'a ty::TypeckResults<'tcx>,
202-
) -> Result<PlaceBuilder<'tcx>, HirId> {
206+
) -> Result<PlaceBuilder<'tcx>, PlaceBuilder<'tcx>> {
203207
match from_builder.base {
204208
PlaceBase::Local(_) => Ok(from_builder),
205209
PlaceBase::Upvar { var_hir_id, closure_def_id, closure_kind } => {
@@ -230,13 +234,12 @@ fn to_upvars_resolved_place_builder<'a, 'tcx>(
230234
from_builder.projection
231235
)
232236
} else {
233-
// FIXME(project-rfc-2229#24): Handle this case properly
234237
debug!(
235238
"No associated capture found for {:?}[{:#?}]",
236239
var_hir_id, from_builder.projection,
237240
);
238241
}
239-
return Err(var_hir_id);
242+
return Err(from_builder);
240243
};
241244

242245
let closure_ty = typeck_results
@@ -300,6 +303,25 @@ impl<'tcx> PlaceBuilder<'tcx> {
300303
to_upvars_resolved_place_builder(self, tcx, typeck_results).unwrap()
301304
}
302305

306+
/// Attempts to resolve the `PlaceBuilder`.
307+
/// On success, it will return the resolved `PlaceBuilder`.
308+
/// On failure, it will return itself.
309+
///
310+
/// Upvars resolve may fail for a `PlaceBuilder` when attempting to
311+
/// resolve a disjoint field whose root variable is not captured
312+
/// (destructured assignments) or when attempting to resolve a root
313+
/// variable (discriminant matching with only wildcard arm) that is
314+
/// not captured. This can happen because the final mir that will be
315+
/// generated doesn't require a read for this place. Failures will only
316+
/// happen inside closures.
317+
crate fn try_upvars_resolved<'a>(
318+
self,
319+
tcx: TyCtxt<'tcx>,
320+
typeck_results: &'a ty::TypeckResults<'tcx>,
321+
) -> Result<PlaceBuilder<'tcx>, PlaceBuilder<'tcx>> {
322+
to_upvars_resolved_place_builder(self, tcx, typeck_results)
323+
}
324+
303325
crate fn base(&self) -> PlaceBase {
304326
self.base
305327
}
@@ -308,15 +330,22 @@ impl<'tcx> PlaceBuilder<'tcx> {
308330
self.project(PlaceElem::Field(f, ty))
309331
}
310332

311-
fn deref(self) -> Self {
333+
crate fn deref(self) -> Self {
312334
self.project(PlaceElem::Deref)
313335
}
314336

337+
crate fn downcast(self, adt_def: &'tcx AdtDef, variant_index: VariantIdx) -> Self {
338+
self.project(PlaceElem::Downcast(
339+
Some(adt_def.variants[variant_index].ident.name),
340+
variant_index,
341+
))
342+
}
343+
315344
fn index(self, index: Local) -> Self {
316345
self.project(PlaceElem::Index(index))
317346
}
318347

319-
fn project(mut self, elem: PlaceElem<'tcx>) -> Self {
348+
crate fn project(mut self, elem: PlaceElem<'tcx>) -> Self {
320349
self.projection.push(elem);
321350
self
322351
}
@@ -602,13 +631,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
602631
// The "retagging" transformation (for Stacked Borrows) relies on this.
603632
let idx = unpack!(block = self.as_temp(block, temp_lifetime, index, Mutability::Not,));
604633

605-
block = self.bounds_check(
606-
block,
607-
base_place.clone().into_place(self.tcx, self.typeck_results),
608-
idx,
609-
expr_span,
610-
source_info,
611-
);
634+
block = self.bounds_check(block, base_place.clone(), idx, expr_span, source_info);
612635

613636
if is_outermost_index {
614637
self.read_fake_borrows(block, fake_borrow_temps, source_info)
@@ -629,7 +652,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
629652
fn bounds_check(
630653
&mut self,
631654
block: BasicBlock,
632-
slice: Place<'tcx>,
655+
slice: PlaceBuilder<'tcx>,
633656
index: Local,
634657
expr_span: Span,
635658
source_info: SourceInfo,
@@ -641,7 +664,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
641664
let lt = self.temp(bool_ty, expr_span);
642665

643666
// len = len(slice)
644-
self.cfg.push_assign(block, source_info, len, Rvalue::Len(slice));
667+
self.cfg.push_assign(
668+
block,
669+
source_info,
670+
len,
671+
Rvalue::Len(slice.into_place(self.tcx, self.typeck_results)),
672+
);
645673
// lt = idx < len
646674
self.cfg.push_assign(
647675
block,

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

+37-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::build::{BlockAnd, BlockAndExtension, Builder};
88
use crate::thir::*;
99
use rustc_middle::middle::region;
1010
use rustc_middle::mir::AssertKind;
11+
use rustc_middle::mir::Place;
1112
use rustc_middle::mir::*;
1213
use rustc_middle::ty::{self, Ty, UpvarSubsts};
1314
use rustc_span::Span;
@@ -164,7 +165,41 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
164165

165166
block.and(Rvalue::Aggregate(box AggregateKind::Tuple, fields))
166167
}
167-
ExprKind::Closure { closure_id, substs, upvars, movability } => {
168+
ExprKind::Closure { closure_id, substs, upvars, movability, ref fake_reads } => {
169+
// Convert the closure fake reads, if any, from `ExprRef` to mir `Place`
170+
// and push the fake reads.
171+
// This must come before creating the operands. This is required in case
172+
// there is a fake read and a borrow of the same path, since otherwise the
173+
// fake read might interfere with the borrow. Consider an example like this
174+
// one:
175+
// ```
176+
// let mut x = 0;
177+
// let c = || {
178+
// &mut x; // mutable borrow of `x`
179+
// match x { _ => () } // fake read of `x`
180+
// };
181+
// ```
182+
// FIXME(RFC2229): Remove feature gate once diagnostics are improved
183+
if this.tcx.features().capture_disjoint_fields {
184+
for (thir_place, cause, hir_id) in fake_reads.into_iter() {
185+
let place_builder =
186+
unpack!(block = this.as_place_builder(block, thir_place));
187+
188+
if let Ok(place_builder_resolved) =
189+
place_builder.try_upvars_resolved(this.tcx, this.typeck_results)
190+
{
191+
let mir_place =
192+
place_builder_resolved.into_place(this.tcx, this.typeck_results);
193+
this.cfg.push_fake_read(
194+
block,
195+
this.source_info(this.tcx.hir().span(*hir_id)),
196+
*cause,
197+
mir_place,
198+
);
199+
}
200+
}
201+
}
202+
168203
// see (*) above
169204
let operands: Vec<_> = upvars
170205
.into_iter()
@@ -203,6 +238,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
203238
}
204239
})
205240
.collect();
241+
206242
let result = match substs {
207243
UpvarSubsts::Generator(substs) => {
208244
// We implicitly set the discriminant to 0. See

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
6363
mod as_constant;
6464
mod as_operand;
65-
mod as_place;
65+
pub mod as_place;
6666
mod as_rvalue;
6767
mod as_temp;
6868
mod category;

0 commit comments

Comments
 (0)