]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/build/scope.rs
Merge remote-tracking branch 'origin/master' into gen
[rust.git] / src / librustc_mir / build / scope.rs
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.
4 //
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.
10
11 /*!
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
15 `CodeExtent`.
16
17 ### SEME Regions
18
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.
25
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.
31
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.
38
39 ### Drops
40
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:
49
50 ```
51 # let cond = true;
52 loop {
53     let x = ..;
54     if cond { break; }
55     let y = ..;
56 }
57 ```
58
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`.
63
64 ### Early exit
65
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
72 also add all drops.
73
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
80 result.
81
82 ### Loop scopes
83
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`
86 should go to.
87
88 */
89
90 use build::{BlockAnd, BlockAndExtension, Builder, CFG};
91 use rustc::middle::region::CodeExtent;
92 use rustc::ty::Ty;
93 use rustc::mir::*;
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;
98
99 #[derive(Debug)]
100 pub struct Scope<'tcx> {
101     /// The visibility scope this scope was created in.
102     visibility_scope: VisibilityScope,
103
104     /// the extent of this scope within source code.
105     extent: CodeExtent,
106
107     /// the span of that extent
108     extent_span: Span,
109
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
119     needs_cleanup: bool,
120
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>>,
126
127     /// The cache for drop chain on “normal” exit into a particular BasicBlock.
128     cached_exits: FxHashMap<(BasicBlock, CodeExtent), BasicBlock>,
129
130     /// The cache for drop chain on "generator drop" exit.
131     cached_generator_drop: Option<BasicBlock>,
132 }
133
134 #[derive(Debug)]
135 struct DropData<'tcx> {
136     /// span where drop obligation was incurred (typically where lvalue was declared)
137     span: Span,
138
139     /// lvalue to drop
140     location: Lvalue<'tcx>,
141
142     /// Whether this is a full value Drop, or just a StorageDead.
143     kind: DropKind
144 }
145
146 #[derive(Debug, Default, Clone, Copy)]
147 struct CachedBlock {
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
151     /// array)
152     unwind: Option<BasicBlock>,
153
154     /// The cached block for unwinds during cleanups-on-generator-drop path
155     generator_drop: Option<BasicBlock>,
156 }
157
158 #[derive(Debug)]
159 enum DropKind {
160     Value {
161         cached_block: CachedBlock,
162     },
163     Storage
164 }
165
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>,
178 }
179
180 impl CachedBlock {
181     fn invalidate(&mut self) {
182         self.generator_drop = None;
183         self.unwind = None;
184     }
185
186     fn get(&self, generator_drop: bool) -> Option<BasicBlock> {
187         if generator_drop {
188             self.generator_drop
189         } else {
190             self.unwind
191         }
192     }
193
194     fn ref_mut(&mut self, generator_drop: bool) -> &mut Option<BasicBlock> {
195         if generator_drop {
196             &mut self.generator_drop
197         } else {
198             &mut self.unwind
199         }
200     }
201 }
202
203 impl DropKind {
204     fn may_panic(&self) -> bool {
205         match *self {
206             DropKind::Value { .. } => true,
207             DropKind::Storage => false
208         }
209     }
210 }
211
212 impl<'tcx> Scope<'tcx> {
213     /// Invalidate all the cached blocks in the scope.
214     ///
215     /// Should always be run for all inner scopes when a drop is pushed into some scope enclosing a
216     /// larger extent of code.
217     ///
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();
225             }
226         }
227     }
228
229     /// Returns the cached entrypoint for diverging exit from this scope.
230     ///
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| {
235             match data.kind {
236                 DropKind::Value { cached_block } => {
237                     Some(cached_block.get(generator_drop))
238                 }
239                 DropKind::Storage => None
240             }
241         });
242         if let Some(cached_block) = drops.next() {
243             Some(cached_block.expect("drop cache is not filled"))
244         } else {
245             None
246         }
247     }
248
249     /// Given a span and this scope's visibility scope, make a SourceInfo.
250     fn source_info(&self, span: Span) -> SourceInfo {
251         SourceInfo {
252             span,
253             scope: self.visibility_scope
254         }
255     }
256 }
257
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.
263     ///
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>,
269                                     f: F) -> R
270         where F: FnOnce(&mut Builder<'a, 'gcx, 'tcx>) -> R
271     {
272         let extent = self.topmost_scope();
273         let scope = BreakableScope {
274             extent,
275             continue_block: loop_block,
276             break_block,
277             break_destination,
278         };
279         self.breakable_scopes.push(scope);
280         let res = f(self);
281         let breakable_scope = self.breakable_scopes.pop().unwrap();
282         assert!(breakable_scope.extent == extent);
283         res
284     }
285
286     pub fn in_opt_scope<F, R>(&mut self,
287                               opt_extent: Option<(CodeExtent, SourceInfo)>,
288                               mut block: BasicBlock,
289                               f: F)
290                               -> BlockAnd<R>
291         where F: FnOnce(&mut Builder<'a, 'gcx, 'tcx>) -> BlockAnd<R>
292     {
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));
298         }
299         debug!("in_scope: exiting opt_extent={:?} block={:?}", opt_extent, block);
300         block.and(rv)
301     }
302
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,
308                           f: F)
309                           -> BlockAnd<R>
310         where F: FnOnce(&mut Builder<'a, 'gcx, 'tcx>) -> BlockAnd<R>
311     {
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);
317         block.and(rv)
318     }
319
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,
329             extent: extent.0,
330             extent_span: extent.1.span,
331             needs_cleanup: false,
332             drops: vec![],
333             cached_generator_drop: None,
334             cached_exits: FxHashMap()
335         });
336     }
337
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)
344                      -> BlockAnd<()> {
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.
348         let may_panic =
349             self.scopes.last().unwrap().drops.iter().any(|s| s.kind.may_panic());
350         if may_panic {
351             self.diverge_cleanup();
352         }
353         let scope = self.scopes.pop().unwrap();
354         assert_eq!(scope.extent, extent.0);
355         unpack!(block = build_scope_drops(&mut self.cfg,
356                                           &scope,
357                                           &self.scopes,
358                                           block,
359                                           self.arg_count,
360                                           false));
361
362         self.cfg.push_end_region(block, extent.1, scope.extent);
363         block.unit()
364     }
365
366
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,
372                       span: Span,
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)
378                                                       .unwrap_or_else(||{
379             span_bug!(span, "extent {:?} does not enclose", extent)
380         });
381         let len = self.scopes.len();
382         assert!(scope_count < len, "should not use `exit_scope` to pop ALL scopes");
383
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()));
388         if may_panic {
389             self.diverge_cleanup();
390         }
391
392         {
393         let mut rest = &mut self.scopes[(len - scope_count)..];
394         while let Some((scope, rest_)) = {rest}.split_last_mut() {
395             rest = rest_;
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 });
399                 return;
400             } else {
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);
405                 b
406             };
407             unpack!(block = build_scope_drops(&mut self.cfg,
408                                               scope,
409                                               rest,
410                                               block,
411                                               self.arg_count,
412                                               false));
413
414             // End all regions for scopes out of which we are breaking.
415             self.cfg.push_end_region(block, extent.1, scope.extent);
416         }
417         }
418         let scope = &self.scopes[len - scope_count];
419         self.cfg.terminate(block, scope.source_info(span),
420                            TerminatorKind::Goto { target: target });
421     }
422
423     /// Creates a path that performs all required cleanup for dropping a generator.
424     ///
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) {
429             return None;
430         }
431
432         // Fill in the cache
433         self.diverge_cleanup_gen(true);
434
435         let src_info = self.scopes[0].source_info(self.fn_span);
436         let mut block = self.cfg.start_new_block();
437         let result = block;
438         let mut rest = &mut self.scopes[..];
439
440         while let Some((scope, rest_)) = {rest}.split_last_mut() {
441             rest = rest_;
442             if !scope.needs_cleanup {
443                 continue;
444             }
445             block = if let Some(b) = scope.cached_generator_drop {
446                 self.cfg.terminate(block, src_info,
447                                    TerminatorKind::Goto { target: b });
448                 return Some(result);
449             } else {
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 });
454                 b
455             };
456             unpack!(block = build_scope_drops(&mut self.cfg,
457                                               scope,
458                                               rest,
459                                               block,
460                                               self.arg_count,
461                                               true));
462
463             // End all regions for scopes out of which we are breaking.
464             self.cfg.push_end_region(block, src_info, scope.extent);
465         }
466
467         self.cfg.terminate(block, src_info, TerminatorKind::GeneratorDrop);
468
469         Some(result)
470     }
471
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 {
477             span,
478             parent_scope: Some(parent),
479         });
480         scope
481     }
482
483     // Finding scopes
484     // ==============
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,
488                            span: Span,
489                            label: CodeExtent)
490                            -> &mut BreakableScope<'tcx> {
491         // find the loop-scope with the correct id
492         self.breakable_scopes.iter_mut()
493             .rev()
494             .filter(|breakable_scope| breakable_scope.extent == label)
495             .next()
496             .unwrap_or_else(|| span_bug!(span, "no enclosing breakable scope found"))
497     }
498
499     /// Given a span and the current visibility scope, make a SourceInfo.
500     pub fn source_info(&self, span: Span) -> SourceInfo {
501         SourceInfo {
502             span,
503             scope: self.visibility_scope
504         }
505     }
506
507     /// Returns the extent of the scope which should be exited by a
508     /// return.
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,
515             _ => false,
516         });
517         self.scopes[1].extent
518     }
519
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
524     }
525
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).
530     ///
531     /// Consider:
532     ///
533     ///     let x = foo(bar(X, Y));
534     ///
535     /// We wish to pop the storage for X and Y after `bar()` is
536     /// called, not after the whole `let` is completed.
537     ///
538     /// As another example, if the second argument diverges:
539     ///
540     ///     foo(Box::new(2), panic!())
541     ///
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.
545     ///
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> {
549         match self.hir.src {
550             MirSource::Const(_) |
551             MirSource::Static(..) =>
552                 // No need to free storage in this context.
553                 None,
554             MirSource::Fn(_) =>
555                 Some(self.topmost_scope()),
556             MirSource::Promoted(..) |
557             MirSource::GeneratorDrop(..) =>
558                 bug!(),
559         }
560     }
561
562     // Scheduling drops
563     // ================
564     /// Indicates that `lvalue` should be dropped on exit from
565     /// `extent`.
566     pub fn schedule_drop(&mut self,
567                          span: Span,
568                          extent: CodeExtent,
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() }
574         } else {
575             // Only temps and vars need their storage dead.
576             match *lvalue {
577                 Lvalue::Local(index) if index.index() > self.arg_count => DropKind::Storage,
578                 _ => return
579             }
580         };
581
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.
591             //
592             // For example consider there’s two scopes with a drop in each. These are built and
593             // thus the caches are filled:
594             //
595             // +--------------------------------------------------------+
596             // | +---------------------------------+                    |
597             // | | +--------+     +-------------+  |  +---------------+ |
598             // | | | return | <-+ | drop(outer) | <-+ |  drop(middle) | |
599             // | | +--------+     +-------------+  |  +---------------+ |
600             // | +------------|outer_scope cache|--+                    |
601             // +------------------------------|middle_scope cache|------+
602             //
603             // Now, a new, inner-most scope is added along with a new drop into both inner-most and
604             // outer-most scopes:
605             //
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|-----------+
614             //
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
618             // wrong results.
619             //
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.
625             //
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);
632             if this_scope {
633                 if let DropKind::Value { .. } = drop_kind {
634                     scope.needs_cleanup = true;
635                 }
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 = Span { lo: extent_span.hi, .. extent_span};
640                 scope.drops.push(DropData {
641                     span: scope_end,
642                     location: lvalue.clone(),
643                     kind: drop_kind
644                 });
645                 return;
646             }
647         }
648         span_bug!(span, "extent {:?} not in scope to drop {:?}", extent, lvalue);
649     }
650
651     // Other
652     // =====
653     /// Creates a path that performs all required cleanup for unwinding.
654     ///
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)
660     }
661
662     fn diverge_cleanup_gen(&mut self, generator_drop: bool) -> Option<BasicBlock> {
663         if !self.scopes.iter().any(|scope| scope.needs_cleanup) {
664             return None;
665         }
666         assert!(!self.scopes.is_empty()); // or `any` above would be false
667
668         let Builder { ref mut cfg, ref mut scopes,
669                       ref mut cached_resume_block, .. } = *self;
670
671         // Build up the drops in **reverse** order. The end result will
672         // look like:
673         //
674         //    scopes[n] -> scopes[n-1] -> ... -> scopes[0]
675         //
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.
681
682         // To start, create the resume terminator.
683         let mut target = if let Some(target) = *cached_resume_block {
684             target
685         } else {
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);
691             resumeblk
692         };
693
694         for scope in scopes.iter_mut() {
695             target = build_diverge_scope(cfg, scope.extent_span, scope, target, generator_drop);
696         }
697         Some(target)
698     }
699
700     /// Utility function for *non*-scope code to build their own drops
701     pub fn build_drop(&mut self,
702                       block: BasicBlock,
703                       span: Span,
704                       location: Lvalue<'tcx>,
705                       ty: Ty<'tcx>) -> BlockAnd<()> {
706         if !self.hir.needs_drop(ty) {
707             return block.unit();
708         }
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 {
714                                location,
715                                target: next_target,
716                                unwind: diverge_target,
717                            });
718         next_target.unit()
719     }
720
721     /// Utility function for *non*-scope code to build their own drops
722     pub fn build_drop_and_replace(&mut self,
723                                   block: BasicBlock,
724                                   span: Span,
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 {
732                                location,
733                                value,
734                                target: next_target,
735                                unwind: diverge_target,
736                            });
737         next_target.unit()
738     }
739
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,
744                   cond: Operand<'tcx>,
745                   expected: bool,
746                   msg: AssertMessage<'tcx>,
747                   span: Span)
748                   -> BasicBlock {
749         let source_info = self.source_info(span);
750
751         let success_block = self.cfg.start_new_block();
752         let cleanup = self.diverge_cleanup();
753
754         self.cfg.terminate(block, source_info,
755                            TerminatorKind::Assert {
756                                cond,
757                                expected,
758                                msg,
759                                target: success_block,
760                                cleanup,
761                            });
762
763         success_block
764     }
765 }
766
767 /// Builds drops for pop_scope and exit_scope.
768 fn build_scope_drops<'tcx>(cfg: &mut CFG<'tcx>,
769                            scope: &Scope<'tcx>,
770                            earlier_scopes: &[Scope<'tcx>],
771                            mut block: BasicBlock,
772                            arg_count: usize,
773                            generator_drop: bool)
774                            -> BlockAnd<()> {
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| {
784                     match dd.kind {
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?")
789                             }
790                             result
791                         },
792                         DropKind::Storage => None
793                     }
794                 }).next();
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()
799                 });
800                 let next = cfg.start_new_block();
801                 cfg.terminate(block, source_info, TerminatorKind::Drop {
802                     location: drop_data.location.clone(),
803                     target: next,
804                     unwind: on_diverge
805                 });
806                 block = next;
807             }
808             DropKind::Storage => {}
809         }
810
811         // We do not need to emit StorageDead for generator drops
812         if generator_drop {
813             continue
814         }
815
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 {
821                     source_info,
822                     kind: StatementKind::StorageDead(drop_data.location.clone())
823                 });
824             }
825             _ => continue
826         }
827     }
828     block.unit()
829 }
830
831 fn build_diverge_scope<'a, 'gcx, 'tcx>(cfg: &mut CFG<'tcx>,
832                                        span: Span,
833                                        scope: &mut Scope<'tcx>,
834                                        mut target: BasicBlock,
835                                        generator_drop: bool)
836                                        -> BasicBlock
837 {
838     // Build up the drops in **reverse** order. The end result will
839     // look like:
840     //
841     //    [EndRegion Block] -> [drops[n]] -...-> [drops[0]] -> [Free] -> [target]
842     //    |                                                         |
843     //    +---------------------------------------------------------+
844     //     code for scope
845     //
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.
850
851     let visibility_scope = scope.visibility_scope;
852     let source_info = |span| SourceInfo {
853         span,
854         scope: visibility_scope
855     };
856
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,
863         // not StorageDead.
864         //
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
873         };
874         target = if let Some(cached_block) = *cached_block {
875             cached_block
876         } else {
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(),
881                               target,
882                               unwind: None
883                           });
884             *cached_block = Some(block);
885             block
886         };
887     }
888
889     // Finally, push the EndRegion block, used by mir-borrowck. (Block
890     // becomes trivial goto after pass that removes all EndRegions.)
891     {
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 });
895         target = block
896     }
897
898     target
899 }