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