]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_mir_build/src/build/expr/as_place.rs
New upstream version 1.65.0+dfsg1
[rustc.git] / compiler / rustc_mir_build / src / build / expr / as_place.rs
CommitLineData
e9174d1e
SL
1//! See docs in build/expr/mod.rs
2
9fa01778
XL
3use crate::build::expr::category::Category;
4use crate::build::ForGuard::{OutsideGuard, RefWithinGuard};
f2b60f7d 5use crate::build::{BlockAnd, BlockAndExtension, Builder, Capture, CaptureMap};
064997fb 6use rustc_hir::def_id::LocalDefId;
923072b8 7use rustc_middle::hir::place::Projection as HirProjection;
fc512014 8use rustc_middle::hir::place::ProjectionKind as HirProjectionKind;
5869c6ff 9use rustc_middle::middle::region;
ba9703b0
XL
10use rustc_middle::mir::AssertKind::BoundsCheck;
11use rustc_middle::mir::*;
17df50a5 12use rustc_middle::thir::*;
6a06907d 13use rustc_middle::ty::AdtDef;
ba9703b0 14use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty, TyCtxt, Variance};
dfeec247 15use rustc_span::Span;
fc512014 16use rustc_target::abi::VariantIdx;
e9174d1e 17
e74abb32
XL
18use rustc_index::vec::Idx;
19
cdc7bbd5
XL
20use std::iter;
21
fc512014 22/// The "outermost" place that holds this value.
6a06907d 23#[derive(Copy, Clone, Debug, PartialEq)]
923072b8 24pub(crate) enum PlaceBase {
fc512014
XL
25 /// Denotes the start of a `Place`.
26 Local(Local),
27
28 /// When building place for an expression within a closure, the place might start off a
29 /// captured path. When `capture_disjoint_fields` is enabled, we might not know the capture
30 /// index (within the desugared closure) of the captured path until most of the projections
31 /// are applied. We use `PlaceBase::Upvar` to keep track of the root variable off of which the
32 /// captured path starts, the closure the capture belongs to and the trait the closure
33 /// implements.
34 ///
35 /// Once we have figured out the capture index, we can convert the place builder to start from
36 /// `PlaceBase::Local`.
37 ///
38 /// Consider the following example
39 /// ```rust
04454e1e 40 /// let t = (((10, 10), 10), 10);
fc512014
XL
41 ///
42 /// let c = || {
43 /// println!("{}", t.0.0.0);
44 /// };
45 /// ```
46 /// Here the THIR expression for `t.0.0.0` will be something like
47 ///
04454e1e 48 /// ```ignore (illustrative)
fc512014
XL
49 /// * Field(0)
50 /// * Field(0)
51 /// * Field(0)
52 /// * UpvarRef(t)
53 /// ```
54 ///
55 /// When `capture_disjoint_fields` is enabled, `t.0.0.0` is captured and we won't be able to
56 /// figure out that it is captured until all the `Field` projections are applied.
57 Upvar {
58 /// HirId of the upvar
923072b8 59 var_hir_id: LocalVarId,
fc512014 60 /// DefId of the closure
064997fb 61 closure_def_id: LocalDefId,
5869c6ff 62 },
fc512014
XL
63}
64
e74abb32
XL
65/// `PlaceBuilder` is used to create places during MIR construction. It allows you to "build up" a
66/// place by pushing more and more projections onto the end, and then convert the final set into a
67/// place using the `into_place` method.
68///
69/// This is used internally when building a place for an expression like `a.b.c`. The fields `b`
70/// and `c` can be progressively pushed onto the place builder that is created when converting `a`.
6a06907d 71#[derive(Clone, Debug, PartialEq)]
923072b8 72pub(crate) struct PlaceBuilder<'tcx> {
fc512014 73 base: PlaceBase,
e74abb32
XL
74 projection: Vec<PlaceElem<'tcx>>,
75}
76
fc512014
XL
77/// Given a list of MIR projections, convert them to list of HIR ProjectionKind.
78/// The projections are truncated to represent a path that might be captured by a
79/// closure/generator. This implies the vector returned from this function doesn't contain
80/// ProjectionElems `Downcast`, `ConstantIndex`, `Index`, or `Subslice` because those will never be
cdc7bbd5 81/// part of a path that is captured by a closure. We stop applying projections once we see the first
fc512014
XL
82/// projection that isn't captured by a closure.
83fn convert_to_hir_projections_and_truncate_for_capture<'tcx>(
5869c6ff 84 mir_projections: &[PlaceElem<'tcx>],
fc512014 85) -> Vec<HirProjectionKind> {
5869c6ff 86 let mut hir_projections = Vec::new();
6a06907d 87 let mut variant = None;
fc512014
XL
88
89 for mir_projection in mir_projections {
90 let hir_projection = match mir_projection {
91 ProjectionElem::Deref => HirProjectionKind::Deref,
92 ProjectionElem::Field(field, _) => {
6a06907d
XL
93 let variant = variant.unwrap_or(VariantIdx::new(0));
94 HirProjectionKind::Field(field.index() as u32, variant)
5869c6ff 95 }
6a06907d
XL
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;
5869c6ff 104 }
fc512014
XL
105 ProjectionElem::Index(..)
106 | ProjectionElem::ConstantIndex { .. }
107 | ProjectionElem::Subslice { .. } => {
108 // We don't capture array-access projections.
109 // We can stop here as arrays are captured completely.
5869c6ff
XL
110 break;
111 }
fc512014 112 };
6a06907d 113 variant = None;
fc512014
XL
114 hir_projections.push(hir_projection);
115 }
116
117 hir_projections
118}
119
120/// Return true if the `proj_possible_ancestor` represents an ancestor path
121/// to `proj_capture` or `proj_possible_ancestor` is same as `proj_capture`,
122/// assuming they both start off of the same root variable.
123///
124/// **Note:** It's the caller's responsibility to ensure that both lists of projections
125/// start off of the same root variable.
126///
127/// Eg: 1. `foo.x` which is represented using `projections=[Field(x)]` is an ancestor of
128/// `foo.x.y` which is represented using `projections=[Field(x), Field(y)]`.
129/// Note both `foo.x` and `foo.x.y` start off of the same root variable `foo`.
130/// 2. Since we only look at the projections here function will return `bar.x` as an a valid
131/// ancestor of `foo.x.y`. It's the caller's responsibility to ensure that both projections
132/// list are being applied to the same root variable.
133fn is_ancestor_or_same_capture(
923072b8 134 proj_possible_ancestor: &[HirProjectionKind],
5869c6ff 135 proj_capture: &[HirProjectionKind],
fc512014
XL
136) -> bool {
137 // We want to make sure `is_ancestor_or_same_capture("x.0.0", "x.0")` to return false.
138 // Therefore we can't just check if all projections are same in the zipped iterator below.
139 if proj_possible_ancestor.len() > proj_capture.len() {
140 return false;
141 }
142
cdc7bbd5 143 iter::zip(proj_possible_ancestor, proj_capture).all(|(a, b)| a == b)
fc512014
XL
144}
145
fc512014
XL
146/// Given a closure, returns the index of a capture within the desugared closure struct and the
147/// `ty::CapturedPlace` which is the ancestor of the Place represented using the `var_hir_id`
148/// and `projection`.
149///
150/// Note there will be at most one ancestor for any given Place.
151///
152/// Returns None, when the ancestor is not found.
153fn find_capture_matching_projections<'a, 'tcx>(
f2b60f7d 154 upvars: &'a CaptureMap<'tcx>,
923072b8 155 var_hir_id: LocalVarId,
5869c6ff 156 projections: &[PlaceElem<'tcx>],
f2b60f7d 157) -> Option<(usize, &'a Capture<'tcx>)> {
fc512014
XL
158 let hir_projections = convert_to_hir_projections_and_truncate_for_capture(projections);
159
f2b60f7d 160 upvars.get_by_key_enumerated(var_hir_id.0).find(|(_, capture)| {
923072b8 161 let possible_ancestor_proj_kinds: Vec<_> =
f2b60f7d 162 capture.captured_place.place.projections.iter().map(|proj| proj.kind).collect();
5869c6ff 163 is_ancestor_or_same_capture(&possible_ancestor_proj_kinds, &hir_projections)
f2b60f7d 164 })
fc512014
XL
165}
166
167/// Takes a PlaceBuilder and resolves the upvar (if any) within it, so that the
168/// `PlaceBuilder` now starts from `PlaceBase::Local`.
169///
6a06907d 170/// Returns a Result with the error being the PlaceBuilder (`from_builder`) that was not found.
fc512014
XL
171fn to_upvars_resolved_place_builder<'a, 'tcx>(
172 from_builder: PlaceBuilder<'tcx>,
173 tcx: TyCtxt<'tcx>,
f2b60f7d 174 upvars: &'a CaptureMap<'tcx>,
6a06907d 175) -> Result<PlaceBuilder<'tcx>, PlaceBuilder<'tcx>> {
fc512014
XL
176 match from_builder.base {
177 PlaceBase::Local(_) => Ok(from_builder),
f2b60f7d 178 PlaceBase::Upvar { var_hir_id, closure_def_id } => {
3c0e092e 179 let Some((capture_index, capture)) =
5869c6ff 180 find_capture_matching_projections(
f2b60f7d 181 upvars,
fc512014 182 var_hir_id,
fc512014 183 &from_builder.projection,
3c0e092e 184 ) else {
5099ac24 185 let closure_span = tcx.def_span(closure_def_id);
136023e0 186 if !enable_precise_capture(tcx, closure_span) {
5869c6ff
XL
187 bug!(
188 "No associated capture found for {:?}[{:#?}] even though \
fc512014 189 capture_disjoint_fields isn't enabled",
5869c6ff
XL
190 var_hir_id,
191 from_builder.projection
192 )
193 } else {
5869c6ff
XL
194 debug!(
195 "No associated capture found for {:?}[{:#?}]",
196 var_hir_id, from_builder.projection,
197 );
198 }
6a06907d 199 return Err(from_builder);
5869c6ff 200 };
fc512014 201
fc512014 202 // Access the capture by accessing the field within the Closure struct.
f2b60f7d
FG
203 let capture_info = &upvars[capture_index];
204
205 let mut upvar_resolved_place_builder = PlaceBuilder::from(capture_info.use_place);
fc512014 206
fc512014
XL
207 // We used some of the projections to build the capture itself,
208 // now we apply the remaining to the upvar resolved place.
923072b8 209 let remaining_projections = strip_prefix(
f2b60f7d 210 capture.captured_place.place.base_ty,
923072b8 211 from_builder.projection,
f2b60f7d 212 &capture.captured_place.place.projections,
923072b8
FG
213 );
214 upvar_resolved_place_builder.projection.extend(remaining_projections);
fc512014
XL
215
216 Ok(upvar_resolved_place_builder)
217 }
218 }
219}
220
923072b8
FG
221/// Returns projections remaining after stripping an initial prefix of HIR
222/// projections.
223///
224/// Supports only HIR projection kinds that represent a path that might be
225/// captured by a closure or a generator, i.e., an `Index` or a `Subslice`
226/// projection kinds are unsupported.
227fn strip_prefix<'tcx>(
228 mut base_ty: Ty<'tcx>,
229 projections: Vec<PlaceElem<'tcx>>,
230 prefix_projections: &[HirProjection<'tcx>],
231) -> impl Iterator<Item = PlaceElem<'tcx>> {
232 let mut iter = projections.into_iter();
233 for projection in prefix_projections {
234 match projection.kind {
235 HirProjectionKind::Deref => {
236 assert!(matches!(iter.next(), Some(ProjectionElem::Deref)));
237 }
238 HirProjectionKind::Field(..) => {
239 if base_ty.is_enum() {
240 assert!(matches!(iter.next(), Some(ProjectionElem::Downcast(..))));
241 }
242 assert!(matches!(iter.next(), Some(ProjectionElem::Field(..))));
243 }
244 HirProjectionKind::Index | HirProjectionKind::Subslice => {
245 bug!("unexpected projection kind: {:?}", projection);
246 }
247 }
248 base_ty = projection.ty;
249 }
250 iter
251}
252
dfeec247 253impl<'tcx> PlaceBuilder<'tcx> {
f2b60f7d 254 pub(in crate::build) fn into_place<'a>(
fc512014
XL
255 self,
256 tcx: TyCtxt<'tcx>,
f2b60f7d 257 upvars: &'a CaptureMap<'tcx>,
fc512014
XL
258 ) -> Place<'tcx> {
259 if let PlaceBase::Local(local) = self.base {
260 Place { local, projection: tcx.intern_place_elems(&self.projection) }
261 } else {
f2b60f7d 262 self.expect_upvars_resolved(tcx, upvars).into_place(tcx, upvars)
fc512014
XL
263 }
264 }
265
266 fn expect_upvars_resolved<'a>(
267 self,
268 tcx: TyCtxt<'tcx>,
f2b60f7d 269 upvars: &'a CaptureMap<'tcx>,
fc512014 270 ) -> PlaceBuilder<'tcx> {
f2b60f7d 271 to_upvars_resolved_place_builder(self, tcx, upvars).unwrap()
fc512014
XL
272 }
273
6a06907d
XL
274 /// Attempts to resolve the `PlaceBuilder`.
275 /// On success, it will return the resolved `PlaceBuilder`.
276 /// On failure, it will return itself.
277 ///
278 /// Upvars resolve may fail for a `PlaceBuilder` when attempting to
279 /// resolve a disjoint field whose root variable is not captured
280 /// (destructured assignments) or when attempting to resolve a root
281 /// variable (discriminant matching with only wildcard arm) that is
282 /// not captured. This can happen because the final mir that will be
283 /// generated doesn't require a read for this place. Failures will only
284 /// happen inside closures.
f2b60f7d 285 pub(in crate::build) fn try_upvars_resolved<'a>(
6a06907d
XL
286 self,
287 tcx: TyCtxt<'tcx>,
f2b60f7d 288 upvars: &'a CaptureMap<'tcx>,
6a06907d 289 ) -> Result<PlaceBuilder<'tcx>, PlaceBuilder<'tcx>> {
f2b60f7d 290 to_upvars_resolved_place_builder(self, tcx, upvars)
6a06907d
XL
291 }
292
923072b8 293 pub(crate) fn base(&self) -> PlaceBase {
fc512014 294 self.base
e74abb32
XL
295 }
296
923072b8 297 pub(crate) fn field(self, f: Field, ty: Ty<'tcx>) -> Self {
e74abb32
XL
298 self.project(PlaceElem::Field(f, ty))
299 }
300
923072b8 301 pub(crate) fn deref(self) -> Self {
e74abb32
XL
302 self.project(PlaceElem::Deref)
303 }
304
923072b8 305 pub(crate) fn downcast(self, adt_def: AdtDef<'tcx>, variant_index: VariantIdx) -> Self {
5e7ed085 306 self.project(PlaceElem::Downcast(Some(adt_def.variant(variant_index).name), variant_index))
6a06907d
XL
307 }
308
e74abb32
XL
309 fn index(self, index: Local) -> Self {
310 self.project(PlaceElem::Index(index))
311 }
312
923072b8 313 pub(crate) fn project(mut self, elem: PlaceElem<'tcx>) -> Self {
e74abb32
XL
314 self.projection.push(elem);
315 self
316 }
317}
318
dfeec247 319impl<'tcx> From<Local> for PlaceBuilder<'tcx> {
e74abb32 320 fn from(local: Local) -> Self {
fc512014
XL
321 Self { base: PlaceBase::Local(local), projection: Vec::new() }
322 }
323}
324
325impl<'tcx> From<PlaceBase> for PlaceBuilder<'tcx> {
326 fn from(base: PlaceBase) -> Self {
327 Self { base, projection: Vec::new() }
e74abb32
XL
328 }
329}
3157f602 330
f2b60f7d
FG
331impl<'tcx> From<Place<'tcx>> for PlaceBuilder<'tcx> {
332 fn from(p: Place<'tcx>) -> Self {
333 Self { base: PlaceBase::Local(p.local), projection: p.projection.to_vec() }
334 }
335}
336
dc9dc135 337impl<'a, 'tcx> Builder<'a, 'tcx> {
ff7c6d11 338 /// Compile `expr`, yielding a place that we can move from etc.
60c5eb7d
XL
339 ///
340 /// WARNING: Any user code might:
341 /// * Invalidate any slice bounds checks performed.
342 /// * Change the address that this `Place` refers to.
343 /// * Modify the memory that this place refers to.
344 /// * Invalidate the memory that this place refers to, this will be caught
345 /// by borrow checking.
346 ///
347 /// Extra care is needed if any user code is allowed to run between calling
348 /// this method and using it, as is the case for `match` and index
349 /// expressions.
923072b8 350 pub(crate) fn as_place(
6a06907d
XL
351 &mut self,
352 mut block: BasicBlock,
17df50a5 353 expr: &Expr<'tcx>,
6a06907d 354 ) -> BlockAnd<Place<'tcx>> {
e74abb32 355 let place_builder = unpack!(block = self.as_place_builder(block, expr));
f2b60f7d 356 block.and(place_builder.into_place(self.tcx, &self.upvars))
e74abb32
XL
357 }
358
359 /// This is used when constructing a compound `Place`, so that we can avoid creating
360 /// intermediate `Place` values until we know the full set of projections.
923072b8 361 pub(crate) fn as_place_builder(
5869c6ff
XL
362 &mut self,
363 block: BasicBlock,
17df50a5 364 expr: &Expr<'tcx>,
6a06907d 365 ) -> BlockAnd<PlaceBuilder<'tcx>> {
60c5eb7d 366 self.expr_as_place(block, expr, Mutability::Mut, None)
e9174d1e
SL
367 }
368
b7449926
XL
369 /// Compile `expr`, yielding a place that we can move from etc.
370 /// Mutability note: The caller of this method promises only to read from the resulting
371 /// place. The place itself may or may not be mutable:
372 /// * If this expr is a place expr like a.b, then we will return that place.
373 /// * Otherwise, a temporary is created: in that event, it will be an immutable temporary.
923072b8 374 pub(crate) fn as_read_only_place(
dfeec247
XL
375 &mut self,
376 mut block: BasicBlock,
17df50a5 377 expr: &Expr<'tcx>,
6a06907d 378 ) -> BlockAnd<Place<'tcx>> {
e74abb32 379 let place_builder = unpack!(block = self.as_read_only_place_builder(block, expr));
f2b60f7d 380 block.and(place_builder.into_place(self.tcx, &self.upvars))
e74abb32
XL
381 }
382
383 /// This is used when constructing a compound `Place`, so that we can avoid creating
384 /// intermediate `Place` values until we know the full set of projections.
385 /// Mutability note: The caller of this method promises only to read from the resulting
386 /// place. The place itself may or may not be mutable:
387 /// * If this expr is a place expr like a.b, then we will return that place.
388 /// * Otherwise, a temporary is created: in that event, it will be an immutable temporary.
6a06907d 389 fn as_read_only_place_builder(
e74abb32
XL
390 &mut self,
391 block: BasicBlock,
17df50a5 392 expr: &Expr<'tcx>,
6a06907d 393 ) -> BlockAnd<PlaceBuilder<'tcx>> {
60c5eb7d 394 self.expr_as_place(block, expr, Mutability::Not, None)
b7449926
XL
395 }
396
397 fn expr_as_place(
398 &mut self,
399 mut block: BasicBlock,
17df50a5 400 expr: &Expr<'tcx>,
b7449926 401 mutability: Mutability,
60c5eb7d 402 fake_borrow_temps: Option<&mut Vec<Local>>,
e74abb32 403 ) -> BlockAnd<PlaceBuilder<'tcx>> {
dfeec247 404 debug!("expr_as_place(block={:?}, expr={:?}, mutability={:?})", block, expr, mutability);
e9174d1e
SL
405
406 let this = self;
407 let expr_span = expr.span;
3157f602 408 let source_info = this.source_info(expr_span);
e9174d1e 409 match expr.kind {
dfeec247
XL
410 ExprKind::Scope { region_scope, lint_level, value } => {
411 this.in_scope((region_scope, source_info), lint_level, |this| {
17df50a5 412 this.expr_as_place(block, &this.thir[value], mutability, fake_borrow_temps)
dfeec247
XL
413 })
414 }
923072b8
FG
415 ExprKind::Field { lhs, variant_index, name } => {
416 let lhs = &this.thir[lhs];
417 let mut place_builder =
418 unpack!(block = this.expr_as_place(block, lhs, mutability, fake_borrow_temps,));
419 if let ty::Adt(adt_def, _) = lhs.ty.kind() {
420 if adt_def.is_enum() {
421 place_builder = place_builder.downcast(*adt_def, variant_index);
422 }
423 }
e74abb32 424 block.and(place_builder.field(name, expr.ty))
e9174d1e
SL
425 }
426 ExprKind::Deref { arg } => {
17df50a5
XL
427 let place_builder = unpack!(
428 block =
429 this.expr_as_place(block, &this.thir[arg], mutability, fake_borrow_temps,)
430 );
e74abb32 431 block.and(place_builder.deref())
e9174d1e 432 }
dfeec247
XL
433 ExprKind::Index { lhs, index } => this.lower_index_expression(
434 block,
17df50a5
XL
435 &this.thir[lhs],
436 &this.thir[index],
dfeec247
XL
437 mutability,
438 fake_borrow_temps,
439 expr.temp_lifetime,
440 expr_span,
441 source_info,
442 ),
fc512014 443 ExprKind::UpvarRef { closure_def_id, var_hir_id } => {
923072b8 444 this.lower_captured_upvar(block, closure_def_id.expect_local(), var_hir_id)
fc512014
XL
445 }
446
e9174d1e 447 ExprKind::VarRef { id } => {
e74abb32 448 let place_builder = if this.is_bound_var_in_guard(id) {
8faf50e0 449 let index = this.var_local_id(id, RefWithinGuard);
e74abb32 450 PlaceBuilder::from(index).deref()
83c7162d
XL
451 } else {
452 let index = this.var_local_id(id, OutsideGuard);
e74abb32 453 PlaceBuilder::from(index)
83c7162d 454 };
e74abb32 455 block.and(place_builder)
e9174d1e 456 }
e9174d1e 457
f2b60f7d 458 ExprKind::PlaceTypeAscription { source, ref user_ty } => {
dfeec247 459 let place_builder = unpack!(
17df50a5
XL
460 block = this.expr_as_place(
461 block,
462 &this.thir[source],
463 mutability,
464 fake_borrow_temps,
465 )
dfeec247 466 );
0bf4aa26 467 if let Some(user_ty) = user_ty {
dfeec247
XL
468 let annotation_index =
469 this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
9fa01778 470 span: source_info.span,
f2b60f7d 471 user_ty: user_ty.clone(),
9fa01778 472 inferred_ty: expr.ty,
dfeec247 473 });
e74abb32 474
f2b60f7d 475 let place = place_builder.clone().into_place(this.tcx, &this.upvars);
0bf4aa26
XL
476 this.cfg.push(
477 block,
478 Statement {
479 source_info,
480 kind: StatementKind::AscribeUserType(
94222f64 481 Box::new((
e74abb32 482 place,
dfeec247 483 UserTypeProjection { base: annotation_index, projs: vec![] },
94222f64 484 )),
0bf4aa26 485 Variance::Invariant,
0bf4aa26
XL
486 ),
487 },
488 );
489 }
e74abb32 490 block.and(place_builder)
0bf4aa26 491 }
f2b60f7d 492 ExprKind::ValueTypeAscription { source, ref user_ty } => {
17df50a5 493 let source = &this.thir[source];
dfeec247
XL
494 let temp =
495 unpack!(block = this.as_temp(block, source.temp_lifetime, source, mutability));
0bf4aa26 496 if let Some(user_ty) = user_ty {
dfeec247
XL
497 let annotation_index =
498 this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
9fa01778 499 span: source_info.span,
f2b60f7d 500 user_ty: user_ty.clone(),
9fa01778 501 inferred_ty: expr.ty,
dfeec247 502 });
0bf4aa26
XL
503 this.cfg.push(
504 block,
505 Statement {
506 source_info,
507 kind: StatementKind::AscribeUserType(
94222f64 508 Box::new((
dfeec247
XL
509 Place::from(temp),
510 UserTypeProjection { base: annotation_index, projs: vec![] },
94222f64 511 )),
0bf4aa26 512 Variance::Invariant,
0bf4aa26
XL
513 ),
514 },
515 );
516 }
e74abb32 517 block.and(PlaceBuilder::from(temp))
0bf4aa26
XL
518 }
519
b7449926
XL
520 ExprKind::Array { .. }
521 | ExprKind::Tuple { .. }
522 | ExprKind::Adt { .. }
523 | ExprKind::Closure { .. }
524 | ExprKind::Unary { .. }
525 | ExprKind::Binary { .. }
526 | ExprKind::LogicalOp { .. }
527 | ExprKind::Box { .. }
528 | ExprKind::Cast { .. }
529 | ExprKind::Use { .. }
530 | ExprKind::NeverToAny { .. }
48663c56 531 | ExprKind::Pointer { .. }
b7449926
XL
532 | ExprKind::Repeat { .. }
533 | ExprKind::Borrow { .. }
dfeec247 534 | ExprKind::AddressOf { .. }
b7449926 535 | ExprKind::Match { .. }
5869c6ff 536 | ExprKind::If { .. }
b7449926
XL
537 | ExprKind::Loop { .. }
538 | ExprKind::Block { .. }
94222f64 539 | ExprKind::Let { .. }
b7449926
XL
540 | ExprKind::Assign { .. }
541 | ExprKind::AssignOp { .. }
542 | ExprKind::Break { .. }
543 | ExprKind::Continue { .. }
544 | ExprKind::Return { .. }
545 | ExprKind::Literal { .. }
5e7ed085
FG
546 | ExprKind::NamedConst { .. }
547 | ExprKind::NonHirLiteral { .. }
064997fb 548 | ExprKind::ZstLiteral { .. }
5e7ed085 549 | ExprKind::ConstParam { .. }
29967ef6 550 | ExprKind::ConstBlock { .. }
60c5eb7d 551 | ExprKind::StaticRef { .. }
f9f354fc 552 | ExprKind::InlineAsm { .. }
b7449926 553 | ExprKind::Yield { .. }
f9f354fc 554 | ExprKind::ThreadLocalRef(_)
b7449926 555 | ExprKind::Call { .. } => {
ff7c6d11 556 // these are not places, so we need to make a temporary.
29967ef6 557 debug_assert!(!matches!(Category::of(&expr.kind), Some(Category::Place)));
b7449926
XL
558 let temp =
559 unpack!(block = this.as_temp(block, expr.temp_lifetime, expr, mutability));
e74abb32 560 block.and(PlaceBuilder::from(temp))
e9174d1e
SL
561 }
562 }
563 }
60c5eb7d 564
fc512014
XL
565 /// Lower a captured upvar. Note we might not know the actual capture index,
566 /// so we create a place starting from `PlaceBase::Upvar`, which will be resolved
cdc7bbd5 567 /// once all projections that allow us to identify a capture have been applied.
fc512014
XL
568 fn lower_captured_upvar(
569 &mut self,
570 block: BasicBlock,
064997fb 571 closure_def_id: LocalDefId,
923072b8 572 var_hir_id: LocalVarId,
fc512014 573 ) -> BlockAnd<PlaceBuilder<'tcx>> {
f2b60f7d 574 block.and(PlaceBuilder::from(PlaceBase::Upvar { var_hir_id, closure_def_id }))
fc512014
XL
575 }
576
60c5eb7d
XL
577 /// Lower an index expression
578 ///
579 /// This has two complications;
580 ///
581 /// * We need to do a bounds check.
582 /// * We need to ensure that the bounds check can't be invalidated using an
583 /// expression like `x[1][{x = y; 2}]`. We use fake borrows here to ensure
584 /// that this is the case.
585 fn lower_index_expression(
586 &mut self,
587 mut block: BasicBlock,
17df50a5
XL
588 base: &Expr<'tcx>,
589 index: &Expr<'tcx>,
60c5eb7d
XL
590 mutability: Mutability,
591 fake_borrow_temps: Option<&mut Vec<Local>>,
592 temp_lifetime: Option<region::Scope>,
593 expr_span: Span,
dfeec247 594 source_info: SourceInfo,
60c5eb7d 595 ) -> BlockAnd<PlaceBuilder<'tcx>> {
60c5eb7d
XL
596 let base_fake_borrow_temps = &mut Vec::new();
597 let is_outermost_index = fake_borrow_temps.is_none();
598 let fake_borrow_temps = fake_borrow_temps.unwrap_or(base_fake_borrow_temps);
599
fc512014 600 let mut base_place =
6a06907d 601 unpack!(block = self.expr_as_place(block, base, mutability, Some(fake_borrow_temps),));
60c5eb7d
XL
602
603 // Making this a *fresh* temporary means we do not have to worry about
604 // the index changing later: Nothing will ever change this temporary.
605 // The "retagging" transformation (for Stacked Borrows) relies on this.
dfeec247 606 let idx = unpack!(block = self.as_temp(block, temp_lifetime, index, Mutability::Not,));
60c5eb7d 607
6a06907d 608 block = self.bounds_check(block, base_place.clone(), idx, expr_span, source_info);
60c5eb7d
XL
609
610 if is_outermost_index {
611 self.read_fake_borrows(block, fake_borrow_temps, source_info)
612 } else {
f2b60f7d 613 base_place = base_place.expect_upvars_resolved(self.tcx, &self.upvars);
60c5eb7d
XL
614 self.add_fake_borrows_of_base(
615 &base_place,
616 block,
617 fake_borrow_temps,
618 expr_span,
619 source_info,
620 );
621 }
622
623 block.and(base_place.index(idx))
624 }
625
626 fn bounds_check(
627 &mut self,
628 block: BasicBlock,
6a06907d 629 slice: PlaceBuilder<'tcx>,
60c5eb7d
XL
630 index: Local,
631 expr_span: Span,
632 source_info: SourceInfo,
633 ) -> BasicBlock {
6a06907d
XL
634 let usize_ty = self.tcx.types.usize;
635 let bool_ty = self.tcx.types.bool;
60c5eb7d
XL
636 // bounds check:
637 let len = self.temp(usize_ty, expr_span);
638 let lt = self.temp(bool_ty, expr_span);
639
640 // len = len(slice)
6a06907d
XL
641 self.cfg.push_assign(
642 block,
643 source_info,
644 len,
f2b60f7d 645 Rvalue::Len(slice.into_place(self.tcx, &self.upvars)),
6a06907d 646 );
60c5eb7d
XL
647 // lt = idx < len
648 self.cfg.push_assign(
649 block,
650 source_info,
ba9703b0 651 lt,
6a06907d
XL
652 Rvalue::BinaryOp(
653 BinOp::Lt,
94222f64 654 Box::new((Operand::Copy(Place::from(index)), Operand::Copy(len))),
6a06907d 655 ),
60c5eb7d 656 );
dfeec247 657 let msg = BoundsCheck { len: Operand::Move(len), index: Operand::Copy(Place::from(index)) };
60c5eb7d
XL
658 // assert!(lt, "...")
659 self.assert(block, Operand::Move(lt), true, msg, expr_span)
660 }
661
662 fn add_fake_borrows_of_base(
663 &mut self,
664 base_place: &PlaceBuilder<'tcx>,
665 block: BasicBlock,
666 fake_borrow_temps: &mut Vec<Local>,
667 expr_span: Span,
668 source_info: SourceInfo,
669 ) {
6a06907d 670 let tcx = self.tcx;
fc512014
XL
671 let local = match base_place.base {
672 PlaceBase::Local(local) => local,
5869c6ff 673 PlaceBase::Upvar { .. } => bug!("Expected PlacseBase::Local found Upvar"),
fc512014
XL
674 };
675
676 let place_ty = Place::ty_from(local, &base_place.projection, &self.local_decls, tcx);
1b1a35ee 677 if let ty::Slice(_) = place_ty.ty.kind() {
60c5eb7d
XL
678 // We need to create fake borrows to ensure that the bounds
679 // check that we just did stays valid. Since we can't assign to
680 // unsized values, we only need to ensure that none of the
681 // pointers in the base place are modified.
682 for (idx, elem) in base_place.projection.iter().enumerate().rev() {
683 match elem {
684 ProjectionElem::Deref => {
685 let fake_borrow_deref_ty = Place::ty_from(
fc512014 686 local,
60c5eb7d
XL
687 &base_place.projection[..idx],
688 &self.local_decls,
689 tcx,
dfeec247
XL
690 )
691 .ty;
692 let fake_borrow_ty =
693 tcx.mk_imm_ref(tcx.lifetimes.re_erased, fake_borrow_deref_ty);
694 let fake_borrow_temp =
f9f354fc 695 self.local_decls.push(LocalDecl::new(fake_borrow_ty, expr_span));
60c5eb7d
XL
696 let projection = tcx.intern_place_elems(&base_place.projection[..idx]);
697 self.cfg.push_assign(
698 block,
699 source_info,
ba9703b0 700 fake_borrow_temp.into(),
60c5eb7d
XL
701 Rvalue::Ref(
702 tcx.lifetimes.re_erased,
703 BorrowKind::Shallow,
fc512014 704 Place { local, projection },
60c5eb7d
XL
705 ),
706 );
707 fake_borrow_temps.push(fake_borrow_temp);
708 }
709 ProjectionElem::Index(_) => {
710 let index_ty = Place::ty_from(
fc512014 711 local,
60c5eb7d
XL
712 &base_place.projection[..idx],
713 &self.local_decls,
714 tcx,
715 );
1b1a35ee 716 match index_ty.ty.kind() {
60c5eb7d
XL
717 // The previous index expression has already
718 // done any index expressions needed here.
719 ty::Slice(_) => break,
720 ty::Array(..) => (),
721 _ => bug!("unexpected index base"),
722 }
723 }
724 ProjectionElem::Field(..)
725 | ProjectionElem::Downcast(..)
726 | ProjectionElem::ConstantIndex { .. }
727 | ProjectionElem::Subslice { .. } => (),
728 }
729 }
730 }
731 }
732
733 fn read_fake_borrows(
734 &mut self,
735 bb: BasicBlock,
736 fake_borrow_temps: &mut Vec<Local>,
737 source_info: SourceInfo,
738 ) {
739 // All indexes have been evaluated now, read all of the
740 // fake borrows so that they are live across those index
741 // expressions.
742 for temp in fake_borrow_temps {
743 self.cfg.push_fake_read(bb, source_info, FakeReadCause::ForIndex, Place::from(*temp));
744 }
745 }
e9174d1e 746}
136023e0
XL
747
748/// Precise capture is enabled if the feature gate `capture_disjoint_fields` is enabled or if
749/// user is using Rust Edition 2021 or higher.
750fn enable_precise_capture(tcx: TyCtxt<'_>, closure_span: Span) -> bool {
751 tcx.features().capture_disjoint_fields || closure_span.rust_2021()
752}