]>
Commit | Line | Data |
---|---|---|
dfeec247 | 1 | use rustc_hir as hir; |
3dfed10e | 2 | use rustc_hir::lang_items::LangItem; |
49aad941 | 3 | use rustc_index::Idx; |
c295e0f8 | 4 | use rustc_middle::mir::patch::MirPatch; |
ba9703b0 XL |
5 | use rustc_middle::mir::*; |
6 | use rustc_middle::traits::Reveal; | |
ba9703b0 | 7 | use rustc_middle::ty::util::IntTypeExt; |
add651ee | 8 | use rustc_middle::ty::GenericArgsRef; |
ba9703b0 | 9 | use rustc_middle::ty::{self, Ty, TyCtxt}; |
353b0b11 | 10 | use rustc_target::abi::{FieldIdx, VariantIdx, FIRST_VARIANT}; |
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 | ||
353b0b11 | 80 | fn into_action(self) -> UnwindAction { |
7cac9316 | 81 | match self { |
353b0b11 | 82 | Unwind::To(bb) => UnwindAction::Cleanup(bb), |
781aab86 | 83 | Unwind::InCleanup => UnwindAction::Terminate(UnwindTerminateReason::InCleanup), |
7cac9316 XL |
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. | |
353b0b11 | 132 | fn field_subpath(&self, path: Self::Path, field: FieldIdx) -> 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 | { |
781aab86 | 197 | #[instrument(level = "trace", skip(self), ret)] |
ba9703b0 | 198 | fn place_ty(&self, place: Place<'tcx>) -> Ty<'tcx> { |
dc9dc135 | 199 | place.ty(self.elaborator.body(), self.tcx()).ty |
cc61c64b XL |
200 | } |
201 | ||
dc9dc135 | 202 | fn tcx(&self) -> TyCtxt<'tcx> { |
cc61c64b XL |
203 | self.elaborator.tcx() |
204 | } | |
205 | ||
206 | /// This elaborates a single drop instruction, located at `bb`, and | |
207 | /// patches over it. | |
208 | /// | |
209 | /// The elaborated drop checks the drop flags to only drop what | |
210 | /// is initialized. | |
211 | /// | |
212 | /// In addition, the relevant drop flags also need to be cleared | |
213 | /// to avoid double-drops. However, in the middle of a complex | |
214 | /// drop, one must avoid clearing some of the flags before they | |
215 | /// are read, as that would cause a memory leak. | |
216 | /// | |
217 | /// In particular, when dropping an ADT, multiple fields may be | |
218 | /// joined together under the `rest` subpath. They are all controlled | |
219 | /// by the primary drop flag, but only the last rest-field dropped | |
220 | /// should clear it (and it must also not clear anything else). | |
9fa01778 XL |
221 | // |
222 | // FIXME: I think we should just control the flags externally, | |
223 | // and then we do not need this machinery. | |
781aab86 | 224 | #[instrument(level = "debug")] |
dc9dc135 | 225 | pub fn elaborate_drop(&mut self, bb: BasicBlock) { |
781aab86 | 226 | match self.elaborator.drop_style(self.path, DropFlagMode::Deep) { |
cc61c64b | 227 | DropStyle::Dead => { |
dfeec247 XL |
228 | self.elaborator |
229 | .patch() | |
230 | .patch_terminator(bb, TerminatorKind::Goto { target: self.succ }); | |
cc61c64b XL |
231 | } |
232 | DropStyle::Static => { | |
dfeec247 XL |
233 | self.elaborator.patch().patch_terminator( |
234 | bb, | |
235 | TerminatorKind::Drop { | |
f035d41b | 236 | place: self.place, |
dfeec247 | 237 | target: self.succ, |
353b0b11 | 238 | unwind: self.unwind.into_action(), |
49aad941 | 239 | replace: false, |
dfeec247 XL |
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, | |
add651ee | 265 | args: GenericArgsRef<'tcx>, |
dfeec247 XL |
266 | ) -> Vec<(Place<'tcx>, Option<D::Path>)> { |
267 | variant | |
268 | .fields | |
269 | .iter() | |
270 | .enumerate() | |
271 | .map(|(i, f)| { | |
353b0b11 | 272 | let field = FieldIdx::new(i); |
dfeec247 XL |
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 | 277 | let field_ty = |
add651ee | 278 | tcx.normalize_erasing_regions(self.elaborator.param_env(), f.ty(tcx, args)); |
49aad941 | 279 | |
ba9703b0 | 280 | (tcx.mk_place_field(base_place, field, field_ty), subpath) |
dfeec247 XL |
281 | }) |
282 | .collect() | |
cc61c64b XL |
283 | } |
284 | ||
dfeec247 XL |
285 | fn drop_subpath( |
286 | &mut self, | |
ba9703b0 | 287 | place: Place<'tcx>, |
dfeec247 XL |
288 | path: Option<D::Path>, |
289 | succ: BasicBlock, | |
290 | unwind: Unwind, | |
291 | ) -> BasicBlock { | |
cc61c64b | 292 | if let Some(path) = path { |
ff7c6d11 | 293 | debug!("drop_subpath: for std field {:?}", place); |
cc61c64b XL |
294 | |
295 | DropCtxt { | |
296 | elaborator: self.elaborator, | |
297 | source_info: self.source_info, | |
dfeec247 XL |
298 | path, |
299 | place, | |
300 | succ, | |
301 | unwind, | |
302 | } | |
303 | .elaborated_drop_block() | |
cc61c64b | 304 | } else { |
ff7c6d11 | 305 | debug!("drop_subpath: for rest field {:?}", place); |
cc61c64b XL |
306 | |
307 | DropCtxt { | |
308 | elaborator: self.elaborator, | |
309 | source_info: self.source_info, | |
dfeec247 XL |
310 | place, |
311 | succ, | |
312 | unwind, | |
cc61c64b XL |
313 | // Using `self.path` here to condition the drop on |
314 | // our own drop flag. | |
dfeec247 XL |
315 | path: self.path, |
316 | } | |
29967ef6 | 317 | .complete_drop(succ, unwind) |
cc61c64b XL |
318 | } |
319 | } | |
320 | ||
9fa01778 | 321 | /// Creates one-half of the drop ladder for a list of fields, and return |
7cac9316 XL |
322 | /// the list of steps in it in reverse order, with the first step |
323 | /// dropping 0 fields and so on. | |
cc61c64b XL |
324 | /// |
325 | /// `unwind_ladder` is such a list of steps in reverse order, | |
7cac9316 | 326 | /// which is called if the matching step of the drop glue panics. |
dfeec247 XL |
327 | fn drop_halfladder( |
328 | &mut self, | |
329 | unwind_ladder: &[Unwind], | |
330 | mut succ: BasicBlock, | |
331 | fields: &[(Place<'tcx>, Option<D::Path>)], | |
332 | ) -> Vec<BasicBlock> { | |
5099ac24 | 333 | iter::once(succ) |
ba9703b0 XL |
334 | .chain(fields.iter().rev().zip(unwind_ladder).map(|(&(place, path), &unwind_succ)| { |
335 | succ = self.drop_subpath(place, path, succ, unwind_succ); | |
336 | succ | |
337 | })) | |
dfeec247 | 338 | .collect() |
7cac9316 | 339 | } |
cc61c64b | 340 | |
7cac9316 XL |
341 | fn drop_ladder_bottom(&mut self) -> (BasicBlock, Unwind) { |
342 | // Clear the "master" drop flag at the end. This is needed | |
343 | // because the "master" drop protects the ADT's discriminant, | |
344 | // which is invalidated after the ADT is dropped. | |
29967ef6 | 345 | (self.drop_flag_reset_block(DropFlagMode::Shallow, self.succ, self.unwind), self.unwind) |
cc61c64b XL |
346 | } |
347 | ||
9fa01778 | 348 | /// Creates a full drop ladder, consisting of 2 connected half-drop-ladders |
cc61c64b XL |
349 | /// |
350 | /// For example, with 3 fields, the drop ladder is | |
351 | /// | |
352 | /// .d0: | |
353 | /// ELAB(drop location.0 [target=.d1, unwind=.c1]) | |
354 | /// .d1: | |
355 | /// ELAB(drop location.1 [target=.d2, unwind=.c2]) | |
356 | /// .d2: | |
357 | /// ELAB(drop location.2 [target=`self.succ`, unwind=`self.unwind`]) | |
358 | /// .c1: | |
359 | /// ELAB(drop location.1 [target=.c2]) | |
360 | /// .c2: | |
361 | /// ELAB(drop location.2 [target=`self.unwind`]) | |
7cac9316 XL |
362 | /// |
363 | /// NOTE: this does not clear the master drop flag, so you need | |
364 | /// to point succ/unwind on a `drop_ladder_bottom`. | |
dc9dc135 XL |
365 | fn drop_ladder( |
366 | &mut self, | |
367 | fields: Vec<(Place<'tcx>, Option<D::Path>)>, | |
368 | succ: BasicBlock, | |
369 | unwind: Unwind, | |
370 | ) -> (BasicBlock, Unwind) { | |
cc61c64b XL |
371 | debug!("drop_ladder({:?}, {:?})", self, fields); |
372 | ||
373 | let mut fields = fields; | |
ba9703b0 | 374 | fields.retain(|&(place, _)| { |
ff7c6d11 | 375 | self.place_ty(place).needs_drop(self.tcx(), self.elaborator.param_env()) |
cc61c64b XL |
376 | }); |
377 | ||
378 | debug!("drop_ladder - fields needing drop: {:?}", fields); | |
379 | ||
7cac9316 XL |
380 | let unwind_ladder = vec![Unwind::InCleanup; fields.len() + 1]; |
381 | let unwind_ladder: Vec<_> = if let Unwind::To(target) = unwind { | |
382 | let halfladder = self.drop_halfladder(&unwind_ladder, target, &fields); | |
383 | halfladder.into_iter().map(Unwind::To).collect() | |
cc61c64b | 384 | } else { |
7cac9316 | 385 | unwind_ladder |
cc61c64b XL |
386 | }; |
387 | ||
dfeec247 | 388 | let normal_ladder = self.drop_halfladder(&unwind_ladder, succ, &fields); |
cc61c64b | 389 | |
7cac9316 | 390 | (*normal_ladder.last().unwrap(), *unwind_ladder.last().unwrap()) |
cc61c64b XL |
391 | } |
392 | ||
dc9dc135 | 393 | fn open_drop_for_tuple(&mut self, tys: &[Ty<'tcx>]) -> BasicBlock { |
cc61c64b XL |
394 | debug!("open_drop_for_tuple({:?}, {:?})", self, tys); |
395 | ||
dfeec247 XL |
396 | let fields = tys |
397 | .iter() | |
398 | .enumerate() | |
399 | .map(|(i, &ty)| { | |
400 | ( | |
353b0b11 FG |
401 | self.tcx().mk_place_field(self.place, FieldIdx::new(i), ty), |
402 | self.elaborator.field_subpath(self.path, FieldIdx::new(i)), | |
dfeec247 XL |
403 | ) |
404 | }) | |
405 | .collect(); | |
cc61c64b | 406 | |
7cac9316 XL |
407 | let (succ, unwind) = self.drop_ladder_bottom(); |
408 | self.drop_ladder(fields, succ, unwind).0 | |
cc61c64b XL |
409 | } |
410 | ||
fe692bf9 | 411 | /// Drops the T contained in a `Box<T>` if it has not been moved out of |
487cf647 | 412 | #[instrument(level = "debug", ret)] |
fe692bf9 FG |
413 | fn open_drop_for_box_contents( |
414 | &mut self, | |
415 | adt: ty::AdtDef<'tcx>, | |
add651ee | 416 | args: GenericArgsRef<'tcx>, |
fe692bf9 FG |
417 | succ: BasicBlock, |
418 | unwind: Unwind, | |
419 | ) -> BasicBlock { | |
923072b8 FG |
420 | // drop glue is sent straight to codegen |
421 | // box cannot be directly dereferenced | |
add651ee | 422 | let unique_ty = adt.non_enum_variant().fields[FieldIdx::new(0)].ty(self.tcx(), args); |
353b0b11 | 423 | let unique_variant = unique_ty.ty_adt_def().unwrap().non_enum_variant(); |
add651ee FG |
424 | let nonnull_ty = unique_variant.fields[FieldIdx::from_u32(0)].ty(self.tcx(), args); |
425 | let ptr_ty = Ty::new_imm_ptr(self.tcx(), args[0].expect_ty()); | |
923072b8 | 426 | |
353b0b11 FG |
427 | let unique_place = self.tcx().mk_place_field(self.place, FieldIdx::new(0), unique_ty); |
428 | let nonnull_place = self.tcx().mk_place_field(unique_place, FieldIdx::new(0), nonnull_ty); | |
429 | let ptr_place = self.tcx().mk_place_field(nonnull_place, FieldIdx::new(0), ptr_ty); | |
923072b8 FG |
430 | let interior = self.tcx().mk_place_deref(ptr_place); |
431 | ||
cc61c64b XL |
432 | let interior_path = self.elaborator.deref_subpath(self.path); |
433 | ||
fe692bf9 | 434 | self.drop_subpath(interior, interior_path, succ, unwind) |
cc61c64b XL |
435 | } |
436 | ||
487cf647 | 437 | #[instrument(level = "debug", ret)] |
add651ee FG |
438 | fn open_drop_for_adt( |
439 | &mut self, | |
440 | adt: ty::AdtDef<'tcx>, | |
441 | args: GenericArgsRef<'tcx>, | |
442 | ) -> BasicBlock { | |
5e7ed085 | 443 | if adt.variants().is_empty() { |
cc61c64b XL |
444 | return self.elaborator.patch().new_block(BasicBlockData { |
445 | statements: vec![], | |
446 | terminator: Some(Terminator { | |
447 | source_info: self.source_info, | |
dfeec247 | 448 | kind: TerminatorKind::Unreachable, |
cc61c64b | 449 | }), |
dfeec247 | 450 | is_cleanup: self.unwind.is_cleanup(), |
cc61c64b XL |
451 | }); |
452 | } | |
453 | ||
8faf50e0 | 454 | let skip_contents = |
5e7ed085 | 455 | adt.is_union() || Some(adt.did()) == self.tcx().lang_items().manually_drop(); |
8faf50e0 | 456 | let contents_drop = if skip_contents { |
cc61c64b XL |
457 | (self.succ, self.unwind) |
458 | } else { | |
add651ee | 459 | self.open_drop_for_adt_contents(adt, args) |
cc61c64b XL |
460 | }; |
461 | ||
fe692bf9 FG |
462 | if adt.is_box() { |
463 | // we need to drop the inside of the box before running the destructor | |
464 | let succ = self.destructor_call_block(contents_drop); | |
465 | let unwind = contents_drop | |
466 | .1 | |
467 | .map(|unwind| self.destructor_call_block((unwind, Unwind::InCleanup))); | |
468 | ||
add651ee | 469 | self.open_drop_for_box_contents(adt, args, succ, unwind) |
fe692bf9 | 470 | } else if adt.has_dtor(self.tcx()) { |
cc61c64b XL |
471 | self.destructor_call_block(contents_drop) |
472 | } else { | |
473 | contents_drop.0 | |
474 | } | |
475 | } | |
476 | ||
dfeec247 XL |
477 | fn open_drop_for_adt_contents( |
478 | &mut self, | |
5e7ed085 | 479 | adt: ty::AdtDef<'tcx>, |
add651ee | 480 | args: GenericArgsRef<'tcx>, |
dfeec247 | 481 | ) -> (BasicBlock, Unwind) { |
7cac9316 | 482 | let (succ, unwind) = self.drop_ladder_bottom(); |
ff7c6d11 | 483 | if !adt.is_enum() { |
7cac9316 | 484 | let fields = self.move_paths_for_fields( |
ff7c6d11 | 485 | self.place, |
7cac9316 | 486 | self.path, |
353b0b11 | 487 | &adt.variant(FIRST_VARIANT), |
add651ee | 488 | args, |
7cac9316 XL |
489 | ); |
490 | self.drop_ladder(fields, succ, unwind) | |
491 | } else { | |
add651ee | 492 | self.open_drop_for_multivariant(adt, args, succ, unwind) |
7cac9316 XL |
493 | } |
494 | } | |
495 | ||
dfeec247 XL |
496 | fn open_drop_for_multivariant( |
497 | &mut self, | |
5e7ed085 | 498 | adt: ty::AdtDef<'tcx>, |
add651ee | 499 | args: GenericArgsRef<'tcx>, |
dfeec247 XL |
500 | succ: BasicBlock, |
501 | unwind: Unwind, | |
502 | ) -> (BasicBlock, Unwind) { | |
5e7ed085 FG |
503 | let mut values = Vec::with_capacity(adt.variants().len()); |
504 | let mut normal_blocks = Vec::with_capacity(adt.variants().len()); | |
dfeec247 | 505 | let mut unwind_blocks = |
5e7ed085 | 506 | if unwind.is_cleanup() { None } else { Some(Vec::with_capacity(adt.variants().len())) }; |
cc61c64b | 507 | |
74b04a01 | 508 | let mut have_otherwise_with_drop_glue = false; |
7cac9316 | 509 | let mut have_otherwise = false; |
e74abb32 | 510 | let tcx = self.tcx(); |
7cac9316 | 511 | |
e74abb32 | 512 | for (variant_index, discr) in adt.discriminants(tcx) { |
5e7ed085 | 513 | let variant = &adt.variant(variant_index); |
dfeec247 | 514 | let subpath = self.elaborator.downcast_subpath(self.path, variant_index); |
74b04a01 | 515 | |
7cac9316 | 516 | if let Some(variant_path) = subpath { |
e74abb32 | 517 | let base_place = tcx.mk_place_elem( |
ba9703b0 | 518 | self.place, |
5099ac24 | 519 | ProjectionElem::Downcast(Some(variant.name), variant_index), |
dfeec247 | 520 | ); |
add651ee | 521 | let fields = self.move_paths_for_fields(base_place, variant_path, &variant, args); |
0531ce1d | 522 | values.push(discr.val); |
7cac9316 XL |
523 | if let Unwind::To(unwind) = unwind { |
524 | // We can't use the half-ladder from the original | |
525 | // drop ladder, because this breaks the | |
526 | // "funclet can't have 2 successor funclets" | |
527 | // requirement from MSVC: | |
528 | // | |
529 | // switch unwind-switch | |
530 | // / \ / \ | |
531 | // v1.0 v2.0 v2.0-unwind v1.0-unwind | |
532 | // | | / | | |
533 | // v1.1-unwind v2.1-unwind | | |
534 | // ^ | | |
535 | // \-------------------------------/ | |
536 | // | |
537 | // Create a duplicate half-ladder to avoid that. We | |
538 | // could technically only do this on MSVC, but I | |
539 | // I want to minimize the divergence between MSVC | |
540 | // and non-MSVC. | |
541 | ||
542 | let unwind_blocks = unwind_blocks.as_mut().unwrap(); | |
543 | let unwind_ladder = vec![Unwind::InCleanup; fields.len() + 1]; | |
dfeec247 | 544 | let halfladder = self.drop_halfladder(&unwind_ladder, unwind, &fields); |
7cac9316 | 545 | unwind_blocks.push(halfladder.last().cloned().unwrap()); |
cc61c64b | 546 | } |
7cac9316 XL |
547 | let (normal, _) = self.drop_ladder(fields, succ, unwind); |
548 | normal_blocks.push(normal); | |
549 | } else { | |
550 | have_otherwise = true; | |
74b04a01 XL |
551 | |
552 | let param_env = self.elaborator.param_env(); | |
553 | let have_field_with_drop_glue = variant | |
554 | .fields | |
555 | .iter() | |
add651ee | 556 | .any(|field| field.ty(tcx, args).needs_drop(tcx, param_env)); |
74b04a01 XL |
557 | if have_field_with_drop_glue { |
558 | have_otherwise_with_drop_glue = true; | |
559 | } | |
7cac9316 XL |
560 | } |
561 | } | |
cc61c64b | 562 | |
74b04a01 XL |
563 | if !have_otherwise { |
564 | values.pop(); | |
565 | } else if !have_otherwise_with_drop_glue { | |
566 | normal_blocks.push(self.goto_block(succ, unwind)); | |
567 | if let Unwind::To(unwind) = unwind { | |
568 | unwind_blocks.as_mut().unwrap().push(self.goto_block(unwind, Unwind::InCleanup)); | |
569 | } | |
570 | } else { | |
7cac9316 XL |
571 | normal_blocks.push(self.drop_block(succ, unwind)); |
572 | if let Unwind::To(unwind) = unwind { | |
dfeec247 | 573 | unwind_blocks.as_mut().unwrap().push(self.drop_block(unwind, Unwind::InCleanup)); |
cc61c64b XL |
574 | } |
575 | } | |
7cac9316 | 576 | |
dfeec247 XL |
577 | ( |
578 | self.adt_switch_block(adt, normal_blocks, &values, succ, unwind), | |
579 | unwind.map(|unwind| { | |
580 | self.adt_switch_block( | |
581 | adt, | |
582 | unwind_blocks.unwrap(), | |
583 | &values, | |
584 | unwind, | |
585 | Unwind::InCleanup, | |
586 | ) | |
587 | }), | |
588 | ) | |
589 | } | |
590 | ||
591 | fn adt_switch_block( | |
592 | &mut self, | |
5e7ed085 | 593 | adt: ty::AdtDef<'tcx>, |
dfeec247 XL |
594 | blocks: Vec<BasicBlock>, |
595 | values: &[u128], | |
596 | succ: BasicBlock, | |
597 | unwind: Unwind, | |
598 | ) -> BasicBlock { | |
cc61c64b XL |
599 | // If there are multiple variants, then if something |
600 | // is present within the enum the discriminant, tracked | |
601 | // by the rest path, must be initialized. | |
602 | // | |
603 | // Additionally, we do not want to switch on the | |
604 | // discriminant after it is free-ed, because that | |
605 | // way lies only trouble. | |
5e7ed085 | 606 | let discr_ty = adt.repr().discr_type().to_ty(self.tcx()); |
dc9dc135 | 607 | let discr = Place::from(self.new_temp(discr_ty)); |
ba9703b0 | 608 | let discr_rv = Rvalue::Discriminant(self.place); |
7cac9316 | 609 | let switch_block = BasicBlockData { |
ba9703b0 | 610 | statements: vec![self.assign(discr, discr_rv)], |
cc61c64b XL |
611 | terminator: Some(Terminator { |
612 | source_info: self.source_info, | |
613 | kind: TerminatorKind::SwitchInt { | |
ff7c6d11 | 614 | discr: Operand::Move(discr), |
29967ef6 XL |
615 | targets: SwitchTargets::new( |
616 | values.iter().copied().zip(blocks.iter().copied()), | |
617 | *blocks.last().unwrap(), | |
618 | ), | |
dfeec247 | 619 | }, |
cc61c64b | 620 | }), |
7cac9316 XL |
621 | is_cleanup: unwind.is_cleanup(), |
622 | }; | |
623 | let switch_block = self.elaborator.patch().new_block(switch_block); | |
624 | self.drop_flag_test_block(switch_block, succ, unwind) | |
cc61c64b XL |
625 | } |
626 | ||
dc9dc135 | 627 | fn destructor_call_block(&mut self, (succ, unwind): (BasicBlock, Unwind)) -> BasicBlock { |
cc61c64b XL |
628 | debug!("destructor_call_block({:?}, {:?})", self, succ); |
629 | let tcx = self.tcx(); | |
3dfed10e | 630 | let drop_trait = tcx.require_lang_item(LangItem::Drop, None); |
3c0e092e | 631 | let drop_fn = tcx.associated_item_def_ids(drop_trait)[0]; |
ff7c6d11 | 632 | let ty = self.place_ty(self.place); |
cc61c64b | 633 | |
fe692bf9 FG |
634 | let ref_ty = Ty::new_ref( |
635 | tcx, | |
636 | tcx.lifetimes.re_erased, | |
637 | ty::TypeAndMut { ty, mutbl: hir::Mutability::Mut }, | |
638 | ); | |
ff7c6d11 | 639 | let ref_place = self.new_temp(ref_ty); |
fe692bf9 | 640 | let unit_temp = Place::from(self.new_temp(Ty::new_unit(tcx))); |
cc61c64b | 641 | |
7cac9316 XL |
642 | let result = BasicBlockData { |
643 | statements: vec![self.assign( | |
ba9703b0 | 644 | Place::from(ref_place), |
dfeec247 XL |
645 | Rvalue::Ref( |
646 | tcx.lifetimes.re_erased, | |
fe692bf9 | 647 | BorrowKind::Mut { kind: MutBorrowKind::Default }, |
ba9703b0 | 648 | self.place, |
dfeec247 | 649 | ), |
7cac9316 | 650 | )], |
cc61c64b XL |
651 | terminator: Some(Terminator { |
652 | kind: TerminatorKind::Call { | |
9c376795 FG |
653 | func: Operand::function_handle( |
654 | tcx, | |
655 | drop_fn, | |
656 | [ty.into()], | |
657 | self.source_info.span, | |
658 | ), | |
dc9dc135 | 659 | args: vec![Operand::Move(Place::from(ref_place))], |
923072b8 FG |
660 | destination: unit_temp, |
661 | target: Some(succ), | |
353b0b11 | 662 | unwind: unwind.into_action(), |
fe692bf9 | 663 | call_source: CallSource::Misc, |
f035d41b | 664 | fn_span: self.source_info.span, |
cc61c64b | 665 | }, |
0bf4aa26 | 666 | source_info: self.source_info, |
cc61c64b | 667 | }), |
7cac9316 XL |
668 | is_cleanup: unwind.is_cleanup(), |
669 | }; | |
fe692bf9 FG |
670 | |
671 | let destructor_block = self.elaborator.patch().new_block(result); | |
672 | ||
673 | let block_start = Location { block: destructor_block, statement_index: 0 }; | |
674 | self.elaborator.clear_drop_flag(block_start, self.path, DropFlagMode::Shallow); | |
675 | ||
676 | self.drop_flag_test_block(destructor_block, succ, unwind) | |
7cac9316 XL |
677 | } |
678 | ||
48663c56 | 679 | /// Create a loop that drops an array: |
7cac9316 | 680 | /// |
48663c56 | 681 | /// ```text |
7cac9316 | 682 | /// loop-block: |
353b0b11 | 683 | /// can_go = cur == len |
7cac9316 XL |
684 | /// if can_go then succ else drop-block |
685 | /// drop-block: | |
353b0b11 FG |
686 | /// ptr = &raw mut P[cur] |
687 | /// cur = cur + 1 | |
7cac9316 | 688 | /// drop(ptr) |
48663c56 XL |
689 | /// ``` |
690 | fn drop_loop( | |
691 | &mut self, | |
692 | succ: BasicBlock, | |
693 | cur: Local, | |
353b0b11 | 694 | len: Local, |
48663c56 XL |
695 | ety: Ty<'tcx>, |
696 | unwind: Unwind, | |
48663c56 | 697 | ) -> BasicBlock { |
dfeec247 XL |
698 | let copy = |place: Place<'tcx>| Operand::Copy(place); |
699 | let move_ = |place: Place<'tcx>| Operand::Move(place); | |
7cac9316 XL |
700 | let tcx = self.tcx(); |
701 | ||
fe692bf9 | 702 | let ptr_ty = Ty::new_ptr(tcx, ty::TypeAndMut { ty: ety, mutbl: hir::Mutability::Mut }); |
ba9703b0 | 703 | let ptr = Place::from(self.new_temp(ptr_ty)); |
dfeec247 | 704 | let can_go = Place::from(self.new_temp(tcx.types.bool)); |
7cac9316 | 705 | let one = self.constant_usize(1); |
7cac9316 XL |
706 | |
707 | let drop_block = BasicBlockData { | |
353b0b11 FG |
708 | statements: vec![ |
709 | self.assign( | |
710 | ptr, | |
711 | Rvalue::AddressOf(Mutability::Mut, tcx.mk_place_index(self.place, cur)), | |
712 | ), | |
713 | self.assign( | |
714 | cur.into(), | |
715 | Rvalue::BinaryOp(BinOp::Add, Box::new((move_(cur.into()), one))), | |
716 | ), | |
717 | ], | |
7cac9316 XL |
718 | is_cleanup: unwind.is_cleanup(), |
719 | terminator: Some(Terminator { | |
720 | source_info: self.source_info, | |
721 | // this gets overwritten by drop elaboration. | |
722 | kind: TerminatorKind::Unreachable, | |
dfeec247 | 723 | }), |
7cac9316 XL |
724 | }; |
725 | let drop_block = self.elaborator.patch().new_block(drop_block); | |
726 | ||
727 | let loop_block = BasicBlockData { | |
dfeec247 | 728 | statements: vec![self.assign( |
ba9703b0 | 729 | can_go, |
353b0b11 | 730 | Rvalue::BinaryOp(BinOp::Eq, Box::new((copy(Place::from(cur)), copy(len.into())))), |
dfeec247 | 731 | )], |
7cac9316 XL |
732 | is_cleanup: unwind.is_cleanup(), |
733 | terminator: Some(Terminator { | |
734 | source_info: self.source_info, | |
9c376795 | 735 | kind: TerminatorKind::if_(move_(can_go), succ, drop_block), |
dfeec247 | 736 | }), |
7cac9316 XL |
737 | }; |
738 | let loop_block = self.elaborator.patch().new_block(loop_block); | |
739 | ||
dfeec247 XL |
740 | self.elaborator.patch().patch_terminator( |
741 | drop_block, | |
742 | TerminatorKind::Drop { | |
f035d41b | 743 | place: tcx.mk_place_deref(ptr), |
dfeec247 | 744 | target: loop_block, |
353b0b11 | 745 | unwind: unwind.into_action(), |
49aad941 | 746 | replace: false, |
dfeec247 XL |
747 | }, |
748 | ); | |
7cac9316 XL |
749 | |
750 | loop_block | |
751 | } | |
752 | ||
ff7c6d11 XL |
753 | fn open_drop_for_array(&mut self, ety: Ty<'tcx>, opt_size: Option<u64>) -> BasicBlock { |
754 | debug!("open_drop_for_array({:?}, {:?})", ety, opt_size); | |
e74abb32 XL |
755 | let tcx = self.tcx(); |
756 | ||
ff7c6d11 | 757 | if let Some(size) = opt_size { |
353b0b11 FG |
758 | enum ProjectionKind<Path> { |
759 | Drop(std::ops::Range<u64>), | |
760 | Keep(u64, Path), | |
761 | } | |
762 | // Previously, we'd make a projection for every element in the array and create a drop | |
763 | // ladder if any `array_subpath` was `Some`, i.e. moving out with an array pattern. | |
764 | // This caused huge memory usage when generating the drops for large arrays, so we instead | |
765 | // record the *subslices* which are dropped and the *indexes* which are kept | |
766 | let mut drop_ranges = vec![]; | |
767 | let mut dropping = true; | |
768 | let mut start = 0; | |
769 | for i in 0..size { | |
770 | let path = self.elaborator.array_subpath(self.path, i, size); | |
771 | if dropping && path.is_some() { | |
772 | drop_ranges.push(ProjectionKind::Drop(start..i)); | |
773 | dropping = false; | |
774 | } else if !dropping && path.is_none() { | |
775 | dropping = true; | |
776 | start = i; | |
777 | } | |
778 | if let Some(path) = path { | |
779 | drop_ranges.push(ProjectionKind::Keep(i, path)); | |
780 | } | |
781 | } | |
782 | if !drop_ranges.is_empty() { | |
783 | if dropping { | |
784 | drop_ranges.push(ProjectionKind::Drop(start..size)); | |
785 | } | |
786 | let fields = drop_ranges | |
787 | .iter() | |
788 | .rev() | |
789 | .map(|p| { | |
790 | let (project, path) = match p { | |
791 | ProjectionKind::Drop(r) => ( | |
792 | ProjectionElem::Subslice { | |
793 | from: r.start, | |
794 | to: r.end, | |
795 | from_end: false, | |
796 | }, | |
797 | None, | |
798 | ), | |
799 | &ProjectionKind::Keep(offset, path) => ( | |
800 | ProjectionElem::ConstantIndex { | |
801 | offset, | |
802 | min_length: size, | |
803 | from_end: false, | |
804 | }, | |
805 | Some(path), | |
806 | ), | |
807 | }; | |
808 | (tcx.mk_place_elem(self.place, project), path) | |
809 | }) | |
810 | .collect::<Vec<_>>(); | |
ff7c6d11 | 811 | let (succ, unwind) = self.drop_ladder_bottom(); |
dfeec247 | 812 | return self.drop_ladder(fields, succ, unwind).0; |
ff7c6d11 XL |
813 | } |
814 | } | |
7cac9316 | 815 | |
353b0b11 | 816 | self.drop_loop_pair(ety) |
7cac9316 XL |
817 | } |
818 | ||
74b04a01 | 819 | /// Creates a pair of drop-loops of `place`, which drops its contents, even |
353b0b11 FG |
820 | /// in the case of 1 panic. |
821 | fn drop_loop_pair(&mut self, ety: Ty<'tcx>) -> BasicBlock { | |
822 | debug!("drop_loop_pair({:?})", ety); | |
7cac9316 | 823 | let tcx = self.tcx(); |
353b0b11 FG |
824 | let len = self.new_temp(tcx.types.usize); |
825 | let cur = self.new_temp(tcx.types.usize); | |
7cac9316 | 826 | |
353b0b11 FG |
827 | let unwind = |
828 | self.unwind.map(|unwind| self.drop_loop(unwind, cur, len, ety, Unwind::InCleanup)); | |
7cac9316 | 829 | |
353b0b11 | 830 | let loop_block = self.drop_loop(self.succ, cur, len, ety, unwind); |
7cac9316 | 831 | |
353b0b11 FG |
832 | let zero = self.constant_usize(0); |
833 | let block = BasicBlockData { | |
834 | statements: vec![ | |
835 | self.assign(len.into(), Rvalue::Len(self.place)), | |
836 | self.assign(cur.into(), Rvalue::Use(zero)), | |
837 | ], | |
7cac9316 XL |
838 | is_cleanup: unwind.is_cleanup(), |
839 | terminator: Some(Terminator { | |
840 | source_info: self.source_info, | |
dfeec247 XL |
841 | kind: TerminatorKind::Goto { target: loop_block }, |
842 | }), | |
353b0b11 | 843 | }; |
7cac9316 | 844 | |
353b0b11 | 845 | let drop_block = self.elaborator.patch().new_block(block); |
7cac9316 XL |
846 | // FIXME(#34708): handle partially-dropped array/slice elements. |
847 | let reset_block = self.drop_flag_reset_block(DropFlagMode::Deep, drop_block, unwind); | |
48663c56 | 848 | self.drop_flag_test_block(reset_block, self.succ, unwind) |
cc61c64b XL |
849 | } |
850 | ||
851 | /// The slow-path - create an "open", elaborated drop for a type | |
852 | /// which is moved-out-of only partially, and patch `bb` to a jump | |
853 | /// to it. This must not be called on ADTs with a destructor, | |
854 | /// as these can't be moved-out-of, except for `Box<T>`, which is | |
855 | /// special-cased. | |
856 | /// | |
857 | /// This creates a "drop ladder" that drops the needed fields of the | |
858 | /// ADT, both in the success case or if one of the destructors fail. | |
dc9dc135 | 859 | fn open_drop(&mut self) -> BasicBlock { |
ff7c6d11 | 860 | let ty = self.place_ty(self.place); |
1b1a35ee | 861 | match ty.kind() { |
add651ee | 862 | ty::Closure(_, args) => self.open_drop_for_tuple(&args.as_closure().upvar_tys()), |
ea8adc8c XL |
863 | // Note that `elaborate_drops` only drops the upvars of a generator, |
864 | // and this is ok because `open_drop` here can only be reached | |
865 | // within that own generator's resume function. | |
866 | // This should only happen for the self argument on the resume function. | |
5e7ed085 | 867 | // It effectively only contains upvars until the generator transformation runs. |
dc9dc135 | 868 | // See librustc_body/transform/generator.rs for more details. |
add651ee | 869 | ty::Generator(_, args, _) => self.open_drop_for_tuple(&args.as_generator().upvar_tys()), |
5e7ed085 | 870 | ty::Tuple(fields) => self.open_drop_for_tuple(fields), |
add651ee | 871 | ty::Adt(def, args) => self.open_drop_for_adt(*def, args), |
29967ef6 | 872 | ty::Dynamic(..) => self.complete_drop(self.succ, self.unwind), |
b7449926 | 873 | ty::Array(ety, size) => { |
9ffffee4 | 874 | let size = size.try_eval_target_usize(self.tcx(), self.elaborator.param_env()); |
5099ac24 | 875 | self.open_drop_for_array(*ety, size) |
dfeec247 | 876 | } |
353b0b11 | 877 | ty::Slice(ety) => self.drop_loop_pair(*ety), |
ff7c6d11 | 878 | |
2b03887a | 879 | _ => span_bug!(self.source_info.span, "open drop from non-ADT `{:?}`", ty), |
cc61c64b XL |
880 | } |
881 | } | |
882 | ||
29967ef6 XL |
883 | fn complete_drop(&mut self, succ: BasicBlock, unwind: Unwind) -> BasicBlock { |
884 | debug!("complete_drop(succ={:?}, unwind={:?})", succ, unwind); | |
cc61c64b | 885 | |
7cac9316 | 886 | let drop_block = self.drop_block(succ, unwind); |
7cac9316 XL |
887 | |
888 | self.drop_flag_test_block(drop_block, succ, unwind) | |
889 | } | |
cc61c64b | 890 | |
f9f354fc XL |
891 | /// Creates a block that resets the drop flag. If `mode` is deep, all children drop flags will |
892 | /// also be cleared. | |
dfeec247 XL |
893 | fn drop_flag_reset_block( |
894 | &mut self, | |
895 | mode: DropFlagMode, | |
896 | succ: BasicBlock, | |
897 | unwind: Unwind, | |
898 | ) -> BasicBlock { | |
7cac9316 XL |
899 | debug!("drop_flag_reset_block({:?},{:?})", self, mode); |
900 | ||
29967ef6 XL |
901 | if unwind.is_cleanup() { |
902 | // The drop flag isn't read again on the unwind path, so don't | |
903 | // bother setting it. | |
904 | return succ; | |
905 | } | |
7cac9316 | 906 | let block = self.new_block(unwind, TerminatorKind::Goto { target: succ }); |
74b04a01 | 907 | let block_start = Location { block, statement_index: 0 }; |
7cac9316 XL |
908 | self.elaborator.clear_drop_flag(block_start, self.path, mode); |
909 | block | |
cc61c64b XL |
910 | } |
911 | ||
dc9dc135 | 912 | fn elaborated_drop_block(&mut self) -> BasicBlock { |
cc61c64b | 913 | debug!("elaborated_drop_block({:?})", self); |
f9f354fc | 914 | let blk = self.drop_block(self.succ, self.unwind); |
cc61c64b XL |
915 | self.elaborate_drop(blk); |
916 | blk | |
917 | } | |
918 | ||
dc9dc135 | 919 | fn drop_block(&mut self, target: BasicBlock, unwind: Unwind) -> BasicBlock { |
49aad941 FG |
920 | let block = TerminatorKind::Drop { |
921 | place: self.place, | |
922 | target, | |
923 | unwind: unwind.into_action(), | |
924 | replace: false, | |
925 | }; | |
7cac9316 | 926 | self.new_block(unwind, block) |
cc61c64b XL |
927 | } |
928 | ||
74b04a01 XL |
929 | fn goto_block(&mut self, target: BasicBlock, unwind: Unwind) -> BasicBlock { |
930 | let block = TerminatorKind::Goto { target }; | |
931 | self.new_block(unwind, block) | |
932 | } | |
933 | ||
f9f354fc XL |
934 | /// Returns the block to jump to in order to test the drop flag and execute the drop. |
935 | /// | |
936 | /// Depending on the required `DropStyle`, this might be a generated block with an `if` | |
937 | /// terminator (for dynamic/open drops), or it might be `on_set` or `on_unset` itself, in case | |
938 | /// the drop can be statically determined. | |
dfeec247 XL |
939 | fn drop_flag_test_block( |
940 | &mut self, | |
941 | on_set: BasicBlock, | |
942 | on_unset: BasicBlock, | |
943 | unwind: Unwind, | |
944 | ) -> BasicBlock { | |
cc61c64b | 945 | let style = self.elaborator.drop_style(self.path, DropFlagMode::Shallow); |
dfeec247 XL |
946 | debug!( |
947 | "drop_flag_test_block({:?},{:?},{:?},{:?}) - {:?}", | |
948 | self, on_set, on_unset, unwind, style | |
949 | ); | |
cc61c64b XL |
950 | |
951 | match style { | |
952 | DropStyle::Dead => on_unset, | |
953 | DropStyle::Static => on_set, | |
954 | DropStyle::Conditional | DropStyle::Open => { | |
955 | let flag = self.elaborator.get_drop_flag(self.path).unwrap(); | |
9c376795 | 956 | let term = TerminatorKind::if_(flag, on_set, on_unset); |
7cac9316 | 957 | self.new_block(unwind, term) |
cc61c64b XL |
958 | } |
959 | } | |
960 | } | |
961 | ||
dc9dc135 | 962 | fn new_block(&mut self, unwind: Unwind, k: TerminatorKind<'tcx>) -> BasicBlock { |
cc61c64b XL |
963 | self.elaborator.patch().new_block(BasicBlockData { |
964 | statements: vec![], | |
dfeec247 XL |
965 | terminator: Some(Terminator { source_info: self.source_info, kind: k }), |
966 | is_cleanup: unwind.is_cleanup(), | |
cc61c64b XL |
967 | }) |
968 | } | |
969 | ||
970 | fn new_temp(&mut self, ty: Ty<'tcx>) -> Local { | |
971 | self.elaborator.patch().new_temp(ty, self.source_info.span) | |
972 | } | |
973 | ||
7cac9316 | 974 | fn constant_usize(&self, val: u16) -> Operand<'tcx> { |
781aab86 | 975 | Operand::Constant(Box::new(ConstOperand { |
7cac9316 | 976 | span: self.source_info.span, |
b7449926 | 977 | user_ty: None, |
781aab86 | 978 | const_: Const::from_usize(self.tcx(), val.into()), |
94222f64 | 979 | })) |
7cac9316 XL |
980 | } |
981 | ||
ba9703b0 | 982 | fn assign(&self, lhs: Place<'tcx>, rhs: Rvalue<'tcx>) -> Statement<'tcx> { |
94222f64 XL |
983 | Statement { |
984 | source_info: self.source_info, | |
985 | kind: StatementKind::Assign(Box::new((lhs, rhs))), | |
986 | } | |
7cac9316 | 987 | } |
cc61c64b | 988 | } |