]>
Commit | Line | Data |
---|---|---|
dfeec247 | 1 | use rustc_hir as hir; |
3dfed10e | 2 | use rustc_hir::lang_items::LangItem; |
e74abb32 | 3 | use rustc_index::vec::Idx; |
c295e0f8 | 4 | use rustc_middle::mir::patch::MirPatch; |
ba9703b0 XL |
5 | use rustc_middle::mir::*; |
6 | use rustc_middle::traits::Reveal; | |
7 | use rustc_middle::ty::subst::SubstsRef; | |
8 | use rustc_middle::ty::util::IntTypeExt; | |
9 | use rustc_middle::ty::{self, Ty, TyCtxt}; | |
10 | use rustc_target::abi::VariantIdx; | |
5099ac24 | 11 | use std::{fmt, iter}; |
cc61c64b | 12 | |
f9f354fc | 13 | /// The value of an inserted drop flag. |
cc61c64b XL |
14 | #[derive(Debug, PartialEq, Eq, Copy, Clone)] |
15 | pub enum DropFlagState { | |
f9f354fc XL |
16 | /// The tracked value is initialized and needs to be dropped when leaving its scope. |
17 | Present, | |
18 | ||
19 | /// The tracked value is uninitialized or was moved out of and does not need to be dropped when | |
20 | /// leaving its scope. | |
21 | Absent, | |
cc61c64b XL |
22 | } |
23 | ||
24 | impl DropFlagState { | |
25 | pub fn value(self) -> bool { | |
26 | match self { | |
27 | DropFlagState::Present => true, | |
dfeec247 | 28 | DropFlagState::Absent => false, |
cc61c64b XL |
29 | } |
30 | } | |
31 | } | |
32 | ||
f9f354fc | 33 | /// Describes how/if a value should be dropped. |
cc61c64b XL |
34 | #[derive(Debug)] |
35 | pub enum DropStyle { | |
f9f354fc | 36 | /// The value is already dead at the drop location, no drop will be executed. |
cc61c64b | 37 | Dead, |
f9f354fc XL |
38 | |
39 | /// The value is known to always be initialized at the drop location, drop will always be | |
40 | /// executed. | |
cc61c64b | 41 | Static, |
f9f354fc XL |
42 | |
43 | /// Whether the value needs to be dropped depends on its drop flag. | |
cc61c64b | 44 | Conditional, |
f9f354fc XL |
45 | |
46 | /// An "open" drop is one where only the fields of a value are dropped. | |
47 | /// | |
48 | /// For example, this happens when moving out of a struct field: The rest of the struct will be | |
49 | /// dropped in such an "open" drop. It is also used to generate drop glue for the individual | |
50 | /// components of a value, for example for dropping array elements. | |
cc61c64b XL |
51 | Open, |
52 | } | |
53 | ||
f9f354fc | 54 | /// Which drop flags to affect/check with an operation. |
cc61c64b XL |
55 | #[derive(Debug)] |
56 | pub enum DropFlagMode { | |
f9f354fc | 57 | /// Only affect the top-level drop flag, not that of any contained fields. |
cc61c64b | 58 | Shallow, |
f9f354fc | 59 | /// Affect all nested drop flags in addition to the top-level one. |
dfeec247 | 60 | Deep, |
cc61c64b XL |
61 | } |
62 | ||
f9f354fc | 63 | /// Describes if unwinding is necessary and where to unwind to if a panic occurs. |
7cac9316 XL |
64 | #[derive(Copy, Clone, Debug)] |
65 | pub enum Unwind { | |
f9f354fc | 66 | /// Unwind to this block. |
7cac9316 | 67 | To(BasicBlock), |
f9f354fc | 68 | /// Already in an unwind path, any panic will cause an abort. |
dfeec247 | 69 | InCleanup, |
7cac9316 XL |
70 | } |
71 | ||
72 | impl Unwind { | |
73 | fn is_cleanup(self) -> bool { | |
74 | match self { | |
75 | Unwind::To(..) => false, | |
dfeec247 | 76 | Unwind::InCleanup => true, |
7cac9316 XL |
77 | } |
78 | } | |
79 | ||
80 | fn into_option(self) -> Option<BasicBlock> { | |
81 | match self { | |
82 | Unwind::To(bb) => Some(bb), | |
83 | Unwind::InCleanup => None, | |
84 | } | |
85 | } | |
86 | ||
dfeec247 XL |
87 | fn map<F>(self, f: F) -> Self |
88 | where | |
89 | F: FnOnce(BasicBlock) -> BasicBlock, | |
90 | { | |
7cac9316 XL |
91 | match self { |
92 | Unwind::To(bb) => Unwind::To(f(bb)), | |
dfeec247 | 93 | Unwind::InCleanup => Unwind::InCleanup, |
7cac9316 XL |
94 | } |
95 | } | |
96 | } | |
97 | ||
dc9dc135 | 98 | pub trait DropElaborator<'a, 'tcx>: fmt::Debug { |
f9f354fc XL |
99 | /// The type representing paths that can be moved out of. |
100 | /// | |
101 | /// Users can move out of individual fields of a struct, such as `a.b.c`. This type is used to | |
102 | /// represent such move paths. Sometimes tracking individual move paths is not necessary, in | |
103 | /// which case this may be set to (for example) `()`. | |
dfeec247 | 104 | type Path: Copy + fmt::Debug; |
cc61c64b | 105 | |
f9f354fc XL |
106 | // Accessors |
107 | ||
cc61c64b | 108 | fn patch(&mut self) -> &mut MirPatch<'tcx>; |
dc9dc135 XL |
109 | fn body(&self) -> &'a Body<'tcx>; |
110 | fn tcx(&self) -> TyCtxt<'tcx>; | |
7cac9316 | 111 | fn param_env(&self) -> ty::ParamEnv<'tcx>; |
cc61c64b | 112 | |
f9f354fc XL |
113 | // Drop logic |
114 | ||
115 | /// Returns how `path` should be dropped, given `mode`. | |
cc61c64b | 116 | fn drop_style(&self, path: Self::Path, mode: DropFlagMode) -> DropStyle; |
f9f354fc XL |
117 | |
118 | /// Returns the drop flag of `path` as a MIR `Operand` (or `None` if `path` has no drop flag). | |
cc61c64b | 119 | fn get_drop_flag(&mut self, path: Self::Path) -> Option<Operand<'tcx>>; |
f9f354fc XL |
120 | |
121 | /// Modifies the MIR patch so that the drop flag of `path` (if any) is cleared at `location`. | |
122 | /// | |
123 | /// If `mode` is deep, drop flags of all child paths should also be cleared by inserting | |
124 | /// additional statements. | |
cc61c64b XL |
125 | fn clear_drop_flag(&mut self, location: Location, path: Self::Path, mode: DropFlagMode); |
126 | ||
f9f354fc XL |
127 | // Subpaths |
128 | ||
129 | /// Returns the subpath of a field of `path` (or `None` if there is no dedicated subpath). | |
130 | /// | |
131 | /// If this returns `None`, `field` will not get a dedicated drop flag. | |
cc61c64b | 132 | fn field_subpath(&self, path: Self::Path, field: Field) -> Option<Self::Path>; |
f9f354fc XL |
133 | |
134 | /// Returns the subpath of a dereference of `path` (or `None` if there is no dedicated subpath). | |
135 | /// | |
136 | /// If this returns `None`, `*path` will not get a dedicated drop flag. | |
137 | /// | |
138 | /// This is only relevant for `Box<T>`, where the contained `T` can be moved out of the box. | |
cc61c64b | 139 | fn deref_subpath(&self, path: Self::Path) -> Option<Self::Path>; |
f9f354fc XL |
140 | |
141 | /// Returns the subpath of downcasting `path` to one of its variants. | |
142 | /// | |
143 | /// If this returns `None`, the downcast of `path` will not get a dedicated drop flag. | |
a1dfa0c6 | 144 | fn downcast_subpath(&self, path: Self::Path, variant: VariantIdx) -> Option<Self::Path>; |
f9f354fc XL |
145 | |
146 | /// Returns the subpath of indexing a fixed-size array `path`. | |
147 | /// | |
148 | /// If this returns `None`, elements of `path` will not get a dedicated drop flag. | |
149 | /// | |
150 | /// This is only relevant for array patterns, which can move out of individual array elements. | |
1b1a35ee | 151 | fn array_subpath(&self, path: Self::Path, index: u64, size: u64) -> Option<Self::Path>; |
cc61c64b XL |
152 | } |
153 | ||
154 | #[derive(Debug)] | |
dc9dc135 XL |
155 | struct DropCtxt<'l, 'b, 'tcx, D> |
156 | where | |
157 | D: DropElaborator<'b, 'tcx>, | |
cc61c64b XL |
158 | { |
159 | elaborator: &'l mut D, | |
160 | ||
161 | source_info: SourceInfo, | |
cc61c64b | 162 | |
ba9703b0 | 163 | place: Place<'tcx>, |
cc61c64b XL |
164 | path: D::Path, |
165 | succ: BasicBlock, | |
7cac9316 | 166 | unwind: Unwind, |
cc61c64b XL |
167 | } |
168 | ||
f9f354fc XL |
169 | /// "Elaborates" a drop of `place`/`path` and patches `bb`'s terminator to execute it. |
170 | /// | |
171 | /// The passed `elaborator` is used to determine what should happen at the drop terminator. It | |
172 | /// decides whether the drop can be statically determined or whether it needs a dynamic drop flag, | |
173 | /// and whether the drop is "open", ie. should be expanded to drop all subfields of the dropped | |
174 | /// value. | |
175 | /// | |
176 | /// When this returns, the MIR patch in the `elaborator` contains the necessary changes. | |
cc61c64b XL |
177 | pub fn elaborate_drop<'b, 'tcx, D>( |
178 | elaborator: &mut D, | |
179 | source_info: SourceInfo, | |
ba9703b0 | 180 | place: Place<'tcx>, |
cc61c64b XL |
181 | path: D::Path, |
182 | succ: BasicBlock, | |
7cac9316 | 183 | unwind: Unwind, |
dc9dc135 XL |
184 | bb: BasicBlock, |
185 | ) where | |
186 | D: DropElaborator<'b, 'tcx>, | |
187 | 'tcx: 'b, | |
cc61c64b | 188 | { |
dfeec247 | 189 | DropCtxt { elaborator, source_info, place, path, succ, unwind }.elaborate_drop(bb) |
cc61c64b XL |
190 | } |
191 | ||
192 | impl<'l, 'b, 'tcx, D> DropCtxt<'l, 'b, 'tcx, D> | |
dc9dc135 XL |
193 | where |
194 | D: DropElaborator<'b, 'tcx>, | |
195 | 'tcx: 'b, | |
cc61c64b | 196 | { |
ba9703b0 | 197 | fn place_ty(&self, place: Place<'tcx>) -> Ty<'tcx> { |
dc9dc135 | 198 | place.ty(self.elaborator.body(), self.tcx()).ty |
cc61c64b XL |
199 | } |
200 | ||
dc9dc135 | 201 | fn tcx(&self) -> TyCtxt<'tcx> { |
cc61c64b XL |
202 | self.elaborator.tcx() |
203 | } | |
204 | ||
205 | /// This elaborates a single drop instruction, located at `bb`, and | |
206 | /// patches over it. | |
207 | /// | |
208 | /// The elaborated drop checks the drop flags to only drop what | |
209 | /// is initialized. | |
210 | /// | |
211 | /// In addition, the relevant drop flags also need to be cleared | |
212 | /// to avoid double-drops. However, in the middle of a complex | |
213 | /// drop, one must avoid clearing some of the flags before they | |
214 | /// are read, as that would cause a memory leak. | |
215 | /// | |
216 | /// In particular, when dropping an ADT, multiple fields may be | |
217 | /// joined together under the `rest` subpath. They are all controlled | |
218 | /// by the primary drop flag, but only the last rest-field dropped | |
219 | /// should clear it (and it must also not clear anything else). | |
9fa01778 XL |
220 | // |
221 | // FIXME: I think we should just control the flags externally, | |
222 | // and then we do not need this machinery. | |
dc9dc135 | 223 | pub fn elaborate_drop(&mut self, bb: BasicBlock) { |
74b04a01 | 224 | debug!("elaborate_drop({:?}, {:?})", bb, self); |
cc61c64b | 225 | let style = self.elaborator.drop_style(self.path, DropFlagMode::Deep); |
74b04a01 | 226 | debug!("elaborate_drop({:?}, {:?}): live - {:?}", bb, self, style); |
cc61c64b XL |
227 | match style { |
228 | DropStyle::Dead => { | |
dfeec247 XL |
229 | self.elaborator |
230 | .patch() | |
231 | .patch_terminator(bb, TerminatorKind::Goto { target: self.succ }); | |
cc61c64b XL |
232 | } |
233 | DropStyle::Static => { | |
dfeec247 XL |
234 | self.elaborator.patch().patch_terminator( |
235 | bb, | |
236 | TerminatorKind::Drop { | |
f035d41b | 237 | place: self.place, |
dfeec247 XL |
238 | target: self.succ, |
239 | unwind: self.unwind.into_option(), | |
240 | }, | |
241 | ); | |
cc61c64b XL |
242 | } |
243 | DropStyle::Conditional => { | |
29967ef6 | 244 | let drop_bb = self.complete_drop(self.succ, self.unwind); |
dfeec247 XL |
245 | self.elaborator |
246 | .patch() | |
247 | .patch_terminator(bb, TerminatorKind::Goto { target: drop_bb }); | |
cc61c64b XL |
248 | } |
249 | DropStyle::Open => { | |
250 | let drop_bb = self.open_drop(); | |
dfeec247 XL |
251 | self.elaborator |
252 | .patch() | |
253 | .patch_terminator(bb, TerminatorKind::Goto { target: drop_bb }); | |
cc61c64b XL |
254 | } |
255 | } | |
256 | } | |
257 | ||
9fa01778 | 258 | /// Returns the place and move path for each field of `variant`, |
cc61c64b | 259 | /// (the move path is `None` if the field is a rest field). |
dfeec247 XL |
260 | fn move_paths_for_fields( |
261 | &self, | |
ba9703b0 | 262 | base_place: Place<'tcx>, |
dfeec247 XL |
263 | variant_path: D::Path, |
264 | variant: &'tcx ty::VariantDef, | |
265 | substs: SubstsRef<'tcx>, | |
266 | ) -> Vec<(Place<'tcx>, Option<D::Path>)> { | |
267 | variant | |
268 | .fields | |
269 | .iter() | |
270 | .enumerate() | |
271 | .map(|(i, f)| { | |
272 | let field = Field::new(i); | |
273 | let subpath = self.elaborator.field_subpath(variant_path, field); | |
274 | let tcx = self.tcx(); | |
275 | ||
f035d41b | 276 | assert_eq!(self.elaborator.param_env().reveal(), Reveal::All); |
dfeec247 XL |
277 | let field_ty = |
278 | tcx.normalize_erasing_regions(self.elaborator.param_env(), f.ty(tcx, substs)); | |
ba9703b0 | 279 | (tcx.mk_place_field(base_place, field, field_ty), subpath) |
dfeec247 XL |
280 | }) |
281 | .collect() | |
cc61c64b XL |
282 | } |
283 | ||
dfeec247 XL |
284 | fn drop_subpath( |
285 | &mut self, | |
ba9703b0 | 286 | place: Place<'tcx>, |
dfeec247 XL |
287 | path: Option<D::Path>, |
288 | succ: BasicBlock, | |
289 | unwind: Unwind, | |
290 | ) -> BasicBlock { | |
cc61c64b | 291 | if let Some(path) = path { |
ff7c6d11 | 292 | debug!("drop_subpath: for std field {:?}", place); |
cc61c64b XL |
293 | |
294 | DropCtxt { | |
295 | elaborator: self.elaborator, | |
296 | source_info: self.source_info, | |
dfeec247 XL |
297 | path, |
298 | place, | |
299 | succ, | |
300 | unwind, | |
301 | } | |
302 | .elaborated_drop_block() | |
cc61c64b | 303 | } else { |
ff7c6d11 | 304 | debug!("drop_subpath: for rest field {:?}", place); |
cc61c64b XL |
305 | |
306 | DropCtxt { | |
307 | elaborator: self.elaborator, | |
308 | source_info: self.source_info, | |
dfeec247 XL |
309 | place, |
310 | succ, | |
311 | unwind, | |
cc61c64b XL |
312 | // Using `self.path` here to condition the drop on |
313 | // our own drop flag. | |
dfeec247 XL |
314 | path: self.path, |
315 | } | |
29967ef6 | 316 | .complete_drop(succ, unwind) |
cc61c64b XL |
317 | } |
318 | } | |
319 | ||
9fa01778 | 320 | /// Creates one-half of the drop ladder for a list of fields, and return |
7cac9316 XL |
321 | /// the list of steps in it in reverse order, with the first step |
322 | /// dropping 0 fields and so on. | |
cc61c64b XL |
323 | /// |
324 | /// `unwind_ladder` is such a list of steps in reverse order, | |
7cac9316 | 325 | /// which is called if the matching step of the drop glue panics. |
dfeec247 XL |
326 | fn drop_halfladder( |
327 | &mut self, | |
328 | unwind_ladder: &[Unwind], | |
329 | mut succ: BasicBlock, | |
330 | fields: &[(Place<'tcx>, Option<D::Path>)], | |
331 | ) -> Vec<BasicBlock> { | |
5099ac24 | 332 | iter::once(succ) |
ba9703b0 XL |
333 | .chain(fields.iter().rev().zip(unwind_ladder).map(|(&(place, path), &unwind_succ)| { |
334 | succ = self.drop_subpath(place, path, succ, unwind_succ); | |
335 | succ | |
336 | })) | |
dfeec247 | 337 | .collect() |
7cac9316 | 338 | } |
cc61c64b | 339 | |
7cac9316 XL |
340 | fn drop_ladder_bottom(&mut self) -> (BasicBlock, Unwind) { |
341 | // Clear the "master" drop flag at the end. This is needed | |
342 | // because the "master" drop protects the ADT's discriminant, | |
343 | // which is invalidated after the ADT is dropped. | |
29967ef6 | 344 | (self.drop_flag_reset_block(DropFlagMode::Shallow, self.succ, self.unwind), self.unwind) |
cc61c64b XL |
345 | } |
346 | ||
9fa01778 | 347 | /// Creates a full drop ladder, consisting of 2 connected half-drop-ladders |
cc61c64b XL |
348 | /// |
349 | /// For example, with 3 fields, the drop ladder is | |
350 | /// | |
351 | /// .d0: | |
352 | /// ELAB(drop location.0 [target=.d1, unwind=.c1]) | |
353 | /// .d1: | |
354 | /// ELAB(drop location.1 [target=.d2, unwind=.c2]) | |
355 | /// .d2: | |
356 | /// ELAB(drop location.2 [target=`self.succ`, unwind=`self.unwind`]) | |
357 | /// .c1: | |
358 | /// ELAB(drop location.1 [target=.c2]) | |
359 | /// .c2: | |
360 | /// ELAB(drop location.2 [target=`self.unwind`]) | |
7cac9316 XL |
361 | /// |
362 | /// NOTE: this does not clear the master drop flag, so you need | |
363 | /// to point succ/unwind on a `drop_ladder_bottom`. | |
dc9dc135 XL |
364 | fn drop_ladder( |
365 | &mut self, | |
366 | fields: Vec<(Place<'tcx>, Option<D::Path>)>, | |
367 | succ: BasicBlock, | |
368 | unwind: Unwind, | |
369 | ) -> (BasicBlock, Unwind) { | |
cc61c64b XL |
370 | debug!("drop_ladder({:?}, {:?})", self, fields); |
371 | ||
372 | let mut fields = fields; | |
ba9703b0 | 373 | fields.retain(|&(place, _)| { |
ff7c6d11 | 374 | self.place_ty(place).needs_drop(self.tcx(), self.elaborator.param_env()) |
cc61c64b XL |
375 | }); |
376 | ||
377 | debug!("drop_ladder - fields needing drop: {:?}", fields); | |
378 | ||
7cac9316 XL |
379 | let unwind_ladder = vec![Unwind::InCleanup; fields.len() + 1]; |
380 | let unwind_ladder: Vec<_> = if let Unwind::To(target) = unwind { | |
381 | let halfladder = self.drop_halfladder(&unwind_ladder, target, &fields); | |
382 | halfladder.into_iter().map(Unwind::To).collect() | |
cc61c64b | 383 | } else { |
7cac9316 | 384 | unwind_ladder |
cc61c64b XL |
385 | }; |
386 | ||
dfeec247 | 387 | let normal_ladder = self.drop_halfladder(&unwind_ladder, succ, &fields); |
cc61c64b | 388 | |
7cac9316 | 389 | (*normal_ladder.last().unwrap(), *unwind_ladder.last().unwrap()) |
cc61c64b XL |
390 | } |
391 | ||
dc9dc135 | 392 | fn open_drop_for_tuple(&mut self, tys: &[Ty<'tcx>]) -> BasicBlock { |
cc61c64b XL |
393 | debug!("open_drop_for_tuple({:?}, {:?})", self, tys); |
394 | ||
dfeec247 XL |
395 | let fields = tys |
396 | .iter() | |
397 | .enumerate() | |
398 | .map(|(i, &ty)| { | |
399 | ( | |
ba9703b0 | 400 | self.tcx().mk_place_field(self.place, Field::new(i), ty), |
dfeec247 XL |
401 | self.elaborator.field_subpath(self.path, Field::new(i)), |
402 | ) | |
403 | }) | |
404 | .collect(); | |
cc61c64b | 405 | |
7cac9316 XL |
406 | let (succ, unwind) = self.drop_ladder_bottom(); |
407 | self.drop_ladder(fields, succ, unwind).0 | |
cc61c64b XL |
408 | } |
409 | ||
5e7ed085 | 410 | fn open_drop_for_box(&mut self, adt: ty::AdtDef<'tcx>, substs: SubstsRef<'tcx>) -> BasicBlock { |
83c7162d | 411 | debug!("open_drop_for_box({:?}, {:?}, {:?})", self, adt, substs); |
cc61c64b | 412 | |
923072b8 FG |
413 | // drop glue is sent straight to codegen |
414 | // box cannot be directly dereferenced | |
415 | let unique_ty = adt.non_enum_variant().fields[0].ty(self.tcx(), substs); | |
416 | let nonnull_ty = | |
417 | unique_ty.ty_adt_def().unwrap().non_enum_variant().fields[0].ty(self.tcx(), substs); | |
418 | let ptr_ty = self.tcx().mk_imm_ptr(substs[0].expect_ty()); | |
419 | ||
420 | let unique_place = self.tcx().mk_place_field(self.place, Field::new(0), unique_ty); | |
421 | let nonnull_place = self.tcx().mk_place_field(unique_place, Field::new(0), nonnull_ty); | |
422 | let ptr_place = self.tcx().mk_place_field(nonnull_place, Field::new(0), ptr_ty); | |
423 | let interior = self.tcx().mk_place_deref(ptr_place); | |
424 | ||
cc61c64b XL |
425 | let interior_path = self.elaborator.deref_subpath(self.path); |
426 | ||
f9f354fc | 427 | let succ = self.box_free_block(adt, substs, self.succ, self.unwind); |
dfeec247 XL |
428 | let unwind_succ = |
429 | self.unwind.map(|unwind| self.box_free_block(adt, substs, unwind, Unwind::InCleanup)); | |
cc61c64b | 430 | |
ba9703b0 | 431 | self.drop_subpath(interior, interior_path, succ, unwind_succ) |
cc61c64b XL |
432 | } |
433 | ||
5e7ed085 | 434 | fn open_drop_for_adt(&mut self, adt: ty::AdtDef<'tcx>, substs: SubstsRef<'tcx>) -> BasicBlock { |
cc61c64b | 435 | debug!("open_drop_for_adt({:?}, {:?}, {:?})", self, adt, substs); |
5e7ed085 | 436 | if adt.variants().is_empty() { |
cc61c64b XL |
437 | return self.elaborator.patch().new_block(BasicBlockData { |
438 | statements: vec![], | |
439 | terminator: Some(Terminator { | |
440 | source_info: self.source_info, | |
dfeec247 | 441 | kind: TerminatorKind::Unreachable, |
cc61c64b | 442 | }), |
dfeec247 | 443 | is_cleanup: self.unwind.is_cleanup(), |
cc61c64b XL |
444 | }); |
445 | } | |
446 | ||
8faf50e0 | 447 | let skip_contents = |
5e7ed085 | 448 | adt.is_union() || Some(adt.did()) == self.tcx().lang_items().manually_drop(); |
8faf50e0 | 449 | let contents_drop = if skip_contents { |
cc61c64b XL |
450 | (self.succ, self.unwind) |
451 | } else { | |
452 | self.open_drop_for_adt_contents(adt, substs) | |
453 | }; | |
454 | ||
455 | if adt.has_dtor(self.tcx()) { | |
456 | self.destructor_call_block(contents_drop) | |
457 | } else { | |
458 | contents_drop.0 | |
459 | } | |
460 | } | |
461 | ||
dfeec247 XL |
462 | fn open_drop_for_adt_contents( |
463 | &mut self, | |
5e7ed085 | 464 | adt: ty::AdtDef<'tcx>, |
dfeec247 XL |
465 | substs: SubstsRef<'tcx>, |
466 | ) -> (BasicBlock, Unwind) { | |
7cac9316 | 467 | let (succ, unwind) = self.drop_ladder_bottom(); |
ff7c6d11 | 468 | if !adt.is_enum() { |
7cac9316 | 469 | let fields = self.move_paths_for_fields( |
ff7c6d11 | 470 | self.place, |
7cac9316 | 471 | self.path, |
5e7ed085 | 472 | &adt.variant(VariantIdx::new(0)), |
dfeec247 | 473 | substs, |
7cac9316 XL |
474 | ); |
475 | self.drop_ladder(fields, succ, unwind) | |
476 | } else { | |
477 | self.open_drop_for_multivariant(adt, substs, succ, unwind) | |
478 | } | |
479 | } | |
480 | ||
dfeec247 XL |
481 | fn open_drop_for_multivariant( |
482 | &mut self, | |
5e7ed085 | 483 | adt: ty::AdtDef<'tcx>, |
dfeec247 XL |
484 | substs: SubstsRef<'tcx>, |
485 | succ: BasicBlock, | |
486 | unwind: Unwind, | |
487 | ) -> (BasicBlock, Unwind) { | |
5e7ed085 FG |
488 | let mut values = Vec::with_capacity(adt.variants().len()); |
489 | let mut normal_blocks = Vec::with_capacity(adt.variants().len()); | |
dfeec247 | 490 | let mut unwind_blocks = |
5e7ed085 | 491 | if unwind.is_cleanup() { None } else { Some(Vec::with_capacity(adt.variants().len())) }; |
cc61c64b | 492 | |
74b04a01 | 493 | let mut have_otherwise_with_drop_glue = false; |
7cac9316 | 494 | let mut have_otherwise = false; |
e74abb32 | 495 | let tcx = self.tcx(); |
7cac9316 | 496 | |
e74abb32 | 497 | for (variant_index, discr) in adt.discriminants(tcx) { |
5e7ed085 | 498 | let variant = &adt.variant(variant_index); |
dfeec247 | 499 | let subpath = self.elaborator.downcast_subpath(self.path, variant_index); |
74b04a01 | 500 | |
7cac9316 | 501 | if let Some(variant_path) = subpath { |
e74abb32 | 502 | let base_place = tcx.mk_place_elem( |
ba9703b0 | 503 | self.place, |
5099ac24 | 504 | ProjectionElem::Downcast(Some(variant.name), variant_index), |
dfeec247 | 505 | ); |
ba9703b0 | 506 | let fields = self.move_paths_for_fields(base_place, variant_path, &variant, substs); |
0531ce1d | 507 | values.push(discr.val); |
7cac9316 XL |
508 | if let Unwind::To(unwind) = unwind { |
509 | // We can't use the half-ladder from the original | |
510 | // drop ladder, because this breaks the | |
511 | // "funclet can't have 2 successor funclets" | |
512 | // requirement from MSVC: | |
513 | // | |
514 | // switch unwind-switch | |
515 | // / \ / \ | |
516 | // v1.0 v2.0 v2.0-unwind v1.0-unwind | |
517 | // | | / | | |
518 | // v1.1-unwind v2.1-unwind | | |
519 | // ^ | | |
520 | // \-------------------------------/ | |
521 | // | |
522 | // Create a duplicate half-ladder to avoid that. We | |
523 | // could technically only do this on MSVC, but I | |
524 | // I want to minimize the divergence between MSVC | |
525 | // and non-MSVC. | |
526 | ||
527 | let unwind_blocks = unwind_blocks.as_mut().unwrap(); | |
528 | let unwind_ladder = vec![Unwind::InCleanup; fields.len() + 1]; | |
dfeec247 | 529 | let halfladder = self.drop_halfladder(&unwind_ladder, unwind, &fields); |
7cac9316 | 530 | unwind_blocks.push(halfladder.last().cloned().unwrap()); |
cc61c64b | 531 | } |
7cac9316 XL |
532 | let (normal, _) = self.drop_ladder(fields, succ, unwind); |
533 | normal_blocks.push(normal); | |
534 | } else { | |
535 | have_otherwise = true; | |
74b04a01 XL |
536 | |
537 | let param_env = self.elaborator.param_env(); | |
538 | let have_field_with_drop_glue = variant | |
539 | .fields | |
540 | .iter() | |
541 | .any(|field| field.ty(tcx, substs).needs_drop(tcx, param_env)); | |
542 | if have_field_with_drop_glue { | |
543 | have_otherwise_with_drop_glue = true; | |
544 | } | |
7cac9316 XL |
545 | } |
546 | } | |
cc61c64b | 547 | |
74b04a01 XL |
548 | if !have_otherwise { |
549 | values.pop(); | |
550 | } else if !have_otherwise_with_drop_glue { | |
551 | normal_blocks.push(self.goto_block(succ, unwind)); | |
552 | if let Unwind::To(unwind) = unwind { | |
553 | unwind_blocks.as_mut().unwrap().push(self.goto_block(unwind, Unwind::InCleanup)); | |
554 | } | |
555 | } else { | |
7cac9316 XL |
556 | normal_blocks.push(self.drop_block(succ, unwind)); |
557 | if let Unwind::To(unwind) = unwind { | |
dfeec247 | 558 | unwind_blocks.as_mut().unwrap().push(self.drop_block(unwind, Unwind::InCleanup)); |
cc61c64b XL |
559 | } |
560 | } | |
7cac9316 | 561 | |
dfeec247 XL |
562 | ( |
563 | self.adt_switch_block(adt, normal_blocks, &values, succ, unwind), | |
564 | unwind.map(|unwind| { | |
565 | self.adt_switch_block( | |
566 | adt, | |
567 | unwind_blocks.unwrap(), | |
568 | &values, | |
569 | unwind, | |
570 | Unwind::InCleanup, | |
571 | ) | |
572 | }), | |
573 | ) | |
574 | } | |
575 | ||
576 | fn adt_switch_block( | |
577 | &mut self, | |
5e7ed085 | 578 | adt: ty::AdtDef<'tcx>, |
dfeec247 XL |
579 | blocks: Vec<BasicBlock>, |
580 | values: &[u128], | |
581 | succ: BasicBlock, | |
582 | unwind: Unwind, | |
583 | ) -> BasicBlock { | |
cc61c64b XL |
584 | // If there are multiple variants, then if something |
585 | // is present within the enum the discriminant, tracked | |
586 | // by the rest path, must be initialized. | |
587 | // | |
588 | // Additionally, we do not want to switch on the | |
589 | // discriminant after it is free-ed, because that | |
590 | // way lies only trouble. | |
5e7ed085 | 591 | let discr_ty = adt.repr().discr_type().to_ty(self.tcx()); |
dc9dc135 | 592 | let discr = Place::from(self.new_temp(discr_ty)); |
ba9703b0 | 593 | let discr_rv = Rvalue::Discriminant(self.place); |
7cac9316 | 594 | let switch_block = BasicBlockData { |
ba9703b0 | 595 | statements: vec![self.assign(discr, discr_rv)], |
cc61c64b XL |
596 | terminator: Some(Terminator { |
597 | source_info: self.source_info, | |
598 | kind: TerminatorKind::SwitchInt { | |
ff7c6d11 | 599 | discr: Operand::Move(discr), |
cc61c64b | 600 | switch_ty: discr_ty, |
29967ef6 XL |
601 | targets: SwitchTargets::new( |
602 | values.iter().copied().zip(blocks.iter().copied()), | |
603 | *blocks.last().unwrap(), | |
604 | ), | |
dfeec247 | 605 | }, |
cc61c64b | 606 | }), |
7cac9316 XL |
607 | is_cleanup: unwind.is_cleanup(), |
608 | }; | |
609 | let switch_block = self.elaborator.patch().new_block(switch_block); | |
610 | self.drop_flag_test_block(switch_block, succ, unwind) | |
cc61c64b XL |
611 | } |
612 | ||
dc9dc135 | 613 | fn destructor_call_block(&mut self, (succ, unwind): (BasicBlock, Unwind)) -> BasicBlock { |
cc61c64b XL |
614 | debug!("destructor_call_block({:?}, {:?})", self, succ); |
615 | let tcx = self.tcx(); | |
3dfed10e | 616 | let drop_trait = tcx.require_lang_item(LangItem::Drop, None); |
3c0e092e | 617 | let drop_fn = tcx.associated_item_def_ids(drop_trait)[0]; |
ff7c6d11 | 618 | let ty = self.place_ty(self.place); |
94b46f34 | 619 | let substs = tcx.mk_substs_trait(ty, &[]); |
cc61c64b | 620 | |
dfeec247 XL |
621 | let ref_ty = |
622 | tcx.mk_ref(tcx.lifetimes.re_erased, ty::TypeAndMut { ty, mutbl: hir::Mutability::Mut }); | |
ff7c6d11 | 623 | let ref_place = self.new_temp(ref_ty); |
dc9dc135 | 624 | let unit_temp = Place::from(self.new_temp(tcx.mk_unit())); |
cc61c64b | 625 | |
7cac9316 XL |
626 | let result = BasicBlockData { |
627 | statements: vec![self.assign( | |
ba9703b0 | 628 | Place::from(ref_place), |
dfeec247 XL |
629 | Rvalue::Ref( |
630 | tcx.lifetimes.re_erased, | |
631 | BorrowKind::Mut { allow_two_phase_borrow: false }, | |
ba9703b0 | 632 | self.place, |
dfeec247 | 633 | ), |
7cac9316 | 634 | )], |
cc61c64b XL |
635 | terminator: Some(Terminator { |
636 | kind: TerminatorKind::Call { | |
3c0e092e | 637 | func: Operand::function_handle(tcx, drop_fn, substs, self.source_info.span), |
dc9dc135 | 638 | args: vec![Operand::Move(Place::from(ref_place))], |
923072b8 FG |
639 | destination: unit_temp, |
640 | target: Some(succ), | |
7cac9316 | 641 | cleanup: unwind.into_option(), |
0bf4aa26 | 642 | from_hir_call: true, |
f035d41b | 643 | fn_span: self.source_info.span, |
cc61c64b | 644 | }, |
0bf4aa26 | 645 | source_info: self.source_info, |
cc61c64b | 646 | }), |
7cac9316 XL |
647 | is_cleanup: unwind.is_cleanup(), |
648 | }; | |
649 | self.elaborator.patch().new_block(result) | |
650 | } | |
651 | ||
48663c56 | 652 | /// Create a loop that drops an array: |
7cac9316 | 653 | /// |
48663c56 | 654 | /// ```text |
7cac9316 XL |
655 | /// loop-block: |
656 | /// can_go = cur == length_or_end | |
657 | /// if can_go then succ else drop-block | |
658 | /// drop-block: | |
659 | /// if ptr_based { | |
dfeec247 | 660 | /// ptr = cur |
7cac9316 XL |
661 | /// cur = cur.offset(1) |
662 | /// } else { | |
dfeec247 | 663 | /// ptr = &raw mut P[cur] |
7cac9316 XL |
664 | /// cur = cur + 1 |
665 | /// } | |
666 | /// drop(ptr) | |
48663c56 XL |
667 | /// ``` |
668 | fn drop_loop( | |
669 | &mut self, | |
670 | succ: BasicBlock, | |
671 | cur: Local, | |
ba9703b0 | 672 | length_or_end: Place<'tcx>, |
48663c56 XL |
673 | ety: Ty<'tcx>, |
674 | unwind: Unwind, | |
675 | ptr_based: bool, | |
676 | ) -> BasicBlock { | |
dfeec247 XL |
677 | let copy = |place: Place<'tcx>| Operand::Copy(place); |
678 | let move_ = |place: Place<'tcx>| Operand::Move(place); | |
7cac9316 XL |
679 | let tcx = self.tcx(); |
680 | ||
dfeec247 | 681 | let ptr_ty = tcx.mk_ptr(ty::TypeAndMut { ty: ety, mutbl: hir::Mutability::Mut }); |
ba9703b0 | 682 | let ptr = Place::from(self.new_temp(ptr_ty)); |
dfeec247 | 683 | let can_go = Place::from(self.new_temp(tcx.types.bool)); |
7cac9316 XL |
684 | |
685 | let one = self.constant_usize(1); | |
686 | let (ptr_next, cur_next) = if ptr_based { | |
6a06907d XL |
687 | ( |
688 | Rvalue::Use(copy(cur.into())), | |
94222f64 | 689 | Rvalue::BinaryOp(BinOp::Offset, Box::new((move_(cur.into()), one))), |
6a06907d | 690 | ) |
7cac9316 | 691 | } else { |
dfeec247 | 692 | ( |
ba9703b0 | 693 | Rvalue::AddressOf(Mutability::Mut, tcx.mk_place_index(self.place, cur)), |
94222f64 | 694 | Rvalue::BinaryOp(BinOp::Add, Box::new((move_(cur.into()), one))), |
dfeec247 | 695 | ) |
7cac9316 XL |
696 | }; |
697 | ||
698 | let drop_block = BasicBlockData { | |
ba9703b0 | 699 | statements: vec![self.assign(ptr, ptr_next), self.assign(Place::from(cur), cur_next)], |
7cac9316 XL |
700 | is_cleanup: unwind.is_cleanup(), |
701 | terminator: Some(Terminator { | |
702 | source_info: self.source_info, | |
703 | // this gets overwritten by drop elaboration. | |
704 | kind: TerminatorKind::Unreachable, | |
dfeec247 | 705 | }), |
7cac9316 XL |
706 | }; |
707 | let drop_block = self.elaborator.patch().new_block(drop_block); | |
708 | ||
709 | let loop_block = BasicBlockData { | |
dfeec247 | 710 | statements: vec![self.assign( |
ba9703b0 | 711 | can_go, |
94222f64 XL |
712 | Rvalue::BinaryOp( |
713 | BinOp::Eq, | |
714 | Box::new((copy(Place::from(cur)), copy(length_or_end))), | |
715 | ), | |
dfeec247 | 716 | )], |
7cac9316 XL |
717 | is_cleanup: unwind.is_cleanup(), |
718 | terminator: Some(Terminator { | |
719 | source_info: self.source_info, | |
dfeec247 XL |
720 | kind: TerminatorKind::if_(tcx, move_(can_go), succ, drop_block), |
721 | }), | |
7cac9316 XL |
722 | }; |
723 | let loop_block = self.elaborator.patch().new_block(loop_block); | |
724 | ||
dfeec247 XL |
725 | self.elaborator.patch().patch_terminator( |
726 | drop_block, | |
727 | TerminatorKind::Drop { | |
f035d41b | 728 | place: tcx.mk_place_deref(ptr), |
dfeec247 XL |
729 | target: loop_block, |
730 | unwind: unwind.into_option(), | |
731 | }, | |
732 | ); | |
7cac9316 XL |
733 | |
734 | loop_block | |
735 | } | |
736 | ||
ff7c6d11 XL |
737 | fn open_drop_for_array(&mut self, ety: Ty<'tcx>, opt_size: Option<u64>) -> BasicBlock { |
738 | debug!("open_drop_for_array({:?}, {:?})", ety, opt_size); | |
7cac9316 XL |
739 | |
740 | // if size_of::<ety>() == 0 { | |
741 | // index_based_loop | |
742 | // } else { | |
743 | // ptr_based_loop | |
744 | // } | |
745 | ||
e74abb32 XL |
746 | let tcx = self.tcx(); |
747 | ||
ff7c6d11 | 748 | if let Some(size) = opt_size { |
e74abb32 XL |
749 | let fields: Vec<(Place<'tcx>, Option<D::Path>)> = (0..size) |
750 | .map(|i| { | |
751 | ( | |
752 | tcx.mk_place_elem( | |
ba9703b0 | 753 | self.place, |
e74abb32 XL |
754 | ProjectionElem::ConstantIndex { |
755 | offset: i, | |
756 | min_length: size, | |
757 | from_end: false, | |
758 | }, | |
759 | ), | |
760 | self.elaborator.array_subpath(self.path, i, size), | |
761 | ) | |
762 | }) | |
763 | .collect(); | |
ff7c6d11 | 764 | |
dfeec247 | 765 | if fields.iter().any(|(_, path)| path.is_some()) { |
ff7c6d11 | 766 | let (succ, unwind) = self.drop_ladder_bottom(); |
dfeec247 | 767 | return self.drop_ladder(fields, succ, unwind).0; |
ff7c6d11 XL |
768 | } |
769 | } | |
7cac9316 | 770 | |
ba9703b0 XL |
771 | let move_ = |place: Place<'tcx>| Operand::Move(place); |
772 | let elem_size = Place::from(self.new_temp(tcx.types.usize)); | |
773 | let len = Place::from(self.new_temp(tcx.types.usize)); | |
48663c56 | 774 | |
7cac9316 XL |
775 | let base_block = BasicBlockData { |
776 | statements: vec![ | |
48663c56 | 777 | self.assign(elem_size, Rvalue::NullaryOp(NullOp::SizeOf, ety)), |
ba9703b0 | 778 | self.assign(len, Rvalue::Len(self.place)), |
7cac9316 XL |
779 | ], |
780 | is_cleanup: self.unwind.is_cleanup(), | |
781 | terminator: Some(Terminator { | |
782 | source_info: self.source_info, | |
48663c56 XL |
783 | kind: TerminatorKind::SwitchInt { |
784 | discr: move_(elem_size), | |
785 | switch_ty: tcx.types.usize, | |
29967ef6 XL |
786 | targets: SwitchTargets::static_if( |
787 | 0, | |
ba9703b0 XL |
788 | self.drop_loop_pair(ety, false, len), |
789 | self.drop_loop_pair(ety, true, len), | |
29967ef6 | 790 | ), |
48663c56 | 791 | }, |
dfeec247 | 792 | }), |
7cac9316 XL |
793 | }; |
794 | self.elaborator.patch().new_block(base_block) | |
795 | } | |
796 | ||
74b04a01 | 797 | /// Creates a pair of drop-loops of `place`, which drops its contents, even |
48663c56 XL |
798 | /// in the case of 1 panic. If `ptr_based`, creates a pointer loop, |
799 | /// otherwise create an index loop. | |
800 | fn drop_loop_pair( | |
801 | &mut self, | |
802 | ety: Ty<'tcx>, | |
803 | ptr_based: bool, | |
804 | length: Place<'tcx>, | |
805 | ) -> BasicBlock { | |
7cac9316 XL |
806 | debug!("drop_loop_pair({:?}, {:?})", ety, ptr_based); |
807 | let tcx = self.tcx(); | |
dfeec247 | 808 | let iter_ty = if ptr_based { tcx.mk_mut_ptr(ety) } else { tcx.types.usize }; |
7cac9316 | 809 | |
ea8adc8c | 810 | let cur = self.new_temp(iter_ty); |
dfeec247 | 811 | let length_or_end = if ptr_based { Place::from(self.new_temp(iter_ty)) } else { length }; |
7cac9316 XL |
812 | |
813 | let unwind = self.unwind.map(|unwind| { | |
ba9703b0 | 814 | self.drop_loop(unwind, cur, length_or_end, ety, Unwind::InCleanup, ptr_based) |
7cac9316 XL |
815 | }); |
816 | ||
ba9703b0 | 817 | let loop_block = self.drop_loop(self.succ, cur, length_or_end, ety, unwind, ptr_based); |
7cac9316 | 818 | |
dc9dc135 | 819 | let cur = Place::from(cur); |
48663c56 | 820 | let drop_block_stmts = if ptr_based { |
ff7c6d11 | 821 | let tmp_ty = tcx.mk_mut_ptr(self.place_ty(self.place)); |
dc9dc135 | 822 | let tmp = Place::from(self.new_temp(tmp_ty)); |
dfeec247 | 823 | // tmp = &raw mut P; |
7cac9316 XL |
824 | // cur = tmp as *mut T; |
825 | // end = Offset(cur, len); | |
48663c56 | 826 | vec![ |
ba9703b0 XL |
827 | self.assign(tmp, Rvalue::AddressOf(Mutability::Mut, self.place)), |
828 | self.assign(cur, Rvalue::Cast(CastKind::Misc, Operand::Move(tmp), iter_ty)), | |
48663c56 | 829 | self.assign( |
ba9703b0 | 830 | length_or_end, |
6a06907d XL |
831 | Rvalue::BinaryOp( |
832 | BinOp::Offset, | |
94222f64 | 833 | Box::new((Operand::Copy(cur), Operand::Move(length))), |
6a06907d | 834 | ), |
dfeec247 | 835 | ), |
48663c56 | 836 | ] |
7cac9316 | 837 | } else { |
48663c56 XL |
838 | // cur = 0 (length already pushed) |
839 | let zero = self.constant_usize(0); | |
ba9703b0 | 840 | vec![self.assign(cur, Rvalue::Use(zero))] |
48663c56 | 841 | }; |
7cac9316 XL |
842 | let drop_block = self.elaborator.patch().new_block(BasicBlockData { |
843 | statements: drop_block_stmts, | |
844 | is_cleanup: unwind.is_cleanup(), | |
845 | terminator: Some(Terminator { | |
846 | source_info: self.source_info, | |
dfeec247 XL |
847 | kind: TerminatorKind::Goto { target: loop_block }, |
848 | }), | |
7cac9316 XL |
849 | }); |
850 | ||
851 | // FIXME(#34708): handle partially-dropped array/slice elements. | |
852 | let reset_block = self.drop_flag_reset_block(DropFlagMode::Deep, drop_block, unwind); | |
48663c56 | 853 | self.drop_flag_test_block(reset_block, self.succ, unwind) |
cc61c64b XL |
854 | } |
855 | ||
856 | /// The slow-path - create an "open", elaborated drop for a type | |
857 | /// which is moved-out-of only partially, and patch `bb` to a jump | |
858 | /// to it. This must not be called on ADTs with a destructor, | |
859 | /// as these can't be moved-out-of, except for `Box<T>`, which is | |
860 | /// special-cased. | |
861 | /// | |
862 | /// This creates a "drop ladder" that drops the needed fields of the | |
863 | /// ADT, both in the success case or if one of the destructors fail. | |
dc9dc135 | 864 | fn open_drop(&mut self) -> BasicBlock { |
ff7c6d11 | 865 | let ty = self.place_ty(self.place); |
1b1a35ee | 866 | match ty.kind() { |
ba9703b0 XL |
867 | ty::Closure(_, substs) => { |
868 | let tys: Vec<_> = substs.as_closure().upvar_tys().collect(); | |
94b46f34 XL |
869 | self.open_drop_for_tuple(&tys) |
870 | } | |
ea8adc8c XL |
871 | // Note that `elaborate_drops` only drops the upvars of a generator, |
872 | // and this is ok because `open_drop` here can only be reached | |
873 | // within that own generator's resume function. | |
874 | // This should only happen for the self argument on the resume function. | |
5e7ed085 | 875 | // It effectively only contains upvars until the generator transformation runs. |
dc9dc135 | 876 | // See librustc_body/transform/generator.rs for more details. |
ba9703b0 XL |
877 | ty::Generator(_, substs, _) => { |
878 | let tys: Vec<_> = substs.as_generator().upvar_tys().collect(); | |
cc61c64b XL |
879 | self.open_drop_for_tuple(&tys) |
880 | } | |
5e7ed085 | 881 | ty::Tuple(fields) => self.open_drop_for_tuple(fields), |
b7449926 | 882 | ty::Adt(def, substs) => { |
83c7162d | 883 | if def.is_box() { |
5e7ed085 | 884 | self.open_drop_for_box(*def, substs) |
83c7162d | 885 | } else { |
5e7ed085 | 886 | self.open_drop_for_adt(*def, substs) |
83c7162d | 887 | } |
cc61c64b | 888 | } |
29967ef6 | 889 | ty::Dynamic(..) => self.complete_drop(self.succ, self.unwind), |
b7449926 | 890 | ty::Array(ety, size) => { |
416331ca | 891 | let size = size.try_eval_usize(self.tcx(), self.elaborator.param_env()); |
5099ac24 | 892 | self.open_drop_for_array(*ety, size) |
dfeec247 | 893 | } |
5099ac24 | 894 | ty::Slice(ety) => self.open_drop_for_array(*ety, None), |
ff7c6d11 | 895 | |
dfeec247 | 896 | _ => bug!("open drop from non-ADT `{:?}`", ty), |
cc61c64b XL |
897 | } |
898 | } | |
899 | ||
29967ef6 XL |
900 | fn complete_drop(&mut self, succ: BasicBlock, unwind: Unwind) -> BasicBlock { |
901 | debug!("complete_drop(succ={:?}, unwind={:?})", succ, unwind); | |
cc61c64b | 902 | |
7cac9316 | 903 | let drop_block = self.drop_block(succ, unwind); |
7cac9316 XL |
904 | |
905 | self.drop_flag_test_block(drop_block, succ, unwind) | |
906 | } | |
cc61c64b | 907 | |
f9f354fc XL |
908 | /// Creates a block that resets the drop flag. If `mode` is deep, all children drop flags will |
909 | /// also be cleared. | |
dfeec247 XL |
910 | fn drop_flag_reset_block( |
911 | &mut self, | |
912 | mode: DropFlagMode, | |
913 | succ: BasicBlock, | |
914 | unwind: Unwind, | |
915 | ) -> BasicBlock { | |
7cac9316 XL |
916 | debug!("drop_flag_reset_block({:?},{:?})", self, mode); |
917 | ||
29967ef6 XL |
918 | if unwind.is_cleanup() { |
919 | // The drop flag isn't read again on the unwind path, so don't | |
920 | // bother setting it. | |
921 | return succ; | |
922 | } | |
7cac9316 | 923 | let block = self.new_block(unwind, TerminatorKind::Goto { target: succ }); |
74b04a01 | 924 | let block_start = Location { block, statement_index: 0 }; |
7cac9316 XL |
925 | self.elaborator.clear_drop_flag(block_start, self.path, mode); |
926 | block | |
cc61c64b XL |
927 | } |
928 | ||
dc9dc135 | 929 | fn elaborated_drop_block(&mut self) -> BasicBlock { |
cc61c64b | 930 | debug!("elaborated_drop_block({:?})", self); |
f9f354fc | 931 | let blk = self.drop_block(self.succ, self.unwind); |
cc61c64b XL |
932 | self.elaborate_drop(blk); |
933 | blk | |
934 | } | |
935 | ||
f9f354fc XL |
936 | /// Creates a block that frees the backing memory of a `Box` if its drop is required (either |
937 | /// statically or by checking its drop flag). | |
938 | /// | |
939 | /// The contained value will not be dropped. | |
dc9dc135 | 940 | fn box_free_block( |
cc61c64b | 941 | &mut self, |
5e7ed085 | 942 | adt: ty::AdtDef<'tcx>, |
532ac7d7 | 943 | substs: SubstsRef<'tcx>, |
cc61c64b | 944 | target: BasicBlock, |
7cac9316 | 945 | unwind: Unwind, |
cc61c64b | 946 | ) -> BasicBlock { |
83c7162d | 947 | let block = self.unelaborated_free_block(adt, substs, target, unwind); |
7cac9316 | 948 | self.drop_flag_test_block(block, target, unwind) |
cc61c64b XL |
949 | } |
950 | ||
f9f354fc XL |
951 | /// Creates a block that frees the backing memory of a `Box` (without dropping the contained |
952 | /// value). | |
dc9dc135 | 953 | fn unelaborated_free_block( |
cc61c64b | 954 | &mut self, |
5e7ed085 | 955 | adt: ty::AdtDef<'tcx>, |
532ac7d7 | 956 | substs: SubstsRef<'tcx>, |
cc61c64b | 957 | target: BasicBlock, |
dc9dc135 | 958 | unwind: Unwind, |
cc61c64b XL |
959 | ) -> BasicBlock { |
960 | let tcx = self.tcx(); | |
dc9dc135 | 961 | let unit_temp = Place::from(self.new_temp(tcx.mk_unit())); |
3dfed10e | 962 | let free_func = tcx.require_lang_item(LangItem::BoxFree, Some(self.source_info.span)); |
5e7ed085 FG |
963 | let args = adt |
964 | .variant(VariantIdx::new(0)) | |
dfeec247 XL |
965 | .fields |
966 | .iter() | |
967 | .enumerate() | |
968 | .map(|(i, f)| { | |
969 | let field = Field::new(i); | |
970 | let field_ty = f.ty(tcx, substs); | |
ba9703b0 | 971 | Operand::Move(tcx.mk_place_field(self.place, field, field_ty)) |
dfeec247 XL |
972 | }) |
973 | .collect(); | |
cc61c64b XL |
974 | |
975 | let call = TerminatorKind::Call { | |
976 | func: Operand::function_handle(tcx, free_func, substs, self.source_info.span), | |
74b04a01 | 977 | args, |
923072b8 FG |
978 | destination: unit_temp, |
979 | target: Some(target), | |
0bf4aa26 XL |
980 | cleanup: None, |
981 | from_hir_call: false, | |
f035d41b | 982 | fn_span: self.source_info.span, |
0531ce1d | 983 | }; // FIXME(#43234) |
7cac9316 | 984 | let free_block = self.new_block(unwind, call); |
cc61c64b XL |
985 | |
986 | let block_start = Location { block: free_block, statement_index: 0 }; | |
987 | self.elaborator.clear_drop_flag(block_start, self.path, DropFlagMode::Shallow); | |
988 | free_block | |
989 | } | |
990 | ||
dc9dc135 | 991 | fn drop_block(&mut self, target: BasicBlock, unwind: Unwind) -> BasicBlock { |
dfeec247 | 992 | let block = |
f035d41b | 993 | TerminatorKind::Drop { place: self.place, target, unwind: unwind.into_option() }; |
7cac9316 | 994 | self.new_block(unwind, block) |
cc61c64b XL |
995 | } |
996 | ||
74b04a01 XL |
997 | fn goto_block(&mut self, target: BasicBlock, unwind: Unwind) -> BasicBlock { |
998 | let block = TerminatorKind::Goto { target }; | |
999 | self.new_block(unwind, block) | |
1000 | } | |
1001 | ||
f9f354fc XL |
1002 | /// Returns the block to jump to in order to test the drop flag and execute the drop. |
1003 | /// | |
1004 | /// Depending on the required `DropStyle`, this might be a generated block with an `if` | |
1005 | /// terminator (for dynamic/open drops), or it might be `on_set` or `on_unset` itself, in case | |
1006 | /// the drop can be statically determined. | |
dfeec247 XL |
1007 | fn drop_flag_test_block( |
1008 | &mut self, | |
1009 | on_set: BasicBlock, | |
1010 | on_unset: BasicBlock, | |
1011 | unwind: Unwind, | |
1012 | ) -> BasicBlock { | |
cc61c64b | 1013 | let style = self.elaborator.drop_style(self.path, DropFlagMode::Shallow); |
dfeec247 XL |
1014 | debug!( |
1015 | "drop_flag_test_block({:?},{:?},{:?},{:?}) - {:?}", | |
1016 | self, on_set, on_unset, unwind, style | |
1017 | ); | |
cc61c64b XL |
1018 | |
1019 | match style { | |
1020 | DropStyle::Dead => on_unset, | |
1021 | DropStyle::Static => on_set, | |
1022 | DropStyle::Conditional | DropStyle::Open => { | |
1023 | let flag = self.elaborator.get_drop_flag(self.path).unwrap(); | |
1024 | let term = TerminatorKind::if_(self.tcx(), flag, on_set, on_unset); | |
7cac9316 | 1025 | self.new_block(unwind, term) |
cc61c64b XL |
1026 | } |
1027 | } | |
1028 | } | |
1029 | ||
dc9dc135 | 1030 | fn new_block(&mut self, unwind: Unwind, k: TerminatorKind<'tcx>) -> BasicBlock { |
cc61c64b XL |
1031 | self.elaborator.patch().new_block(BasicBlockData { |
1032 | statements: vec![], | |
dfeec247 XL |
1033 | terminator: Some(Terminator { source_info: self.source_info, kind: k }), |
1034 | is_cleanup: unwind.is_cleanup(), | |
cc61c64b XL |
1035 | }) |
1036 | } | |
1037 | ||
1038 | fn new_temp(&mut self, ty: Ty<'tcx>) -> Local { | |
1039 | self.elaborator.patch().new_temp(ty, self.source_info.span) | |
1040 | } | |
1041 | ||
7cac9316 | 1042 | fn constant_usize(&self, val: u16) -> Operand<'tcx> { |
94222f64 | 1043 | Operand::Constant(Box::new(Constant { |
7cac9316 | 1044 | span: self.source_info.span, |
b7449926 | 1045 | user_ty: None, |
923072b8 | 1046 | literal: ConstantKind::from_usize(self.tcx(), val.into()), |
94222f64 | 1047 | })) |
7cac9316 XL |
1048 | } |
1049 | ||
ba9703b0 | 1050 | fn assign(&self, lhs: Place<'tcx>, rhs: Rvalue<'tcx>) -> Statement<'tcx> { |
94222f64 XL |
1051 | Statement { |
1052 | source_info: self.source_info, | |
1053 | kind: StatementKind::Assign(Box::new((lhs, rhs))), | |
1054 | } | |
7cac9316 | 1055 | } |
cc61c64b | 1056 | } |