]> git.lizzy.rs Git - rust.git/blob - src/librustc/mir/mod.rs
Stabilize feature from_ref
[rust.git] / src / librustc / mir / mod.rs
1 // Copyright 2014 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 //! MIR datatypes and passes. See the [rustc guide] for more info.
12 //!
13 //! [rustc guide]: https://rust-lang-nursery.github.io/rustc-guide/mir.html
14
15 use graphviz::IntoCow;
16 use middle::region;
17 use rustc_data_structures::sync::{Lrc};
18 use rustc_data_structures::indexed_vec::{IndexVec, Idx};
19 use rustc_data_structures::control_flow_graph::dominators::{Dominators, dominators};
20 use rustc_data_structures::control_flow_graph::{GraphPredecessors, GraphSuccessors};
21 use rustc_data_structures::control_flow_graph::ControlFlowGraph;
22 use rustc_data_structures::small_vec::SmallVec;
23 use rustc_serialize as serialize;
24 use hir::def::CtorKind;
25 use hir::def_id::DefId;
26 use mir::visit::MirVisitable;
27 use mir::interpret::{Value, PrimVal, EvalErrorKind};
28 use ty::subst::{Subst, Substs};
29 use ty::{self, AdtDef, CanonicalTy, ClosureSubsts, GeneratorSubsts, Region, Ty, TyCtxt};
30 use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
31 use util::ppaux;
32 use std::slice;
33 use hir::{self, InlineAsm};
34 use std::borrow::{Cow};
35 use rustc_data_structures::sync::ReadGuard;
36 use std::fmt::{self, Debug, Formatter, Write};
37 use std::{iter, mem, option, u32};
38 use std::ops::{Index, IndexMut};
39 use std::vec::IntoIter;
40 use syntax::ast::{self, Name};
41 use syntax::symbol::InternedString;
42 use syntax_pos::{Span, DUMMY_SP};
43 use rustc_apfloat::ieee::{Single, Double};
44 use rustc_apfloat::Float;
45
46 pub use mir::interpret::AssertMessage;
47
48 mod cache;
49 pub mod tcx;
50 pub mod visit;
51 pub mod traversal;
52 pub mod interpret;
53 pub mod mono;
54
55 /// Types for locals
56 type LocalDecls<'tcx> = IndexVec<Local, LocalDecl<'tcx>>;
57
58 pub trait HasLocalDecls<'tcx> {
59     fn local_decls(&self) -> &LocalDecls<'tcx>;
60 }
61
62 impl<'tcx> HasLocalDecls<'tcx> for LocalDecls<'tcx> {
63     fn local_decls(&self) -> &LocalDecls<'tcx> {
64         self
65     }
66 }
67
68 impl<'tcx> HasLocalDecls<'tcx> for Mir<'tcx> {
69     fn local_decls(&self) -> &LocalDecls<'tcx> {
70         &self.local_decls
71     }
72 }
73
74 /// Lowered representation of a single function.
75 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
76 pub struct Mir<'tcx> {
77     /// List of basic blocks. References to basic block use a newtyped index type `BasicBlock`
78     /// that indexes into this vector.
79     basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
80
81     /// List of visibility (lexical) scopes; these are referenced by statements
82     /// and used (eventually) for debuginfo. Indexed by a `VisibilityScope`.
83     pub visibility_scopes: IndexVec<VisibilityScope, VisibilityScopeData>,
84
85     /// Crate-local information for each visibility scope, that can't (and
86     /// needn't) be tracked across crates.
87     pub visibility_scope_info: ClearCrossCrate<IndexVec<VisibilityScope, VisibilityScopeInfo>>,
88
89     /// Rvalues promoted from this function, such as borrows of constants.
90     /// Each of them is the Mir of a constant with the fn's type parameters
91     /// in scope, but a separate set of locals.
92     pub promoted: IndexVec<Promoted, Mir<'tcx>>,
93
94     /// Yield type of the function, if it is a generator.
95     pub yield_ty: Option<Ty<'tcx>>,
96
97     /// Generator drop glue
98     pub generator_drop: Option<Box<Mir<'tcx>>>,
99
100     /// The layout of a generator. Produced by the state transformation.
101     pub generator_layout: Option<GeneratorLayout<'tcx>>,
102
103     /// Declarations of locals.
104     ///
105     /// The first local is the return value pointer, followed by `arg_count`
106     /// locals for the function arguments, followed by any user-declared
107     /// variables and temporaries.
108     pub local_decls: LocalDecls<'tcx>,
109
110     /// Number of arguments this function takes.
111     ///
112     /// Starting at local 1, `arg_count` locals will be provided by the caller
113     /// and can be assumed to be initialized.
114     ///
115     /// If this MIR was built for a constant, this will be 0.
116     pub arg_count: usize,
117
118     /// Names and capture modes of all the closure upvars, assuming
119     /// the first argument is either the closure or a reference to it.
120     pub upvar_decls: Vec<UpvarDecl>,
121
122     /// Mark an argument local (which must be a tuple) as getting passed as
123     /// its individual components at the LLVM level.
124     ///
125     /// This is used for the "rust-call" ABI.
126     pub spread_arg: Option<Local>,
127
128     /// A span representing this MIR, for error reporting
129     pub span: Span,
130
131     /// A cache for various calculations
132     cache: cache::Cache
133 }
134
135 /// where execution begins
136 pub const START_BLOCK: BasicBlock = BasicBlock(0);
137
138 impl<'tcx> Mir<'tcx> {
139     pub fn new(basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
140                visibility_scopes: IndexVec<VisibilityScope, VisibilityScopeData>,
141                visibility_scope_info: ClearCrossCrate<IndexVec<VisibilityScope,
142                                                                VisibilityScopeInfo>>,
143                promoted: IndexVec<Promoted, Mir<'tcx>>,
144                yield_ty: Option<Ty<'tcx>>,
145                local_decls: IndexVec<Local, LocalDecl<'tcx>>,
146                arg_count: usize,
147                upvar_decls: Vec<UpvarDecl>,
148                span: Span) -> Self
149     {
150         // We need `arg_count` locals, and one for the return place
151         assert!(local_decls.len() >= arg_count + 1,
152             "expected at least {} locals, got {}", arg_count + 1, local_decls.len());
153
154         Mir {
155             basic_blocks,
156             visibility_scopes,
157             visibility_scope_info,
158             promoted,
159             yield_ty,
160             generator_drop: None,
161             generator_layout: None,
162             local_decls,
163             arg_count,
164             upvar_decls,
165             spread_arg: None,
166             span,
167             cache: cache::Cache::new()
168         }
169     }
170
171     #[inline]
172     pub fn basic_blocks(&self) -> &IndexVec<BasicBlock, BasicBlockData<'tcx>> {
173         &self.basic_blocks
174     }
175
176     #[inline]
177     pub fn basic_blocks_mut(&mut self) -> &mut IndexVec<BasicBlock, BasicBlockData<'tcx>> {
178         self.cache.invalidate();
179         &mut self.basic_blocks
180     }
181
182     #[inline]
183     pub fn basic_blocks_and_local_decls_mut(&mut self) -> (
184         &mut IndexVec<BasicBlock, BasicBlockData<'tcx>>,
185         &mut LocalDecls<'tcx>,
186     ) {
187         self.cache.invalidate();
188         (&mut self.basic_blocks, &mut self.local_decls)
189     }
190
191     #[inline]
192     pub fn predecessors(&self) -> ReadGuard<IndexVec<BasicBlock, Vec<BasicBlock>>> {
193         self.cache.predecessors(self)
194     }
195
196     #[inline]
197     pub fn predecessors_for(&self, bb: BasicBlock) -> ReadGuard<Vec<BasicBlock>> {
198         ReadGuard::map(self.predecessors(), |p| &p[bb])
199     }
200
201     #[inline]
202     pub fn dominators(&self) -> Dominators<BasicBlock> {
203         dominators(self)
204     }
205
206     #[inline]
207     pub fn local_kind(&self, local: Local) -> LocalKind {
208         let index = local.0 as usize;
209         if index == 0 {
210             debug_assert!(self.local_decls[local].mutability == Mutability::Mut,
211                           "return place should be mutable");
212
213             LocalKind::ReturnPointer
214         } else if index < self.arg_count + 1 {
215             LocalKind::Arg
216         } else if self.local_decls[local].name.is_some() {
217             LocalKind::Var
218         } else {
219             debug_assert!(self.local_decls[local].mutability == Mutability::Mut,
220                           "temp should be mutable");
221
222             LocalKind::Temp
223         }
224     }
225
226     /// Returns an iterator over all temporaries.
227     #[inline]
228     pub fn temps_iter<'a>(&'a self) -> impl Iterator<Item=Local> + 'a {
229         (self.arg_count+1..self.local_decls.len()).filter_map(move |index| {
230             let local = Local::new(index);
231             if self.local_decls[local].is_user_variable {
232                 None
233             } else {
234                 Some(local)
235             }
236         })
237     }
238
239     /// Returns an iterator over all user-declared locals.
240     #[inline]
241     pub fn vars_iter<'a>(&'a self) -> impl Iterator<Item=Local> + 'a {
242         (self.arg_count+1..self.local_decls.len()).filter_map(move |index| {
243             let local = Local::new(index);
244             if self.local_decls[local].is_user_variable {
245                 Some(local)
246             } else {
247                 None
248             }
249         })
250     }
251
252     /// Returns an iterator over all user-declared mutable arguments and locals.
253     #[inline]
254     pub fn mut_vars_and_args_iter<'a>(&'a self) -> impl Iterator<Item=Local> + 'a {
255         (1..self.local_decls.len()).filter_map(move |index| {
256             let local = Local::new(index);
257             let decl = &self.local_decls[local];
258             if (decl.is_user_variable || index < self.arg_count + 1)
259                && decl.mutability == Mutability::Mut
260             {
261                 Some(local)
262             } else {
263                 None
264             }
265         })
266     }
267
268     /// Returns an iterator over all function arguments.
269     #[inline]
270     pub fn args_iter(&self) -> impl Iterator<Item=Local> {
271         let arg_count = self.arg_count;
272         (1..arg_count+1).map(Local::new)
273     }
274
275     /// Returns an iterator over all user-defined variables and compiler-generated temporaries (all
276     /// locals that are neither arguments nor the return place).
277     #[inline]
278     pub fn vars_and_temps_iter(&self) -> impl Iterator<Item=Local> {
279         let arg_count = self.arg_count;
280         let local_count = self.local_decls.len();
281         (arg_count+1..local_count).map(Local::new)
282     }
283
284     /// Changes a statement to a nop. This is both faster than deleting instructions and avoids
285     /// invalidating statement indices in `Location`s.
286     pub fn make_statement_nop(&mut self, location: Location) {
287         let block = &mut self[location.block];
288         debug_assert!(location.statement_index < block.statements.len());
289         block.statements[location.statement_index].make_nop()
290     }
291
292     /// Returns the source info associated with `location`.
293     pub fn source_info(&self, location: Location) -> &SourceInfo {
294         let block = &self[location.block];
295         let stmts = &block.statements;
296         let idx = location.statement_index;
297         if idx < stmts.len() {
298             &stmts[idx].source_info
299         } else {
300             assert!(idx == stmts.len());
301             &block.terminator().source_info
302         }
303     }
304
305     /// Return the return type, it always return first element from `local_decls` array
306     pub fn return_ty(&self) -> Ty<'tcx> {
307         self.local_decls[RETURN_PLACE].ty
308     }
309 }
310
311 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
312 pub struct VisibilityScopeInfo {
313     /// A NodeId with lint levels equivalent to this scope's lint levels.
314     pub lint_root: ast::NodeId,
315     /// The unsafe block that contains this node.
316     pub safety: Safety,
317 }
318
319 #[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
320 pub enum Safety {
321     Safe,
322     /// Unsafe because of a PushUnsafeBlock
323     BuiltinUnsafe,
324     /// Unsafe because of an unsafe fn
325     FnUnsafe,
326     /// Unsafe because of an `unsafe` block
327     ExplicitUnsafe(ast::NodeId)
328 }
329
330 impl_stable_hash_for!(struct Mir<'tcx> {
331     basic_blocks,
332     visibility_scopes,
333     visibility_scope_info,
334     promoted,
335     yield_ty,
336     generator_drop,
337     generator_layout,
338     local_decls,
339     arg_count,
340     upvar_decls,
341     spread_arg,
342     span,
343     cache
344 });
345
346 impl<'tcx> Index<BasicBlock> for Mir<'tcx> {
347     type Output = BasicBlockData<'tcx>;
348
349     #[inline]
350     fn index(&self, index: BasicBlock) -> &BasicBlockData<'tcx> {
351         &self.basic_blocks()[index]
352     }
353 }
354
355 impl<'tcx> IndexMut<BasicBlock> for Mir<'tcx> {
356     #[inline]
357     fn index_mut(&mut self, index: BasicBlock) -> &mut BasicBlockData<'tcx> {
358         &mut self.basic_blocks_mut()[index]
359     }
360 }
361
362 #[derive(Clone, Debug)]
363 pub enum ClearCrossCrate<T> {
364     Clear,
365     Set(T)
366 }
367
368 impl<T: serialize::Encodable> serialize::UseSpecializedEncodable for ClearCrossCrate<T> {}
369 impl<T: serialize::Decodable> serialize::UseSpecializedDecodable for ClearCrossCrate<T> {}
370
371 /// Grouped information about the source code origin of a MIR entity.
372 /// Intended to be inspected by diagnostics and debuginfo.
373 /// Most passes can work with it as a whole, within a single function.
374 #[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
375 pub struct SourceInfo {
376     /// Source span for the AST pertaining to this MIR entity.
377     pub span: Span,
378
379     /// The lexical visibility scope, i.e. which bindings can be seen.
380     pub scope: VisibilityScope
381 }
382
383 ///////////////////////////////////////////////////////////////////////////
384 // Mutability and borrow kinds
385
386 #[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
387 pub enum Mutability {
388     Mut,
389     Not,
390 }
391
392 #[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
393 pub enum BorrowKind {
394     /// Data must be immutable and is aliasable.
395     Shared,
396
397     /// Data must be immutable but not aliasable.  This kind of borrow
398     /// cannot currently be expressed by the user and is used only in
399     /// implicit closure bindings. It is needed when you the closure
400     /// is borrowing or mutating a mutable referent, e.g.:
401     ///
402     ///    let x: &mut isize = ...;
403     ///    let y = || *x += 5;
404     ///
405     /// If we were to try to translate this closure into a more explicit
406     /// form, we'd encounter an error with the code as written:
407     ///
408     ///    struct Env { x: & &mut isize }
409     ///    let x: &mut isize = ...;
410     ///    let y = (&mut Env { &x }, fn_ptr);  // Closure is pair of env and fn
411     ///    fn fn_ptr(env: &mut Env) { **env.x += 5; }
412     ///
413     /// This is then illegal because you cannot mutate a `&mut` found
414     /// in an aliasable location. To solve, you'd have to translate with
415     /// an `&mut` borrow:
416     ///
417     ///    struct Env { x: & &mut isize }
418     ///    let x: &mut isize = ...;
419     ///    let y = (&mut Env { &mut x }, fn_ptr); // changed from &x to &mut x
420     ///    fn fn_ptr(env: &mut Env) { **env.x += 5; }
421     ///
422     /// Now the assignment to `**env.x` is legal, but creating a
423     /// mutable pointer to `x` is not because `x` is not mutable. We
424     /// could fix this by declaring `x` as `let mut x`. This is ok in
425     /// user code, if awkward, but extra weird for closures, since the
426     /// borrow is hidden.
427     ///
428     /// So we introduce a "unique imm" borrow -- the referent is
429     /// immutable, but not aliasable. This solves the problem. For
430     /// simplicity, we don't give users the way to express this
431     /// borrow, it's just used when translating closures.
432     Unique,
433
434     /// Data is mutable and not aliasable.
435     Mut {
436         /// True if this borrow arose from method-call auto-ref
437         /// (i.e. `adjustment::Adjust::Borrow`)
438         allow_two_phase_borrow: bool
439     }
440 }
441
442 impl BorrowKind {
443     pub fn allows_two_phase_borrow(&self) -> bool {
444         match *self {
445             BorrowKind::Shared | BorrowKind::Unique => false,
446             BorrowKind::Mut { allow_two_phase_borrow } => allow_two_phase_borrow,
447         }
448     }
449 }
450
451 ///////////////////////////////////////////////////////////////////////////
452 // Variables and temps
453
454 newtype_index!(Local
455     {
456         DEBUG_FORMAT = "_{}",
457         const RETURN_PLACE = 0,
458     });
459
460 /// Classifies locals into categories. See `Mir::local_kind`.
461 #[derive(PartialEq, Eq, Debug)]
462 pub enum LocalKind {
463     /// User-declared variable binding
464     Var,
465     /// Compiler-introduced temporary
466     Temp,
467     /// Function argument
468     Arg,
469     /// Location of function's return value
470     ReturnPointer,
471 }
472
473 /// A MIR local.
474 ///
475 /// This can be a binding declared by the user, a temporary inserted by the compiler, a function
476 /// argument, or the return place.
477 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
478 pub struct LocalDecl<'tcx> {
479     /// `let mut x` vs `let x`.
480     ///
481     /// Temporaries and the return place are always mutable.
482     pub mutability: Mutability,
483
484     /// True if this corresponds to a user-declared local variable.
485     pub is_user_variable: bool,
486
487     /// True if this is an internal local
488     ///
489     /// These locals are not based on types in the source code and are only used
490     /// for a few desugarings at the moment.
491     ///
492     /// The generator transformation will sanity check the locals which are live
493     /// across a suspension point against the type components of the generator
494     /// which type checking knows are live across a suspension point. We need to
495     /// flag drop flags to avoid triggering this check as they are introduced
496     /// after typeck.
497     ///
498     /// Unsafety checking will also ignore dereferences of these locals,
499     /// so they can be used for raw pointers only used in a desugaring.
500     ///
501     /// This should be sound because the drop flags are fully algebraic, and
502     /// therefore don't affect the OIBIT or outlives properties of the
503     /// generator.
504     pub internal: bool,
505
506     /// Type of this local.
507     pub ty: Ty<'tcx>,
508
509     /// Name of the local, used in debuginfo and pretty-printing.
510     ///
511     /// Note that function arguments can also have this set to `Some(_)`
512     /// to generate better debuginfo.
513     pub name: Option<Name>,
514
515     /// Source info of the local.
516     pub source_info: SourceInfo,
517
518     /// The *syntactic* visibility scope the local is defined
519     /// in. If the local was defined in a let-statement, this
520     /// is *within* the let-statement, rather than outside
521     /// of it.
522     ///
523     /// This is needed because visibility scope of locals within a let-statement
524     /// is weird.
525     ///
526     /// The reason is that we want the local to be *within* the let-statement
527     /// for lint purposes, but we want the local to be *after* the let-statement
528     /// for names-in-scope purposes.
529     ///
530     /// That's it, if we have a let-statement like the one in this
531     /// function:
532     ///
533     /// ```
534     /// fn foo(x: &str) {
535     ///     #[allow(unused_mut)]
536     ///     let mut x: u32 = { // <- one unused mut
537     ///         let mut y: u32 = x.parse().unwrap();
538     ///         y + 2
539     ///     };
540     ///     drop(x);
541     /// }
542     /// ```
543     ///
544     /// Then, from a lint point of view, the declaration of `x: u32`
545     /// (and `y: u32`) are within the `#[allow(unused_mut)]` scope - the
546     /// lint scopes are the same as the AST/HIR nesting.
547     ///
548     /// However, from a name lookup point of view, the scopes look more like
549     /// as if the let-statements were `match` expressions:
550     ///
551     /// ```
552     /// fn foo(x: &str) {
553     ///     match {
554     ///         match x.parse().unwrap() {
555     ///             y => y + 2
556     ///         }
557     ///     } {
558     ///         x => drop(x)
559     ///     };
560     /// }
561     /// ```
562     ///
563     /// We care about the name-lookup scopes for debuginfo - if the
564     /// debuginfo instruction pointer is at the call to `x.parse()`, we
565     /// want `x` to refer to `x: &str`, but if it is at the call to
566     /// `drop(x)`, we want it to refer to `x: u32`.
567     ///
568     /// To allow both uses to work, we need to have more than a single scope
569     /// for a local. We have the `syntactic_scope` represent the
570     /// "syntactic" lint scope (with a variable being under its let
571     /// block) while the source-info scope represents the "local variable"
572     /// scope (where the "rest" of a block is under all prior let-statements).
573     ///
574     /// The end result looks like this:
575     ///
576     /// ```text
577     /// ROOT SCOPE
578     ///  │{ argument x: &str }
579     ///  │
580     ///  │ │{ #[allow(unused_mut] } // this is actually split into 2 scopes
581     ///  │ │                        // in practice because I'm lazy.
582     ///  │ │
583     ///  │ │← x.syntactic_scope
584     ///  │ │← `x.parse().unwrap()`
585     ///  │ │
586     ///  │ │ │← y.syntactic_scope
587     ///  │ │
588     ///  │ │ │{ let y: u32 }
589     ///  │ │ │
590     ///  │ │ │← y.source_info.scope
591     ///  │ │ │← `y + 2`
592     ///  │
593     ///  │ │{ let x: u32 }
594     ///  │ │← x.source_info.scope
595     ///  │ │← `drop(x)` // this accesses `x: u32`
596     /// ```
597     pub syntactic_scope: VisibilityScope,
598 }
599
600 impl<'tcx> LocalDecl<'tcx> {
601     /// Create a new `LocalDecl` for a temporary.
602     #[inline]
603     pub fn new_temp(ty: Ty<'tcx>, span: Span) -> Self {
604         LocalDecl {
605             mutability: Mutability::Mut,
606             ty,
607             name: None,
608             source_info: SourceInfo {
609                 span,
610                 scope: ARGUMENT_VISIBILITY_SCOPE
611             },
612             syntactic_scope: ARGUMENT_VISIBILITY_SCOPE,
613             internal: false,
614             is_user_variable: false
615         }
616     }
617
618     /// Create a new `LocalDecl` for a internal temporary.
619     #[inline]
620     pub fn new_internal(ty: Ty<'tcx>, span: Span) -> Self {
621         LocalDecl {
622             mutability: Mutability::Mut,
623             ty,
624             name: None,
625             source_info: SourceInfo {
626                 span,
627                 scope: ARGUMENT_VISIBILITY_SCOPE
628             },
629             syntactic_scope: ARGUMENT_VISIBILITY_SCOPE,
630             internal: true,
631             is_user_variable: false
632         }
633     }
634
635     /// Builds a `LocalDecl` for the return place.
636     ///
637     /// This must be inserted into the `local_decls` list as the first local.
638     #[inline]
639     pub fn new_return_place(return_ty: Ty, span: Span) -> LocalDecl {
640         LocalDecl {
641             mutability: Mutability::Mut,
642             ty: return_ty,
643             source_info: SourceInfo {
644                 span,
645                 scope: ARGUMENT_VISIBILITY_SCOPE
646             },
647             syntactic_scope: ARGUMENT_VISIBILITY_SCOPE,
648             internal: false,
649             name: None,     // FIXME maybe we do want some name here?
650             is_user_variable: false
651         }
652     }
653 }
654
655 /// A closure capture, with its name and mode.
656 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
657 pub struct UpvarDecl {
658     pub debug_name: Name,
659
660     /// If true, the capture is behind a reference.
661     pub by_ref: bool,
662
663     pub mutability: Mutability,
664 }
665
666 ///////////////////////////////////////////////////////////////////////////
667 // BasicBlock
668
669 newtype_index!(BasicBlock { DEBUG_FORMAT = "bb{}" });
670
671 impl BasicBlock {
672     pub fn start_location(self) -> Location {
673         Location {
674             block: self,
675             statement_index: 0,
676         }
677     }
678 }
679
680 ///////////////////////////////////////////////////////////////////////////
681 // BasicBlockData and Terminator
682
683 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
684 pub struct BasicBlockData<'tcx> {
685     /// List of statements in this block.
686     pub statements: Vec<Statement<'tcx>>,
687
688     /// Terminator for this block.
689     ///
690     /// NB. This should generally ONLY be `None` during construction.
691     /// Therefore, you should generally access it via the
692     /// `terminator()` or `terminator_mut()` methods. The only
693     /// exception is that certain passes, such as `simplify_cfg`, swap
694     /// out the terminator temporarily with `None` while they continue
695     /// to recurse over the set of basic blocks.
696     pub terminator: Option<Terminator<'tcx>>,
697
698     /// If true, this block lies on an unwind path. This is used
699     /// during codegen where distinct kinds of basic blocks may be
700     /// generated (particularly for MSVC cleanup). Unwind blocks must
701     /// only branch to other unwind blocks.
702     pub is_cleanup: bool,
703 }
704
705 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
706 pub struct Terminator<'tcx> {
707     pub source_info: SourceInfo,
708     pub kind: TerminatorKind<'tcx>
709 }
710
711 #[derive(Clone, RustcEncodable, RustcDecodable)]
712 pub enum TerminatorKind<'tcx> {
713     /// block should have one successor in the graph; we jump there
714     Goto {
715         target: BasicBlock,
716     },
717
718     /// operand evaluates to an integer; jump depending on its value
719     /// to one of the targets, and otherwise fallback to `otherwise`
720     SwitchInt {
721         /// discriminant value being tested
722         discr: Operand<'tcx>,
723
724         /// type of value being tested
725         switch_ty: Ty<'tcx>,
726
727         /// Possible values. The locations to branch to in each case
728         /// are found in the corresponding indices from the `targets` vector.
729         values: Cow<'tcx, [u128]>,
730
731         /// Possible branch sites. The last element of this vector is used
732         /// for the otherwise branch, so targets.len() == values.len() + 1
733         /// should hold.
734         // This invariant is quite non-obvious and also could be improved.
735         // One way to make this invariant is to have something like this instead:
736         //
737         // branches: Vec<(ConstInt, BasicBlock)>,
738         // otherwise: Option<BasicBlock> // exhaustive if None
739         //
740         // However we’ve decided to keep this as-is until we figure a case
741         // where some other approach seems to be strictly better than other.
742         targets: Vec<BasicBlock>,
743     },
744
745     /// Indicates that the landing pad is finished and unwinding should
746     /// continue. Emitted by build::scope::diverge_cleanup.
747     Resume,
748
749     /// Indicates that the landing pad is finished and that the process
750     /// should abort. Used to prevent unwinding for foreign items.
751     Abort,
752
753     /// Indicates a normal return. The return place should have
754     /// been filled in by now. This should occur at most once.
755     Return,
756
757     /// Indicates a terminator that can never be reached.
758     Unreachable,
759
760     /// Drop the Place
761     Drop {
762         location: Place<'tcx>,
763         target: BasicBlock,
764         unwind: Option<BasicBlock>
765     },
766
767     /// Drop the Place and assign the new value over it. This ensures
768     /// that the assignment to `P` occurs *even if* the destructor for
769     /// place unwinds. Its semantics are best explained by by the
770     /// elaboration:
771     ///
772     /// ```
773     /// BB0 {
774     ///   DropAndReplace(P <- V, goto BB1, unwind BB2)
775     /// }
776     /// ```
777     ///
778     /// becomes
779     ///
780     /// ```
781     /// BB0 {
782     ///   Drop(P, goto BB1, unwind BB2)
783     /// }
784     /// BB1 {
785     ///   // P is now unitialized
786     ///   P <- V
787     /// }
788     /// BB2 {
789     ///   // P is now unitialized -- its dtor panicked
790     ///   P <- V
791     /// }
792     /// ```
793     DropAndReplace {
794         location: Place<'tcx>,
795         value: Operand<'tcx>,
796         target: BasicBlock,
797         unwind: Option<BasicBlock>,
798     },
799
800     /// Block ends with a call of a converging function
801     Call {
802         /// The function that’s being called
803         func: Operand<'tcx>,
804         /// Arguments the function is called with.
805         /// These are owned by the callee, which is free to modify them.
806         /// This allows the memory occupied by "by-value" arguments to be
807         /// reused across function calls without duplicating the contents.
808         args: Vec<Operand<'tcx>>,
809         /// Destination for the return value. If some, the call is converging.
810         destination: Option<(Place<'tcx>, BasicBlock)>,
811         /// Cleanups to be done if the call unwinds.
812         cleanup: Option<BasicBlock>
813     },
814
815     /// Jump to the target if the condition has the expected value,
816     /// otherwise panic with a message and a cleanup target.
817     Assert {
818         cond: Operand<'tcx>,
819         expected: bool,
820         msg: AssertMessage<'tcx>,
821         target: BasicBlock,
822         cleanup: Option<BasicBlock>
823     },
824
825     /// A suspend point
826     Yield {
827         /// The value to return
828         value: Operand<'tcx>,
829         /// Where to resume to
830         resume: BasicBlock,
831         /// Cleanup to be done if the generator is dropped at this suspend point
832         drop: Option<BasicBlock>,
833     },
834
835     /// Indicates the end of the dropping of a generator
836     GeneratorDrop,
837
838     /// A block where control flow only ever takes one real path, but borrowck
839     /// needs to be more conservative.
840     FalseEdges {
841         /// The target normal control flow will take
842         real_target: BasicBlock,
843         /// The list of blocks control flow could conceptually take, but won't
844         /// in practice
845         imaginary_targets: Vec<BasicBlock>,
846     },
847     /// A terminator for blocks that only take one path in reality, but where we
848     /// reserve the right to unwind in borrowck, even if it won't happen in practice.
849     /// This can arise in infinite loops with no function calls for example.
850     FalseUnwind {
851         /// The target normal control flow will take
852         real_target: BasicBlock,
853         /// The imaginary cleanup block link. This particular path will never be taken
854         /// in practice, but in order to avoid fragility we want to always
855         /// consider it in borrowck. We don't want to accept programs which
856         /// pass borrowck only when panic=abort or some assertions are disabled
857         /// due to release vs. debug mode builds. This needs to be an Option because
858         /// of the remove_noop_landing_pads and no_landing_pads passes
859         unwind: Option<BasicBlock>,
860     },
861 }
862
863 pub type Successors<'a> =
864     iter::Chain<option::IntoIter<&'a BasicBlock>, slice::Iter<'a, BasicBlock>>;
865 pub type SuccessorsMut<'a> =
866     iter::Chain<option::IntoIter<&'a mut BasicBlock>, slice::IterMut<'a, BasicBlock>>;
867
868 impl<'tcx> Terminator<'tcx> {
869     pub fn successors(&self) -> Successors {
870         self.kind.successors()
871     }
872
873     pub fn successors_mut(&mut self) -> SuccessorsMut {
874         self.kind.successors_mut()
875     }
876
877     pub fn unwind_mut(&mut self) -> Option<&mut Option<BasicBlock>> {
878         self.kind.unwind_mut()
879     }
880 }
881
882 impl<'tcx> TerminatorKind<'tcx> {
883     pub fn if_<'a, 'gcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>, cond: Operand<'tcx>,
884                          t: BasicBlock, f: BasicBlock) -> TerminatorKind<'tcx> {
885         static BOOL_SWITCH_FALSE: &'static [u128] = &[0];
886         TerminatorKind::SwitchInt {
887             discr: cond,
888             switch_ty: tcx.types.bool,
889             values: From::from(BOOL_SWITCH_FALSE),
890             targets: vec![f, t],
891         }
892     }
893
894     pub fn successors(&self) -> Successors {
895         use self::TerminatorKind::*;
896         match *self {
897             Resume | Abort | GeneratorDrop | Return | Unreachable |
898             Call { destination: None, cleanup: None, .. } => {
899                 None.into_iter().chain(&[])
900             }
901             Goto { target: ref t } |
902             Call { destination: None, cleanup: Some(ref t), .. } |
903             Call { destination: Some((_, ref t)), cleanup: None, .. } |
904             Yield { resume: ref t, drop: None, .. } |
905             DropAndReplace { target: ref t, unwind: None, .. } |
906             Drop { target: ref t, unwind: None, .. } |
907             Assert { target: ref t, cleanup: None, .. } |
908             FalseUnwind { real_target: ref t, unwind: None } => {
909                 Some(t).into_iter().chain(&[])
910             }
911             Call { destination: Some((_, ref t)), cleanup: Some(ref u), .. } |
912             Yield { resume: ref t, drop: Some(ref u), .. } |
913             DropAndReplace { target: ref t, unwind: Some(ref u), .. } |
914             Drop { target: ref t, unwind: Some(ref u), .. } |
915             Assert { target: ref t, cleanup: Some(ref u), .. } |
916             FalseUnwind { real_target: ref t, unwind: Some(ref u) } => {
917                 Some(t).into_iter().chain(slice::from_ref(u))
918             }
919             SwitchInt { ref targets, .. } => {
920                 None.into_iter().chain(&targets[..])
921             }
922             FalseEdges { ref real_target, ref imaginary_targets } => {
923                 Some(real_target).into_iter().chain(&imaginary_targets[..])
924             }
925         }
926     }
927
928     pub fn successors_mut(&mut self) -> SuccessorsMut {
929         use self::TerminatorKind::*;
930         match *self {
931             Resume | Abort | GeneratorDrop | Return | Unreachable |
932             Call { destination: None, cleanup: None, .. } => {
933                 None.into_iter().chain(&mut [])
934             }
935             Goto { target: ref mut t } |
936             Call { destination: None, cleanup: Some(ref mut t), .. } |
937             Call { destination: Some((_, ref mut t)), cleanup: None, .. } |
938             Yield { resume: ref mut t, drop: None, .. } |
939             DropAndReplace { target: ref mut t, unwind: None, .. } |
940             Drop { target: ref mut t, unwind: None, .. } |
941             Assert { target: ref mut t, cleanup: None, .. } |
942             FalseUnwind { real_target: ref mut t, unwind: None } => {
943                 Some(t).into_iter().chain(&mut [])
944             }
945             Call { destination: Some((_, ref mut t)), cleanup: Some(ref mut u), .. } |
946             Yield { resume: ref mut t, drop: Some(ref mut u), .. } |
947             DropAndReplace { target: ref mut t, unwind: Some(ref mut u), .. } |
948             Drop { target: ref mut t, unwind: Some(ref mut u), .. } |
949             Assert { target: ref mut t, cleanup: Some(ref mut u), .. } |
950             FalseUnwind { real_target: ref mut t, unwind: Some(ref mut u) } => {
951                 Some(t).into_iter().chain(slice::from_mut(u))
952             }
953             SwitchInt { ref mut targets, .. } => {
954                 None.into_iter().chain(&mut targets[..])
955             }
956             FalseEdges { ref mut real_target, ref mut imaginary_targets } => {
957                 Some(real_target).into_iter().chain(&mut imaginary_targets[..])
958             }
959         }
960     }
961
962     pub fn unwind_mut(&mut self) -> Option<&mut Option<BasicBlock>> {
963         match *self {
964             TerminatorKind::Goto { .. } |
965             TerminatorKind::Resume |
966             TerminatorKind::Abort |
967             TerminatorKind::Return |
968             TerminatorKind::Unreachable |
969             TerminatorKind::GeneratorDrop |
970             TerminatorKind::Yield { .. } |
971             TerminatorKind::SwitchInt { .. } |
972             TerminatorKind::FalseEdges { .. } => {
973                 None
974             },
975             TerminatorKind::Call { cleanup: ref mut unwind, .. } |
976             TerminatorKind::Assert { cleanup: ref mut unwind, .. } |
977             TerminatorKind::DropAndReplace { ref mut unwind, .. } |
978             TerminatorKind::Drop { ref mut unwind, .. } |
979             TerminatorKind::FalseUnwind { ref mut unwind, .. } => {
980                 Some(unwind)
981             }
982         }
983     }
984 }
985
986 impl<'tcx> BasicBlockData<'tcx> {
987     pub fn new(terminator: Option<Terminator<'tcx>>) -> BasicBlockData<'tcx> {
988         BasicBlockData {
989             statements: vec![],
990             terminator,
991             is_cleanup: false,
992         }
993     }
994
995     /// Accessor for terminator.
996     ///
997     /// Terminator may not be None after construction of the basic block is complete. This accessor
998     /// provides a convenience way to reach the terminator.
999     pub fn terminator(&self) -> &Terminator<'tcx> {
1000         self.terminator.as_ref().expect("invalid terminator state")
1001     }
1002
1003     pub fn terminator_mut(&mut self) -> &mut Terminator<'tcx> {
1004         self.terminator.as_mut().expect("invalid terminator state")
1005     }
1006
1007     pub fn retain_statements<F>(&mut self, mut f: F) where F: FnMut(&mut Statement) -> bool {
1008         for s in &mut self.statements {
1009             if !f(s) {
1010                 s.make_nop();
1011             }
1012         }
1013     }
1014
1015     pub fn expand_statements<F, I>(&mut self, mut f: F)
1016         where F: FnMut(&mut Statement<'tcx>) -> Option<I>,
1017               I: iter::TrustedLen<Item = Statement<'tcx>>
1018     {
1019         // Gather all the iterators we'll need to splice in, and their positions.
1020         let mut splices: Vec<(usize, I)> = vec![];
1021         let mut extra_stmts = 0;
1022         for (i, s) in self.statements.iter_mut().enumerate() {
1023             if let Some(mut new_stmts) = f(s) {
1024                 if let Some(first) = new_stmts.next() {
1025                     // We can already store the first new statement.
1026                     *s = first;
1027
1028                     // Save the other statements for optimized splicing.
1029                     let remaining = new_stmts.size_hint().0;
1030                     if remaining > 0 {
1031                         splices.push((i + 1 + extra_stmts, new_stmts));
1032                         extra_stmts += remaining;
1033                     }
1034                 } else {
1035                     s.make_nop();
1036                 }
1037             }
1038         }
1039
1040         // Splice in the new statements, from the end of the block.
1041         // FIXME(eddyb) This could be more efficient with a "gap buffer"
1042         // where a range of elements ("gap") is left uninitialized, with
1043         // splicing adding new elements to the end of that gap and moving
1044         // existing elements from before the gap to the end of the gap.
1045         // For now, this is safe code, emulating a gap but initializing it.
1046         let mut gap = self.statements.len()..self.statements.len()+extra_stmts;
1047         self.statements.resize(gap.end, Statement {
1048             source_info: SourceInfo {
1049                 span: DUMMY_SP,
1050                 scope: ARGUMENT_VISIBILITY_SCOPE
1051             },
1052             kind: StatementKind::Nop
1053         });
1054         for (splice_start, new_stmts) in splices.into_iter().rev() {
1055             let splice_end = splice_start + new_stmts.size_hint().0;
1056             while gap.end > splice_end {
1057                 gap.start -= 1;
1058                 gap.end -= 1;
1059                 self.statements.swap(gap.start, gap.end);
1060             }
1061             self.statements.splice(splice_start..splice_end, new_stmts);
1062             gap.end = splice_start;
1063         }
1064     }
1065
1066     pub fn visitable(&self, index: usize) -> &dyn MirVisitable<'tcx> {
1067         if index < self.statements.len() {
1068             &self.statements[index]
1069         } else {
1070             &self.terminator
1071         }
1072     }
1073 }
1074
1075 impl<'tcx> Debug for TerminatorKind<'tcx> {
1076     fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
1077         self.fmt_head(fmt)?;
1078         let successor_count = self.successors().count();
1079         let labels = self.fmt_successor_labels();
1080         assert_eq!(successor_count, labels.len());
1081
1082         match successor_count {
1083             0 => Ok(()),
1084
1085             1 => write!(fmt, " -> {:?}", self.successors().nth(0).unwrap()),
1086
1087             _ => {
1088                 write!(fmt, " -> [")?;
1089                 for (i, target) in self.successors().enumerate() {
1090                     if i > 0 {
1091                         write!(fmt, ", ")?;
1092                     }
1093                     write!(fmt, "{}: {:?}", labels[i], target)?;
1094                 }
1095                 write!(fmt, "]")
1096             }
1097
1098         }
1099     }
1100 }
1101
1102 impl<'tcx> TerminatorKind<'tcx> {
1103     /// Write the "head" part of the terminator; that is, its name and the data it uses to pick the
1104     /// successor basic block, if any. The only information not included is the list of possible
1105     /// successors, which may be rendered differently between the text and the graphviz format.
1106     pub fn fmt_head<W: Write>(&self, fmt: &mut W) -> fmt::Result {
1107         use self::TerminatorKind::*;
1108         match *self {
1109             Goto { .. } => write!(fmt, "goto"),
1110             SwitchInt { discr: ref place, .. } => write!(fmt, "switchInt({:?})", place),
1111             Return => write!(fmt, "return"),
1112             GeneratorDrop => write!(fmt, "generator_drop"),
1113             Resume => write!(fmt, "resume"),
1114             Abort => write!(fmt, "abort"),
1115             Yield { ref value, .. } => write!(fmt, "_1 = suspend({:?})", value),
1116             Unreachable => write!(fmt, "unreachable"),
1117             Drop { ref location, .. } => write!(fmt, "drop({:?})", location),
1118             DropAndReplace { ref location, ref value, .. } =>
1119                 write!(fmt, "replace({:?} <- {:?})", location, value),
1120             Call { ref func, ref args, ref destination, .. } => {
1121                 if let Some((ref destination, _)) = *destination {
1122                     write!(fmt, "{:?} = ", destination)?;
1123                 }
1124                 write!(fmt, "{:?}(", func)?;
1125                 for (index, arg) in args.iter().enumerate() {
1126                     if index > 0 {
1127                         write!(fmt, ", ")?;
1128                     }
1129                     write!(fmt, "{:?}", arg)?;
1130                 }
1131                 write!(fmt, ")")
1132             }
1133             Assert { ref cond, expected, ref msg, .. } => {
1134                 write!(fmt, "assert(")?;
1135                 if !expected {
1136                     write!(fmt, "!")?;
1137                 }
1138                 write!(fmt, "{:?}, \"{:?}\")", cond, msg)
1139             },
1140             FalseEdges { .. } => write!(fmt, "falseEdges"),
1141             FalseUnwind { .. } => write!(fmt, "falseUnwind"),
1142         }
1143     }
1144
1145     /// Return the list of labels for the edges to the successor basic blocks.
1146     pub fn fmt_successor_labels(&self) -> Vec<Cow<'static, str>> {
1147         use self::TerminatorKind::*;
1148         match *self {
1149             Return | Resume | Abort | Unreachable | GeneratorDrop => vec![],
1150             Goto { .. } => vec!["".into()],
1151             SwitchInt { ref values, switch_ty, .. } => {
1152                 values.iter()
1153                       .map(|&u| {
1154                           let mut s = String::new();
1155                           print_miri_value(
1156                               Value::ByVal(PrimVal::Bytes(u)),
1157                               switch_ty,
1158                               &mut s,
1159                           ).unwrap();
1160                           s.into()
1161                       })
1162                       .chain(iter::once(String::from("otherwise").into()))
1163                       .collect()
1164             }
1165             Call { destination: Some(_), cleanup: Some(_), .. } =>
1166                 vec!["return".into_cow(), "unwind".into_cow()],
1167             Call { destination: Some(_), cleanup: None, .. } => vec!["return".into_cow()],
1168             Call { destination: None, cleanup: Some(_), .. } => vec!["unwind".into_cow()],
1169             Call { destination: None, cleanup: None, .. } => vec![],
1170             Yield { drop: Some(_), .. } =>
1171                 vec!["resume".into_cow(), "drop".into_cow()],
1172             Yield { drop: None, .. } => vec!["resume".into_cow()],
1173             DropAndReplace { unwind: None, .. } |
1174             Drop { unwind: None, .. } => vec!["return".into_cow()],
1175             DropAndReplace { unwind: Some(_), .. } |
1176             Drop { unwind: Some(_), .. } => {
1177                 vec!["return".into_cow(), "unwind".into_cow()]
1178             }
1179             Assert { cleanup: None, .. } => vec!["".into()],
1180             Assert { .. } =>
1181                 vec!["success".into_cow(), "unwind".into_cow()],
1182             FalseEdges { ref imaginary_targets, .. } => {
1183                 let mut l = vec!["real".into()];
1184                 l.resize(imaginary_targets.len() + 1, "imaginary".into());
1185                 l
1186             }
1187             FalseUnwind { unwind: Some(_), .. } => vec!["real".into(), "cleanup".into()],
1188             FalseUnwind { unwind: None, .. } => vec!["real".into()],
1189         }
1190     }
1191 }
1192
1193 ///////////////////////////////////////////////////////////////////////////
1194 // Statements
1195
1196 #[derive(Clone, RustcEncodable, RustcDecodable)]
1197 pub struct Statement<'tcx> {
1198     pub source_info: SourceInfo,
1199     pub kind: StatementKind<'tcx>,
1200 }
1201
1202 impl<'tcx> Statement<'tcx> {
1203     /// Changes a statement to a nop. This is both faster than deleting instructions and avoids
1204     /// invalidating statement indices in `Location`s.
1205     pub fn make_nop(&mut self) {
1206         self.kind = StatementKind::Nop
1207     }
1208
1209     /// Changes a statement to a nop and returns the original statement.
1210     pub fn replace_nop(&mut self) -> Self {
1211         Statement {
1212             source_info: self.source_info,
1213             kind: mem::replace(&mut self.kind, StatementKind::Nop)
1214         }
1215     }
1216 }
1217
1218 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
1219 pub enum StatementKind<'tcx> {
1220     /// Write the RHS Rvalue to the LHS Place.
1221     Assign(Place<'tcx>, Rvalue<'tcx>),
1222
1223     /// Write the discriminant for a variant to the enum Place.
1224     SetDiscriminant { place: Place<'tcx>, variant_index: usize },
1225
1226     /// Start a live range for the storage of the local.
1227     StorageLive(Local),
1228
1229     /// End the current live range for the storage of the local.
1230     StorageDead(Local),
1231
1232     /// Execute a piece of inline Assembly.
1233     InlineAsm {
1234         asm: Box<InlineAsm>,
1235         outputs: Vec<Place<'tcx>>,
1236         inputs: Vec<Operand<'tcx>>
1237     },
1238
1239     /// Assert the given places to be valid inhabitants of their type.  These statements are
1240     /// currently only interpreted by miri and only generated when "-Z mir-emit-validate" is passed.
1241     /// See <https://internals.rust-lang.org/t/types-as-contracts/5562/73> for more details.
1242     Validate(ValidationOp, Vec<ValidationOperand<'tcx, Place<'tcx>>>),
1243
1244     /// Mark one terminating point of a region scope (i.e. static region).
1245     /// (The starting point(s) arise implicitly from borrows.)
1246     EndRegion(region::Scope),
1247
1248     /// Encodes a user's type assertion. These need to be preserved intact so that NLL can respect
1249     /// them. For example:
1250     ///
1251     ///     let (a, b): (T, U) = y;
1252     ///
1253     /// Here we would insert a `UserAssertTy<(T, U)>(y)` instruction to check that the type of `y`
1254     /// is the right thing.
1255     ///
1256     /// `CanonicalTy` is used to capture "inference variables" from the user's types. For example:
1257     ///
1258     ///     let x: Vec<_> = ...;
1259     ///     let y: &u32 = ...;
1260     ///
1261     /// would result in `Vec<?0>` and `&'?0 u32` respectively (where `?0` is a canonicalized
1262     /// variable).
1263     UserAssertTy(CanonicalTy<'tcx>, Local),
1264
1265     /// No-op. Useful for deleting instructions without affecting statement indices.
1266     Nop,
1267 }
1268
1269 /// The `ValidationOp` describes what happens with each of the operands of a
1270 /// `Validate` statement.
1271 #[derive(Copy, Clone, RustcEncodable, RustcDecodable, PartialEq, Eq)]
1272 pub enum ValidationOp {
1273     /// Recursively traverse the place following the type and validate that all type
1274     /// invariants are maintained.  Furthermore, acquire exclusive/read-only access to the
1275     /// memory reachable from the place.
1276     Acquire,
1277     /// Recursive traverse the *mutable* part of the type and relinquish all exclusive
1278     /// access.
1279     Release,
1280     /// Recursive traverse the *mutable* part of the type and relinquish all exclusive
1281     /// access *until* the given region ends.  Then, access will be recovered.
1282     Suspend(region::Scope),
1283 }
1284
1285 impl Debug for ValidationOp {
1286     fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
1287         use self::ValidationOp::*;
1288         match *self {
1289             Acquire => write!(fmt, "Acquire"),
1290             Release => write!(fmt, "Release"),
1291             // (reuse lifetime rendering policy from ppaux.)
1292             Suspend(ref ce) => write!(fmt, "Suspend({})", ty::ReScope(*ce)),
1293         }
1294     }
1295 }
1296
1297 // This is generic so that it can be reused by miri
1298 #[derive(Clone, RustcEncodable, RustcDecodable)]
1299 pub struct ValidationOperand<'tcx, T> {
1300     pub place: T,
1301     pub ty: Ty<'tcx>,
1302     pub re: Option<region::Scope>,
1303     pub mutbl: hir::Mutability,
1304 }
1305
1306 impl<'tcx, T: Debug> Debug for ValidationOperand<'tcx, T> {
1307     fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
1308         write!(fmt, "{:?}: {:?}", self.place, self.ty)?;
1309         if let Some(ce) = self.re {
1310             // (reuse lifetime rendering policy from ppaux.)
1311             write!(fmt, "/{}", ty::ReScope(ce))?;
1312         }
1313         if let hir::MutImmutable = self.mutbl {
1314             write!(fmt, " (imm)")?;
1315         }
1316         Ok(())
1317     }
1318 }
1319
1320 impl<'tcx> Debug for Statement<'tcx> {
1321     fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
1322         use self::StatementKind::*;
1323         match self.kind {
1324             Assign(ref place, ref rv) => write!(fmt, "{:?} = {:?}", place, rv),
1325             // (reuse lifetime rendering policy from ppaux.)
1326             EndRegion(ref ce) => write!(fmt, "EndRegion({})", ty::ReScope(*ce)),
1327             Validate(ref op, ref places) => write!(fmt, "Validate({:?}, {:?})", op, places),
1328             StorageLive(ref place) => write!(fmt, "StorageLive({:?})", place),
1329             StorageDead(ref place) => write!(fmt, "StorageDead({:?})", place),
1330             SetDiscriminant { ref place, variant_index } => {
1331                 write!(fmt, "discriminant({:?}) = {:?}", place, variant_index)
1332             },
1333             InlineAsm { ref asm, ref outputs, ref inputs } => {
1334                 write!(fmt, "asm!({:?} : {:?} : {:?})", asm, outputs, inputs)
1335             },
1336             UserAssertTy(ref c_ty, ref local) => write!(fmt, "UserAssertTy({:?}, {:?})",
1337                                                         c_ty, local),
1338             Nop => write!(fmt, "nop"),
1339         }
1340     }
1341 }
1342
1343 ///////////////////////////////////////////////////////////////////////////
1344 // Places
1345
1346 /// A path to a value; something that can be evaluated without
1347 /// changing or disturbing program state.
1348 #[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
1349 pub enum Place<'tcx> {
1350     /// local variable
1351     Local(Local),
1352
1353     /// static or static mut variable
1354     Static(Box<Static<'tcx>>),
1355
1356     /// projection out of a place (access a field, deref a pointer, etc)
1357     Projection(Box<PlaceProjection<'tcx>>),
1358 }
1359
1360 /// The def-id of a static, along with its normalized type (which is
1361 /// stored to avoid requiring normalization when reading MIR).
1362 #[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
1363 pub struct Static<'tcx> {
1364     pub def_id: DefId,
1365     pub ty: Ty<'tcx>,
1366 }
1367
1368 impl_stable_hash_for!(struct Static<'tcx> {
1369     def_id,
1370     ty
1371 });
1372
1373 /// The `Projection` data structure defines things of the form `B.x`
1374 /// or `*B` or `B[index]`. Note that it is parameterized because it is
1375 /// shared between `Constant` and `Place`. See the aliases
1376 /// `PlaceProjection` etc below.
1377 #[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
1378 pub struct Projection<'tcx, B, V, T> {
1379     pub base: B,
1380     pub elem: ProjectionElem<'tcx, V, T>,
1381 }
1382
1383 #[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
1384 pub enum ProjectionElem<'tcx, V, T> {
1385     Deref,
1386     Field(Field, T),
1387     Index(V),
1388
1389     /// These indices are generated by slice patterns. Easiest to explain
1390     /// by example:
1391     ///
1392     /// ```
1393     /// [X, _, .._, _, _] => { offset: 0, min_length: 4, from_end: false },
1394     /// [_, X, .._, _, _] => { offset: 1, min_length: 4, from_end: false },
1395     /// [_, _, .._, X, _] => { offset: 2, min_length: 4, from_end: true },
1396     /// [_, _, .._, _, X] => { offset: 1, min_length: 4, from_end: true },
1397     /// ```
1398     ConstantIndex {
1399         /// index or -index (in Python terms), depending on from_end
1400         offset: u32,
1401         /// thing being indexed must be at least this long
1402         min_length: u32,
1403         /// counting backwards from end?
1404         from_end: bool,
1405     },
1406
1407     /// These indices are generated by slice patterns.
1408     ///
1409     /// slice[from:-to] in Python terms.
1410     Subslice {
1411         from: u32,
1412         to: u32,
1413     },
1414
1415     /// "Downcast" to a variant of an ADT. Currently, we only introduce
1416     /// this for ADTs with more than one variant. It may be better to
1417     /// just introduce it always, or always for enums.
1418     Downcast(&'tcx AdtDef, usize),
1419 }
1420
1421 /// Alias for projections as they appear in places, where the base is a place
1422 /// and the index is a local.
1423 pub type PlaceProjection<'tcx> = Projection<'tcx, Place<'tcx>, Local, Ty<'tcx>>;
1424
1425 /// Alias for projections as they appear in places, where the base is a place
1426 /// and the index is a local.
1427 pub type PlaceElem<'tcx> = ProjectionElem<'tcx, Local, Ty<'tcx>>;
1428
1429 newtype_index!(Field { DEBUG_FORMAT = "field[{}]" });
1430
1431 impl<'tcx> Place<'tcx> {
1432     pub fn field(self, f: Field, ty: Ty<'tcx>) -> Place<'tcx> {
1433         self.elem(ProjectionElem::Field(f, ty))
1434     }
1435
1436     pub fn deref(self) -> Place<'tcx> {
1437         self.elem(ProjectionElem::Deref)
1438     }
1439
1440     pub fn downcast(self, adt_def: &'tcx AdtDef, variant_index: usize) -> Place<'tcx> {
1441         self.elem(ProjectionElem::Downcast(adt_def, variant_index))
1442     }
1443
1444     pub fn index(self, index: Local) -> Place<'tcx> {
1445         self.elem(ProjectionElem::Index(index))
1446     }
1447
1448     pub fn elem(self, elem: PlaceElem<'tcx>) -> Place<'tcx> {
1449         Place::Projection(Box::new(PlaceProjection {
1450             base: self,
1451             elem,
1452         }))
1453     }
1454 }
1455
1456 impl<'tcx> Debug for Place<'tcx> {
1457     fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
1458         use self::Place::*;
1459
1460         match *self {
1461             Local(id) => write!(fmt, "{:?}", id),
1462             Static(box self::Static { def_id, ty }) =>
1463                 write!(fmt, "({}: {:?})", ty::tls::with(|tcx| tcx.item_path_str(def_id)), ty),
1464             Projection(ref data) =>
1465                 match data.elem {
1466                     ProjectionElem::Downcast(ref adt_def, index) =>
1467                         write!(fmt, "({:?} as {})", data.base, adt_def.variants[index].name),
1468                     ProjectionElem::Deref =>
1469                         write!(fmt, "(*{:?})", data.base),
1470                     ProjectionElem::Field(field, ty) =>
1471                         write!(fmt, "({:?}.{:?}: {:?})", data.base, field.index(), ty),
1472                     ProjectionElem::Index(ref index) =>
1473                         write!(fmt, "{:?}[{:?}]", data.base, index),
1474                     ProjectionElem::ConstantIndex { offset, min_length, from_end: false } =>
1475                         write!(fmt, "{:?}[{:?} of {:?}]", data.base, offset, min_length),
1476                     ProjectionElem::ConstantIndex { offset, min_length, from_end: true } =>
1477                         write!(fmt, "{:?}[-{:?} of {:?}]", data.base, offset, min_length),
1478                     ProjectionElem::Subslice { from, to } if to == 0 =>
1479                         write!(fmt, "{:?}[{:?}:]", data.base, from),
1480                     ProjectionElem::Subslice { from, to } if from == 0 =>
1481                         write!(fmt, "{:?}[:-{:?}]", data.base, to),
1482                     ProjectionElem::Subslice { from, to } =>
1483                         write!(fmt, "{:?}[{:?}:-{:?}]", data.base,
1484                                from, to),
1485
1486                 },
1487         }
1488     }
1489 }
1490
1491 ///////////////////////////////////////////////////////////////////////////
1492 // Scopes
1493
1494 newtype_index!(VisibilityScope
1495     {
1496         DEBUG_FORMAT = "scope[{}]",
1497         const ARGUMENT_VISIBILITY_SCOPE = 0,
1498     });
1499
1500 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
1501 pub struct VisibilityScopeData {
1502     pub span: Span,
1503     pub parent_scope: Option<VisibilityScope>,
1504 }
1505
1506 ///////////////////////////////////////////////////////////////////////////
1507 // Operands
1508
1509 /// These are values that can appear inside an rvalue (or an index
1510 /// place). They are intentionally limited to prevent rvalues from
1511 /// being nested in one another.
1512 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable)]
1513 pub enum Operand<'tcx> {
1514     /// Copy: The value must be available for use afterwards.
1515     ///
1516     /// This implies that the type of the place must be `Copy`; this is true
1517     /// by construction during build, but also checked by the MIR type checker.
1518     Copy(Place<'tcx>),
1519     /// Move: The value (including old borrows of it) will not be used again.
1520     ///
1521     /// Safe for values of all types (modulo future developments towards `?Move`).
1522     /// Correct usage patterns are enforced by the borrow checker for safe code.
1523     /// `Copy` may be converted to `Move` to enable "last-use" optimizations.
1524     Move(Place<'tcx>),
1525     Constant(Box<Constant<'tcx>>),
1526 }
1527
1528 impl<'tcx> Debug for Operand<'tcx> {
1529     fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
1530         use self::Operand::*;
1531         match *self {
1532             Constant(ref a) => write!(fmt, "{:?}", a),
1533             Copy(ref place) => write!(fmt, "{:?}", place),
1534             Move(ref place) => write!(fmt, "move {:?}", place),
1535         }
1536     }
1537 }
1538
1539 impl<'tcx> Operand<'tcx> {
1540     pub fn function_handle<'a>(
1541         tcx: TyCtxt<'a, 'tcx, 'tcx>,
1542         def_id: DefId,
1543         substs: &'tcx Substs<'tcx>,
1544         span: Span,
1545     ) -> Self {
1546         let ty = tcx.type_of(def_id).subst(tcx, substs);
1547         Operand::Constant(box Constant {
1548             span,
1549             ty,
1550             literal: Literal::Value {
1551                 value: ty::Const::zero_sized(tcx, ty),
1552             },
1553         })
1554     }
1555
1556     pub fn to_copy(&self) -> Self {
1557         match *self {
1558             Operand::Copy(_) | Operand::Constant(_) => self.clone(),
1559             Operand::Move(ref place) => Operand::Copy(place.clone())
1560         }
1561     }
1562 }
1563
1564 ///////////////////////////////////////////////////////////////////////////
1565 /// Rvalues
1566
1567 #[derive(Clone, RustcEncodable, RustcDecodable)]
1568 pub enum Rvalue<'tcx> {
1569     /// x (either a move or copy, depending on type of x)
1570     Use(Operand<'tcx>),
1571
1572     /// [x; 32]
1573     Repeat(Operand<'tcx>, u64),
1574
1575     /// &x or &mut x
1576     Ref(Region<'tcx>, BorrowKind, Place<'tcx>),
1577
1578     /// length of a [X] or [X;n] value
1579     Len(Place<'tcx>),
1580
1581     Cast(CastKind, Operand<'tcx>, Ty<'tcx>),
1582
1583     BinaryOp(BinOp, Operand<'tcx>, Operand<'tcx>),
1584     CheckedBinaryOp(BinOp, Operand<'tcx>, Operand<'tcx>),
1585
1586     NullaryOp(NullOp, Ty<'tcx>),
1587     UnaryOp(UnOp, Operand<'tcx>),
1588
1589     /// Read the discriminant of an ADT.
1590     ///
1591     /// Undefined (i.e. no effort is made to make it defined, but there’s no reason why it cannot
1592     /// be defined to return, say, a 0) if ADT is not an enum.
1593     Discriminant(Place<'tcx>),
1594
1595     /// Create an aggregate value, like a tuple or struct.  This is
1596     /// only needed because we want to distinguish `dest = Foo { x:
1597     /// ..., y: ... }` from `dest.x = ...; dest.y = ...;` in the case
1598     /// that `Foo` has a destructor. These rvalues can be optimized
1599     /// away after type-checking and before lowering.
1600     Aggregate(Box<AggregateKind<'tcx>>, Vec<Operand<'tcx>>),
1601 }
1602
1603 #[derive(Clone, Copy, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
1604 pub enum CastKind {
1605     Misc,
1606
1607     /// Convert unique, zero-sized type for a fn to fn()
1608     ReifyFnPointer,
1609
1610     /// Convert non capturing closure to fn()
1611     ClosureFnPointer,
1612
1613     /// Convert safe fn() to unsafe fn()
1614     UnsafeFnPointer,
1615
1616     /// "Unsize" -- convert a thin-or-fat pointer to a fat pointer.
1617     /// codegen must figure out the details once full monomorphization
1618     /// is known. For example, this could be used to cast from a
1619     /// `&[i32;N]` to a `&[i32]`, or a `Box<T>` to a `Box<Trait>`
1620     /// (presuming `T: Trait`).
1621     Unsize,
1622 }
1623
1624 #[derive(Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
1625 pub enum AggregateKind<'tcx> {
1626     /// The type is of the element
1627     Array(Ty<'tcx>),
1628     Tuple,
1629
1630     /// The second field is the variant index. It's equal to 0 for struct
1631     /// and union expressions. The fourth field is
1632     /// active field number and is present only for union expressions
1633     /// -- e.g. for a union expression `SomeUnion { c: .. }`, the
1634     /// active field index would identity the field `c`
1635     Adt(&'tcx AdtDef, usize, &'tcx Substs<'tcx>, Option<usize>),
1636
1637     Closure(DefId, ClosureSubsts<'tcx>),
1638     Generator(DefId, GeneratorSubsts<'tcx>, hir::GeneratorMovability),
1639 }
1640
1641 #[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
1642 pub enum BinOp {
1643     /// The `+` operator (addition)
1644     Add,
1645     /// The `-` operator (subtraction)
1646     Sub,
1647     /// The `*` operator (multiplication)
1648     Mul,
1649     /// The `/` operator (division)
1650     Div,
1651     /// The `%` operator (modulus)
1652     Rem,
1653     /// The `^` operator (bitwise xor)
1654     BitXor,
1655     /// The `&` operator (bitwise and)
1656     BitAnd,
1657     /// The `|` operator (bitwise or)
1658     BitOr,
1659     /// The `<<` operator (shift left)
1660     Shl,
1661     /// The `>>` operator (shift right)
1662     Shr,
1663     /// The `==` operator (equality)
1664     Eq,
1665     /// The `<` operator (less than)
1666     Lt,
1667     /// The `<=` operator (less than or equal to)
1668     Le,
1669     /// The `!=` operator (not equal to)
1670     Ne,
1671     /// The `>=` operator (greater than or equal to)
1672     Ge,
1673     /// The `>` operator (greater than)
1674     Gt,
1675     /// The `ptr.offset` operator
1676     Offset,
1677 }
1678
1679 impl BinOp {
1680     pub fn is_checkable(self) -> bool {
1681         use self::BinOp::*;
1682         match self {
1683             Add | Sub | Mul | Shl | Shr => true,
1684             _ => false
1685         }
1686     }
1687 }
1688
1689 #[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
1690 pub enum NullOp {
1691     /// Return the size of a value of that type
1692     SizeOf,
1693     /// Create a new uninitialized box for a value of that type
1694     Box,
1695 }
1696
1697 #[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
1698 pub enum UnOp {
1699     /// The `!` operator for logical inversion
1700     Not,
1701     /// The `-` operator for negation
1702     Neg,
1703 }
1704
1705 impl<'tcx> Debug for Rvalue<'tcx> {
1706     fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
1707         use self::Rvalue::*;
1708
1709         match *self {
1710             Use(ref place) => write!(fmt, "{:?}", place),
1711             Repeat(ref a, ref b) => write!(fmt, "[{:?}; {:?}]", a, b),
1712             Len(ref a) => write!(fmt, "Len({:?})", a),
1713             Cast(ref kind, ref place, ref ty) => {
1714                 write!(fmt, "{:?} as {:?} ({:?})", place, ty, kind)
1715             }
1716             BinaryOp(ref op, ref a, ref b) => write!(fmt, "{:?}({:?}, {:?})", op, a, b),
1717             CheckedBinaryOp(ref op, ref a, ref b) => {
1718                 write!(fmt, "Checked{:?}({:?}, {:?})", op, a, b)
1719             }
1720             UnaryOp(ref op, ref a) => write!(fmt, "{:?}({:?})", op, a),
1721             Discriminant(ref place) => write!(fmt, "discriminant({:?})", place),
1722             NullaryOp(ref op, ref t) => write!(fmt, "{:?}({:?})", op, t),
1723             Ref(region, borrow_kind, ref place) => {
1724                 let kind_str = match borrow_kind {
1725                     BorrowKind::Shared => "",
1726                     BorrowKind::Mut { .. } | BorrowKind::Unique => "mut ",
1727                 };
1728
1729                 // When printing regions, add trailing space if necessary.
1730                 let region = if ppaux::verbose() || ppaux::identify_regions() {
1731                     let mut region = format!("{}", region);
1732                     if region.len() > 0 { region.push(' '); }
1733                     region
1734                 } else {
1735                     // Do not even print 'static
1736                     "".to_owned()
1737                 };
1738                 write!(fmt, "&{}{}{:?}", region, kind_str, place)
1739             }
1740
1741             Aggregate(ref kind, ref places) => {
1742                 fn fmt_tuple(fmt: &mut Formatter, places: &[Operand]) -> fmt::Result {
1743                     let mut tuple_fmt = fmt.debug_tuple("");
1744                     for place in places {
1745                         tuple_fmt.field(place);
1746                     }
1747                     tuple_fmt.finish()
1748                 }
1749
1750                 match **kind {
1751                     AggregateKind::Array(_) => write!(fmt, "{:?}", places),
1752
1753                     AggregateKind::Tuple => {
1754                         match places.len() {
1755                             0 => write!(fmt, "()"),
1756                             1 => write!(fmt, "({:?},)", places[0]),
1757                             _ => fmt_tuple(fmt, places),
1758                         }
1759                     }
1760
1761                     AggregateKind::Adt(adt_def, variant, substs, _) => {
1762                         let variant_def = &adt_def.variants[variant];
1763
1764                         ppaux::parameterized(fmt, substs, variant_def.did, &[])?;
1765
1766                         match variant_def.ctor_kind {
1767                             CtorKind::Const => Ok(()),
1768                             CtorKind::Fn => fmt_tuple(fmt, places),
1769                             CtorKind::Fictive => {
1770                                 let mut struct_fmt = fmt.debug_struct("");
1771                                 for (field, place) in variant_def.fields.iter().zip(places) {
1772                                     struct_fmt.field(&field.name.as_str(), place);
1773                                 }
1774                                 struct_fmt.finish()
1775                             }
1776                         }
1777                     }
1778
1779                     AggregateKind::Closure(def_id, _) => ty::tls::with(|tcx| {
1780                         if let Some(node_id) = tcx.hir.as_local_node_id(def_id) {
1781                             let name = if tcx.sess.opts.debugging_opts.span_free_formats {
1782                                 format!("[closure@{:?}]", node_id)
1783                             } else {
1784                                 format!("[closure@{:?}]", tcx.hir.span(node_id))
1785                             };
1786                             let mut struct_fmt = fmt.debug_struct(&name);
1787
1788                             tcx.with_freevars(node_id, |freevars| {
1789                                 for (freevar, place) in freevars.iter().zip(places) {
1790                                     let var_name = tcx.hir.name(freevar.var_id());
1791                                     struct_fmt.field(&var_name.as_str(), place);
1792                                 }
1793                             });
1794
1795                             struct_fmt.finish()
1796                         } else {
1797                             write!(fmt, "[closure]")
1798                         }
1799                     }),
1800
1801                     AggregateKind::Generator(def_id, _, _) => ty::tls::with(|tcx| {
1802                         if let Some(node_id) = tcx.hir.as_local_node_id(def_id) {
1803                             let name = format!("[generator@{:?}]", tcx.hir.span(node_id));
1804                             let mut struct_fmt = fmt.debug_struct(&name);
1805
1806                             tcx.with_freevars(node_id, |freevars| {
1807                                 for (freevar, place) in freevars.iter().zip(places) {
1808                                     let var_name = tcx.hir.name(freevar.var_id());
1809                                     struct_fmt.field(&var_name.as_str(), place);
1810                                 }
1811                                 struct_fmt.field("$state", &places[freevars.len()]);
1812                                 for i in (freevars.len() + 1)..places.len() {
1813                                     struct_fmt.field(&format!("${}", i - freevars.len() - 1),
1814                                                      &places[i]);
1815                                 }
1816                             });
1817
1818                             struct_fmt.finish()
1819                         } else {
1820                             write!(fmt, "[generator]")
1821                         }
1822                     }),
1823                 }
1824             }
1825         }
1826     }
1827 }
1828
1829 ///////////////////////////////////////////////////////////////////////////
1830 /// Constants
1831 ///
1832 /// Two constants are equal if they are the same constant. Note that
1833 /// this does not necessarily mean that they are "==" in Rust -- in
1834 /// particular one must be wary of `NaN`!
1835
1836 #[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
1837 pub struct Constant<'tcx> {
1838     pub span: Span,
1839     pub ty: Ty<'tcx>,
1840     pub literal: Literal<'tcx>,
1841 }
1842
1843 newtype_index!(Promoted { DEBUG_FORMAT = "promoted[{}]" });
1844
1845
1846 #[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
1847 pub enum Literal<'tcx> {
1848     Value {
1849         value: &'tcx ty::Const<'tcx>,
1850     },
1851     Promoted {
1852         // Index into the `promoted` vector of `Mir`.
1853         index: Promoted
1854     },
1855 }
1856
1857 impl<'tcx> Debug for Constant<'tcx> {
1858     fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
1859         write!(fmt, "{:?}", self.literal)
1860     }
1861 }
1862
1863 impl<'tcx> Debug for Literal<'tcx> {
1864     fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
1865         use self::Literal::*;
1866         match *self {
1867             Value { value } => {
1868                 write!(fmt, "const ")?;
1869                 fmt_const_val(fmt, value)
1870             }
1871             Promoted { index } => {
1872                 write!(fmt, "{:?}", index)
1873             }
1874         }
1875     }
1876 }
1877
1878 /// Write a `ConstVal` in a way closer to the original source code than the `Debug` output.
1879 pub fn fmt_const_val<W: Write>(fmt: &mut W, const_val: &ty::Const) -> fmt::Result {
1880     use middle::const_val::ConstVal;
1881     match const_val.val {
1882         ConstVal::Unevaluated(..) => write!(fmt, "{:?}", const_val),
1883         ConstVal::Value(val) => {
1884             if let Some(value) = val.to_byval_value() {
1885                 print_miri_value(value, const_val.ty, fmt)
1886             } else {
1887                 write!(fmt, "{:?}:{}", val, const_val.ty)
1888             }
1889         },
1890     }
1891 }
1892
1893 pub fn print_miri_value<W: Write>(value: Value, ty: Ty, f: &mut W) -> fmt::Result {
1894     use ty::TypeVariants::*;
1895     match (value, &ty.sty) {
1896         (Value::ByVal(PrimVal::Bytes(0)), &TyBool) => write!(f, "false"),
1897         (Value::ByVal(PrimVal::Bytes(1)), &TyBool) => write!(f, "true"),
1898         (Value::ByVal(PrimVal::Bytes(bits)), &TyFloat(ast::FloatTy::F32)) =>
1899             write!(f, "{}f32", Single::from_bits(bits)),
1900         (Value::ByVal(PrimVal::Bytes(bits)), &TyFloat(ast::FloatTy::F64)) =>
1901             write!(f, "{}f64", Double::from_bits(bits)),
1902         (Value::ByVal(PrimVal::Bytes(n)), &TyUint(ui)) => write!(f, "{:?}{}", n, ui),
1903         (Value::ByVal(PrimVal::Bytes(n)), &TyInt(i)) => write!(f, "{:?}{}", n as i128, i),
1904         (Value::ByVal(PrimVal::Bytes(n)), &TyChar) =>
1905             write!(f, "{:?}", ::std::char::from_u32(n as u32).unwrap()),
1906         (Value::ByVal(PrimVal::Undef), &TyFnDef(did, _)) =>
1907             write!(f, "{}", item_path_str(did)),
1908         (Value::ByValPair(PrimVal::Ptr(ptr), PrimVal::Bytes(len)),
1909          &TyRef(_, &ty::TyS { sty: TyStr, .. }, _)) => {
1910             ty::tls::with(|tcx| {
1911                 let alloc = tcx
1912                     .interpret_interner
1913                     .get_alloc(ptr.alloc_id);
1914                 if let Some(alloc) = alloc {
1915                     assert_eq!(len as usize as u128, len);
1916                     let slice = &alloc.bytes[(ptr.offset.bytes() as usize)..][..(len as usize)];
1917                     let s = ::std::str::from_utf8(slice)
1918                         .expect("non utf8 str from miri");
1919                     write!(f, "{:?}", s)
1920                 } else {
1921                     write!(f, "pointer to erroneous constant {:?}, {:?}", ptr, len)
1922                 }
1923             })
1924         },
1925         _ => write!(f, "{:?}:{}", value, ty),
1926     }
1927 }
1928
1929 fn item_path_str(def_id: DefId) -> String {
1930     ty::tls::with(|tcx| tcx.item_path_str(def_id))
1931 }
1932
1933 impl<'tcx> ControlFlowGraph for Mir<'tcx> {
1934
1935     type Node = BasicBlock;
1936
1937     fn num_nodes(&self) -> usize { self.basic_blocks.len() }
1938
1939     fn start_node(&self) -> Self::Node { START_BLOCK }
1940
1941     fn predecessors<'graph>(&'graph self, node: Self::Node)
1942                             -> <Self as GraphPredecessors<'graph>>::Iter
1943     {
1944         self.predecessors_for(node).clone().into_iter()
1945     }
1946     fn successors<'graph>(&'graph self, node: Self::Node)
1947                           -> <Self as GraphSuccessors<'graph>>::Iter
1948     {
1949         self.basic_blocks[node].terminator().successors().cloned()
1950     }
1951 }
1952
1953 impl<'a, 'b> GraphPredecessors<'b> for Mir<'a> {
1954     type Item = BasicBlock;
1955     type Iter = IntoIter<BasicBlock>;
1956 }
1957
1958 impl<'a, 'b>  GraphSuccessors<'b> for Mir<'a> {
1959     type Item = BasicBlock;
1960     type Iter = iter::Cloned<Successors<'b>>;
1961 }
1962
1963 #[derive(Copy, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
1964 pub struct Location {
1965     /// the location is within this block
1966     pub block: BasicBlock,
1967
1968     /// the location is the start of the statement; or, if `statement_index`
1969     /// == num-statements, then the start of the terminator.
1970     pub statement_index: usize,
1971 }
1972
1973 impl fmt::Debug for Location {
1974     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1975         write!(fmt, "{:?}[{}]", self.block, self.statement_index)
1976     }
1977 }
1978
1979 impl Location {
1980     pub const START: Location = Location {
1981         block: START_BLOCK,
1982         statement_index: 0,
1983     };
1984
1985     /// Returns the location immediately after this one within the enclosing block.
1986     ///
1987     /// Note that if this location represents a terminator, then the
1988     /// resulting location would be out of bounds and invalid.
1989     pub fn successor_within_block(&self) -> Location {
1990         Location { block: self.block, statement_index: self.statement_index + 1 }
1991     }
1992
1993     pub fn dominates(&self, other: Location, dominators: &Dominators<BasicBlock>) -> bool {
1994         if self.block == other.block {
1995             self.statement_index <= other.statement_index
1996         } else {
1997             dominators.is_dominated_by(other.block, self.block)
1998         }
1999     }
2000 }
2001
2002 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
2003 pub enum UnsafetyViolationKind {
2004     General,
2005     ExternStatic(ast::NodeId),
2006     BorrowPacked(ast::NodeId),
2007 }
2008
2009 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
2010 pub struct UnsafetyViolation {
2011     pub source_info: SourceInfo,
2012     pub description: InternedString,
2013     pub kind: UnsafetyViolationKind,
2014 }
2015
2016 #[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
2017 pub struct UnsafetyCheckResult {
2018     /// Violations that are propagated *upwards* from this function
2019     pub violations: Lrc<[UnsafetyViolation]>,
2020     /// unsafe blocks in this function, along with whether they are used. This is
2021     /// used for the "unused_unsafe" lint.
2022     pub unsafe_blocks: Lrc<[(ast::NodeId, bool)]>,
2023 }
2024
2025 /// The layout of generator state
2026 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
2027 pub struct GeneratorLayout<'tcx> {
2028     pub fields: Vec<LocalDecl<'tcx>>,
2029 }
2030
2031 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
2032 pub struct BorrowCheckResult<'gcx> {
2033     pub closure_requirements: Option<ClosureRegionRequirements<'gcx>>,
2034     pub used_mut_upvars: SmallVec<[Field; 8]>,
2035 }
2036
2037 /// After we borrow check a closure, we are left with various
2038 /// requirements that we have inferred between the free regions that
2039 /// appear in the closure's signature or on its field types.  These
2040 /// requirements are then verified and proved by the closure's
2041 /// creating function. This struct encodes those requirements.
2042 ///
2043 /// The requirements are listed as being between various
2044 /// `RegionVid`. The 0th region refers to `'static`; subsequent region
2045 /// vids refer to the free regions that appear in the closure (or
2046 /// generator's) type, in order of appearance. (This numbering is
2047 /// actually defined by the `UniversalRegions` struct in the NLL
2048 /// region checker. See for example
2049 /// `UniversalRegions::closure_mapping`.) Note that we treat the free
2050 /// regions in the closure's type "as if" they were erased, so their
2051 /// precise identity is not important, only their position.
2052 ///
2053 /// Example: If type check produces a closure with the closure substs:
2054 ///
2055 /// ```text
2056 /// ClosureSubsts = [
2057 ///     i8,                                  // the "closure kind"
2058 ///     for<'x> fn(&'a &'x u32) -> &'x u32,  // the "closure signature"
2059 ///     &'a String,                          // some upvar
2060 /// ]
2061 /// ```
2062 ///
2063 /// here, there is one unique free region (`'a`) but it appears
2064 /// twice. We would "renumber" each occurrence to a unique vid, as follows:
2065 ///
2066 /// ```text
2067 /// ClosureSubsts = [
2068 ///     i8,                                  // the "closure kind"
2069 ///     for<'x> fn(&'1 &'x u32) -> &'x u32,  // the "closure signature"
2070 ///     &'2 String,                          // some upvar
2071 /// ]
2072 /// ```
2073 ///
2074 /// Now the code might impose a requirement like `'1: '2`. When an
2075 /// instance of the closure is created, the corresponding free regions
2076 /// can be extracted from its type and constrained to have the given
2077 /// outlives relationship.
2078 ///
2079 /// In some cases, we have to record outlives requirements between
2080 /// types and regions as well. In that case, if those types include
2081 /// any regions, those regions are recorded as `ReClosureBound`
2082 /// instances assigned one of these same indices. Those regions will
2083 /// be substituted away by the creator. We use `ReClosureBound` in
2084 /// that case because the regions must be allocated in the global
2085 /// TyCtxt, and hence we cannot use `ReVar` (which is what we use
2086 /// internally within the rest of the NLL code).
2087 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
2088 pub struct ClosureRegionRequirements<'gcx> {
2089     /// The number of external regions defined on the closure.  In our
2090     /// example above, it would be 3 -- one for `'static`, then `'1`
2091     /// and `'2`. This is just used for a sanity check later on, to
2092     /// make sure that the number of regions we see at the callsite
2093     /// matches.
2094     pub num_external_vids: usize,
2095
2096     /// Requirements between the various free regions defined in
2097     /// indices.
2098     pub outlives_requirements: Vec<ClosureOutlivesRequirement<'gcx>>,
2099 }
2100
2101 /// Indicates an outlives constraint between a type or between two
2102 /// free-regions declared on the closure.
2103 #[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
2104 pub struct ClosureOutlivesRequirement<'tcx> {
2105     // This region or type ...
2106     pub subject: ClosureOutlivesSubject<'tcx>,
2107
2108     // .. must outlive this one.
2109     pub outlived_free_region: ty::RegionVid,
2110
2111     // If not, report an error here.
2112     pub blame_span: Span,
2113 }
2114
2115 /// The subject of a ClosureOutlivesRequirement -- that is, the thing
2116 /// that must outlive some region.
2117 #[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
2118 pub enum ClosureOutlivesSubject<'tcx> {
2119     /// Subject is a type, typically a type parameter, but could also
2120     /// be a projection. Indicates a requirement like `T: 'a` being
2121     /// passed to the caller, where the type here is `T`.
2122     ///
2123     /// The type here is guaranteed not to contain any free regions at
2124     /// present.
2125     Ty(Ty<'tcx>),
2126
2127     /// Subject is a free region from the closure. Indicates a requirement
2128     /// like `'a: 'b` being passed to the caller; the region here is `'a`.
2129     Region(ty::RegionVid),
2130 }
2131
2132 /*
2133  * TypeFoldable implementations for MIR types
2134  */
2135
2136 CloneTypeFoldableAndLiftImpls! {
2137     Mutability,
2138     SourceInfo,
2139     UpvarDecl,
2140     ValidationOp,
2141     VisibilityScopeData,
2142     VisibilityScope,
2143     VisibilityScopeInfo,
2144 }
2145
2146 BraceStructTypeFoldableImpl! {
2147     impl<'tcx> TypeFoldable<'tcx> for Mir<'tcx> {
2148         basic_blocks,
2149         visibility_scopes,
2150         visibility_scope_info,
2151         promoted,
2152         yield_ty,
2153         generator_drop,
2154         generator_layout,
2155         local_decls,
2156         arg_count,
2157         upvar_decls,
2158         spread_arg,
2159         span,
2160         cache,
2161     }
2162 }
2163
2164 BraceStructTypeFoldableImpl! {
2165     impl<'tcx> TypeFoldable<'tcx> for GeneratorLayout<'tcx> {
2166         fields
2167     }
2168 }
2169
2170 BraceStructTypeFoldableImpl! {
2171     impl<'tcx> TypeFoldable<'tcx> for LocalDecl<'tcx> {
2172         mutability,
2173         is_user_variable,
2174         internal,
2175         ty,
2176         name,
2177         source_info,
2178         syntactic_scope,
2179     }
2180 }
2181
2182 BraceStructTypeFoldableImpl! {
2183     impl<'tcx> TypeFoldable<'tcx> for BasicBlockData<'tcx> {
2184         statements,
2185         terminator,
2186         is_cleanup,
2187     }
2188 }
2189
2190 BraceStructTypeFoldableImpl! {
2191     impl<'tcx> TypeFoldable<'tcx> for ValidationOperand<'tcx, Place<'tcx>> {
2192         place, ty, re, mutbl
2193     }
2194 }
2195
2196 BraceStructTypeFoldableImpl! {
2197     impl<'tcx> TypeFoldable<'tcx> for Statement<'tcx> {
2198         source_info, kind
2199     }
2200 }
2201
2202 EnumTypeFoldableImpl! {
2203     impl<'tcx> TypeFoldable<'tcx> for StatementKind<'tcx> {
2204         (StatementKind::Assign)(a, b),
2205         (StatementKind::SetDiscriminant) { place, variant_index },
2206         (StatementKind::StorageLive)(a),
2207         (StatementKind::StorageDead)(a),
2208         (StatementKind::InlineAsm) { asm, outputs, inputs },
2209         (StatementKind::Validate)(a, b),
2210         (StatementKind::EndRegion)(a),
2211         (StatementKind::UserAssertTy)(a, b),
2212         (StatementKind::Nop),
2213     }
2214 }
2215
2216 EnumTypeFoldableImpl! {
2217     impl<'tcx, T> TypeFoldable<'tcx> for ClearCrossCrate<T> {
2218         (ClearCrossCrate::Clear),
2219         (ClearCrossCrate::Set)(a),
2220     } where T: TypeFoldable<'tcx>
2221 }
2222
2223 impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
2224     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
2225         use mir::TerminatorKind::*;
2226
2227         let kind = match self.kind {
2228             Goto { target } => Goto { target: target },
2229             SwitchInt { ref discr, switch_ty, ref values, ref targets } => SwitchInt {
2230                 discr: discr.fold_with(folder),
2231                 switch_ty: switch_ty.fold_with(folder),
2232                 values: values.clone(),
2233                 targets: targets.clone()
2234             },
2235             Drop { ref location, target, unwind } => Drop {
2236                 location: location.fold_with(folder),
2237                 target,
2238                 unwind,
2239             },
2240             DropAndReplace { ref location, ref value, target, unwind } => DropAndReplace {
2241                 location: location.fold_with(folder),
2242                 value: value.fold_with(folder),
2243                 target,
2244                 unwind,
2245             },
2246             Yield { ref value, resume, drop } => Yield {
2247                 value: value.fold_with(folder),
2248                 resume: resume,
2249                 drop: drop,
2250             },
2251             Call { ref func, ref args, ref destination, cleanup } => {
2252                 let dest = destination.as_ref().map(|&(ref loc, dest)| {
2253                     (loc.fold_with(folder), dest)
2254                 });
2255
2256                 Call {
2257                     func: func.fold_with(folder),
2258                     args: args.fold_with(folder),
2259                     destination: dest,
2260                     cleanup,
2261                 }
2262             },
2263             Assert { ref cond, expected, ref msg, target, cleanup } => {
2264                 let msg = if let EvalErrorKind::BoundsCheck { ref len, ref index } = *msg {
2265                     EvalErrorKind::BoundsCheck {
2266                         len: len.fold_with(folder),
2267                         index: index.fold_with(folder),
2268                     }
2269                 } else {
2270                     msg.clone()
2271                 };
2272                 Assert {
2273                     cond: cond.fold_with(folder),
2274                     expected,
2275                     msg,
2276                     target,
2277                     cleanup,
2278                 }
2279             },
2280             GeneratorDrop => GeneratorDrop,
2281             Resume => Resume,
2282             Abort => Abort,
2283             Return => Return,
2284             Unreachable => Unreachable,
2285             FalseEdges { real_target, ref imaginary_targets } =>
2286                 FalseEdges { real_target, imaginary_targets: imaginary_targets.clone() },
2287             FalseUnwind { real_target, unwind } => FalseUnwind { real_target, unwind },
2288         };
2289         Terminator {
2290             source_info: self.source_info,
2291             kind,
2292         }
2293     }
2294
2295     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
2296         use mir::TerminatorKind::*;
2297
2298         match self.kind {
2299             SwitchInt { ref discr, switch_ty, .. } =>
2300                 discr.visit_with(visitor) || switch_ty.visit_with(visitor),
2301             Drop { ref location, ..} => location.visit_with(visitor),
2302             DropAndReplace { ref location, ref value, ..} =>
2303                 location.visit_with(visitor) || value.visit_with(visitor),
2304             Yield { ref value, ..} =>
2305                 value.visit_with(visitor),
2306             Call { ref func, ref args, ref destination, .. } => {
2307                 let dest = if let Some((ref loc, _)) = *destination {
2308                     loc.visit_with(visitor)
2309                 } else { false };
2310                 dest || func.visit_with(visitor) || args.visit_with(visitor)
2311             },
2312             Assert { ref cond, ref msg, .. } => {
2313                 if cond.visit_with(visitor) {
2314                     if let EvalErrorKind::BoundsCheck { ref len, ref index } = *msg {
2315                         len.visit_with(visitor) || index.visit_with(visitor)
2316                     } else {
2317                         false
2318                     }
2319                 } else {
2320                     false
2321                 }
2322             },
2323             Goto { .. } |
2324             Resume |
2325             Abort |
2326             Return |
2327             GeneratorDrop |
2328             Unreachable |
2329             FalseEdges { .. } |
2330             FalseUnwind { .. } => false
2331         }
2332     }
2333 }
2334
2335 impl<'tcx> TypeFoldable<'tcx> for Place<'tcx> {
2336     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
2337         match self {
2338             &Place::Projection(ref p) => Place::Projection(p.fold_with(folder)),
2339             _ => self.clone()
2340         }
2341     }
2342
2343     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
2344         if let &Place::Projection(ref p) = self {
2345             p.visit_with(visitor)
2346         } else {
2347             false
2348         }
2349     }
2350 }
2351
2352 impl<'tcx> TypeFoldable<'tcx> for Rvalue<'tcx> {
2353     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
2354         use mir::Rvalue::*;
2355         match *self {
2356             Use(ref op) => Use(op.fold_with(folder)),
2357             Repeat(ref op, len) => Repeat(op.fold_with(folder), len),
2358             Ref(region, bk, ref place) =>
2359                 Ref(region.fold_with(folder), bk, place.fold_with(folder)),
2360             Len(ref place) => Len(place.fold_with(folder)),
2361             Cast(kind, ref op, ty) => Cast(kind, op.fold_with(folder), ty.fold_with(folder)),
2362             BinaryOp(op, ref rhs, ref lhs) =>
2363                 BinaryOp(op, rhs.fold_with(folder), lhs.fold_with(folder)),
2364             CheckedBinaryOp(op, ref rhs, ref lhs) =>
2365                 CheckedBinaryOp(op, rhs.fold_with(folder), lhs.fold_with(folder)),
2366             UnaryOp(op, ref val) => UnaryOp(op, val.fold_with(folder)),
2367             Discriminant(ref place) => Discriminant(place.fold_with(folder)),
2368             NullaryOp(op, ty) => NullaryOp(op, ty.fold_with(folder)),
2369             Aggregate(ref kind, ref fields) => {
2370                 let kind = box match **kind {
2371                     AggregateKind::Array(ty) => AggregateKind::Array(ty.fold_with(folder)),
2372                     AggregateKind::Tuple => AggregateKind::Tuple,
2373                     AggregateKind::Adt(def, v, substs, n) =>
2374                         AggregateKind::Adt(def, v, substs.fold_with(folder), n),
2375                     AggregateKind::Closure(id, substs) =>
2376                         AggregateKind::Closure(id, substs.fold_with(folder)),
2377                     AggregateKind::Generator(id, substs, movablity) =>
2378                         AggregateKind::Generator(id, substs.fold_with(folder), movablity),
2379                 };
2380                 Aggregate(kind, fields.fold_with(folder))
2381             }
2382         }
2383     }
2384
2385     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
2386         use mir::Rvalue::*;
2387         match *self {
2388             Use(ref op) => op.visit_with(visitor),
2389             Repeat(ref op, _) => op.visit_with(visitor),
2390             Ref(region, _, ref place) => region.visit_with(visitor) || place.visit_with(visitor),
2391             Len(ref place) => place.visit_with(visitor),
2392             Cast(_, ref op, ty) => op.visit_with(visitor) || ty.visit_with(visitor),
2393             BinaryOp(_, ref rhs, ref lhs) |
2394             CheckedBinaryOp(_, ref rhs, ref lhs) =>
2395                 rhs.visit_with(visitor) || lhs.visit_with(visitor),
2396             UnaryOp(_, ref val) => val.visit_with(visitor),
2397             Discriminant(ref place) => place.visit_with(visitor),
2398             NullaryOp(_, ty) => ty.visit_with(visitor),
2399             Aggregate(ref kind, ref fields) => {
2400                 (match **kind {
2401                     AggregateKind::Array(ty) => ty.visit_with(visitor),
2402                     AggregateKind::Tuple => false,
2403                     AggregateKind::Adt(_, _, substs, _) => substs.visit_with(visitor),
2404                     AggregateKind::Closure(_, substs) => substs.visit_with(visitor),
2405                     AggregateKind::Generator(_, substs, _) => substs.visit_with(visitor),
2406                 }) || fields.visit_with(visitor)
2407             }
2408         }
2409     }
2410 }
2411
2412 impl<'tcx> TypeFoldable<'tcx> for Operand<'tcx> {
2413     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
2414         match *self {
2415             Operand::Copy(ref place) => Operand::Copy(place.fold_with(folder)),
2416             Operand::Move(ref place) => Operand::Move(place.fold_with(folder)),
2417             Operand::Constant(ref c) => Operand::Constant(c.fold_with(folder)),
2418         }
2419     }
2420
2421     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
2422         match *self {
2423             Operand::Copy(ref place) |
2424             Operand::Move(ref place) => place.visit_with(visitor),
2425             Operand::Constant(ref c) => c.visit_with(visitor)
2426         }
2427     }
2428 }
2429
2430 impl<'tcx, B, V, T> TypeFoldable<'tcx> for Projection<'tcx, B, V, T>
2431     where B: TypeFoldable<'tcx>, V: TypeFoldable<'tcx>, T: TypeFoldable<'tcx>
2432 {
2433     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
2434         use mir::ProjectionElem::*;
2435
2436         let base = self.base.fold_with(folder);
2437         let elem = match self.elem {
2438             Deref => Deref,
2439             Field(f, ref ty) => Field(f, ty.fold_with(folder)),
2440             Index(ref v) => Index(v.fold_with(folder)),
2441             ref elem => elem.clone()
2442         };
2443
2444         Projection {
2445             base,
2446             elem,
2447         }
2448     }
2449
2450     fn super_visit_with<Vs: TypeVisitor<'tcx>>(&self, visitor: &mut Vs) -> bool {
2451         use mir::ProjectionElem::*;
2452
2453         self.base.visit_with(visitor) ||
2454             match self.elem {
2455                 Field(_, ref ty) => ty.visit_with(visitor),
2456                 Index(ref v) => v.visit_with(visitor),
2457                 _ => false
2458             }
2459     }
2460 }
2461
2462 impl<'tcx> TypeFoldable<'tcx> for Field {
2463     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, _: &mut F) -> Self {
2464         *self
2465     }
2466     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> bool {
2467         false
2468     }
2469 }
2470
2471 impl<'tcx> TypeFoldable<'tcx> for Constant<'tcx> {
2472     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
2473         Constant {
2474             span: self.span.clone(),
2475             ty: self.ty.fold_with(folder),
2476             literal: self.literal.fold_with(folder)
2477         }
2478     }
2479     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
2480         self.ty.visit_with(visitor) || self.literal.visit_with(visitor)
2481     }
2482 }
2483
2484 impl<'tcx> TypeFoldable<'tcx> for Literal<'tcx> {
2485     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
2486         match *self {
2487             Literal::Value { value } => Literal::Value {
2488                 value: value.fold_with(folder)
2489             },
2490             Literal::Promoted { index } => Literal::Promoted { index }
2491         }
2492     }
2493     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
2494         match *self {
2495             Literal::Value { value } => value.visit_with(visitor),
2496             Literal::Promoted { .. } => false
2497         }
2498     }
2499 }