2015-08-18 17:59:21 -04:00
|
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
2018-03-16 12:43:22 -05:00
|
|
|
|
//! MIR datatypes and passes. See the [rustc guide] for more info.
|
2017-12-31 17:08:04 +01:00
|
|
|
|
//!
|
2018-03-16 12:43:22 -05:00
|
|
|
|
//! [rustc guide]: https://rust-lang-nursery.github.io/rustc-guide/mir.html
|
2017-04-29 05:28:35 -04:00
|
|
|
|
|
2016-01-29 19:06:23 +02:00
|
|
|
|
use graphviz::IntoCow;
|
2017-08-31 21:37:38 +03:00
|
|
|
|
use middle::region;
|
2018-02-27 17:11:14 +01:00
|
|
|
|
use rustc_data_structures::sync::{Lrc};
|
2016-06-07 17:28:36 +03:00
|
|
|
|
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
|
2016-06-09 15:49:07 -07:00
|
|
|
|
use rustc_data_structures::control_flow_graph::dominators::{Dominators, dominators};
|
|
|
|
|
use rustc_data_structures::control_flow_graph::{GraphPredecessors, GraphSuccessors};
|
|
|
|
|
use rustc_data_structures::control_flow_graph::ControlFlowGraph;
|
2018-03-02 20:42:37 -08:00
|
|
|
|
use rustc_data_structures::small_vec::SmallVec;
|
2017-09-13 22:33:07 +03:00
|
|
|
|
use rustc_serialize as serialize;
|
2016-09-15 00:51:46 +03:00
|
|
|
|
use hir::def::CtorKind;
|
2016-03-29 12:54:26 +03:00
|
|
|
|
use hir::def_id::DefId;
|
2017-12-12 17:29:37 -03:00
|
|
|
|
use mir::visit::MirVisitable;
|
2018-05-20 23:43:16 +02:00
|
|
|
|
use mir::interpret::{Value, Scalar, EvalErrorKind};
|
2017-03-14 01:08:21 +02:00
|
|
|
|
use ty::subst::{Subst, Substs};
|
2018-05-02 13:14:30 +02:00
|
|
|
|
use ty::{self, AdtDef, CanonicalTy, ClosureSubsts, GeneratorSubsts, Region, Ty, TyCtxt};
|
2017-02-08 22:24:49 +13:00
|
|
|
|
use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
|
2016-02-23 22:04:27 +02:00
|
|
|
|
use util::ppaux;
|
2017-12-06 09:25:29 +01:00
|
|
|
|
use std::slice;
|
2017-07-21 12:43:09 -07:00
|
|
|
|
use hir::{self, InlineAsm};
|
2016-01-29 19:06:23 +02:00
|
|
|
|
use std::borrow::{Cow};
|
2018-02-15 10:52:26 +01:00
|
|
|
|
use rustc_data_structures::sync::ReadGuard;
|
2015-12-31 20:11:25 -06:00
|
|
|
|
use std::fmt::{self, Debug, Formatter, Write};
|
2018-04-27 14:02:09 +03:00
|
|
|
|
use std::{iter, mem, option, u32};
|
2016-01-07 05:49:46 -05:00
|
|
|
|
use std::ops::{Index, IndexMut};
|
2016-06-09 15:49:07 -07:00
|
|
|
|
use std::vec::IntoIter;
|
2017-09-13 22:33:07 +03:00
|
|
|
|
use syntax::ast::{self, Name};
|
2017-12-06 09:25:29 +01:00
|
|
|
|
use syntax::symbol::InternedString;
|
2018-02-16 19:20:18 +02:00
|
|
|
|
use syntax_pos::{Span, DUMMY_SP};
|
2018-04-25 15:26:12 +02:00
|
|
|
|
use rustc_apfloat::ieee::{Single, Double};
|
|
|
|
|
use rustc_apfloat::Float;
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2018-04-27 15:21:31 +02:00
|
|
|
|
pub use mir::interpret::AssertMessage;
|
|
|
|
|
|
2016-09-19 23:50:00 +03:00
|
|
|
|
mod cache;
|
|
|
|
|
pub mod tcx;
|
|
|
|
|
pub mod visit;
|
|
|
|
|
pub mod traversal;
|
2017-09-18 16:18:23 +02:00
|
|
|
|
pub mod interpret;
|
2017-10-27 10:50:39 +02:00
|
|
|
|
pub mod mono;
|
2016-06-07 22:02:08 +03:00
|
|
|
|
|
2017-07-11 16:02:06 -07:00
|
|
|
|
/// Types for locals
|
|
|
|
|
type LocalDecls<'tcx> = IndexVec<Local, LocalDecl<'tcx>>;
|
|
|
|
|
|
2017-07-12 13:15:29 -07:00
|
|
|
|
pub trait HasLocalDecls<'tcx> {
|
|
|
|
|
fn local_decls(&self) -> &LocalDecls<'tcx>;
|
2017-07-12 12:59:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-12 13:15:29 -07:00
|
|
|
|
impl<'tcx> HasLocalDecls<'tcx> for LocalDecls<'tcx> {
|
|
|
|
|
fn local_decls(&self) -> &LocalDecls<'tcx> {
|
2017-07-12 12:59:05 -07:00
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-12 13:15:29 -07:00
|
|
|
|
impl<'tcx> HasLocalDecls<'tcx> for Mir<'tcx> {
|
|
|
|
|
fn local_decls(&self) -> &LocalDecls<'tcx> {
|
2017-07-12 12:59:05 -07:00
|
|
|
|
&self.local_decls
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
|
/// Lowered representation of a single function.
|
2017-02-08 22:24:49 +13:00
|
|
|
|
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub struct Mir<'tcx> {
|
2015-11-12 14:29:23 -05:00
|
|
|
|
/// List of basic blocks. References to basic block use a newtyped index type `BasicBlock`
|
|
|
|
|
/// that indexes into this vector.
|
2016-06-07 21:20:50 +03:00
|
|
|
|
basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2018-05-28 14:16:09 +03:00
|
|
|
|
/// List of source scopes; these are referenced by statements
|
|
|
|
|
/// and used for debuginfo. Indexed by a `SourceScope`.
|
|
|
|
|
pub source_scopes: IndexVec<SourceScope, SourceScopeData>,
|
2016-03-09 11:04:26 -05:00
|
|
|
|
|
2018-05-28 14:16:09 +03:00
|
|
|
|
/// Crate-local information for each source scope, that can't (and
|
2017-09-13 22:33:07 +03:00
|
|
|
|
/// needn't) be tracked across crates.
|
2018-05-28 17:37:48 +03:00
|
|
|
|
pub source_scope_local_data: ClearCrossCrate<IndexVec<SourceScope, SourceScopeLocalData>>,
|
2017-09-13 22:33:07 +03:00
|
|
|
|
|
2016-05-03 00:26:41 +03:00
|
|
|
|
/// Rvalues promoted from this function, such as borrows of constants.
|
|
|
|
|
/// Each of them is the Mir of a constant with the fn's type parameters
|
2016-09-25 01:38:27 +02:00
|
|
|
|
/// in scope, but a separate set of locals.
|
2016-06-07 17:28:36 +03:00
|
|
|
|
pub promoted: IndexVec<Promoted, Mir<'tcx>>,
|
2016-05-03 00:26:41 +03:00
|
|
|
|
|
2017-07-10 21:11:31 +02:00
|
|
|
|
/// Yield type of the function, if it is a generator.
|
|
|
|
|
pub yield_ty: Option<Ty<'tcx>>,
|
2016-12-26 14:34:03 +01:00
|
|
|
|
|
|
|
|
|
/// Generator drop glue
|
|
|
|
|
pub generator_drop: Option<Box<Mir<'tcx>>>,
|
|
|
|
|
|
|
|
|
|
/// The layout of a generator. Produced by the state transformation.
|
|
|
|
|
pub generator_layout: Option<GeneratorLayout<'tcx>>,
|
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
|
/// Declarations of locals.
|
|
|
|
|
///
|
|
|
|
|
/// The first local is the return value pointer, followed by `arg_count`
|
|
|
|
|
/// locals for the function arguments, followed by any user-declared
|
|
|
|
|
/// variables and temporaries.
|
2017-07-11 16:02:06 -07:00
|
|
|
|
pub local_decls: LocalDecls<'tcx>,
|
2015-11-12 14:29:23 -05:00
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
|
/// Number of arguments this function takes.
|
|
|
|
|
///
|
2016-09-29 01:11:54 +02:00
|
|
|
|
/// Starting at local 1, `arg_count` locals will be provided by the caller
|
2016-09-25 01:38:27 +02:00
|
|
|
|
/// and can be assumed to be initialized.
|
|
|
|
|
///
|
|
|
|
|
/// If this MIR was built for a constant, this will be 0.
|
|
|
|
|
pub arg_count: usize,
|
2016-02-07 21:13:00 +02:00
|
|
|
|
|
2016-04-16 21:51:26 +03:00
|
|
|
|
/// Names and capture modes of all the closure upvars, assuming
|
|
|
|
|
/// the first argument is either the closure or a reference to it.
|
|
|
|
|
pub upvar_decls: Vec<UpvarDecl>,
|
|
|
|
|
|
2016-09-26 22:44:01 +02:00
|
|
|
|
/// Mark an argument local (which must be a tuple) as getting passed as
|
|
|
|
|
/// its individual components at the LLVM level.
|
2016-09-21 00:45:30 +02:00
|
|
|
|
///
|
|
|
|
|
/// This is used for the "rust-call" ABI.
|
2016-09-26 22:44:01 +02:00
|
|
|
|
pub spread_arg: Option<Local>,
|
2016-09-21 00:45:30 +02:00
|
|
|
|
|
2016-02-07 21:13:00 +02:00
|
|
|
|
/// A span representing this MIR, for error reporting
|
|
|
|
|
pub span: Span,
|
2016-06-07 22:02:08 +03:00
|
|
|
|
|
|
|
|
|
/// A cache for various calculations
|
2016-09-19 23:50:00 +03:00
|
|
|
|
cache: cache::Cache
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// where execution begins
|
|
|
|
|
pub const START_BLOCK: BasicBlock = BasicBlock(0);
|
|
|
|
|
|
2015-10-05 12:31:48 -04:00
|
|
|
|
impl<'tcx> Mir<'tcx> {
|
2016-06-07 21:20:50 +03:00
|
|
|
|
pub fn new(basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
|
2018-05-28 14:16:09 +03:00
|
|
|
|
source_scopes: IndexVec<SourceScope, SourceScopeData>,
|
2018-05-28 17:37:48 +03:00
|
|
|
|
source_scope_local_data: ClearCrossCrate<IndexVec<SourceScope,
|
|
|
|
|
SourceScopeLocalData>>,
|
2016-06-07 21:20:50 +03:00
|
|
|
|
promoted: IndexVec<Promoted, Mir<'tcx>>,
|
2017-07-10 21:11:31 +02:00
|
|
|
|
yield_ty: Option<Ty<'tcx>>,
|
2016-09-25 01:38:27 +02:00
|
|
|
|
local_decls: IndexVec<Local, LocalDecl<'tcx>>,
|
|
|
|
|
arg_count: usize,
|
2016-06-07 21:20:50 +03:00
|
|
|
|
upvar_decls: Vec<UpvarDecl>,
|
|
|
|
|
span: Span) -> Self
|
|
|
|
|
{
|
2017-12-06 09:25:29 +01:00
|
|
|
|
// We need `arg_count` locals, and one for the return place
|
2016-09-25 01:38:27 +02:00
|
|
|
|
assert!(local_decls.len() >= arg_count + 1,
|
|
|
|
|
"expected at least {} locals, got {}", arg_count + 1, local_decls.len());
|
|
|
|
|
|
2016-06-07 21:20:50 +03:00
|
|
|
|
Mir {
|
2017-07-03 11:19:51 -07:00
|
|
|
|
basic_blocks,
|
2018-05-28 14:16:09 +03:00
|
|
|
|
source_scopes,
|
2018-05-28 17:37:48 +03:00
|
|
|
|
source_scope_local_data,
|
2017-07-03 11:19:51 -07:00
|
|
|
|
promoted,
|
2017-07-10 21:11:31 +02:00
|
|
|
|
yield_ty,
|
2016-12-26 14:34:03 +01:00
|
|
|
|
generator_drop: None,
|
|
|
|
|
generator_layout: None,
|
2017-07-03 11:19:51 -07:00
|
|
|
|
local_decls,
|
|
|
|
|
arg_count,
|
|
|
|
|
upvar_decls,
|
2016-09-26 22:44:01 +02:00
|
|
|
|
spread_arg: None,
|
2017-07-03 11:19:51 -07:00
|
|
|
|
span,
|
2016-09-19 23:50:00 +03:00
|
|
|
|
cache: cache::Cache::new()
|
2016-06-07 21:20:50 +03:00
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-07 21:20:50 +03:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn basic_blocks(&self) -> &IndexVec<BasicBlock, BasicBlockData<'tcx>> {
|
|
|
|
|
&self.basic_blocks
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-07 21:20:50 +03:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn basic_blocks_mut(&mut self) -> &mut IndexVec<BasicBlock, BasicBlockData<'tcx>> {
|
2016-06-07 22:02:08 +03:00
|
|
|
|
self.cache.invalidate();
|
2016-06-07 21:20:50 +03:00
|
|
|
|
&mut self.basic_blocks
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
2016-06-07 22:02:08 +03:00
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn basic_blocks_and_local_decls_mut(&mut self) -> (
|
|
|
|
|
&mut IndexVec<BasicBlock, BasicBlockData<'tcx>>,
|
|
|
|
|
&mut LocalDecls<'tcx>,
|
|
|
|
|
) {
|
|
|
|
|
self.cache.invalidate();
|
|
|
|
|
(&mut self.basic_blocks, &mut self.local_decls)
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-07 22:02:08 +03:00
|
|
|
|
#[inline]
|
2018-02-15 10:52:26 +01:00
|
|
|
|
pub fn predecessors(&self) -> ReadGuard<IndexVec<BasicBlock, Vec<BasicBlock>>> {
|
2016-06-07 22:02:08 +03:00
|
|
|
|
self.cache.predecessors(self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2018-02-15 10:52:26 +01:00
|
|
|
|
pub fn predecessors_for(&self, bb: BasicBlock) -> ReadGuard<Vec<BasicBlock>> {
|
|
|
|
|
ReadGuard::map(self.predecessors(), |p| &p[bb])
|
2016-06-07 22:02:08 +03:00
|
|
|
|
}
|
2016-06-20 23:55:14 +03:00
|
|
|
|
|
2016-06-09 15:49:07 -07:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn dominators(&self) -> Dominators<BasicBlock> {
|
|
|
|
|
dominators(self)
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn local_kind(&self, local: Local) -> LocalKind {
|
|
|
|
|
let index = local.0 as usize;
|
|
|
|
|
if index == 0 {
|
|
|
|
|
debug_assert!(self.local_decls[local].mutability == Mutability::Mut,
|
2017-12-06 09:25:29 +01:00
|
|
|
|
"return place should be mutable");
|
2016-09-25 01:38:27 +02:00
|
|
|
|
|
|
|
|
|
LocalKind::ReturnPointer
|
|
|
|
|
} else if index < self.arg_count + 1 {
|
|
|
|
|
LocalKind::Arg
|
|
|
|
|
} else if self.local_decls[local].name.is_some() {
|
|
|
|
|
LocalKind::Var
|
|
|
|
|
} else {
|
|
|
|
|
debug_assert!(self.local_decls[local].mutability == Mutability::Mut,
|
|
|
|
|
"temp should be mutable");
|
|
|
|
|
|
|
|
|
|
LocalKind::Temp
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns an iterator over all temporaries.
|
|
|
|
|
#[inline]
|
2016-09-26 22:50:03 +02:00
|
|
|
|
pub fn temps_iter<'a>(&'a self) -> impl Iterator<Item=Local> + 'a {
|
2016-09-25 01:38:27 +02:00
|
|
|
|
(self.arg_count+1..self.local_decls.len()).filter_map(move |index| {
|
|
|
|
|
let local = Local::new(index);
|
2018-06-07 15:25:08 +02:00
|
|
|
|
if self.local_decls[local].is_user_variable.is_some() {
|
2016-09-25 01:38:27 +02:00
|
|
|
|
None
|
2017-04-11 23:52:51 +03:00
|
|
|
|
} else {
|
|
|
|
|
Some(local)
|
2016-06-20 23:55:14 +03:00
|
|
|
|
}
|
2016-09-25 01:38:27 +02:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns an iterator over all user-declared locals.
|
|
|
|
|
#[inline]
|
2016-09-26 22:50:03 +02:00
|
|
|
|
pub fn vars_iter<'a>(&'a self) -> impl Iterator<Item=Local> + 'a {
|
2016-09-25 01:38:27 +02:00
|
|
|
|
(self.arg_count+1..self.local_decls.len()).filter_map(move |index| {
|
|
|
|
|
let local = Local::new(index);
|
2018-06-07 15:25:08 +02:00
|
|
|
|
if self.local_decls[local].is_user_variable.is_some() {
|
2016-09-25 01:38:27 +02:00
|
|
|
|
Some(local)
|
2017-04-11 23:52:51 +03:00
|
|
|
|
} else {
|
|
|
|
|
None
|
2016-06-20 23:55:14 +03:00
|
|
|
|
}
|
2016-09-25 01:38:27 +02:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-12 08:47:44 -07:00
|
|
|
|
/// Returns an iterator over all user-declared mutable arguments and locals.
|
2018-02-28 01:09:08 -08:00
|
|
|
|
#[inline]
|
2018-03-12 08:47:44 -07:00
|
|
|
|
pub fn mut_vars_and_args_iter<'a>(&'a self) -> impl Iterator<Item=Local> + 'a {
|
|
|
|
|
(1..self.local_decls.len()).filter_map(move |index| {
|
2018-02-28 01:09:08 -08:00
|
|
|
|
let local = Local::new(index);
|
|
|
|
|
let decl = &self.local_decls[local];
|
2018-06-07 15:25:08 +02:00
|
|
|
|
if (decl.is_user_variable.is_some() || index < self.arg_count + 1)
|
2018-03-12 08:47:44 -07:00
|
|
|
|
&& decl.mutability == Mutability::Mut
|
|
|
|
|
{
|
2018-02-28 01:09:08 -08:00
|
|
|
|
Some(local)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
|
/// Returns an iterator over all function arguments.
|
|
|
|
|
#[inline]
|
2016-09-26 22:51:51 +02:00
|
|
|
|
pub fn args_iter(&self) -> impl Iterator<Item=Local> {
|
|
|
|
|
let arg_count = self.arg_count;
|
|
|
|
|
(1..arg_count+1).map(Local::new)
|
2016-09-25 01:38:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns an iterator over all user-defined variables and compiler-generated temporaries (all
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// locals that are neither arguments nor the return place).
|
2016-09-25 01:38:27 +02:00
|
|
|
|
#[inline]
|
2016-09-27 02:03:35 +02:00
|
|
|
|
pub fn vars_and_temps_iter(&self) -> impl Iterator<Item=Local> {
|
|
|
|
|
let arg_count = self.arg_count;
|
|
|
|
|
let local_count = self.local_decls.len();
|
|
|
|
|
(arg_count+1..local_count).map(Local::new)
|
2016-09-15 18:18:40 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-15 18:17:58 -07:00
|
|
|
|
/// Changes a statement to a nop. This is both faster than deleting instructions and avoids
|
|
|
|
|
/// invalidating statement indices in `Location`s.
|
|
|
|
|
pub fn make_statement_nop(&mut self, location: Location) {
|
|
|
|
|
let block = &mut self[location.block];
|
|
|
|
|
debug_assert!(location.statement_index < block.statements.len());
|
|
|
|
|
block.statements[location.statement_index].make_nop()
|
|
|
|
|
}
|
2017-12-06 09:25:29 +01:00
|
|
|
|
|
|
|
|
|
/// Returns the source info associated with `location`.
|
|
|
|
|
pub fn source_info(&self, location: Location) -> &SourceInfo {
|
|
|
|
|
let block = &self[location.block];
|
|
|
|
|
let stmts = &block.statements;
|
|
|
|
|
let idx = location.statement_index;
|
|
|
|
|
if idx < stmts.len() {
|
|
|
|
|
&stmts[idx].source_info
|
|
|
|
|
} else {
|
|
|
|
|
assert!(idx == stmts.len());
|
|
|
|
|
&block.terminator().source_info
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-13 14:51:53 -03:00
|
|
|
|
/// Check if `sub` is a sub scope of `sup`
|
|
|
|
|
pub fn is_sub_scope(&self, mut sub: SourceScope, sup: SourceScope) -> bool {
|
|
|
|
|
loop {
|
|
|
|
|
if sub == sup {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
match self.source_scopes[sub].parent_scope {
|
|
|
|
|
None => return false,
|
|
|
|
|
Some(p) => sub = p,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// Return the return type, it always return first element from `local_decls` array
|
|
|
|
|
pub fn return_ty(&self) -> Ty<'tcx> {
|
|
|
|
|
self.local_decls[RETURN_PLACE].ty
|
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
|
2017-09-19 16:20:02 +03:00
|
|
|
|
pub enum Safety {
|
|
|
|
|
Safe,
|
|
|
|
|
/// Unsafe because of a PushUnsafeBlock
|
|
|
|
|
BuiltinUnsafe,
|
|
|
|
|
/// Unsafe because of an unsafe fn
|
|
|
|
|
FnUnsafe,
|
|
|
|
|
/// Unsafe because of an `unsafe` block
|
|
|
|
|
ExplicitUnsafe(ast::NodeId)
|
2017-09-13 22:33:07 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-30 15:27:27 +02:00
|
|
|
|
impl_stable_hash_for!(struct Mir<'tcx> {
|
|
|
|
|
basic_blocks,
|
2018-05-28 14:16:09 +03:00
|
|
|
|
source_scopes,
|
2018-05-28 17:37:48 +03:00
|
|
|
|
source_scope_local_data,
|
2017-03-30 15:27:27 +02:00
|
|
|
|
promoted,
|
2017-07-10 21:11:31 +02:00
|
|
|
|
yield_ty,
|
2016-12-26 14:34:03 +01:00
|
|
|
|
generator_drop,
|
|
|
|
|
generator_layout,
|
2017-03-30 15:27:27 +02:00
|
|
|
|
local_decls,
|
|
|
|
|
arg_count,
|
|
|
|
|
upvar_decls,
|
|
|
|
|
spread_arg,
|
|
|
|
|
span,
|
|
|
|
|
cache
|
|
|
|
|
});
|
|
|
|
|
|
2016-01-07 05:49:46 -05:00
|
|
|
|
impl<'tcx> Index<BasicBlock> for Mir<'tcx> {
|
|
|
|
|
type Output = BasicBlockData<'tcx>;
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn index(&self, index: BasicBlock) -> &BasicBlockData<'tcx> {
|
2016-06-07 21:20:50 +03:00
|
|
|
|
&self.basic_blocks()[index]
|
2016-01-07 05:49:46 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'tcx> IndexMut<BasicBlock> for Mir<'tcx> {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn index_mut(&mut self, index: BasicBlock) -> &mut BasicBlockData<'tcx> {
|
2016-06-07 21:20:50 +03:00
|
|
|
|
&mut self.basic_blocks_mut()[index]
|
2016-01-07 05:49:46 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-07 15:25:08 +02:00
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
2017-12-06 09:25:29 +01:00
|
|
|
|
pub enum ClearCrossCrate<T> {
|
2017-09-13 22:33:07 +03:00
|
|
|
|
Clear,
|
|
|
|
|
Set(T)
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
impl<T: serialize::Encodable> serialize::UseSpecializedEncodable for ClearCrossCrate<T> {}
|
|
|
|
|
impl<T: serialize::Decodable> serialize::UseSpecializedDecodable for ClearCrossCrate<T> {}
|
2017-09-13 22:33:07 +03:00
|
|
|
|
|
2016-06-07 19:21:56 +03:00
|
|
|
|
/// Grouped information about the source code origin of a MIR entity.
|
|
|
|
|
/// Intended to be inspected by diagnostics and debuginfo.
|
|
|
|
|
/// Most passes can work with it as a whole, within a single function.
|
2017-09-19 16:20:02 +03:00
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
|
2016-06-07 19:21:56 +03:00
|
|
|
|
pub struct SourceInfo {
|
|
|
|
|
/// Source span for the AST pertaining to this MIR entity.
|
|
|
|
|
pub span: Span,
|
|
|
|
|
|
2018-05-28 14:16:09 +03:00
|
|
|
|
/// The source scope, keeping track of which bindings can be
|
|
|
|
|
/// seen by debuginfo, active lint levels, `unsafe {...}`, etc.
|
|
|
|
|
pub scope: SourceScope
|
2016-06-07 19:21:56 +03:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Mutability and borrow kinds
|
|
|
|
|
|
2015-12-08 15:53:19 -05:00
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
|
2015-08-18 17:59:21 -04:00
|
|
|
|
pub enum Mutability {
|
|
|
|
|
Mut,
|
|
|
|
|
Not,
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-07 15:25:08 +02:00
|
|
|
|
impl From<Mutability> for hir::Mutability {
|
|
|
|
|
fn from(m: Mutability) -> Self {
|
|
|
|
|
match m {
|
|
|
|
|
Mutability::Mut => hir::MutMutable,
|
|
|
|
|
Mutability::Not => hir::MutImmutable,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-12-08 15:53:19 -05:00
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
|
2015-08-18 17:59:21 -04:00
|
|
|
|
pub enum BorrowKind {
|
|
|
|
|
/// Data must be immutable and is aliasable.
|
|
|
|
|
Shared,
|
|
|
|
|
|
|
|
|
|
/// Data must be immutable but not aliasable. This kind of borrow
|
|
|
|
|
/// cannot currently be expressed by the user and is used only in
|
|
|
|
|
/// implicit closure bindings. It is needed when you the closure
|
|
|
|
|
/// is borrowing or mutating a mutable referent, e.g.:
|
|
|
|
|
///
|
|
|
|
|
/// let x: &mut isize = ...;
|
|
|
|
|
/// let y = || *x += 5;
|
|
|
|
|
///
|
|
|
|
|
/// If we were to try to translate this closure into a more explicit
|
|
|
|
|
/// form, we'd encounter an error with the code as written:
|
|
|
|
|
///
|
|
|
|
|
/// struct Env { x: & &mut isize }
|
|
|
|
|
/// let x: &mut isize = ...;
|
|
|
|
|
/// let y = (&mut Env { &x }, fn_ptr); // Closure is pair of env and fn
|
|
|
|
|
/// fn fn_ptr(env: &mut Env) { **env.x += 5; }
|
|
|
|
|
///
|
|
|
|
|
/// This is then illegal because you cannot mutate a `&mut` found
|
|
|
|
|
/// in an aliasable location. To solve, you'd have to translate with
|
|
|
|
|
/// an `&mut` borrow:
|
|
|
|
|
///
|
|
|
|
|
/// struct Env { x: & &mut isize }
|
|
|
|
|
/// let x: &mut isize = ...;
|
|
|
|
|
/// let y = (&mut Env { &mut x }, fn_ptr); // changed from &x to &mut x
|
|
|
|
|
/// fn fn_ptr(env: &mut Env) { **env.x += 5; }
|
|
|
|
|
///
|
|
|
|
|
/// Now the assignment to `**env.x` is legal, but creating a
|
|
|
|
|
/// mutable pointer to `x` is not because `x` is not mutable. We
|
|
|
|
|
/// could fix this by declaring `x` as `let mut x`. This is ok in
|
|
|
|
|
/// user code, if awkward, but extra weird for closures, since the
|
|
|
|
|
/// borrow is hidden.
|
|
|
|
|
///
|
|
|
|
|
/// So we introduce a "unique imm" borrow -- the referent is
|
|
|
|
|
/// immutable, but not aliasable. This solves the problem. For
|
|
|
|
|
/// simplicity, we don't give users the way to express this
|
|
|
|
|
/// borrow, it's just used when translating closures.
|
|
|
|
|
Unique,
|
|
|
|
|
|
|
|
|
|
/// Data is mutable and not aliasable.
|
2018-01-15 12:47:26 +01:00
|
|
|
|
Mut {
|
|
|
|
|
/// True if this borrow arose from method-call auto-ref
|
|
|
|
|
/// (i.e. `adjustment::Adjust::Borrow`)
|
|
|
|
|
allow_two_phase_borrow: bool
|
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-16 10:52:52 +01:00
|
|
|
|
impl BorrowKind {
|
|
|
|
|
pub fn allows_two_phase_borrow(&self) -> bool {
|
|
|
|
|
match *self {
|
|
|
|
|
BorrowKind::Shared | BorrowKind::Unique => false,
|
|
|
|
|
BorrowKind::Mut { allow_two_phase_borrow } => allow_two_phase_borrow,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Variables and temps
|
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
newtype_index!(Local
|
|
|
|
|
{
|
|
|
|
|
DEBUG_FORMAT = "_{}",
|
|
|
|
|
const RETURN_PLACE = 0,
|
|
|
|
|
});
|
2016-09-25 01:38:27 +02:00
|
|
|
|
|
|
|
|
|
/// Classifies locals into categories. See `Mir::local_kind`.
|
|
|
|
|
#[derive(PartialEq, Eq, Debug)]
|
|
|
|
|
pub enum LocalKind {
|
|
|
|
|
/// User-declared variable binding
|
|
|
|
|
Var,
|
|
|
|
|
/// Compiler-introduced temporary
|
|
|
|
|
Temp,
|
|
|
|
|
/// Function argument
|
|
|
|
|
Arg,
|
|
|
|
|
/// Location of function's return value
|
|
|
|
|
ReturnPointer,
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-07 15:25:08 +02:00
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
|
|
|
|
|
pub struct VarBindingForm {
|
|
|
|
|
/// Is variable bound via `x`, `mut x`, `ref x`, or `ref mut x`?
|
|
|
|
|
pub binding_mode: ty::BindingMode,
|
|
|
|
|
/// If an explicit type was provided for this variable binding,
|
|
|
|
|
/// this holds the source Span of that type.
|
|
|
|
|
///
|
|
|
|
|
/// NOTE: If you want to change this to a `HirId`, be wary that
|
|
|
|
|
/// doing so breaks incremental compilation (as of this writing),
|
|
|
|
|
/// while a `Span` does not cause our tests to fail.
|
|
|
|
|
pub opt_ty_info: Option<Span>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
|
|
|
|
|
pub enum BindingForm {
|
|
|
|
|
/// This is a binding for a non-`self` binding, or a `self` that has an explicit type.
|
|
|
|
|
Var(VarBindingForm),
|
|
|
|
|
/// Binding for a `self`/`&self`/`&mut self` binding where the type is implicit.
|
|
|
|
|
ImplicitSelf,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CloneTypeFoldableAndLiftImpls! { BindingForm, }
|
|
|
|
|
|
|
|
|
|
impl_stable_hash_for!(struct self::VarBindingForm { binding_mode, opt_ty_info });
|
|
|
|
|
|
|
|
|
|
impl_stable_hash_for!(enum self::BindingForm { Var(binding), ImplicitSelf, });
|
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
|
/// A MIR local.
|
|
|
|
|
///
|
|
|
|
|
/// This can be a binding declared by the user, a temporary inserted by the compiler, a function
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// argument, or the return place.
|
2016-02-07 21:13:00 +02:00
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
|
2016-09-25 01:38:27 +02:00
|
|
|
|
pub struct LocalDecl<'tcx> {
|
|
|
|
|
/// `let mut x` vs `let x`.
|
|
|
|
|
///
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// Temporaries and the return place are always mutable.
|
2015-08-18 17:59:21 -04:00
|
|
|
|
pub mutability: Mutability,
|
2016-03-23 04:21:02 -04:00
|
|
|
|
|
2018-06-07 15:25:08 +02:00
|
|
|
|
/// Some(binding_mode) if this corresponds to a user-declared local variable.
|
|
|
|
|
///
|
|
|
|
|
/// This is solely used for local diagnostics when generating
|
|
|
|
|
/// warnings/errors when compiling the current crate, and
|
|
|
|
|
/// therefore it need not be visible across crates. pnkfelix
|
|
|
|
|
/// currently hypothesized we *need* to wrap this in a
|
|
|
|
|
/// `ClearCrossCrate` as long as it carries as `HirId`.
|
|
|
|
|
pub is_user_variable: Option<ClearCrossCrate<BindingForm>>,
|
2017-04-11 23:52:51 +03:00
|
|
|
|
|
2017-08-24 11:42:32 -07:00
|
|
|
|
/// True if this is an internal local
|
|
|
|
|
///
|
2017-08-11 06:20:28 +02:00
|
|
|
|
/// These locals are not based on types in the source code and are only used
|
2017-09-19 16:20:02 +03:00
|
|
|
|
/// for a few desugarings at the moment.
|
2017-08-24 11:42:32 -07:00
|
|
|
|
///
|
|
|
|
|
/// The generator transformation will sanity check the locals which are live
|
|
|
|
|
/// across a suspension point against the type components of the generator
|
|
|
|
|
/// which type checking knows are live across a suspension point. We need to
|
|
|
|
|
/// flag drop flags to avoid triggering this check as they are introduced
|
2017-08-11 06:20:28 +02:00
|
|
|
|
/// after typeck.
|
2017-08-24 11:42:32 -07:00
|
|
|
|
///
|
2017-09-19 16:20:02 +03:00
|
|
|
|
/// Unsafety checking will also ignore dereferences of these locals,
|
|
|
|
|
/// so they can be used for raw pointers only used in a desugaring.
|
|
|
|
|
///
|
2017-08-24 11:42:32 -07:00
|
|
|
|
/// This should be sound because the drop flags are fully algebraic, and
|
|
|
|
|
/// therefore don't affect the OIBIT or outlives properties of the
|
|
|
|
|
/// generator.
|
2017-07-15 22:41:33 +02:00
|
|
|
|
pub internal: bool,
|
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
|
/// Type of this local.
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub ty: Ty<'tcx>,
|
2016-03-23 04:21:02 -04:00
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
|
/// Name of the local, used in debuginfo and pretty-printing.
|
|
|
|
|
///
|
|
|
|
|
/// Note that function arguments can also have this set to `Some(_)`
|
|
|
|
|
/// to generate better debuginfo.
|
|
|
|
|
pub name: Option<Name>,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2018-05-28 14:16:09 +03:00
|
|
|
|
/// The *syntactic* (i.e. not visibility) source scope the local is defined
|
2017-09-13 22:33:07 +03:00
|
|
|
|
/// in. If the local was defined in a let-statement, this
|
|
|
|
|
/// is *within* the let-statement, rather than outside
|
2017-09-20 16:34:31 +03:00
|
|
|
|
/// of it.
|
2017-12-21 00:35:19 +02:00
|
|
|
|
///
|
2018-05-28 14:16:09 +03:00
|
|
|
|
/// This is needed because the visibility source scope of locals within
|
|
|
|
|
/// a let-statement is weird.
|
2017-12-21 00:35:19 +02:00
|
|
|
|
///
|
|
|
|
|
/// The reason is that we want the local to be *within* the let-statement
|
|
|
|
|
/// for lint purposes, but we want the local to be *after* the let-statement
|
|
|
|
|
/// for names-in-scope purposes.
|
|
|
|
|
///
|
|
|
|
|
/// That's it, if we have a let-statement like the one in this
|
|
|
|
|
/// function:
|
2017-12-31 17:17:01 +01:00
|
|
|
|
///
|
2017-12-21 00:35:19 +02:00
|
|
|
|
/// ```
|
|
|
|
|
/// fn foo(x: &str) {
|
|
|
|
|
/// #[allow(unused_mut)]
|
|
|
|
|
/// let mut x: u32 = { // <- one unused mut
|
|
|
|
|
/// let mut y: u32 = x.parse().unwrap();
|
|
|
|
|
/// y + 2
|
|
|
|
|
/// };
|
|
|
|
|
/// drop(x);
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// Then, from a lint point of view, the declaration of `x: u32`
|
|
|
|
|
/// (and `y: u32`) are within the `#[allow(unused_mut)]` scope - the
|
|
|
|
|
/// lint scopes are the same as the AST/HIR nesting.
|
|
|
|
|
///
|
|
|
|
|
/// However, from a name lookup point of view, the scopes look more like
|
|
|
|
|
/// as if the let-statements were `match` expressions:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// fn foo(x: &str) {
|
|
|
|
|
/// match {
|
|
|
|
|
/// match x.parse().unwrap() {
|
|
|
|
|
/// y => y + 2
|
|
|
|
|
/// }
|
|
|
|
|
/// } {
|
|
|
|
|
/// x => drop(x)
|
|
|
|
|
/// };
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// We care about the name-lookup scopes for debuginfo - if the
|
|
|
|
|
/// debuginfo instruction pointer is at the call to `x.parse()`, we
|
|
|
|
|
/// want `x` to refer to `x: &str`, but if it is at the call to
|
|
|
|
|
/// `drop(x)`, we want it to refer to `x: u32`.
|
|
|
|
|
///
|
|
|
|
|
/// To allow both uses to work, we need to have more than a single scope
|
2018-05-29 21:31:33 +03:00
|
|
|
|
/// for a local. We have the `source_info.scope` represent the
|
2017-12-21 00:35:19 +02:00
|
|
|
|
/// "syntactic" lint scope (with a variable being under its let
|
2018-05-29 13:55:21 +03:00
|
|
|
|
/// block) while the `visibility_scope` represents the "local variable"
|
2017-12-21 00:35:19 +02:00
|
|
|
|
/// scope (where the "rest" of a block is under all prior let-statements).
|
|
|
|
|
///
|
|
|
|
|
/// The end result looks like this:
|
|
|
|
|
///
|
2017-12-31 17:17:01 +01:00
|
|
|
|
/// ```text
|
2017-12-21 00:35:19 +02:00
|
|
|
|
/// ROOT SCOPE
|
|
|
|
|
/// │{ argument x: &str }
|
|
|
|
|
/// │
|
|
|
|
|
/// │ │{ #[allow(unused_mut] } // this is actually split into 2 scopes
|
|
|
|
|
/// │ │ // in practice because I'm lazy.
|
|
|
|
|
/// │ │
|
2018-05-29 21:31:33 +03:00
|
|
|
|
/// │ │← x.source_info.scope
|
2017-12-21 00:35:19 +02:00
|
|
|
|
/// │ │← `x.parse().unwrap()`
|
|
|
|
|
/// │ │
|
2018-05-29 21:31:33 +03:00
|
|
|
|
/// │ │ │← y.source_info.scope
|
2017-12-21 00:35:19 +02:00
|
|
|
|
/// │ │
|
|
|
|
|
/// │ │ │{ let y: u32 }
|
|
|
|
|
/// │ │ │
|
2018-05-29 13:55:21 +03:00
|
|
|
|
/// │ │ │← y.visibility_scope
|
2017-12-21 00:35:19 +02:00
|
|
|
|
/// │ │ │← `y + 2`
|
|
|
|
|
/// │
|
|
|
|
|
/// │ │{ let x: u32 }
|
2018-05-29 13:55:21 +03:00
|
|
|
|
/// │ │← x.visibility_scope
|
2017-12-21 00:35:19 +02:00
|
|
|
|
/// │ │← `drop(x)` // this accesses `x: u32`
|
2017-12-31 17:17:01 +01:00
|
|
|
|
/// ```
|
2018-05-29 21:31:33 +03:00
|
|
|
|
pub source_info: SourceInfo,
|
2018-05-29 17:37:24 +03:00
|
|
|
|
|
2018-05-29 13:55:21 +03:00
|
|
|
|
/// Source scope within which the local is visible (for debuginfo)
|
2018-05-29 21:31:33 +03:00
|
|
|
|
/// (see `source_info` for more details).
|
2018-05-29 13:55:21 +03:00
|
|
|
|
pub visibility_scope: SourceScope,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
|
impl<'tcx> LocalDecl<'tcx> {
|
2018-06-15 05:47:36 +02:00
|
|
|
|
/// Returns true only if local is a binding that can itself be
|
|
|
|
|
/// made mutable via the addition of the `mut` keyword, namely
|
|
|
|
|
/// something like the occurrences of `x` in:
|
|
|
|
|
/// - `fn foo(x: Type) { ... }`,
|
|
|
|
|
/// - `let x = ...`,
|
|
|
|
|
/// - or `match ... { C(x) => ... }`
|
|
|
|
|
pub fn can_be_made_mutable(&self) -> bool
|
|
|
|
|
{
|
|
|
|
|
match self.is_user_variable {
|
|
|
|
|
Some(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
|
|
|
|
|
binding_mode: ty::BindingMode::BindByValue(_),
|
|
|
|
|
opt_ty_info: _,
|
|
|
|
|
}))) => true,
|
|
|
|
|
|
|
|
|
|
// FIXME: might be able to thread the distinction between
|
|
|
|
|
// `self`/`mut self`/`&self`/`&mut self` into the
|
|
|
|
|
// `BindingForm::ImplicitSelf` variant, (and then return
|
|
|
|
|
// true here for solely the first case).
|
|
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns true if local is definitely not a `ref ident` or
|
|
|
|
|
/// `ref mut ident` binding. (Such bindings cannot be made into
|
|
|
|
|
/// mutable bindings, but the inverse does not necessarily hold).
|
|
|
|
|
pub fn is_nonref_binding(&self) -> bool
|
|
|
|
|
{
|
|
|
|
|
match self.is_user_variable {
|
|
|
|
|
Some(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
|
|
|
|
|
binding_mode: ty::BindingMode::BindByValue(_),
|
|
|
|
|
opt_ty_info: _,
|
|
|
|
|
}))) => true,
|
|
|
|
|
|
|
|
|
|
Some(ClearCrossCrate::Set(BindingForm::ImplicitSelf)) => true,
|
|
|
|
|
|
|
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
|
/// Create a new `LocalDecl` for a temporary.
|
|
|
|
|
#[inline]
|
2017-04-11 23:52:51 +03:00
|
|
|
|
pub fn new_temp(ty: Ty<'tcx>, span: Span) -> Self {
|
2016-09-25 01:38:27 +02:00
|
|
|
|
LocalDecl {
|
|
|
|
|
mutability: Mutability::Mut,
|
2017-07-03 11:19:51 -07:00
|
|
|
|
ty,
|
2016-09-25 01:38:27 +02:00
|
|
|
|
name: None,
|
2018-05-29 21:31:33 +03:00
|
|
|
|
source_info: SourceInfo {
|
2017-07-03 11:19:51 -07:00
|
|
|
|
span,
|
2018-05-28 14:16:09 +03:00
|
|
|
|
scope: OUTERMOST_SOURCE_SCOPE
|
2017-04-11 23:52:51 +03:00
|
|
|
|
},
|
2018-05-29 13:55:21 +03:00
|
|
|
|
visibility_scope: OUTERMOST_SOURCE_SCOPE,
|
2017-07-15 22:41:33 +02:00
|
|
|
|
internal: false,
|
2018-06-07 15:25:08 +02:00
|
|
|
|
is_user_variable: None,
|
2017-07-15 22:41:33 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create a new `LocalDecl` for a internal temporary.
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn new_internal(ty: Ty<'tcx>, span: Span) -> Self {
|
|
|
|
|
LocalDecl {
|
|
|
|
|
mutability: Mutability::Mut,
|
|
|
|
|
ty,
|
|
|
|
|
name: None,
|
2018-05-29 21:31:33 +03:00
|
|
|
|
source_info: SourceInfo {
|
2017-07-15 22:41:33 +02:00
|
|
|
|
span,
|
2018-05-28 14:16:09 +03:00
|
|
|
|
scope: OUTERMOST_SOURCE_SCOPE
|
2017-07-15 22:41:33 +02:00
|
|
|
|
},
|
2018-05-29 13:55:21 +03:00
|
|
|
|
visibility_scope: OUTERMOST_SOURCE_SCOPE,
|
2017-07-15 22:41:33 +02:00
|
|
|
|
internal: true,
|
2018-06-07 15:25:08 +02:00
|
|
|
|
is_user_variable: None,
|
2016-09-25 01:38:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-08 14:24:44 +02:00
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// Builds a `LocalDecl` for the return place.
|
2016-09-25 01:38:27 +02:00
|
|
|
|
///
|
|
|
|
|
/// This must be inserted into the `local_decls` list as the first local.
|
|
|
|
|
#[inline]
|
2017-12-06 09:25:29 +01:00
|
|
|
|
pub fn new_return_place(return_ty: Ty, span: Span) -> LocalDecl {
|
2016-09-25 01:38:27 +02:00
|
|
|
|
LocalDecl {
|
|
|
|
|
mutability: Mutability::Mut,
|
|
|
|
|
ty: return_ty,
|
2018-05-29 21:31:33 +03:00
|
|
|
|
source_info: SourceInfo {
|
2017-07-03 11:19:51 -07:00
|
|
|
|
span,
|
2018-05-28 14:16:09 +03:00
|
|
|
|
scope: OUTERMOST_SOURCE_SCOPE
|
2017-04-11 23:52:51 +03:00
|
|
|
|
},
|
2018-05-29 13:55:21 +03:00
|
|
|
|
visibility_scope: OUTERMOST_SOURCE_SCOPE,
|
2017-07-15 22:41:33 +02:00
|
|
|
|
internal: false,
|
2016-09-25 01:38:27 +02:00
|
|
|
|
name: None, // FIXME maybe we do want some name here?
|
2018-06-07 15:25:08 +02:00
|
|
|
|
is_user_variable: None,
|
2016-09-25 01:38:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-04-16 21:51:26 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A closure capture, with its name and mode.
|
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
|
|
|
|
|
pub struct UpvarDecl {
|
|
|
|
|
pub debug_name: Name,
|
|
|
|
|
|
|
|
|
|
/// If true, the capture is behind a reference.
|
2017-12-06 09:25:29 +01:00
|
|
|
|
pub by_ref: bool,
|
|
|
|
|
|
|
|
|
|
pub mutability: Mutability,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// BasicBlock
|
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
newtype_index!(BasicBlock { DEBUG_FORMAT = "bb{}" });
|
|
|
|
|
|
|
|
|
|
impl BasicBlock {
|
|
|
|
|
pub fn start_location(self) -> Location {
|
|
|
|
|
Location {
|
|
|
|
|
block: self,
|
|
|
|
|
statement_index: 0,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2016-02-26 18:05:50 +02:00
|
|
|
|
// BasicBlockData and Terminator
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2016-01-26 08:36:34 -05:00
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub struct BasicBlockData<'tcx> {
|
2016-03-24 06:12:19 -04:00
|
|
|
|
/// List of statements in this block.
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub statements: Vec<Statement<'tcx>>,
|
2016-03-24 06:12:19 -04:00
|
|
|
|
|
|
|
|
|
/// Terminator for this block.
|
|
|
|
|
///
|
|
|
|
|
/// NB. This should generally ONLY be `None` during construction.
|
|
|
|
|
/// Therefore, you should generally access it via the
|
|
|
|
|
/// `terminator()` or `terminator_mut()` methods. The only
|
|
|
|
|
/// exception is that certain passes, such as `simplify_cfg`, swap
|
|
|
|
|
/// out the terminator temporarily with `None` while they continue
|
|
|
|
|
/// to recurse over the set of basic blocks.
|
2015-12-19 00:44:32 +02:00
|
|
|
|
pub terminator: Option<Terminator<'tcx>>,
|
2016-03-24 06:12:19 -04:00
|
|
|
|
|
|
|
|
|
/// If true, this block lies on an unwind path. This is used
|
2018-05-08 16:10:16 +03:00
|
|
|
|
/// during codegen where distinct kinds of basic blocks may be
|
2016-03-24 06:12:19 -04:00
|
|
|
|
/// generated (particularly for MSVC cleanup). Unwind blocks must
|
|
|
|
|
/// only branch to other unwind blocks.
|
2015-12-20 15:30:09 +02:00
|
|
|
|
pub is_cleanup: bool,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-10 09:55:15 -05:00
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
|
|
|
|
|
pub struct Terminator<'tcx> {
|
2016-06-07 19:21:56 +03:00
|
|
|
|
pub source_info: SourceInfo,
|
2016-03-10 09:55:15 -05:00
|
|
|
|
pub kind: TerminatorKind<'tcx>
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-26 08:36:34 -05:00
|
|
|
|
#[derive(Clone, RustcEncodable, RustcDecodable)]
|
2016-03-10 09:55:15 -05:00
|
|
|
|
pub enum TerminatorKind<'tcx> {
|
2015-08-18 17:59:21 -04:00
|
|
|
|
/// block should have one successor in the graph; we jump there
|
2015-10-07 14:37:42 +02:00
|
|
|
|
Goto {
|
|
|
|
|
target: BasicBlock,
|
|
|
|
|
},
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2015-10-26 14:35:18 -04:00
|
|
|
|
/// operand evaluates to an integer; jump depending on its value
|
|
|
|
|
/// to one of the targets, and otherwise fallback to `otherwise`
|
|
|
|
|
SwitchInt {
|
|
|
|
|
/// discriminant value being tested
|
2017-01-31 05:32:08 +02:00
|
|
|
|
discr: Operand<'tcx>,
|
2015-10-26 14:35:18 -04:00
|
|
|
|
|
|
|
|
|
/// type of value being tested
|
|
|
|
|
switch_ty: Ty<'tcx>,
|
|
|
|
|
|
|
|
|
|
/// Possible values. The locations to branch to in each case
|
|
|
|
|
/// are found in the corresponding indices from the `targets` vector.
|
2018-01-16 09:24:38 +01:00
|
|
|
|
values: Cow<'tcx, [u128]>,
|
2015-10-26 14:35:18 -04:00
|
|
|
|
|
2017-02-02 20:35:54 +02:00
|
|
|
|
/// Possible branch sites. The last element of this vector is used
|
2017-03-16 11:19:47 -04:00
|
|
|
|
/// for the otherwise branch, so targets.len() == values.len() + 1
|
2017-02-02 20:35:54 +02:00
|
|
|
|
/// should hold.
|
|
|
|
|
// This invariant is quite non-obvious and also could be improved.
|
|
|
|
|
// One way to make this invariant is to have something like this instead:
|
|
|
|
|
//
|
|
|
|
|
// branches: Vec<(ConstInt, BasicBlock)>,
|
|
|
|
|
// otherwise: Option<BasicBlock> // exhaustive if None
|
|
|
|
|
//
|
|
|
|
|
// However we’ve decided to keep this as-is until we figure a case
|
|
|
|
|
// where some other approach seems to be strictly better than other.
|
2015-10-26 14:35:18 -04:00
|
|
|
|
targets: Vec<BasicBlock>,
|
|
|
|
|
},
|
|
|
|
|
|
2015-12-15 20:46:39 +02:00
|
|
|
|
/// Indicates that the landing pad is finished and unwinding should
|
|
|
|
|
/// continue. Emitted by build::scope::diverge_cleanup.
|
|
|
|
|
Resume,
|
|
|
|
|
|
2017-12-19 01:17:16 +01:00
|
|
|
|
/// Indicates that the landing pad is finished and that the process
|
|
|
|
|
/// should abort. Used to prevent unwinding for foreign items.
|
|
|
|
|
Abort,
|
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// Indicates a normal return. The return place should have
|
|
|
|
|
/// been filled in by now. This should occur at most once.
|
2015-08-18 17:59:21 -04:00
|
|
|
|
Return,
|
|
|
|
|
|
2016-06-08 19:26:19 +03:00
|
|
|
|
/// Indicates a terminator that can never be reached.
|
|
|
|
|
Unreachable,
|
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// Drop the Place
|
2016-01-30 00:18:47 +02:00
|
|
|
|
Drop {
|
2017-12-06 09:25:29 +01:00
|
|
|
|
location: Place<'tcx>,
|
2016-01-30 00:18:47 +02:00
|
|
|
|
target: BasicBlock,
|
|
|
|
|
unwind: Option<BasicBlock>
|
|
|
|
|
},
|
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// Drop the Place and assign the new value over it. This ensures
|
2018-01-29 01:49:29 +02:00
|
|
|
|
/// that the assignment to `P` occurs *even if* the destructor for
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// place unwinds. Its semantics are best explained by by the
|
|
|
|
|
/// elaboration:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// BB0 {
|
2018-01-29 01:49:29 +02:00
|
|
|
|
/// DropAndReplace(P <- V, goto BB1, unwind BB2)
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// becomes
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// BB0 {
|
2018-01-29 01:49:29 +02:00
|
|
|
|
/// Drop(P, goto BB1, unwind BB2)
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// }
|
|
|
|
|
/// BB1 {
|
2018-01-29 01:49:29 +02:00
|
|
|
|
/// // P is now unitialized
|
|
|
|
|
/// P <- V
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// }
|
|
|
|
|
/// BB2 {
|
2018-01-29 01:49:29 +02:00
|
|
|
|
/// // P is now unitialized -- its dtor panicked
|
|
|
|
|
/// P <- V
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2016-05-17 01:06:52 +03:00
|
|
|
|
DropAndReplace {
|
2017-12-06 09:25:29 +01:00
|
|
|
|
location: Place<'tcx>,
|
2016-05-17 01:06:52 +03:00
|
|
|
|
value: Operand<'tcx>,
|
|
|
|
|
target: BasicBlock,
|
|
|
|
|
unwind: Option<BasicBlock>,
|
|
|
|
|
},
|
|
|
|
|
|
2015-12-14 23:27:58 +02:00
|
|
|
|
/// Block ends with a call of a converging function
|
2015-10-07 14:37:42 +02:00
|
|
|
|
Call {
|
2015-12-14 23:27:58 +02:00
|
|
|
|
/// The function that’s being called
|
|
|
|
|
func: Operand<'tcx>,
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// Arguments the function is called with.
|
|
|
|
|
/// These are owned by the callee, which is free to modify them.
|
|
|
|
|
/// This allows the memory occupied by "by-value" arguments to be
|
|
|
|
|
/// reused across function calls without duplicating the contents.
|
2015-12-14 23:27:58 +02:00
|
|
|
|
args: Vec<Operand<'tcx>>,
|
2016-01-29 20:42:02 +02:00
|
|
|
|
/// Destination for the return value. If some, the call is converging.
|
2017-12-06 09:25:29 +01:00
|
|
|
|
destination: Option<(Place<'tcx>, BasicBlock)>,
|
2016-01-29 20:42:02 +02:00
|
|
|
|
/// Cleanups to be done if the call unwinds.
|
|
|
|
|
cleanup: Option<BasicBlock>
|
2015-10-07 14:37:42 +02:00
|
|
|
|
},
|
2016-05-25 08:39:32 +03:00
|
|
|
|
|
|
|
|
|
/// Jump to the target if the condition has the expected value,
|
|
|
|
|
/// otherwise panic with a message and a cleanup target.
|
|
|
|
|
Assert {
|
|
|
|
|
cond: Operand<'tcx>,
|
|
|
|
|
expected: bool,
|
|
|
|
|
msg: AssertMessage<'tcx>,
|
|
|
|
|
target: BasicBlock,
|
|
|
|
|
cleanup: Option<BasicBlock>
|
2016-12-26 14:34:03 +01:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/// A suspend point
|
2017-07-10 21:11:31 +02:00
|
|
|
|
Yield {
|
2016-12-26 14:34:03 +01:00
|
|
|
|
/// The value to return
|
|
|
|
|
value: Operand<'tcx>,
|
|
|
|
|
/// Where to resume to
|
|
|
|
|
resume: BasicBlock,
|
|
|
|
|
/// Cleanup to be done if the generator is dropped at this suspend point
|
|
|
|
|
drop: Option<BasicBlock>,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/// Indicates the end of the dropping of a generator
|
|
|
|
|
GeneratorDrop,
|
2017-12-06 09:25:29 +01:00
|
|
|
|
|
2018-01-25 01:45:45 -05:00
|
|
|
|
/// A block where control flow only ever takes one real path, but borrowck
|
|
|
|
|
/// needs to be more conservative.
|
2017-12-06 09:25:29 +01:00
|
|
|
|
FalseEdges {
|
2018-01-25 01:45:45 -05:00
|
|
|
|
/// The target normal control flow will take
|
2017-12-06 09:25:29 +01:00
|
|
|
|
real_target: BasicBlock,
|
2018-01-25 01:45:45 -05:00
|
|
|
|
/// The list of blocks control flow could conceptually take, but won't
|
|
|
|
|
/// in practice
|
|
|
|
|
imaginary_targets: Vec<BasicBlock>,
|
|
|
|
|
},
|
|
|
|
|
/// A terminator for blocks that only take one path in reality, but where we
|
|
|
|
|
/// reserve the right to unwind in borrowck, even if it won't happen in practice.
|
|
|
|
|
/// This can arise in infinite loops with no function calls for example.
|
|
|
|
|
FalseUnwind {
|
|
|
|
|
/// The target normal control flow will take
|
|
|
|
|
real_target: BasicBlock,
|
|
|
|
|
/// The imaginary cleanup block link. This particular path will never be taken
|
|
|
|
|
/// in practice, but in order to avoid fragility we want to always
|
|
|
|
|
/// consider it in borrowck. We don't want to accept programs which
|
|
|
|
|
/// pass borrowck only when panic=abort or some assertions are disabled
|
|
|
|
|
/// due to release vs. debug mode builds. This needs to be an Option because
|
|
|
|
|
/// of the remove_noop_landing_pads and no_landing_pads passes
|
|
|
|
|
unwind: Option<BasicBlock>,
|
2017-12-06 09:25:29 +01:00
|
|
|
|
},
|
2015-12-14 23:27:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-27 14:02:09 +03:00
|
|
|
|
pub type Successors<'a> =
|
|
|
|
|
iter::Chain<option::IntoIter<&'a BasicBlock>, slice::Iter<'a, BasicBlock>>;
|
|
|
|
|
pub type SuccessorsMut<'a> =
|
|
|
|
|
iter::Chain<option::IntoIter<&'a mut BasicBlock>, slice::IterMut<'a, BasicBlock>>;
|
|
|
|
|
|
2015-10-05 12:31:48 -04:00
|
|
|
|
impl<'tcx> Terminator<'tcx> {
|
2018-04-27 14:02:09 +03:00
|
|
|
|
pub fn successors(&self) -> Successors {
|
2016-03-10 09:55:15 -05:00
|
|
|
|
self.kind.successors()
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-27 14:02:09 +03:00
|
|
|
|
pub fn successors_mut(&mut self) -> SuccessorsMut {
|
2016-03-10 09:55:15 -05:00
|
|
|
|
self.kind.successors_mut()
|
|
|
|
|
}
|
2017-12-06 09:25:29 +01:00
|
|
|
|
|
|
|
|
|
pub fn unwind_mut(&mut self) -> Option<&mut Option<BasicBlock>> {
|
|
|
|
|
self.kind.unwind_mut()
|
|
|
|
|
}
|
2016-03-10 09:55:15 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'tcx> TerminatorKind<'tcx> {
|
2017-09-14 21:13:36 -04:00
|
|
|
|
pub fn if_<'a, 'gcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>, cond: Operand<'tcx>,
|
2017-02-03 03:36:32 +02:00
|
|
|
|
t: BasicBlock, f: BasicBlock) -> TerminatorKind<'tcx> {
|
2018-01-16 09:24:38 +01:00
|
|
|
|
static BOOL_SWITCH_FALSE: &'static [u128] = &[0];
|
2017-02-03 03:36:32 +02:00
|
|
|
|
TerminatorKind::SwitchInt {
|
|
|
|
|
discr: cond,
|
|
|
|
|
switch_ty: tcx.types.bool,
|
|
|
|
|
values: From::from(BOOL_SWITCH_FALSE),
|
|
|
|
|
targets: vec![f, t],
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-27 14:02:09 +03:00
|
|
|
|
pub fn successors(&self) -> Successors {
|
2016-03-10 09:55:15 -05:00
|
|
|
|
use self::TerminatorKind::*;
|
2015-08-18 17:59:21 -04:00
|
|
|
|
match *self {
|
2018-04-27 14:02:09 +03:00
|
|
|
|
Resume | Abort | GeneratorDrop | Return | Unreachable |
|
|
|
|
|
Call { destination: None, cleanup: None, .. } => {
|
|
|
|
|
None.into_iter().chain(&[])
|
|
|
|
|
}
|
|
|
|
|
Goto { target: ref t } |
|
|
|
|
|
Call { destination: None, cleanup: Some(ref t), .. } |
|
|
|
|
|
Call { destination: Some((_, ref t)), cleanup: None, .. } |
|
|
|
|
|
Yield { resume: ref t, drop: None, .. } |
|
|
|
|
|
DropAndReplace { target: ref t, unwind: None, .. } |
|
|
|
|
|
Drop { target: ref t, unwind: None, .. } |
|
|
|
|
|
Assert { target: ref t, cleanup: None, .. } |
|
|
|
|
|
FalseUnwind { real_target: ref t, unwind: None } => {
|
|
|
|
|
Some(t).into_iter().chain(&[])
|
2016-05-17 01:06:52 +03:00
|
|
|
|
}
|
2018-04-27 14:02:09 +03:00
|
|
|
|
Call { destination: Some((_, ref t)), cleanup: Some(ref u), .. } |
|
|
|
|
|
Yield { resume: ref t, drop: Some(ref u), .. } |
|
|
|
|
|
DropAndReplace { target: ref t, unwind: Some(ref u), .. } |
|
|
|
|
|
Drop { target: ref t, unwind: Some(ref u), .. } |
|
|
|
|
|
Assert { target: ref t, cleanup: Some(ref u), .. } |
|
|
|
|
|
FalseUnwind { real_target: ref t, unwind: Some(ref u) } => {
|
|
|
|
|
Some(t).into_iter().chain(slice::from_ref(u))
|
|
|
|
|
}
|
|
|
|
|
SwitchInt { ref targets, .. } => {
|
|
|
|
|
None.into_iter().chain(&targets[..])
|
2016-05-17 01:06:52 +03:00
|
|
|
|
}
|
2017-12-06 09:25:29 +01:00
|
|
|
|
FalseEdges { ref real_target, ref imaginary_targets } => {
|
2018-04-27 14:02:09 +03:00
|
|
|
|
Some(real_target).into_iter().chain(&imaginary_targets[..])
|
2017-12-06 09:25:29 +01:00
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-10 21:38:36 +01:00
|
|
|
|
|
2018-04-27 14:02:09 +03:00
|
|
|
|
pub fn successors_mut(&mut self) -> SuccessorsMut {
|
2016-03-10 09:55:15 -05:00
|
|
|
|
use self::TerminatorKind::*;
|
2015-11-10 21:38:36 +01:00
|
|
|
|
match *self {
|
2018-04-27 14:02:09 +03:00
|
|
|
|
Resume | Abort | GeneratorDrop | Return | Unreachable |
|
|
|
|
|
Call { destination: None, cleanup: None, .. } => {
|
|
|
|
|
None.into_iter().chain(&mut [])
|
|
|
|
|
}
|
|
|
|
|
Goto { target: ref mut t } |
|
|
|
|
|
Call { destination: None, cleanup: Some(ref mut t), .. } |
|
|
|
|
|
Call { destination: Some((_, ref mut t)), cleanup: None, .. } |
|
|
|
|
|
Yield { resume: ref mut t, drop: None, .. } |
|
|
|
|
|
DropAndReplace { target: ref mut t, unwind: None, .. } |
|
|
|
|
|
Drop { target: ref mut t, unwind: None, .. } |
|
|
|
|
|
Assert { target: ref mut t, cleanup: None, .. } |
|
|
|
|
|
FalseUnwind { real_target: ref mut t, unwind: None } => {
|
|
|
|
|
Some(t).into_iter().chain(&mut [])
|
|
|
|
|
}
|
|
|
|
|
Call { destination: Some((_, ref mut t)), cleanup: Some(ref mut u), .. } |
|
|
|
|
|
Yield { resume: ref mut t, drop: Some(ref mut u), .. } |
|
|
|
|
|
DropAndReplace { target: ref mut t, unwind: Some(ref mut u), .. } |
|
|
|
|
|
Drop { target: ref mut t, unwind: Some(ref mut u), .. } |
|
|
|
|
|
Assert { target: ref mut t, cleanup: Some(ref mut u), .. } |
|
|
|
|
|
FalseUnwind { real_target: ref mut t, unwind: Some(ref mut u) } => {
|
2018-05-21 17:59:20 +02:00
|
|
|
|
Some(t).into_iter().chain(slice::from_mut(u))
|
2018-04-27 14:02:09 +03:00
|
|
|
|
}
|
|
|
|
|
SwitchInt { ref mut targets, .. } => {
|
|
|
|
|
None.into_iter().chain(&mut targets[..])
|
2016-05-17 01:06:52 +03:00
|
|
|
|
}
|
2017-12-06 09:25:29 +01:00
|
|
|
|
FalseEdges { ref mut real_target, ref mut imaginary_targets } => {
|
2018-04-27 14:02:09 +03:00
|
|
|
|
Some(real_target).into_iter().chain(&mut imaginary_targets[..])
|
2017-12-06 09:25:29 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn unwind_mut(&mut self) -> Option<&mut Option<BasicBlock>> {
|
|
|
|
|
match *self {
|
|
|
|
|
TerminatorKind::Goto { .. } |
|
|
|
|
|
TerminatorKind::Resume |
|
2017-12-19 01:17:16 +01:00
|
|
|
|
TerminatorKind::Abort |
|
2017-12-06 09:25:29 +01:00
|
|
|
|
TerminatorKind::Return |
|
|
|
|
|
TerminatorKind::Unreachable |
|
|
|
|
|
TerminatorKind::GeneratorDrop |
|
|
|
|
|
TerminatorKind::Yield { .. } |
|
|
|
|
|
TerminatorKind::SwitchInt { .. } |
|
|
|
|
|
TerminatorKind::FalseEdges { .. } => {
|
|
|
|
|
None
|
|
|
|
|
},
|
|
|
|
|
TerminatorKind::Call { cleanup: ref mut unwind, .. } |
|
|
|
|
|
TerminatorKind::Assert { cleanup: ref mut unwind, .. } |
|
|
|
|
|
TerminatorKind::DropAndReplace { ref mut unwind, .. } |
|
2018-01-25 01:45:45 -05:00
|
|
|
|
TerminatorKind::Drop { ref mut unwind, .. } |
|
|
|
|
|
TerminatorKind::FalseUnwind { ref mut unwind, .. } => {
|
2017-12-06 09:25:29 +01:00
|
|
|
|
Some(unwind)
|
|
|
|
|
}
|
2015-11-10 21:38:36 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-05 12:31:48 -04:00
|
|
|
|
impl<'tcx> BasicBlockData<'tcx> {
|
2015-12-19 00:44:32 +02:00
|
|
|
|
pub fn new(terminator: Option<Terminator<'tcx>>) -> BasicBlockData<'tcx> {
|
2015-08-18 17:59:21 -04:00
|
|
|
|
BasicBlockData {
|
|
|
|
|
statements: vec![],
|
2017-07-03 11:19:51 -07:00
|
|
|
|
terminator,
|
2015-12-20 15:30:09 +02:00
|
|
|
|
is_cleanup: false,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-19 00:44:32 +02:00
|
|
|
|
|
|
|
|
|
/// Accessor for terminator.
|
|
|
|
|
///
|
|
|
|
|
/// Terminator may not be None after construction of the basic block is complete. This accessor
|
|
|
|
|
/// provides a convenience way to reach the terminator.
|
|
|
|
|
pub fn terminator(&self) -> &Terminator<'tcx> {
|
|
|
|
|
self.terminator.as_ref().expect("invalid terminator state")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn terminator_mut(&mut self) -> &mut Terminator<'tcx> {
|
|
|
|
|
self.terminator.as_mut().expect("invalid terminator state")
|
|
|
|
|
}
|
2016-12-26 14:34:03 +01:00
|
|
|
|
|
|
|
|
|
pub fn retain_statements<F>(&mut self, mut f: F) where F: FnMut(&mut Statement) -> bool {
|
|
|
|
|
for s in &mut self.statements {
|
|
|
|
|
if !f(s) {
|
2018-02-16 19:20:18 +02:00
|
|
|
|
s.make_nop();
|
2016-12-26 14:34:03 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-12 17:29:37 -03:00
|
|
|
|
|
2018-02-16 19:20:18 +02:00
|
|
|
|
pub fn expand_statements<F, I>(&mut self, mut f: F)
|
|
|
|
|
where F: FnMut(&mut Statement<'tcx>) -> Option<I>,
|
|
|
|
|
I: iter::TrustedLen<Item = Statement<'tcx>>
|
|
|
|
|
{
|
|
|
|
|
// Gather all the iterators we'll need to splice in, and their positions.
|
|
|
|
|
let mut splices: Vec<(usize, I)> = vec![];
|
|
|
|
|
let mut extra_stmts = 0;
|
|
|
|
|
for (i, s) in self.statements.iter_mut().enumerate() {
|
|
|
|
|
if let Some(mut new_stmts) = f(s) {
|
|
|
|
|
if let Some(first) = new_stmts.next() {
|
|
|
|
|
// We can already store the first new statement.
|
|
|
|
|
*s = first;
|
|
|
|
|
|
|
|
|
|
// Save the other statements for optimized splicing.
|
|
|
|
|
let remaining = new_stmts.size_hint().0;
|
|
|
|
|
if remaining > 0 {
|
|
|
|
|
splices.push((i + 1 + extra_stmts, new_stmts));
|
|
|
|
|
extra_stmts += remaining;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
s.make_nop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Splice in the new statements, from the end of the block.
|
|
|
|
|
// FIXME(eddyb) This could be more efficient with a "gap buffer"
|
|
|
|
|
// where a range of elements ("gap") is left uninitialized, with
|
|
|
|
|
// splicing adding new elements to the end of that gap and moving
|
|
|
|
|
// existing elements from before the gap to the end of the gap.
|
|
|
|
|
// For now, this is safe code, emulating a gap but initializing it.
|
|
|
|
|
let mut gap = self.statements.len()..self.statements.len()+extra_stmts;
|
|
|
|
|
self.statements.resize(gap.end, Statement {
|
|
|
|
|
source_info: SourceInfo {
|
|
|
|
|
span: DUMMY_SP,
|
2018-05-28 14:16:09 +03:00
|
|
|
|
scope: OUTERMOST_SOURCE_SCOPE
|
2018-02-16 19:20:18 +02:00
|
|
|
|
},
|
|
|
|
|
kind: StatementKind::Nop
|
|
|
|
|
});
|
|
|
|
|
for (splice_start, new_stmts) in splices.into_iter().rev() {
|
|
|
|
|
let splice_end = splice_start + new_stmts.size_hint().0;
|
|
|
|
|
while gap.end > splice_end {
|
|
|
|
|
gap.start -= 1;
|
|
|
|
|
gap.end -= 1;
|
|
|
|
|
self.statements.swap(gap.start, gap.end);
|
|
|
|
|
}
|
|
|
|
|
self.statements.splice(splice_start..splice_end, new_stmts);
|
|
|
|
|
gap.end = splice_start;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-12 17:29:37 -03:00
|
|
|
|
pub fn visitable(&self, index: usize) -> &dyn MirVisitable<'tcx> {
|
|
|
|
|
if index < self.statements.len() {
|
|
|
|
|
&self.statements[index]
|
|
|
|
|
} else {
|
|
|
|
|
&self.terminator
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-10 09:55:15 -05:00
|
|
|
|
impl<'tcx> Debug for TerminatorKind<'tcx> {
|
2015-12-31 20:11:25 -06:00
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
2016-03-22 22:01:37 -05:00
|
|
|
|
self.fmt_head(fmt)?;
|
2018-04-27 14:02:09 +03:00
|
|
|
|
let successor_count = self.successors().count();
|
2015-12-18 19:29:03 -06:00
|
|
|
|
let labels = self.fmt_successor_labels();
|
2018-04-27 14:02:09 +03:00
|
|
|
|
assert_eq!(successor_count, labels.len());
|
2015-12-18 19:29:03 -06:00
|
|
|
|
|
2018-04-27 14:02:09 +03:00
|
|
|
|
match successor_count {
|
2015-12-18 19:29:03 -06:00
|
|
|
|
0 => Ok(()),
|
|
|
|
|
|
2018-04-27 14:02:09 +03:00
|
|
|
|
1 => write!(fmt, " -> {:?}", self.successors().nth(0).unwrap()),
|
2015-12-18 19:29:03 -06:00
|
|
|
|
|
|
|
|
|
_ => {
|
2016-03-22 22:01:37 -05:00
|
|
|
|
write!(fmt, " -> [")?;
|
2018-04-27 14:02:09 +03:00
|
|
|
|
for (i, target) in self.successors().enumerate() {
|
2015-12-18 19:29:03 -06:00
|
|
|
|
if i > 0 {
|
2016-03-22 22:01:37 -05:00
|
|
|
|
write!(fmt, ", ")?;
|
2015-12-18 19:29:03 -06:00
|
|
|
|
}
|
2016-03-22 22:01:37 -05:00
|
|
|
|
write!(fmt, "{}: {:?}", labels[i], target)?;
|
2015-12-18 19:29:03 -06:00
|
|
|
|
}
|
|
|
|
|
write!(fmt, "]")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-10 09:55:15 -05:00
|
|
|
|
impl<'tcx> TerminatorKind<'tcx> {
|
2015-12-18 21:52:55 -06:00
|
|
|
|
/// Write the "head" part of the terminator; that is, its name and the data it uses to pick the
|
2017-08-11 20:34:14 +02:00
|
|
|
|
/// successor basic block, if any. The only information not included is the list of possible
|
2015-12-18 21:52:55 -06:00
|
|
|
|
/// successors, which may be rendered differently between the text and the graphviz format.
|
2015-12-31 20:11:25 -06:00
|
|
|
|
pub fn fmt_head<W: Write>(&self, fmt: &mut W) -> fmt::Result {
|
2016-03-10 09:55:15 -05:00
|
|
|
|
use self::TerminatorKind::*;
|
2015-08-18 17:59:21 -04:00
|
|
|
|
match *self {
|
2015-12-18 19:29:03 -06:00
|
|
|
|
Goto { .. } => write!(fmt, "goto"),
|
2017-12-06 09:25:29 +01:00
|
|
|
|
SwitchInt { discr: ref place, .. } => write!(fmt, "switchInt({:?})", place),
|
2015-12-18 19:29:03 -06:00
|
|
|
|
Return => write!(fmt, "return"),
|
2016-12-26 14:34:03 +01:00
|
|
|
|
GeneratorDrop => write!(fmt, "generator_drop"),
|
2015-12-15 20:46:39 +02:00
|
|
|
|
Resume => write!(fmt, "resume"),
|
2017-12-19 01:17:16 +01:00
|
|
|
|
Abort => write!(fmt, "abort"),
|
2017-07-10 21:11:31 +02:00
|
|
|
|
Yield { ref value, .. } => write!(fmt, "_1 = suspend({:?})", value),
|
2016-06-08 19:26:19 +03:00
|
|
|
|
Unreachable => write!(fmt, "unreachable"),
|
2016-05-17 01:06:52 +03:00
|
|
|
|
Drop { ref location, .. } => write!(fmt, "drop({:?})", location),
|
|
|
|
|
DropAndReplace { ref location, ref value, .. } =>
|
|
|
|
|
write!(fmt, "replace({:?} <- {:?})", location, value),
|
2016-01-29 20:42:02 +02:00
|
|
|
|
Call { ref func, ref args, ref destination, .. } => {
|
|
|
|
|
if let Some((ref destination, _)) = *destination {
|
2016-03-22 22:01:37 -05:00
|
|
|
|
write!(fmt, "{:?} = ", destination)?;
|
2015-12-22 01:46:56 +02:00
|
|
|
|
}
|
2016-03-22 22:01:37 -05:00
|
|
|
|
write!(fmt, "{:?}(", func)?;
|
2015-12-22 01:46:56 +02:00
|
|
|
|
for (index, arg) in args.iter().enumerate() {
|
|
|
|
|
if index > 0 {
|
2016-03-22 22:01:37 -05:00
|
|
|
|
write!(fmt, ", ")?;
|
2015-12-22 01:46:56 +02:00
|
|
|
|
}
|
2016-03-22 22:01:37 -05:00
|
|
|
|
write!(fmt, "{:?}", arg)?;
|
2015-12-22 01:46:56 +02:00
|
|
|
|
}
|
|
|
|
|
write!(fmt, ")")
|
2015-12-18 19:29:03 -06:00
|
|
|
|
}
|
2016-05-25 08:39:32 +03:00
|
|
|
|
Assert { ref cond, expected, ref msg, .. } => {
|
|
|
|
|
write!(fmt, "assert(")?;
|
|
|
|
|
if !expected {
|
|
|
|
|
write!(fmt, "!")?;
|
|
|
|
|
}
|
2018-05-01 12:26:58 +02:00
|
|
|
|
write!(fmt, "{:?}, \"{:?}\")", cond, msg)
|
2017-12-06 09:25:29 +01:00
|
|
|
|
},
|
2018-01-25 01:45:45 -05:00
|
|
|
|
FalseEdges { .. } => write!(fmt, "falseEdges"),
|
|
|
|
|
FalseUnwind { .. } => write!(fmt, "falseUnwind"),
|
2015-12-18 19:29:03 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-18 21:52:55 -06:00
|
|
|
|
/// Return the list of labels for the edges to the successor basic blocks.
|
2015-12-18 19:29:03 -06:00
|
|
|
|
pub fn fmt_successor_labels(&self) -> Vec<Cow<'static, str>> {
|
2016-03-10 09:55:15 -05:00
|
|
|
|
use self::TerminatorKind::*;
|
2015-12-18 19:29:03 -06:00
|
|
|
|
match *self {
|
2017-12-19 01:17:16 +01:00
|
|
|
|
Return | Resume | Abort | Unreachable | GeneratorDrop => vec![],
|
2016-01-29 19:06:23 +02:00
|
|
|
|
Goto { .. } => vec!["".into()],
|
2018-01-16 09:24:38 +01:00
|
|
|
|
SwitchInt { ref values, switch_ty, .. } => {
|
2018-05-22 21:08:33 +02:00
|
|
|
|
let size = ty::tls::with(|tcx| {
|
|
|
|
|
let param_env = ty::ParamEnv::empty();
|
2018-05-24 11:30:24 +02:00
|
|
|
|
let switch_ty = tcx.lift_to_global(&switch_ty).unwrap();
|
2018-05-24 10:11:24 +02:00
|
|
|
|
tcx.layout_of(param_env.and(switch_ty)).unwrap().size
|
2018-05-22 21:08:33 +02:00
|
|
|
|
});
|
2015-12-18 19:29:03 -06:00
|
|
|
|
values.iter()
|
2018-01-16 09:24:38 +01:00
|
|
|
|
.map(|&u| {
|
|
|
|
|
let mut s = String::new();
|
|
|
|
|
print_miri_value(
|
2018-05-24 10:11:24 +02:00
|
|
|
|
Value::Scalar(Scalar::Bits { bits: u, defined: size.bits() as u8 }),
|
2018-01-16 09:24:38 +01:00
|
|
|
|
switch_ty,
|
|
|
|
|
&mut s,
|
|
|
|
|
).unwrap();
|
|
|
|
|
s.into()
|
2015-12-18 19:29:03 -06:00
|
|
|
|
})
|
2016-01-29 19:06:23 +02:00
|
|
|
|
.chain(iter::once(String::from("otherwise").into()))
|
2015-12-18 19:29:03 -06:00
|
|
|
|
.collect()
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
2016-01-29 20:42:02 +02:00
|
|
|
|
Call { destination: Some(_), cleanup: Some(_), .. } =>
|
|
|
|
|
vec!["return".into_cow(), "unwind".into_cow()],
|
|
|
|
|
Call { destination: Some(_), cleanup: None, .. } => vec!["return".into_cow()],
|
|
|
|
|
Call { destination: None, cleanup: Some(_), .. } => vec!["unwind".into_cow()],
|
|
|
|
|
Call { destination: None, cleanup: None, .. } => vec![],
|
2017-07-10 21:11:31 +02:00
|
|
|
|
Yield { drop: Some(_), .. } =>
|
2016-12-26 14:34:03 +01:00
|
|
|
|
vec!["resume".into_cow(), "drop".into_cow()],
|
2017-07-10 21:11:31 +02:00
|
|
|
|
Yield { drop: None, .. } => vec!["resume".into_cow()],
|
2016-05-17 01:06:52 +03:00
|
|
|
|
DropAndReplace { unwind: None, .. } |
|
2016-01-30 00:18:47 +02:00
|
|
|
|
Drop { unwind: None, .. } => vec!["return".into_cow()],
|
2016-05-17 01:06:52 +03:00
|
|
|
|
DropAndReplace { unwind: Some(_), .. } |
|
|
|
|
|
Drop { unwind: Some(_), .. } => {
|
|
|
|
|
vec!["return".into_cow(), "unwind".into_cow()]
|
|
|
|
|
}
|
2016-05-25 08:39:32 +03:00
|
|
|
|
Assert { cleanup: None, .. } => vec!["".into()],
|
|
|
|
|
Assert { .. } =>
|
2017-12-06 09:25:29 +01:00
|
|
|
|
vec!["success".into_cow(), "unwind".into_cow()],
|
|
|
|
|
FalseEdges { ref imaginary_targets, .. } => {
|
|
|
|
|
let mut l = vec!["real".into()];
|
|
|
|
|
l.resize(imaginary_targets.len() + 1, "imaginary".into());
|
|
|
|
|
l
|
|
|
|
|
}
|
2018-01-25 01:45:45 -05:00
|
|
|
|
FalseUnwind { unwind: Some(_), .. } => vec!["real".into(), "cleanup".into()],
|
|
|
|
|
FalseUnwind { unwind: None, .. } => vec!["real".into()],
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Statements
|
|
|
|
|
|
2016-01-26 08:36:34 -05:00
|
|
|
|
#[derive(Clone, RustcEncodable, RustcDecodable)]
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub struct Statement<'tcx> {
|
2016-06-07 19:21:56 +03:00
|
|
|
|
pub source_info: SourceInfo,
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub kind: StatementKind<'tcx>,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-15 18:17:58 -07:00
|
|
|
|
impl<'tcx> Statement<'tcx> {
|
|
|
|
|
/// Changes a statement to a nop. This is both faster than deleting instructions and avoids
|
|
|
|
|
/// invalidating statement indices in `Location`s.
|
|
|
|
|
pub fn make_nop(&mut self) {
|
|
|
|
|
self.kind = StatementKind::Nop
|
|
|
|
|
}
|
2018-02-16 19:20:18 +02:00
|
|
|
|
|
|
|
|
|
/// Changes a statement to a nop and returns the original statement.
|
|
|
|
|
pub fn replace_nop(&mut self) -> Self {
|
|
|
|
|
Statement {
|
|
|
|
|
source_info: self.source_info,
|
|
|
|
|
kind: mem::replace(&mut self.kind, StatementKind::Nop)
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-15 18:17:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-26 08:36:34 -05:00
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub enum StatementKind<'tcx> {
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// Write the RHS Rvalue to the LHS Place.
|
|
|
|
|
Assign(Place<'tcx>, Rvalue<'tcx>),
|
2016-08-14 06:34:14 +03:00
|
|
|
|
|
2018-05-04 12:04:33 +02:00
|
|
|
|
/// This represents all the reading that a pattern match may do
|
|
|
|
|
/// (e.g. inspecting constants and discriminant values).
|
|
|
|
|
ReadForMatch(Place<'tcx>),
|
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// Write the discriminant for a variant to the enum Place.
|
|
|
|
|
SetDiscriminant { place: Place<'tcx>, variant_index: usize },
|
2016-08-14 06:34:14 +03:00
|
|
|
|
|
|
|
|
|
/// Start a live range for the storage of the local.
|
2017-09-04 08:01:46 +03:00
|
|
|
|
StorageLive(Local),
|
2016-08-14 06:34:14 +03:00
|
|
|
|
|
|
|
|
|
/// End the current live range for the storage of the local.
|
2017-09-04 08:01:46 +03:00
|
|
|
|
StorageDead(Local),
|
2016-09-15 18:17:58 -07:00
|
|
|
|
|
2017-07-11 14:01:07 -07:00
|
|
|
|
/// Execute a piece of inline Assembly.
|
2017-02-15 21:21:36 +02:00
|
|
|
|
InlineAsm {
|
2017-05-12 01:38:26 +03:00
|
|
|
|
asm: Box<InlineAsm>,
|
2017-12-06 09:25:29 +01:00
|
|
|
|
outputs: Vec<Place<'tcx>>,
|
2017-02-15 21:21:36 +02:00
|
|
|
|
inputs: Vec<Operand<'tcx>>
|
|
|
|
|
},
|
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// Assert the given places to be valid inhabitants of their type. These statements are
|
2017-07-31 13:20:37 -07:00
|
|
|
|
/// currently only interpreted by miri and only generated when "-Z mir-emit-validate" is passed.
|
|
|
|
|
/// See <https://internals.rust-lang.org/t/types-as-contracts/5562/73> for more details.
|
2017-12-06 09:25:29 +01:00
|
|
|
|
Validate(ValidationOp, Vec<ValidationOperand<'tcx, Place<'tcx>>>),
|
2017-07-11 14:01:07 -07:00
|
|
|
|
|
2017-08-31 21:37:38 +03:00
|
|
|
|
/// Mark one terminating point of a region scope (i.e. static region).
|
Add `EndRegion` statement kind to MIR.
* Emit `EndRegion` for every code-extent for which we observe a
borrow. To do this, we needed to thread source info back through
to `fn in_scope`, which makes this commit a bit more painful than
one might have expected.
* There is `end_region` emission in `Builder::pop_scope` and in
`Builder::exit_scope`; the first handles falling out of a scope
normally, the second handles e.g. `break`.
* Remove `EndRegion` statements during the erase_regions mir
transformation.
* Preallocate the terminator block, and throw an `Unreachable` marker
on it from the outset. Then overwrite that Terminator as necessary
on demand.
* Instead of marking the scope as needs_cleanup after seeing a
borrow, just treat every scope in the chain as being part of the
diverge_block (after any *one* of them has separately signalled
that it needs cleanup, e.g. due to having a destructor to run).
* Allow for resume terminators to be patched when looking up drop flags.
(In particular, `MirPatch::new` has an explicit code path,
presumably previously unreachable, that patches up such resume
terminators.)
* Make `Scope` implement `Debug` trait.
* Expanded a stray comment: we do not emit StorageDead on diverging
paths, but that end behavior might not be desirable.
2017-02-17 13:38:42 +01:00
|
|
|
|
/// (The starting point(s) arise implicitly from borrows.)
|
2017-08-31 21:37:38 +03:00
|
|
|
|
EndRegion(region::Scope),
|
Add `EndRegion` statement kind to MIR.
* Emit `EndRegion` for every code-extent for which we observe a
borrow. To do this, we needed to thread source info back through
to `fn in_scope`, which makes this commit a bit more painful than
one might have expected.
* There is `end_region` emission in `Builder::pop_scope` and in
`Builder::exit_scope`; the first handles falling out of a scope
normally, the second handles e.g. `break`.
* Remove `EndRegion` statements during the erase_regions mir
transformation.
* Preallocate the terminator block, and throw an `Unreachable` marker
on it from the outset. Then overwrite that Terminator as necessary
on demand.
* Instead of marking the scope as needs_cleanup after seeing a
borrow, just treat every scope in the chain as being part of the
diverge_block (after any *one* of them has separately signalled
that it needs cleanup, e.g. due to having a destructor to run).
* Allow for resume terminators to be patched when looking up drop flags.
(In particular, `MirPatch::new` has an explicit code path,
presumably previously unreachable, that patches up such resume
terminators.)
* Make `Scope` implement `Debug` trait.
* Expanded a stray comment: we do not emit StorageDead on diverging
paths, but that end behavior might not be desirable.
2017-02-17 13:38:42 +01:00
|
|
|
|
|
2018-02-23 20:52:05 +00:00
|
|
|
|
/// Encodes a user's type assertion. These need to be preserved intact so that NLL can respect
|
|
|
|
|
/// them. For example:
|
|
|
|
|
///
|
|
|
|
|
/// let (a, b): (T, U) = y;
|
|
|
|
|
///
|
|
|
|
|
/// Here we would insert a `UserAssertTy<(T, U)>(y)` instruction to check that the type of `y`
|
|
|
|
|
/// is the right thing.
|
2018-03-23 17:48:03 +00:00
|
|
|
|
///
|
|
|
|
|
/// `CanonicalTy` is used to capture "inference variables" from the user's types. For example:
|
|
|
|
|
///
|
|
|
|
|
/// let x: Vec<_> = ...;
|
|
|
|
|
/// let y: &u32 = ...;
|
|
|
|
|
///
|
|
|
|
|
/// would result in `Vec<?0>` and `&'?0 u32` respectively (where `?0` is a canonicalized
|
|
|
|
|
/// variable).
|
2018-03-15 18:49:10 +00:00
|
|
|
|
UserAssertTy(CanonicalTy<'tcx>, Local),
|
2018-02-23 20:52:05 +00:00
|
|
|
|
|
2016-09-15 18:17:58 -07:00
|
|
|
|
/// No-op. Useful for deleting instructions without affecting statement indices.
|
|
|
|
|
Nop,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-31 13:20:37 -07:00
|
|
|
|
/// The `ValidationOp` describes what happens with each of the operands of a
|
|
|
|
|
/// `Validate` statement.
|
2017-07-20 15:27:26 -07:00
|
|
|
|
#[derive(Copy, Clone, RustcEncodable, RustcDecodable, PartialEq, Eq)]
|
2017-07-11 14:01:07 -07:00
|
|
|
|
pub enum ValidationOp {
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// Recursively traverse the place following the type and validate that all type
|
2017-07-31 13:20:37 -07:00
|
|
|
|
/// invariants are maintained. Furthermore, acquire exclusive/read-only access to the
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// memory reachable from the place.
|
2017-07-11 14:01:07 -07:00
|
|
|
|
Acquire,
|
2017-07-31 13:20:37 -07:00
|
|
|
|
/// Recursive traverse the *mutable* part of the type and relinquish all exclusive
|
|
|
|
|
/// access.
|
2017-07-11 14:01:07 -07:00
|
|
|
|
Release,
|
2017-07-31 13:20:37 -07:00
|
|
|
|
/// Recursive traverse the *mutable* part of the type and relinquish all exclusive
|
|
|
|
|
/// access *until* the given region ends. Then, access will be recovered.
|
2017-08-31 21:37:38 +03:00
|
|
|
|
Suspend(region::Scope),
|
2017-07-11 14:01:07 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-20 15:27:26 -07:00
|
|
|
|
impl Debug for ValidationOp {
|
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
|
|
|
|
use self::ValidationOp::*;
|
|
|
|
|
match *self {
|
|
|
|
|
Acquire => write!(fmt, "Acquire"),
|
|
|
|
|
Release => write!(fmt, "Release"),
|
|
|
|
|
// (reuse lifetime rendering policy from ppaux.)
|
|
|
|
|
Suspend(ref ce) => write!(fmt, "Suspend({})", ty::ReScope(*ce)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-21 14:49:01 -07:00
|
|
|
|
// This is generic so that it can be reused by miri
|
2017-07-21 12:43:09 -07:00
|
|
|
|
#[derive(Clone, RustcEncodable, RustcDecodable)]
|
2017-07-21 14:49:01 -07:00
|
|
|
|
pub struct ValidationOperand<'tcx, T> {
|
2017-12-06 09:25:29 +01:00
|
|
|
|
pub place: T,
|
2017-07-21 12:43:09 -07:00
|
|
|
|
pub ty: Ty<'tcx>,
|
2017-08-31 21:37:38 +03:00
|
|
|
|
pub re: Option<region::Scope>,
|
2017-07-21 12:43:09 -07:00
|
|
|
|
pub mutbl: hir::Mutability,
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-21 14:49:01 -07:00
|
|
|
|
impl<'tcx, T: Debug> Debug for ValidationOperand<'tcx, T> {
|
2017-07-21 12:43:09 -07:00
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
2017-12-06 09:25:29 +01:00
|
|
|
|
write!(fmt, "{:?}: {:?}", self.place, self.ty)?;
|
2017-07-21 12:43:09 -07:00
|
|
|
|
if let Some(ce) = self.re {
|
|
|
|
|
// (reuse lifetime rendering policy from ppaux.)
|
|
|
|
|
write!(fmt, "/{}", ty::ReScope(ce))?;
|
|
|
|
|
}
|
|
|
|
|
if let hir::MutImmutable = self.mutbl {
|
|
|
|
|
write!(fmt, " (imm)")?;
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-05 12:31:48 -04:00
|
|
|
|
impl<'tcx> Debug for Statement<'tcx> {
|
2015-12-31 20:11:25 -06:00
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
2015-08-18 17:59:21 -04:00
|
|
|
|
use self::StatementKind::*;
|
|
|
|
|
match self.kind {
|
2017-12-06 09:25:29 +01:00
|
|
|
|
Assign(ref place, ref rv) => write!(fmt, "{:?} = {:?}", place, rv),
|
2018-05-04 12:04:33 +02:00
|
|
|
|
ReadForMatch(ref place) => write!(fmt, "ReadForMatch({:?})", place),
|
Add `EndRegion` statement kind to MIR.
* Emit `EndRegion` for every code-extent for which we observe a
borrow. To do this, we needed to thread source info back through
to `fn in_scope`, which makes this commit a bit more painful than
one might have expected.
* There is `end_region` emission in `Builder::pop_scope` and in
`Builder::exit_scope`; the first handles falling out of a scope
normally, the second handles e.g. `break`.
* Remove `EndRegion` statements during the erase_regions mir
transformation.
* Preallocate the terminator block, and throw an `Unreachable` marker
on it from the outset. Then overwrite that Terminator as necessary
on demand.
* Instead of marking the scope as needs_cleanup after seeing a
borrow, just treat every scope in the chain as being part of the
diverge_block (after any *one* of them has separately signalled
that it needs cleanup, e.g. due to having a destructor to run).
* Allow for resume terminators to be patched when looking up drop flags.
(In particular, `MirPatch::new` has an explicit code path,
presumably previously unreachable, that patches up such resume
terminators.)
* Make `Scope` implement `Debug` trait.
* Expanded a stray comment: we do not emit StorageDead on diverging
paths, but that end behavior might not be desirable.
2017-02-17 13:38:42 +01:00
|
|
|
|
// (reuse lifetime rendering policy from ppaux.)
|
|
|
|
|
EndRegion(ref ce) => write!(fmt, "EndRegion({})", ty::ReScope(*ce)),
|
2017-12-06 09:25:29 +01:00
|
|
|
|
Validate(ref op, ref places) => write!(fmt, "Validate({:?}, {:?})", op, places),
|
|
|
|
|
StorageLive(ref place) => write!(fmt, "StorageLive({:?})", place),
|
|
|
|
|
StorageDead(ref place) => write!(fmt, "StorageDead({:?})", place),
|
|
|
|
|
SetDiscriminant { ref place, variant_index } => {
|
|
|
|
|
write!(fmt, "discriminant({:?}) = {:?}", place, variant_index)
|
2017-02-15 21:21:36 +02:00
|
|
|
|
},
|
|
|
|
|
InlineAsm { ref asm, ref outputs, ref inputs } => {
|
|
|
|
|
write!(fmt, "asm!({:?} : {:?} : {:?})", asm, outputs, inputs)
|
|
|
|
|
},
|
2018-03-15 18:49:10 +00:00
|
|
|
|
UserAssertTy(ref c_ty, ref local) => write!(fmt, "UserAssertTy({:?}, {:?})",
|
|
|
|
|
c_ty, local),
|
2016-09-15 18:17:58 -07:00
|
|
|
|
Nop => write!(fmt, "nop"),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-09 13:36:04 -05:00
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2017-12-06 09:25:29 +01:00
|
|
|
|
// Places
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
|
|
|
|
/// A path to a value; something that can be evaluated without
|
|
|
|
|
/// changing or disturbing program state.
|
2017-12-01 12:13:01 +01:00
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
|
2017-12-06 09:25:29 +01:00
|
|
|
|
pub enum Place<'tcx> {
|
2016-09-25 01:38:27 +02:00
|
|
|
|
/// local variable
|
|
|
|
|
Local(Local),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
|
|
|
|
/// static or static mut variable
|
2017-03-01 02:29:57 +02:00
|
|
|
|
Static(Box<Static<'tcx>>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// projection out of a place (access a field, deref a pointer, etc)
|
|
|
|
|
Projection(Box<PlaceProjection<'tcx>>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-01 02:29:57 +02:00
|
|
|
|
/// The def-id of a static, along with its normalized type (which is
|
|
|
|
|
/// stored to avoid requiring normalization when reading MIR).
|
2017-12-01 12:13:01 +01:00
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
|
2017-03-01 02:29:57 +02:00
|
|
|
|
pub struct Static<'tcx> {
|
|
|
|
|
pub def_id: DefId,
|
|
|
|
|
pub ty: Ty<'tcx>,
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-30 15:27:27 +02:00
|
|
|
|
impl_stable_hash_for!(struct Static<'tcx> {
|
|
|
|
|
def_id,
|
|
|
|
|
ty
|
|
|
|
|
});
|
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
|
/// The `Projection` data structure defines things of the form `B.x`
|
|
|
|
|
/// or `*B` or `B[index]`. Note that it is parameterized because it is
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// shared between `Constant` and `Place`. See the aliases
|
|
|
|
|
/// `PlaceProjection` etc below.
|
2016-01-25 14:34:34 +01:00
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
|
2017-07-27 23:12:08 +03:00
|
|
|
|
pub struct Projection<'tcx, B, V, T> {
|
2015-08-18 17:59:21 -04:00
|
|
|
|
pub base: B,
|
2017-07-27 23:12:08 +03:00
|
|
|
|
pub elem: ProjectionElem<'tcx, V, T>,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-25 14:34:34 +01:00
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
|
2017-07-27 23:12:08 +03:00
|
|
|
|
pub enum ProjectionElem<'tcx, V, T> {
|
2015-08-18 17:59:21 -04:00
|
|
|
|
Deref,
|
2017-07-27 23:12:08 +03:00
|
|
|
|
Field(Field, T),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
Index(V),
|
|
|
|
|
|
2016-02-03 13:25:07 +01:00
|
|
|
|
/// These indices are generated by slice patterns. Easiest to explain
|
|
|
|
|
/// by example:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// [X, _, .._, _, _] => { offset: 0, min_length: 4, from_end: false },
|
|
|
|
|
/// [_, X, .._, _, _] => { offset: 1, min_length: 4, from_end: false },
|
|
|
|
|
/// [_, _, .._, X, _] => { offset: 2, min_length: 4, from_end: true },
|
|
|
|
|
/// [_, _, .._, _, X] => { offset: 1, min_length: 4, from_end: true },
|
|
|
|
|
/// ```
|
2015-08-18 17:59:21 -04:00
|
|
|
|
ConstantIndex {
|
2016-02-03 13:25:07 +01:00
|
|
|
|
/// index or -index (in Python terms), depending on from_end
|
|
|
|
|
offset: u32,
|
|
|
|
|
/// thing being indexed must be at least this long
|
|
|
|
|
min_length: u32,
|
|
|
|
|
/// counting backwards from end?
|
|
|
|
|
from_end: bool,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
},
|
|
|
|
|
|
2016-03-11 12:54:59 +02:00
|
|
|
|
/// These indices are generated by slice patterns.
|
|
|
|
|
///
|
|
|
|
|
/// slice[from:-to] in Python terms.
|
|
|
|
|
Subslice {
|
|
|
|
|
from: u32,
|
|
|
|
|
to: u32,
|
|
|
|
|
},
|
|
|
|
|
|
2016-02-03 13:25:07 +01:00
|
|
|
|
/// "Downcast" to a variant of an ADT. Currently, we only introduce
|
|
|
|
|
/// this for ADTs with more than one variant. It may be better to
|
|
|
|
|
/// just introduce it always, or always for enums.
|
2016-11-25 01:33:29 +02:00
|
|
|
|
Downcast(&'tcx AdtDef, usize),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// Alias for projections as they appear in places, where the base is a place
|
2017-09-03 21:55:41 +03:00
|
|
|
|
/// and the index is a local.
|
2017-12-06 09:25:29 +01:00
|
|
|
|
pub type PlaceProjection<'tcx> = Projection<'tcx, Place<'tcx>, Local, Ty<'tcx>>;
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// Alias for projections as they appear in places, where the base is a place
|
2017-09-03 21:55:41 +03:00
|
|
|
|
/// and the index is a local.
|
2017-12-06 09:25:29 +01:00
|
|
|
|
pub type PlaceElem<'tcx> = ProjectionElem<'tcx, Local, Ty<'tcx>>;
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
newtype_index!(Field { DEBUG_FORMAT = "field[{}]" });
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
impl<'tcx> Place<'tcx> {
|
|
|
|
|
pub fn field(self, f: Field, ty: Ty<'tcx>) -> Place<'tcx> {
|
2016-02-11 18:31:42 +02:00
|
|
|
|
self.elem(ProjectionElem::Field(f, ty))
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
pub fn deref(self) -> Place<'tcx> {
|
2015-08-18 17:59:21 -04:00
|
|
|
|
self.elem(ProjectionElem::Deref)
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
pub fn downcast(self, adt_def: &'tcx AdtDef, variant_index: usize) -> Place<'tcx> {
|
2016-12-01 01:12:03 +08:00
|
|
|
|
self.elem(ProjectionElem::Downcast(adt_def, variant_index))
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
pub fn index(self, index: Local) -> Place<'tcx> {
|
2015-08-18 17:59:21 -04:00
|
|
|
|
self.elem(ProjectionElem::Index(index))
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
pub fn elem(self, elem: PlaceElem<'tcx>) -> Place<'tcx> {
|
|
|
|
|
Place::Projection(Box::new(PlaceProjection {
|
2015-10-07 14:37:42 +02:00
|
|
|
|
base: self,
|
2017-07-03 11:19:51 -07:00
|
|
|
|
elem,
|
2015-10-07 14:37:42 +02:00
|
|
|
|
}))
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
impl<'tcx> Debug for Place<'tcx> {
|
2015-12-31 20:11:25 -06:00
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
2017-12-06 09:25:29 +01:00
|
|
|
|
use self::Place::*;
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
|
|
|
|
match *self {
|
2016-09-25 01:38:27 +02:00
|
|
|
|
Local(id) => write!(fmt, "{:?}", id),
|
2017-03-01 02:29:57 +02:00
|
|
|
|
Static(box self::Static { def_id, ty }) =>
|
|
|
|
|
write!(fmt, "({}: {:?})", ty::tls::with(|tcx| tcx.item_path_str(def_id)), ty),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
Projection(ref data) =>
|
|
|
|
|
match data.elem {
|
2015-12-18 19:29:03 -06:00
|
|
|
|
ProjectionElem::Downcast(ref adt_def, index) =>
|
2016-01-01 21:01:17 -06:00
|
|
|
|
write!(fmt, "({:?} as {})", data.base, adt_def.variants[index].name),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
ProjectionElem::Deref =>
|
2016-01-01 21:01:17 -06:00
|
|
|
|
write!(fmt, "(*{:?})", data.base),
|
2016-02-11 18:31:42 +02:00
|
|
|
|
ProjectionElem::Field(field, ty) =>
|
|
|
|
|
write!(fmt, "({:?}.{:?}: {:?})", data.base, field.index(), ty),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
ProjectionElem::Index(ref index) =>
|
2016-01-01 21:01:17 -06:00
|
|
|
|
write!(fmt, "{:?}[{:?}]", data.base, index),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
ProjectionElem::ConstantIndex { offset, min_length, from_end: false } =>
|
2016-01-01 21:01:17 -06:00
|
|
|
|
write!(fmt, "{:?}[{:?} of {:?}]", data.base, offset, min_length),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
ProjectionElem::ConstantIndex { offset, min_length, from_end: true } =>
|
2016-01-01 21:01:17 -06:00
|
|
|
|
write!(fmt, "{:?}[-{:?} of {:?}]", data.base, offset, min_length),
|
2016-03-11 12:54:59 +02:00
|
|
|
|
ProjectionElem::Subslice { from, to } if to == 0 =>
|
2016-11-10 17:54:31 +00:00
|
|
|
|
write!(fmt, "{:?}[{:?}:]", data.base, from),
|
2016-03-11 12:54:59 +02:00
|
|
|
|
ProjectionElem::Subslice { from, to } if from == 0 =>
|
|
|
|
|
write!(fmt, "{:?}[:-{:?}]", data.base, to),
|
|
|
|
|
ProjectionElem::Subslice { from, to } =>
|
|
|
|
|
write!(fmt, "{:?}[{:?}:-{:?}]", data.base,
|
|
|
|
|
from, to),
|
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-09 11:04:26 -05:00
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Scopes
|
|
|
|
|
|
2018-05-28 14:16:09 +03:00
|
|
|
|
newtype_index!(SourceScope
|
2017-12-06 09:25:29 +01:00
|
|
|
|
{
|
|
|
|
|
DEBUG_FORMAT = "scope[{}]",
|
2018-05-28 14:16:09 +03:00
|
|
|
|
const OUTERMOST_SOURCE_SCOPE = 0,
|
2017-12-06 09:25:29 +01:00
|
|
|
|
});
|
2016-03-09 11:04:26 -05:00
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
|
2018-05-28 14:16:09 +03:00
|
|
|
|
pub struct SourceScopeData {
|
2016-04-06 17:17:12 +03:00
|
|
|
|
pub span: Span,
|
2018-05-28 14:16:09 +03:00
|
|
|
|
pub parent_scope: Option<SourceScope>,
|
2016-03-09 11:04:26 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-28 17:37:48 +03:00
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
|
|
|
|
|
pub struct SourceScopeLocalData {
|
|
|
|
|
/// A NodeId with lint levels equivalent to this scope's lint levels.
|
|
|
|
|
pub lint_root: ast::NodeId,
|
|
|
|
|
/// The unsafe block that contains this node.
|
|
|
|
|
pub safety: Safety,
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Operands
|
2016-03-09 11:04:26 -05:00
|
|
|
|
|
2016-02-03 13:25:07 +01:00
|
|
|
|
/// These are values that can appear inside an rvalue (or an index
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// place). They are intentionally limited to prevent rvalues from
|
2016-02-03 13:25:07 +01:00
|
|
|
|
/// being nested in one another.
|
2015-12-08 15:53:19 -05:00
|
|
|
|
#[derive(Clone, PartialEq, RustcEncodable, RustcDecodable)]
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub enum Operand<'tcx> {
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// Copy: The value must be available for use afterwards.
|
|
|
|
|
///
|
|
|
|
|
/// This implies that the type of the place must be `Copy`; this is true
|
|
|
|
|
/// by construction during build, but also checked by the MIR type checker.
|
|
|
|
|
Copy(Place<'tcx>),
|
|
|
|
|
/// Move: The value (including old borrows of it) will not be used again.
|
|
|
|
|
///
|
|
|
|
|
/// Safe for values of all types (modulo future developments towards `?Move`).
|
|
|
|
|
/// Correct usage patterns are enforced by the borrow checker for safe code.
|
|
|
|
|
/// `Copy` may be converted to `Move` to enable "last-use" optimizations.
|
|
|
|
|
Move(Place<'tcx>),
|
2017-05-12 01:38:26 +03:00
|
|
|
|
Constant(Box<Constant<'tcx>>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-05 12:31:48 -04:00
|
|
|
|
impl<'tcx> Debug for Operand<'tcx> {
|
2015-12-31 20:11:25 -06:00
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
2015-08-18 17:59:21 -04:00
|
|
|
|
use self::Operand::*;
|
|
|
|
|
match *self {
|
|
|
|
|
Constant(ref a) => write!(fmt, "{:?}", a),
|
2017-12-06 09:25:29 +01:00
|
|
|
|
Copy(ref place) => write!(fmt, "{:?}", place),
|
|
|
|
|
Move(ref place) => write!(fmt, "move {:?}", place),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-14 01:08:21 +02:00
|
|
|
|
impl<'tcx> Operand<'tcx> {
|
2017-03-07 16:09:01 +01:00
|
|
|
|
pub fn function_handle<'a>(
|
2017-09-14 21:13:36 -04:00
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-03-07 16:09:01 +01:00
|
|
|
|
def_id: DefId,
|
|
|
|
|
substs: &'tcx Substs<'tcx>,
|
|
|
|
|
span: Span,
|
|
|
|
|
) -> Self {
|
2017-08-04 11:25:13 +03:00
|
|
|
|
let ty = tcx.type_of(def_id).subst(tcx, substs);
|
2017-05-12 01:38:26 +03:00
|
|
|
|
Operand::Constant(box Constant {
|
2017-07-03 11:19:51 -07:00
|
|
|
|
span,
|
2017-08-04 11:25:13 +03:00
|
|
|
|
ty,
|
2017-08-04 00:41:44 +03:00
|
|
|
|
literal: Literal::Value {
|
2018-04-26 09:18:19 +02:00
|
|
|
|
value: ty::Const::zero_sized(tcx, ty),
|
2017-08-04 00:41:44 +03:00
|
|
|
|
},
|
2017-03-14 01:08:21 +02:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 17:46:23 +02:00
|
|
|
|
pub fn to_copy(&self) -> Self {
|
|
|
|
|
match *self {
|
|
|
|
|
Operand::Copy(_) | Operand::Constant(_) => self.clone(),
|
|
|
|
|
Operand::Move(ref place) => Operand::Copy(place.clone())
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-14 01:08:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2016-02-03 13:25:07 +01:00
|
|
|
|
/// Rvalues
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2015-12-08 15:53:19 -05:00
|
|
|
|
#[derive(Clone, RustcEncodable, RustcDecodable)]
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub enum Rvalue<'tcx> {
|
2016-02-03 13:25:07 +01:00
|
|
|
|
/// x (either a move or copy, depending on type of x)
|
2015-10-05 12:31:48 -04:00
|
|
|
|
Use(Operand<'tcx>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2016-02-03 13:25:07 +01:00
|
|
|
|
/// [x; 32]
|
2018-01-25 16:44:45 +01:00
|
|
|
|
Repeat(Operand<'tcx>, u64),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2016-02-03 13:25:07 +01:00
|
|
|
|
/// &x or &mut x
|
2017-12-06 09:25:29 +01:00
|
|
|
|
Ref(Region<'tcx>, BorrowKind, Place<'tcx>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2016-02-03 13:25:07 +01:00
|
|
|
|
/// length of a [X] or [X;n] value
|
2017-12-06 09:25:29 +01:00
|
|
|
|
Len(Place<'tcx>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2015-10-05 12:31:48 -04:00
|
|
|
|
Cast(CastKind, Operand<'tcx>, Ty<'tcx>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2015-10-05 12:31:48 -04:00
|
|
|
|
BinaryOp(BinOp, Operand<'tcx>, Operand<'tcx>),
|
2016-03-31 18:50:07 +13:00
|
|
|
|
CheckedBinaryOp(BinOp, Operand<'tcx>, Operand<'tcx>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2017-05-18 18:43:52 +03:00
|
|
|
|
NullaryOp(NullOp, Ty<'tcx>),
|
2015-10-05 12:31:48 -04:00
|
|
|
|
UnaryOp(UnOp, Operand<'tcx>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2017-01-31 01:10:54 +02:00
|
|
|
|
/// Read the discriminant of an ADT.
|
|
|
|
|
///
|
|
|
|
|
/// Undefined (i.e. no effort is made to make it defined, but there’s no reason why it cannot
|
|
|
|
|
/// be defined to return, say, a 0) if ADT is not an enum.
|
2017-12-06 09:25:29 +01:00
|
|
|
|
Discriminant(Place<'tcx>),
|
2017-01-31 01:10:54 +02:00
|
|
|
|
|
2016-02-03 13:25:07 +01:00
|
|
|
|
/// Create an aggregate value, like a tuple or struct. This is
|
|
|
|
|
/// only needed because we want to distinguish `dest = Foo { x:
|
|
|
|
|
/// ..., y: ... }` from `dest.x = ...; dest.y = ...;` in the case
|
|
|
|
|
/// that `Foo` has a destructor. These rvalues can be optimized
|
|
|
|
|
/// away after type-checking and before lowering.
|
2017-05-12 01:38:26 +03:00
|
|
|
|
Aggregate(Box<AggregateKind<'tcx>>, Vec<Operand<'tcx>>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-02 14:46:39 +01:00
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
|
2015-08-18 17:59:21 -04:00
|
|
|
|
pub enum CastKind {
|
|
|
|
|
Misc,
|
|
|
|
|
|
|
|
|
|
/// Convert unique, zero-sized type for a fn to fn()
|
|
|
|
|
ReifyFnPointer,
|
|
|
|
|
|
2017-02-22 01:24:16 +01:00
|
|
|
|
/// Convert non capturing closure to fn()
|
|
|
|
|
ClosureFnPointer,
|
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
|
/// Convert safe fn() to unsafe fn()
|
|
|
|
|
UnsafeFnPointer,
|
|
|
|
|
|
|
|
|
|
/// "Unsize" -- convert a thin-or-fat pointer to a fat pointer.
|
2018-05-08 16:10:16 +03:00
|
|
|
|
/// codegen must figure out the details once full monomorphization
|
2015-08-18 17:59:21 -04:00
|
|
|
|
/// is known. For example, this could be used to cast from a
|
|
|
|
|
/// `&[i32;N]` to a `&[i32]`, or a `Box<T>` to a `Box<Trait>`
|
|
|
|
|
/// (presuming `T: Trait`).
|
|
|
|
|
Unsize,
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-08 15:53:19 -05:00
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub enum AggregateKind<'tcx> {
|
2017-02-26 00:32:14 +02:00
|
|
|
|
/// The type is of the element
|
|
|
|
|
Array(Ty<'tcx>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
Tuple,
|
2017-12-06 09:25:29 +01:00
|
|
|
|
|
2018-01-29 14:11:32 +05:30
|
|
|
|
/// The second field is the variant index. It's equal to 0 for struct
|
|
|
|
|
/// and union expressions. The fourth field is
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// active field number and is present only for union expressions
|
|
|
|
|
/// -- e.g. for a union expression `SomeUnion { c: .. }`, the
|
|
|
|
|
/// active field index would identity the field `c`
|
2016-11-25 01:33:29 +02:00
|
|
|
|
Adt(&'tcx AdtDef, usize, &'tcx Substs<'tcx>, Option<usize>),
|
2017-12-06 09:25:29 +01:00
|
|
|
|
|
2016-04-29 08:30:54 +03:00
|
|
|
|
Closure(DefId, ClosureSubsts<'tcx>),
|
2018-05-02 13:14:30 +02:00
|
|
|
|
Generator(DefId, GeneratorSubsts<'tcx>, hir::GeneratorMovability),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-08 15:53:19 -05:00
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
|
2015-08-18 17:59:21 -04:00
|
|
|
|
pub enum BinOp {
|
|
|
|
|
/// The `+` operator (addition)
|
|
|
|
|
Add,
|
|
|
|
|
/// The `-` operator (subtraction)
|
|
|
|
|
Sub,
|
|
|
|
|
/// The `*` operator (multiplication)
|
|
|
|
|
Mul,
|
|
|
|
|
/// The `/` operator (division)
|
|
|
|
|
Div,
|
|
|
|
|
/// The `%` operator (modulus)
|
|
|
|
|
Rem,
|
|
|
|
|
/// The `^` operator (bitwise xor)
|
|
|
|
|
BitXor,
|
|
|
|
|
/// The `&` operator (bitwise and)
|
|
|
|
|
BitAnd,
|
|
|
|
|
/// The `|` operator (bitwise or)
|
|
|
|
|
BitOr,
|
|
|
|
|
/// The `<<` operator (shift left)
|
|
|
|
|
Shl,
|
|
|
|
|
/// The `>>` operator (shift right)
|
|
|
|
|
Shr,
|
|
|
|
|
/// The `==` operator (equality)
|
|
|
|
|
Eq,
|
|
|
|
|
/// The `<` operator (less than)
|
|
|
|
|
Lt,
|
|
|
|
|
/// The `<=` operator (less than or equal to)
|
|
|
|
|
Le,
|
|
|
|
|
/// The `!=` operator (not equal to)
|
|
|
|
|
Ne,
|
|
|
|
|
/// The `>=` operator (greater than or equal to)
|
|
|
|
|
Ge,
|
|
|
|
|
/// The `>` operator (greater than)
|
|
|
|
|
Gt,
|
2017-05-18 18:43:52 +03:00
|
|
|
|
/// The `ptr.offset` operator
|
|
|
|
|
Offset,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 18:50:07 +13:00
|
|
|
|
impl BinOp {
|
|
|
|
|
pub fn is_checkable(self) -> bool {
|
|
|
|
|
use self::BinOp::*;
|
|
|
|
|
match self {
|
|
|
|
|
Add | Sub | Mul | Shl | Shr => true,
|
|
|
|
|
_ => false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-18 18:43:52 +03:00
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
|
|
|
|
|
pub enum NullOp {
|
|
|
|
|
/// Return the size of a value of that type
|
|
|
|
|
SizeOf,
|
|
|
|
|
/// Create a new uninitialized box for a value of that type
|
|
|
|
|
Box,
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-08 15:53:19 -05:00
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
|
2015-08-18 17:59:21 -04:00
|
|
|
|
pub enum UnOp {
|
|
|
|
|
/// The `!` operator for logical inversion
|
|
|
|
|
Not,
|
|
|
|
|
/// The `-` operator for negation
|
2015-10-07 14:37:42 +02:00
|
|
|
|
Neg,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-05 12:31:48 -04:00
|
|
|
|
impl<'tcx> Debug for Rvalue<'tcx> {
|
2015-12-31 20:11:25 -06:00
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
2015-08-18 17:59:21 -04:00
|
|
|
|
use self::Rvalue::*;
|
|
|
|
|
|
|
|
|
|
match *self {
|
2017-12-06 09:25:29 +01:00
|
|
|
|
Use(ref place) => write!(fmt, "{:?}", place),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
Repeat(ref a, ref b) => write!(fmt, "[{:?}; {:?}]", a, b),
|
2015-12-18 19:29:03 -06:00
|
|
|
|
Len(ref a) => write!(fmt, "Len({:?})", a),
|
2017-12-06 09:25:29 +01:00
|
|
|
|
Cast(ref kind, ref place, ref ty) => {
|
|
|
|
|
write!(fmt, "{:?} as {:?} ({:?})", place, ty, kind)
|
|
|
|
|
}
|
2015-12-18 19:29:03 -06:00
|
|
|
|
BinaryOp(ref op, ref a, ref b) => write!(fmt, "{:?}({:?}, {:?})", op, a, b),
|
2016-03-31 18:50:07 +13:00
|
|
|
|
CheckedBinaryOp(ref op, ref a, ref b) => {
|
|
|
|
|
write!(fmt, "Checked{:?}({:?}, {:?})", op, a, b)
|
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
UnaryOp(ref op, ref a) => write!(fmt, "{:?}({:?})", op, a),
|
2017-12-06 09:25:29 +01:00
|
|
|
|
Discriminant(ref place) => write!(fmt, "discriminant({:?})", place),
|
2017-05-18 18:43:52 +03:00
|
|
|
|
NullaryOp(ref op, ref t) => write!(fmt, "{:?}({:?})", op, t),
|
2017-12-06 09:25:29 +01:00
|
|
|
|
Ref(region, borrow_kind, ref place) => {
|
2016-01-01 00:39:02 -06:00
|
|
|
|
let kind_str = match borrow_kind {
|
|
|
|
|
BorrowKind::Shared => "",
|
2018-01-15 12:47:26 +01:00
|
|
|
|
BorrowKind::Mut { .. } | BorrowKind::Unique => "mut ",
|
2016-01-01 00:39:02 -06:00
|
|
|
|
};
|
2017-03-23 11:08:08 +01:00
|
|
|
|
|
2017-07-24 13:43:05 -07:00
|
|
|
|
// When printing regions, add trailing space if necessary.
|
2017-07-24 16:32:11 -07:00
|
|
|
|
let region = if ppaux::verbose() || ppaux::identify_regions() {
|
2017-03-23 11:08:08 +01:00
|
|
|
|
let mut region = format!("{}", region);
|
|
|
|
|
if region.len() > 0 { region.push(' '); }
|
|
|
|
|
region
|
2017-07-24 16:32:11 -07:00
|
|
|
|
} else {
|
|
|
|
|
// Do not even print 'static
|
|
|
|
|
"".to_owned()
|
2017-03-23 11:08:08 +01:00
|
|
|
|
};
|
2017-12-06 09:25:29 +01:00
|
|
|
|
write!(fmt, "&{}{}{:?}", region, kind_str, place)
|
2016-01-01 00:39:02 -06:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
Aggregate(ref kind, ref places) => {
|
|
|
|
|
fn fmt_tuple(fmt: &mut Formatter, places: &[Operand]) -> fmt::Result {
|
2016-02-23 22:04:27 +02:00
|
|
|
|
let mut tuple_fmt = fmt.debug_tuple("");
|
2017-12-06 09:25:29 +01:00
|
|
|
|
for place in places {
|
|
|
|
|
tuple_fmt.field(place);
|
2015-12-31 21:38:44 -06:00
|
|
|
|
}
|
|
|
|
|
tuple_fmt.finish()
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-12 01:38:26 +03:00
|
|
|
|
match **kind {
|
2017-12-06 09:25:29 +01:00
|
|
|
|
AggregateKind::Array(_) => write!(fmt, "{:?}", places),
|
2015-12-31 21:38:44 -06:00
|
|
|
|
|
2016-09-20 02:14:46 +02:00
|
|
|
|
AggregateKind::Tuple => {
|
2017-12-06 09:25:29 +01:00
|
|
|
|
match places.len() {
|
2016-01-06 14:17:38 -06:00
|
|
|
|
0 => write!(fmt, "()"),
|
2017-12-06 09:25:29 +01:00
|
|
|
|
1 => write!(fmt, "({:?},)", places[0]),
|
|
|
|
|
_ => fmt_tuple(fmt, places),
|
2015-12-31 21:38:44 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-20 02:14:46 +02:00
|
|
|
|
AggregateKind::Adt(adt_def, variant, substs, _) => {
|
2015-12-31 21:38:44 -06:00
|
|
|
|
let variant_def = &adt_def.variants[variant];
|
2016-02-23 22:04:27 +02:00
|
|
|
|
|
2016-10-28 16:26:47 -06:00
|
|
|
|
ppaux::parameterized(fmt, substs, variant_def.did, &[])?;
|
2015-12-31 21:38:44 -06:00
|
|
|
|
|
2016-09-15 00:51:46 +03:00
|
|
|
|
match variant_def.ctor_kind {
|
|
|
|
|
CtorKind::Const => Ok(()),
|
2017-12-06 09:25:29 +01:00
|
|
|
|
CtorKind::Fn => fmt_tuple(fmt, places),
|
2016-09-15 00:51:46 +03:00
|
|
|
|
CtorKind::Fictive => {
|
2016-02-23 22:04:27 +02:00
|
|
|
|
let mut struct_fmt = fmt.debug_struct("");
|
2017-12-06 09:25:29 +01:00
|
|
|
|
for (field, place) in variant_def.fields.iter().zip(places) {
|
2018-05-26 15:12:38 +03:00
|
|
|
|
struct_fmt.field(&field.ident.as_str(), place);
|
2015-12-31 21:38:44 -06:00
|
|
|
|
}
|
|
|
|
|
struct_fmt.finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-20 02:14:46 +02:00
|
|
|
|
AggregateKind::Closure(def_id, _) => ty::tls::with(|tcx| {
|
2017-01-26 02:41:06 +02:00
|
|
|
|
if let Some(node_id) = tcx.hir.as_local_node_id(def_id) {
|
2017-03-16 14:41:48 +01:00
|
|
|
|
let name = if tcx.sess.opts.debugging_opts.span_free_formats {
|
|
|
|
|
format!("[closure@{:?}]", node_id)
|
|
|
|
|
} else {
|
|
|
|
|
format!("[closure@{:?}]", tcx.hir.span(node_id))
|
|
|
|
|
};
|
2015-12-31 21:38:44 -06:00
|
|
|
|
let mut struct_fmt = fmt.debug_struct(&name);
|
|
|
|
|
|
|
|
|
|
tcx.with_freevars(node_id, |freevars| {
|
2017-12-06 09:25:29 +01:00
|
|
|
|
for (freevar, place) in freevars.iter().zip(places) {
|
2017-04-29 14:39:47 +03:00
|
|
|
|
let var_name = tcx.hir.name(freevar.var_id());
|
2017-12-06 09:25:29 +01:00
|
|
|
|
struct_fmt.field(&var_name.as_str(), place);
|
2015-12-31 21:38:44 -06:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
struct_fmt.finish()
|
|
|
|
|
} else {
|
|
|
|
|
write!(fmt, "[closure]")
|
|
|
|
|
}
|
|
|
|
|
}),
|
2016-12-26 14:34:03 +01:00
|
|
|
|
|
2018-05-02 13:14:30 +02:00
|
|
|
|
AggregateKind::Generator(def_id, _, _) => ty::tls::with(|tcx| {
|
2016-12-26 14:34:03 +01:00
|
|
|
|
if let Some(node_id) = tcx.hir.as_local_node_id(def_id) {
|
|
|
|
|
let name = format!("[generator@{:?}]", tcx.hir.span(node_id));
|
|
|
|
|
let mut struct_fmt = fmt.debug_struct(&name);
|
|
|
|
|
|
|
|
|
|
tcx.with_freevars(node_id, |freevars| {
|
2017-12-06 09:25:29 +01:00
|
|
|
|
for (freevar, place) in freevars.iter().zip(places) {
|
2017-04-29 14:39:47 +03:00
|
|
|
|
let var_name = tcx.hir.name(freevar.var_id());
|
2017-12-06 09:25:29 +01:00
|
|
|
|
struct_fmt.field(&var_name.as_str(), place);
|
2016-12-26 14:34:03 +01:00
|
|
|
|
}
|
2017-12-06 09:25:29 +01:00
|
|
|
|
struct_fmt.field("$state", &places[freevars.len()]);
|
|
|
|
|
for i in (freevars.len() + 1)..places.len() {
|
2017-07-05 14:57:26 -07:00
|
|
|
|
struct_fmt.field(&format!("${}", i - freevars.len() - 1),
|
2017-12-06 09:25:29 +01:00
|
|
|
|
&places[i]);
|
2016-12-26 14:34:03 +01:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
struct_fmt.finish()
|
|
|
|
|
} else {
|
|
|
|
|
write!(fmt, "[generator]")
|
|
|
|
|
}
|
|
|
|
|
}),
|
2015-12-31 21:38:44 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2016-02-03 13:25:07 +01:00
|
|
|
|
/// Constants
|
|
|
|
|
///
|
|
|
|
|
/// Two constants are equal if they are the same constant. Note that
|
|
|
|
|
/// this does not necessarily mean that they are "==" in Rust -- in
|
|
|
|
|
/// particular one must be wary of `NaN`!
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2016-01-25 14:34:34 +01:00
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub struct Constant<'tcx> {
|
|
|
|
|
pub span: Span,
|
|
|
|
|
pub ty: Ty<'tcx>,
|
2015-10-07 14:37:42 +02:00
|
|
|
|
pub literal: Literal<'tcx>,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
newtype_index!(Promoted { DEBUG_FORMAT = "promoted[{}]" });
|
|
|
|
|
|
2016-06-07 17:28:36 +03:00
|
|
|
|
|
2016-01-25 14:34:34 +01:00
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub enum Literal<'tcx> {
|
2015-10-07 14:37:42 +02:00
|
|
|
|
Value {
|
2017-08-04 11:25:13 +03:00
|
|
|
|
value: &'tcx ty::Const<'tcx>,
|
2015-10-07 14:37:42 +02:00
|
|
|
|
},
|
2016-05-03 00:26:41 +03:00
|
|
|
|
Promoted {
|
|
|
|
|
// Index into the `promoted` vector of `Mir`.
|
2016-06-07 17:28:36 +03:00
|
|
|
|
index: Promoted
|
2016-05-03 00:26:41 +03:00
|
|
|
|
},
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
2015-12-18 19:29:03 -06:00
|
|
|
|
|
|
|
|
|
impl<'tcx> Debug for Constant<'tcx> {
|
2015-12-31 20:11:25 -06:00
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
2015-12-18 19:29:03 -06:00
|
|
|
|
write!(fmt, "{:?}", self.literal)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'tcx> Debug for Literal<'tcx> {
|
2015-12-31 20:11:25 -06:00
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
2015-12-18 19:29:03 -06:00
|
|
|
|
use self::Literal::*;
|
|
|
|
|
match *self {
|
2017-08-04 11:25:13 +03:00
|
|
|
|
Value { value } => {
|
2016-03-22 22:01:37 -05:00
|
|
|
|
write!(fmt, "const ")?;
|
2018-01-16 09:24:38 +01:00
|
|
|
|
fmt_const_val(fmt, value)
|
2016-01-05 23:06:33 -06:00
|
|
|
|
}
|
2016-05-03 00:26:41 +03:00
|
|
|
|
Promoted { index } => {
|
2016-06-07 17:28:36 +03:00
|
|
|
|
write!(fmt, "{:?}", index)
|
2016-05-03 00:26:41 +03:00
|
|
|
|
}
|
2015-12-18 19:29:03 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-18 21:52:55 -06:00
|
|
|
|
/// Write a `ConstVal` in a way closer to the original source code than the `Debug` output.
|
2018-04-26 09:18:19 +02:00
|
|
|
|
pub fn fmt_const_val<W: Write>(fmt: &mut W, const_val: &ty::Const) -> fmt::Result {
|
|
|
|
|
use middle::const_val::ConstVal;
|
2018-01-16 09:24:38 +01:00
|
|
|
|
match const_val.val {
|
2018-04-26 09:18:19 +02:00
|
|
|
|
ConstVal::Unevaluated(..) => write!(fmt, "{:?}", const_val),
|
|
|
|
|
ConstVal::Value(val) => {
|
|
|
|
|
if let Some(value) = val.to_byval_value() {
|
|
|
|
|
print_miri_value(value, const_val.ty, fmt)
|
|
|
|
|
} else {
|
|
|
|
|
write!(fmt, "{:?}:{}", val, const_val.ty)
|
|
|
|
|
}
|
|
|
|
|
},
|
2018-01-16 09:24:38 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-16 10:16:38 +01:00
|
|
|
|
pub fn print_miri_value<W: Write>(value: Value, ty: Ty, f: &mut W) -> fmt::Result {
|
2018-01-16 09:24:38 +01:00
|
|
|
|
use ty::TypeVariants::*;
|
|
|
|
|
match (value, &ty.sty) {
|
2018-05-24 10:18:54 +02:00
|
|
|
|
(Value::Scalar(Scalar::Bits { bits: 0, .. }), &TyBool) => write!(f, "false"),
|
|
|
|
|
(Value::Scalar(Scalar::Bits { bits: 1, .. }), &TyBool) => write!(f, "true"),
|
|
|
|
|
(Value::Scalar(Scalar::Bits { bits, .. }), &TyFloat(ast::FloatTy::F32)) =>
|
2018-04-26 14:52:59 +02:00
|
|
|
|
write!(f, "{}f32", Single::from_bits(bits)),
|
2018-05-24 10:18:54 +02:00
|
|
|
|
(Value::Scalar(Scalar::Bits { bits, .. }), &TyFloat(ast::FloatTy::F64)) =>
|
2018-04-26 14:52:59 +02:00
|
|
|
|
write!(f, "{}f64", Double::from_bits(bits)),
|
2018-05-22 10:28:46 +02:00
|
|
|
|
(Value::Scalar(Scalar::Bits { bits, .. }), &TyUint(ui)) => write!(f, "{:?}{}", bits, ui),
|
2018-05-23 17:45:50 +02:00
|
|
|
|
(Value::Scalar(Scalar::Bits { bits, .. }), &TyInt(i)) => {
|
|
|
|
|
let bit_width = ty::tls::with(|tcx| {
|
2018-05-24 11:30:24 +02:00
|
|
|
|
let ty = tcx.lift_to_global(&ty).unwrap();
|
|
|
|
|
tcx.layout_of(ty::ParamEnv::empty().and(ty)).unwrap().size.bits()
|
2018-05-23 17:45:50 +02:00
|
|
|
|
});
|
2018-05-24 14:20:45 +02:00
|
|
|
|
let shift = 128 - bit_width;
|
|
|
|
|
write!(f, "{:?}{}", ((bits as i128) << shift) >> shift, i)
|
2018-05-22 10:28:46 +02:00
|
|
|
|
},
|
2018-05-24 10:18:54 +02:00
|
|
|
|
(Value::Scalar(Scalar::Bits { bits, .. }), &TyChar) =>
|
2018-05-22 10:28:46 +02:00
|
|
|
|
write!(f, "{:?}", ::std::char::from_u32(bits as u32).unwrap()),
|
2018-05-24 10:20:06 +02:00
|
|
|
|
(_, &TyFnDef(did, _)) =>
|
2018-01-16 09:24:38 +01:00
|
|
|
|
write!(f, "{}", item_path_str(did)),
|
2018-05-22 10:28:46 +02:00
|
|
|
|
(Value::ScalarPair(Scalar::Ptr(ptr), Scalar::Bits { bits: len, .. }),
|
2018-05-02 15:21:05 +02:00
|
|
|
|
&TyRef(_, &ty::TyS { sty: TyStr, .. }, _)) => {
|
2018-01-16 09:24:38 +01:00
|
|
|
|
ty::tls::with(|tcx| {
|
2018-05-02 06:03:06 +02:00
|
|
|
|
match tcx.alloc_map.lock().get(ptr.alloc_id) {
|
|
|
|
|
Some(interpret::AllocType::Memory(alloc)) => {
|
|
|
|
|
assert_eq!(len as usize as u128, len);
|
|
|
|
|
let slice = &alloc.bytes[(ptr.offset.bytes() as usize)..][..(len as usize)];
|
|
|
|
|
let s = ::std::str::from_utf8(slice)
|
|
|
|
|
.expect("non utf8 str from miri");
|
|
|
|
|
write!(f, "{:?}", s)
|
|
|
|
|
}
|
|
|
|
|
_ => write!(f, "pointer to erroneous constant {:?}, {:?}", ptr, len),
|
2018-01-16 09:31:48 +01:00
|
|
|
|
}
|
2018-01-16 09:24:38 +01:00
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
_ => write!(f, "{:?}:{}", value, ty),
|
2015-12-18 19:29:03 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-05 23:06:33 -06:00
|
|
|
|
|
|
|
|
|
fn item_path_str(def_id: DefId) -> String {
|
|
|
|
|
ty::tls::with(|tcx| tcx.item_path_str(def_id))
|
|
|
|
|
}
|
2016-06-09 15:49:07 -07:00
|
|
|
|
|
|
|
|
|
impl<'tcx> ControlFlowGraph for Mir<'tcx> {
|
|
|
|
|
|
|
|
|
|
type Node = BasicBlock;
|
|
|
|
|
|
|
|
|
|
fn num_nodes(&self) -> usize { self.basic_blocks.len() }
|
|
|
|
|
|
|
|
|
|
fn start_node(&self) -> Self::Node { START_BLOCK }
|
|
|
|
|
|
|
|
|
|
fn predecessors<'graph>(&'graph self, node: Self::Node)
|
|
|
|
|
-> <Self as GraphPredecessors<'graph>>::Iter
|
|
|
|
|
{
|
|
|
|
|
self.predecessors_for(node).clone().into_iter()
|
|
|
|
|
}
|
|
|
|
|
fn successors<'graph>(&'graph self, node: Self::Node)
|
|
|
|
|
-> <Self as GraphSuccessors<'graph>>::Iter
|
|
|
|
|
{
|
2018-04-27 14:02:09 +03:00
|
|
|
|
self.basic_blocks[node].terminator().successors().cloned()
|
2016-06-09 15:49:07 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, 'b> GraphPredecessors<'b> for Mir<'a> {
|
|
|
|
|
type Item = BasicBlock;
|
|
|
|
|
type Iter = IntoIter<BasicBlock>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, 'b> GraphSuccessors<'b> for Mir<'a> {
|
|
|
|
|
type Item = BasicBlock;
|
2018-04-27 14:02:09 +03:00
|
|
|
|
type Iter = iter::Cloned<Successors<'b>>;
|
2016-06-09 15:49:07 -07:00
|
|
|
|
}
|
2016-08-08 18:46:06 -07:00
|
|
|
|
|
2016-06-11 23:47:28 +03:00
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
|
2016-08-08 18:46:06 -07:00
|
|
|
|
pub struct Location {
|
|
|
|
|
/// the location is within this block
|
|
|
|
|
pub block: BasicBlock,
|
|
|
|
|
|
2018-02-04 16:24:18 +01:00
|
|
|
|
/// the location is the start of the statement; or, if `statement_index`
|
2016-08-08 18:46:06 -07:00
|
|
|
|
/// == num-statements, then the start of the terminator.
|
|
|
|
|
pub statement_index: usize,
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-11 23:47:28 +03:00
|
|
|
|
impl fmt::Debug for Location {
|
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
write!(fmt, "{:?}[{}]", self.block, self.statement_index)
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-15 18:18:40 -07:00
|
|
|
|
|
|
|
|
|
impl Location {
|
2018-05-01 09:46:11 -04:00
|
|
|
|
pub const START: Location = Location {
|
|
|
|
|
block: START_BLOCK,
|
|
|
|
|
statement_index: 0,
|
|
|
|
|
};
|
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// Returns the location immediately after this one within the enclosing block.
|
|
|
|
|
///
|
|
|
|
|
/// Note that if this location represents a terminator, then the
|
|
|
|
|
/// resulting location would be out of bounds and invalid.
|
|
|
|
|
pub fn successor_within_block(&self) -> Location {
|
|
|
|
|
Location { block: self.block, statement_index: self.statement_index + 1 }
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-06 20:48:13 -04:00
|
|
|
|
pub fn dominates(&self, other: Location, dominators: &Dominators<BasicBlock>) -> bool {
|
2016-09-15 18:18:40 -07:00
|
|
|
|
if self.block == other.block {
|
|
|
|
|
self.statement_index <= other.statement_index
|
|
|
|
|
} else {
|
|
|
|
|
dominators.is_dominated_by(other.block, self.block)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-08 22:24:49 +13:00
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
|
|
|
|
|
pub enum UnsafetyViolationKind {
|
|
|
|
|
General,
|
|
|
|
|
ExternStatic(ast::NodeId),
|
|
|
|
|
BorrowPacked(ast::NodeId),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
|
2017-09-19 16:20:02 +03:00
|
|
|
|
pub struct UnsafetyViolation {
|
|
|
|
|
pub source_info: SourceInfo,
|
2017-12-06 09:25:29 +01:00
|
|
|
|
pub description: InternedString,
|
|
|
|
|
pub kind: UnsafetyViolationKind,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
|
|
|
|
|
pub struct UnsafetyCheckResult {
|
|
|
|
|
/// Violations that are propagated *upwards* from this function
|
2018-02-27 17:11:14 +01:00
|
|
|
|
pub violations: Lrc<[UnsafetyViolation]>,
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// unsafe blocks in this function, along with whether they are used. This is
|
|
|
|
|
/// used for the "unused_unsafe" lint.
|
2018-02-27 17:11:14 +01:00
|
|
|
|
pub unsafe_blocks: Lrc<[(ast::NodeId, bool)]>,
|
2017-09-19 16:20:02 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-26 14:34:03 +01:00
|
|
|
|
/// The layout of generator state
|
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
|
|
|
|
|
pub struct GeneratorLayout<'tcx> {
|
|
|
|
|
pub fields: Vec<LocalDecl<'tcx>>,
|
|
|
|
|
}
|
2017-02-08 22:24:49 +13:00
|
|
|
|
|
2018-03-02 20:42:37 -08:00
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
|
|
|
|
|
pub struct BorrowCheckResult<'gcx> {
|
|
|
|
|
pub closure_requirements: Option<ClosureRegionRequirements<'gcx>>,
|
|
|
|
|
pub used_mut_upvars: SmallVec<[Field; 8]>,
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-22 17:38:51 -05:00
|
|
|
|
/// After we borrow check a closure, we are left with various
|
|
|
|
|
/// requirements that we have inferred between the free regions that
|
|
|
|
|
/// appear in the closure's signature or on its field types. These
|
|
|
|
|
/// requirements are then verified and proved by the closure's
|
|
|
|
|
/// creating function. This struct encodes those requirements.
|
|
|
|
|
///
|
|
|
|
|
/// The requirements are listed as being between various
|
|
|
|
|
/// `RegionVid`. The 0th region refers to `'static`; subsequent region
|
|
|
|
|
/// vids refer to the free regions that appear in the closure (or
|
|
|
|
|
/// generator's) type, in order of appearance. (This numbering is
|
|
|
|
|
/// actually defined by the `UniversalRegions` struct in the NLL
|
|
|
|
|
/// region checker. See for example
|
|
|
|
|
/// `UniversalRegions::closure_mapping`.) Note that we treat the free
|
|
|
|
|
/// regions in the closure's type "as if" they were erased, so their
|
|
|
|
|
/// precise identity is not important, only their position.
|
|
|
|
|
///
|
|
|
|
|
/// Example: If type check produces a closure with the closure substs:
|
|
|
|
|
///
|
2017-12-23 22:03:04 -05:00
|
|
|
|
/// ```text
|
2017-11-22 17:38:51 -05:00
|
|
|
|
/// ClosureSubsts = [
|
|
|
|
|
/// i8, // the "closure kind"
|
|
|
|
|
/// for<'x> fn(&'a &'x u32) -> &'x u32, // the "closure signature"
|
|
|
|
|
/// &'a String, // some upvar
|
|
|
|
|
/// ]
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// here, there is one unique free region (`'a`) but it appears
|
2018-02-16 15:56:50 +01:00
|
|
|
|
/// twice. We would "renumber" each occurrence to a unique vid, as follows:
|
2017-11-22 17:38:51 -05:00
|
|
|
|
///
|
2017-12-23 22:03:04 -05:00
|
|
|
|
/// ```text
|
2017-11-22 17:38:51 -05:00
|
|
|
|
/// ClosureSubsts = [
|
|
|
|
|
/// i8, // the "closure kind"
|
|
|
|
|
/// for<'x> fn(&'1 &'x u32) -> &'x u32, // the "closure signature"
|
|
|
|
|
/// &'2 String, // some upvar
|
|
|
|
|
/// ]
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// Now the code might impose a requirement like `'1: '2`. When an
|
|
|
|
|
/// instance of the closure is created, the corresponding free regions
|
|
|
|
|
/// can be extracted from its type and constrained to have the given
|
|
|
|
|
/// outlives relationship.
|
2017-12-05 05:18:58 -05:00
|
|
|
|
///
|
|
|
|
|
/// In some cases, we have to record outlives requirements between
|
|
|
|
|
/// types and regions as well. In that case, if those types include
|
|
|
|
|
/// any regions, those regions are recorded as `ReClosureBound`
|
|
|
|
|
/// instances assigned one of these same indices. Those regions will
|
|
|
|
|
/// be substituted away by the creator. We use `ReClosureBound` in
|
|
|
|
|
/// that case because the regions must be allocated in the global
|
|
|
|
|
/// TyCtxt, and hence we cannot use `ReVar` (which is what we use
|
|
|
|
|
/// internally within the rest of the NLL code).
|
2017-12-08 10:17:17 +01:00
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
|
2017-12-04 11:37:34 -05:00
|
|
|
|
pub struct ClosureRegionRequirements<'gcx> {
|
2017-11-22 17:38:51 -05:00
|
|
|
|
/// The number of external regions defined on the closure. In our
|
|
|
|
|
/// example above, it would be 3 -- one for `'static`, then `'1`
|
|
|
|
|
/// and `'2`. This is just used for a sanity check later on, to
|
|
|
|
|
/// make sure that the number of regions we see at the callsite
|
|
|
|
|
/// matches.
|
|
|
|
|
pub num_external_vids: usize,
|
|
|
|
|
|
|
|
|
|
/// Requirements between the various free regions defined in
|
|
|
|
|
/// indices.
|
2017-12-04 11:37:34 -05:00
|
|
|
|
pub outlives_requirements: Vec<ClosureOutlivesRequirement<'gcx>>,
|
2017-11-22 17:38:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 11:37:34 -05:00
|
|
|
|
/// Indicates an outlives constraint between a type or between two
|
|
|
|
|
/// free-regions declared on the closure.
|
|
|
|
|
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
|
|
|
|
|
pub struct ClosureOutlivesRequirement<'tcx> {
|
|
|
|
|
// This region or type ...
|
|
|
|
|
pub subject: ClosureOutlivesSubject<'tcx>,
|
2017-11-22 17:38:51 -05:00
|
|
|
|
|
|
|
|
|
// .. must outlive this one.
|
|
|
|
|
pub outlived_free_region: ty::RegionVid,
|
|
|
|
|
|
|
|
|
|
// If not, report an error here.
|
|
|
|
|
pub blame_span: Span,
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 11:37:34 -05:00
|
|
|
|
/// The subject of a ClosureOutlivesRequirement -- that is, the thing
|
|
|
|
|
/// that must outlive some region.
|
|
|
|
|
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
|
|
|
|
|
pub enum ClosureOutlivesSubject<'tcx> {
|
|
|
|
|
/// Subject is a type, typically a type parameter, but could also
|
|
|
|
|
/// be a projection. Indicates a requirement like `T: 'a` being
|
|
|
|
|
/// passed to the caller, where the type here is `T`.
|
|
|
|
|
///
|
|
|
|
|
/// The type here is guaranteed not to contain any free regions at
|
|
|
|
|
/// present.
|
|
|
|
|
Ty(Ty<'tcx>),
|
|
|
|
|
|
|
|
|
|
/// Subject is a free region from the closure. Indicates a requirement
|
|
|
|
|
/// like `'a: 'b` being passed to the caller; the region here is `'a`.
|
|
|
|
|
Region(ty::RegionVid),
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-08 22:24:49 +13:00
|
|
|
|
/*
|
|
|
|
|
* TypeFoldable implementations for MIR types
|
|
|
|
|
*/
|
|
|
|
|
|
2018-02-09 10:34:23 -05:00
|
|
|
|
CloneTypeFoldableAndLiftImpls! {
|
|
|
|
|
Mutability,
|
|
|
|
|
SourceInfo,
|
|
|
|
|
UpvarDecl,
|
|
|
|
|
ValidationOp,
|
2018-05-28 14:16:09 +03:00
|
|
|
|
SourceScope,
|
2018-05-28 17:37:48 +03:00
|
|
|
|
SourceScopeData,
|
|
|
|
|
SourceScopeLocalData,
|
2017-02-08 22:24:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-09 10:34:23 -05:00
|
|
|
|
BraceStructTypeFoldableImpl! {
|
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for Mir<'tcx> {
|
|
|
|
|
basic_blocks,
|
2018-05-28 14:16:09 +03:00
|
|
|
|
source_scopes,
|
2018-05-28 17:37:48 +03:00
|
|
|
|
source_scope_local_data,
|
2018-02-09 10:34:23 -05:00
|
|
|
|
promoted,
|
|
|
|
|
yield_ty,
|
|
|
|
|
generator_drop,
|
|
|
|
|
generator_layout,
|
|
|
|
|
local_decls,
|
|
|
|
|
arg_count,
|
|
|
|
|
upvar_decls,
|
|
|
|
|
spread_arg,
|
|
|
|
|
span,
|
|
|
|
|
cache,
|
2016-12-26 14:34:03 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-09 10:34:23 -05:00
|
|
|
|
BraceStructTypeFoldableImpl! {
|
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for GeneratorLayout<'tcx> {
|
|
|
|
|
fields
|
2017-02-08 22:24:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-09 10:34:23 -05:00
|
|
|
|
BraceStructTypeFoldableImpl! {
|
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for LocalDecl<'tcx> {
|
|
|
|
|
mutability,
|
|
|
|
|
is_user_variable,
|
|
|
|
|
internal,
|
|
|
|
|
ty,
|
|
|
|
|
name,
|
2018-05-29 21:31:33 +03:00
|
|
|
|
source_info,
|
2018-05-29 13:55:21 +03:00
|
|
|
|
visibility_scope,
|
2017-02-08 22:24:49 +13:00
|
|
|
|
}
|
2018-02-09 10:34:23 -05:00
|
|
|
|
}
|
2017-02-08 22:24:49 +13:00
|
|
|
|
|
2018-02-09 10:34:23 -05:00
|
|
|
|
BraceStructTypeFoldableImpl! {
|
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for BasicBlockData<'tcx> {
|
|
|
|
|
statements,
|
|
|
|
|
terminator,
|
|
|
|
|
is_cleanup,
|
2017-02-08 22:24:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-09 10:34:23 -05:00
|
|
|
|
BraceStructTypeFoldableImpl! {
|
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for ValidationOperand<'tcx, Place<'tcx>> {
|
|
|
|
|
place, ty, re, mutbl
|
2017-07-21 12:43:09 -07:00
|
|
|
|
}
|
2018-02-09 10:34:23 -05:00
|
|
|
|
}
|
2017-07-21 12:43:09 -07:00
|
|
|
|
|
2018-02-09 10:34:23 -05:00
|
|
|
|
BraceStructTypeFoldableImpl! {
|
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for Statement<'tcx> {
|
|
|
|
|
source_info, kind
|
2017-07-21 12:43:09 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-09 10:34:23 -05:00
|
|
|
|
EnumTypeFoldableImpl! {
|
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for StatementKind<'tcx> {
|
|
|
|
|
(StatementKind::Assign)(a, b),
|
2018-05-04 12:04:33 +02:00
|
|
|
|
(StatementKind::ReadForMatch)(place),
|
2018-02-09 10:34:23 -05:00
|
|
|
|
(StatementKind::SetDiscriminant) { place, variant_index },
|
|
|
|
|
(StatementKind::StorageLive)(a),
|
|
|
|
|
(StatementKind::StorageDead)(a),
|
|
|
|
|
(StatementKind::InlineAsm) { asm, outputs, inputs },
|
|
|
|
|
(StatementKind::Validate)(a, b),
|
|
|
|
|
(StatementKind::EndRegion)(a),
|
2018-02-23 20:52:05 +00:00
|
|
|
|
(StatementKind::UserAssertTy)(a, b),
|
2018-02-09 10:34:23 -05:00
|
|
|
|
(StatementKind::Nop),
|
2017-02-08 22:24:49 +13:00
|
|
|
|
}
|
2018-02-09 10:34:23 -05:00
|
|
|
|
}
|
2017-02-08 22:24:49 +13:00
|
|
|
|
|
2018-02-09 10:34:23 -05:00
|
|
|
|
EnumTypeFoldableImpl! {
|
|
|
|
|
impl<'tcx, T> TypeFoldable<'tcx> for ClearCrossCrate<T> {
|
|
|
|
|
(ClearCrossCrate::Clear),
|
|
|
|
|
(ClearCrossCrate::Set)(a),
|
|
|
|
|
} where T: TypeFoldable<'tcx>
|
2017-02-08 22:24:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
|
|
|
|
|
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
|
|
|
|
|
use mir::TerminatorKind::*;
|
|
|
|
|
|
|
|
|
|
let kind = match self.kind {
|
|
|
|
|
Goto { target } => Goto { target: target },
|
|
|
|
|
SwitchInt { ref discr, switch_ty, ref values, ref targets } => SwitchInt {
|
|
|
|
|
discr: discr.fold_with(folder),
|
|
|
|
|
switch_ty: switch_ty.fold_with(folder),
|
|
|
|
|
values: values.clone(),
|
|
|
|
|
targets: targets.clone()
|
|
|
|
|
},
|
|
|
|
|
Drop { ref location, target, unwind } => Drop {
|
|
|
|
|
location: location.fold_with(folder),
|
2017-07-03 11:19:51 -07:00
|
|
|
|
target,
|
|
|
|
|
unwind,
|
2017-02-08 22:24:49 +13:00
|
|
|
|
},
|
|
|
|
|
DropAndReplace { ref location, ref value, target, unwind } => DropAndReplace {
|
|
|
|
|
location: location.fold_with(folder),
|
|
|
|
|
value: value.fold_with(folder),
|
2017-07-03 11:19:51 -07:00
|
|
|
|
target,
|
|
|
|
|
unwind,
|
2017-02-08 22:24:49 +13:00
|
|
|
|
},
|
2017-07-10 21:11:31 +02:00
|
|
|
|
Yield { ref value, resume, drop } => Yield {
|
2016-12-26 14:34:03 +01:00
|
|
|
|
value: value.fold_with(folder),
|
|
|
|
|
resume: resume,
|
|
|
|
|
drop: drop,
|
|
|
|
|
},
|
2017-02-08 22:24:49 +13:00
|
|
|
|
Call { ref func, ref args, ref destination, cleanup } => {
|
|
|
|
|
let dest = destination.as_ref().map(|&(ref loc, dest)| {
|
|
|
|
|
(loc.fold_with(folder), dest)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Call {
|
|
|
|
|
func: func.fold_with(folder),
|
|
|
|
|
args: args.fold_with(folder),
|
|
|
|
|
destination: dest,
|
2017-07-03 11:19:51 -07:00
|
|
|
|
cleanup,
|
2017-02-08 22:24:49 +13:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Assert { ref cond, expected, ref msg, target, cleanup } => {
|
2018-04-27 15:21:31 +02:00
|
|
|
|
let msg = if let EvalErrorKind::BoundsCheck { ref len, ref index } = *msg {
|
|
|
|
|
EvalErrorKind::BoundsCheck {
|
2017-02-08 22:24:49 +13:00
|
|
|
|
len: len.fold_with(folder),
|
|
|
|
|
index: index.fold_with(folder),
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
msg.clone()
|
|
|
|
|
};
|
|
|
|
|
Assert {
|
|
|
|
|
cond: cond.fold_with(folder),
|
2017-07-03 11:19:51 -07:00
|
|
|
|
expected,
|
|
|
|
|
msg,
|
|
|
|
|
target,
|
|
|
|
|
cleanup,
|
2017-02-08 22:24:49 +13:00
|
|
|
|
}
|
|
|
|
|
},
|
2016-12-26 14:34:03 +01:00
|
|
|
|
GeneratorDrop => GeneratorDrop,
|
2017-02-08 22:24:49 +13:00
|
|
|
|
Resume => Resume,
|
2017-12-19 01:17:16 +01:00
|
|
|
|
Abort => Abort,
|
2017-02-08 22:24:49 +13:00
|
|
|
|
Return => Return,
|
|
|
|
|
Unreachable => Unreachable,
|
2017-12-06 09:25:29 +01:00
|
|
|
|
FalseEdges { real_target, ref imaginary_targets } =>
|
2018-01-25 01:45:45 -05:00
|
|
|
|
FalseEdges { real_target, imaginary_targets: imaginary_targets.clone() },
|
|
|
|
|
FalseUnwind { real_target, unwind } => FalseUnwind { real_target, unwind },
|
2017-02-08 22:24:49 +13:00
|
|
|
|
};
|
|
|
|
|
Terminator {
|
|
|
|
|
source_info: self.source_info,
|
2017-07-03 11:19:51 -07:00
|
|
|
|
kind,
|
2017-02-08 22:24:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
|
|
|
|
|
use mir::TerminatorKind::*;
|
|
|
|
|
|
|
|
|
|
match self.kind {
|
|
|
|
|
SwitchInt { ref discr, switch_ty, .. } =>
|
|
|
|
|
discr.visit_with(visitor) || switch_ty.visit_with(visitor),
|
|
|
|
|
Drop { ref location, ..} => location.visit_with(visitor),
|
|
|
|
|
DropAndReplace { ref location, ref value, ..} =>
|
|
|
|
|
location.visit_with(visitor) || value.visit_with(visitor),
|
2017-07-10 21:11:31 +02:00
|
|
|
|
Yield { ref value, ..} =>
|
2016-12-26 14:34:03 +01:00
|
|
|
|
value.visit_with(visitor),
|
2017-02-08 22:24:49 +13:00
|
|
|
|
Call { ref func, ref args, ref destination, .. } => {
|
|
|
|
|
let dest = if let Some((ref loc, _)) = *destination {
|
|
|
|
|
loc.visit_with(visitor)
|
|
|
|
|
} else { false };
|
|
|
|
|
dest || func.visit_with(visitor) || args.visit_with(visitor)
|
|
|
|
|
},
|
|
|
|
|
Assert { ref cond, ref msg, .. } => {
|
|
|
|
|
if cond.visit_with(visitor) {
|
2018-04-27 15:21:31 +02:00
|
|
|
|
if let EvalErrorKind::BoundsCheck { ref len, ref index } = *msg {
|
2017-02-08 22:24:49 +13:00
|
|
|
|
len.visit_with(visitor) || index.visit_with(visitor)
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Goto { .. } |
|
|
|
|
|
Resume |
|
2017-12-19 01:17:16 +01:00
|
|
|
|
Abort |
|
2017-02-08 22:24:49 +13:00
|
|
|
|
Return |
|
2016-12-26 14:34:03 +01:00
|
|
|
|
GeneratorDrop |
|
2017-12-06 09:25:29 +01:00
|
|
|
|
Unreachable |
|
2018-01-25 01:45:45 -05:00
|
|
|
|
FalseEdges { .. } |
|
|
|
|
|
FalseUnwind { .. } => false
|
2017-02-08 22:24:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for Place<'tcx> {
|
2017-02-08 22:24:49 +13:00
|
|
|
|
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
|
|
|
|
|
match self {
|
2017-12-06 09:25:29 +01:00
|
|
|
|
&Place::Projection(ref p) => Place::Projection(p.fold_with(folder)),
|
2017-02-08 22:24:49 +13:00
|
|
|
|
_ => self.clone()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
|
2017-12-06 09:25:29 +01:00
|
|
|
|
if let &Place::Projection(ref p) = self {
|
2017-02-08 22:24:49 +13:00
|
|
|
|
p.visit_with(visitor)
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for Rvalue<'tcx> {
|
|
|
|
|
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
|
|
|
|
|
use mir::Rvalue::*;
|
|
|
|
|
match *self {
|
|
|
|
|
Use(ref op) => Use(op.fold_with(folder)),
|
|
|
|
|
Repeat(ref op, len) => Repeat(op.fold_with(folder), len),
|
2017-12-06 09:25:29 +01:00
|
|
|
|
Ref(region, bk, ref place) =>
|
|
|
|
|
Ref(region.fold_with(folder), bk, place.fold_with(folder)),
|
|
|
|
|
Len(ref place) => Len(place.fold_with(folder)),
|
2017-02-08 22:24:49 +13:00
|
|
|
|
Cast(kind, ref op, ty) => Cast(kind, op.fold_with(folder), ty.fold_with(folder)),
|
|
|
|
|
BinaryOp(op, ref rhs, ref lhs) =>
|
|
|
|
|
BinaryOp(op, rhs.fold_with(folder), lhs.fold_with(folder)),
|
|
|
|
|
CheckedBinaryOp(op, ref rhs, ref lhs) =>
|
|
|
|
|
CheckedBinaryOp(op, rhs.fold_with(folder), lhs.fold_with(folder)),
|
|
|
|
|
UnaryOp(op, ref val) => UnaryOp(op, val.fold_with(folder)),
|
2017-12-06 09:25:29 +01:00
|
|
|
|
Discriminant(ref place) => Discriminant(place.fold_with(folder)),
|
2017-05-18 18:43:52 +03:00
|
|
|
|
NullaryOp(op, ty) => NullaryOp(op, ty.fold_with(folder)),
|
2017-02-08 22:24:49 +13:00
|
|
|
|
Aggregate(ref kind, ref fields) => {
|
2017-05-12 01:38:26 +03:00
|
|
|
|
let kind = box match **kind {
|
2017-02-08 22:24:49 +13:00
|
|
|
|
AggregateKind::Array(ty) => AggregateKind::Array(ty.fold_with(folder)),
|
|
|
|
|
AggregateKind::Tuple => AggregateKind::Tuple,
|
|
|
|
|
AggregateKind::Adt(def, v, substs, n) =>
|
|
|
|
|
AggregateKind::Adt(def, v, substs.fold_with(folder), n),
|
|
|
|
|
AggregateKind::Closure(id, substs) =>
|
2016-12-26 14:34:03 +01:00
|
|
|
|
AggregateKind::Closure(id, substs.fold_with(folder)),
|
2018-05-02 13:14:30 +02:00
|
|
|
|
AggregateKind::Generator(id, substs, movablity) =>
|
|
|
|
|
AggregateKind::Generator(id, substs.fold_with(folder), movablity),
|
2017-02-08 22:24:49 +13:00
|
|
|
|
};
|
|
|
|
|
Aggregate(kind, fields.fold_with(folder))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
|
|
|
|
|
use mir::Rvalue::*;
|
|
|
|
|
match *self {
|
|
|
|
|
Use(ref op) => op.visit_with(visitor),
|
|
|
|
|
Repeat(ref op, _) => op.visit_with(visitor),
|
2017-12-06 09:25:29 +01:00
|
|
|
|
Ref(region, _, ref place) => region.visit_with(visitor) || place.visit_with(visitor),
|
|
|
|
|
Len(ref place) => place.visit_with(visitor),
|
2017-02-08 22:24:49 +13:00
|
|
|
|
Cast(_, ref op, ty) => op.visit_with(visitor) || ty.visit_with(visitor),
|
|
|
|
|
BinaryOp(_, ref rhs, ref lhs) |
|
|
|
|
|
CheckedBinaryOp(_, ref rhs, ref lhs) =>
|
|
|
|
|
rhs.visit_with(visitor) || lhs.visit_with(visitor),
|
|
|
|
|
UnaryOp(_, ref val) => val.visit_with(visitor),
|
2017-12-06 09:25:29 +01:00
|
|
|
|
Discriminant(ref place) => place.visit_with(visitor),
|
2017-05-18 18:43:52 +03:00
|
|
|
|
NullaryOp(_, ty) => ty.visit_with(visitor),
|
2017-02-08 22:24:49 +13:00
|
|
|
|
Aggregate(ref kind, ref fields) => {
|
2017-05-12 01:38:26 +03:00
|
|
|
|
(match **kind {
|
2017-02-08 22:24:49 +13:00
|
|
|
|
AggregateKind::Array(ty) => ty.visit_with(visitor),
|
|
|
|
|
AggregateKind::Tuple => false,
|
|
|
|
|
AggregateKind::Adt(_, _, substs, _) => substs.visit_with(visitor),
|
2016-12-26 14:34:03 +01:00
|
|
|
|
AggregateKind::Closure(_, substs) => substs.visit_with(visitor),
|
2018-05-02 13:14:30 +02:00
|
|
|
|
AggregateKind::Generator(_, substs, _) => substs.visit_with(visitor),
|
2017-02-08 22:24:49 +13:00
|
|
|
|
}) || fields.visit_with(visitor)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for Operand<'tcx> {
|
|
|
|
|
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
|
|
|
|
|
match *self {
|
2017-12-06 09:25:29 +01:00
|
|
|
|
Operand::Copy(ref place) => Operand::Copy(place.fold_with(folder)),
|
|
|
|
|
Operand::Move(ref place) => Operand::Move(place.fold_with(folder)),
|
2017-02-08 22:24:49 +13:00
|
|
|
|
Operand::Constant(ref c) => Operand::Constant(c.fold_with(folder)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
|
|
|
|
|
match *self {
|
2017-12-06 09:25:29 +01:00
|
|
|
|
Operand::Copy(ref place) |
|
|
|
|
|
Operand::Move(ref place) => place.visit_with(visitor),
|
2017-02-08 22:24:49 +13:00
|
|
|
|
Operand::Constant(ref c) => c.visit_with(visitor)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-27 23:12:08 +03:00
|
|
|
|
impl<'tcx, B, V, T> TypeFoldable<'tcx> for Projection<'tcx, B, V, T>
|
|
|
|
|
where B: TypeFoldable<'tcx>, V: TypeFoldable<'tcx>, T: TypeFoldable<'tcx>
|
2017-02-08 22:24:49 +13:00
|
|
|
|
{
|
|
|
|
|
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
|
|
|
|
|
use mir::ProjectionElem::*;
|
|
|
|
|
|
|
|
|
|
let base = self.base.fold_with(folder);
|
|
|
|
|
let elem = match self.elem {
|
|
|
|
|
Deref => Deref,
|
2017-07-27 23:12:08 +03:00
|
|
|
|
Field(f, ref ty) => Field(f, ty.fold_with(folder)),
|
2017-02-08 22:24:49 +13:00
|
|
|
|
Index(ref v) => Index(v.fold_with(folder)),
|
|
|
|
|
ref elem => elem.clone()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Projection {
|
2017-07-03 11:19:51 -07:00
|
|
|
|
base,
|
|
|
|
|
elem,
|
2017-02-08 22:24:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn super_visit_with<Vs: TypeVisitor<'tcx>>(&self, visitor: &mut Vs) -> bool {
|
|
|
|
|
use mir::ProjectionElem::*;
|
|
|
|
|
|
|
|
|
|
self.base.visit_with(visitor) ||
|
|
|
|
|
match self.elem {
|
2017-07-27 23:12:08 +03:00
|
|
|
|
Field(_, ref ty) => ty.visit_with(visitor),
|
2017-02-08 22:24:49 +13:00
|
|
|
|
Index(ref v) => v.visit_with(visitor),
|
|
|
|
|
_ => false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-16 09:24:38 +01:00
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for Field {
|
|
|
|
|
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, _: &mut F) -> Self {
|
|
|
|
|
*self
|
|
|
|
|
}
|
|
|
|
|
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> bool {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-08 22:24:49 +13:00
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for Constant<'tcx> {
|
|
|
|
|
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
|
|
|
|
|
Constant {
|
|
|
|
|
span: self.span.clone(),
|
|
|
|
|
ty: self.ty.fold_with(folder),
|
|
|
|
|
literal: self.literal.fold_with(folder)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
|
|
|
|
|
self.ty.visit_with(visitor) || self.literal.visit_with(visitor)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for Literal<'tcx> {
|
|
|
|
|
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
|
|
|
|
|
match *self {
|
2017-09-03 20:34:48 +03:00
|
|
|
|
Literal::Value { value } => Literal::Value {
|
|
|
|
|
value: value.fold_with(folder)
|
2017-02-08 22:24:49 +13:00
|
|
|
|
},
|
2017-09-03 20:34:48 +03:00
|
|
|
|
Literal::Promoted { index } => Literal::Promoted { index }
|
2017-02-08 22:24:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
|
|
|
|
|
match *self {
|
2017-09-03 20:34:48 +03:00
|
|
|
|
Literal::Value { value } => value.visit_with(visitor),
|
|
|
|
|
Literal::Promoted { .. } => false
|
2017-02-08 22:24:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|