]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_build/src/build/expr/as_place.rs
Auto merge of #104572 - pkubaj:patch-1, r=cuviper
[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 `to_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 an upvar place and tries to resolve it into a `PlaceBuilder`
171 /// with `PlaceBase::Local`
172 #[instrument(level = "trace", skip(cx), ret)]
173 fn to_upvars_resolved_place_builder<'tcx>(
174     cx: &Builder<'_, 'tcx>,
175     var_hir_id: LocalVarId,
176     closure_def_id: LocalDefId,
177     projection: &[PlaceElem<'tcx>],
178 ) -> Option<PlaceBuilder<'tcx>> {
179     let Some((capture_index, capture)) =
180         find_capture_matching_projections(
181             &cx.upvars,
182             var_hir_id,
183             &projection,
184         ) else {
185         let closure_span = cx.tcx.def_span(closure_def_id);
186         if !enable_precise_capture(cx.tcx, closure_span) {
187             bug!(
188                 "No associated capture found for {:?}[{:#?}] even though \
189                     capture_disjoint_fields isn't enabled",
190                 var_hir_id,
191                 projection
192             )
193         } else {
194             debug!(
195                 "No associated capture found for {:?}[{:#?}]",
196                 var_hir_id, projection,
197             );
198         }
199         return None;
200     };
201
202     // Access the capture by accessing the field within the Closure struct.
203     let capture_info = &cx.upvars[capture_index];
204
205     let mut upvar_resolved_place_builder = PlaceBuilder::from(capture_info.use_place);
206
207     // We used some of the projections to build the capture itself,
208     // now we apply the remaining to the upvar resolved place.
209     trace!(?capture.captured_place, ?projection);
210     let remaining_projections = strip_prefix(
211         capture.captured_place.place.base_ty,
212         projection,
213         &capture.captured_place.place.projections,
214     );
215     upvar_resolved_place_builder.projection.extend(remaining_projections);
216
217     Some(upvar_resolved_place_builder)
218 }
219
220 /// Returns projections remaining after stripping an initial prefix of HIR
221 /// projections.
222 ///
223 /// Supports only HIR projection kinds that represent a path that might be
224 /// captured by a closure or a generator, i.e., an `Index` or a `Subslice`
225 /// projection kinds are unsupported.
226 fn strip_prefix<'a, 'tcx>(
227     mut base_ty: Ty<'tcx>,
228     projections: &'a [PlaceElem<'tcx>],
229     prefix_projections: &[HirProjection<'tcx>],
230 ) -> impl Iterator<Item = PlaceElem<'tcx>> + 'a {
231     let mut iter = projections
232         .iter()
233         .copied()
234         // Filter out opaque casts, they are unnecessary in the prefix.
235         .filter(|elem| !matches!(elem, ProjectionElem::OpaqueCast(..)));
236     for projection in prefix_projections {
237         match projection.kind {
238             HirProjectionKind::Deref => {
239                 assert_matches!(iter.next(), Some(ProjectionElem::Deref));
240             }
241             HirProjectionKind::Field(..) => {
242                 if base_ty.is_enum() {
243                     assert_matches!(iter.next(), Some(ProjectionElem::Downcast(..)));
244                 }
245                 assert_matches!(iter.next(), Some(ProjectionElem::Field(..)));
246             }
247             HirProjectionKind::Index | HirProjectionKind::Subslice => {
248                 bug!("unexpected projection kind: {:?}", projection);
249             }
250         }
251         base_ty = projection.ty;
252     }
253     iter
254 }
255
256 impl<'tcx> PlaceBuilder<'tcx> {
257     pub(in crate::build) fn to_place(&self, cx: &Builder<'_, 'tcx>) -> Place<'tcx> {
258         self.try_to_place(cx).unwrap()
259     }
260
261     /// Creates a `Place` or returns `None` if an upvar cannot be resolved
262     pub(in crate::build) fn try_to_place(&self, cx: &Builder<'_, 'tcx>) -> Option<Place<'tcx>> {
263         let resolved = self.resolve_upvar(cx);
264         let builder = resolved.as_ref().unwrap_or(self);
265         let PlaceBase::Local(local) = builder.base else { return None };
266         let projection = cx.tcx.intern_place_elems(&builder.projection);
267         Some(Place { local, projection })
268     }
269
270     /// Attempts to resolve the `PlaceBuilder`.
271     /// Returns `None` if this is not an upvar.
272     ///
273     /// Upvars resolve may fail for a `PlaceBuilder` when attempting to
274     /// resolve a disjoint field whose root variable is not captured
275     /// (destructured assignments) or when attempting to resolve a root
276     /// variable (discriminant matching with only wildcard arm) that is
277     /// not captured. This can happen because the final mir that will be
278     /// generated doesn't require a read for this place. Failures will only
279     /// happen inside closures.
280     pub(in crate::build) fn resolve_upvar(
281         &self,
282         cx: &Builder<'_, 'tcx>,
283     ) -> Option<PlaceBuilder<'tcx>> {
284         let PlaceBase::Upvar { var_hir_id, closure_def_id } = self.base else {
285             return None;
286         };
287         to_upvars_resolved_place_builder(cx, var_hir_id, closure_def_id, &self.projection)
288     }
289
290     pub(crate) fn base(&self) -> PlaceBase {
291         self.base
292     }
293
294     pub(crate) fn projection(&self) -> &[PlaceElem<'tcx>] {
295         &self.projection
296     }
297
298     pub(crate) fn field(self, f: Field, ty: Ty<'tcx>) -> Self {
299         self.project(PlaceElem::Field(f, ty))
300     }
301
302     pub(crate) fn deref(self) -> Self {
303         self.project(PlaceElem::Deref)
304     }
305
306     pub(crate) fn downcast(self, adt_def: AdtDef<'tcx>, variant_index: VariantIdx) -> Self {
307         self.project(PlaceElem::Downcast(Some(adt_def.variant(variant_index).name), variant_index))
308     }
309
310     fn index(self, index: Local) -> Self {
311         self.project(PlaceElem::Index(index))
312     }
313
314     pub(crate) fn project(mut self, elem: PlaceElem<'tcx>) -> Self {
315         self.projection.push(elem);
316         self
317     }
318
319     /// Same as `.clone().project(..)` but more efficient
320     pub(crate) fn clone_project(&self, elem: PlaceElem<'tcx>) -> Self {
321         Self {
322             base: self.base,
323             projection: Vec::from_iter(self.projection.iter().copied().chain([elem])),
324         }
325     }
326 }
327
328 impl<'tcx> From<Local> for PlaceBuilder<'tcx> {
329     fn from(local: Local) -> Self {
330         Self { base: PlaceBase::Local(local), projection: Vec::new() }
331     }
332 }
333
334 impl<'tcx> From<PlaceBase> for PlaceBuilder<'tcx> {
335     fn from(base: PlaceBase) -> Self {
336         Self { base, projection: Vec::new() }
337     }
338 }
339
340 impl<'tcx> From<Place<'tcx>> for PlaceBuilder<'tcx> {
341     fn from(p: Place<'tcx>) -> Self {
342         Self { base: PlaceBase::Local(p.local), projection: p.projection.to_vec() }
343     }
344 }
345
346 impl<'a, 'tcx> Builder<'a, 'tcx> {
347     /// Compile `expr`, yielding a place that we can move from etc.
348     ///
349     /// WARNING: Any user code might:
350     /// * Invalidate any slice bounds checks performed.
351     /// * Change the address that this `Place` refers to.
352     /// * Modify the memory that this place refers to.
353     /// * Invalidate the memory that this place refers to, this will be caught
354     ///   by borrow checking.
355     ///
356     /// Extra care is needed if any user code is allowed to run between calling
357     /// this method and using it, as is the case for `match` and index
358     /// expressions.
359     pub(crate) fn as_place(
360         &mut self,
361         mut block: BasicBlock,
362         expr: &Expr<'tcx>,
363     ) -> BlockAnd<Place<'tcx>> {
364         let place_builder = unpack!(block = self.as_place_builder(block, expr));
365         block.and(place_builder.to_place(self))
366     }
367
368     /// This is used when constructing a compound `Place`, so that we can avoid creating
369     /// intermediate `Place` values until we know the full set of projections.
370     pub(crate) fn as_place_builder(
371         &mut self,
372         block: BasicBlock,
373         expr: &Expr<'tcx>,
374     ) -> BlockAnd<PlaceBuilder<'tcx>> {
375         self.expr_as_place(block, expr, Mutability::Mut, None)
376     }
377
378     /// Compile `expr`, yielding a place that we can move from etc.
379     /// Mutability note: The caller of this method promises only to read from the resulting
380     /// place. The place itself may or may not be mutable:
381     /// * If this expr is a place expr like a.b, then we will return that place.
382     /// * Otherwise, a temporary is created: in that event, it will be an immutable temporary.
383     pub(crate) fn as_read_only_place(
384         &mut self,
385         mut block: BasicBlock,
386         expr: &Expr<'tcx>,
387     ) -> BlockAnd<Place<'tcx>> {
388         let place_builder = unpack!(block = self.as_read_only_place_builder(block, expr));
389         block.and(place_builder.to_place(self))
390     }
391
392     /// This is used when constructing a compound `Place`, so that we can avoid creating
393     /// intermediate `Place` values until we know the full set of projections.
394     /// Mutability note: The caller of this method promises only to read from the resulting
395     /// place. The place itself may or may not be mutable:
396     /// * If this expr is a place expr like a.b, then we will return that place.
397     /// * Otherwise, a temporary is created: in that event, it will be an immutable temporary.
398     fn as_read_only_place_builder(
399         &mut self,
400         block: BasicBlock,
401         expr: &Expr<'tcx>,
402     ) -> BlockAnd<PlaceBuilder<'tcx>> {
403         self.expr_as_place(block, expr, Mutability::Not, None)
404     }
405
406     fn expr_as_place(
407         &mut self,
408         mut block: BasicBlock,
409         expr: &Expr<'tcx>,
410         mutability: Mutability,
411         fake_borrow_temps: Option<&mut Vec<Local>>,
412     ) -> BlockAnd<PlaceBuilder<'tcx>> {
413         debug!("expr_as_place(block={:?}, expr={:?}, mutability={:?})", block, expr, mutability);
414
415         let this = self;
416         let expr_span = expr.span;
417         let source_info = this.source_info(expr_span);
418         match expr.kind {
419             ExprKind::Scope { region_scope, lint_level, value } => {
420                 this.in_scope((region_scope, source_info), lint_level, |this| {
421                     this.expr_as_place(block, &this.thir[value], mutability, fake_borrow_temps)
422                 })
423             }
424             ExprKind::Field { lhs, variant_index, name } => {
425                 let lhs = &this.thir[lhs];
426                 let mut place_builder =
427                     unpack!(block = this.expr_as_place(block, lhs, mutability, fake_borrow_temps,));
428                 if let ty::Adt(adt_def, _) = lhs.ty.kind() {
429                     if adt_def.is_enum() {
430                         place_builder = place_builder.downcast(*adt_def, variant_index);
431                     }
432                 }
433                 block.and(place_builder.field(name, expr.ty))
434             }
435             ExprKind::Deref { arg } => {
436                 let place_builder = unpack!(
437                     block =
438                         this.expr_as_place(block, &this.thir[arg], mutability, fake_borrow_temps,)
439                 );
440                 block.and(place_builder.deref())
441             }
442             ExprKind::Index { lhs, index } => this.lower_index_expression(
443                 block,
444                 &this.thir[lhs],
445                 &this.thir[index],
446                 mutability,
447                 fake_borrow_temps,
448                 expr.temp_lifetime,
449                 expr_span,
450                 source_info,
451             ),
452             ExprKind::UpvarRef { closure_def_id, var_hir_id } => {
453                 this.lower_captured_upvar(block, closure_def_id.expect_local(), var_hir_id)
454             }
455
456             ExprKind::VarRef { id } => {
457                 let place_builder = if this.is_bound_var_in_guard(id) {
458                     let index = this.var_local_id(id, RefWithinGuard);
459                     PlaceBuilder::from(index).deref()
460                 } else {
461                     let index = this.var_local_id(id, OutsideGuard);
462                     PlaceBuilder::from(index)
463                 };
464                 block.and(place_builder)
465             }
466
467             ExprKind::PlaceTypeAscription { source, ref user_ty } => {
468                 let place_builder = unpack!(
469                     block = this.expr_as_place(
470                         block,
471                         &this.thir[source],
472                         mutability,
473                         fake_borrow_temps,
474                     )
475                 );
476                 if let Some(user_ty) = user_ty {
477                     let annotation_index =
478                         this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
479                             span: source_info.span,
480                             user_ty: user_ty.clone(),
481                             inferred_ty: expr.ty,
482                         });
483
484                     let place = place_builder.to_place(this);
485                     this.cfg.push(
486                         block,
487                         Statement {
488                             source_info,
489                             kind: StatementKind::AscribeUserType(
490                                 Box::new((
491                                     place,
492                                     UserTypeProjection { base: annotation_index, projs: vec![] },
493                                 )),
494                                 Variance::Invariant,
495                             ),
496                         },
497                     );
498                 }
499                 block.and(place_builder)
500             }
501             ExprKind::ValueTypeAscription { source, ref user_ty } => {
502                 let source = &this.thir[source];
503                 let temp =
504                     unpack!(block = this.as_temp(block, source.temp_lifetime, source, mutability));
505                 if let Some(user_ty) = user_ty {
506                     let annotation_index =
507                         this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
508                             span: source_info.span,
509                             user_ty: user_ty.clone(),
510                             inferred_ty: expr.ty,
511                         });
512                     this.cfg.push(
513                         block,
514                         Statement {
515                             source_info,
516                             kind: StatementKind::AscribeUserType(
517                                 Box::new((
518                                     Place::from(temp),
519                                     UserTypeProjection { base: annotation_index, projs: vec![] },
520                                 )),
521                                 Variance::Invariant,
522                             ),
523                         },
524                     );
525                 }
526                 block.and(PlaceBuilder::from(temp))
527             }
528
529             ExprKind::Array { .. }
530             | ExprKind::Tuple { .. }
531             | ExprKind::Adt { .. }
532             | ExprKind::Closure { .. }
533             | ExprKind::Unary { .. }
534             | ExprKind::Binary { .. }
535             | ExprKind::LogicalOp { .. }
536             | ExprKind::Box { .. }
537             | ExprKind::Cast { .. }
538             | ExprKind::Use { .. }
539             | ExprKind::NeverToAny { .. }
540             | ExprKind::Pointer { .. }
541             | ExprKind::Repeat { .. }
542             | ExprKind::Borrow { .. }
543             | ExprKind::AddressOf { .. }
544             | ExprKind::Match { .. }
545             | ExprKind::If { .. }
546             | ExprKind::Loop { .. }
547             | ExprKind::Block { .. }
548             | ExprKind::Let { .. }
549             | ExprKind::Assign { .. }
550             | ExprKind::AssignOp { .. }
551             | ExprKind::Break { .. }
552             | ExprKind::Continue { .. }
553             | ExprKind::Return { .. }
554             | ExprKind::Literal { .. }
555             | ExprKind::NamedConst { .. }
556             | ExprKind::NonHirLiteral { .. }
557             | ExprKind::ZstLiteral { .. }
558             | ExprKind::ConstParam { .. }
559             | ExprKind::ConstBlock { .. }
560             | ExprKind::StaticRef { .. }
561             | ExprKind::InlineAsm { .. }
562             | ExprKind::Yield { .. }
563             | ExprKind::ThreadLocalRef(_)
564             | ExprKind::Call { .. } => {
565                 // these are not places, so we need to make a temporary.
566                 debug_assert!(!matches!(Category::of(&expr.kind), Some(Category::Place)));
567                 let temp =
568                     unpack!(block = this.as_temp(block, expr.temp_lifetime, expr, mutability));
569                 block.and(PlaceBuilder::from(temp))
570             }
571         }
572     }
573
574     /// Lower a captured upvar. Note we might not know the actual capture index,
575     /// so we create a place starting from `PlaceBase::Upvar`, which will be resolved
576     /// once all projections that allow us to identify a capture have been applied.
577     fn lower_captured_upvar(
578         &mut self,
579         block: BasicBlock,
580         closure_def_id: LocalDefId,
581         var_hir_id: LocalVarId,
582     ) -> BlockAnd<PlaceBuilder<'tcx>> {
583         block.and(PlaceBuilder::from(PlaceBase::Upvar { var_hir_id, closure_def_id }))
584     }
585
586     /// Lower an index expression
587     ///
588     /// This has two complications;
589     ///
590     /// * We need to do a bounds check.
591     /// * We need to ensure that the bounds check can't be invalidated using an
592     ///   expression like `x[1][{x = y; 2}]`. We use fake borrows here to ensure
593     ///   that this is the case.
594     fn lower_index_expression(
595         &mut self,
596         mut block: BasicBlock,
597         base: &Expr<'tcx>,
598         index: &Expr<'tcx>,
599         mutability: Mutability,
600         fake_borrow_temps: Option<&mut Vec<Local>>,
601         temp_lifetime: Option<region::Scope>,
602         expr_span: Span,
603         source_info: SourceInfo,
604     ) -> BlockAnd<PlaceBuilder<'tcx>> {
605         let base_fake_borrow_temps = &mut Vec::new();
606         let is_outermost_index = fake_borrow_temps.is_none();
607         let fake_borrow_temps = fake_borrow_temps.unwrap_or(base_fake_borrow_temps);
608
609         let base_place =
610             unpack!(block = self.expr_as_place(block, base, mutability, Some(fake_borrow_temps),));
611
612         // Making this a *fresh* temporary means we do not have to worry about
613         // the index changing later: Nothing will ever change this temporary.
614         // The "retagging" transformation (for Stacked Borrows) relies on this.
615         let idx = unpack!(block = self.as_temp(block, temp_lifetime, index, Mutability::Not,));
616
617         block = self.bounds_check(block, &base_place, idx, expr_span, source_info);
618
619         if is_outermost_index {
620             self.read_fake_borrows(block, fake_borrow_temps, source_info)
621         } else {
622             self.add_fake_borrows_of_base(
623                 base_place.to_place(self),
624                 block,
625                 fake_borrow_temps,
626                 expr_span,
627                 source_info,
628             );
629         }
630
631         block.and(base_place.index(idx))
632     }
633
634     fn bounds_check(
635         &mut self,
636         block: BasicBlock,
637         slice: &PlaceBuilder<'tcx>,
638         index: Local,
639         expr_span: Span,
640         source_info: SourceInfo,
641     ) -> BasicBlock {
642         let usize_ty = self.tcx.types.usize;
643         let bool_ty = self.tcx.types.bool;
644         // bounds check:
645         let len = self.temp(usize_ty, expr_span);
646         let lt = self.temp(bool_ty, expr_span);
647
648         // len = len(slice)
649         self.cfg.push_assign(block, source_info, len, Rvalue::Len(slice.to_place(self)));
650         // lt = idx < len
651         self.cfg.push_assign(
652             block,
653             source_info,
654             lt,
655             Rvalue::BinaryOp(
656                 BinOp::Lt,
657                 Box::new((Operand::Copy(Place::from(index)), Operand::Copy(len))),
658             ),
659         );
660         let msg = BoundsCheck { len: Operand::Move(len), index: Operand::Copy(Place::from(index)) };
661         // assert!(lt, "...")
662         self.assert(block, Operand::Move(lt), true, msg, expr_span)
663     }
664
665     fn add_fake_borrows_of_base(
666         &mut self,
667         base_place: Place<'tcx>,
668         block: BasicBlock,
669         fake_borrow_temps: &mut Vec<Local>,
670         expr_span: Span,
671         source_info: SourceInfo,
672     ) {
673         let tcx = self.tcx;
674
675         let place_ty = base_place.ty(&self.local_decls, tcx);
676         if let ty::Slice(_) = place_ty.ty.kind() {
677             // We need to create fake borrows to ensure that the bounds
678             // check that we just did stays valid. Since we can't assign to
679             // unsized values, we only need to ensure that none of the
680             // pointers in the base place are modified.
681             for (idx, elem) in base_place.projection.iter().enumerate().rev() {
682                 match elem {
683                     ProjectionElem::Deref => {
684                         let fake_borrow_deref_ty = Place::ty_from(
685                             base_place.local,
686                             &base_place.projection[..idx],
687                             &self.local_decls,
688                             tcx,
689                         )
690                         .ty;
691                         let fake_borrow_ty =
692                             tcx.mk_imm_ref(tcx.lifetimes.re_erased, fake_borrow_deref_ty);
693                         let fake_borrow_temp =
694                             self.local_decls.push(LocalDecl::new(fake_borrow_ty, expr_span));
695                         let projection = tcx.intern_place_elems(&base_place.projection[..idx]);
696                         self.cfg.push_assign(
697                             block,
698                             source_info,
699                             fake_borrow_temp.into(),
700                             Rvalue::Ref(
701                                 tcx.lifetimes.re_erased,
702                                 BorrowKind::Shallow,
703                                 Place { local: base_place.local, projection },
704                             ),
705                         );
706                         fake_borrow_temps.push(fake_borrow_temp);
707                     }
708                     ProjectionElem::Index(_) => {
709                         let index_ty = Place::ty_from(
710                             base_place.local,
711                             &base_place.projection[..idx],
712                             &self.local_decls,
713                             tcx,
714                         );
715                         match index_ty.ty.kind() {
716                             // The previous index expression has already
717                             // done any index expressions needed here.
718                             ty::Slice(_) => break,
719                             ty::Array(..) => (),
720                             _ => bug!("unexpected index base"),
721                         }
722                     }
723                     ProjectionElem::Field(..)
724                     | ProjectionElem::Downcast(..)
725                     | ProjectionElem::OpaqueCast(..)
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     }
746 }
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.
750 fn enable_precise_capture(tcx: TyCtxt<'_>, closure_span: Span) -> bool {
751     tcx.features().capture_disjoint_fields || closure_span.rust_2021()
752 }