]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_build/src/build/expr/as_place.rs
Rollup merge of #105873 - matthiaskrgr:clippy_fmt, r=Nilstrieb
[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::convert::From;
23 use std::iter;
24
25 /// `PlaceBuilder` is used to create places during MIR construction. It allows you to "build up" a
26 /// place by pushing more and more projections onto the end, and then convert the final set into a
27 /// place using the `into_place` method.
28 ///
29 /// This is used internally when building a place for an expression like `a.b.c`. The fields `b`
30 /// and `c` can be progressively pushed onto the place builder that is created when converting `a`.
31 #[derive(Clone, Debug, PartialEq)]
32 pub(in crate::build) enum PlaceBuilder<'tcx> {
33     /// Denotes the start of a `Place`.
34     ///
35     /// We use `PlaceElem` since this has all `Field` types available.
36     Local { local: Local, projection: Vec<PlaceElem<'tcx>> },
37
38     /// When building place for an expression within a closure, the place might start off a
39     /// captured path. When `capture_disjoint_fields` is enabled, we might not know the capture
40     /// index (within the desugared closure) of the captured path until most of the projections
41     /// are applied. We use `PlaceBuilder::Upvar` to keep track of the root variable off of which the
42     /// captured path starts, the closure the capture belongs to and the trait the closure
43     /// implements.
44     ///
45     /// Once we have figured out the capture index, we can convert the place builder to
46     /// `PlaceBuilder::Local`.
47     ///
48     /// Consider the following example
49     /// ```rust
50     /// let t = (((10, 10), 10), 10);
51     ///
52     /// let c = || {
53     ///     println!("{}", t.0.0.0);
54     /// };
55     /// ```
56     /// Here the THIR expression for `t.0.0.0` will be something like
57     ///
58     /// ```ignore (illustrative)
59     /// * Field(0)
60     ///     * Field(0)
61     ///         * Field(0)
62     ///             * UpvarRef(t)
63     /// ```
64     ///
65     /// When `capture_disjoint_fields` is enabled, `t.0.0.0` is captured and we won't be able to
66     /// figure out that it is captured until all the `Field` projections are applied.
67     ///
68     /// Note: in contrast to `PlaceBuilder::Local` we have not yet determined all `Field` types
69     /// and will only do so once converting to `PlaceBuilder::Local`.
70     Upvar { upvar: Upvar, projection: Vec<UpvarProjectionElem<'tcx>> },
71 }
72
73 #[derive(Copy, Clone, Debug, PartialEq)]
74 pub(crate) struct Upvar {
75     var_hir_id: LocalVarId,
76     closure_def_id: LocalDefId,
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: &[UpvarProjectionElem<'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: &[UpvarProjectionElem<'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: &[UpvarProjectionElem<'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
201         return None;
202     };
203
204     // Access the capture by accessing the field within the Closure struct.
205     let capture_info = &cx.upvars[capture_index];
206
207     let Place { local: upvar_resolved_local, projection: local_projection } =
208         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     let upvar_projection = strip_prefix(
213         capture.captured_place.place.base_ty,
214         projection,
215         &capture.captured_place.place.projections,
216     );
217
218     let upvar_resolved_place_builder = PlaceBuilder::construct_local_place_builder(
219         cx,
220         upvar_resolved_local,
221         local_projection.as_slice(),
222         upvar_projection,
223     );
224
225     assert!(matches!(upvar_resolved_place_builder, PlaceBuilder::Local { .. }));
226
227     Some(upvar_resolved_place_builder)
228 }
229
230 /// Returns projections remaining after stripping an initial prefix of HIR
231 /// projections.
232 ///
233 /// Supports only HIR projection kinds that represent a path that might be
234 /// captured by a closure or a generator, i.e., an `Index` or a `Subslice`
235 /// projection kinds are unsupported.
236 fn strip_prefix<'a, 'tcx>(
237     mut base_ty: Ty<'tcx>,
238     projections: &'a [UpvarProjectionElem<'tcx>],
239     prefix_projections: &[HirProjection<'tcx>],
240 ) -> impl Iterator<Item = UpvarProjectionElem<'tcx>> + 'a {
241     let mut iter = projections
242         .iter()
243         .copied()
244         // Filter out opaque casts, they are unnecessary in the prefix.
245         .filter(|elem| !matches!(elem, ProjectionElem::OpaqueCast(..)));
246     for projection in prefix_projections {
247         debug!(?projection, ?projection.ty);
248
249         match projection.kind {
250             HirProjectionKind::Deref => {
251                 assert_matches!(iter.next(), Some(ProjectionElem::Deref));
252             }
253             HirProjectionKind::Field(..) => {
254                 if base_ty.is_enum() {
255                     assert_matches!(iter.next(), Some(ProjectionElem::Downcast(..)));
256                 }
257                 assert_matches!(iter.next(), Some(ProjectionElem::Field(..)));
258             }
259             HirProjectionKind::Index | HirProjectionKind::Subslice => {
260                 bug!("unexpected projection kind: {:?}", projection);
261             }
262         }
263
264         base_ty = projection.ty;
265     }
266
267     iter
268 }
269
270 impl<'tcx> PlaceBuilder<'tcx> {
271     pub(in crate::build) fn to_place(&self, cx: &Builder<'_, 'tcx>) -> Place<'tcx> {
272         self.try_to_place(cx).unwrap()
273     }
274
275     /// Creates a `Place` or returns `None` if an upvar cannot be resolved
276     pub(in crate::build) fn try_to_place(&self, cx: &Builder<'_, 'tcx>) -> Option<Place<'tcx>> {
277         let resolved = self.resolve_upvar(cx);
278         let builder = resolved.as_ref().unwrap_or(self);
279         let PlaceBuilder::Local{local, ref projection} = builder else { return None };
280         let projection = cx.tcx.intern_place_elems(projection);
281         Some(Place { local: *local, projection })
282     }
283
284     /// Attempts to resolve the `PlaceBuilder`.
285     /// Returns `None` if this is not an upvar.
286     ///
287     /// Upvars resolve may fail for a `PlaceBuilder` when attempting to
288     /// resolve a disjoint field whose root variable is not captured
289     /// (destructured assignments) or when attempting to resolve a root
290     /// variable (discriminant matching with only wildcard arm) that is
291     /// not captured. This can happen because the final mir that will be
292     /// generated doesn't require a read for this place. Failures will only
293     /// happen inside closures.
294     pub(in crate::build) fn resolve_upvar(
295         &self,
296         cx: &Builder<'_, 'tcx>,
297     ) -> Option<PlaceBuilder<'tcx>> {
298         let PlaceBuilder::Upvar{ upvar: Upvar {var_hir_id, closure_def_id }, projection} = self else {
299             return None;
300         };
301
302         to_upvars_resolved_place_builder(cx, *var_hir_id, *closure_def_id, &projection)
303     }
304
305     #[instrument(skip(cx), level = "debug")]
306     pub(crate) fn field(self, cx: &Builder<'_, 'tcx>, f: Field) -> Self {
307         match self.clone() {
308             PlaceBuilder::Local { local, projection } => {
309                 let base_place = PlaceBuilder::Local { local, projection };
310                 let PlaceTy { ty, variant_index } =
311                     base_place.to_place(cx).ty(&cx.local_decls, cx.tcx);
312                 let base_ty = cx.tcx.normalize_erasing_regions(cx.param_env, ty);
313
314                 let field_ty = PlaceBuilder::compute_field_ty(cx, f, base_ty, variant_index);
315
316                 self.project(ProjectionElem::Field(f, field_ty))
317             }
318             PlaceBuilder::Upvar { upvar, mut projection } => {
319                 projection.push(ProjectionElem::Field(f, ()));
320                 PlaceBuilder::Upvar { upvar, projection }
321             }
322         }
323     }
324
325     pub(crate) fn deref(self) -> Self {
326         self.project(PlaceElem::Deref)
327     }
328
329     pub(crate) fn downcast(self, adt_def: AdtDef<'tcx>, variant_index: VariantIdx) -> Self {
330         self.project(PlaceElem::Downcast(Some(adt_def.variant(variant_index).name), variant_index))
331     }
332
333     fn index(self, index: Local) -> Self {
334         self.project(PlaceElem::Index(index))
335     }
336
337     #[instrument(level = "debug")]
338     pub(crate) fn project(self, elem: PlaceElem<'tcx>) -> Self {
339         let result = match self {
340             PlaceBuilder::Local { local, mut projection } => {
341                 projection.push(elem);
342                 PlaceBuilder::Local { local, projection }
343             }
344             PlaceBuilder::Upvar { upvar, mut projection } => {
345                 projection.push(elem.into());
346                 PlaceBuilder::Upvar { upvar, projection }
347             }
348         };
349
350         debug!(?result);
351         result
352     }
353
354     /// Same as `.clone().project(..)` but more efficient
355     pub(crate) fn clone_project(&self, elem: PlaceElem<'tcx>) -> Self {
356         match self {
357             PlaceBuilder::Local { local, projection } => PlaceBuilder::Local {
358                 local: *local,
359                 projection: Vec::from_iter(projection.iter().copied().chain([elem])),
360             },
361             PlaceBuilder::Upvar { upvar, projection } => PlaceBuilder::Upvar {
362                 upvar: *upvar,
363                 projection: Vec::from_iter(projection.iter().copied().chain([elem.into()])),
364             },
365         }
366     }
367
368     /// Similar to `Place::ty` but needed during mir building.
369     ///
370     /// Applies the projections in the `PlaceBuilder` to the base
371     /// type.
372     ///
373     /// Fallible as the root of this place may be an upvar for
374     /// which no base type can be determined.
375     #[instrument(skip(cx), level = "debug")]
376     fn compute_field_ty(
377         cx: &Builder<'_, 'tcx>,
378         field: Field,
379         base_ty: Ty<'tcx>,
380         variant_index: Option<VariantIdx>,
381     ) -> Ty<'tcx> {
382         let field_idx = field.as_usize();
383         let field_ty = match base_ty.kind() {
384             ty::Adt(adt_def, substs) if adt_def.is_enum() => {
385                 let variant_idx = variant_index.unwrap();
386                 adt_def.variant(variant_idx).fields[field_idx].ty(cx.tcx, substs)
387             }
388             ty::Adt(adt_def, substs) => adt_def
389                 .all_fields()
390                 .nth(field_idx)
391                 .unwrap_or_else(|| {
392                     bug!(
393                         "expected to take field with idx {:?} of fields of {:?}",
394                         field_idx,
395                         adt_def
396                     )
397                 })
398                 .ty(cx.tcx, substs),
399             ty::Tuple(elems) => elems.iter().nth(field_idx).unwrap_or_else(|| {
400                 bug!("expected to take field with idx {:?} of {:?}", field_idx, elems)
401             }),
402             ty::Closure(_, substs) => {
403                 let substs = substs.as_closure();
404                 let Some(f_ty) = substs.upvar_tys().nth(field_idx) else {
405                     bug!("expected to take field with idx {:?} of {:?}", field_idx, substs.upvar_tys().collect::<Vec<_>>());
406                 };
407
408                 f_ty
409             }
410             &ty::Generator(def_id, substs, _) => {
411                 if let Some(var) = variant_index {
412                     let gen_body = cx.tcx.optimized_mir(def_id);
413                     let Some(layout) = gen_body.generator_layout() else {
414                         bug!("No generator layout for {:?}", base_ty);
415                     };
416
417                     let Some(&local) = layout.variant_fields[var].get(field) else {
418                         bug!("expected to take field {:?} of {:?}", field, layout.variant_fields[var]);
419                     };
420
421                     let Some(&f_ty) = layout.field_tys.get(local) else {
422                         bug!("expected to get element for {:?} in {:?}", local, layout.field_tys);
423                     };
424
425                     f_ty
426                 } else {
427                     let Some(f_ty) = substs.as_generator().prefix_tys().nth(field.index()) else {
428                         bug!(
429                             "expected to take index {:?} in {:?}",
430                             field.index(),
431                             substs.as_generator().prefix_tys().collect::<Vec<_>>()
432                         );
433                     };
434
435                     f_ty
436                 }
437             }
438             _ => bug!("couldn't create field type, unexpected base type: {:?}", base_ty),
439         };
440
441         cx.tcx.normalize_erasing_regions(cx.param_env, field_ty)
442     }
443
444     /// Creates a `PlaceBuilder::Local` from a `PlaceBuilder::Upvar` whose upvars
445     /// are resolved. This function takes two kinds of projections: `local_projection`
446     /// contains the projections of the captured upvar and `upvar_projection` the
447     /// projections that are applied to the captured upvar. The main purpose of this
448     /// function is to figure out the `Ty`s of the field projections in `upvar_projection`.
449     #[instrument(skip(cx, local, upvar_projection))]
450     fn construct_local_place_builder(
451         cx: &Builder<'_, 'tcx>,
452         local: Local,
453         local_projection: &[PlaceElem<'tcx>],
454         upvar_projection: impl Iterator<Item = UpvarProjectionElem<'tcx>>,
455     ) -> Self {
456         // We maintain a `Ty` to which we apply a projection in each iteration over `upvar_projection`.
457         // This `ancestor_ty` let's us infer the field type whenever we encounter a
458         // `ProjectionElem::Field`.
459         let (mut ancestor_ty, mut opt_variant_idx) =
460             local_projections_to_ty(cx, local, local_projection);
461
462         // We add all projection elements we encounter to this `Vec`.
463         let mut local_projection = local_projection.to_vec();
464
465         for (i, proj) in upvar_projection.enumerate() {
466             debug!("i: {:?}, proj: {:?}, local_projection: {:?}", i, proj, local_projection);
467             match proj {
468                 ProjectionElem::Field(field, _) => {
469                     let field_ty =
470                         PlaceBuilder::compute_field_ty(cx, field, ancestor_ty, opt_variant_idx);
471                     debug!(?field_ty);
472
473                     local_projection.push(ProjectionElem::Field(field, field_ty));
474                     ancestor_ty = field_ty;
475                     opt_variant_idx = None;
476                 }
477                 _ => {
478                     let proj = upvar_proj_to_place_elem_no_field_proj(proj);
479                     (ancestor_ty, opt_variant_idx) = project_ty(cx.tcx, ancestor_ty, proj);
480                     local_projection.push(proj);
481                 }
482             }
483         }
484
485         PlaceBuilder::Local { local, projection: local_projection }
486     }
487 }
488
489 impl<'tcx> From<Local> for PlaceBuilder<'tcx> {
490     fn from(local: Local) -> Self {
491         Self::Local { local, projection: Vec::new() }
492     }
493 }
494
495 impl<'tcx> From<Place<'tcx>> for PlaceBuilder<'tcx> {
496     fn from(p: Place<'tcx>) -> Self {
497         Self::Local { local: p.local, projection: p.projection.to_vec() }
498     }
499 }
500
501 fn project_ty<'tcx>(
502     tcx: TyCtxt<'tcx>,
503     ty: Ty<'tcx>,
504     elem: PlaceElem<'tcx>,
505 ) -> (Ty<'tcx>, Option<VariantIdx>) {
506     match elem {
507         ProjectionElem::Deref => {
508             let updated_ty = ty
509                 .builtin_deref(true)
510                 .unwrap_or_else(|| bug!("deref projection of non-dereferenceable ty {:?}", ty))
511                 .ty;
512
513             (updated_ty, None)
514         }
515         ProjectionElem::Index(_) | ProjectionElem::ConstantIndex { .. } => {
516             (ty.builtin_index().unwrap(), None)
517         }
518         ProjectionElem::Subslice { from, to, from_end } => {
519             let ty = match ty.kind() {
520                 ty::Slice(..) => ty,
521                 ty::Array(inner, _) if !from_end => tcx.mk_array(*inner, (to - from) as u64),
522                 ty::Array(inner, size) if from_end => {
523                     let size = size.eval_usize(tcx, ty::ParamEnv::empty());
524                     let len = size - (from as u64) - (to as u64);
525                     tcx.mk_array(*inner, len)
526                 }
527                 _ => bug!("cannot subslice non-array type: `{:?}`", ty),
528             };
529
530             (ty, None)
531         }
532         ProjectionElem::Downcast(_, variant_idx) => (ty, Some(variant_idx)),
533         ProjectionElem::Field(_, ty) => (ty, None),
534         ProjectionElem::OpaqueCast(..) => bug!("didn't expect OpaqueCast"),
535     }
536 }
537
538 fn local_projections_to_ty<'a, 'tcx>(
539     cx: &'a Builder<'a, 'tcx>,
540     local: Local,
541     projection: &'a [PlaceElem<'tcx>],
542 ) -> (Ty<'tcx>, Option<VariantIdx>) {
543     let local_ty = cx.local_decls.local_decls()[local].ty;
544     projection.iter().fold((local_ty, None), |ty_variant_idx, elem| {
545         let ty = ty_variant_idx.0;
546         project_ty(cx.tcx, ty, *elem)
547     })
548 }
549
550 // Converts an `UpvarProjectionElem` to `PlaceElem`, ICE'ing when being passed a
551 // field projection.
552 fn upvar_proj_to_place_elem_no_field_proj<'tcx>(
553     upvar_proj: UpvarProjectionElem<'tcx>,
554 ) -> PlaceElem<'tcx> {
555     match upvar_proj {
556         ProjectionElem::Deref => ProjectionElem::Deref,
557         ProjectionElem::Index(i) => ProjectionElem::Index(i),
558         ProjectionElem::ConstantIndex { offset, min_length, from_end } => {
559             ProjectionElem::ConstantIndex { offset, min_length, from_end }
560         }
561         ProjectionElem::Subslice { from, to, from_end } => {
562             ProjectionElem::Subslice { from, to, from_end }
563         }
564         ProjectionElem::Downcast(ty, variant_idx) => ProjectionElem::Downcast(ty, variant_idx),
565         ProjectionElem::OpaqueCast(ty) => ProjectionElem::OpaqueCast(ty),
566         ProjectionElem::Field(..) => bug!("should not be called with `ProjectionElem::Field`"),
567     }
568 }
569
570 impl<'a, 'tcx> Builder<'a, 'tcx> {
571     /// Compile `expr`, yielding a place that we can move from etc.
572     ///
573     /// WARNING: Any user code might:
574     /// * Invalidate any slice bounds checks performed.
575     /// * Change the address that this `Place` refers to.
576     /// * Modify the memory that this place refers to.
577     /// * Invalidate the memory that this place refers to, this will be caught
578     ///   by borrow checking.
579     ///
580     /// Extra care is needed if any user code is allowed to run between calling
581     /// this method and using it, as is the case for `match` and index
582     /// expressions.
583     pub(crate) fn as_place(
584         &mut self,
585         mut block: BasicBlock,
586         expr: &Expr<'tcx>,
587     ) -> BlockAnd<Place<'tcx>> {
588         let place_builder = unpack!(block = self.as_place_builder(block, expr));
589         block.and(place_builder.to_place(self))
590     }
591
592     /// This is used when constructing a compound `Place`, so that we can avoid creating
593     /// intermediate `Place` values until we know the full set of projections.
594     pub(crate) fn as_place_builder(
595         &mut self,
596         block: BasicBlock,
597         expr: &Expr<'tcx>,
598     ) -> BlockAnd<PlaceBuilder<'tcx>> {
599         self.expr_as_place(block, expr, Mutability::Mut, None)
600     }
601
602     /// Compile `expr`, yielding a place that we can move from etc.
603     /// Mutability note: The caller of this method promises only to read from the resulting
604     /// place. The place itself may or may not be mutable:
605     /// * If this expr is a place expr like a.b, then we will return that place.
606     /// * Otherwise, a temporary is created: in that event, it will be an immutable temporary.
607     pub(crate) fn as_read_only_place(
608         &mut self,
609         mut block: BasicBlock,
610         expr: &Expr<'tcx>,
611     ) -> BlockAnd<Place<'tcx>> {
612         let place_builder = unpack!(block = self.as_read_only_place_builder(block, expr));
613         block.and(place_builder.to_place(self))
614     }
615
616     /// This is used when constructing a compound `Place`, so that we can avoid creating
617     /// intermediate `Place` values until we know the full set of projections.
618     /// Mutability note: The caller of this method promises only to read from the resulting
619     /// place. The place itself may or may not be mutable:
620     /// * If this expr is a place expr like a.b, then we will return that place.
621     /// * Otherwise, a temporary is created: in that event, it will be an immutable temporary.
622     fn as_read_only_place_builder(
623         &mut self,
624         block: BasicBlock,
625         expr: &Expr<'tcx>,
626     ) -> BlockAnd<PlaceBuilder<'tcx>> {
627         self.expr_as_place(block, expr, Mutability::Not, None)
628     }
629
630     #[instrument(skip(self, fake_borrow_temps), level = "debug")]
631     fn expr_as_place(
632         &mut self,
633         mut block: BasicBlock,
634         expr: &Expr<'tcx>,
635         mutability: Mutability,
636         fake_borrow_temps: Option<&mut Vec<Local>>,
637     ) -> BlockAnd<PlaceBuilder<'tcx>> {
638         let this = self;
639         let expr_span = expr.span;
640         let source_info = this.source_info(expr_span);
641         match expr.kind {
642             ExprKind::Scope { region_scope, lint_level, value } => {
643                 this.in_scope((region_scope, source_info), lint_level, |this| {
644                     this.expr_as_place(block, &this.thir[value], mutability, fake_borrow_temps)
645                 })
646             }
647             ExprKind::Field { lhs, variant_index, name } => {
648                 let lhs = &this.thir[lhs];
649                 let mut place_builder =
650                     unpack!(block = this.expr_as_place(block, lhs, mutability, fake_borrow_temps,));
651                 debug!(?place_builder);
652                 if let ty::Adt(adt_def, _) = lhs.ty.kind() {
653                     if adt_def.is_enum() {
654                         place_builder = place_builder.downcast(*adt_def, variant_index);
655                     }
656                 }
657                 block.and(place_builder.field(this, name))
658             }
659             ExprKind::Deref { arg } => {
660                 let place_builder = unpack!(
661                     block =
662                         this.expr_as_place(block, &this.thir[arg], mutability, fake_borrow_temps,)
663                 );
664                 block.and(place_builder.deref())
665             }
666             ExprKind::Index { lhs, index } => this.lower_index_expression(
667                 block,
668                 &this.thir[lhs],
669                 &this.thir[index],
670                 mutability,
671                 fake_borrow_temps,
672                 expr.temp_lifetime,
673                 expr_span,
674                 source_info,
675             ),
676             ExprKind::UpvarRef { closure_def_id, var_hir_id } => {
677                 this.lower_captured_upvar(block, closure_def_id.expect_local(), var_hir_id)
678             }
679
680             ExprKind::VarRef { id } => {
681                 let place_builder = if this.is_bound_var_in_guard(id) {
682                     let index = this.var_local_id(id, RefWithinGuard);
683                     PlaceBuilder::from(index).deref()
684                 } else {
685                     let index = this.var_local_id(id, OutsideGuard);
686                     PlaceBuilder::from(index)
687                 };
688                 block.and(place_builder)
689             }
690
691             ExprKind::PlaceTypeAscription { source, ref user_ty } => {
692                 let place_builder = unpack!(
693                     block = this.expr_as_place(
694                         block,
695                         &this.thir[source],
696                         mutability,
697                         fake_borrow_temps,
698                     )
699                 );
700                 if let Some(user_ty) = user_ty {
701                     let annotation_index =
702                         this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
703                             span: source_info.span,
704                             user_ty: user_ty.clone(),
705                             inferred_ty: expr.ty,
706                         });
707
708                     let place = place_builder.to_place(this);
709                     this.cfg.push(
710                         block,
711                         Statement {
712                             source_info,
713                             kind: StatementKind::AscribeUserType(
714                                 Box::new((
715                                     place,
716                                     UserTypeProjection { base: annotation_index, projs: vec![] },
717                                 )),
718                                 Variance::Invariant,
719                             ),
720                         },
721                     );
722                 }
723                 block.and(place_builder)
724             }
725             ExprKind::ValueTypeAscription { source, ref user_ty } => {
726                 let source = &this.thir[source];
727                 let temp =
728                     unpack!(block = this.as_temp(block, source.temp_lifetime, source, mutability));
729                 if let Some(user_ty) = user_ty {
730                     let annotation_index =
731                         this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
732                             span: source_info.span,
733                             user_ty: user_ty.clone(),
734                             inferred_ty: expr.ty,
735                         });
736                     this.cfg.push(
737                         block,
738                         Statement {
739                             source_info,
740                             kind: StatementKind::AscribeUserType(
741                                 Box::new((
742                                     Place::from(temp),
743                                     UserTypeProjection { base: annotation_index, projs: vec![] },
744                                 )),
745                                 Variance::Invariant,
746                             ),
747                         },
748                     );
749                 }
750                 block.and(PlaceBuilder::from(temp))
751             }
752
753             ExprKind::Array { .. }
754             | ExprKind::Tuple { .. }
755             | ExprKind::Adt { .. }
756             | ExprKind::Closure { .. }
757             | ExprKind::Unary { .. }
758             | ExprKind::Binary { .. }
759             | ExprKind::LogicalOp { .. }
760             | ExprKind::Box { .. }
761             | ExprKind::Cast { .. }
762             | ExprKind::Use { .. }
763             | ExprKind::NeverToAny { .. }
764             | ExprKind::Pointer { .. }
765             | ExprKind::Repeat { .. }
766             | ExprKind::Borrow { .. }
767             | ExprKind::AddressOf { .. }
768             | ExprKind::Match { .. }
769             | ExprKind::If { .. }
770             | ExprKind::Loop { .. }
771             | ExprKind::Block { .. }
772             | ExprKind::Let { .. }
773             | ExprKind::Assign { .. }
774             | ExprKind::AssignOp { .. }
775             | ExprKind::Break { .. }
776             | ExprKind::Continue { .. }
777             | ExprKind::Return { .. }
778             | ExprKind::Literal { .. }
779             | ExprKind::NamedConst { .. }
780             | ExprKind::NonHirLiteral { .. }
781             | ExprKind::ZstLiteral { .. }
782             | ExprKind::ConstParam { .. }
783             | ExprKind::ConstBlock { .. }
784             | ExprKind::StaticRef { .. }
785             | ExprKind::InlineAsm { .. }
786             | ExprKind::Yield { .. }
787             | ExprKind::ThreadLocalRef(_)
788             | ExprKind::Call { .. } => {
789                 // these are not places, so we need to make a temporary.
790                 debug_assert!(!matches!(Category::of(&expr.kind), Some(Category::Place)));
791                 let temp =
792                     unpack!(block = this.as_temp(block, expr.temp_lifetime, expr, mutability));
793                 block.and(PlaceBuilder::from(temp))
794             }
795         }
796     }
797
798     /// Lower a captured upvar. Note we might not know the actual capture index,
799     /// so we create a place starting from `Upvar`, which will be resolved
800     /// once all projections that allow us to identify a capture have been applied.
801     fn lower_captured_upvar(
802         &mut self,
803         block: BasicBlock,
804         closure_def_id: LocalDefId,
805         var_hir_id: LocalVarId,
806     ) -> BlockAnd<PlaceBuilder<'tcx>> {
807         block.and(PlaceBuilder::Upvar {
808             upvar: Upvar { var_hir_id, closure_def_id },
809             projection: vec![],
810         })
811     }
812
813     /// Lower an index expression
814     ///
815     /// This has two complications;
816     ///
817     /// * We need to do a bounds check.
818     /// * We need to ensure that the bounds check can't be invalidated using an
819     ///   expression like `x[1][{x = y; 2}]`. We use fake borrows here to ensure
820     ///   that this is the case.
821     fn lower_index_expression(
822         &mut self,
823         mut block: BasicBlock,
824         base: &Expr<'tcx>,
825         index: &Expr<'tcx>,
826         mutability: Mutability,
827         fake_borrow_temps: Option<&mut Vec<Local>>,
828         temp_lifetime: Option<region::Scope>,
829         expr_span: Span,
830         source_info: SourceInfo,
831     ) -> BlockAnd<PlaceBuilder<'tcx>> {
832         let base_fake_borrow_temps = &mut Vec::new();
833         let is_outermost_index = fake_borrow_temps.is_none();
834         let fake_borrow_temps = fake_borrow_temps.unwrap_or(base_fake_borrow_temps);
835
836         let base_place =
837             unpack!(block = self.expr_as_place(block, base, mutability, Some(fake_borrow_temps),));
838
839         // Making this a *fresh* temporary means we do not have to worry about
840         // the index changing later: Nothing will ever change this temporary.
841         // The "retagging" transformation (for Stacked Borrows) relies on this.
842         let idx = unpack!(block = self.as_temp(block, temp_lifetime, index, Mutability::Not,));
843
844         block = self.bounds_check(block, &base_place, idx, expr_span, source_info);
845
846         if is_outermost_index {
847             self.read_fake_borrows(block, fake_borrow_temps, source_info)
848         } else {
849             self.add_fake_borrows_of_base(
850                 base_place.to_place(self),
851                 block,
852                 fake_borrow_temps,
853                 expr_span,
854                 source_info,
855             );
856         }
857
858         block.and(base_place.index(idx))
859     }
860
861     fn bounds_check(
862         &mut self,
863         block: BasicBlock,
864         slice: &PlaceBuilder<'tcx>,
865         index: Local,
866         expr_span: Span,
867         source_info: SourceInfo,
868     ) -> BasicBlock {
869         let usize_ty = self.tcx.types.usize;
870         let bool_ty = self.tcx.types.bool;
871         // bounds check:
872         let len = self.temp(usize_ty, expr_span);
873         let lt = self.temp(bool_ty, expr_span);
874
875         // len = len(slice)
876         self.cfg.push_assign(block, source_info, len, Rvalue::Len(slice.to_place(self)));
877         // lt = idx < len
878         self.cfg.push_assign(
879             block,
880             source_info,
881             lt,
882             Rvalue::BinaryOp(
883                 BinOp::Lt,
884                 Box::new((Operand::Copy(Place::from(index)), Operand::Copy(len))),
885             ),
886         );
887         let msg = BoundsCheck { len: Operand::Move(len), index: Operand::Copy(Place::from(index)) };
888         // assert!(lt, "...")
889         self.assert(block, Operand::Move(lt), true, msg, expr_span)
890     }
891
892     fn add_fake_borrows_of_base(
893         &mut self,
894         base_place: Place<'tcx>,
895         block: BasicBlock,
896         fake_borrow_temps: &mut Vec<Local>,
897         expr_span: Span,
898         source_info: SourceInfo,
899     ) {
900         let tcx = self.tcx;
901         let place_ty = base_place.ty(&self.local_decls, tcx);
902
903         if let ty::Slice(_) = place_ty.ty.kind() {
904             // We need to create fake borrows to ensure that the bounds
905             // check that we just did stays valid. Since we can't assign to
906             // unsized values, we only need to ensure that none of the
907             // pointers in the base place are modified.
908             for (idx, elem) in base_place.projection.iter().enumerate().rev() {
909                 match elem {
910                     ProjectionElem::Deref => {
911                         let fake_borrow_deref_ty = Place::ty_from(
912                             base_place.local,
913                             &base_place.projection[..idx],
914                             &self.local_decls,
915                             tcx,
916                         )
917                         .ty;
918                         let fake_borrow_ty =
919                             tcx.mk_imm_ref(tcx.lifetimes.re_erased, fake_borrow_deref_ty);
920                         let fake_borrow_temp =
921                             self.local_decls.push(LocalDecl::new(fake_borrow_ty, expr_span));
922                         let projection = tcx.intern_place_elems(&base_place.projection[..idx]);
923                         self.cfg.push_assign(
924                             block,
925                             source_info,
926                             fake_borrow_temp.into(),
927                             Rvalue::Ref(
928                                 tcx.lifetimes.re_erased,
929                                 BorrowKind::Shallow,
930                                 Place { local: base_place.local, projection },
931                             ),
932                         );
933                         fake_borrow_temps.push(fake_borrow_temp);
934                     }
935                     ProjectionElem::Index(_) => {
936                         let index_ty = Place::ty_from(
937                             base_place.local,
938                             &base_place.projection[..idx],
939                             &self.local_decls,
940                             tcx,
941                         );
942                         match index_ty.ty.kind() {
943                             // The previous index expression has already
944                             // done any index expressions needed here.
945                             ty::Slice(_) => break,
946                             ty::Array(..) => (),
947                             _ => bug!("unexpected index base"),
948                         }
949                     }
950                     ProjectionElem::Field(..)
951                     | ProjectionElem::Downcast(..)
952                     | ProjectionElem::OpaqueCast(..)
953                     | ProjectionElem::ConstantIndex { .. }
954                     | ProjectionElem::Subslice { .. } => (),
955                 }
956             }
957         }
958     }
959
960     fn read_fake_borrows(
961         &mut self,
962         bb: BasicBlock,
963         fake_borrow_temps: &mut Vec<Local>,
964         source_info: SourceInfo,
965     ) {
966         // All indexes have been evaluated now, read all of the
967         // fake borrows so that they are live across those index
968         // expressions.
969         for temp in fake_borrow_temps {
970             self.cfg.push_fake_read(bb, source_info, FakeReadCause::ForIndex, Place::from(*temp));
971         }
972     }
973 }
974
975 /// Precise capture is enabled if the feature gate `capture_disjoint_fields` is enabled or if
976 /// user is using Rust Edition 2021 or higher.
977 fn enable_precise_capture(tcx: TyCtxt<'_>, closure_span: Span) -> bool {
978     tcx.features().capture_disjoint_fields || closure_span.rust_2021()
979 }