]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_mir_transform/src/elaborate_drops.rs
New upstream version 1.61.0+dfsg1
[rustc.git] / compiler / rustc_mir_transform / src / elaborate_drops.rs
CommitLineData
c295e0f8 1use crate::MirPass;
dfeec247 2use rustc_data_structures::fx::FxHashMap;
e74abb32 3use rustc_index::bit_set::BitSet;
c295e0f8 4use rustc_middle::mir::patch::MirPatch;
ba9703b0
XL
5use rustc_middle::mir::*;
6use rustc_middle::ty::{self, TyCtxt};
c295e0f8
XL
7use rustc_mir_dataflow::elaborate_drops::{elaborate_drop, DropFlagState, Unwind};
8use rustc_mir_dataflow::elaborate_drops::{DropElaborator, DropFlagMode, DropStyle};
9use rustc_mir_dataflow::impls::{MaybeInitializedPlaces, MaybeUninitializedPlaces};
10use rustc_mir_dataflow::move_paths::{LookupResult, MoveData, MovePathIndex};
11use rustc_mir_dataflow::on_lookup_result_bits;
12use rustc_mir_dataflow::MoveDataParamEnv;
13use rustc_mir_dataflow::{on_all_children_bits, on_all_drop_children_bits};
14use rustc_mir_dataflow::{Analysis, ResultsCursor};
dfeec247 15use rustc_span::Span;
ba9703b0 16use rustc_target::abi::VariantIdx;
0bf4aa26 17use std::fmt;
3157f602
XL
18
19pub struct ElaborateDrops;
20
e1599b0c 21impl<'tcx> MirPass<'tcx> for ElaborateDrops {
a2a8927a 22 fn phase_change(&self) -> Option<MirPhase> {
5e7ed085 23 Some(MirPhase::DropsLowered)
a2a8927a
XL
24 }
25
29967ef6
XL
26 fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
27 debug!("elaborate_drops({:?} @ {:?})", body.source, body.span);
abe05a73 28
29967ef6
XL
29 let def_id = body.source.def_id();
30 let param_env = tcx.param_env_reveal_all_normalized(def_id);
60c5eb7d 31 let move_data = match MoveData::gather_moves(body, tcx, param_env) {
8faf50e0 32 Ok(move_data) => move_data,
dfeec247
XL
33 Err((move_data, _)) => {
34 tcx.sess.delay_span_bug(
35 body.span,
36 "No `move_errors` should be allowed in MIR borrowck",
37 );
38 move_data
39 }
8faf50e0 40 };
3157f602 41 let elaborate_patch = {
dc9dc135 42 let body = &*body;
dfeec247 43 let env = MoveDataParamEnv { move_data, param_env };
29967ef6 44 let dead_unwinds = find_dead_unwinds(tcx, body, &env);
74b04a01
XL
45
46 let inits = MaybeInitializedPlaces::new(tcx, body, &env)
29967ef6 47 .into_engine(tcx, body)
74b04a01 48 .dead_unwinds(&dead_unwinds)
1b1a35ee 49 .pass_name("elaborate_drops")
74b04a01
XL
50 .iterate_to_fixpoint()
51 .into_results_cursor(body);
52
53 let uninits = MaybeUninitializedPlaces::new(tcx, body, &env)
f035d41b 54 .mark_inactive_variants_as_uninit()
29967ef6 55 .into_engine(tcx, body)
74b04a01 56 .dead_unwinds(&dead_unwinds)
1b1a35ee 57 .pass_name("elaborate_drops")
74b04a01
XL
58 .iterate_to_fixpoint()
59 .into_results_cursor(body);
3157f602
XL
60
61 ElaborateDropsCtxt {
3b2f2976 62 tcx,
dc9dc135 63 body,
3157f602 64 env: &env,
74b04a01 65 init_data: InitializationData { inits, uninits },
0bf4aa26 66 drop_flags: Default::default(),
dc9dc135 67 patch: MirPatch::new(body),
dfeec247
XL
68 }
69 .elaborate()
3157f602 70 };
dc9dc135 71 elaborate_patch.apply(body);
3157f602
XL
72 }
73}
74
9fa01778 75/// Returns the set of basic blocks whose unwind edges are known
cc61c64b
XL
76/// to not be reachable, because they are `drop` terminators
77/// that can't drop anything.
dc9dc135
XL
78fn find_dead_unwinds<'tcx>(
79 tcx: TyCtxt<'tcx>,
80 body: &Body<'tcx>,
dc9dc135
XL
81 env: &MoveDataParamEnv<'tcx>,
82) -> BitSet<BasicBlock> {
83 debug!("find_dead_unwinds({:?})", body.span);
cc61c64b
XL
84 // We only need to do this pass once, because unwind edges can only
85 // reach cleanup blocks, which can't have unwind edges themselves.
dc9dc135 86 let mut dead_unwinds = BitSet::new_empty(body.basic_blocks().len());
74b04a01 87 let mut flow_inits = MaybeInitializedPlaces::new(tcx, body, &env)
29967ef6 88 .into_engine(tcx, body)
1b1a35ee 89 .pass_name("find_dead_unwinds")
74b04a01
XL
90 .iterate_to_fixpoint()
91 .into_results_cursor(body);
dc9dc135 92 for (bb, bb_data) in body.basic_blocks().iter_enumerated() {
f035d41b
XL
93 let place = match bb_data.terminator().kind {
94 TerminatorKind::Drop { ref place, unwind: Some(_), .. }
95 | TerminatorKind::DropAndReplace { ref place, unwind: Some(_), .. } => place,
ea8adc8c
XL
96 _ => continue,
97 };
cc61c64b 98
74b04a01 99 debug!("find_dead_unwinds @ {:?}: {:?}", bb, bb_data);
cc61c64b 100
5e7ed085
FG
101 let LookupResult::Exact(path) = env.move_data.rev_lookup.find(place.as_ref()) else {
102 debug!("find_dead_unwinds: has parent; skipping");
103 continue;
ea8adc8c 104 };
cc61c64b 105
f9f354fc 106 flow_inits.seek_before_primary_effect(body.terminator_loc(bb));
74b04a01
XL
107 debug!(
108 "find_dead_unwinds @ {:?}: path({:?})={:?}; init_data={:?}",
109 bb,
f035d41b 110 place,
74b04a01
XL
111 path,
112 flow_inits.get()
113 );
cc61c64b 114
ea8adc8c 115 let mut maybe_live = false;
dc9dc135 116 on_all_drop_children_bits(tcx, body, &env, path, |child| {
74b04a01 117 maybe_live |= flow_inits.contains(child);
ea8adc8c
XL
118 });
119
120 debug!("find_dead_unwinds @ {:?}: maybe_live={}", bb, maybe_live);
121 if !maybe_live {
0bf4aa26 122 dead_unwinds.insert(bb);
cc61c64b
XL
123 }
124 }
125
126 dead_unwinds
127}
128
74b04a01
XL
129struct InitializationData<'mir, 'tcx> {
130 inits: ResultsCursor<'mir, 'tcx, MaybeInitializedPlaces<'mir, 'tcx>>,
131 uninits: ResultsCursor<'mir, 'tcx, MaybeUninitializedPlaces<'mir, 'tcx>>,
3157f602
XL
132}
133
74b04a01
XL
134impl InitializationData<'_, '_> {
135 fn seek_before(&mut self, loc: Location) {
f9f354fc
XL
136 self.inits.seek_before_primary_effect(loc);
137 self.uninits.seek_before_primary_effect(loc);
3157f602
XL
138 }
139
74b04a01
XL
140 fn maybe_live_dead(&self, path: MovePathIndex) -> (bool, bool) {
141 (self.inits.contains(path), self.uninits.contains(path))
3157f602
XL
142 }
143}
144
dc9dc135 145struct Elaborator<'a, 'b, 'tcx> {
cc61c64b
XL
146 ctxt: &'a mut ElaborateDropsCtxt<'b, 'tcx>,
147}
148
a2a8927a 149impl fmt::Debug for Elaborator<'_, '_, '_> {
9fa01778 150 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
3157f602
XL
151 Ok(())
152 }
153}
154
a2a8927a 155impl<'a, 'tcx> DropElaborator<'a, 'tcx> for Elaborator<'a, '_, 'tcx> {
cc61c64b
XL
156 type Path = MovePathIndex;
157
158 fn patch(&mut self) -> &mut MirPatch<'tcx> {
159 &mut self.ctxt.patch
160 }
161
dc9dc135
XL
162 fn body(&self) -> &'a Body<'tcx> {
163 self.ctxt.body
cc61c64b
XL
164 }
165
dc9dc135 166 fn tcx(&self) -> TyCtxt<'tcx> {
cc61c64b
XL
167 self.ctxt.tcx
168 }
169
7cac9316 170 fn param_env(&self) -> ty::ParamEnv<'tcx> {
cc61c64b
XL
171 self.ctxt.param_env()
172 }
173
174 fn drop_style(&self, path: Self::Path, mode: DropFlagMode) -> DropStyle {
175 let ((maybe_live, maybe_dead), multipart) = match mode {
74b04a01 176 DropFlagMode::Shallow => (self.ctxt.init_data.maybe_live_dead(path), false),
cc61c64b
XL
177 DropFlagMode::Deep => {
178 let mut some_live = false;
179 let mut some_dead = false;
180 let mut children_count = 0;
dfeec247 181 on_all_drop_children_bits(self.tcx(), self.body(), self.ctxt.env, path, |child| {
74b04a01 182 let (live, dead) = self.ctxt.init_data.maybe_live_dead(child);
dfeec247
XL
183 debug!("elaborate_drop: state({:?}) = {:?}", child, (live, dead));
184 some_live |= live;
185 some_dead |= dead;
186 children_count += 1;
187 });
cc61c64b
XL
188 ((some_live, some_dead), children_count != 1)
189 }
190 };
191 match (maybe_live, maybe_dead, multipart) {
192 (false, _, _) => DropStyle::Dead,
193 (true, false, _) => DropStyle::Static,
194 (true, true, false) => DropStyle::Conditional,
195 (true, true, true) => DropStyle::Open,
196 }
197 }
198
199 fn clear_drop_flag(&mut self, loc: Location, path: Self::Path, mode: DropFlagMode) {
200 match mode {
201 DropFlagMode::Shallow => {
202 self.ctxt.set_drop_flag(loc, path, DropFlagState::Absent);
203 }
204 DropFlagMode::Deep => {
205 on_all_children_bits(
dfeec247
XL
206 self.tcx(),
207 self.body(),
208 self.ctxt.move_data(),
209 path,
210 |child| self.ctxt.set_drop_flag(loc, child, DropFlagState::Absent),
211 );
cc61c64b
XL
212 }
213 }
214 }
215
216 fn field_subpath(&self, path: Self::Path, field: Field) -> Option<Self::Path> {
c295e0f8 217 rustc_mir_dataflow::move_path_children_matching(self.ctxt.move_data(), path, |e| match e {
f9f354fc 218 ProjectionElem::Field(idx, _) => idx == field,
e1599b0c 219 _ => false,
cc61c64b
XL
220 })
221 }
222
1b1a35ee 223 fn array_subpath(&self, path: Self::Path, index: u64, size: u64) -> Option<Self::Path> {
c295e0f8 224 rustc_mir_dataflow::move_path_children_matching(self.ctxt.move_data(), path, |e| match e {
60c5eb7d 225 ProjectionElem::ConstantIndex { offset, min_length, from_end } => {
f9f354fc 226 debug_assert!(size == min_length, "min_length should be exact for arrays");
60c5eb7d 227 assert!(!from_end, "from_end should not be used for array element ConstantIndex");
f9f354fc 228 offset == index
e1599b0c 229 }
e1599b0c 230 _ => false,
ff7c6d11
XL
231 })
232 }
233
cc61c64b 234 fn deref_subpath(&self, path: Self::Path) -> Option<Self::Path> {
c295e0f8 235 rustc_mir_dataflow::move_path_children_matching(self.ctxt.move_data(), path, |e| {
f9f354fc 236 e == ProjectionElem::Deref
cc61c64b
XL
237 })
238 }
239
a1dfa0c6 240 fn downcast_subpath(&self, path: Self::Path, variant: VariantIdx) -> Option<Self::Path> {
c295e0f8 241 rustc_mir_dataflow::move_path_children_matching(self.ctxt.move_data(), path, |e| match e {
f9f354fc 242 ProjectionElem::Downcast(_, idx) => idx == variant,
dfeec247 243 _ => false,
cc61c64b
XL
244 })
245 }
246
247 fn get_drop_flag(&mut self, path: Self::Path) -> Option<Operand<'tcx>> {
ff7c6d11 248 self.ctxt.drop_flag(path).map(Operand::Copy)
cc61c64b
XL
249 }
250}
251
dc9dc135
XL
252struct ElaborateDropsCtxt<'a, 'tcx> {
253 tcx: TyCtxt<'tcx>,
254 body: &'a Body<'tcx>,
255 env: &'a MoveDataParamEnv<'tcx>,
74b04a01 256 init_data: InitializationData<'a, 'tcx>,
476ff2be 257 drop_flags: FxHashMap<MovePathIndex, Local>,
3157f602
XL
258 patch: MirPatch<'tcx>,
259}
260
3157f602 261impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
dfeec247
XL
262 fn move_data(&self) -> &'b MoveData<'tcx> {
263 &self.env.move_data
264 }
7cac9316
XL
265
266 fn param_env(&self) -> ty::ParamEnv<'tcx> {
267 self.env.param_env
3157f602
XL
268 }
269
cc61c64b 270 fn create_drop_flag(&mut self, index: MovePathIndex, span: Span) {
3157f602
XL
271 let tcx = self.tcx;
272 let patch = &mut self.patch;
dc9dc135 273 debug!("create_drop_flag({:?})", self.body.span);
dfeec247 274 self.drop_flags.entry(index).or_insert_with(|| patch.new_internal(tcx.types.bool, span));
3157f602
XL
275 }
276
ff7c6d11 277 fn drop_flag(&mut self, index: MovePathIndex) -> Option<Place<'tcx>> {
dc9dc135 278 self.drop_flags.get(&index).map(|t| Place::from(*t))
3157f602
XL
279 }
280
281 /// create a patch that elaborates all drops in the input
282 /// MIR.
dfeec247 283 fn elaborate(mut self) -> MirPatch<'tcx> {
3157f602
XL
284 self.collect_drop_flags();
285
286 self.elaborate_drops();
287
288 self.drop_flags_on_init();
289 self.drop_flags_for_fn_rets();
290 self.drop_flags_for_args();
291 self.drop_flags_for_locs();
292
293 self.patch
294 }
295
dfeec247 296 fn collect_drop_flags(&mut self) {
dc9dc135 297 for (bb, data) in self.body.basic_blocks().iter_enumerated() {
3157f602 298 let terminator = data.terminator();
f035d41b
XL
299 let place = match terminator.kind {
300 TerminatorKind::Drop { ref place, .. }
301 | TerminatorKind::DropAndReplace { ref place, .. } => place,
dfeec247 302 _ => continue,
3157f602
XL
303 };
304
74b04a01 305 self.init_data.seek_before(self.body.terminator_loc(bb));
3157f602 306
f035d41b
XL
307 let path = self.move_data().rev_lookup.find(place.as_ref());
308 debug!("collect_drop_flags: {:?}, place {:?} ({:?})", bb, place, path);
3157f602 309
9e0c209e
SL
310 let path = match path {
311 LookupResult::Exact(e) => e,
312 LookupResult::Parent(None) => continue,
313 LookupResult::Parent(Some(parent)) => {
74b04a01 314 let (_maybe_live, maybe_dead) = self.init_data.maybe_live_dead(parent);
9e0c209e 315 if maybe_dead {
a2a8927a 316 self.tcx.sess.delay_span_bug(
dfeec247 317 terminator.source_info.span,
a2a8927a
XL
318 &format!(
319 "drop of untracked, uninitialized value {:?}, place {:?} ({:?})",
320 bb, place, path,
321 ),
dfeec247 322 );
9e0c209e 323 }
dfeec247 324 continue;
9e0c209e
SL
325 }
326 };
327
dc9dc135 328 on_all_drop_children_bits(self.tcx, self.body, self.env, path, |child| {
74b04a01 329 let (maybe_live, maybe_dead) = self.init_data.maybe_live_dead(child);
dfeec247
XL
330 debug!(
331 "collect_drop_flags: collecting {:?} from {:?}@{:?} - {:?}",
332 child,
f035d41b 333 place,
dfeec247
XL
334 path,
335 (maybe_live, maybe_dead)
336 );
cc61c64b
XL
337 if maybe_live && maybe_dead {
338 self.create_drop_flag(child, terminator.source_info.span)
3157f602
XL
339 }
340 });
341 }
342 }
343
dfeec247 344 fn elaborate_drops(&mut self) {
dc9dc135 345 for (bb, data) in self.body.basic_blocks().iter_enumerated() {
9e0c209e 346 let loc = Location { block: bb, statement_index: data.statements.len() };
3157f602
XL
347 let terminator = data.terminator();
348
349 let resume_block = self.patch.resume_block();
350 match terminator.kind {
f035d41b 351 TerminatorKind::Drop { place, target, unwind } => {
74b04a01 352 self.init_data.seek_before(loc);
f035d41b 353 match self.move_data().rev_lookup.find(place.as_ref()) {
dfeec247 354 LookupResult::Exact(path) => elaborate_drop(
74b04a01 355 &mut Elaborator { ctxt: self },
dfeec247 356 terminator.source_info,
f035d41b 357 place,
dfeec247
XL
358 path,
359 target,
360 if data.is_cleanup {
361 Unwind::InCleanup
362 } else {
363 Unwind::To(Option::unwrap_or(unwind, resume_block))
364 },
365 bb,
366 ),
9e0c209e 367 LookupResult::Parent(..) => {
a2a8927a 368 self.tcx.sess.delay_span_bug(
dfeec247 369 terminator.source_info.span,
a2a8927a 370 &format!("drop of untracked value {:?}", bb),
dfeec247 371 );
9e0c209e
SL
372 }
373 }
3157f602 374 }
f035d41b 375 TerminatorKind::DropAndReplace { place, ref value, target, unwind } => {
3157f602
XL
376 assert!(!data.is_cleanup);
377
f035d41b 378 self.elaborate_replace(loc, place, value, target, unwind);
3157f602 379 }
dfeec247 380 _ => continue,
3157f602
XL
381 }
382 }
383 }
384
385 /// Elaborate a MIR `replace` terminator. This instruction
94b46f34 386 /// is not directly handled by codegen, and therefore
3157f602
XL
387 /// must be desugared.
388 ///
389 /// The desugaring drops the location if needed, and then writes
390 /// the value (including setting the drop flag) over it in *both* arms.
391 ///
ff7c6d11 392 /// The `replace` terminator can also be called on places that
3157f602
XL
393 /// are not tracked by elaboration (for example,
394 /// `replace x[i] <- tmp0`). The borrow checker requires that
395 /// these locations are initialized before the assignment,
396 /// so we just generate an unconditional drop.
397 fn elaborate_replace(
398 &mut self,
399 loc: Location,
f035d41b 400 place: Place<'tcx>,
3157f602
XL
401 value: &Operand<'tcx>,
402 target: BasicBlock,
dfeec247
XL
403 unwind: Option<BasicBlock>,
404 ) {
3157f602 405 let bb = loc.block;
dc9dc135 406 let data = &self.body[bb];
3157f602 407 let terminator = data.terminator();
7cac9316 408 assert!(!data.is_cleanup, "DropAndReplace in unwind path not supported");
3157f602
XL
409
410 let assign = Statement {
94222f64 411 kind: StatementKind::Assign(Box::new((place, Rvalue::Use(value.clone())))),
dfeec247 412 source_info: terminator.source_info,
3157f602
XL
413 };
414
0bf4aa26 415 let unwind = unwind.unwrap_or_else(|| self.patch.resume_block());
3157f602
XL
416 let unwind = self.patch.new_block(BasicBlockData {
417 statements: vec![assign.clone()],
418 terminator: Some(Terminator {
419 kind: TerminatorKind::Goto { target: unwind },
420 ..*terminator
421 }),
dfeec247 422 is_cleanup: true,
3157f602
XL
423 });
424
425 let target = self.patch.new_block(BasicBlockData {
426 statements: vec![assign],
dfeec247 427 terminator: Some(Terminator { kind: TerminatorKind::Goto { target }, ..*terminator }),
7cac9316 428 is_cleanup: false,
3157f602
XL
429 });
430
f035d41b 431 match self.move_data().rev_lookup.find(place.as_ref()) {
9e0c209e
SL
432 LookupResult::Exact(path) => {
433 debug!("elaborate_drop_and_replace({:?}) - tracked {:?}", terminator, path);
74b04a01 434 self.init_data.seek_before(loc);
cc61c64b 435 elaborate_drop(
74b04a01 436 &mut Elaborator { ctxt: self },
cc61c64b 437 terminator.source_info,
f035d41b 438 place,
cc61c64b
XL
439 path,
440 target,
7cac9316 441 Unwind::To(unwind),
dfeec247
XL
442 bb,
443 );
dc9dc135 444 on_all_children_bits(self.tcx, self.body, self.move_data(), path, |child| {
dfeec247
XL
445 self.set_drop_flag(
446 Location { block: target, statement_index: 0 },
447 child,
448 DropFlagState::Present,
449 );
450 self.set_drop_flag(
451 Location { block: unwind, statement_index: 0 },
452 child,
453 DropFlagState::Present,
454 );
9e0c209e
SL
455 });
456 }
457 LookupResult::Parent(parent) => {
458 // drop and replace behind a pointer/array/whatever. The location
459 // must be initialized.
460 debug!("elaborate_drop_and_replace({:?}) - untracked {:?}", terminator, parent);
dfeec247
XL
461 self.patch.patch_terminator(
462 bb,
f035d41b 463 TerminatorKind::Drop { place, target, unwind: Some(unwind) },
dfeec247 464 );
9e0c209e 465 }
3157f602
XL
466 }
467 }
468
3157f602 469 fn constant_bool(&self, span: Span, val: bool) -> Rvalue<'tcx> {
cc61c64b 470 Rvalue::Use(Operand::Constant(Box::new(Constant {
3b2f2976 471 span,
b7449926 472 user_ty: None,
6a06907d 473 literal: ty::Const::from_bool(self.tcx, val).into(),
cc61c64b 474 })))
3157f602
XL
475 }
476
477 fn set_drop_flag(&mut self, loc: Location, path: MovePathIndex, val: DropFlagState) {
478 if let Some(&flag) = self.drop_flags.get(&path) {
dc9dc135 479 let span = self.patch.source_info_for_location(self.body, loc).span;
3157f602 480 let val = self.constant_bool(span, val.value());
dc9dc135 481 self.patch.add_assign(loc, Place::from(flag), val);
3157f602
XL
482 }
483 }
484
485 fn drop_flags_on_init(&mut self) {
83c7162d 486 let loc = Location::START;
dc9dc135 487 let span = self.patch.source_info_for_location(self.body, loc).span;
3157f602
XL
488 let false_ = self.constant_bool(span, false);
489 for flag in self.drop_flags.values() {
dc9dc135 490 self.patch.add_assign(loc, Place::from(*flag), false_.clone());
3157f602
XL
491 }
492 }
493
494 fn drop_flags_for_fn_rets(&mut self) {
dc9dc135 495 for (bb, data) in self.body.basic_blocks().iter_enumerated() {
3157f602 496 if let TerminatorKind::Call {
dfeec247
XL
497 destination: Some((ref place, tgt)),
498 cleanup: Some(_),
499 ..
500 } = data.terminator().kind
501 {
3157f602
XL
502 assert!(!self.patch.is_patched(bb));
503
9e0c209e 504 let loc = Location { block: tgt, statement_index: 0 };
416331ca 505 let path = self.move_data().rev_lookup.find(place.as_ref());
dfeec247
XL
506 on_lookup_result_bits(self.tcx, self.body, self.move_data(), path, |child| {
507 self.set_drop_flag(loc, child, DropFlagState::Present)
508 });
3157f602
XL
509 }
510 }
511 }
512
513 fn drop_flags_for_args(&mut self) {
83c7162d 514 let loc = Location::START;
c295e0f8
XL
515 rustc_mir_dataflow::drop_flag_effects_for_function_entry(
516 self.tcx,
517 self.body,
518 self.env,
519 |path, ds| {
520 self.set_drop_flag(loc, path, ds);
521 },
522 )
3157f602
XL
523 }
524
525 fn drop_flags_for_locs(&mut self) {
526 // We intentionally iterate only over the *old* basic blocks.
527 //
528 // Basic blocks created by drop elaboration update their
529 // drop flags by themselves, to avoid the drop flags being
530 // clobbered before they are read.
531
dc9dc135 532 for (bb, data) in self.body.basic_blocks().iter_enumerated() {
3157f602 533 debug!("drop_flags_for_locs({:?})", data);
dfeec247 534 for i in 0..(data.statements.len() + 1) {
3157f602
XL
535 debug!("drop_flag_for_locs: stmt {}", i);
536 let mut allow_initializations = true;
537 if i == data.statements.len() {
538 match data.terminator().kind {
539 TerminatorKind::Drop { .. } => {
540 // drop elaboration should handle that by itself
dfeec247 541 continue;
3157f602
XL
542 }
543 TerminatorKind::DropAndReplace { .. } => {
544 // this contains the move of the source and
545 // the initialization of the destination. We
546 // only want the former - the latter is handled
547 // by the elaboration code and must be done
548 // *after* the destination is dropped.
549 assert!(self.patch.is_patched(bb));
550 allow_initializations = false;
551 }
041b39d2
XL
552 TerminatorKind::Resume => {
553 // It is possible for `Resume` to be patched
554 // (in particular it can be patched to be replaced with
555 // a Goto; see `MirPatch::new`).
556 }
3157f602
XL
557 _ => {
558 assert!(!self.patch.is_patched(bb));
559 }
560 }
561 }
9e0c209e 562 let loc = Location { block: bb, statement_index: i };
c295e0f8 563 rustc_mir_dataflow::drop_flag_effects_for_location(
dfeec247
XL
564 self.tcx,
565 self.body,
566 self.env,
567 loc,
568 |path, ds| {
3157f602
XL
569 if ds == DropFlagState::Absent || allow_initializations {
570 self.set_drop_flag(loc, path, ds)
571 }
dfeec247 572 },
3157f602
XL
573 )
574 }
575
576 // There may be a critical edge after this call,
577 // so mark the return as initialized *before* the
578 // call.
579 if let TerminatorKind::Call {
ff7c6d11 580 destination: Some((ref place, _)), cleanup: None, ..
dfeec247
XL
581 } = data.terminator().kind
582 {
3157f602
XL
583 assert!(!self.patch.is_patched(bb));
584
9e0c209e 585 let loc = Location { block: bb, statement_index: data.statements.len() };
416331ca 586 let path = self.move_data().rev_lookup.find(place.as_ref());
dfeec247
XL
587 on_lookup_result_bits(self.tcx, self.body, self.move_data(), path, |child| {
588 self.set_drop_flag(loc, child, DropFlagState::Present)
589 });
3157f602
XL
590 }
591 }
592 }
3157f602 593}