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