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 [rustc guide] for more info.
13 //! [rustc guide]: https://rust-lang-nursery.github.io/rustc-guide/mir.html
15 use graphviz::IntoCow;
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};
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;
46 pub use mir::interpret::AssertMessage;
56 type LocalDecls<'tcx> = IndexVec<Local, LocalDecl<'tcx>>;
58 pub trait HasLocalDecls<'tcx> {
59 fn local_decls(&self) -> &LocalDecls<'tcx>;
62 impl<'tcx> HasLocalDecls<'tcx> for LocalDecls<'tcx> {
63 fn local_decls(&self) -> &LocalDecls<'tcx> {
68 impl<'tcx> HasLocalDecls<'tcx> for Mir<'tcx> {
69 fn local_decls(&self) -> &LocalDecls<'tcx> {
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>>,
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>,
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>>,
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>>,
94 /// Yield type of the function, if it is a generator.
95 pub yield_ty: Option<Ty<'tcx>>,
97 /// Generator drop glue
98 pub generator_drop: Option<Box<Mir<'tcx>>>,
100 /// The layout of a generator. Produced by the state transformation.
101 pub generator_layout: Option<GeneratorLayout<'tcx>>,
103 /// Declarations of locals.
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>,
110 /// Number of arguments this function takes.
112 /// Starting at local 1, `arg_count` locals will be provided by the caller
113 /// and can be assumed to be initialized.
115 /// If this MIR was built for a constant, this will be 0.
116 pub arg_count: usize,
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>,
122 /// Mark an argument local (which must be a tuple) as getting passed as
123 /// its individual components at the LLVM level.
125 /// This is used for the "rust-call" ABI.
126 pub spread_arg: Option<Local>,
128 /// A span representing this MIR, for error reporting
131 /// A cache for various calculations
135 /// where execution begins
136 pub const START_BLOCK: BasicBlock = BasicBlock(0);
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>>,
147 upvar_decls: Vec<UpvarDecl>,
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());
157 visibility_scope_info,
160 generator_drop: None,
161 generator_layout: None,
167 cache: cache::Cache::new()
172 pub fn basic_blocks(&self) -> &IndexVec<BasicBlock, BasicBlockData<'tcx>> {
177 pub fn basic_blocks_mut(&mut self) -> &mut IndexVec<BasicBlock, BasicBlockData<'tcx>> {
178 self.cache.invalidate();
179 &mut self.basic_blocks
183 pub fn basic_blocks_and_local_decls_mut(&mut self) -> (
184 &mut IndexVec<BasicBlock, BasicBlockData<'tcx>>,
185 &mut LocalDecls<'tcx>,
187 self.cache.invalidate();
188 (&mut self.basic_blocks, &mut self.local_decls)
192 pub fn predecessors(&self) -> ReadGuard<IndexVec<BasicBlock, Vec<BasicBlock>>> {
193 self.cache.predecessors(self)
197 pub fn predecessors_for(&self, bb: BasicBlock) -> ReadGuard<Vec<BasicBlock>> {
198 ReadGuard::map(self.predecessors(), |p| &p[bb])
202 pub fn dominators(&self) -> Dominators<BasicBlock> {
207 pub fn local_kind(&self, local: Local) -> LocalKind {
208 let index = local.0 as usize;
210 debug_assert!(self.local_decls[local].mutability == Mutability::Mut,
211 "return place should be mutable");
213 LocalKind::ReturnPointer
214 } else if index < self.arg_count + 1 {
216 } else if self.local_decls[local].name.is_some() {
219 debug_assert!(self.local_decls[local].mutability == Mutability::Mut,
220 "temp should be mutable");
226 /// Returns an iterator over all temporaries.
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 {
239 /// Returns an iterator over all user-declared locals.
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 {
252 /// Returns an iterator over all user-declared mutable arguments and locals.
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
268 /// Returns an iterator over all function arguments.
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)
275 /// Returns an iterator over all user-defined variables and compiler-generated temporaries (all
276 /// locals that are neither arguments nor the return place).
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)
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()
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
300 assert!(idx == stmts.len());
301 &block.terminator().source_info
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
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.
319 #[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
322 /// Unsafe because of a PushUnsafeBlock
324 /// Unsafe because of an unsafe fn
326 /// Unsafe because of an `unsafe` block
327 ExplicitUnsafe(ast::NodeId)
330 impl_stable_hash_for!(struct Mir<'tcx> {
333 visibility_scope_info,
346 impl<'tcx> Index<BasicBlock> for Mir<'tcx> {
347 type Output = BasicBlockData<'tcx>;
350 fn index(&self, index: BasicBlock) -> &BasicBlockData<'tcx> {
351 &self.basic_blocks()[index]
355 impl<'tcx> IndexMut<BasicBlock> for Mir<'tcx> {
357 fn index_mut(&mut self, index: BasicBlock) -> &mut BasicBlockData<'tcx> {
358 &mut self.basic_blocks_mut()[index]
362 #[derive(Clone, Debug)]
363 pub enum ClearCrossCrate<T> {
368 impl<T: serialize::Encodable> serialize::UseSpecializedEncodable for ClearCrossCrate<T> {}
369 impl<T: serialize::Decodable> serialize::UseSpecializedDecodable for ClearCrossCrate<T> {}
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.
379 /// The lexical visibility scope, i.e. which bindings can be seen.
380 pub scope: VisibilityScope
383 ///////////////////////////////////////////////////////////////////////////
384 // Mutability and borrow kinds
386 #[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
387 pub enum Mutability {
392 #[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
393 pub enum BorrowKind {
394 /// Data must be immutable and is aliasable.
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.:
402 /// let x: &mut isize = ...;
403 /// let y = || *x += 5;
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:
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; }
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:
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; }
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.
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.
434 /// Data is mutable and not aliasable.
436 /// True if this borrow arose from method-call auto-ref
437 /// (i.e. `adjustment::Adjust::Borrow`)
438 allow_two_phase_borrow: bool
443 pub fn allows_two_phase_borrow(&self) -> bool {
445 BorrowKind::Shared | BorrowKind::Unique => false,
446 BorrowKind::Mut { allow_two_phase_borrow } => allow_two_phase_borrow,
451 ///////////////////////////////////////////////////////////////////////////
452 // Variables and temps
456 DEBUG_FORMAT = "_{}",
457 const RETURN_PLACE = 0,
460 /// Classifies locals into categories. See `Mir::local_kind`.
461 #[derive(PartialEq, Eq, Debug)]
463 /// User-declared variable binding
465 /// Compiler-introduced temporary
467 /// Function argument
469 /// Location of function's return value
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`.
481 /// Temporaries and the return place are always mutable.
482 pub mutability: Mutability,
484 /// True if this corresponds to a user-declared local variable.
485 pub is_user_variable: bool,
487 /// True if this is an internal local
489 /// These locals are not based on types in the source code and are only used
490 /// for a few desugarings at the moment.
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
498 /// Unsafety checking will also ignore dereferences of these locals,
499 /// so they can be used for raw pointers only used in a desugaring.
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
506 /// Type of this local.
509 /// Name of the local, used in debuginfo and pretty-printing.
511 /// Note that function arguments can also have this set to `Some(_)`
512 /// to generate better debuginfo.
513 pub name: Option<Name>,
515 /// Source info of the local.
516 pub source_info: SourceInfo,
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
523 /// This is needed because visibility scope of locals within a let-statement
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.
530 /// That's it, if we have a let-statement like the one in this
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();
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.
548 /// However, from a name lookup point of view, the scopes look more like
549 /// as if the let-statements were `match` expressions:
552 /// fn foo(x: &str) {
554 /// match x.parse().unwrap() {
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`.
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).
574 /// The end result looks like this:
578 /// │{ argument x: &str }
580 /// │ │{ #[allow(unused_mut] } // this is actually split into 2 scopes
581 /// │ │ // in practice because I'm lazy.
583 /// │ │← x.syntactic_scope
584 /// │ │← `x.parse().unwrap()`
586 /// │ │ │← y.syntactic_scope
588 /// │ │ │{ let y: u32 }
590 /// │ │ │← y.source_info.scope
593 /// │ │{ let x: u32 }
594 /// │ │← x.source_info.scope
595 /// │ │← `drop(x)` // this accesses `x: u32`
597 pub syntactic_scope: VisibilityScope,
600 impl<'tcx> LocalDecl<'tcx> {
601 /// Create a new `LocalDecl` for a temporary.
603 pub fn new_temp(ty: Ty<'tcx>, span: Span) -> Self {
605 mutability: Mutability::Mut,
608 source_info: SourceInfo {
610 scope: ARGUMENT_VISIBILITY_SCOPE
612 syntactic_scope: ARGUMENT_VISIBILITY_SCOPE,
614 is_user_variable: false
618 /// Create a new `LocalDecl` for a internal temporary.
620 pub fn new_internal(ty: Ty<'tcx>, span: Span) -> Self {
622 mutability: Mutability::Mut,
625 source_info: SourceInfo {
627 scope: ARGUMENT_VISIBILITY_SCOPE
629 syntactic_scope: ARGUMENT_VISIBILITY_SCOPE,
631 is_user_variable: false
635 /// Builds a `LocalDecl` for the return place.
637 /// This must be inserted into the `local_decls` list as the first local.
639 pub fn new_return_place(return_ty: Ty, span: Span) -> LocalDecl {
641 mutability: Mutability::Mut,
643 source_info: SourceInfo {
645 scope: ARGUMENT_VISIBILITY_SCOPE
647 syntactic_scope: ARGUMENT_VISIBILITY_SCOPE,
649 name: None, // FIXME maybe we do want some name here?
650 is_user_variable: false
655 /// A closure capture, with its name and mode.
656 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
657 pub struct UpvarDecl {
658 pub debug_name: Name,
660 /// If true, the capture is behind a reference.
663 pub mutability: Mutability,
666 ///////////////////////////////////////////////////////////////////////////
669 newtype_index!(BasicBlock { DEBUG_FORMAT = "bb{}" });
672 pub fn start_location(self) -> Location {
680 ///////////////////////////////////////////////////////////////////////////
681 // BasicBlockData and Terminator
683 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
684 pub struct BasicBlockData<'tcx> {
685 /// List of statements in this block.
686 pub statements: Vec<Statement<'tcx>>,
688 /// Terminator for this block.
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>>,
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,
705 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
706 pub struct Terminator<'tcx> {
707 pub source_info: SourceInfo,
708 pub kind: TerminatorKind<'tcx>
711 #[derive(Clone, RustcEncodable, RustcDecodable)]
712 pub enum TerminatorKind<'tcx> {
713 /// block should have one successor in the graph; we jump there
718 /// operand evaluates to an integer; jump depending on its value
719 /// to one of the targets, and otherwise fallback to `otherwise`
721 /// discriminant value being tested
722 discr: Operand<'tcx>,
724 /// type of value being tested
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]>,
731 /// Possible branch sites. The last element of this vector is used
732 /// for the otherwise branch, so targets.len() == values.len() + 1
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:
737 // branches: Vec<(ConstInt, BasicBlock)>,
738 // otherwise: Option<BasicBlock> // exhaustive if None
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>,
745 /// Indicates that the landing pad is finished and unwinding should
746 /// continue. Emitted by build::scope::diverge_cleanup.
749 /// Indicates that the landing pad is finished and that the process
750 /// should abort. Used to prevent unwinding for foreign items.
753 /// Indicates a normal return. The return place should have
754 /// been filled in by now. This should occur at most once.
757 /// Indicates a terminator that can never be reached.
762 location: Place<'tcx>,
764 unwind: Option<BasicBlock>
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
774 /// DropAndReplace(P <- V, goto BB1, unwind BB2)
782 /// Drop(P, goto BB1, unwind BB2)
785 /// // P is now unitialized
789 /// // P is now unitialized -- its dtor panicked
794 location: Place<'tcx>,
795 value: Operand<'tcx>,
797 unwind: Option<BasicBlock>,
800 /// Block ends with a call of a converging function
802 /// The function that’s being called
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>
815 /// Jump to the target if the condition has the expected value,
816 /// otherwise panic with a message and a cleanup target.
820 msg: AssertMessage<'tcx>,
822 cleanup: Option<BasicBlock>
827 /// The value to return
828 value: Operand<'tcx>,
829 /// Where to resume to
831 /// Cleanup to be done if the generator is dropped at this suspend point
832 drop: Option<BasicBlock>,
835 /// Indicates the end of the dropping of a generator
838 /// A block where control flow only ever takes one real path, but borrowck
839 /// needs to be more conservative.
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
845 imaginary_targets: Vec<BasicBlock>,
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.
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>,
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>>;
868 impl<'tcx> Terminator<'tcx> {
869 pub fn successors(&self) -> Successors {
870 self.kind.successors()
873 pub fn successors_mut(&mut self) -> SuccessorsMut {
874 self.kind.successors_mut()
877 pub fn unwind_mut(&mut self) -> Option<&mut Option<BasicBlock>> {
878 self.kind.unwind_mut()
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 {
888 switch_ty: tcx.types.bool,
889 values: From::from(BOOL_SWITCH_FALSE),
894 pub fn successors(&self) -> Successors {
895 use self::TerminatorKind::*;
897 Resume | Abort | GeneratorDrop | Return | Unreachable |
898 Call { destination: None, cleanup: None, .. } => {
899 None.into_iter().chain(&[])
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(&[])
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))
919 SwitchInt { ref targets, .. } => {
920 None.into_iter().chain(&targets[..])
922 FalseEdges { ref real_target, ref imaginary_targets } => {
923 Some(real_target).into_iter().chain(&imaginary_targets[..])
928 pub fn successors_mut(&mut self) -> SuccessorsMut {
929 use self::TerminatorKind::*;
931 Resume | Abort | GeneratorDrop | Return | Unreachable |
932 Call { destination: None, cleanup: None, .. } => {
933 None.into_iter().chain(&mut [])
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 [])
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))
953 SwitchInt { ref mut targets, .. } => {
954 None.into_iter().chain(&mut targets[..])
956 FalseEdges { ref mut real_target, ref mut imaginary_targets } => {
957 Some(real_target).into_iter().chain(&mut imaginary_targets[..])
962 pub fn unwind_mut(&mut self) -> Option<&mut Option<BasicBlock>> {
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 { .. } => {
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, .. } => {
986 impl<'tcx> BasicBlockData<'tcx> {
987 pub fn new(terminator: Option<Terminator<'tcx>>) -> BasicBlockData<'tcx> {
995 /// Accessor for terminator.
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")
1003 pub fn terminator_mut(&mut self) -> &mut Terminator<'tcx> {
1004 self.terminator.as_mut().expect("invalid terminator state")
1007 pub fn retain_statements<F>(&mut self, mut f: F) where F: FnMut(&mut Statement) -> bool {
1008 for s in &mut self.statements {
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>>
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.
1028 // Save the other statements for optimized splicing.
1029 let remaining = new_stmts.size_hint().0;
1031 splices.push((i + 1 + extra_stmts, new_stmts));
1032 extra_stmts += remaining;
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 {
1050 scope: ARGUMENT_VISIBILITY_SCOPE
1052 kind: StatementKind::Nop
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 {
1059 self.statements.swap(gap.start, gap.end);
1061 self.statements.splice(splice_start..splice_end, new_stmts);
1062 gap.end = splice_start;
1066 pub fn visitable(&self, index: usize) -> &dyn MirVisitable<'tcx> {
1067 if index < self.statements.len() {
1068 &self.statements[index]
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());
1082 match successor_count {
1085 1 => write!(fmt, " -> {:?}", self.successors().nth(0).unwrap()),
1088 write!(fmt, " -> [")?;
1089 for (i, target) in self.successors().enumerate() {
1093 write!(fmt, "{}: {:?}", labels[i], target)?;
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::*;
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)?;
1124 write!(fmt, "{:?}(", func)?;
1125 for (index, arg) in args.iter().enumerate() {
1129 write!(fmt, "{:?}", arg)?;
1133 Assert { ref cond, expected, ref msg, .. } => {
1134 write!(fmt, "assert(")?;
1138 write!(fmt, "{:?}, \"{:?}\")", cond, msg)
1140 FalseEdges { .. } => write!(fmt, "falseEdges"),
1141 FalseUnwind { .. } => write!(fmt, "falseUnwind"),
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::*;
1149 Return | Resume | Abort | Unreachable | GeneratorDrop => vec![],
1150 Goto { .. } => vec!["".into()],
1151 SwitchInt { ref values, switch_ty, .. } => {
1154 let mut s = String::new();
1156 Value::ByVal(PrimVal::Bytes(u)),
1162 .chain(iter::once(String::from("otherwise").into()))
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()]
1179 Assert { cleanup: None, .. } => vec!["".into()],
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());
1187 FalseUnwind { unwind: Some(_), .. } => vec!["real".into(), "cleanup".into()],
1188 FalseUnwind { unwind: None, .. } => vec!["real".into()],
1193 ///////////////////////////////////////////////////////////////////////////
1196 #[derive(Clone, RustcEncodable, RustcDecodable)]
1197 pub struct Statement<'tcx> {
1198 pub source_info: SourceInfo,
1199 pub kind: StatementKind<'tcx>,
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
1209 /// Changes a statement to a nop and returns the original statement.
1210 pub fn replace_nop(&mut self) -> Self {
1212 source_info: self.source_info,
1213 kind: mem::replace(&mut self.kind, StatementKind::Nop)
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>),
1223 /// Write the discriminant for a variant to the enum Place.
1224 SetDiscriminant { place: Place<'tcx>, variant_index: usize },
1226 /// Start a live range for the storage of the local.
1229 /// End the current live range for the storage of the local.
1232 /// Execute a piece of inline Assembly.
1234 asm: Box<InlineAsm>,
1235 outputs: Vec<Place<'tcx>>,
1236 inputs: Vec<Operand<'tcx>>
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>>>),
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),
1248 /// Encodes a user's type assertion. These need to be preserved intact so that NLL can respect
1249 /// them. For example:
1251 /// let (a, b): (T, U) = y;
1253 /// Here we would insert a `UserAssertTy<(T, U)>(y)` instruction to check that the type of `y`
1254 /// is the right thing.
1256 /// `CanonicalTy` is used to capture "inference variables" from the user's types. For example:
1258 /// let x: Vec<_> = ...;
1259 /// let y: &u32 = ...;
1261 /// would result in `Vec<?0>` and `&'?0 u32` respectively (where `?0` is a canonicalized
1263 UserAssertTy(CanonicalTy<'tcx>, Local),
1265 /// No-op. Useful for deleting instructions without affecting statement indices.
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.
1277 /// Recursive traverse the *mutable* part of the type and relinquish all exclusive
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),
1285 impl Debug for ValidationOp {
1286 fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
1287 use self::ValidationOp::*;
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)),
1297 // This is generic so that it can be reused by miri
1298 #[derive(Clone, RustcEncodable, RustcDecodable)]
1299 pub struct ValidationOperand<'tcx, T> {
1302 pub re: Option<region::Scope>,
1303 pub mutbl: hir::Mutability,
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))?;
1313 if let hir::MutImmutable = self.mutbl {
1314 write!(fmt, " (imm)")?;
1320 impl<'tcx> Debug for Statement<'tcx> {
1321 fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
1322 use self::StatementKind::*;
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)
1333 InlineAsm { ref asm, ref outputs, ref inputs } => {
1334 write!(fmt, "asm!({:?} : {:?} : {:?})", asm, outputs, inputs)
1336 UserAssertTy(ref c_ty, ref local) => write!(fmt, "UserAssertTy({:?}, {:?})",
1338 Nop => write!(fmt, "nop"),
1343 ///////////////////////////////////////////////////////////////////////////
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> {
1353 /// static or static mut variable
1354 Static(Box<Static<'tcx>>),
1356 /// projection out of a place (access a field, deref a pointer, etc)
1357 Projection(Box<PlaceProjection<'tcx>>),
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> {
1368 impl_stable_hash_for!(struct Static<'tcx> {
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> {
1380 pub elem: ProjectionElem<'tcx, V, T>,
1383 #[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
1384 pub enum ProjectionElem<'tcx, V, T> {
1389 /// These indices are generated by slice patterns. Easiest to explain
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 },
1399 /// index or -index (in Python terms), depending on from_end
1401 /// thing being indexed must be at least this long
1403 /// counting backwards from end?
1407 /// These indices are generated by slice patterns.
1409 /// slice[from:-to] in Python terms.
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),
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>>;
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>>;
1429 newtype_index!(Field { DEBUG_FORMAT = "field[{}]" });
1431 impl<'tcx> Place<'tcx> {
1432 pub fn field(self, f: Field, ty: Ty<'tcx>) -> Place<'tcx> {
1433 self.elem(ProjectionElem::Field(f, ty))
1436 pub fn deref(self) -> Place<'tcx> {
1437 self.elem(ProjectionElem::Deref)
1440 pub fn downcast(self, adt_def: &'tcx AdtDef, variant_index: usize) -> Place<'tcx> {
1441 self.elem(ProjectionElem::Downcast(adt_def, variant_index))
1444 pub fn index(self, index: Local) -> Place<'tcx> {
1445 self.elem(ProjectionElem::Index(index))
1448 pub fn elem(self, elem: PlaceElem<'tcx>) -> Place<'tcx> {
1449 Place::Projection(Box::new(PlaceProjection {
1456 impl<'tcx> Debug for Place<'tcx> {
1457 fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
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) =>
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,
1491 ///////////////////////////////////////////////////////////////////////////
1494 newtype_index!(VisibilityScope
1496 DEBUG_FORMAT = "scope[{}]",
1497 const ARGUMENT_VISIBILITY_SCOPE = 0,
1500 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
1501 pub struct VisibilityScopeData {
1503 pub parent_scope: Option<VisibilityScope>,
1506 ///////////////////////////////////////////////////////////////////////////
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.
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.
1519 /// Move: The value (including old borrows of it) will not be used again.
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.
1525 Constant(Box<Constant<'tcx>>),
1528 impl<'tcx> Debug for Operand<'tcx> {
1529 fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
1530 use self::Operand::*;
1532 Constant(ref a) => write!(fmt, "{:?}", a),
1533 Copy(ref place) => write!(fmt, "{:?}", place),
1534 Move(ref place) => write!(fmt, "move {:?}", place),
1539 impl<'tcx> Operand<'tcx> {
1540 pub fn function_handle<'a>(
1541 tcx: TyCtxt<'a, 'tcx, 'tcx>,
1543 substs: &'tcx Substs<'tcx>,
1546 let ty = tcx.type_of(def_id).subst(tcx, substs);
1547 Operand::Constant(box Constant {
1550 literal: Literal::Value {
1551 value: ty::Const::zero_sized(tcx, ty),
1556 pub fn to_copy(&self) -> Self {
1558 Operand::Copy(_) | Operand::Constant(_) => self.clone(),
1559 Operand::Move(ref place) => Operand::Copy(place.clone())
1564 ///////////////////////////////////////////////////////////////////////////
1567 #[derive(Clone, RustcEncodable, RustcDecodable)]
1568 pub enum Rvalue<'tcx> {
1569 /// x (either a move or copy, depending on type of x)
1573 Repeat(Operand<'tcx>, u64),
1576 Ref(Region<'tcx>, BorrowKind, Place<'tcx>),
1578 /// length of a [X] or [X;n] value
1581 Cast(CastKind, Operand<'tcx>, Ty<'tcx>),
1583 BinaryOp(BinOp, Operand<'tcx>, Operand<'tcx>),
1584 CheckedBinaryOp(BinOp, Operand<'tcx>, Operand<'tcx>),
1586 NullaryOp(NullOp, Ty<'tcx>),
1587 UnaryOp(UnOp, Operand<'tcx>),
1589 /// Read the discriminant of an ADT.
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>),
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>>),
1603 #[derive(Clone, Copy, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
1607 /// Convert unique, zero-sized type for a fn to fn()
1610 /// Convert non capturing closure to fn()
1613 /// Convert safe fn() to unsafe fn()
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`).
1624 #[derive(Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
1625 pub enum AggregateKind<'tcx> {
1626 /// The type is of the element
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>),
1637 Closure(DefId, ClosureSubsts<'tcx>),
1638 Generator(DefId, GeneratorSubsts<'tcx>, hir::GeneratorMovability),
1641 #[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
1643 /// The `+` operator (addition)
1645 /// The `-` operator (subtraction)
1647 /// The `*` operator (multiplication)
1649 /// The `/` operator (division)
1651 /// The `%` operator (modulus)
1653 /// The `^` operator (bitwise xor)
1655 /// The `&` operator (bitwise and)
1657 /// The `|` operator (bitwise or)
1659 /// The `<<` operator (shift left)
1661 /// The `>>` operator (shift right)
1663 /// The `==` operator (equality)
1665 /// The `<` operator (less than)
1667 /// The `<=` operator (less than or equal to)
1669 /// The `!=` operator (not equal to)
1671 /// The `>=` operator (greater than or equal to)
1673 /// The `>` operator (greater than)
1675 /// The `ptr.offset` operator
1680 pub fn is_checkable(self) -> bool {
1683 Add | Sub | Mul | Shl | Shr => true,
1689 #[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
1691 /// Return the size of a value of that type
1693 /// Create a new uninitialized box for a value of that type
1697 #[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
1699 /// The `!` operator for logical inversion
1701 /// The `-` operator for negation
1705 impl<'tcx> Debug for Rvalue<'tcx> {
1706 fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
1707 use self::Rvalue::*;
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)
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)
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 ",
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(' '); }
1735 // Do not even print 'static
1738 write!(fmt, "&{}{}{:?}", region, kind_str, place)
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);
1751 AggregateKind::Array(_) => write!(fmt, "{:?}", places),
1753 AggregateKind::Tuple => {
1754 match places.len() {
1755 0 => write!(fmt, "()"),
1756 1 => write!(fmt, "({:?},)", places[0]),
1757 _ => fmt_tuple(fmt, places),
1761 AggregateKind::Adt(adt_def, variant, substs, _) => {
1762 let variant_def = &adt_def.variants[variant];
1764 ppaux::parameterized(fmt, substs, variant_def.did, &[])?;
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);
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)
1784 format!("[closure@{:?}]", tcx.hir.span(node_id))
1786 let mut struct_fmt = fmt.debug_struct(&name);
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);
1797 write!(fmt, "[closure]")
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);
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);
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),
1820 write!(fmt, "[generator]")
1829 ///////////////////////////////////////////////////////////////////////////
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`!
1836 #[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
1837 pub struct Constant<'tcx> {
1840 pub literal: Literal<'tcx>,
1843 newtype_index!(Promoted { DEBUG_FORMAT = "promoted[{}]" });
1846 #[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
1847 pub enum Literal<'tcx> {
1849 value: &'tcx ty::Const<'tcx>,
1852 // Index into the `promoted` vector of `Mir`.
1857 impl<'tcx> Debug for Constant<'tcx> {
1858 fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
1859 write!(fmt, "{:?}", self.literal)
1863 impl<'tcx> Debug for Literal<'tcx> {
1864 fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
1865 use self::Literal::*;
1867 Value { value } => {
1868 write!(fmt, "const ")?;
1869 fmt_const_val(fmt, value)
1871 Promoted { index } => {
1872 write!(fmt, "{:?}", index)
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)
1887 write!(fmt, "{:?}:{}", val, const_val.ty)
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| {
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)
1921 write!(f, "pointer to erroneous constant {:?}, {:?}", ptr, len)
1925 _ => write!(f, "{:?}:{}", value, ty),
1929 fn item_path_str(def_id: DefId) -> String {
1930 ty::tls::with(|tcx| tcx.item_path_str(def_id))
1933 impl<'tcx> ControlFlowGraph for Mir<'tcx> {
1935 type Node = BasicBlock;
1937 fn num_nodes(&self) -> usize { self.basic_blocks.len() }
1939 fn start_node(&self) -> Self::Node { START_BLOCK }
1941 fn predecessors<'graph>(&'graph self, node: Self::Node)
1942 -> <Self as GraphPredecessors<'graph>>::Iter
1944 self.predecessors_for(node).clone().into_iter()
1946 fn successors<'graph>(&'graph self, node: Self::Node)
1947 -> <Self as GraphSuccessors<'graph>>::Iter
1949 self.basic_blocks[node].terminator().successors().cloned()
1953 impl<'a, 'b> GraphPredecessors<'b> for Mir<'a> {
1954 type Item = BasicBlock;
1955 type Iter = IntoIter<BasicBlock>;
1958 impl<'a, 'b> GraphSuccessors<'b> for Mir<'a> {
1959 type Item = BasicBlock;
1960 type Iter = iter::Cloned<Successors<'b>>;
1963 #[derive(Copy, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
1964 pub struct Location {
1965 /// the location is within this block
1966 pub block: BasicBlock,
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,
1973 impl fmt::Debug for Location {
1974 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1975 write!(fmt, "{:?}[{}]", self.block, self.statement_index)
1980 pub const START: Location = Location {
1985 /// Returns the location immediately after this one within the enclosing block.
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 }
1993 pub fn dominates(&self, other: Location, dominators: &Dominators<BasicBlock>) -> bool {
1994 if self.block == other.block {
1995 self.statement_index <= other.statement_index
1997 dominators.is_dominated_by(other.block, self.block)
2002 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
2003 pub enum UnsafetyViolationKind {
2005 ExternStatic(ast::NodeId),
2006 BorrowPacked(ast::NodeId),
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,
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)]>,
2025 /// The layout of generator state
2026 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
2027 pub struct GeneratorLayout<'tcx> {
2028 pub fields: Vec<LocalDecl<'tcx>>,
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]>,
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.
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.
2053 /// Example: If type check produces a closure with the closure substs:
2056 /// ClosureSubsts = [
2057 /// i8, // the "closure kind"
2058 /// for<'x> fn(&'a &'x u32) -> &'x u32, // the "closure signature"
2059 /// &'a String, // some upvar
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:
2067 /// ClosureSubsts = [
2068 /// i8, // the "closure kind"
2069 /// for<'x> fn(&'1 &'x u32) -> &'x u32, // the "closure signature"
2070 /// &'2 String, // some upvar
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.
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
2094 pub num_external_vids: usize,
2096 /// Requirements between the various free regions defined in
2098 pub outlives_requirements: Vec<ClosureOutlivesRequirement<'gcx>>,
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>,
2108 // .. must outlive this one.
2109 pub outlived_free_region: ty::RegionVid,
2111 // If not, report an error here.
2112 pub blame_span: Span,
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`.
2123 /// The type here is guaranteed not to contain any free regions at
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),
2133 * TypeFoldable implementations for MIR types
2136 CloneTypeFoldableAndLiftImpls! {
2141 VisibilityScopeData,
2143 VisibilityScopeInfo,
2146 BraceStructTypeFoldableImpl! {
2147 impl<'tcx> TypeFoldable<'tcx> for Mir<'tcx> {
2150 visibility_scope_info,
2164 BraceStructTypeFoldableImpl! {
2165 impl<'tcx> TypeFoldable<'tcx> for GeneratorLayout<'tcx> {
2170 BraceStructTypeFoldableImpl! {
2171 impl<'tcx> TypeFoldable<'tcx> for LocalDecl<'tcx> {
2182 BraceStructTypeFoldableImpl! {
2183 impl<'tcx> TypeFoldable<'tcx> for BasicBlockData<'tcx> {
2190 BraceStructTypeFoldableImpl! {
2191 impl<'tcx> TypeFoldable<'tcx> for ValidationOperand<'tcx, Place<'tcx>> {
2192 place, ty, re, mutbl
2196 BraceStructTypeFoldableImpl! {
2197 impl<'tcx> TypeFoldable<'tcx> for Statement<'tcx> {
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),
2216 EnumTypeFoldableImpl! {
2217 impl<'tcx, T> TypeFoldable<'tcx> for ClearCrossCrate<T> {
2218 (ClearCrossCrate::Clear),
2219 (ClearCrossCrate::Set)(a),
2220 } where T: TypeFoldable<'tcx>
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::*;
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()
2235 Drop { ref location, target, unwind } => Drop {
2236 location: location.fold_with(folder),
2240 DropAndReplace { ref location, ref value, target, unwind } => DropAndReplace {
2241 location: location.fold_with(folder),
2242 value: value.fold_with(folder),
2246 Yield { ref value, resume, drop } => Yield {
2247 value: value.fold_with(folder),
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)
2257 func: func.fold_with(folder),
2258 args: args.fold_with(folder),
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),
2273 cond: cond.fold_with(folder),
2280 GeneratorDrop => GeneratorDrop,
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 },
2290 source_info: self.source_info,
2295 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
2296 use mir::TerminatorKind::*;
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)
2310 dest || func.visit_with(visitor) || args.visit_with(visitor)
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)
2330 FalseUnwind { .. } => false
2335 impl<'tcx> TypeFoldable<'tcx> for Place<'tcx> {
2336 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
2338 &Place::Projection(ref p) => Place::Projection(p.fold_with(folder)),
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)
2352 impl<'tcx> TypeFoldable<'tcx> for Rvalue<'tcx> {
2353 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> 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),
2380 Aggregate(kind, fields.fold_with(folder))
2385 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
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) => {
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)
2412 impl<'tcx> TypeFoldable<'tcx> for Operand<'tcx> {
2413 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> 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)),
2421 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
2423 Operand::Copy(ref place) |
2424 Operand::Move(ref place) => place.visit_with(visitor),
2425 Operand::Constant(ref c) => c.visit_with(visitor)
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>
2433 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
2434 use mir::ProjectionElem::*;
2436 let base = self.base.fold_with(folder);
2437 let elem = match self.elem {
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()
2450 fn super_visit_with<Vs: TypeVisitor<'tcx>>(&self, visitor: &mut Vs) -> bool {
2451 use mir::ProjectionElem::*;
2453 self.base.visit_with(visitor) ||
2455 Field(_, ref ty) => ty.visit_with(visitor),
2456 Index(ref v) => v.visit_with(visitor),
2462 impl<'tcx> TypeFoldable<'tcx> for Field {
2463 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, _: &mut F) -> Self {
2466 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> bool {
2471 impl<'tcx> TypeFoldable<'tcx> for Constant<'tcx> {
2472 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
2474 span: self.span.clone(),
2475 ty: self.ty.fold_with(folder),
2476 literal: self.literal.fold_with(folder)
2479 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
2480 self.ty.visit_with(visitor) || self.literal.visit_with(visitor)
2484 impl<'tcx> TypeFoldable<'tcx> for Literal<'tcx> {
2485 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
2487 Literal::Value { value } => Literal::Value {
2488 value: value.fold_with(folder)
2490 Literal::Promoted { index } => Literal::Promoted { index }
2493 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
2495 Literal::Value { value } => value.visit_with(visitor),
2496 Literal::Promoted { .. } => false