1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 Managing the scope stack. The scopes are tied to lexical scopes, so as
13 we descend the HAIR, we push a scope on the stack, translate ite
14 contents, and then pop it off. Every scope is named by a
19 When pushing a new scope, we record the current point in the graph (a
20 basic block); this marks the entry to the scope. We then generate more
21 stuff in the control-flow graph. Whenever the scope is exited, either
22 via a `break` or `return` or just by fallthrough, that marks an exit
23 from the scope. Each lexical scope thus corresponds to a single-entry,
24 multiple-exit (SEME) region in the control-flow graph.
26 For now, we keep a mapping from each `CodeExtent` to its
27 corresponding SEME region for later reference (see caveat in next
28 paragraph). This is because region scopes are tied to
29 them. Eventually, when we shift to non-lexical lifetimes, there should
30 be no need to remember this mapping.
32 There is one additional wrinkle, actually, that I wanted to hide from
33 you but duty compels me to mention. In the course of translating
34 matches, it sometimes happen that certain code (namely guards) gets
35 executed multiple times. This means that the scope lexical scope may
36 in fact correspond to multiple, disjoint SEME regions. So in fact our
37 mapping is from one scope to a vector of SEME regions.
41 The primary purpose for scopes is to insert drops: while translating
42 the contents, we also accumulate lvalues that need to be dropped upon
43 exit from each scope. This is done by calling `schedule_drop`. Once a
44 drop is scheduled, whenever we branch out we will insert drops of all
45 those lvalues onto the outgoing edge. Note that we don't know the full
46 set of scheduled drops up front, and so whenever we exit from the
47 scope we only drop the values scheduled thus far. For example, consider
48 the scope S corresponding to this loop:
59 When processing the `let x`, we will add one drop to the scope for
60 `x`. The break will then insert a drop for `x`. When we process `let
61 y`, we will add another drop (in fact, to a subscope, but let's ignore
62 that for now); any later drops would also drop `y`.
66 There are numerous "normal" ways to early exit a scope: `break`,
67 `continue`, `return` (panics are handled separately). Whenever an
68 early exit occurs, the method `exit_scope` is called. It is given the
69 current point in execution where the early exit occurs, as well as the
70 scope you want to branch to (note that all early exits from to some
71 other enclosing scope). `exit_scope` will record this exit point and
74 Panics are handled in a similar fashion, except that a panic always
75 returns out to the `DIVERGE_BLOCK`. To trigger a panic, simply call
76 `panic(p)` with the current point `p`. Or else you can call
77 `diverge_cleanup`, which will produce a block that you can branch to
78 which does the appropriate cleanup and then diverges. `panic(p)`
79 simply calls `diverge_cleanup()` and adds an edge from `p` to the
84 In addition to the normal scope stack, we track a loop scope stack
85 that contains only loops. It tracks where a `break` and `continue`
90 use build::{BlockAnd, BlockAndExtension, Builder, CFG};
91 use rustc::middle::region::CodeExtent;
94 use rustc::mir::transform::MirSource;
95 use syntax_pos::{Span};
96 use rustc_data_structures::indexed_vec::Idx;
97 use rustc_data_structures::fx::FxHashMap;
100 pub struct Scope<'tcx> {
101 /// The visibility scope this scope was created in.
102 visibility_scope: VisibilityScope,
104 /// the extent of this scope within source code.
107 /// the span of that extent
110 /// Whether there's anything to do for the cleanup path, that is,
111 /// when unwinding through this scope. This includes destructors,
112 /// but not StorageDead statements, which don't get emitted at all
113 /// for unwinding, for several reasons:
114 /// * clang doesn't emit llvm.lifetime.end for C++ unwinding
115 /// * LLVM's memory dependency analysis can't handle it atm
116 /// * polluting the cleanup MIR with StorageDead creates
117 /// landing pads even though there's no actual destructors
118 /// * freeing up stack space has no effect during unwinding
121 /// set of lvalues to drop when exiting this scope. This starts
122 /// out empty but grows as variables are declared during the
123 /// building process. This is a stack, so we always drop from the
124 /// end of the vector (top of the stack) first.
125 drops: Vec<DropData<'tcx>>,
127 /// The cache for drop chain on “normal” exit into a particular BasicBlock.
128 cached_exits: FxHashMap<(BasicBlock, CodeExtent), BasicBlock>,
130 /// The cache for drop chain on "generator drop" exit.
131 cached_generator_drop: Option<BasicBlock>,
135 struct DropData<'tcx> {
136 /// span where drop obligation was incurred (typically where lvalue was declared)
140 location: Lvalue<'tcx>,
142 /// Whether this is a full value Drop, or just a StorageDead.
146 #[derive(Debug, Default, Clone, Copy)]
148 /// The cached block for the cleanups-on-diverge path. This block
149 /// contains code to run the current drop and all the preceding
150 /// drops (i.e. those having lower index in Drop’s Scope drop
152 unwind: Option<BasicBlock>,
154 /// The cached block for unwinds during cleanups-on-generator-drop path
155 generator_drop: Option<BasicBlock>,
161 cached_block: CachedBlock,
166 #[derive(Clone, Debug)]
167 pub struct BreakableScope<'tcx> {
168 /// Extent of the loop
169 pub extent: CodeExtent,
170 /// Where the body of the loop begins. `None` if block
171 pub continue_block: Option<BasicBlock>,
172 /// Block to branch into when the loop or block terminates (either by being `break`-en out
173 /// from, or by having its condition to become false)
174 pub break_block: BasicBlock,
175 /// The destination of the loop/block expression itself (i.e. where to put the result of a
176 /// `break` expression)
177 pub break_destination: Lvalue<'tcx>,
181 fn invalidate(&mut self) {
182 self.generator_drop = None;
186 fn get(&self, generator_drop: bool) -> Option<BasicBlock> {
194 fn ref_mut(&mut self, generator_drop: bool) -> &mut Option<BasicBlock> {
196 &mut self.generator_drop
204 fn may_panic(&self) -> bool {
206 DropKind::Value { .. } => true,
207 DropKind::Storage => false
212 impl<'tcx> Scope<'tcx> {
213 /// Invalidate all the cached blocks in the scope.
215 /// Should always be run for all inner scopes when a drop is pushed into some scope enclosing a
216 /// larger extent of code.
218 /// `unwind` controls whether caches for the unwind branch are also invalidated.
219 fn invalidate_cache(&mut self, unwind: bool) {
220 self.cached_exits.clear();
221 if !unwind { return; }
222 for dropdata in &mut self.drops {
223 if let DropKind::Value { ref mut cached_block } = dropdata.kind {
224 cached_block.invalidate();
229 /// Returns the cached entrypoint for diverging exit from this scope.
231 /// Precondition: the caches must be fully filled (i.e. diverge_cleanup is called) in order for
232 /// this method to work correctly.
233 fn cached_block(&self, generator_drop: bool) -> Option<BasicBlock> {
234 let mut drops = self.drops.iter().rev().filter_map(|data| {
236 DropKind::Value { cached_block } => {
237 Some(cached_block.get(generator_drop))
239 DropKind::Storage => None
242 if let Some(cached_block) = drops.next() {
243 Some(cached_block.expect("drop cache is not filled"))
249 /// Given a span and this scope's visibility scope, make a SourceInfo.
250 fn source_info(&self, span: Span) -> SourceInfo {
253 scope: self.visibility_scope
258 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
259 // Adding and removing scopes
260 // ==========================
261 /// Start a breakable scope, which tracks where `continue` and `break`
262 /// should branch to. See module comment for more details.
264 /// Returns the might_break attribute of the BreakableScope used.
265 pub fn in_breakable_scope<F, R>(&mut self,
266 loop_block: Option<BasicBlock>,
267 break_block: BasicBlock,
268 break_destination: Lvalue<'tcx>,
270 where F: FnOnce(&mut Builder<'a, 'gcx, 'tcx>) -> R
272 let extent = self.topmost_scope();
273 let scope = BreakableScope {
275 continue_block: loop_block,
279 self.breakable_scopes.push(scope);
281 let breakable_scope = self.breakable_scopes.pop().unwrap();
282 assert!(breakable_scope.extent == extent);
286 pub fn in_opt_scope<F, R>(&mut self,
287 opt_extent: Option<(CodeExtent, SourceInfo)>,
288 mut block: BasicBlock,
291 where F: FnOnce(&mut Builder<'a, 'gcx, 'tcx>) -> BlockAnd<R>
293 debug!("in_opt_scope(opt_extent={:?}, block={:?})", opt_extent, block);
294 if let Some(extent) = opt_extent { self.push_scope(extent); }
295 let rv = unpack!(block = f(self));
296 if let Some(extent) = opt_extent {
297 unpack!(block = self.pop_scope(extent, block));
299 debug!("in_scope: exiting opt_extent={:?} block={:?}", opt_extent, block);
303 /// Convenience wrapper that pushes a scope and then executes `f`
304 /// to build its contents, popping the scope afterwards.
305 pub fn in_scope<F, R>(&mut self,
306 extent: (CodeExtent, SourceInfo),
307 mut block: BasicBlock,
310 where F: FnOnce(&mut Builder<'a, 'gcx, 'tcx>) -> BlockAnd<R>
312 debug!("in_scope(extent={:?}, block={:?})", extent, block);
313 self.push_scope(extent);
314 let rv = unpack!(block = f(self));
315 unpack!(block = self.pop_scope(extent, block));
316 debug!("in_scope: exiting extent={:?} block={:?}", extent, block);
320 /// Push a scope onto the stack. You can then build code in this
321 /// scope and call `pop_scope` afterwards. Note that these two
322 /// calls must be paired; using `in_scope` as a convenience
323 /// wrapper maybe preferable.
324 pub fn push_scope(&mut self, extent: (CodeExtent, SourceInfo)) {
325 debug!("push_scope({:?})", extent);
326 let vis_scope = self.visibility_scope;
327 self.scopes.push(Scope {
328 visibility_scope: vis_scope,
330 extent_span: extent.1.span,
331 needs_cleanup: false,
333 cached_generator_drop: None,
334 cached_exits: FxHashMap()
338 /// Pops a scope, which should have extent `extent`, adding any
339 /// drops onto the end of `block` that are needed. This must
340 /// match 1-to-1 with `push_scope`.
341 pub fn pop_scope(&mut self,
342 extent: (CodeExtent, SourceInfo),
343 mut block: BasicBlock)
345 debug!("pop_scope({:?}, {:?})", extent, block);
346 // If we are emitting a `drop` statement, we need to have the cached
347 // diverge cleanup pads ready in case that drop panics.
349 self.scopes.last().unwrap().drops.iter().any(|s| s.kind.may_panic());
351 self.diverge_cleanup();
353 let scope = self.scopes.pop().unwrap();
354 assert_eq!(scope.extent, extent.0);
355 unpack!(block = build_scope_drops(&mut self.cfg,
362 self.cfg.push_end_region(block, extent.1, scope.extent);
367 /// Branch out of `block` to `target`, exiting all scopes up to
368 /// and including `extent`. This will insert whatever drops are
369 /// needed, as well as tracking this exit for the SEME region. See
370 /// module comment for details.
371 pub fn exit_scope(&mut self,
373 extent: (CodeExtent, SourceInfo),
374 mut block: BasicBlock,
375 target: BasicBlock) {
376 debug!("exit_scope(extent={:?}, block={:?}, target={:?})", extent, block, target);
377 let scope_count = 1 + self.scopes.iter().rev().position(|scope| scope.extent == extent.0)
379 span_bug!(span, "extent {:?} does not enclose", extent)
381 let len = self.scopes.len();
382 assert!(scope_count < len, "should not use `exit_scope` to pop ALL scopes");
384 // If we are emitting a `drop` statement, we need to have the cached
385 // diverge cleanup pads ready in case that drop panics.
386 let may_panic = self.scopes[(len - scope_count)..].iter()
387 .any(|s| s.drops.iter().any(|s| s.kind.may_panic()));
389 self.diverge_cleanup();
393 let mut rest = &mut self.scopes[(len - scope_count)..];
394 while let Some((scope, rest_)) = {rest}.split_last_mut() {
396 block = if let Some(&e) = scope.cached_exits.get(&(target, extent.0)) {
397 self.cfg.terminate(block, scope.source_info(span),
398 TerminatorKind::Goto { target: e });
401 let b = self.cfg.start_new_block();
402 self.cfg.terminate(block, scope.source_info(span),
403 TerminatorKind::Goto { target: b });
404 scope.cached_exits.insert((target, extent.0), b);
407 unpack!(block = build_scope_drops(&mut self.cfg,
414 // End all regions for scopes out of which we are breaking.
415 self.cfg.push_end_region(block, extent.1, scope.extent);
418 let scope = &self.scopes[len - scope_count];
419 self.cfg.terminate(block, scope.source_info(span),
420 TerminatorKind::Goto { target: target });
423 /// Creates a path that performs all required cleanup for dropping a generator.
425 /// This path terminates in GeneratorDrop. Returns the start of the path.
426 /// None indicates there’s no cleanup to do at this point.
427 pub fn generator_drop_cleanup(&mut self) -> Option<BasicBlock> {
428 if !self.scopes.iter().any(|scope| scope.needs_cleanup) {
433 self.diverge_cleanup_gen(true);
435 let src_info = self.scopes[0].source_info(self.fn_span);
436 let mut block = self.cfg.start_new_block();
438 let mut rest = &mut self.scopes[..];
440 while let Some((scope, rest_)) = {rest}.split_last_mut() {
442 if !scope.needs_cleanup {
445 block = if let Some(b) = scope.cached_generator_drop {
446 self.cfg.terminate(block, src_info,
447 TerminatorKind::Goto { target: b });
450 let b = self.cfg.start_new_block();
451 scope.cached_generator_drop = Some(b);
452 self.cfg.terminate(block, src_info,
453 TerminatorKind::Goto { target: b });
456 unpack!(block = build_scope_drops(&mut self.cfg,
463 // End all regions for scopes out of which we are breaking.
464 self.cfg.push_end_region(block, src_info, scope.extent);
467 self.cfg.terminate(block, src_info, TerminatorKind::GeneratorDrop);
472 /// Creates a new visibility scope, nested in the current one.
473 pub fn new_visibility_scope(&mut self, span: Span) -> VisibilityScope {
474 let parent = self.visibility_scope;
475 let scope = VisibilityScope::new(self.visibility_scopes.len());
476 self.visibility_scopes.push(VisibilityScopeData {
478 parent_scope: Some(parent),
485 /// Finds the breakable scope for a given label. This is used for
486 /// resolving `break` and `continue`.
487 pub fn find_breakable_scope(&mut self,
490 -> &mut BreakableScope<'tcx> {
491 // find the loop-scope with the correct id
492 self.breakable_scopes.iter_mut()
494 .filter(|breakable_scope| breakable_scope.extent == label)
496 .unwrap_or_else(|| span_bug!(span, "no enclosing breakable scope found"))
499 /// Given a span and the current visibility scope, make a SourceInfo.
500 pub fn source_info(&self, span: Span) -> SourceInfo {
503 scope: self.visibility_scope
507 /// Returns the extent of the scope which should be exited by a
509 pub fn extent_of_return_scope(&self) -> CodeExtent {
510 // The outermost scope (`scopes[0]`) will be the `CallSiteScope`.
511 // We want `scopes[1]`, which is the `ParameterScope`.
512 assert!(self.scopes.len() >= 2);
513 assert!(match self.scopes[1].extent {
514 CodeExtent::ParameterScope(_) => true,
517 self.scopes[1].extent
520 /// Returns the topmost active scope, which is known to be alive until
521 /// the next scope expression.
522 pub fn topmost_scope(&self) -> CodeExtent {
523 self.scopes.last().expect("topmost_scope: no scopes present").extent
526 /// Returns the scope that we should use as the lifetime of an
527 /// operand. Basically, an operand must live until it is consumed.
528 /// This is similar to, but not quite the same as, the temporary
529 /// scope (which can be larger or smaller).
533 /// let x = foo(bar(X, Y));
535 /// We wish to pop the storage for X and Y after `bar()` is
536 /// called, not after the whole `let` is completed.
538 /// As another example, if the second argument diverges:
540 /// foo(Box::new(2), panic!())
542 /// We would allocate the box but then free it on the unwinding
543 /// path; we would also emit a free on the 'success' path from
544 /// panic, but that will turn out to be removed as dead-code.
546 /// When building statics/constants, returns `None` since
547 /// intermediate values do not have to be dropped in that case.
548 pub fn local_scope(&self) -> Option<CodeExtent> {
550 MirSource::Const(_) |
551 MirSource::Static(..) =>
552 // No need to free storage in this context.
555 Some(self.topmost_scope()),
556 MirSource::Promoted(..) |
557 MirSource::GeneratorDrop(..) =>
564 /// Indicates that `lvalue` should be dropped on exit from
566 pub fn schedule_drop(&mut self,
569 lvalue: &Lvalue<'tcx>,
570 lvalue_ty: Ty<'tcx>) {
571 let needs_drop = self.hir.needs_drop(lvalue_ty);
572 let drop_kind = if needs_drop {
573 DropKind::Value { cached_block: CachedBlock::default() }
575 // Only temps and vars need their storage dead.
577 Lvalue::Local(index) if index.index() > self.arg_count => DropKind::Storage,
582 for scope in self.scopes.iter_mut().rev() {
583 let this_scope = scope.extent == extent;
584 // When building drops, we try to cache chains of drops in such a way so these drops
585 // could be reused by the drops which would branch into the cached (already built)
586 // blocks. This, however, means that whenever we add a drop into a scope which already
587 // had some blocks built (and thus, cached) for it, we must invalidate all caches which
588 // might branch into the scope which had a drop just added to it. This is necessary,
589 // because otherwise some other code might use the cache to branch into already built
590 // chain of drops, essentially ignoring the newly added drop.
592 // For example consider there’s two scopes with a drop in each. These are built and
593 // thus the caches are filled:
595 // +--------------------------------------------------------+
596 // | +---------------------------------+ |
597 // | | +--------+ +-------------+ | +---------------+ |
598 // | | | return | <-+ | drop(outer) | <-+ | drop(middle) | |
599 // | | +--------+ +-------------+ | +---------------+ |
600 // | +------------|outer_scope cache|--+ |
601 // +------------------------------|middle_scope cache|------+
603 // Now, a new, inner-most scope is added along with a new drop into both inner-most and
604 // outer-most scopes:
606 // +------------------------------------------------------------+
607 // | +----------------------------------+ |
608 // | | +--------+ +-------------+ | +---------------+ | +-------------+
609 // | | | return | <+ | drop(new) | <-+ | drop(middle) | <--+| drop(inner) |
610 // | | +--------+ | | drop(outer) | | +---------------+ | +-------------+
611 // | | +-+ +-------------+ | |
612 // | +---|invalid outer_scope cache|----+ |
613 // +----=----------------|invalid middle_scope cache|-----------+
615 // If, when adding `drop(new)` we do not invalidate the cached blocks for both
616 // outer_scope and middle_scope, then, when building drops for the inner (right-most)
617 // scope, the old, cached blocks, without `drop(new)` will get used, producing the
620 // The cache and its invalidation for unwind branch is somewhat special. The cache is
621 // per-drop, rather than per scope, which has a several different implications. Adding
622 // a new drop into a scope will not invalidate cached blocks of the prior drops in the
623 // scope. That is true, because none of the already existing drops will have an edge
624 // into a block with the newly added drop.
626 // Note that this code iterates scopes from the inner-most to the outer-most,
627 // invalidating caches of each scope visited. This way bare minimum of the
628 // caches gets invalidated. i.e. if a new drop is added into the middle scope, the
629 // cache of outer scpoe stays intact.
630 let invalidate_unwind = needs_drop && !this_scope;
631 scope.invalidate_cache(invalidate_unwind);
633 if let DropKind::Value { .. } = drop_kind {
634 scope.needs_cleanup = true;
636 let tcx = self.hir.tcx();
637 let extent_span = extent.span(&tcx.hir).unwrap();
638 // Attribute scope exit drops to scope's closing brace
639 let scope_end = extent_span.with_lo(extent_span.hi());
640 scope.drops.push(DropData {
642 location: lvalue.clone(),
648 span_bug!(span, "extent {:?} not in scope to drop {:?}", extent, lvalue);
653 /// Creates a path that performs all required cleanup for unwinding.
655 /// This path terminates in Resume. Returns the start of the path.
656 /// See module comment for more details. None indicates there’s no
657 /// cleanup to do at this point.
658 pub fn diverge_cleanup(&mut self) -> Option<BasicBlock> {
659 self.diverge_cleanup_gen(false)
662 fn diverge_cleanup_gen(&mut self, generator_drop: bool) -> Option<BasicBlock> {
663 if !self.scopes.iter().any(|scope| scope.needs_cleanup) {
666 assert!(!self.scopes.is_empty()); // or `any` above would be false
668 let Builder { ref mut cfg, ref mut scopes,
669 ref mut cached_resume_block, .. } = *self;
671 // Build up the drops in **reverse** order. The end result will
674 // scopes[n] -> scopes[n-1] -> ... -> scopes[0]
676 // However, we build this in **reverse order**. That is, we
677 // process scopes[0], then scopes[1], etc, pointing each one at
678 // the result generates from the one before. Along the way, we
679 // store caches. If everything is cached, we'll just walk right
680 // to left reading the cached results but never created anything.
682 // To start, create the resume terminator.
683 let mut target = if let Some(target) = *cached_resume_block {
686 let resumeblk = cfg.start_new_cleanup_block();
687 cfg.terminate(resumeblk,
688 scopes[0].source_info(self.fn_span),
689 TerminatorKind::Resume);
690 *cached_resume_block = Some(resumeblk);
694 for scope in scopes.iter_mut() {
695 target = build_diverge_scope(cfg, scope.extent_span, scope, target, generator_drop);
700 /// Utility function for *non*-scope code to build their own drops
701 pub fn build_drop(&mut self,
704 location: Lvalue<'tcx>,
705 ty: Ty<'tcx>) -> BlockAnd<()> {
706 if !self.hir.needs_drop(ty) {
709 let source_info = self.source_info(span);
710 let next_target = self.cfg.start_new_block();
711 let diverge_target = self.diverge_cleanup();
712 self.cfg.terminate(block, source_info,
713 TerminatorKind::Drop {
716 unwind: diverge_target,
721 /// Utility function for *non*-scope code to build their own drops
722 pub fn build_drop_and_replace(&mut self,
725 location: Lvalue<'tcx>,
726 value: Operand<'tcx>) -> BlockAnd<()> {
727 let source_info = self.source_info(span);
728 let next_target = self.cfg.start_new_block();
729 let diverge_target = self.diverge_cleanup();
730 self.cfg.terminate(block, source_info,
731 TerminatorKind::DropAndReplace {
735 unwind: diverge_target,
740 /// Create an Assert terminator and return the success block.
741 /// If the boolean condition operand is not the expected value,
742 /// a runtime panic will be caused with the given message.
743 pub fn assert(&mut self, block: BasicBlock,
746 msg: AssertMessage<'tcx>,
749 let source_info = self.source_info(span);
751 let success_block = self.cfg.start_new_block();
752 let cleanup = self.diverge_cleanup();
754 self.cfg.terminate(block, source_info,
755 TerminatorKind::Assert {
759 target: success_block,
767 /// Builds drops for pop_scope and exit_scope.
768 fn build_scope_drops<'tcx>(cfg: &mut CFG<'tcx>,
770 earlier_scopes: &[Scope<'tcx>],
771 mut block: BasicBlock,
773 generator_drop: bool)
775 debug!("build_scope_drops({:?} -> {:?})", block, scope);
776 let mut iter = scope.drops.iter().rev().peekable();
777 while let Some(drop_data) = iter.next() {
778 let source_info = scope.source_info(drop_data.span);
779 match drop_data.kind {
780 DropKind::Value { .. } => {
781 // Try to find the next block with its cached block
782 // for us to diverge into in case the drop panics.
783 let on_diverge = iter.peek().iter().filter_map(|dd| {
785 DropKind::Value { cached_block } => {
786 let result = cached_block.get(generator_drop);
787 if result.is_none() {
788 span_bug!(drop_data.span, "cached block not present?")
792 DropKind::Storage => None
795 // If there’s no `cached_block`s within current scope,
796 // we must look for one in the enclosing scope.
797 let on_diverge = on_diverge.or_else(|| {
798 earlier_scopes.iter().rev().flat_map(|s| s.cached_block(generator_drop)).next()
800 let next = cfg.start_new_block();
801 cfg.terminate(block, source_info, TerminatorKind::Drop {
802 location: drop_data.location.clone(),
808 DropKind::Storage => {}
811 // We do not need to emit StorageDead for generator drops
816 // Drop the storage for both value and storage drops.
817 // Only temps and vars need their storage dead.
818 match drop_data.location {
819 Lvalue::Local(index) if index.index() > arg_count => {
820 cfg.push(block, Statement {
822 kind: StatementKind::StorageDead(drop_data.location.clone())
831 fn build_diverge_scope<'a, 'gcx, 'tcx>(cfg: &mut CFG<'tcx>,
833 scope: &mut Scope<'tcx>,
834 mut target: BasicBlock,
835 generator_drop: bool)
838 // Build up the drops in **reverse** order. The end result will
841 // [EndRegion Block] -> [drops[n]] -...-> [drops[0]] -> [Free] -> [target]
843 // +---------------------------------------------------------+
846 // The code in this function reads from right to left. At each
847 // point, we check for cached blocks representing the
848 // remainder. If everything is cached, we'll just walk right to
849 // left reading the cached results but never create anything.
851 let visibility_scope = scope.visibility_scope;
852 let source_info = |span| SourceInfo {
854 scope: visibility_scope
857 // Next, build up the drops. Here we iterate the vector in
858 // *forward* order, so that we generate drops[0] first (right to
859 // left in diagram above).
860 for (j, drop_data) in scope.drops.iter_mut().enumerate() {
861 debug!("build_diverge_scope drop_data[{}]: {:?}", j, drop_data);
862 // Only full value drops are emitted in the diverging path,
865 // Note: This may not actually be what we desire (are we
866 // "freeing" stack storage as we unwind, or merely observing a
867 // frozen stack)? In particular, the intent may have been to
868 // match the behavior of clang, but on inspection eddyb says
869 // this is not what clang does.
870 let cached_block = match drop_data.kind {
871 DropKind::Value { ref mut cached_block } => cached_block.ref_mut(generator_drop),
872 DropKind::Storage => continue
874 target = if let Some(cached_block) = *cached_block {
877 let block = cfg.start_new_cleanup_block();
878 cfg.terminate(block, source_info(drop_data.span),
879 TerminatorKind::Drop {
880 location: drop_data.location.clone(),
884 *cached_block = Some(block);
889 // Finally, push the EndRegion block, used by mir-borrowck. (Block
890 // becomes trivial goto after pass that removes all EndRegions.)
892 let block = cfg.start_new_cleanup_block();
893 cfg.push_end_region(block, source_info(span), scope.extent);
894 cfg.terminate(block, source_info(span), TerminatorKind::Goto { target: target });