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