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