1 //! See docs in build/expr/mod.rs
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};
17 use rustc_target::abi::VariantIdx;
19 use rustc_index::vec::Idx;
21 use std::assert_matches::assert_matches;
22 use std::convert::From;
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.
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`.
35 /// We use `PlaceElem` since this has all `Field` types available.
36 Local { local: Local, projection: Vec<PlaceElem<'tcx>> },
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
45 /// Once we have figured out the capture index, we can convert the place builder to
46 /// `PlaceBuilder::Local`.
48 /// Consider the following example
50 /// let t = (((10, 10), 10), 10);
53 /// println!("{}", t.0.0.0);
56 /// Here the THIR expression for `t.0.0.0` will be something like
58 /// ```ignore (illustrative)
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.
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>> },
73 #[derive(Copy, Clone, Debug, PartialEq)]
74 pub(crate) struct Upvar {
75 var_hir_id: LocalVarId,
76 closure_def_id: LocalDefId,
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;
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)
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);
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.
118 hir_projections.push(hir_projection);
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.
128 /// **Note:** It's the caller's responsibility to ensure that both lists of projections
129 /// start off of the same root variable.
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],
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() {
147 iter::zip(proj_possible_ancestor, proj_capture).all(|(a, b)| a == b)
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`.
154 /// Note there will be at most one ancestor for any given Place.
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);
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)
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(
186 let closure_span = cx.tcx.def_span(closure_def_id);
187 if !enable_precise_capture(cx.tcx, closure_span) {
189 "No associated capture found for {:?}[{:#?}] even though \
190 capture_disjoint_fields isn't enabled",
196 "No associated capture found for {:?}[{:#?}]",
197 var_hir_id, projection,
204 // Access the capture by accessing the field within the Closure struct.
205 let capture_info = &cx.upvars[capture_index];
207 let Place { local: upvar_resolved_local, projection: local_projection } =
208 capture_info.use_place;
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,
215 &capture.captured_place.place.projections,
218 let upvar_resolved_place_builder = PlaceBuilder::construct_local_place_builder(
220 upvar_resolved_local,
221 local_projection.as_slice(),
225 assert!(matches!(upvar_resolved_place_builder, PlaceBuilder::Local { .. }));
227 Some(upvar_resolved_place_builder)
230 /// Returns projections remaining after stripping an initial prefix of HIR
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
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);
249 match projection.kind {
250 HirProjectionKind::Deref => {
251 assert_matches!(iter.next(), Some(ProjectionElem::Deref));
253 HirProjectionKind::Field(..) => {
254 if base_ty.is_enum() {
255 assert_matches!(iter.next(), Some(ProjectionElem::Downcast(..)));
257 assert_matches!(iter.next(), Some(ProjectionElem::Field(..)));
259 HirProjectionKind::Index | HirProjectionKind::Subslice => {
260 bug!("unexpected projection kind: {:?}", projection);
264 base_ty = projection.ty;
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()
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 })
284 /// Attempts to resolve the `PlaceBuilder`.
285 /// Returns `None` if this is not an upvar.
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(
296 cx: &Builder<'_, 'tcx>,
297 ) -> Option<PlaceBuilder<'tcx>> {
298 let PlaceBuilder::Upvar{ upvar: Upvar {var_hir_id, closure_def_id }, projection} = self else {
302 to_upvars_resolved_place_builder(cx, *var_hir_id, *closure_def_id, &projection)
305 #[instrument(skip(cx), level = "debug")]
306 pub(crate) fn field(self, cx: &Builder<'_, 'tcx>, f: Field) -> Self {
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);
314 let field_ty = PlaceBuilder::compute_field_ty(cx, f, base_ty, variant_index);
316 self.project(ProjectionElem::Field(f, field_ty))
318 PlaceBuilder::Upvar { upvar, mut projection } => {
319 projection.push(ProjectionElem::Field(f, ()));
320 PlaceBuilder::Upvar { upvar, projection }
325 pub(crate) fn deref(self) -> Self {
326 self.project(PlaceElem::Deref)
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))
333 fn index(self, index: Local) -> Self {
334 self.project(PlaceElem::Index(index))
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 }
344 PlaceBuilder::Upvar { upvar, mut projection } => {
345 projection.push(elem.into());
346 PlaceBuilder::Upvar { upvar, projection }
354 /// Same as `.clone().project(..)` but more efficient
355 pub(crate) fn clone_project(&self, elem: PlaceElem<'tcx>) -> Self {
357 PlaceBuilder::Local { local, projection } => PlaceBuilder::Local {
359 projection: Vec::from_iter(projection.iter().copied().chain([elem])),
361 PlaceBuilder::Upvar { upvar, projection } => PlaceBuilder::Upvar {
363 projection: Vec::from_iter(projection.iter().copied().chain([elem.into()])),
368 /// Similar to `Place::ty` but needed during mir building.
370 /// Applies the projections in the `PlaceBuilder` to the base
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")]
377 cx: &Builder<'_, 'tcx>,
380 variant_index: Option<VariantIdx>,
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)
388 ty::Adt(adt_def, substs) => adt_def
393 "expected to take field with idx {:?} of fields of {:?}",
399 ty::Tuple(elems) => elems.iter().nth(field_idx).unwrap_or_else(|| {
400 bug!("expected to take field with idx {:?} of {:?}", field_idx, elems)
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<_>>());
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);
417 let Some(&local) = layout.variant_fields[var].get(field) else {
418 bug!("expected to take field {:?} of {:?}", field, layout.variant_fields[var]);
421 let Some(&f_ty) = layout.field_tys.get(local) else {
422 bug!("expected to get element for {:?} in {:?}", local, layout.field_tys);
427 let Some(f_ty) = substs.as_generator().prefix_tys().nth(field.index()) else {
429 "expected to take index {:?} in {:?}",
431 substs.as_generator().prefix_tys().collect::<Vec<_>>()
438 _ => bug!("couldn't create field type, unexpected base type: {:?}", base_ty),
441 cx.tcx.normalize_erasing_regions(cx.param_env, field_ty)
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>,
453 local_projection: &[PlaceElem<'tcx>],
454 upvar_projection: impl Iterator<Item = UpvarProjectionElem<'tcx>>,
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);
462 // We add all projection elements we encounter to this `Vec`.
463 let mut local_projection = local_projection.to_vec();
465 for (i, proj) in upvar_projection.enumerate() {
466 debug!("i: {:?}, proj: {:?}, local_projection: {:?}", i, proj, local_projection);
468 ProjectionElem::Field(field, _) => {
470 PlaceBuilder::compute_field_ty(cx, field, ancestor_ty, opt_variant_idx);
473 local_projection.push(ProjectionElem::Field(field, field_ty));
474 ancestor_ty = field_ty;
475 opt_variant_idx = None;
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);
485 PlaceBuilder::Local { local, projection: local_projection }
489 impl<'tcx> From<Local> for PlaceBuilder<'tcx> {
490 fn from(local: Local) -> Self {
491 Self::Local { local, projection: Vec::new() }
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() }
504 elem: PlaceElem<'tcx>,
505 ) -> (Ty<'tcx>, Option<VariantIdx>) {
507 ProjectionElem::Deref => {
510 .unwrap_or_else(|| bug!("deref projection of non-dereferenceable ty {:?}", ty))
515 ProjectionElem::Index(_) | ProjectionElem::ConstantIndex { .. } => {
516 (ty.builtin_index().unwrap(), None)
518 ProjectionElem::Subslice { from, to, from_end } => {
519 let ty = match ty.kind() {
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)
527 _ => bug!("cannot subslice non-array type: `{:?}`", ty),
532 ProjectionElem::Downcast(_, variant_idx) => (ty, Some(variant_idx)),
533 ProjectionElem::Field(_, ty) => (ty, None),
534 ProjectionElem::OpaqueCast(..) => bug!("didn't expect OpaqueCast"),
538 fn local_projections_to_ty<'a, 'tcx>(
539 cx: &'a Builder<'a, 'tcx>,
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)
550 // Converts an `UpvarProjectionElem` to `PlaceElem`, ICE'ing when being passed a
552 fn upvar_proj_to_place_elem_no_field_proj<'tcx>(
553 upvar_proj: UpvarProjectionElem<'tcx>,
554 ) -> PlaceElem<'tcx> {
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 }
561 ProjectionElem::Subslice { from, to, from_end } => {
562 ProjectionElem::Subslice { from, to, from_end }
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`"),
570 impl<'a, 'tcx> Builder<'a, 'tcx> {
571 /// Compile `expr`, yielding a place that we can move from etc.
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.
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
583 pub(crate) fn as_place(
585 mut block: BasicBlock,
587 ) -> BlockAnd<Place<'tcx>> {
588 let place_builder = unpack!(block = self.as_place_builder(block, expr));
589 block.and(place_builder.to_place(self))
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(
598 ) -> BlockAnd<PlaceBuilder<'tcx>> {
599 self.expr_as_place(block, expr, Mutability::Mut, None)
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(
609 mut block: BasicBlock,
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))
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(
626 ) -> BlockAnd<PlaceBuilder<'tcx>> {
627 self.expr_as_place(block, expr, Mutability::Not, None)
630 #[instrument(skip(self, fake_borrow_temps), level = "debug")]
633 mut block: BasicBlock,
635 mutability: Mutability,
636 fake_borrow_temps: Option<&mut Vec<Local>>,
637 ) -> BlockAnd<PlaceBuilder<'tcx>> {
639 let expr_span = expr.span;
640 let source_info = this.source_info(expr_span);
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)
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);
657 block.and(place_builder.field(this, name))
659 ExprKind::Deref { arg } => {
660 let place_builder = unpack!(
662 this.expr_as_place(block, &this.thir[arg], mutability, fake_borrow_temps,)
664 block.and(place_builder.deref())
666 ExprKind::Index { lhs, index } => this.lower_index_expression(
676 ExprKind::UpvarRef { closure_def_id, var_hir_id } => {
677 this.lower_captured_upvar(block, closure_def_id.expect_local(), var_hir_id)
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()
685 let index = this.var_local_id(id, OutsideGuard);
686 PlaceBuilder::from(index)
688 block.and(place_builder)
691 ExprKind::PlaceTypeAscription { source, ref user_ty } => {
692 let place_builder = unpack!(
693 block = this.expr_as_place(
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,
708 let place = place_builder.to_place(this);
713 kind: StatementKind::AscribeUserType(
716 UserTypeProjection { base: annotation_index, projs: vec![] },
723 block.and(place_builder)
725 ExprKind::ValueTypeAscription { source, ref user_ty } => {
726 let source = &this.thir[source];
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,
740 kind: StatementKind::AscribeUserType(
743 UserTypeProjection { base: annotation_index, projs: vec![] },
750 block.and(PlaceBuilder::from(temp))
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)));
792 unpack!(block = this.as_temp(block, expr.temp_lifetime, expr, mutability));
793 block.and(PlaceBuilder::from(temp))
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(
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 },
813 /// Lower an index expression
815 /// This has two complications;
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(
823 mut block: BasicBlock,
826 mutability: Mutability,
827 fake_borrow_temps: Option<&mut Vec<Local>>,
828 temp_lifetime: Option<region::Scope>,
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);
837 unpack!(block = self.expr_as_place(block, base, mutability, Some(fake_borrow_temps),));
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,));
844 block = self.bounds_check(block, &base_place, idx, expr_span, source_info);
846 if is_outermost_index {
847 self.read_fake_borrows(block, fake_borrow_temps, source_info)
849 self.add_fake_borrows_of_base(
850 base_place.to_place(self),
858 block.and(base_place.index(idx))
864 slice: &PlaceBuilder<'tcx>,
867 source_info: SourceInfo,
869 let usize_ty = self.tcx.types.usize;
870 let bool_ty = self.tcx.types.bool;
872 let len = self.temp(usize_ty, expr_span);
873 let lt = self.temp(bool_ty, expr_span);
876 self.cfg.push_assign(block, source_info, len, Rvalue::Len(slice.to_place(self)));
878 self.cfg.push_assign(
884 Box::new((Operand::Copy(Place::from(index)), Operand::Copy(len))),
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)
892 fn add_fake_borrows_of_base(
894 base_place: Place<'tcx>,
896 fake_borrow_temps: &mut Vec<Local>,
898 source_info: SourceInfo,
901 let place_ty = base_place.ty(&self.local_decls, tcx);
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() {
910 ProjectionElem::Deref => {
911 let fake_borrow_deref_ty = Place::ty_from(
913 &base_place.projection[..idx],
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(
926 fake_borrow_temp.into(),
928 tcx.lifetimes.re_erased,
930 Place { local: base_place.local, projection },
933 fake_borrow_temps.push(fake_borrow_temp);
935 ProjectionElem::Index(_) => {
936 let index_ty = Place::ty_from(
938 &base_place.projection[..idx],
942 match index_ty.ty.kind() {
943 // The previous index expression has already
944 // done any index expressions needed here.
945 ty::Slice(_) => break,
947 _ => bug!("unexpected index base"),
950 ProjectionElem::Field(..)
951 | ProjectionElem::Downcast(..)
952 | ProjectionElem::OpaqueCast(..)
953 | ProjectionElem::ConstantIndex { .. }
954 | ProjectionElem::Subslice { .. } => (),
960 fn read_fake_borrows(
963 fake_borrow_temps: &mut Vec<Local>,
964 source_info: SourceInfo,
966 // All indexes have been evaluated now, read all of the
967 // fake borrows so that they are live across those index
969 for temp in fake_borrow_temps {
970 self.cfg.push_fake_read(bb, source_info, FakeReadCause::ForIndex, Place::from(*temp));
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()