]>
Commit | Line | Data |
---|---|---|
e9174d1e SL |
1 | //! See docs in build/expr/mod.rs |
2 | ||
9fa01778 XL |
3 | use crate::build::expr::category::Category; |
4 | use crate::build::ForGuard::{OutsideGuard, RefWithinGuard}; | |
5 | use crate::build::{BlockAnd, BlockAndExtension, Builder}; | |
064997fb | 6 | use rustc_hir::def_id::LocalDefId; |
923072b8 | 7 | use rustc_middle::hir::place::Projection as HirProjection; |
fc512014 | 8 | use rustc_middle::hir::place::ProjectionKind as HirProjectionKind; |
5869c6ff | 9 | use rustc_middle::middle::region; |
ba9703b0 XL |
10 | use rustc_middle::mir::AssertKind::BoundsCheck; |
11 | use rustc_middle::mir::*; | |
17df50a5 | 12 | use rustc_middle::thir::*; |
6a06907d | 13 | use rustc_middle::ty::AdtDef; |
ba9703b0 | 14 | use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty, TyCtxt, Variance}; |
dfeec247 | 15 | use rustc_span::Span; |
fc512014 | 16 | use rustc_target::abi::VariantIdx; |
e9174d1e | 17 | |
e74abb32 XL |
18 | use rustc_index::vec::Idx; |
19 | ||
cdc7bbd5 XL |
20 | use std::iter; |
21 | ||
fc512014 | 22 | /// The "outermost" place that holds this value. |
6a06907d | 23 | #[derive(Copy, Clone, Debug, PartialEq)] |
923072b8 | 24 | pub(crate) enum PlaceBase { |
fc512014 XL |
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 | |
04454e1e | 40 | /// let t = (((10, 10), 10), 10); |
fc512014 XL |
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 | /// | |
04454e1e | 48 | /// ```ignore (illustrative) |
fc512014 XL |
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 | |
923072b8 | 59 | var_hir_id: LocalVarId, |
fc512014 | 60 | /// DefId of the closure |
064997fb | 61 | closure_def_id: LocalDefId, |
fc512014 | 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)] |
923072b8 | 74 | pub(crate) struct PlaceBuilder<'tcx> { |
fc512014 | 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. |
85 | fn 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. | |
135 | fn is_ancestor_or_same_capture( | |
923072b8 | 136 | proj_possible_ancestor: &[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`. | |
151 | fn compute_capture_idx<'tcx>( | |
152 | closure_min_captures: &ty::RootVariableMinCaptureList<'tcx>, | |
923072b8 | 153 | var_hir_id: LocalVarId, |
fc512014 XL |
154 | root_var_idx: usize, |
155 | ) -> usize { | |
156 | let mut res = 0; | |
157 | for (var_id, capture_list) in closure_min_captures { | |
923072b8 | 158 | if *var_id == var_hir_id.0 { |
fc512014 XL |
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. | |
176 | fn find_capture_matching_projections<'a, 'tcx>( | |
177 | typeck_results: &'a ty::TypeckResults<'tcx>, | |
923072b8 | 178 | var_hir_id: LocalVarId, |
064997fb | 179 | closure_def_id: LocalDefId, |
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)?; | |
923072b8 | 183 | let root_variable_min_captures = closure_min_captures.get(&var_hir_id.0)?; |
fc512014 XL |
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)| { | |
923072b8 | 190 | let possible_ancestor_proj_kinds: Vec<_> = |
5869c6ff XL |
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 | ||
5e7ed085 | 195 | // Convert index to be from the perspective of the entire closure_min_captures map |
fc512014 XL |
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 |
204 | fn 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 | 244 | // We won't be building MIR if the closure wasn't local |
064997fb | 245 | let closure_hir_id = tcx.hir().local_def_id_to_hir_id(closure_def_id); |
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. | |
5e7ed085 | 259 | let var_ty = substs.tupled_upvars_ty().tuple_fields()[capture_index]; |
fc512014 | 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 | ||
fc512014 XL |
271 | // We used some of the projections to build the capture itself, |
272 | // now we apply the remaining to the upvar resolved place. | |
923072b8 FG |
273 | let remaining_projections = strip_prefix( |
274 | capture.place.base_ty, | |
275 | from_builder.projection, | |
276 | &capture.place.projections, | |
277 | ); | |
278 | upvar_resolved_place_builder.projection.extend(remaining_projections); | |
fc512014 XL |
279 | |
280 | Ok(upvar_resolved_place_builder) | |
281 | } | |
282 | } | |
283 | } | |
284 | ||
923072b8 FG |
285 | /// Returns projections remaining after stripping an initial prefix of HIR |
286 | /// projections. | |
287 | /// | |
288 | /// Supports only HIR projection kinds that represent a path that might be | |
289 | /// captured by a closure or a generator, i.e., an `Index` or a `Subslice` | |
290 | /// projection kinds are unsupported. | |
291 | fn strip_prefix<'tcx>( | |
292 | mut base_ty: Ty<'tcx>, | |
293 | projections: Vec<PlaceElem<'tcx>>, | |
294 | prefix_projections: &[HirProjection<'tcx>], | |
295 | ) -> impl Iterator<Item = PlaceElem<'tcx>> { | |
296 | let mut iter = projections.into_iter(); | |
297 | for projection in prefix_projections { | |
298 | match projection.kind { | |
299 | HirProjectionKind::Deref => { | |
300 | assert!(matches!(iter.next(), Some(ProjectionElem::Deref))); | |
301 | } | |
302 | HirProjectionKind::Field(..) => { | |
303 | if base_ty.is_enum() { | |
304 | assert!(matches!(iter.next(), Some(ProjectionElem::Downcast(..)))); | |
305 | } | |
306 | assert!(matches!(iter.next(), Some(ProjectionElem::Field(..)))); | |
307 | } | |
308 | HirProjectionKind::Index | HirProjectionKind::Subslice => { | |
309 | bug!("unexpected projection kind: {:?}", projection); | |
310 | } | |
311 | } | |
312 | base_ty = projection.ty; | |
313 | } | |
314 | iter | |
315 | } | |
316 | ||
dfeec247 | 317 | impl<'tcx> PlaceBuilder<'tcx> { |
923072b8 | 318 | pub(crate) fn into_place<'a>( |
fc512014 XL |
319 | self, |
320 | tcx: TyCtxt<'tcx>, | |
321 | typeck_results: &'a ty::TypeckResults<'tcx>, | |
322 | ) -> Place<'tcx> { | |
323 | if let PlaceBase::Local(local) = self.base { | |
324 | Place { local, projection: tcx.intern_place_elems(&self.projection) } | |
325 | } else { | |
326 | self.expect_upvars_resolved(tcx, typeck_results).into_place(tcx, typeck_results) | |
327 | } | |
328 | } | |
329 | ||
330 | fn expect_upvars_resolved<'a>( | |
331 | self, | |
332 | tcx: TyCtxt<'tcx>, | |
333 | typeck_results: &'a ty::TypeckResults<'tcx>, | |
334 | ) -> PlaceBuilder<'tcx> { | |
335 | to_upvars_resolved_place_builder(self, tcx, typeck_results).unwrap() | |
336 | } | |
337 | ||
6a06907d XL |
338 | /// Attempts to resolve the `PlaceBuilder`. |
339 | /// On success, it will return the resolved `PlaceBuilder`. | |
340 | /// On failure, it will return itself. | |
341 | /// | |
342 | /// Upvars resolve may fail for a `PlaceBuilder` when attempting to | |
343 | /// resolve a disjoint field whose root variable is not captured | |
344 | /// (destructured assignments) or when attempting to resolve a root | |
345 | /// variable (discriminant matching with only wildcard arm) that is | |
346 | /// not captured. This can happen because the final mir that will be | |
347 | /// generated doesn't require a read for this place. Failures will only | |
348 | /// happen inside closures. | |
923072b8 | 349 | pub(crate) fn try_upvars_resolved<'a>( |
6a06907d XL |
350 | self, |
351 | tcx: TyCtxt<'tcx>, | |
352 | typeck_results: &'a ty::TypeckResults<'tcx>, | |
353 | ) -> Result<PlaceBuilder<'tcx>, PlaceBuilder<'tcx>> { | |
354 | to_upvars_resolved_place_builder(self, tcx, typeck_results) | |
355 | } | |
356 | ||
923072b8 | 357 | pub(crate) fn base(&self) -> PlaceBase { |
fc512014 | 358 | self.base |
e74abb32 XL |
359 | } |
360 | ||
923072b8 | 361 | pub(crate) fn field(self, f: Field, ty: Ty<'tcx>) -> Self { |
e74abb32 XL |
362 | self.project(PlaceElem::Field(f, ty)) |
363 | } | |
364 | ||
923072b8 | 365 | pub(crate) fn deref(self) -> Self { |
e74abb32 XL |
366 | self.project(PlaceElem::Deref) |
367 | } | |
368 | ||
923072b8 | 369 | pub(crate) fn downcast(self, adt_def: AdtDef<'tcx>, variant_index: VariantIdx) -> Self { |
5e7ed085 | 370 | self.project(PlaceElem::Downcast(Some(adt_def.variant(variant_index).name), variant_index)) |
6a06907d XL |
371 | } |
372 | ||
e74abb32 XL |
373 | fn index(self, index: Local) -> Self { |
374 | self.project(PlaceElem::Index(index)) | |
375 | } | |
376 | ||
923072b8 | 377 | pub(crate) fn project(mut self, elem: PlaceElem<'tcx>) -> Self { |
e74abb32 XL |
378 | self.projection.push(elem); |
379 | self | |
380 | } | |
381 | } | |
382 | ||
dfeec247 | 383 | impl<'tcx> From<Local> for PlaceBuilder<'tcx> { |
e74abb32 | 384 | fn from(local: Local) -> Self { |
fc512014 XL |
385 | Self { base: PlaceBase::Local(local), projection: Vec::new() } |
386 | } | |
387 | } | |
388 | ||
389 | impl<'tcx> From<PlaceBase> for PlaceBuilder<'tcx> { | |
390 | fn from(base: PlaceBase) -> Self { | |
391 | Self { base, projection: Vec::new() } | |
e74abb32 XL |
392 | } |
393 | } | |
3157f602 | 394 | |
dc9dc135 | 395 | impl<'a, 'tcx> Builder<'a, 'tcx> { |
ff7c6d11 | 396 | /// Compile `expr`, yielding a place that we can move from etc. |
60c5eb7d XL |
397 | /// |
398 | /// WARNING: Any user code might: | |
399 | /// * Invalidate any slice bounds checks performed. | |
400 | /// * Change the address that this `Place` refers to. | |
401 | /// * Modify the memory that this place refers to. | |
402 | /// * Invalidate the memory that this place refers to, this will be caught | |
403 | /// by borrow checking. | |
404 | /// | |
405 | /// Extra care is needed if any user code is allowed to run between calling | |
406 | /// this method and using it, as is the case for `match` and index | |
407 | /// expressions. | |
923072b8 | 408 | pub(crate) fn as_place( |
6a06907d XL |
409 | &mut self, |
410 | mut block: BasicBlock, | |
17df50a5 | 411 | expr: &Expr<'tcx>, |
6a06907d | 412 | ) -> BlockAnd<Place<'tcx>> { |
e74abb32 | 413 | let place_builder = unpack!(block = self.as_place_builder(block, expr)); |
6a06907d | 414 | block.and(place_builder.into_place(self.tcx, self.typeck_results)) |
e74abb32 XL |
415 | } |
416 | ||
417 | /// This is used when constructing a compound `Place`, so that we can avoid creating | |
418 | /// intermediate `Place` values until we know the full set of projections. | |
923072b8 | 419 | pub(crate) fn as_place_builder( |
5869c6ff XL |
420 | &mut self, |
421 | block: BasicBlock, | |
17df50a5 | 422 | expr: &Expr<'tcx>, |
6a06907d | 423 | ) -> BlockAnd<PlaceBuilder<'tcx>> { |
60c5eb7d | 424 | self.expr_as_place(block, expr, Mutability::Mut, None) |
e9174d1e SL |
425 | } |
426 | ||
b7449926 XL |
427 | /// Compile `expr`, yielding a place that we can move from etc. |
428 | /// Mutability note: The caller of this method promises only to read from the resulting | |
429 | /// place. The place itself may or may not be mutable: | |
430 | /// * If this expr is a place expr like a.b, then we will return that place. | |
431 | /// * Otherwise, a temporary is created: in that event, it will be an immutable temporary. | |
923072b8 | 432 | pub(crate) fn as_read_only_place( |
dfeec247 XL |
433 | &mut self, |
434 | mut block: BasicBlock, | |
17df50a5 | 435 | expr: &Expr<'tcx>, |
6a06907d | 436 | ) -> BlockAnd<Place<'tcx>> { |
e74abb32 | 437 | let place_builder = unpack!(block = self.as_read_only_place_builder(block, expr)); |
6a06907d | 438 | block.and(place_builder.into_place(self.tcx, self.typeck_results)) |
e74abb32 XL |
439 | } |
440 | ||
441 | /// This is used when constructing a compound `Place`, so that we can avoid creating | |
442 | /// intermediate `Place` values until we know the full set of projections. | |
443 | /// Mutability note: The caller of this method promises only to read from the resulting | |
444 | /// place. The place itself may or may not be mutable: | |
445 | /// * If this expr is a place expr like a.b, then we will return that place. | |
446 | /// * Otherwise, a temporary is created: in that event, it will be an immutable temporary. | |
6a06907d | 447 | fn as_read_only_place_builder( |
e74abb32 XL |
448 | &mut self, |
449 | block: BasicBlock, | |
17df50a5 | 450 | expr: &Expr<'tcx>, |
6a06907d | 451 | ) -> BlockAnd<PlaceBuilder<'tcx>> { |
60c5eb7d | 452 | self.expr_as_place(block, expr, Mutability::Not, None) |
b7449926 XL |
453 | } |
454 | ||
455 | fn expr_as_place( | |
456 | &mut self, | |
457 | mut block: BasicBlock, | |
17df50a5 | 458 | expr: &Expr<'tcx>, |
b7449926 | 459 | mutability: Mutability, |
60c5eb7d | 460 | fake_borrow_temps: Option<&mut Vec<Local>>, |
e74abb32 | 461 | ) -> BlockAnd<PlaceBuilder<'tcx>> { |
dfeec247 | 462 | debug!("expr_as_place(block={:?}, expr={:?}, mutability={:?})", block, expr, mutability); |
e9174d1e SL |
463 | |
464 | let this = self; | |
465 | let expr_span = expr.span; | |
3157f602 | 466 | let source_info = this.source_info(expr_span); |
e9174d1e | 467 | match expr.kind { |
dfeec247 XL |
468 | ExprKind::Scope { region_scope, lint_level, value } => { |
469 | this.in_scope((region_scope, source_info), lint_level, |this| { | |
17df50a5 | 470 | this.expr_as_place(block, &this.thir[value], mutability, fake_borrow_temps) |
dfeec247 XL |
471 | }) |
472 | } | |
923072b8 FG |
473 | ExprKind::Field { lhs, variant_index, name } => { |
474 | let lhs = &this.thir[lhs]; | |
475 | let mut place_builder = | |
476 | unpack!(block = this.expr_as_place(block, lhs, mutability, fake_borrow_temps,)); | |
477 | if let ty::Adt(adt_def, _) = lhs.ty.kind() { | |
478 | if adt_def.is_enum() { | |
479 | place_builder = place_builder.downcast(*adt_def, variant_index); | |
480 | } | |
481 | } | |
e74abb32 | 482 | block.and(place_builder.field(name, expr.ty)) |
e9174d1e SL |
483 | } |
484 | ExprKind::Deref { arg } => { | |
17df50a5 XL |
485 | let place_builder = unpack!( |
486 | block = | |
487 | this.expr_as_place(block, &this.thir[arg], mutability, fake_borrow_temps,) | |
488 | ); | |
e74abb32 | 489 | block.and(place_builder.deref()) |
e9174d1e | 490 | } |
dfeec247 XL |
491 | ExprKind::Index { lhs, index } => this.lower_index_expression( |
492 | block, | |
17df50a5 XL |
493 | &this.thir[lhs], |
494 | &this.thir[index], | |
dfeec247 XL |
495 | mutability, |
496 | fake_borrow_temps, | |
497 | expr.temp_lifetime, | |
498 | expr_span, | |
499 | source_info, | |
500 | ), | |
fc512014 | 501 | ExprKind::UpvarRef { closure_def_id, var_hir_id } => { |
923072b8 | 502 | this.lower_captured_upvar(block, closure_def_id.expect_local(), var_hir_id) |
fc512014 XL |
503 | } |
504 | ||
e9174d1e | 505 | ExprKind::VarRef { id } => { |
e74abb32 | 506 | let place_builder = if this.is_bound_var_in_guard(id) { |
8faf50e0 | 507 | let index = this.var_local_id(id, RefWithinGuard); |
e74abb32 | 508 | PlaceBuilder::from(index).deref() |
83c7162d XL |
509 | } else { |
510 | let index = this.var_local_id(id, OutsideGuard); | |
e74abb32 | 511 | PlaceBuilder::from(index) |
83c7162d | 512 | }; |
e74abb32 | 513 | block.and(place_builder) |
e9174d1e | 514 | } |
e9174d1e | 515 | |
0bf4aa26 | 516 | ExprKind::PlaceTypeAscription { source, user_ty } => { |
dfeec247 | 517 | let place_builder = unpack!( |
17df50a5 XL |
518 | block = this.expr_as_place( |
519 | block, | |
520 | &this.thir[source], | |
521 | mutability, | |
522 | fake_borrow_temps, | |
523 | ) | |
dfeec247 | 524 | ); |
0bf4aa26 | 525 | if let Some(user_ty) = user_ty { |
dfeec247 XL |
526 | let annotation_index = |
527 | this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation { | |
9fa01778 XL |
528 | span: source_info.span, |
529 | user_ty, | |
530 | inferred_ty: expr.ty, | |
dfeec247 | 531 | }); |
e74abb32 | 532 | |
6a06907d | 533 | let place = place_builder.clone().into_place(this.tcx, this.typeck_results); |
0bf4aa26 XL |
534 | this.cfg.push( |
535 | block, | |
536 | Statement { | |
537 | source_info, | |
538 | kind: StatementKind::AscribeUserType( | |
94222f64 | 539 | Box::new(( |
e74abb32 | 540 | place, |
dfeec247 | 541 | UserTypeProjection { base: annotation_index, projs: vec![] }, |
94222f64 | 542 | )), |
0bf4aa26 | 543 | Variance::Invariant, |
0bf4aa26 XL |
544 | ), |
545 | }, | |
546 | ); | |
547 | } | |
e74abb32 | 548 | block.and(place_builder) |
0bf4aa26 XL |
549 | } |
550 | ExprKind::ValueTypeAscription { source, user_ty } => { | |
17df50a5 | 551 | let source = &this.thir[source]; |
dfeec247 XL |
552 | let temp = |
553 | unpack!(block = this.as_temp(block, source.temp_lifetime, source, mutability)); | |
0bf4aa26 | 554 | if let Some(user_ty) = user_ty { |
dfeec247 XL |
555 | let annotation_index = |
556 | this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation { | |
9fa01778 XL |
557 | span: source_info.span, |
558 | user_ty, | |
559 | inferred_ty: expr.ty, | |
dfeec247 | 560 | }); |
0bf4aa26 XL |
561 | this.cfg.push( |
562 | block, | |
563 | Statement { | |
564 | source_info, | |
565 | kind: StatementKind::AscribeUserType( | |
94222f64 | 566 | Box::new(( |
dfeec247 XL |
567 | Place::from(temp), |
568 | UserTypeProjection { base: annotation_index, projs: vec![] }, | |
94222f64 | 569 | )), |
0bf4aa26 | 570 | Variance::Invariant, |
0bf4aa26 XL |
571 | ), |
572 | }, | |
573 | ); | |
574 | } | |
e74abb32 | 575 | block.and(PlaceBuilder::from(temp)) |
0bf4aa26 XL |
576 | } |
577 | ||
b7449926 XL |
578 | ExprKind::Array { .. } |
579 | | ExprKind::Tuple { .. } | |
580 | | ExprKind::Adt { .. } | |
581 | | ExprKind::Closure { .. } | |
582 | | ExprKind::Unary { .. } | |
583 | | ExprKind::Binary { .. } | |
584 | | ExprKind::LogicalOp { .. } | |
585 | | ExprKind::Box { .. } | |
586 | | ExprKind::Cast { .. } | |
587 | | ExprKind::Use { .. } | |
588 | | ExprKind::NeverToAny { .. } | |
48663c56 | 589 | | ExprKind::Pointer { .. } |
b7449926 XL |
590 | | ExprKind::Repeat { .. } |
591 | | ExprKind::Borrow { .. } | |
dfeec247 | 592 | | ExprKind::AddressOf { .. } |
b7449926 | 593 | | ExprKind::Match { .. } |
5869c6ff | 594 | | ExprKind::If { .. } |
b7449926 XL |
595 | | ExprKind::Loop { .. } |
596 | | ExprKind::Block { .. } | |
94222f64 | 597 | | ExprKind::Let { .. } |
b7449926 XL |
598 | | ExprKind::Assign { .. } |
599 | | ExprKind::AssignOp { .. } | |
600 | | ExprKind::Break { .. } | |
601 | | ExprKind::Continue { .. } | |
602 | | ExprKind::Return { .. } | |
603 | | ExprKind::Literal { .. } | |
5e7ed085 FG |
604 | | ExprKind::NamedConst { .. } |
605 | | ExprKind::NonHirLiteral { .. } | |
064997fb | 606 | | ExprKind::ZstLiteral { .. } |
5e7ed085 | 607 | | ExprKind::ConstParam { .. } |
29967ef6 | 608 | | ExprKind::ConstBlock { .. } |
60c5eb7d | 609 | | ExprKind::StaticRef { .. } |
f9f354fc | 610 | | ExprKind::InlineAsm { .. } |
b7449926 | 611 | | ExprKind::Yield { .. } |
f9f354fc | 612 | | ExprKind::ThreadLocalRef(_) |
b7449926 | 613 | | ExprKind::Call { .. } => { |
ff7c6d11 | 614 | // these are not places, so we need to make a temporary. |
29967ef6 | 615 | debug_assert!(!matches!(Category::of(&expr.kind), Some(Category::Place))); |
b7449926 XL |
616 | let temp = |
617 | unpack!(block = this.as_temp(block, expr.temp_lifetime, expr, mutability)); | |
e74abb32 | 618 | block.and(PlaceBuilder::from(temp)) |
e9174d1e SL |
619 | } |
620 | } | |
621 | } | |
60c5eb7d | 622 | |
fc512014 XL |
623 | /// Lower a captured upvar. Note we might not know the actual capture index, |
624 | /// so we create a place starting from `PlaceBase::Upvar`, which will be resolved | |
cdc7bbd5 | 625 | /// once all projections that allow us to identify a capture have been applied. |
fc512014 XL |
626 | fn lower_captured_upvar( |
627 | &mut self, | |
628 | block: BasicBlock, | |
064997fb | 629 | closure_def_id: LocalDefId, |
923072b8 | 630 | var_hir_id: LocalVarId, |
fc512014 | 631 | ) -> BlockAnd<PlaceBuilder<'tcx>> { |
923072b8 | 632 | let closure_ty = |
064997fb | 633 | self.typeck_results.node_type(self.tcx.hir().local_def_id_to_hir_id(closure_def_id)); |
fc512014 XL |
634 | |
635 | let closure_kind = if let ty::Closure(_, closure_substs) = closure_ty.kind() { | |
6a06907d | 636 | self.infcx.closure_kind(closure_substs).unwrap() |
fc512014 XL |
637 | } else { |
638 | // Generators are considered FnOnce. | |
639 | ty::ClosureKind::FnOnce | |
640 | }; | |
641 | ||
064997fb | 642 | block.and(PlaceBuilder::from(PlaceBase::Upvar { var_hir_id, closure_def_id, closure_kind })) |
fc512014 XL |
643 | } |
644 | ||
60c5eb7d XL |
645 | /// Lower an index expression |
646 | /// | |
647 | /// This has two complications; | |
648 | /// | |
649 | /// * We need to do a bounds check. | |
650 | /// * We need to ensure that the bounds check can't be invalidated using an | |
651 | /// expression like `x[1][{x = y; 2}]`. We use fake borrows here to ensure | |
652 | /// that this is the case. | |
653 | fn lower_index_expression( | |
654 | &mut self, | |
655 | mut block: BasicBlock, | |
17df50a5 XL |
656 | base: &Expr<'tcx>, |
657 | index: &Expr<'tcx>, | |
60c5eb7d XL |
658 | mutability: Mutability, |
659 | fake_borrow_temps: Option<&mut Vec<Local>>, | |
660 | temp_lifetime: Option<region::Scope>, | |
661 | expr_span: Span, | |
dfeec247 | 662 | source_info: SourceInfo, |
60c5eb7d | 663 | ) -> BlockAnd<PlaceBuilder<'tcx>> { |
60c5eb7d XL |
664 | let base_fake_borrow_temps = &mut Vec::new(); |
665 | let is_outermost_index = fake_borrow_temps.is_none(); | |
666 | let fake_borrow_temps = fake_borrow_temps.unwrap_or(base_fake_borrow_temps); | |
667 | ||
fc512014 | 668 | let mut base_place = |
6a06907d | 669 | unpack!(block = self.expr_as_place(block, base, mutability, Some(fake_borrow_temps),)); |
60c5eb7d XL |
670 | |
671 | // Making this a *fresh* temporary means we do not have to worry about | |
672 | // the index changing later: Nothing will ever change this temporary. | |
673 | // The "retagging" transformation (for Stacked Borrows) relies on this. | |
dfeec247 | 674 | let idx = unpack!(block = self.as_temp(block, temp_lifetime, index, Mutability::Not,)); |
60c5eb7d | 675 | |
6a06907d | 676 | block = self.bounds_check(block, base_place.clone(), idx, expr_span, source_info); |
60c5eb7d XL |
677 | |
678 | if is_outermost_index { | |
679 | self.read_fake_borrows(block, fake_borrow_temps, source_info) | |
680 | } else { | |
6a06907d | 681 | base_place = base_place.expect_upvars_resolved(self.tcx, self.typeck_results); |
60c5eb7d XL |
682 | self.add_fake_borrows_of_base( |
683 | &base_place, | |
684 | block, | |
685 | fake_borrow_temps, | |
686 | expr_span, | |
687 | source_info, | |
688 | ); | |
689 | } | |
690 | ||
691 | block.and(base_place.index(idx)) | |
692 | } | |
693 | ||
694 | fn bounds_check( | |
695 | &mut self, | |
696 | block: BasicBlock, | |
6a06907d | 697 | slice: PlaceBuilder<'tcx>, |
60c5eb7d XL |
698 | index: Local, |
699 | expr_span: Span, | |
700 | source_info: SourceInfo, | |
701 | ) -> BasicBlock { | |
6a06907d XL |
702 | let usize_ty = self.tcx.types.usize; |
703 | let bool_ty = self.tcx.types.bool; | |
60c5eb7d XL |
704 | // bounds check: |
705 | let len = self.temp(usize_ty, expr_span); | |
706 | let lt = self.temp(bool_ty, expr_span); | |
707 | ||
708 | // len = len(slice) | |
6a06907d XL |
709 | self.cfg.push_assign( |
710 | block, | |
711 | source_info, | |
712 | len, | |
713 | Rvalue::Len(slice.into_place(self.tcx, self.typeck_results)), | |
714 | ); | |
60c5eb7d XL |
715 | // lt = idx < len |
716 | self.cfg.push_assign( | |
717 | block, | |
718 | source_info, | |
ba9703b0 | 719 | lt, |
6a06907d XL |
720 | Rvalue::BinaryOp( |
721 | BinOp::Lt, | |
94222f64 | 722 | Box::new((Operand::Copy(Place::from(index)), Operand::Copy(len))), |
6a06907d | 723 | ), |
60c5eb7d | 724 | ); |
dfeec247 | 725 | let msg = BoundsCheck { len: Operand::Move(len), index: Operand::Copy(Place::from(index)) }; |
60c5eb7d XL |
726 | // assert!(lt, "...") |
727 | self.assert(block, Operand::Move(lt), true, msg, expr_span) | |
728 | } | |
729 | ||
730 | fn add_fake_borrows_of_base( | |
731 | &mut self, | |
732 | base_place: &PlaceBuilder<'tcx>, | |
733 | block: BasicBlock, | |
734 | fake_borrow_temps: &mut Vec<Local>, | |
735 | expr_span: Span, | |
736 | source_info: SourceInfo, | |
737 | ) { | |
6a06907d | 738 | let tcx = self.tcx; |
fc512014 XL |
739 | let local = match base_place.base { |
740 | PlaceBase::Local(local) => local, | |
5869c6ff | 741 | PlaceBase::Upvar { .. } => bug!("Expected PlacseBase::Local found Upvar"), |
fc512014 XL |
742 | }; |
743 | ||
744 | let place_ty = Place::ty_from(local, &base_place.projection, &self.local_decls, tcx); | |
1b1a35ee | 745 | if let ty::Slice(_) = place_ty.ty.kind() { |
60c5eb7d XL |
746 | // We need to create fake borrows to ensure that the bounds |
747 | // check that we just did stays valid. Since we can't assign to | |
748 | // unsized values, we only need to ensure that none of the | |
749 | // pointers in the base place are modified. | |
750 | for (idx, elem) in base_place.projection.iter().enumerate().rev() { | |
751 | match elem { | |
752 | ProjectionElem::Deref => { | |
753 | let fake_borrow_deref_ty = Place::ty_from( | |
fc512014 | 754 | local, |
60c5eb7d XL |
755 | &base_place.projection[..idx], |
756 | &self.local_decls, | |
757 | tcx, | |
dfeec247 XL |
758 | ) |
759 | .ty; | |
760 | let fake_borrow_ty = | |
761 | tcx.mk_imm_ref(tcx.lifetimes.re_erased, fake_borrow_deref_ty); | |
762 | let fake_borrow_temp = | |
f9f354fc | 763 | self.local_decls.push(LocalDecl::new(fake_borrow_ty, expr_span)); |
60c5eb7d XL |
764 | let projection = tcx.intern_place_elems(&base_place.projection[..idx]); |
765 | self.cfg.push_assign( | |
766 | block, | |
767 | source_info, | |
ba9703b0 | 768 | fake_borrow_temp.into(), |
60c5eb7d XL |
769 | Rvalue::Ref( |
770 | tcx.lifetimes.re_erased, | |
771 | BorrowKind::Shallow, | |
fc512014 | 772 | Place { local, projection }, |
60c5eb7d XL |
773 | ), |
774 | ); | |
775 | fake_borrow_temps.push(fake_borrow_temp); | |
776 | } | |
777 | ProjectionElem::Index(_) => { | |
778 | let index_ty = Place::ty_from( | |
fc512014 | 779 | local, |
60c5eb7d XL |
780 | &base_place.projection[..idx], |
781 | &self.local_decls, | |
782 | tcx, | |
783 | ); | |
1b1a35ee | 784 | match index_ty.ty.kind() { |
60c5eb7d XL |
785 | // The previous index expression has already |
786 | // done any index expressions needed here. | |
787 | ty::Slice(_) => break, | |
788 | ty::Array(..) => (), | |
789 | _ => bug!("unexpected index base"), | |
790 | } | |
791 | } | |
792 | ProjectionElem::Field(..) | |
793 | | ProjectionElem::Downcast(..) | |
794 | | ProjectionElem::ConstantIndex { .. } | |
795 | | ProjectionElem::Subslice { .. } => (), | |
796 | } | |
797 | } | |
798 | } | |
799 | } | |
800 | ||
801 | fn read_fake_borrows( | |
802 | &mut self, | |
803 | bb: BasicBlock, | |
804 | fake_borrow_temps: &mut Vec<Local>, | |
805 | source_info: SourceInfo, | |
806 | ) { | |
807 | // All indexes have been evaluated now, read all of the | |
808 | // fake borrows so that they are live across those index | |
809 | // expressions. | |
810 | for temp in fake_borrow_temps { | |
811 | self.cfg.push_fake_read(bb, source_info, FakeReadCause::ForIndex, Place::from(*temp)); | |
812 | } | |
813 | } | |
e9174d1e | 814 | } |
136023e0 XL |
815 | |
816 | /// Precise capture is enabled if the feature gate `capture_disjoint_fields` is enabled or if | |
817 | /// user is using Rust Edition 2021 or higher. | |
818 | fn enable_precise_capture(tcx: TyCtxt<'_>, closure_span: Span) -> bool { | |
819 | tcx.features().capture_disjoint_fields || closure_span.rust_2021() | |
820 | } |