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, Variance};
15 use rustc_span::Span;
16 use rustc_target::abi::{FieldIdx, VariantIdx, FIRST_VARIANT};
17
18 use std::assert_matches::assert_matches;
19 use std::iter;
20
21 /// The "outermost" place that holds this value.
22 #[derive(Copy, Clone, Debug, PartialEq)]
23 pub(crate) enum PlaceBase {
24 /// Denotes the start of a `Place`.
25 Local(Local),
26
27 /// When building place for an expression within a closure, the place might start off a
28 /// captured path. When `capture_disjoint_fields` is enabled, we might not know the capture
29 /// index (within the desugared closure) of the captured path until most of the projections
30 /// are applied. We use `PlaceBase::Upvar` to keep track of the root variable off of which the
31 /// captured path starts, the closure the capture belongs to and the trait the closure
32 /// implements.
33 ///
34 /// Once we have figured out the capture index, we can convert the place builder to start from
35 /// `PlaceBase::Local`.
36 ///
37 /// Consider the following example
38 /// ```rust
39 /// let t = (((10, 10), 10), 10);
40 ///
41 /// let c = || {
42 /// println!("{}", t.0.0.0);
43 /// };
44 /// ```
45 /// Here the THIR expression for `t.0.0.0` will be something like
46 ///
47 /// ```ignore (illustrative)
48 /// * Field(0)
49 /// * Field(0)
50 /// * Field(0)
51 /// * UpvarRef(t)
52 /// ```
53 ///
54 /// When `capture_disjoint_fields` is enabled, `t.0.0.0` is captured and we won't be able to
55 /// figure out that it is captured until all the `Field` projections are applied.
56 Upvar {
57 /// HirId of the upvar
58 var_hir_id: LocalVarId,
59 /// DefId of the closure
60 closure_def_id: LocalDefId,
61 },
62 }
63
64 /// `PlaceBuilder` is used to create places during MIR construction. It allows you to "build up" a
65 /// place by pushing more and more projections onto the end, and then convert the final set into a
66 /// place using the `to_place` method.
67 ///
68 /// This is used internally when building a place for an expression like `a.b.c`. The fields `b`
69 /// and `c` can be progressively pushed onto the place builder that is created when converting `a`.
70 #[derive(Clone, Debug, PartialEq)]
71 pub(in crate::build) struct PlaceBuilder<'tcx> {
72 base: PlaceBase,
73 projection: Vec<PlaceElem<'tcx>>,
74 }
75
76 /// Given a list of MIR projections, convert them to list of HIR ProjectionKind.
77 /// The projections are truncated to represent a path that might be captured by a
78 /// closure/generator. This implies the vector returned from this function doesn't contain
79 /// ProjectionElems `Downcast`, `ConstantIndex`, `Index`, or `Subslice` because those will never be
80 /// part of a path that is captured by a closure. We stop applying projections once we see the first
81 /// projection that isn't captured by a closure.
convert_to_hir_projections_and_truncate_for_capture( mir_projections: &[PlaceElem<'_>], ) -> Vec<HirProjectionKind>82 fn convert_to_hir_projections_and_truncate_for_capture(
83 mir_projections: &[PlaceElem<'_>],
84 ) -> Vec<HirProjectionKind> {
85 let mut hir_projections = Vec::new();
86 let mut variant = None;
87
88 for mir_projection in mir_projections {
89 let hir_projection = match mir_projection {
90 ProjectionElem::Deref => HirProjectionKind::Deref,
91 ProjectionElem::Field(field, _) => {
92 let variant = variant.unwrap_or(FIRST_VARIANT);
93 HirProjectionKind::Field(*field, variant)
94 }
95 ProjectionElem::Downcast(.., idx) => {
96 // We don't expect to see multi-variant enums here, as earlier
97 // phases will have truncated them already. However, there can
98 // still be downcasts, thanks to single-variant enums.
99 // We keep track of VariantIdx so we can use this information
100 // if the next ProjectionElem is a Field.
101 variant = Some(*idx);
102 continue;
103 }
104 // These do not affect anything, they just make sure we know the right type.
105 ProjectionElem::OpaqueCast(_) => continue,
106 ProjectionElem::Index(..)
107 | ProjectionElem::ConstantIndex { .. }
108 | ProjectionElem::Subslice { .. } => {
109 // We don't capture array-access projections.
110 // We can stop here as arrays are captured completely.
111 break;
112 }
113 };
114 variant = None;
115 hir_projections.push(hir_projection);
116 }
117
118 hir_projections
119 }
120
121 /// Return true if the `proj_possible_ancestor` represents an ancestor path
122 /// to `proj_capture` or `proj_possible_ancestor` is same as `proj_capture`,
123 /// assuming they both start off of the same root variable.
124 ///
125 /// **Note:** It's the caller's responsibility to ensure that both lists of projections
126 /// start off of the same root variable.
127 ///
128 /// Eg: 1. `foo.x` which is represented using `projections=[Field(x)]` is an ancestor of
129 /// `foo.x.y` which is represented using `projections=[Field(x), Field(y)]`.
130 /// Note both `foo.x` and `foo.x.y` start off of the same root variable `foo`.
131 /// 2. Since we only look at the projections here function will return `bar.x` as an a valid
132 /// ancestor of `foo.x.y`. It's the caller's responsibility to ensure that both projections
133 /// list are being applied to the same root variable.
is_ancestor_or_same_capture( proj_possible_ancestor: &[HirProjectionKind], proj_capture: &[HirProjectionKind], ) -> bool134 fn is_ancestor_or_same_capture(
135 proj_possible_ancestor: &[HirProjectionKind],
136 proj_capture: &[HirProjectionKind],
137 ) -> bool {
138 // We want to make sure `is_ancestor_or_same_capture("x.0.0", "x.0")` to return false.
139 // Therefore we can't just check if all projections are same in the zipped iterator below.
140 if proj_possible_ancestor.len() > proj_capture.len() {
141 return false;
142 }
143
144 iter::zip(proj_possible_ancestor, proj_capture).all(|(a, b)| a == b)
145 }
146
147 /// Given a closure, returns the index of a capture within the desugared closure struct and the
148 /// `ty::CapturedPlace` which is the ancestor of the Place represented using the `var_hir_id`
149 /// and `projection`.
150 ///
151 /// Note there will be at most one ancestor for any given Place.
152 ///
153 /// Returns None, when the ancestor is not found.
find_capture_matching_projections<'a, 'tcx>( upvars: &'a CaptureMap<'tcx>, var_hir_id: LocalVarId, projections: &[PlaceElem<'tcx>], ) -> Option<(usize, &'a Capture<'tcx>)>154 fn find_capture_matching_projections<'a, 'tcx>(
155 upvars: &'a CaptureMap<'tcx>,
156 var_hir_id: LocalVarId,
157 projections: &[PlaceElem<'tcx>],
158 ) -> Option<(usize, &'a Capture<'tcx>)> {
159 let hir_projections = convert_to_hir_projections_and_truncate_for_capture(projections);
160
161 upvars.get_by_key_enumerated(var_hir_id.0).find(|(_, capture)| {
162 let possible_ancestor_proj_kinds: Vec<_> =
163 capture.captured_place.place.projections.iter().map(|proj| proj.kind).collect();
164 is_ancestor_or_same_capture(&possible_ancestor_proj_kinds, &hir_projections)
165 })
166 }
167
168 /// Takes an upvar place and tries to resolve it into a `PlaceBuilder`
169 /// with `PlaceBase::Local`
170 #[instrument(level = "trace", skip(cx), ret)]
to_upvars_resolved_place_builder<'tcx>( cx: &Builder<'_, 'tcx>, var_hir_id: LocalVarId, closure_def_id: LocalDefId, projection: &[PlaceElem<'tcx>], ) -> Option<PlaceBuilder<'tcx>>171 fn to_upvars_resolved_place_builder<'tcx>(
172 cx: &Builder<'_, 'tcx>,
173 var_hir_id: LocalVarId,
174 closure_def_id: LocalDefId,
175 projection: &[PlaceElem<'tcx>],
176 ) -> Option<PlaceBuilder<'tcx>> {
177 let Some((capture_index, capture)) =
178 find_capture_matching_projections(
179 &cx.upvars,
180 var_hir_id,
181 &projection,
182 ) else {
183 let closure_span = cx.tcx.def_span(closure_def_id);
184 if !enable_precise_capture(closure_span) {
185 bug!(
186 "No associated capture found for {:?}[{:#?}] even though \
187 capture_disjoint_fields isn't enabled",
188 var_hir_id,
189 projection
190 )
191 } else {
192 debug!(
193 "No associated capture found for {:?}[{:#?}]",
194 var_hir_id, projection,
195 );
196 }
197 return None;
198 };
199
200 // Access the capture by accessing the field within the Closure struct.
201 let capture_info = &cx.upvars[capture_index];
202
203 let mut upvar_resolved_place_builder = PlaceBuilder::from(capture_info.use_place);
204
205 // We used some of the projections to build the capture itself,
206 // now we apply the remaining to the upvar resolved place.
207 trace!(?capture.captured_place, ?projection);
208 let remaining_projections = strip_prefix(
209 capture.captured_place.place.base_ty,
210 projection,
211 &capture.captured_place.place.projections,
212 );
213 upvar_resolved_place_builder.projection.extend(remaining_projections);
214
215 Some(upvar_resolved_place_builder)
216 }
217
218 /// Returns projections remaining after stripping an initial prefix of HIR
219 /// projections.
220 ///
221 /// Supports only HIR projection kinds that represent a path that might be
222 /// captured by a closure or a generator, i.e., an `Index` or a `Subslice`
223 /// projection kinds are unsupported.
strip_prefix<'a, 'tcx>( mut base_ty: Ty<'tcx>, projections: &'a [PlaceElem<'tcx>], prefix_projections: &[HirProjection<'tcx>], ) -> impl Iterator<Item = PlaceElem<'tcx>> + 'a224 fn strip_prefix<'a, 'tcx>(
225 mut base_ty: Ty<'tcx>,
226 projections: &'a [PlaceElem<'tcx>],
227 prefix_projections: &[HirProjection<'tcx>],
228 ) -> impl Iterator<Item = PlaceElem<'tcx>> + 'a {
229 let mut iter = projections
230 .iter()
231 .copied()
232 // Filter out opaque casts, they are unnecessary in the prefix.
233 .filter(|elem| !matches!(elem, ProjectionElem::OpaqueCast(..)));
234 for projection in prefix_projections {
235 match projection.kind {
236 HirProjectionKind::Deref => {
237 assert_matches!(iter.next(), Some(ProjectionElem::Deref));
238 }
239 HirProjectionKind::Field(..) => {
240 if base_ty.is_enum() {
241 assert_matches!(iter.next(), Some(ProjectionElem::Downcast(..)));
242 }
243 assert_matches!(iter.next(), Some(ProjectionElem::Field(..)));
244 }
245 HirProjectionKind::Index | HirProjectionKind::Subslice => {
246 bug!("unexpected projection kind: {:?}", projection);
247 }
248 }
249 base_ty = projection.ty;
250 }
251 iter
252 }
253
254 impl<'tcx> PlaceBuilder<'tcx> {
to_place(&self, cx: &Builder<'_, 'tcx>) -> Place<'tcx>255 pub(in crate::build) fn to_place(&self, cx: &Builder<'_, 'tcx>) -> Place<'tcx> {
256 self.try_to_place(cx).unwrap()
257 }
258
259 /// Creates a `Place` or returns `None` if an upvar cannot be resolved
try_to_place(&self, cx: &Builder<'_, 'tcx>) -> Option<Place<'tcx>>260 pub(in crate::build) fn try_to_place(&self, cx: &Builder<'_, 'tcx>) -> Option<Place<'tcx>> {
261 let resolved = self.resolve_upvar(cx);
262 let builder = resolved.as_ref().unwrap_or(self);
263 let PlaceBase::Local(local) = builder.base else { return None };
264 let projection = cx.tcx.mk_place_elems(&builder.projection);
265 Some(Place { local, projection })
266 }
267
268 /// Attempts to resolve the `PlaceBuilder`.
269 /// Returns `None` if this is not an upvar.
270 ///
271 /// Upvars resolve may fail for a `PlaceBuilder` when attempting to
272 /// resolve a disjoint field whose root variable is not captured
273 /// (destructured assignments) or when attempting to resolve a root
274 /// variable (discriminant matching with only wildcard arm) that is
275 /// not captured. This can happen because the final mir that will be
276 /// generated doesn't require a read for this place. Failures will only
277 /// happen inside closures.
resolve_upvar( &self, cx: &Builder<'_, 'tcx>, ) -> Option<PlaceBuilder<'tcx>>278 pub(in crate::build) fn resolve_upvar(
279 &self,
280 cx: &Builder<'_, 'tcx>,
281 ) -> Option<PlaceBuilder<'tcx>> {
282 let PlaceBase::Upvar { var_hir_id, closure_def_id } = self.base else {
283 return None;
284 };
285 to_upvars_resolved_place_builder(cx, var_hir_id, closure_def_id, &self.projection)
286 }
287
base(&self) -> PlaceBase288 pub(crate) fn base(&self) -> PlaceBase {
289 self.base
290 }
291
projection(&self) -> &[PlaceElem<'tcx>]292 pub(crate) fn projection(&self) -> &[PlaceElem<'tcx>] {
293 &self.projection
294 }
295
field(self, f: FieldIdx, ty: Ty<'tcx>) -> Self296 pub(crate) fn field(self, f: FieldIdx, ty: Ty<'tcx>) -> Self {
297 self.project(PlaceElem::Field(f, ty))
298 }
299
deref(self) -> Self300 pub(crate) fn deref(self) -> Self {
301 self.project(PlaceElem::Deref)
302 }
303
downcast(self, adt_def: AdtDef<'tcx>, variant_index: VariantIdx) -> Self304 pub(crate) fn downcast(self, adt_def: AdtDef<'tcx>, variant_index: VariantIdx) -> Self {
305 self.project(PlaceElem::Downcast(Some(adt_def.variant(variant_index).name), variant_index))
306 }
307
index(self, index: Local) -> Self308 fn index(self, index: Local) -> Self {
309 self.project(PlaceElem::Index(index))
310 }
311
project(mut self, elem: PlaceElem<'tcx>) -> Self312 pub(crate) fn project(mut self, elem: PlaceElem<'tcx>) -> Self {
313 self.projection.push(elem);
314 self
315 }
316
317 /// Same as `.clone().project(..)` but more efficient
clone_project(&self, elem: PlaceElem<'tcx>) -> Self318 pub(crate) fn clone_project(&self, elem: PlaceElem<'tcx>) -> Self {
319 Self {
320 base: self.base,
321 projection: Vec::from_iter(self.projection.iter().copied().chain([elem])),
322 }
323 }
324 }
325
326 impl<'tcx> From<Local> for PlaceBuilder<'tcx> {
from(local: Local) -> Self327 fn from(local: Local) -> Self {
328 Self { base: PlaceBase::Local(local), projection: Vec::new() }
329 }
330 }
331
332 impl<'tcx> From<PlaceBase> for PlaceBuilder<'tcx> {
from(base: PlaceBase) -> Self333 fn from(base: PlaceBase) -> Self {
334 Self { base, projection: Vec::new() }
335 }
336 }
337
338 impl<'tcx> From<Place<'tcx>> for PlaceBuilder<'tcx> {
from(p: Place<'tcx>) -> Self339 fn from(p: Place<'tcx>) -> Self {
340 Self { base: PlaceBase::Local(p.local), projection: p.projection.to_vec() }
341 }
342 }
343
344 impl<'a, 'tcx> Builder<'a, 'tcx> {
345 /// Compile `expr`, yielding a place that we can move from etc.
346 ///
347 /// WARNING: Any user code might:
348 /// * Invalidate any slice bounds checks performed.
349 /// * Change the address that this `Place` refers to.
350 /// * Modify the memory that this place refers to.
351 /// * Invalidate the memory that this place refers to, this will be caught
352 /// by borrow checking.
353 ///
354 /// Extra care is needed if any user code is allowed to run between calling
355 /// this method and using it, as is the case for `match` and index
356 /// expressions.
as_place( &mut self, mut block: BasicBlock, expr: &Expr<'tcx>, ) -> BlockAnd<Place<'tcx>>357 pub(crate) fn as_place(
358 &mut self,
359 mut block: BasicBlock,
360 expr: &Expr<'tcx>,
361 ) -> BlockAnd<Place<'tcx>> {
362 let place_builder = unpack!(block = self.as_place_builder(block, expr));
363 block.and(place_builder.to_place(self))
364 }
365
366 /// This is used when constructing a compound `Place`, so that we can avoid creating
367 /// intermediate `Place` values until we know the full set of projections.
as_place_builder( &mut self, block: BasicBlock, expr: &Expr<'tcx>, ) -> BlockAnd<PlaceBuilder<'tcx>>368 pub(crate) fn as_place_builder(
369 &mut self,
370 block: BasicBlock,
371 expr: &Expr<'tcx>,
372 ) -> BlockAnd<PlaceBuilder<'tcx>> {
373 self.expr_as_place(block, expr, Mutability::Mut, None)
374 }
375
376 /// Compile `expr`, yielding a place that we can move from etc.
377 /// Mutability note: The caller of this method promises only to read from the resulting
378 /// place. The place itself may or may not be mutable:
379 /// * If this expr is a place expr like a.b, then we will return that place.
380 /// * Otherwise, a temporary is created: in that event, it will be an immutable temporary.
as_read_only_place( &mut self, mut block: BasicBlock, expr: &Expr<'tcx>, ) -> BlockAnd<Place<'tcx>>381 pub(crate) fn as_read_only_place(
382 &mut self,
383 mut block: BasicBlock,
384 expr: &Expr<'tcx>,
385 ) -> BlockAnd<Place<'tcx>> {
386 let place_builder = unpack!(block = self.as_read_only_place_builder(block, expr));
387 block.and(place_builder.to_place(self))
388 }
389
390 /// This is used when constructing a compound `Place`, so that we can avoid creating
391 /// intermediate `Place` values until we know the full set of projections.
392 /// Mutability note: The caller of this method promises only to read from the resulting
393 /// place. The place itself may or may not be mutable:
394 /// * If this expr is a place expr like a.b, then we will return that place.
395 /// * Otherwise, a temporary is created: in that event, it will be an immutable temporary.
as_read_only_place_builder( &mut self, block: BasicBlock, expr: &Expr<'tcx>, ) -> BlockAnd<PlaceBuilder<'tcx>>396 fn as_read_only_place_builder(
397 &mut self,
398 block: BasicBlock,
399 expr: &Expr<'tcx>,
400 ) -> BlockAnd<PlaceBuilder<'tcx>> {
401 self.expr_as_place(block, expr, Mutability::Not, None)
402 }
403
expr_as_place( &mut self, mut block: BasicBlock, expr: &Expr<'tcx>, mutability: Mutability, fake_borrow_temps: Option<&mut Vec<Local>>, ) -> BlockAnd<PlaceBuilder<'tcx>>404 fn expr_as_place(
405 &mut self,
406 mut block: BasicBlock,
407 expr: &Expr<'tcx>,
408 mutability: Mutability,
409 fake_borrow_temps: Option<&mut Vec<Local>>,
410 ) -> BlockAnd<PlaceBuilder<'tcx>> {
411 debug!("expr_as_place(block={:?}, expr={:?}, mutability={:?})", block, expr, mutability);
412
413 let this = self;
414 let expr_span = expr.span;
415 let source_info = this.source_info(expr_span);
416 match expr.kind {
417 ExprKind::Scope { region_scope, lint_level, value } => {
418 this.in_scope((region_scope, source_info), lint_level, |this| {
419 this.expr_as_place(block, &this.thir[value], mutability, fake_borrow_temps)
420 })
421 }
422 ExprKind::Field { lhs, variant_index, name } => {
423 let lhs = &this.thir[lhs];
424 let mut place_builder =
425 unpack!(block = this.expr_as_place(block, lhs, mutability, fake_borrow_temps,));
426 if let ty::Adt(adt_def, _) = lhs.ty.kind() {
427 if adt_def.is_enum() {
428 place_builder = place_builder.downcast(*adt_def, variant_index);
429 }
430 }
431 block.and(place_builder.field(name, expr.ty))
432 }
433 ExprKind::Deref { arg } => {
434 let place_builder = unpack!(
435 block =
436 this.expr_as_place(block, &this.thir[arg], mutability, fake_borrow_temps,)
437 );
438 block.and(place_builder.deref())
439 }
440 ExprKind::Index { lhs, index } => this.lower_index_expression(
441 block,
442 &this.thir[lhs],
443 &this.thir[index],
444 mutability,
445 fake_borrow_temps,
446 expr.temp_lifetime,
447 expr_span,
448 source_info,
449 ),
450 ExprKind::UpvarRef { closure_def_id, var_hir_id } => {
451 this.lower_captured_upvar(block, closure_def_id.expect_local(), var_hir_id)
452 }
453
454 ExprKind::VarRef { id } => {
455 let place_builder = if this.is_bound_var_in_guard(id) {
456 let index = this.var_local_id(id, RefWithinGuard);
457 PlaceBuilder::from(index).deref()
458 } else {
459 let index = this.var_local_id(id, OutsideGuard);
460 PlaceBuilder::from(index)
461 };
462 block.and(place_builder)
463 }
464
465 ExprKind::PlaceTypeAscription { source, ref user_ty } => {
466 let place_builder = unpack!(
467 block = this.expr_as_place(
468 block,
469 &this.thir[source],
470 mutability,
471 fake_borrow_temps,
472 )
473 );
474 if let Some(user_ty) = user_ty {
475 let annotation_index =
476 this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
477 span: source_info.span,
478 user_ty: user_ty.clone(),
479 inferred_ty: expr.ty,
480 });
481
482 let place = place_builder.to_place(this);
483 this.cfg.push(
484 block,
485 Statement {
486 source_info,
487 kind: StatementKind::AscribeUserType(
488 Box::new((
489 place,
490 UserTypeProjection { base: annotation_index, projs: vec![] },
491 )),
492 Variance::Invariant,
493 ),
494 },
495 );
496 }
497 block.and(place_builder)
498 }
499 ExprKind::ValueTypeAscription { source, ref user_ty } => {
500 let source = &this.thir[source];
501 let temp =
502 unpack!(block = this.as_temp(block, source.temp_lifetime, source, mutability));
503 if let Some(user_ty) = user_ty {
504 let annotation_index =
505 this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
506 span: source_info.span,
507 user_ty: user_ty.clone(),
508 inferred_ty: expr.ty,
509 });
510 this.cfg.push(
511 block,
512 Statement {
513 source_info,
514 kind: StatementKind::AscribeUserType(
515 Box::new((
516 Place::from(temp),
517 UserTypeProjection { base: annotation_index, projs: vec![] },
518 )),
519 Variance::Invariant,
520 ),
521 },
522 );
523 }
524 block.and(PlaceBuilder::from(temp))
525 }
526
527 ExprKind::Array { .. }
528 | ExprKind::Tuple { .. }
529 | ExprKind::Adt { .. }
530 | ExprKind::Closure { .. }
531 | ExprKind::Unary { .. }
532 | ExprKind::Binary { .. }
533 | ExprKind::LogicalOp { .. }
534 | ExprKind::Box { .. }
535 | ExprKind::Cast { .. }
536 | ExprKind::Use { .. }
537 | ExprKind::NeverToAny { .. }
538 | ExprKind::PointerCoercion { .. }
539 | ExprKind::Repeat { .. }
540 | ExprKind::Borrow { .. }
541 | ExprKind::AddressOf { .. }
542 | ExprKind::Match { .. }
543 | ExprKind::If { .. }
544 | ExprKind::Loop { .. }
545 | ExprKind::Block { .. }
546 | ExprKind::Let { .. }
547 | ExprKind::Assign { .. }
548 | ExprKind::AssignOp { .. }
549 | ExprKind::Break { .. }
550 | ExprKind::Continue { .. }
551 | ExprKind::Return { .. }
552 | ExprKind::Become { .. }
553 | ExprKind::Literal { .. }
554 | ExprKind::NamedConst { .. }
555 | ExprKind::NonHirLiteral { .. }
556 | ExprKind::ZstLiteral { .. }
557 | ExprKind::ConstParam { .. }
558 | ExprKind::ConstBlock { .. }
559 | ExprKind::StaticRef { .. }
560 | ExprKind::InlineAsm { .. }
561 | ExprKind::OffsetOf { .. }
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.
lower_captured_upvar( &mut self, block: BasicBlock, closure_def_id: LocalDefId, var_hir_id: LocalVarId, ) -> BlockAnd<PlaceBuilder<'tcx>>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.
lower_index_expression( &mut self, mut block: BasicBlock, base: &Expr<'tcx>, index: &Expr<'tcx>, mutability: Mutability, fake_borrow_temps: Option<&mut Vec<Local>>, temp_lifetime: Option<region::Scope>, expr_span: Span, source_info: SourceInfo, ) -> BlockAnd<PlaceBuilder<'tcx>>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
bounds_check( &mut self, block: BasicBlock, slice: &PlaceBuilder<'tcx>, index: Local, expr_span: Span, source_info: SourceInfo, ) -> BasicBlock634 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
add_fake_borrows_of_base( &mut self, base_place: Place<'tcx>, block: BasicBlock, fake_borrow_temps: &mut Vec<Local>, expr_span: Span, source_info: SourceInfo, )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 (base_place, elem) in base_place.iter_projections().rev() {
682 match elem {
683 ProjectionElem::Deref => {
684 let fake_borrow_deref_ty = base_place.ty(&self.local_decls, tcx).ty;
685 let fake_borrow_ty =
686 Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, fake_borrow_deref_ty);
687 let fake_borrow_temp =
688 self.local_decls.push(LocalDecl::new(fake_borrow_ty, expr_span));
689 let projection = tcx.mk_place_elems(&base_place.projection);
690 self.cfg.push_assign(
691 block,
692 source_info,
693 fake_borrow_temp.into(),
694 Rvalue::Ref(
695 tcx.lifetimes.re_erased,
696 BorrowKind::Shallow,
697 Place { local: base_place.local, projection },
698 ),
699 );
700 fake_borrow_temps.push(fake_borrow_temp);
701 }
702 ProjectionElem::Index(_) => {
703 let index_ty = base_place.ty(&self.local_decls, tcx);
704 match index_ty.ty.kind() {
705 // The previous index expression has already
706 // done any index expressions needed here.
707 ty::Slice(_) => break,
708 ty::Array(..) => (),
709 _ => bug!("unexpected index base"),
710 }
711 }
712 ProjectionElem::Field(..)
713 | ProjectionElem::Downcast(..)
714 | ProjectionElem::OpaqueCast(..)
715 | ProjectionElem::ConstantIndex { .. }
716 | ProjectionElem::Subslice { .. } => (),
717 }
718 }
719 }
720 }
721
read_fake_borrows( &mut self, bb: BasicBlock, fake_borrow_temps: &mut Vec<Local>, source_info: SourceInfo, )722 fn read_fake_borrows(
723 &mut self,
724 bb: BasicBlock,
725 fake_borrow_temps: &mut Vec<Local>,
726 source_info: SourceInfo,
727 ) {
728 // All indexes have been evaluated now, read all of the
729 // fake borrows so that they are live across those index
730 // expressions.
731 for temp in fake_borrow_temps {
732 self.cfg.push_fake_read(bb, source_info, FakeReadCause::ForIndex, Place::from(*temp));
733 }
734 }
735 }
736
737 /// Precise capture is enabled if user is using Rust Edition 2021 or higher.
enable_precise_capture(closure_span: Span) -> bool738 fn enable_precise_capture(closure_span: Span) -> bool {
739 closure_span.rust_2021()
740 }
741