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.
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.
11 //! MIR datatypes and passes. See the module-level [README] for details.
13 //! [README]: https://github.com/rust-lang/rust/blob/master/src/librustc/mir/README.md
15 use graphviz::IntoCow;
16 use middle::const_val::ConstVal;
18 use rustc_const_math::{ConstUsize, ConstMathErr};
19 use rustc_data_structures::sync::{Lrc};
20 use rustc_data_structures::indexed_vec::{IndexVec, Idx};
21 use rustc_data_structures::control_flow_graph::dominators::{Dominators, dominators};
22 use rustc_data_structures::control_flow_graph::{GraphPredecessors, GraphSuccessors};
23 use rustc_data_structures::control_flow_graph::ControlFlowGraph;
24 use rustc_serialize as serialize;
25 use hir::def::CtorKind;
26 use hir::def_id::DefId;
27 use mir::visit::MirVisitable;
28 use mir::interpret::{Value, PrimVal};
29 use ty::subst::{Subst, Substs};
30 use ty::{self, AdtDef, ClosureSubsts, Region, Ty, TyCtxt, GeneratorInterior};
31 use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
35 use hir::{self, InlineAsm};
36 use std::borrow::{Cow};
38 use std::fmt::{self, Debug, Formatter, Write};
39 use std::{iter, mem, u32};
40 use std::ops::{Index, IndexMut};
41 use std::vec::IntoIter;
42 use syntax::ast::{self, Name};
43 use syntax::symbol::InternedString;
44 use syntax_pos::{Span, DUMMY_SP};
54 type LocalDecls<'tcx> = IndexVec<Local, LocalDecl<'tcx>>;
56 pub trait HasLocalDecls<'tcx> {
57 fn local_decls(&self) -> &LocalDecls<'tcx>;
60 impl<'tcx> HasLocalDecls<'tcx> for LocalDecls<'tcx> {
61 fn local_decls(&self) -> &LocalDecls<'tcx> {
66 impl<'tcx> HasLocalDecls<'tcx> for Mir<'tcx> {
67 fn local_decls(&self) -> &LocalDecls<'tcx> {
72 /// Lowered representation of a single function.
73 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
74 pub struct Mir<'tcx> {
75 /// List of basic blocks. References to basic block use a newtyped index type `BasicBlock`
76 /// that indexes into this vector.
77 basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
79 /// List of visibility (lexical) scopes; these are referenced by statements
80 /// and used (eventually) for debuginfo. Indexed by a `VisibilityScope`.
81 pub visibility_scopes: IndexVec<VisibilityScope, VisibilityScopeData>,
83 /// Crate-local information for each visibility scope, that can't (and
84 /// needn't) be tracked across crates.
85 pub visibility_scope_info: ClearCrossCrate<IndexVec<VisibilityScope, VisibilityScopeInfo>>,
87 /// Rvalues promoted from this function, such as borrows of constants.
88 /// Each of them is the Mir of a constant with the fn's type parameters
89 /// in scope, but a separate set of locals.
90 pub promoted: IndexVec<Promoted, Mir<'tcx>>,
92 /// Yield type of the function, if it is a generator.
93 pub yield_ty: Option<Ty<'tcx>>,
95 /// Generator drop glue
96 pub generator_drop: Option<Box<Mir<'tcx>>>,
98 /// The layout of a generator. Produced by the state transformation.
99 pub generator_layout: Option<GeneratorLayout<'tcx>>,
101 /// Declarations of locals.
103 /// The first local is the return value pointer, followed by `arg_count`
104 /// locals for the function arguments, followed by any user-declared
105 /// variables and temporaries.
106 pub local_decls: LocalDecls<'tcx>,
108 /// Number of arguments this function takes.
110 /// Starting at local 1, `arg_count` locals will be provided by the caller
111 /// and can be assumed to be initialized.
113 /// If this MIR was built for a constant, this will be 0.
114 pub arg_count: usize,
116 /// Names and capture modes of all the closure upvars, assuming
117 /// the first argument is either the closure or a reference to it.
118 pub upvar_decls: Vec<UpvarDecl>,
120 /// Mark an argument local (which must be a tuple) as getting passed as
121 /// its individual components at the LLVM level.
123 /// This is used for the "rust-call" ABI.
124 pub spread_arg: Option<Local>,
126 /// A span representing this MIR, for error reporting
129 /// A cache for various calculations
133 /// where execution begins
134 pub const START_BLOCK: BasicBlock = BasicBlock(0);
136 impl<'tcx> Mir<'tcx> {
137 pub fn new(basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
138 visibility_scopes: IndexVec<VisibilityScope, VisibilityScopeData>,
139 visibility_scope_info: ClearCrossCrate<IndexVec<VisibilityScope,
140 VisibilityScopeInfo>>,
141 promoted: IndexVec<Promoted, Mir<'tcx>>,
142 yield_ty: Option<Ty<'tcx>>,
143 local_decls: IndexVec<Local, LocalDecl<'tcx>>,
145 upvar_decls: Vec<UpvarDecl>,
148 // We need `arg_count` locals, and one for the return place
149 assert!(local_decls.len() >= arg_count + 1,
150 "expected at least {} locals, got {}", arg_count + 1, local_decls.len());
155 visibility_scope_info,
158 generator_drop: None,
159 generator_layout: None,
165 cache: cache::Cache::new()
170 pub fn basic_blocks(&self) -> &IndexVec<BasicBlock, BasicBlockData<'tcx>> {
175 pub fn basic_blocks_mut(&mut self) -> &mut IndexVec<BasicBlock, BasicBlockData<'tcx>> {
176 self.cache.invalidate();
177 &mut self.basic_blocks
181 pub fn basic_blocks_and_local_decls_mut(&mut self) -> (
182 &mut IndexVec<BasicBlock, BasicBlockData<'tcx>>,
183 &mut LocalDecls<'tcx>,
185 self.cache.invalidate();
186 (&mut self.basic_blocks, &mut self.local_decls)
190 pub fn predecessors(&self) -> Ref<IndexVec<BasicBlock, Vec<BasicBlock>>> {
191 self.cache.predecessors(self)
195 pub fn predecessors_for(&self, bb: BasicBlock) -> Ref<Vec<BasicBlock>> {
196 Ref::map(self.predecessors(), |p| &p[bb])
200 pub fn dominators(&self) -> Dominators<BasicBlock> {
205 pub fn local_kind(&self, local: Local) -> LocalKind {
206 let index = local.0 as usize;
208 debug_assert!(self.local_decls[local].mutability == Mutability::Mut,
209 "return place should be mutable");
211 LocalKind::ReturnPointer
212 } else if index < self.arg_count + 1 {
214 } else if self.local_decls[local].name.is_some() {
217 debug_assert!(self.local_decls[local].mutability == Mutability::Mut,
218 "temp should be mutable");
224 /// Returns an iterator over all temporaries.
226 pub fn temps_iter<'a>(&'a self) -> impl Iterator<Item=Local> + 'a {
227 (self.arg_count+1..self.local_decls.len()).filter_map(move |index| {
228 let local = Local::new(index);
229 if self.local_decls[local].is_user_variable {
237 /// Returns an iterator over all user-declared locals.
239 pub fn vars_iter<'a>(&'a self) -> impl Iterator<Item=Local> + 'a {
240 (self.arg_count+1..self.local_decls.len()).filter_map(move |index| {
241 let local = Local::new(index);
242 if self.local_decls[local].is_user_variable {
250 /// Returns an iterator over all function arguments.
252 pub fn args_iter(&self) -> impl Iterator<Item=Local> {
253 let arg_count = self.arg_count;
254 (1..arg_count+1).map(Local::new)
257 /// Returns an iterator over all user-defined variables and compiler-generated temporaries (all
258 /// locals that are neither arguments nor the return place).
260 pub fn vars_and_temps_iter(&self) -> impl Iterator<Item=Local> {
261 let arg_count = self.arg_count;
262 let local_count = self.local_decls.len();
263 (arg_count+1..local_count).map(Local::new)
266 /// Changes a statement to a nop. This is both faster than deleting instructions and avoids
267 /// invalidating statement indices in `Location`s.
268 pub fn make_statement_nop(&mut self, location: Location) {
269 let block = &mut self[location.block];
270 debug_assert!(location.statement_index < block.statements.len());
271 block.statements[location.statement_index].make_nop()
274 /// Returns the source info associated with `location`.
275 pub fn source_info(&self, location: Location) -> &SourceInfo {
276 let block = &self[location.block];
277 let stmts = &block.statements;
278 let idx = location.statement_index;
279 if idx < stmts.len() {
280 &stmts[idx].source_info
282 assert!(idx == stmts.len());
283 &block.terminator().source_info
287 /// Return the return type, it always return first element from `local_decls` array
288 pub fn return_ty(&self) -> Ty<'tcx> {
289 self.local_decls[RETURN_PLACE].ty
293 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
294 pub struct VisibilityScopeInfo {
295 /// A NodeId with lint levels equivalent to this scope's lint levels.
296 pub lint_root: ast::NodeId,
297 /// The unsafe block that contains this node.
301 #[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
304 /// Unsafe because of a PushUnsafeBlock
306 /// Unsafe because of an unsafe fn
308 /// Unsafe because of an `unsafe` block
309 ExplicitUnsafe(ast::NodeId)
312 impl_stable_hash_for!(struct Mir<'tcx> {
315 visibility_scope_info,
328 impl<'tcx> Index<BasicBlock> for Mir<'tcx> {
329 type Output = BasicBlockData<'tcx>;
332 fn index(&self, index: BasicBlock) -> &BasicBlockData<'tcx> {
333 &self.basic_blocks()[index]
337 impl<'tcx> IndexMut<BasicBlock> for Mir<'tcx> {
339 fn index_mut(&mut self, index: BasicBlock) -> &mut BasicBlockData<'tcx> {
340 &mut self.basic_blocks_mut()[index]
344 #[derive(Clone, Debug)]
345 pub enum ClearCrossCrate<T> {
350 impl<T: serialize::Encodable> serialize::UseSpecializedEncodable for ClearCrossCrate<T> {}
351 impl<T: serialize::Decodable> serialize::UseSpecializedDecodable for ClearCrossCrate<T> {}
353 /// Grouped information about the source code origin of a MIR entity.
354 /// Intended to be inspected by diagnostics and debuginfo.
355 /// Most passes can work with it as a whole, within a single function.
356 #[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
357 pub struct SourceInfo {
358 /// Source span for the AST pertaining to this MIR entity.
361 /// The lexical visibility scope, i.e. which bindings can be seen.
362 pub scope: VisibilityScope
365 ///////////////////////////////////////////////////////////////////////////
366 // Mutability and borrow kinds
368 #[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
369 pub enum Mutability {
374 #[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
375 pub enum BorrowKind {
376 /// Data must be immutable and is aliasable.
379 /// Data must be immutable but not aliasable. This kind of borrow
380 /// cannot currently be expressed by the user and is used only in
381 /// implicit closure bindings. It is needed when you the closure
382 /// is borrowing or mutating a mutable referent, e.g.:
384 /// let x: &mut isize = ...;
385 /// let y = || *x += 5;
387 /// If we were to try to translate this closure into a more explicit
388 /// form, we'd encounter an error with the code as written:
390 /// struct Env { x: & &mut isize }
391 /// let x: &mut isize = ...;
392 /// let y = (&mut Env { &x }, fn_ptr); // Closure is pair of env and fn
393 /// fn fn_ptr(env: &mut Env) { **env.x += 5; }
395 /// This is then illegal because you cannot mutate a `&mut` found
396 /// in an aliasable location. To solve, you'd have to translate with
397 /// an `&mut` borrow:
399 /// struct Env { x: & &mut isize }
400 /// let x: &mut isize = ...;
401 /// let y = (&mut Env { &mut x }, fn_ptr); // changed from &x to &mut x
402 /// fn fn_ptr(env: &mut Env) { **env.x += 5; }
404 /// Now the assignment to `**env.x` is legal, but creating a
405 /// mutable pointer to `x` is not because `x` is not mutable. We
406 /// could fix this by declaring `x` as `let mut x`. This is ok in
407 /// user code, if awkward, but extra weird for closures, since the
408 /// borrow is hidden.
410 /// So we introduce a "unique imm" borrow -- the referent is
411 /// immutable, but not aliasable. This solves the problem. For
412 /// simplicity, we don't give users the way to express this
413 /// borrow, it's just used when translating closures.
416 /// Data is mutable and not aliasable.
418 /// True if this borrow arose from method-call auto-ref
419 /// (i.e. `adjustment::Adjust::Borrow`)
420 allow_two_phase_borrow: bool
425 pub fn allows_two_phase_borrow(&self) -> bool {
427 BorrowKind::Shared | BorrowKind::Unique => false,
428 BorrowKind::Mut { allow_two_phase_borrow } => allow_two_phase_borrow,
433 ///////////////////////////////////////////////////////////////////////////
434 // Variables and temps
438 DEBUG_FORMAT = "_{}",
439 const RETURN_PLACE = 0,
442 /// Classifies locals into categories. See `Mir::local_kind`.
443 #[derive(PartialEq, Eq, Debug)]
445 /// User-declared variable binding
447 /// Compiler-introduced temporary
449 /// Function argument
451 /// Location of function's return value
457 /// This can be a binding declared by the user, a temporary inserted by the compiler, a function
458 /// argument, or the return place.
459 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
460 pub struct LocalDecl<'tcx> {
461 /// `let mut x` vs `let x`.
463 /// Temporaries and the return place are always mutable.
464 pub mutability: Mutability,
466 /// True if this corresponds to a user-declared local variable.
467 pub is_user_variable: bool,
469 /// True if this is an internal local
471 /// These locals are not based on types in the source code and are only used
472 /// for a few desugarings at the moment.
474 /// The generator transformation will sanity check the locals which are live
475 /// across a suspension point against the type components of the generator
476 /// which type checking knows are live across a suspension point. We need to
477 /// flag drop flags to avoid triggering this check as they are introduced
480 /// Unsafety checking will also ignore dereferences of these locals,
481 /// so they can be used for raw pointers only used in a desugaring.
483 /// This should be sound because the drop flags are fully algebraic, and
484 /// therefore don't affect the OIBIT or outlives properties of the
488 /// Type of this local.
491 /// Name of the local, used in debuginfo and pretty-printing.
493 /// Note that function arguments can also have this set to `Some(_)`
494 /// to generate better debuginfo.
495 pub name: Option<Name>,
497 /// Source info of the local.
498 pub source_info: SourceInfo,
500 /// The *syntactic* visibility scope the local is defined
501 /// in. If the local was defined in a let-statement, this
502 /// is *within* the let-statement, rather than outside
505 /// This is needed because visibility scope of locals within a let-statement
508 /// The reason is that we want the local to be *within* the let-statement
509 /// for lint purposes, but we want the local to be *after* the let-statement
510 /// for names-in-scope purposes.
512 /// That's it, if we have a let-statement like the one in this
516 /// fn foo(x: &str) {
517 /// #[allow(unused_mut)]
518 /// let mut x: u32 = { // <- one unused mut
519 /// let mut y: u32 = x.parse().unwrap();
526 /// Then, from a lint point of view, the declaration of `x: u32`
527 /// (and `y: u32`) are within the `#[allow(unused_mut)]` scope - the
528 /// lint scopes are the same as the AST/HIR nesting.
530 /// However, from a name lookup point of view, the scopes look more like
531 /// as if the let-statements were `match` expressions:
534 /// fn foo(x: &str) {
536 /// match x.parse().unwrap() {
545 /// We care about the name-lookup scopes for debuginfo - if the
546 /// debuginfo instruction pointer is at the call to `x.parse()`, we
547 /// want `x` to refer to `x: &str`, but if it is at the call to
548 /// `drop(x)`, we want it to refer to `x: u32`.
550 /// To allow both uses to work, we need to have more than a single scope
551 /// for a local. We have the `syntactic_scope` represent the
552 /// "syntactic" lint scope (with a variable being under its let
553 /// block) while the source-info scope represents the "local variable"
554 /// scope (where the "rest" of a block is under all prior let-statements).
556 /// The end result looks like this:
560 /// │{ argument x: &str }
562 /// │ │{ #[allow(unused_mut] } // this is actually split into 2 scopes
563 /// │ │ // in practice because I'm lazy.
565 /// │ │← x.syntactic_scope
566 /// │ │← `x.parse().unwrap()`
568 /// │ │ │← y.syntactic_scope
570 /// │ │ │{ let y: u32 }
572 /// │ │ │← y.source_info.scope
575 /// │ │{ let x: u32 }
576 /// │ │← x.source_info.scope
577 /// │ │← `drop(x)` // this accesses `x: u32`
579 pub syntactic_scope: VisibilityScope,
582 impl<'tcx> LocalDecl<'tcx> {
583 /// Create a new `LocalDecl` for a temporary.
585 pub fn new_temp(ty: Ty<'tcx>, span: Span) -> Self {
587 mutability: Mutability::Mut,
590 source_info: SourceInfo {
592 scope: ARGUMENT_VISIBILITY_SCOPE
594 syntactic_scope: ARGUMENT_VISIBILITY_SCOPE,
596 is_user_variable: false
600 /// Create a new `LocalDecl` for a internal temporary.
602 pub fn new_internal(ty: Ty<'tcx>, span: Span) -> Self {
604 mutability: Mutability::Mut,
607 source_info: SourceInfo {
609 scope: ARGUMENT_VISIBILITY_SCOPE
611 syntactic_scope: ARGUMENT_VISIBILITY_SCOPE,
613 is_user_variable: false
617 /// Builds a `LocalDecl` for the return place.
619 /// This must be inserted into the `local_decls` list as the first local.
621 pub fn new_return_place(return_ty: Ty, span: Span) -> LocalDecl {
623 mutability: Mutability::Mut,
625 source_info: SourceInfo {
627 scope: ARGUMENT_VISIBILITY_SCOPE
629 syntactic_scope: ARGUMENT_VISIBILITY_SCOPE,
631 name: None, // FIXME maybe we do want some name here?
632 is_user_variable: false
637 /// A closure capture, with its name and mode.
638 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
639 pub struct UpvarDecl {
640 pub debug_name: Name,
642 /// If true, the capture is behind a reference.
645 pub mutability: Mutability,
648 ///////////////////////////////////////////////////////////////////////////
651 newtype_index!(BasicBlock { DEBUG_FORMAT = "bb{}" });
654 pub fn start_location(self) -> Location {
662 ///////////////////////////////////////////////////////////////////////////
663 // BasicBlockData and Terminator
665 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
666 pub struct BasicBlockData<'tcx> {
667 /// List of statements in this block.
668 pub statements: Vec<Statement<'tcx>>,
670 /// Terminator for this block.
672 /// NB. This should generally ONLY be `None` during construction.
673 /// Therefore, you should generally access it via the
674 /// `terminator()` or `terminator_mut()` methods. The only
675 /// exception is that certain passes, such as `simplify_cfg`, swap
676 /// out the terminator temporarily with `None` while they continue
677 /// to recurse over the set of basic blocks.
678 pub terminator: Option<Terminator<'tcx>>,
680 /// If true, this block lies on an unwind path. This is used
681 /// during trans where distinct kinds of basic blocks may be
682 /// generated (particularly for MSVC cleanup). Unwind blocks must
683 /// only branch to other unwind blocks.
684 pub is_cleanup: bool,
687 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
688 pub struct Terminator<'tcx> {
689 pub source_info: SourceInfo,
690 pub kind: TerminatorKind<'tcx>
693 #[derive(Clone, RustcEncodable, RustcDecodable)]
694 pub enum TerminatorKind<'tcx> {
695 /// block should have one successor in the graph; we jump there
700 /// operand evaluates to an integer; jump depending on its value
701 /// to one of the targets, and otherwise fallback to `otherwise`
703 /// discriminant value being tested
704 discr: Operand<'tcx>,
706 /// type of value being tested
709 /// Possible values. The locations to branch to in each case
710 /// are found in the corresponding indices from the `targets` vector.
711 values: Cow<'tcx, [u128]>,
713 /// Possible branch sites. The last element of this vector is used
714 /// for the otherwise branch, so targets.len() == values.len() + 1
716 // This invariant is quite non-obvious and also could be improved.
717 // One way to make this invariant is to have something like this instead:
719 // branches: Vec<(ConstInt, BasicBlock)>,
720 // otherwise: Option<BasicBlock> // exhaustive if None
722 // However we’ve decided to keep this as-is until we figure a case
723 // where some other approach seems to be strictly better than other.
724 targets: Vec<BasicBlock>,
727 /// Indicates that the landing pad is finished and unwinding should
728 /// continue. Emitted by build::scope::diverge_cleanup.
731 /// Indicates that the landing pad is finished and that the process
732 /// should abort. Used to prevent unwinding for foreign items.
735 /// Indicates a normal return. The return place should have
736 /// been filled in by now. This should occur at most once.
739 /// Indicates a terminator that can never be reached.
744 location: Place<'tcx>,
746 unwind: Option<BasicBlock>
749 /// Drop the Place and assign the new value over it. This ensures
750 /// that the assignment to `P` occurs *even if* the destructor for
751 /// place unwinds. Its semantics are best explained by by the
756 /// DropAndReplace(P <- V, goto BB1, unwind BB2)
764 /// Drop(P, goto BB1, unwind BB2)
767 /// // P is now unitialized
771 /// // P is now unitialized -- its dtor panicked
776 location: Place<'tcx>,
777 value: Operand<'tcx>,
779 unwind: Option<BasicBlock>,
782 /// Block ends with a call of a converging function
784 /// The function that’s being called
786 /// Arguments the function is called with.
787 /// These are owned by the callee, which is free to modify them.
788 /// This allows the memory occupied by "by-value" arguments to be
789 /// reused across function calls without duplicating the contents.
790 args: Vec<Operand<'tcx>>,
791 /// Destination for the return value. If some, the call is converging.
792 destination: Option<(Place<'tcx>, BasicBlock)>,
793 /// Cleanups to be done if the call unwinds.
794 cleanup: Option<BasicBlock>
797 /// Jump to the target if the condition has the expected value,
798 /// otherwise panic with a message and a cleanup target.
802 msg: AssertMessage<'tcx>,
804 cleanup: Option<BasicBlock>
809 /// The value to return
810 value: Operand<'tcx>,
811 /// Where to resume to
813 /// Cleanup to be done if the generator is dropped at this suspend point
814 drop: Option<BasicBlock>,
817 /// Indicates the end of the dropping of a generator
820 /// A block where control flow only ever takes one real path, but borrowck
821 /// needs to be more conservative.
823 /// The target normal control flow will take
824 real_target: BasicBlock,
825 /// The list of blocks control flow could conceptually take, but won't
827 imaginary_targets: Vec<BasicBlock>,
829 /// A terminator for blocks that only take one path in reality, but where we
830 /// reserve the right to unwind in borrowck, even if it won't happen in practice.
831 /// This can arise in infinite loops with no function calls for example.
833 /// The target normal control flow will take
834 real_target: BasicBlock,
835 /// The imaginary cleanup block link. This particular path will never be taken
836 /// in practice, but in order to avoid fragility we want to always
837 /// consider it in borrowck. We don't want to accept programs which
838 /// pass borrowck only when panic=abort or some assertions are disabled
839 /// due to release vs. debug mode builds. This needs to be an Option because
840 /// of the remove_noop_landing_pads and no_landing_pads passes
841 unwind: Option<BasicBlock>,
845 impl<'tcx> Terminator<'tcx> {
846 pub fn successors(&self) -> Cow<[BasicBlock]> {
847 self.kind.successors()
850 pub fn successors_mut(&mut self) -> Vec<&mut BasicBlock> {
851 self.kind.successors_mut()
854 pub fn unwind_mut(&mut self) -> Option<&mut Option<BasicBlock>> {
855 self.kind.unwind_mut()
859 impl<'tcx> TerminatorKind<'tcx> {
860 pub fn if_<'a, 'gcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>, cond: Operand<'tcx>,
861 t: BasicBlock, f: BasicBlock) -> TerminatorKind<'tcx> {
862 static BOOL_SWITCH_FALSE: &'static [u128] = &[0];
863 TerminatorKind::SwitchInt {
865 switch_ty: tcx.types.bool,
866 values: From::from(BOOL_SWITCH_FALSE),
871 pub fn successors(&self) -> Cow<[BasicBlock]> {
872 use self::TerminatorKind::*;
874 Goto { target: ref b } => slice::from_ref(b).into_cow(),
875 SwitchInt { targets: ref b, .. } => b[..].into_cow(),
876 Resume | Abort | GeneratorDrop => (&[]).into_cow(),
877 Return => (&[]).into_cow(),
878 Unreachable => (&[]).into_cow(),
879 Call { destination: Some((_, t)), cleanup: Some(c), .. } => vec![t, c].into_cow(),
880 Call { destination: Some((_, ref t)), cleanup: None, .. } =>
881 slice::from_ref(t).into_cow(),
882 Call { destination: None, cleanup: Some(ref c), .. } => slice::from_ref(c).into_cow(),
883 Call { destination: None, cleanup: None, .. } => (&[]).into_cow(),
884 Yield { resume: t, drop: Some(c), .. } => vec![t, c].into_cow(),
885 Yield { resume: ref t, drop: None, .. } => slice::from_ref(t).into_cow(),
886 DropAndReplace { target, unwind: Some(unwind), .. } |
887 Drop { target, unwind: Some(unwind), .. } => {
888 vec![target, unwind].into_cow()
890 DropAndReplace { ref target, unwind: None, .. } |
891 Drop { ref target, unwind: None, .. } => {
892 slice::from_ref(target).into_cow()
894 Assert { target, cleanup: Some(unwind), .. } => vec![target, unwind].into_cow(),
895 Assert { ref target, .. } => slice::from_ref(target).into_cow(),
896 FalseEdges { ref real_target, ref imaginary_targets } => {
897 let mut s = vec![*real_target];
898 s.extend_from_slice(imaginary_targets);
901 FalseUnwind { real_target: t, unwind: Some(u) } => vec![t, u].into_cow(),
902 FalseUnwind { real_target: ref t, unwind: None } => slice::from_ref(t).into_cow(),
906 // FIXME: no mootable cow. I’m honestly not sure what a “cow” between `&mut [BasicBlock]` and
907 // `Vec<&mut BasicBlock>` would look like in the first place.
908 pub fn successors_mut(&mut self) -> Vec<&mut BasicBlock> {
909 use self::TerminatorKind::*;
911 Goto { target: ref mut b } => vec![b],
912 SwitchInt { targets: ref mut b, .. } => b.iter_mut().collect(),
913 Resume | Abort | GeneratorDrop => Vec::new(),
914 Return => Vec::new(),
915 Unreachable => Vec::new(),
916 Call { destination: Some((_, ref mut t)), cleanup: Some(ref mut c), .. } => vec![t, c],
917 Call { destination: Some((_, ref mut t)), cleanup: None, .. } => vec![t],
918 Call { destination: None, cleanup: Some(ref mut c), .. } => vec![c],
919 Call { destination: None, cleanup: None, .. } => vec![],
920 Yield { resume: ref mut t, drop: Some(ref mut c), .. } => vec![t, c],
921 Yield { resume: ref mut t, drop: None, .. } => vec![t],
922 DropAndReplace { ref mut target, unwind: Some(ref mut unwind), .. } |
923 Drop { ref mut target, unwind: Some(ref mut unwind), .. } => vec![target, unwind],
924 DropAndReplace { ref mut target, unwind: None, .. } |
925 Drop { ref mut target, unwind: None, .. } => {
928 Assert { ref mut target, cleanup: Some(ref mut unwind), .. } => vec![target, unwind],
929 Assert { ref mut target, .. } => vec![target],
930 FalseEdges { ref mut real_target, ref mut imaginary_targets } => {
931 let mut s = vec![real_target];
932 s.extend(imaginary_targets.iter_mut());
935 FalseUnwind { real_target: ref mut t, unwind: Some(ref mut u) } => vec![t, u],
936 FalseUnwind { ref mut real_target, unwind: None } => vec![real_target],
940 pub fn unwind_mut(&mut self) -> Option<&mut Option<BasicBlock>> {
942 TerminatorKind::Goto { .. } |
943 TerminatorKind::Resume |
944 TerminatorKind::Abort |
945 TerminatorKind::Return |
946 TerminatorKind::Unreachable |
947 TerminatorKind::GeneratorDrop |
948 TerminatorKind::Yield { .. } |
949 TerminatorKind::SwitchInt { .. } |
950 TerminatorKind::FalseEdges { .. } => {
953 TerminatorKind::Call { cleanup: ref mut unwind, .. } |
954 TerminatorKind::Assert { cleanup: ref mut unwind, .. } |
955 TerminatorKind::DropAndReplace { ref mut unwind, .. } |
956 TerminatorKind::Drop { ref mut unwind, .. } |
957 TerminatorKind::FalseUnwind { ref mut unwind, .. } => {
964 impl<'tcx> BasicBlockData<'tcx> {
965 pub fn new(terminator: Option<Terminator<'tcx>>) -> BasicBlockData<'tcx> {
973 /// Accessor for terminator.
975 /// Terminator may not be None after construction of the basic block is complete. This accessor
976 /// provides a convenience way to reach the terminator.
977 pub fn terminator(&self) -> &Terminator<'tcx> {
978 self.terminator.as_ref().expect("invalid terminator state")
981 pub fn terminator_mut(&mut self) -> &mut Terminator<'tcx> {
982 self.terminator.as_mut().expect("invalid terminator state")
985 pub fn retain_statements<F>(&mut self, mut f: F) where F: FnMut(&mut Statement) -> bool {
986 for s in &mut self.statements {
993 pub fn expand_statements<F, I>(&mut self, mut f: F)
994 where F: FnMut(&mut Statement<'tcx>) -> Option<I>,
995 I: iter::TrustedLen<Item = Statement<'tcx>>
997 // Gather all the iterators we'll need to splice in, and their positions.
998 let mut splices: Vec<(usize, I)> = vec![];
999 let mut extra_stmts = 0;
1000 for (i, s) in self.statements.iter_mut().enumerate() {
1001 if let Some(mut new_stmts) = f(s) {
1002 if let Some(first) = new_stmts.next() {
1003 // We can already store the first new statement.
1006 // Save the other statements for optimized splicing.
1007 let remaining = new_stmts.size_hint().0;
1009 splices.push((i + 1 + extra_stmts, new_stmts));
1010 extra_stmts += remaining;
1018 // Splice in the new statements, from the end of the block.
1019 // FIXME(eddyb) This could be more efficient with a "gap buffer"
1020 // where a range of elements ("gap") is left uninitialized, with
1021 // splicing adding new elements to the end of that gap and moving
1022 // existing elements from before the gap to the end of the gap.
1023 // For now, this is safe code, emulating a gap but initializing it.
1024 let mut gap = self.statements.len()..self.statements.len()+extra_stmts;
1025 self.statements.resize(gap.end, Statement {
1026 source_info: SourceInfo {
1028 scope: ARGUMENT_VISIBILITY_SCOPE
1030 kind: StatementKind::Nop
1032 for (splice_start, new_stmts) in splices.into_iter().rev() {
1033 let splice_end = splice_start + new_stmts.size_hint().0;
1034 while gap.end > splice_end {
1037 self.statements.swap(gap.start, gap.end);
1039 self.statements.splice(splice_start..splice_end, new_stmts);
1040 gap.end = splice_start;
1044 pub fn visitable(&self, index: usize) -> &dyn MirVisitable<'tcx> {
1045 if index < self.statements.len() {
1046 &self.statements[index]
1053 impl<'tcx> Debug for TerminatorKind<'tcx> {
1054 fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
1055 self.fmt_head(fmt)?;
1056 let successors = self.successors();
1057 let labels = self.fmt_successor_labels();
1058 assert_eq!(successors.len(), labels.len());
1060 match successors.len() {
1063 1 => write!(fmt, " -> {:?}", successors[0]),
1066 write!(fmt, " -> [")?;
1067 for (i, target) in successors.iter().enumerate() {
1071 write!(fmt, "{}: {:?}", labels[i], target)?;
1080 impl<'tcx> TerminatorKind<'tcx> {
1081 /// Write the "head" part of the terminator; that is, its name and the data it uses to pick the
1082 /// successor basic block, if any. The only information not included is the list of possible
1083 /// successors, which may be rendered differently between the text and the graphviz format.
1084 pub fn fmt_head<W: Write>(&self, fmt: &mut W) -> fmt::Result {
1085 use self::TerminatorKind::*;
1087 Goto { .. } => write!(fmt, "goto"),
1088 SwitchInt { discr: ref place, .. } => write!(fmt, "switchInt({:?})", place),
1089 Return => write!(fmt, "return"),
1090 GeneratorDrop => write!(fmt, "generator_drop"),
1091 Resume => write!(fmt, "resume"),
1092 Abort => write!(fmt, "abort"),
1093 Yield { ref value, .. } => write!(fmt, "_1 = suspend({:?})", value),
1094 Unreachable => write!(fmt, "unreachable"),
1095 Drop { ref location, .. } => write!(fmt, "drop({:?})", location),
1096 DropAndReplace { ref location, ref value, .. } =>
1097 write!(fmt, "replace({:?} <- {:?})", location, value),
1098 Call { ref func, ref args, ref destination, .. } => {
1099 if let Some((ref destination, _)) = *destination {
1100 write!(fmt, "{:?} = ", destination)?;
1102 write!(fmt, "{:?}(", func)?;
1103 for (index, arg) in args.iter().enumerate() {
1107 write!(fmt, "{:?}", arg)?;
1111 Assert { ref cond, expected, ref msg, .. } => {
1112 write!(fmt, "assert(")?;
1116 write!(fmt, "{:?}, ", cond)?;
1119 AssertMessage::BoundsCheck { ref len, ref index } => {
1120 write!(fmt, "{:?}, {:?}, {:?}",
1121 "index out of bounds: the len is {} but the index is {}",
1124 AssertMessage::Math(ref err) => {
1125 write!(fmt, "{:?}", err.description())?;
1127 AssertMessage::GeneratorResumedAfterReturn => {
1128 write!(fmt, "{:?}", "generator resumed after completion")?;
1130 AssertMessage::GeneratorResumedAfterPanic => {
1131 write!(fmt, "{:?}", "generator resumed after panicking")?;
1137 FalseEdges { .. } => write!(fmt, "falseEdges"),
1138 FalseUnwind { .. } => write!(fmt, "falseUnwind"),
1142 /// Return the list of labels for the edges to the successor basic blocks.
1143 pub fn fmt_successor_labels(&self) -> Vec<Cow<'static, str>> {
1144 use self::TerminatorKind::*;
1146 Return | Resume | Abort | Unreachable | GeneratorDrop => vec![],
1147 Goto { .. } => vec!["".into()],
1148 SwitchInt { ref values, switch_ty, .. } => {
1151 let mut s = String::new();
1153 Value::ByVal(PrimVal::Bytes(u)),
1159 .chain(iter::once(String::from("otherwise").into()))
1162 Call { destination: Some(_), cleanup: Some(_), .. } =>
1163 vec!["return".into_cow(), "unwind".into_cow()],
1164 Call { destination: Some(_), cleanup: None, .. } => vec!["return".into_cow()],
1165 Call { destination: None, cleanup: Some(_), .. } => vec!["unwind".into_cow()],
1166 Call { destination: None, cleanup: None, .. } => vec![],
1167 Yield { drop: Some(_), .. } =>
1168 vec!["resume".into_cow(), "drop".into_cow()],
1169 Yield { drop: None, .. } => vec!["resume".into_cow()],
1170 DropAndReplace { unwind: None, .. } |
1171 Drop { unwind: None, .. } => vec!["return".into_cow()],
1172 DropAndReplace { unwind: Some(_), .. } |
1173 Drop { unwind: Some(_), .. } => {
1174 vec!["return".into_cow(), "unwind".into_cow()]
1176 Assert { cleanup: None, .. } => vec!["".into()],
1178 vec!["success".into_cow(), "unwind".into_cow()],
1179 FalseEdges { ref imaginary_targets, .. } => {
1180 let mut l = vec!["real".into()];
1181 l.resize(imaginary_targets.len() + 1, "imaginary".into());
1184 FalseUnwind { unwind: Some(_), .. } => vec!["real".into(), "cleanup".into()],
1185 FalseUnwind { unwind: None, .. } => vec!["real".into()],
1190 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
1191 pub enum AssertMessage<'tcx> {
1194 index: Operand<'tcx>
1197 GeneratorResumedAfterReturn,
1198 GeneratorResumedAfterPanic,
1201 ///////////////////////////////////////////////////////////////////////////
1204 #[derive(Clone, RustcEncodable, RustcDecodable)]
1205 pub struct Statement<'tcx> {
1206 pub source_info: SourceInfo,
1207 pub kind: StatementKind<'tcx>,
1210 impl<'tcx> Statement<'tcx> {
1211 /// Changes a statement to a nop. This is both faster than deleting instructions and avoids
1212 /// invalidating statement indices in `Location`s.
1213 pub fn make_nop(&mut self) {
1214 self.kind = StatementKind::Nop
1217 /// Changes a statement to a nop and returns the original statement.
1218 pub fn replace_nop(&mut self) -> Self {
1220 source_info: self.source_info,
1221 kind: mem::replace(&mut self.kind, StatementKind::Nop)
1226 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
1227 pub enum StatementKind<'tcx> {
1228 /// Write the RHS Rvalue to the LHS Place.
1229 Assign(Place<'tcx>, Rvalue<'tcx>),
1231 /// Write the discriminant for a variant to the enum Place.
1232 SetDiscriminant { place: Place<'tcx>, variant_index: usize },
1234 /// Start a live range for the storage of the local.
1237 /// End the current live range for the storage of the local.
1240 /// Execute a piece of inline Assembly.
1242 asm: Box<InlineAsm>,
1243 outputs: Vec<Place<'tcx>>,
1244 inputs: Vec<Operand<'tcx>>
1247 /// Assert the given places to be valid inhabitants of their type. These statements are
1248 /// currently only interpreted by miri and only generated when "-Z mir-emit-validate" is passed.
1249 /// See <https://internals.rust-lang.org/t/types-as-contracts/5562/73> for more details.
1250 Validate(ValidationOp, Vec<ValidationOperand<'tcx, Place<'tcx>>>),
1252 /// Mark one terminating point of a region scope (i.e. static region).
1253 /// (The starting point(s) arise implicitly from borrows.)
1254 EndRegion(region::Scope),
1256 /// No-op. Useful for deleting instructions without affecting statement indices.
1260 /// The `ValidationOp` describes what happens with each of the operands of a
1261 /// `Validate` statement.
1262 #[derive(Copy, Clone, RustcEncodable, RustcDecodable, PartialEq, Eq)]
1263 pub enum ValidationOp {
1264 /// Recursively traverse the place following the type and validate that all type
1265 /// invariants are maintained. Furthermore, acquire exclusive/read-only access to the
1266 /// memory reachable from the place.
1268 /// Recursive traverse the *mutable* part of the type and relinquish all exclusive
1271 /// Recursive traverse the *mutable* part of the type and relinquish all exclusive
1272 /// access *until* the given region ends. Then, access will be recovered.
1273 Suspend(region::Scope),
1276 impl Debug for ValidationOp {
1277 fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
1278 use self::ValidationOp::*;
1280 Acquire => write!(fmt, "Acquire"),
1281 Release => write!(fmt, "Release"),
1282 // (reuse lifetime rendering policy from ppaux.)
1283 Suspend(ref ce) => write!(fmt, "Suspend({})", ty::ReScope(*ce)),
1288 // This is generic so that it can be reused by miri
1289 #[derive(Clone, RustcEncodable, RustcDecodable)]
1290 pub struct ValidationOperand<'tcx, T> {
1293 pub re: Option<region::Scope>,
1294 pub mutbl: hir::Mutability,
1297 impl<'tcx, T: Debug> Debug for ValidationOperand<'tcx, T> {
1298 fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
1299 write!(fmt, "{:?}: {:?}", self.place, self.ty)?;
1300 if let Some(ce) = self.re {
1301 // (reuse lifetime rendering policy from ppaux.)
1302 write!(fmt, "/{}", ty::ReScope(ce))?;
1304 if let hir::MutImmutable = self.mutbl {
1305 write!(fmt, " (imm)")?;
1311 impl<'tcx> Debug for Statement<'tcx> {
1312 fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
1313 use self::StatementKind::*;
1315 Assign(ref place, ref rv) => write!(fmt, "{:?} = {:?}", place, rv),
1316 // (reuse lifetime rendering policy from ppaux.)
1317 EndRegion(ref ce) => write!(fmt, "EndRegion({})", ty::ReScope(*ce)),
1318 Validate(ref op, ref places) => write!(fmt, "Validate({:?}, {:?})", op, places),
1319 StorageLive(ref place) => write!(fmt, "StorageLive({:?})", place),
1320 StorageDead(ref place) => write!(fmt, "StorageDead({:?})", place),
1321 SetDiscriminant { ref place, variant_index } => {
1322 write!(fmt, "discriminant({:?}) = {:?}", place, variant_index)
1324 InlineAsm { ref asm, ref outputs, ref inputs } => {
1325 write!(fmt, "asm!({:?} : {:?} : {:?})", asm, outputs, inputs)
1327 Nop => write!(fmt, "nop"),
1332 ///////////////////////////////////////////////////////////////////////////
1335 /// A path to a value; something that can be evaluated without
1336 /// changing or disturbing program state.
1337 #[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
1338 pub enum Place<'tcx> {
1342 /// static or static mut variable
1343 Static(Box<Static<'tcx>>),
1345 /// projection out of a place (access a field, deref a pointer, etc)
1346 Projection(Box<PlaceProjection<'tcx>>),
1349 /// The def-id of a static, along with its normalized type (which is
1350 /// stored to avoid requiring normalization when reading MIR).
1351 #[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
1352 pub struct Static<'tcx> {
1357 impl_stable_hash_for!(struct Static<'tcx> {
1362 /// The `Projection` data structure defines things of the form `B.x`
1363 /// or `*B` or `B[index]`. Note that it is parameterized because it is
1364 /// shared between `Constant` and `Place`. See the aliases
1365 /// `PlaceProjection` etc below.
1366 #[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
1367 pub struct Projection<'tcx, B, V, T> {
1369 pub elem: ProjectionElem<'tcx, V, T>,
1372 #[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
1373 pub enum ProjectionElem<'tcx, V, T> {
1378 /// These indices are generated by slice patterns. Easiest to explain
1382 /// [X, _, .._, _, _] => { offset: 0, min_length: 4, from_end: false },
1383 /// [_, X, .._, _, _] => { offset: 1, min_length: 4, from_end: false },
1384 /// [_, _, .._, X, _] => { offset: 2, min_length: 4, from_end: true },
1385 /// [_, _, .._, _, X] => { offset: 1, min_length: 4, from_end: true },
1388 /// index or -index (in Python terms), depending on from_end
1390 /// thing being indexed must be at least this long
1392 /// counting backwards from end?
1396 /// These indices are generated by slice patterns.
1398 /// slice[from:-to] in Python terms.
1404 /// "Downcast" to a variant of an ADT. Currently, we only introduce
1405 /// this for ADTs with more than one variant. It may be better to
1406 /// just introduce it always, or always for enums.
1407 Downcast(&'tcx AdtDef, usize),
1410 /// Alias for projections as they appear in places, where the base is a place
1411 /// and the index is a local.
1412 pub type PlaceProjection<'tcx> = Projection<'tcx, Place<'tcx>, Local, Ty<'tcx>>;
1414 /// Alias for projections as they appear in places, where the base is a place
1415 /// and the index is a local.
1416 pub type PlaceElem<'tcx> = ProjectionElem<'tcx, Local, Ty<'tcx>>;
1418 newtype_index!(Field { DEBUG_FORMAT = "field[{}]" });
1420 impl<'tcx> Place<'tcx> {
1421 pub fn field(self, f: Field, ty: Ty<'tcx>) -> Place<'tcx> {
1422 self.elem(ProjectionElem::Field(f, ty))
1425 pub fn deref(self) -> Place<'tcx> {
1426 self.elem(ProjectionElem::Deref)
1429 pub fn downcast(self, adt_def: &'tcx AdtDef, variant_index: usize) -> Place<'tcx> {
1430 self.elem(ProjectionElem::Downcast(adt_def, variant_index))
1433 pub fn index(self, index: Local) -> Place<'tcx> {
1434 self.elem(ProjectionElem::Index(index))
1437 pub fn elem(self, elem: PlaceElem<'tcx>) -> Place<'tcx> {
1438 Place::Projection(Box::new(PlaceProjection {
1445 impl<'tcx> Debug for Place<'tcx> {
1446 fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
1450 Local(id) => write!(fmt, "{:?}", id),
1451 Static(box self::Static { def_id, ty }) =>
1452 write!(fmt, "({}: {:?})", ty::tls::with(|tcx| tcx.item_path_str(def_id)), ty),
1453 Projection(ref data) =>
1455 ProjectionElem::Downcast(ref adt_def, index) =>
1456 write!(fmt, "({:?} as {})", data.base, adt_def.variants[index].name),
1457 ProjectionElem::Deref =>
1458 write!(fmt, "(*{:?})", data.base),
1459 ProjectionElem::Field(field, ty) =>
1460 write!(fmt, "({:?}.{:?}: {:?})", data.base, field.index(), ty),
1461 ProjectionElem::Index(ref index) =>
1462 write!(fmt, "{:?}[{:?}]", data.base, index),
1463 ProjectionElem::ConstantIndex { offset, min_length, from_end: false } =>
1464 write!(fmt, "{:?}[{:?} of {:?}]", data.base, offset, min_length),
1465 ProjectionElem::ConstantIndex { offset, min_length, from_end: true } =>
1466 write!(fmt, "{:?}[-{:?} of {:?}]", data.base, offset, min_length),
1467 ProjectionElem::Subslice { from, to } if to == 0 =>
1468 write!(fmt, "{:?}[{:?}:]", data.base, from),
1469 ProjectionElem::Subslice { from, to } if from == 0 =>
1470 write!(fmt, "{:?}[:-{:?}]", data.base, to),
1471 ProjectionElem::Subslice { from, to } =>
1472 write!(fmt, "{:?}[{:?}:-{:?}]", data.base,
1480 ///////////////////////////////////////////////////////////////////////////
1483 newtype_index!(VisibilityScope
1485 DEBUG_FORMAT = "scope[{}]",
1486 const ARGUMENT_VISIBILITY_SCOPE = 0,
1489 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
1490 pub struct VisibilityScopeData {
1492 pub parent_scope: Option<VisibilityScope>,
1495 ///////////////////////////////////////////////////////////////////////////
1498 /// These are values that can appear inside an rvalue (or an index
1499 /// place). They are intentionally limited to prevent rvalues from
1500 /// being nested in one another.
1501 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable)]
1502 pub enum Operand<'tcx> {
1503 /// Copy: The value must be available for use afterwards.
1505 /// This implies that the type of the place must be `Copy`; this is true
1506 /// by construction during build, but also checked by the MIR type checker.
1508 /// Move: The value (including old borrows of it) will not be used again.
1510 /// Safe for values of all types (modulo future developments towards `?Move`).
1511 /// Correct usage patterns are enforced by the borrow checker for safe code.
1512 /// `Copy` may be converted to `Move` to enable "last-use" optimizations.
1514 Constant(Box<Constant<'tcx>>),
1517 impl<'tcx> Debug for Operand<'tcx> {
1518 fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
1519 use self::Operand::*;
1521 Constant(ref a) => write!(fmt, "{:?}", a),
1522 Copy(ref place) => write!(fmt, "{:?}", place),
1523 Move(ref place) => write!(fmt, "move {:?}", place),
1528 impl<'tcx> Operand<'tcx> {
1529 pub fn function_handle<'a>(
1530 tcx: TyCtxt<'a, 'tcx, 'tcx>,
1532 substs: &'tcx Substs<'tcx>,
1535 let ty = tcx.type_of(def_id).subst(tcx, substs);
1536 Operand::Constant(box Constant {
1539 literal: Literal::Value {
1540 value: tcx.mk_const(ty::Const {
1541 // ZST function type
1542 val: ConstVal::Value(Value::ByVal(PrimVal::Undef)),
1549 pub fn to_copy(&self) -> Self {
1551 Operand::Copy(_) | Operand::Constant(_) => self.clone(),
1552 Operand::Move(ref place) => Operand::Copy(place.clone())
1557 ///////////////////////////////////////////////////////////////////////////
1560 #[derive(Clone, RustcEncodable, RustcDecodable)]
1561 pub enum Rvalue<'tcx> {
1562 /// x (either a move or copy, depending on type of x)
1566 Repeat(Operand<'tcx>, ConstUsize),
1569 Ref(Region<'tcx>, BorrowKind, Place<'tcx>),
1571 /// length of a [X] or [X;n] value
1574 Cast(CastKind, Operand<'tcx>, Ty<'tcx>),
1576 BinaryOp(BinOp, Operand<'tcx>, Operand<'tcx>),
1577 CheckedBinaryOp(BinOp, Operand<'tcx>, Operand<'tcx>),
1579 NullaryOp(NullOp, Ty<'tcx>),
1580 UnaryOp(UnOp, Operand<'tcx>),
1582 /// Read the discriminant of an ADT.
1584 /// Undefined (i.e. no effort is made to make it defined, but there’s no reason why it cannot
1585 /// be defined to return, say, a 0) if ADT is not an enum.
1586 Discriminant(Place<'tcx>),
1588 /// Create an aggregate value, like a tuple or struct. This is
1589 /// only needed because we want to distinguish `dest = Foo { x:
1590 /// ..., y: ... }` from `dest.x = ...; dest.y = ...;` in the case
1591 /// that `Foo` has a destructor. These rvalues can be optimized
1592 /// away after type-checking and before lowering.
1593 Aggregate(Box<AggregateKind<'tcx>>, Vec<Operand<'tcx>>),
1596 #[derive(Clone, Copy, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
1600 /// Convert unique, zero-sized type for a fn to fn()
1603 /// Convert non capturing closure to fn()
1606 /// Convert safe fn() to unsafe fn()
1609 /// "Unsize" -- convert a thin-or-fat pointer to a fat pointer.
1610 /// trans must figure out the details once full monomorphization
1611 /// is known. For example, this could be used to cast from a
1612 /// `&[i32;N]` to a `&[i32]`, or a `Box<T>` to a `Box<Trait>`
1613 /// (presuming `T: Trait`).
1617 #[derive(Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
1618 pub enum AggregateKind<'tcx> {
1619 /// The type is of the element
1623 /// The second field is the variant index. It's equal to 0 for struct
1624 /// and union expressions. The fourth field is
1625 /// active field number and is present only for union expressions
1626 /// -- e.g. for a union expression `SomeUnion { c: .. }`, the
1627 /// active field index would identity the field `c`
1628 Adt(&'tcx AdtDef, usize, &'tcx Substs<'tcx>, Option<usize>),
1630 Closure(DefId, ClosureSubsts<'tcx>),
1631 Generator(DefId, ClosureSubsts<'tcx>, GeneratorInterior<'tcx>),
1634 #[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
1636 /// The `+` operator (addition)
1638 /// The `-` operator (subtraction)
1640 /// The `*` operator (multiplication)
1642 /// The `/` operator (division)
1644 /// The `%` operator (modulus)
1646 /// The `^` operator (bitwise xor)
1648 /// The `&` operator (bitwise and)
1650 /// The `|` operator (bitwise or)
1652 /// The `<<` operator (shift left)
1654 /// The `>>` operator (shift right)
1656 /// The `==` operator (equality)
1658 /// The `<` operator (less than)
1660 /// The `<=` operator (less than or equal to)
1662 /// The `!=` operator (not equal to)
1664 /// The `>=` operator (greater than or equal to)
1666 /// The `>` operator (greater than)
1668 /// The `ptr.offset` operator
1673 pub fn is_checkable(self) -> bool {
1676 Add | Sub | Mul | Shl | Shr => true,
1682 #[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
1684 /// Return the size of a value of that type
1686 /// Create a new uninitialized box for a value of that type
1690 #[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
1692 /// The `!` operator for logical inversion
1694 /// The `-` operator for negation
1698 impl<'tcx> Debug for Rvalue<'tcx> {
1699 fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
1700 use self::Rvalue::*;
1703 Use(ref place) => write!(fmt, "{:?}", place),
1704 Repeat(ref a, ref b) => write!(fmt, "[{:?}; {:?}]", a, b),
1705 Len(ref a) => write!(fmt, "Len({:?})", a),
1706 Cast(ref kind, ref place, ref ty) => {
1707 write!(fmt, "{:?} as {:?} ({:?})", place, ty, kind)
1709 BinaryOp(ref op, ref a, ref b) => write!(fmt, "{:?}({:?}, {:?})", op, a, b),
1710 CheckedBinaryOp(ref op, ref a, ref b) => {
1711 write!(fmt, "Checked{:?}({:?}, {:?})", op, a, b)
1713 UnaryOp(ref op, ref a) => write!(fmt, "{:?}({:?})", op, a),
1714 Discriminant(ref place) => write!(fmt, "discriminant({:?})", place),
1715 NullaryOp(ref op, ref t) => write!(fmt, "{:?}({:?})", op, t),
1716 Ref(region, borrow_kind, ref place) => {
1717 let kind_str = match borrow_kind {
1718 BorrowKind::Shared => "",
1719 BorrowKind::Mut { .. } | BorrowKind::Unique => "mut ",
1722 // When printing regions, add trailing space if necessary.
1723 let region = if ppaux::verbose() || ppaux::identify_regions() {
1724 let mut region = format!("{}", region);
1725 if region.len() > 0 { region.push(' '); }
1728 // Do not even print 'static
1731 write!(fmt, "&{}{}{:?}", region, kind_str, place)
1734 Aggregate(ref kind, ref places) => {
1735 fn fmt_tuple(fmt: &mut Formatter, places: &[Operand]) -> fmt::Result {
1736 let mut tuple_fmt = fmt.debug_tuple("");
1737 for place in places {
1738 tuple_fmt.field(place);
1744 AggregateKind::Array(_) => write!(fmt, "{:?}", places),
1746 AggregateKind::Tuple => {
1747 match places.len() {
1748 0 => write!(fmt, "()"),
1749 1 => write!(fmt, "({:?},)", places[0]),
1750 _ => fmt_tuple(fmt, places),
1754 AggregateKind::Adt(adt_def, variant, substs, _) => {
1755 let variant_def = &adt_def.variants[variant];
1757 ppaux::parameterized(fmt, substs, variant_def.did, &[])?;
1759 match variant_def.ctor_kind {
1760 CtorKind::Const => Ok(()),
1761 CtorKind::Fn => fmt_tuple(fmt, places),
1762 CtorKind::Fictive => {
1763 let mut struct_fmt = fmt.debug_struct("");
1764 for (field, place) in variant_def.fields.iter().zip(places) {
1765 struct_fmt.field(&field.name.as_str(), place);
1772 AggregateKind::Closure(def_id, _) => ty::tls::with(|tcx| {
1773 if let Some(node_id) = tcx.hir.as_local_node_id(def_id) {
1774 let name = if tcx.sess.opts.debugging_opts.span_free_formats {
1775 format!("[closure@{:?}]", node_id)
1777 format!("[closure@{:?}]", tcx.hir.span(node_id))
1779 let mut struct_fmt = fmt.debug_struct(&name);
1781 tcx.with_freevars(node_id, |freevars| {
1782 for (freevar, place) in freevars.iter().zip(places) {
1783 let var_name = tcx.hir.name(freevar.var_id());
1784 struct_fmt.field(&var_name.as_str(), place);
1790 write!(fmt, "[closure]")
1794 AggregateKind::Generator(def_id, _, _) => ty::tls::with(|tcx| {
1795 if let Some(node_id) = tcx.hir.as_local_node_id(def_id) {
1796 let name = format!("[generator@{:?}]", tcx.hir.span(node_id));
1797 let mut struct_fmt = fmt.debug_struct(&name);
1799 tcx.with_freevars(node_id, |freevars| {
1800 for (freevar, place) in freevars.iter().zip(places) {
1801 let var_name = tcx.hir.name(freevar.var_id());
1802 struct_fmt.field(&var_name.as_str(), place);
1804 struct_fmt.field("$state", &places[freevars.len()]);
1805 for i in (freevars.len() + 1)..places.len() {
1806 struct_fmt.field(&format!("${}", i - freevars.len() - 1),
1813 write!(fmt, "[generator]")
1822 ///////////////////////////////////////////////////////////////////////////
1825 /// Two constants are equal if they are the same constant. Note that
1826 /// this does not necessarily mean that they are "==" in Rust -- in
1827 /// particular one must be wary of `NaN`!
1829 #[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
1830 pub struct Constant<'tcx> {
1833 pub literal: Literal<'tcx>,
1836 newtype_index!(Promoted { DEBUG_FORMAT = "promoted[{}]" });
1839 #[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
1840 pub enum Literal<'tcx> {
1842 value: &'tcx ty::Const<'tcx>,
1845 // Index into the `promoted` vector of `Mir`.
1850 impl<'tcx> Debug for Constant<'tcx> {
1851 fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
1852 write!(fmt, "{:?}", self.literal)
1856 impl<'tcx> Debug for Literal<'tcx> {
1857 fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
1858 use self::Literal::*;
1860 Value { value } => {
1861 write!(fmt, "const ")?;
1862 fmt_const_val(fmt, value)
1864 Promoted { index } => {
1865 write!(fmt, "{:?}", index)
1871 /// Write a `ConstVal` in a way closer to the original source code than the `Debug` output.
1872 fn fmt_const_val<W: Write>(fmt: &mut W, const_val: &ty::Const) -> fmt::Result {
1873 use middle::const_val::ConstVal::*;
1874 match const_val.val {
1875 Unevaluated(..) => write!(fmt, "{:?}", const_val),
1876 Value(val) => print_miri_value(val, const_val.ty, fmt),
1880 fn print_miri_value<W: Write>(value: Value, ty: Ty, f: &mut W) -> fmt::Result {
1881 use ty::TypeVariants::*;
1882 use rustc_const_math::ConstFloat;
1883 match (value, &ty.sty) {
1884 (Value::ByVal(PrimVal::Bytes(0)), &TyBool) => write!(f, "false"),
1885 (Value::ByVal(PrimVal::Bytes(1)), &TyBool) => write!(f, "true"),
1886 (Value::ByVal(PrimVal::Bytes(bits)), &TyFloat(fty)) =>
1887 write!(f, "{}", ConstFloat { bits, ty: fty }),
1888 (Value::ByVal(PrimVal::Bytes(n)), &TyUint(ui)) => write!(f, "{:?}{}", n, ui),
1889 (Value::ByVal(PrimVal::Bytes(n)), &TyInt(i)) => write!(f, "{:?}{}", n as i128, i),
1890 (Value::ByVal(PrimVal::Bytes(n)), &TyChar) =>
1891 write!(f, "{:?}", ::std::char::from_u32(n as u32).unwrap()),
1892 (Value::ByVal(PrimVal::Undef), &TyFnDef(did, _)) =>
1893 write!(f, "{}", item_path_str(did)),
1894 (Value::ByValPair(PrimVal::Ptr(ptr), PrimVal::Bytes(len)), &TyRef(_, TypeAndMut {
1895 ty: &ty::TyS { sty: TyStr, .. }, ..
1897 ty::tls::with(|tcx| {
1901 .get_alloc(ptr.alloc_id)
1902 .expect("miri alloc not found");
1903 assert_eq!(len as usize as u128, len);
1904 let slice = &alloc.bytes[(ptr.offset as usize)..][..(len as usize)];
1905 let s = ::std::str::from_utf8(slice)
1906 .expect("non utf8 str from miri");
1907 write!(f, "{:?}", s)
1910 _ => write!(f, "{:?}:{}", value, ty),
1914 fn item_path_str(def_id: DefId) -> String {
1915 ty::tls::with(|tcx| tcx.item_path_str(def_id))
1918 impl<'tcx> ControlFlowGraph for Mir<'tcx> {
1920 type Node = BasicBlock;
1922 fn num_nodes(&self) -> usize { self.basic_blocks.len() }
1924 fn start_node(&self) -> Self::Node { START_BLOCK }
1926 fn predecessors<'graph>(&'graph self, node: Self::Node)
1927 -> <Self as GraphPredecessors<'graph>>::Iter
1929 self.predecessors_for(node).clone().into_iter()
1931 fn successors<'graph>(&'graph self, node: Self::Node)
1932 -> <Self as GraphSuccessors<'graph>>::Iter
1934 self.basic_blocks[node].terminator().successors().into_owned().into_iter()
1938 impl<'a, 'b> GraphPredecessors<'b> for Mir<'a> {
1939 type Item = BasicBlock;
1940 type Iter = IntoIter<BasicBlock>;
1943 impl<'a, 'b> GraphSuccessors<'b> for Mir<'a> {
1944 type Item = BasicBlock;
1945 type Iter = IntoIter<BasicBlock>;
1948 #[derive(Copy, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
1949 pub struct Location {
1950 /// the location is within this block
1951 pub block: BasicBlock,
1953 /// the location is the start of the statement; or, if `statement_index`
1954 /// == num-statements, then the start of the terminator.
1955 pub statement_index: usize,
1958 impl fmt::Debug for Location {
1959 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1960 write!(fmt, "{:?}[{}]", self.block, self.statement_index)
1965 /// Returns the location immediately after this one within the enclosing block.
1967 /// Note that if this location represents a terminator, then the
1968 /// resulting location would be out of bounds and invalid.
1969 pub fn successor_within_block(&self) -> Location {
1970 Location { block: self.block, statement_index: self.statement_index + 1 }
1973 pub fn dominates(&self, other: &Location, dominators: &Dominators<BasicBlock>) -> bool {
1974 if self.block == other.block {
1975 self.statement_index <= other.statement_index
1977 dominators.is_dominated_by(other.block, self.block)
1982 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
1983 pub enum UnsafetyViolationKind {
1985 ExternStatic(ast::NodeId),
1986 BorrowPacked(ast::NodeId),
1989 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
1990 pub struct UnsafetyViolation {
1991 pub source_info: SourceInfo,
1992 pub description: InternedString,
1993 pub kind: UnsafetyViolationKind,
1996 #[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
1997 pub struct UnsafetyCheckResult {
1998 /// Violations that are propagated *upwards* from this function
1999 pub violations: Lrc<[UnsafetyViolation]>,
2000 /// unsafe blocks in this function, along with whether they are used. This is
2001 /// used for the "unused_unsafe" lint.
2002 pub unsafe_blocks: Lrc<[(ast::NodeId, bool)]>,
2005 /// The layout of generator state
2006 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
2007 pub struct GeneratorLayout<'tcx> {
2008 pub fields: Vec<LocalDecl<'tcx>>,
2011 /// After we borrow check a closure, we are left with various
2012 /// requirements that we have inferred between the free regions that
2013 /// appear in the closure's signature or on its field types. These
2014 /// requirements are then verified and proved by the closure's
2015 /// creating function. This struct encodes those requirements.
2017 /// The requirements are listed as being between various
2018 /// `RegionVid`. The 0th region refers to `'static`; subsequent region
2019 /// vids refer to the free regions that appear in the closure (or
2020 /// generator's) type, in order of appearance. (This numbering is
2021 /// actually defined by the `UniversalRegions` struct in the NLL
2022 /// region checker. See for example
2023 /// `UniversalRegions::closure_mapping`.) Note that we treat the free
2024 /// regions in the closure's type "as if" they were erased, so their
2025 /// precise identity is not important, only their position.
2027 /// Example: If type check produces a closure with the closure substs:
2030 /// ClosureSubsts = [
2031 /// i8, // the "closure kind"
2032 /// for<'x> fn(&'a &'x u32) -> &'x u32, // the "closure signature"
2033 /// &'a String, // some upvar
2037 /// here, there is one unique free region (`'a`) but it appears
2038 /// twice. We would "renumber" each occurrence to a unique vid, as follows:
2041 /// ClosureSubsts = [
2042 /// i8, // the "closure kind"
2043 /// for<'x> fn(&'1 &'x u32) -> &'x u32, // the "closure signature"
2044 /// &'2 String, // some upvar
2048 /// Now the code might impose a requirement like `'1: '2`. When an
2049 /// instance of the closure is created, the corresponding free regions
2050 /// can be extracted from its type and constrained to have the given
2051 /// outlives relationship.
2053 /// In some cases, we have to record outlives requirements between
2054 /// types and regions as well. In that case, if those types include
2055 /// any regions, those regions are recorded as `ReClosureBound`
2056 /// instances assigned one of these same indices. Those regions will
2057 /// be substituted away by the creator. We use `ReClosureBound` in
2058 /// that case because the regions must be allocated in the global
2059 /// TyCtxt, and hence we cannot use `ReVar` (which is what we use
2060 /// internally within the rest of the NLL code).
2061 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
2062 pub struct ClosureRegionRequirements<'gcx> {
2063 /// The number of external regions defined on the closure. In our
2064 /// example above, it would be 3 -- one for `'static`, then `'1`
2065 /// and `'2`. This is just used for a sanity check later on, to
2066 /// make sure that the number of regions we see at the callsite
2068 pub num_external_vids: usize,
2070 /// Requirements between the various free regions defined in
2072 pub outlives_requirements: Vec<ClosureOutlivesRequirement<'gcx>>,
2075 /// Indicates an outlives constraint between a type or between two
2076 /// free-regions declared on the closure.
2077 #[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
2078 pub struct ClosureOutlivesRequirement<'tcx> {
2079 // This region or type ...
2080 pub subject: ClosureOutlivesSubject<'tcx>,
2082 // .. must outlive this one.
2083 pub outlived_free_region: ty::RegionVid,
2085 // If not, report an error here.
2086 pub blame_span: Span,
2089 /// The subject of a ClosureOutlivesRequirement -- that is, the thing
2090 /// that must outlive some region.
2091 #[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
2092 pub enum ClosureOutlivesSubject<'tcx> {
2093 /// Subject is a type, typically a type parameter, but could also
2094 /// be a projection. Indicates a requirement like `T: 'a` being
2095 /// passed to the caller, where the type here is `T`.
2097 /// The type here is guaranteed not to contain any free regions at
2101 /// Subject is a free region from the closure. Indicates a requirement
2102 /// like `'a: 'b` being passed to the caller; the region here is `'a`.
2103 Region(ty::RegionVid),
2107 * TypeFoldable implementations for MIR types
2110 impl<'tcx> TypeFoldable<'tcx> for Mir<'tcx> {
2111 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
2113 basic_blocks: self.basic_blocks.fold_with(folder),
2114 visibility_scopes: self.visibility_scopes.clone(),
2115 visibility_scope_info: self.visibility_scope_info.clone(),
2116 promoted: self.promoted.fold_with(folder),
2117 yield_ty: self.yield_ty.fold_with(folder),
2118 generator_drop: self.generator_drop.fold_with(folder),
2119 generator_layout: self.generator_layout.fold_with(folder),
2120 local_decls: self.local_decls.fold_with(folder),
2121 arg_count: self.arg_count,
2122 upvar_decls: self.upvar_decls.clone(),
2123 spread_arg: self.spread_arg,
2125 cache: cache::Cache::new()
2129 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
2130 self.basic_blocks.visit_with(visitor) ||
2131 self.generator_drop.visit_with(visitor) ||
2132 self.generator_layout.visit_with(visitor) ||
2133 self.yield_ty.visit_with(visitor) ||
2134 self.promoted.visit_with(visitor) ||
2135 self.local_decls.visit_with(visitor)
2139 impl<'tcx> TypeFoldable<'tcx> for GeneratorLayout<'tcx> {
2140 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
2142 fields: self.fields.fold_with(folder),
2146 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
2147 self.fields.visit_with(visitor)
2151 impl<'tcx> TypeFoldable<'tcx> for LocalDecl<'tcx> {
2152 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
2154 ty: self.ty.fold_with(folder),
2159 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
2160 self.ty.visit_with(visitor)
2164 impl<'tcx> TypeFoldable<'tcx> for BasicBlockData<'tcx> {
2165 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
2167 statements: self.statements.fold_with(folder),
2168 terminator: self.terminator.fold_with(folder),
2169 is_cleanup: self.is_cleanup
2173 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
2174 self.statements.visit_with(visitor) || self.terminator.visit_with(visitor)
2178 impl<'tcx> TypeFoldable<'tcx> for ValidationOperand<'tcx, Place<'tcx>> {
2179 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
2181 place: self.place.fold_with(folder),
2182 ty: self.ty.fold_with(folder),
2188 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
2189 self.place.visit_with(visitor) || self.ty.visit_with(visitor)
2193 impl<'tcx> TypeFoldable<'tcx> for Statement<'tcx> {
2194 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
2195 use mir::StatementKind::*;
2197 let kind = match self.kind {
2198 Assign(ref place, ref rval) => Assign(place.fold_with(folder), rval.fold_with(folder)),
2199 SetDiscriminant { ref place, variant_index } => SetDiscriminant {
2200 place: place.fold_with(folder),
2203 StorageLive(ref local) => StorageLive(local.fold_with(folder)),
2204 StorageDead(ref local) => StorageDead(local.fold_with(folder)),
2205 InlineAsm { ref asm, ref outputs, ref inputs } => InlineAsm {
2207 outputs: outputs.fold_with(folder),
2208 inputs: inputs.fold_with(folder)
2211 // Note for future: If we want to expose the region scopes
2212 // during the fold, we need to either generalize EndRegion
2213 // to carry `[ty::Region]`, or extend the `TypeFolder`
2214 // trait with a `fn fold_scope`.
2215 EndRegion(ref region_scope) => EndRegion(region_scope.clone()),
2217 Validate(ref op, ref places) =>
2218 Validate(op.clone(),
2219 places.iter().map(|operand| operand.fold_with(folder)).collect()),
2224 source_info: self.source_info,
2229 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
2230 use mir::StatementKind::*;
2233 Assign(ref place, ref rval) => { place.visit_with(visitor) || rval.visit_with(visitor) }
2234 SetDiscriminant { ref place, .. } => place.visit_with(visitor),
2235 StorageLive(ref local) |
2236 StorageDead(ref local) => local.visit_with(visitor),
2237 InlineAsm { ref outputs, ref inputs, .. } =>
2238 outputs.visit_with(visitor) || inputs.visit_with(visitor),
2240 // Note for future: If we want to expose the region scopes
2241 // during the visit, we need to either generalize EndRegion
2242 // to carry `[ty::Region]`, or extend the `TypeVisitor`
2243 // trait with a `fn visit_scope`.
2244 EndRegion(ref _scope) => false,
2246 Validate(ref _op, ref places) =>
2247 places.iter().any(|ty_and_place| ty_and_place.visit_with(visitor)),
2254 impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
2255 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
2256 use mir::TerminatorKind::*;
2258 let kind = match self.kind {
2259 Goto { target } => Goto { target: target },
2260 SwitchInt { ref discr, switch_ty, ref values, ref targets } => SwitchInt {
2261 discr: discr.fold_with(folder),
2262 switch_ty: switch_ty.fold_with(folder),
2263 values: values.clone(),
2264 targets: targets.clone()
2266 Drop { ref location, target, unwind } => Drop {
2267 location: location.fold_with(folder),
2271 DropAndReplace { ref location, ref value, target, unwind } => DropAndReplace {
2272 location: location.fold_with(folder),
2273 value: value.fold_with(folder),
2277 Yield { ref value, resume, drop } => Yield {
2278 value: value.fold_with(folder),
2282 Call { ref func, ref args, ref destination, cleanup } => {
2283 let dest = destination.as_ref().map(|&(ref loc, dest)| {
2284 (loc.fold_with(folder), dest)
2288 func: func.fold_with(folder),
2289 args: args.fold_with(folder),
2294 Assert { ref cond, expected, ref msg, target, cleanup } => {
2295 let msg = if let AssertMessage::BoundsCheck { ref len, ref index } = *msg {
2296 AssertMessage::BoundsCheck {
2297 len: len.fold_with(folder),
2298 index: index.fold_with(folder),
2304 cond: cond.fold_with(folder),
2311 GeneratorDrop => GeneratorDrop,
2315 Unreachable => Unreachable,
2316 FalseEdges { real_target, ref imaginary_targets } =>
2317 FalseEdges { real_target, imaginary_targets: imaginary_targets.clone() },
2318 FalseUnwind { real_target, unwind } => FalseUnwind { real_target, unwind },
2321 source_info: self.source_info,
2326 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
2327 use mir::TerminatorKind::*;
2330 SwitchInt { ref discr, switch_ty, .. } =>
2331 discr.visit_with(visitor) || switch_ty.visit_with(visitor),
2332 Drop { ref location, ..} => location.visit_with(visitor),
2333 DropAndReplace { ref location, ref value, ..} =>
2334 location.visit_with(visitor) || value.visit_with(visitor),
2335 Yield { ref value, ..} =>
2336 value.visit_with(visitor),
2337 Call { ref func, ref args, ref destination, .. } => {
2338 let dest = if let Some((ref loc, _)) = *destination {
2339 loc.visit_with(visitor)
2341 dest || func.visit_with(visitor) || args.visit_with(visitor)
2343 Assert { ref cond, ref msg, .. } => {
2344 if cond.visit_with(visitor) {
2345 if let AssertMessage::BoundsCheck { ref len, ref index } = *msg {
2346 len.visit_with(visitor) || index.visit_with(visitor)
2361 FalseUnwind { .. } => false
2366 impl<'tcx> TypeFoldable<'tcx> for Place<'tcx> {
2367 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
2369 &Place::Projection(ref p) => Place::Projection(p.fold_with(folder)),
2374 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
2375 if let &Place::Projection(ref p) = self {
2376 p.visit_with(visitor)
2383 impl<'tcx> TypeFoldable<'tcx> for Rvalue<'tcx> {
2384 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
2387 Use(ref op) => Use(op.fold_with(folder)),
2388 Repeat(ref op, len) => Repeat(op.fold_with(folder), len),
2389 Ref(region, bk, ref place) =>
2390 Ref(region.fold_with(folder), bk, place.fold_with(folder)),
2391 Len(ref place) => Len(place.fold_with(folder)),
2392 Cast(kind, ref op, ty) => Cast(kind, op.fold_with(folder), ty.fold_with(folder)),
2393 BinaryOp(op, ref rhs, ref lhs) =>
2394 BinaryOp(op, rhs.fold_with(folder), lhs.fold_with(folder)),
2395 CheckedBinaryOp(op, ref rhs, ref lhs) =>
2396 CheckedBinaryOp(op, rhs.fold_with(folder), lhs.fold_with(folder)),
2397 UnaryOp(op, ref val) => UnaryOp(op, val.fold_with(folder)),
2398 Discriminant(ref place) => Discriminant(place.fold_with(folder)),
2399 NullaryOp(op, ty) => NullaryOp(op, ty.fold_with(folder)),
2400 Aggregate(ref kind, ref fields) => {
2401 let kind = box match **kind {
2402 AggregateKind::Array(ty) => AggregateKind::Array(ty.fold_with(folder)),
2403 AggregateKind::Tuple => AggregateKind::Tuple,
2404 AggregateKind::Adt(def, v, substs, n) =>
2405 AggregateKind::Adt(def, v, substs.fold_with(folder), n),
2406 AggregateKind::Closure(id, substs) =>
2407 AggregateKind::Closure(id, substs.fold_with(folder)),
2408 AggregateKind::Generator(id, substs, interior) =>
2409 AggregateKind::Generator(id,
2410 substs.fold_with(folder),
2411 interior.fold_with(folder)),
2413 Aggregate(kind, fields.fold_with(folder))
2418 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
2421 Use(ref op) => op.visit_with(visitor),
2422 Repeat(ref op, _) => op.visit_with(visitor),
2423 Ref(region, _, ref place) => region.visit_with(visitor) || place.visit_with(visitor),
2424 Len(ref place) => place.visit_with(visitor),
2425 Cast(_, ref op, ty) => op.visit_with(visitor) || ty.visit_with(visitor),
2426 BinaryOp(_, ref rhs, ref lhs) |
2427 CheckedBinaryOp(_, ref rhs, ref lhs) =>
2428 rhs.visit_with(visitor) || lhs.visit_with(visitor),
2429 UnaryOp(_, ref val) => val.visit_with(visitor),
2430 Discriminant(ref place) => place.visit_with(visitor),
2431 NullaryOp(_, ty) => ty.visit_with(visitor),
2432 Aggregate(ref kind, ref fields) => {
2434 AggregateKind::Array(ty) => ty.visit_with(visitor),
2435 AggregateKind::Tuple => false,
2436 AggregateKind::Adt(_, _, substs, _) => substs.visit_with(visitor),
2437 AggregateKind::Closure(_, substs) => substs.visit_with(visitor),
2438 AggregateKind::Generator(_, substs, interior) => substs.visit_with(visitor) ||
2439 interior.visit_with(visitor),
2440 }) || fields.visit_with(visitor)
2446 impl<'tcx> TypeFoldable<'tcx> for Operand<'tcx> {
2447 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
2449 Operand::Copy(ref place) => Operand::Copy(place.fold_with(folder)),
2450 Operand::Move(ref place) => Operand::Move(place.fold_with(folder)),
2451 Operand::Constant(ref c) => Operand::Constant(c.fold_with(folder)),
2455 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
2457 Operand::Copy(ref place) |
2458 Operand::Move(ref place) => place.visit_with(visitor),
2459 Operand::Constant(ref c) => c.visit_with(visitor)
2464 impl<'tcx, B, V, T> TypeFoldable<'tcx> for Projection<'tcx, B, V, T>
2465 where B: TypeFoldable<'tcx>, V: TypeFoldable<'tcx>, T: TypeFoldable<'tcx>
2467 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
2468 use mir::ProjectionElem::*;
2470 let base = self.base.fold_with(folder);
2471 let elem = match self.elem {
2473 Field(f, ref ty) => Field(f, ty.fold_with(folder)),
2474 Index(ref v) => Index(v.fold_with(folder)),
2475 ref elem => elem.clone()
2484 fn super_visit_with<Vs: TypeVisitor<'tcx>>(&self, visitor: &mut Vs) -> bool {
2485 use mir::ProjectionElem::*;
2487 self.base.visit_with(visitor) ||
2489 Field(_, ref ty) => ty.visit_with(visitor),
2490 Index(ref v) => v.visit_with(visitor),
2496 impl<'tcx> TypeFoldable<'tcx> for Field {
2497 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, _: &mut F) -> Self {
2500 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> bool {
2505 impl<'tcx> TypeFoldable<'tcx> for Constant<'tcx> {
2506 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
2508 span: self.span.clone(),
2509 ty: self.ty.fold_with(folder),
2510 literal: self.literal.fold_with(folder)
2513 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
2514 self.ty.visit_with(visitor) || self.literal.visit_with(visitor)
2518 impl<'tcx> TypeFoldable<'tcx> for Literal<'tcx> {
2519 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
2521 Literal::Value { value } => Literal::Value {
2522 value: value.fold_with(folder)
2524 Literal::Promoted { index } => Literal::Promoted { index }
2527 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
2529 Literal::Value { value } => value.visit_with(visitor),
2530 Literal::Promoted { .. } => false