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