2019-04-22 15:29:04 +01:00
|
|
|
|
// ignore-tidy-filelength
|
|
|
|
|
|
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-11-26 15:03:13 -06:00
|
|
|
|
//! [rustc guide]: https://rust-lang.github.io/rustc-guide/mir/index.html
|
2017-04-29 05:28:35 -04:00
|
|
|
|
|
2018-12-12 15:12:48 +02:00
|
|
|
|
use crate::hir::def::{CtorKind, Namespace};
|
2019-02-05 11:20:45 -06:00
|
|
|
|
use crate::hir::def_id::DefId;
|
2019-11-25 13:38:08 +00:00
|
|
|
|
use crate::hir::{self, GeneratorKind};
|
2019-11-18 22:15:33 +00:00
|
|
|
|
use crate::mir::interpret::{GlobalAlloc, PanicInfo, Scalar};
|
2019-02-05 11:20:45 -06:00
|
|
|
|
use crate::mir::visit::MirVisitable;
|
2019-07-12 22:49:15 +02:00
|
|
|
|
use crate::ty::adjustment::PointerCast;
|
|
|
|
|
use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
|
|
|
|
|
use crate::ty::layout::VariantIdx;
|
|
|
|
|
use crate::ty::print::{FmtPrinter, Printer};
|
|
|
|
|
use crate::ty::subst::{Subst, SubstsRef};
|
|
|
|
|
use crate::ty::{
|
2019-10-20 16:11:04 -04:00
|
|
|
|
self, AdtDef, CanonicalUserTypeAnnotations, List, Region, Ty, TyCtxt, UserTypeAnnotationIndex,
|
2019-07-12 22:49:15 +02:00
|
|
|
|
};
|
2019-09-06 03:57:44 +01:00
|
|
|
|
|
2019-07-12 22:48:02 +02:00
|
|
|
|
use polonius_engine::Atom;
|
2019-09-26 05:30:10 +00:00
|
|
|
|
use rustc_index::bit_set::BitMatrix;
|
2018-11-05 15:14:28 +01:00
|
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2018-07-02 06:14:49 -04:00
|
|
|
|
use rustc_data_structures::graph::dominators::{dominators, Dominators};
|
2018-07-13 01:17:20 -04:00
|
|
|
|
use rustc_data_structures::graph::{self, GraphPredecessors, GraphSuccessors};
|
2019-09-26 05:38:33 +00:00
|
|
|
|
use rustc_index::vec::{Idx, IndexVec};
|
2018-06-19 21:22:52 -03:00
|
|
|
|
use rustc_data_structures::sync::Lrc;
|
2018-08-04 16:24:39 -06:00
|
|
|
|
use rustc_data_structures::sync::MappedReadGuard;
|
2018-12-03 01:14:35 +01:00
|
|
|
|
use rustc_macros::HashStable;
|
2019-07-23 18:50:47 +03:00
|
|
|
|
use rustc_serialize::{Encodable, Decodable};
|
2018-08-30 15:06:27 -03:00
|
|
|
|
use smallvec::SmallVec;
|
2018-06-19 21:22:52 -03:00
|
|
|
|
use std::borrow::Cow;
|
2019-07-12 22:49:15 +02:00
|
|
|
|
use std::fmt::{self, Debug, Display, Formatter, Write};
|
2016-01-07 05:49:46 -05:00
|
|
|
|
use std::ops::{Index, IndexMut};
|
2018-06-19 21:22:52 -03:00
|
|
|
|
use std::slice;
|
2016-06-09 15:49:07 -07:00
|
|
|
|
use std::vec::IntoIter;
|
2018-06-19 21:22:52 -03:00
|
|
|
|
use std::{iter, mem, option, u32};
|
2019-03-29 10:52:09 +01:00
|
|
|
|
use syntax::ast::Name;
|
2019-10-18 15:01:51 +11:00
|
|
|
|
use syntax::symbol::Symbol;
|
2018-02-16 19:20:18 +02:00
|
|
|
|
use syntax_pos::{Span, DUMMY_SP};
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2019-02-05 11:20:45 -06:00
|
|
|
|
pub use crate::mir::interpret::AssertMessage;
|
2018-04-27 15:21:31 +02:00
|
|
|
|
|
2016-09-19 23:50:00 +03:00
|
|
|
|
mod cache;
|
2017-09-18 16:18:23 +02:00
|
|
|
|
pub mod interpret;
|
2017-10-27 10:50:39 +02:00
|
|
|
|
pub mod mono;
|
2018-06-19 21:22:52 -03:00
|
|
|
|
pub mod tcx;
|
|
|
|
|
pub mod traversal;
|
|
|
|
|
pub mod visit;
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 23:55:04 +02:00
|
|
|
|
impl<'tcx> HasLocalDecls<'tcx> for Body<'tcx> {
|
2017-07-12 13:15:29 -07:00
|
|
|
|
fn local_decls(&self) -> &LocalDecls<'tcx> {
|
2017-07-12 12:59:05 -07:00
|
|
|
|
&self.local_decls
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-20 16:18:17 -04:00
|
|
|
|
/// The various "big phases" that MIR goes through.
|
|
|
|
|
///
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// Warning: ordering of variants is significant.
|
2019-11-09 23:17:42 +01:00
|
|
|
|
#[derive(Copy, Clone, RustcEncodable, RustcDecodable, HashStable,
|
|
|
|
|
Debug, PartialEq, Eq, PartialOrd, Ord)]
|
2018-10-20 16:18:17 -04:00
|
|
|
|
pub enum MirPhase {
|
2018-10-25 08:35:53 -04:00
|
|
|
|
Build = 0,
|
|
|
|
|
Const = 1,
|
|
|
|
|
Validated = 2,
|
|
|
|
|
Optimized = 3,
|
2018-10-20 16:18:17 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-21 10:47:01 -04:00
|
|
|
|
impl MirPhase {
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// Gets the index of the current MirPhase within the set of all `MirPhase`s.
|
2018-10-21 10:47:01 -04:00
|
|
|
|
pub fn phase_index(&self) -> usize {
|
2018-10-25 08:35:53 -04:00
|
|
|
|
*self as usize
|
2018-10-21 10:47:01 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// The lowered representation of a single function.
|
2019-11-09 23:17:42 +01:00
|
|
|
|
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable, TypeFoldable)]
|
2019-05-17 23:55:04 +02:00
|
|
|
|
pub struct Body<'tcx> {
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// A list of basic blocks. References to basic block use a newtyped index type `BasicBlock`
|
2015-11-12 14:29:23 -05:00
|
|
|
|
/// 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-10-20 16:18:17 -04:00
|
|
|
|
/// Records how far through the "desugaring and optimization" process this particular
|
|
|
|
|
/// MIR has traversed. This is particularly useful when inlining, since in that context
|
|
|
|
|
/// we instantiate the promoted constants and add them to our promoted vector -- but those
|
|
|
|
|
/// promoted items have already been optimized, whereas ours have not. This field allows
|
|
|
|
|
/// us to see the difference and forego optimization on the inlined promoted items.
|
|
|
|
|
pub phase: MirPhase,
|
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// A list of source scopes; these are referenced by statements
|
2018-05-28 14:16:09 +03:00
|
|
|
|
/// 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
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// The yield type of the function, if it is a generator.
|
2017-07-10 21:11:31 +02:00
|
|
|
|
pub yield_ty: Option<Ty<'tcx>>,
|
2016-12-26 14:34:03 +01:00
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// Generator drop glue.
|
2019-05-17 23:55:04 +02:00
|
|
|
|
pub generator_drop: Option<Box<Body<'tcx>>>,
|
2016-12-26 14:34:03 +01:00
|
|
|
|
|
|
|
|
|
/// The layout of a generator. Produced by the state transformation.
|
|
|
|
|
pub generator_layout: Option<GeneratorLayout<'tcx>>,
|
|
|
|
|
|
2019-11-25 12:58:40 +00:00
|
|
|
|
/// If this is a generator then record the type of source expression that caused this generator
|
|
|
|
|
/// to be created.
|
|
|
|
|
pub generator_kind: Option<GeneratorKind>,
|
|
|
|
|
|
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
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// User type annotations.
|
2018-11-16 22:56:18 +01:00
|
|
|
|
pub user_type_annotations: CanonicalUserTypeAnnotations<'tcx>,
|
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// The number of arguments this function takes.
|
2016-09-25 01:38:27 +02:00
|
|
|
|
///
|
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-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
|
|
|
|
|
2018-05-16 18:58:54 +03:00
|
|
|
|
/// Debug information pertaining to user variables, including captures.
|
|
|
|
|
pub var_debug_info: Vec<VarDebugInfo<'tcx>>,
|
2018-05-16 15:40:32 +03:00
|
|
|
|
|
2018-11-24 14:38:31 +01:00
|
|
|
|
/// Mark this MIR of a const context other than const functions as having converted a `&&` or
|
|
|
|
|
/// `||` expression into `&` or `|` respectively. This is problematic because if we ever stop
|
|
|
|
|
/// this conversion from happening and use short circuiting, we will cause the following code
|
|
|
|
|
/// to change the value of `x`: `let mut x = 42; false && { x = 55; true };`
|
2018-11-26 17:30:19 +01:00
|
|
|
|
///
|
|
|
|
|
/// List of places where control flow was destroyed. Used for error reporting.
|
|
|
|
|
pub control_flow_destroyed: Vec<(Span, String)>,
|
2018-11-24 14:38:31 +01:00
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// A span representing this MIR, for error reporting.
|
2016-02-07 21:13:00 +02:00
|
|
|
|
pub span: Span,
|
2016-06-07 22:02:08 +03:00
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// A cache for various calculations.
|
2018-06-19 21:22:52 -03:00
|
|
|
|
cache: cache::Cache,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 23:55:04 +02:00
|
|
|
|
impl<'tcx> Body<'tcx> {
|
2018-06-19 21:22:52 -03:00
|
|
|
|
pub fn new(
|
|
|
|
|
basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
|
|
|
|
|
source_scopes: IndexVec<SourceScope, SourceScopeData>,
|
|
|
|
|
source_scope_local_data: ClearCrossCrate<IndexVec<SourceScope, SourceScopeLocalData>>,
|
2018-11-16 22:56:18 +01:00
|
|
|
|
local_decls: LocalDecls<'tcx>,
|
|
|
|
|
user_type_annotations: CanonicalUserTypeAnnotations<'tcx>,
|
2018-06-19 21:22:52 -03:00
|
|
|
|
arg_count: usize,
|
2018-05-16 18:58:54 +03:00
|
|
|
|
var_debug_info: Vec<VarDebugInfo<'tcx>>,
|
2018-06-19 21:22:52 -03:00
|
|
|
|
span: Span,
|
2018-11-26 17:30:19 +01:00
|
|
|
|
control_flow_destroyed: Vec<(Span, String)>,
|
2019-11-25 12:58:40 +00:00
|
|
|
|
generator_kind : Option<GeneratorKind>,
|
2018-06-19 21:22:52 -03:00
|
|
|
|
) -> Self {
|
2019-09-06 03:57:44 +01:00
|
|
|
|
// We need `arg_count` locals, and one for the return place.
|
2018-06-19 21:22:52 -03:00
|
|
|
|
assert!(
|
|
|
|
|
local_decls.len() >= arg_count + 1,
|
|
|
|
|
"expected at least {} locals, got {}",
|
|
|
|
|
arg_count + 1,
|
|
|
|
|
local_decls.len()
|
|
|
|
|
);
|
2016-09-25 01:38:27 +02:00
|
|
|
|
|
2019-05-17 23:55:04 +02:00
|
|
|
|
Body {
|
2018-10-20 16:18:17 -04:00
|
|
|
|
phase: MirPhase::Build,
|
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,
|
2018-05-16 18:58:54 +03:00
|
|
|
|
yield_ty: None,
|
2016-12-26 14:34:03 +01:00
|
|
|
|
generator_drop: None,
|
|
|
|
|
generator_layout: None,
|
2019-11-25 12:58:40 +00:00
|
|
|
|
generator_kind,
|
2017-07-03 11:19:51 -07:00
|
|
|
|
local_decls,
|
2018-11-16 22:56:18 +01:00
|
|
|
|
user_type_annotations,
|
2017-07-03 11:19:51 -07:00
|
|
|
|
arg_count,
|
2016-09-26 22:44:01 +02:00
|
|
|
|
spread_arg: None,
|
2018-05-16 18:58:54 +03:00
|
|
|
|
var_debug_info,
|
2017-07-03 11:19:51 -07:00
|
|
|
|
span,
|
2018-06-19 21:22:52 -03:00
|
|
|
|
cache: cache::Cache::new(),
|
2018-11-26 09:35:23 +01:00
|
|
|
|
control_flow_destroyed,
|
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]
|
2018-06-19 21:22:52 -03:00
|
|
|
|
pub fn basic_blocks_and_local_decls_mut(
|
|
|
|
|
&mut self,
|
2019-07-12 22:49:15 +02:00
|
|
|
|
) -> (&mut IndexVec<BasicBlock, BasicBlockData<'tcx>>, &mut LocalDecls<'tcx>) {
|
2017-12-06 09:25:29 +01:00
|
|
|
|
self.cache.invalidate();
|
|
|
|
|
(&mut self.basic_blocks, &mut self.local_decls)
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-07 22:02:08 +03:00
|
|
|
|
#[inline]
|
2018-08-04 16:24:39 -06:00
|
|
|
|
pub fn predecessors(&self) -> MappedReadGuard<'_, IndexVec<BasicBlock, Vec<BasicBlock>>> {
|
2016-06-07 22:02:08 +03:00
|
|
|
|
self.cache.predecessors(self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2018-08-04 16:24:39 -06:00
|
|
|
|
pub fn predecessors_for(&self, bb: BasicBlock) -> MappedReadGuard<'_, Vec<BasicBlock>> {
|
|
|
|
|
MappedReadGuard::map(self.predecessors(), |p| &p[bb])
|
2016-06-07 22:02:08 +03:00
|
|
|
|
}
|
2016-06-20 23:55:14 +03:00
|
|
|
|
|
2018-08-30 18:54:32 -03:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn predecessor_locations(&self, loc: Location) -> impl Iterator<Item = Location> + '_ {
|
|
|
|
|
let if_zero_locations = if loc.statement_index == 0 {
|
|
|
|
|
let predecessor_blocks = self.predecessors_for(loc.block);
|
|
|
|
|
let num_predecessor_blocks = predecessor_blocks.len();
|
2018-08-30 15:06:27 -03:00
|
|
|
|
Some(
|
|
|
|
|
(0..num_predecessor_blocks)
|
|
|
|
|
.map(move |i| predecessor_blocks[i])
|
|
|
|
|
.map(move |bb| self.terminator_loc(bb)),
|
2018-08-30 18:54:32 -03:00
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let if_not_zero_locations = if loc.statement_index == 0 {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
2019-07-12 22:49:15 +02:00
|
|
|
|
Some(Location { block: loc.block, statement_index: loc.statement_index - 1 })
|
2018-08-30 18:54:32 -03:00
|
|
|
|
};
|
|
|
|
|
|
2019-07-12 22:49:15 +02:00
|
|
|
|
if_zero_locations.into_iter().flatten().chain(if_not_zero_locations)
|
2018-08-30 18:54:32 -03:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-09 15:49:07 -07:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn dominators(&self) -> Dominators<BasicBlock> {
|
|
|
|
|
dominators(self)
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-18 14:53:55 -07:00
|
|
|
|
/// Returns `true` if a cycle exists in the control-flow graph that is reachable from the
|
|
|
|
|
/// `START_BLOCK`.
|
|
|
|
|
pub fn is_cfg_cyclic(&self) -> bool {
|
|
|
|
|
graph::is_cyclic(self)
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn local_kind(&self, local: Local) -> LocalKind {
|
2018-08-28 12:20:56 -04:00
|
|
|
|
let index = local.as_usize();
|
2016-09-25 01:38:27 +02:00
|
|
|
|
if index == 0 {
|
2018-06-19 21:22:52 -03:00
|
|
|
|
debug_assert!(
|
|
|
|
|
self.local_decls[local].mutability == Mutability::Mut,
|
|
|
|
|
"return place should be mutable"
|
|
|
|
|
);
|
2016-09-25 01:38:27 +02:00
|
|
|
|
|
|
|
|
|
LocalKind::ReturnPointer
|
|
|
|
|
} else if index < self.arg_count + 1 {
|
|
|
|
|
LocalKind::Arg
|
2018-05-16 18:58:54 +03:00
|
|
|
|
} else if self.local_decls[local].is_user_variable() {
|
2016-09-25 01:38:27 +02:00
|
|
|
|
LocalKind::Var
|
|
|
|
|
} else {
|
|
|
|
|
LocalKind::Temp
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns an iterator over all temporaries.
|
|
|
|
|
#[inline]
|
2018-06-19 21:22:52 -03:00
|
|
|
|
pub fn temps_iter<'a>(&'a self) -> impl Iterator<Item = Local> + 'a {
|
|
|
|
|
(self.arg_count + 1..self.local_decls.len()).filter_map(move |index| {
|
2016-09-25 01:38:27 +02:00
|
|
|
|
let local = Local::new(index);
|
2019-11-18 23:04:06 +00:00
|
|
|
|
if self.local_decls[local].is_user_variable() {
|
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]
|
2018-06-19 21:22:52 -03:00
|
|
|
|
pub fn vars_iter<'a>(&'a self) -> impl Iterator<Item = Local> + 'a {
|
|
|
|
|
(self.arg_count + 1..self.local_decls.len()).filter_map(move |index| {
|
2016-09-25 01:38:27 +02:00
|
|
|
|
let local = Local::new(index);
|
2019-11-18 23:04:06 +00:00
|
|
|
|
if self.local_decls[local].is_user_variable() {
|
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-11-07 13:40:55 +01:00
|
|
|
|
/// Returns an iterator over all user-declared mutable locals.
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn mut_vars_iter<'a>(&'a self) -> impl Iterator<Item = Local> + 'a {
|
|
|
|
|
(self.arg_count + 1..self.local_decls.len()).filter_map(move |index| {
|
|
|
|
|
let local = Local::new(index);
|
|
|
|
|
let decl = &self.local_decls[local];
|
2019-11-18 23:04:06 +00:00
|
|
|
|
if decl.is_user_variable() && decl.mutability == Mutability::Mut {
|
2018-11-07 13:40:55 +01:00
|
|
|
|
Some(local)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
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-06-19 21:22:52 -03:00
|
|
|
|
pub fn mut_vars_and_args_iter<'a>(&'a self) -> impl Iterator<Item = Local> + 'a {
|
2018-03-12 08:47:44 -07:00
|
|
|
|
(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];
|
2019-11-18 23:04:06 +00:00
|
|
|
|
if (decl.is_user_variable() || index < self.arg_count + 1)
|
2018-06-19 21:22:52 -03:00
|
|
|
|
&& decl.mutability == Mutability::Mut
|
2018-03-12 08:47:44 -07:00
|
|
|
|
{
|
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]
|
2018-06-19 21:22:52 -03:00
|
|
|
|
pub fn args_iter(&self) -> impl Iterator<Item = Local> {
|
2016-09-26 22:51:51 +02:00
|
|
|
|
let arg_count = self.arg_count;
|
2018-12-04 11:17:58 -08:00
|
|
|
|
(1..=arg_count).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]
|
2018-06-19 21:22:52 -03:00
|
|
|
|
pub fn vars_and_temps_iter(&self) -> impl Iterator<Item = Local> {
|
2016-09-27 02:03:35 +02:00
|
|
|
|
let arg_count = self.arg_count;
|
|
|
|
|
let local_count = self.local_decls.len();
|
2018-06-19 21:22:52 -03:00
|
|
|
|
(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 {
|
2018-10-03 15:07:18 +02:00
|
|
|
|
assert_eq!(idx, stmts.len());
|
2017-12-06 09:25:29 +01:00
|
|
|
|
&block.terminator().source_info
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
|
/// Checks if `sub` is a sub scope of `sup`
|
2018-06-13 14:51:53 -03:00
|
|
|
|
pub fn is_sub_scope(&self, mut sub: SourceScope, sup: SourceScope) -> bool {
|
2018-10-03 15:07:18 +02:00
|
|
|
|
while sub != sup {
|
2018-06-13 14:51:53 -03:00
|
|
|
|
match self.source_scopes[sub].parent_scope {
|
|
|
|
|
None => return false,
|
|
|
|
|
Some(p) => sub = p,
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-03 15:07:18 +02:00
|
|
|
|
true
|
2018-06-13 14:51:53 -03:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// Returns the return type; it always return first element from `local_decls` array.
|
2017-12-06 09:25:29 +01:00
|
|
|
|
pub fn return_ty(&self) -> Ty<'tcx> {
|
|
|
|
|
self.local_decls[RETURN_PLACE].ty
|
|
|
|
|
}
|
2018-08-12 13:07:14 -04:00
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// Gets the location of the terminator for the given block.
|
2018-08-12 13:07:14 -04:00
|
|
|
|
pub fn terminator_loc(&self, bb: BasicBlock) -> Location {
|
2019-07-12 22:49:15 +02:00
|
|
|
|
Location { block: bb, statement_index: self[bb].statements.len() }
|
2018-08-12 13:07:14 -04:00
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-03 01:14:35 +01:00
|
|
|
|
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
|
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
|
2019-02-22 15:48:14 +01:00
|
|
|
|
ExplicitUnsafe(hir::HirId),
|
2017-09-13 22:33:07 +03:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 23:55:04 +02:00
|
|
|
|
impl<'tcx> Index<BasicBlock> for Body<'tcx> {
|
2016-01-07 05:49:46 -05:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 23:55:04 +02:00
|
|
|
|
impl<'tcx> IndexMut<BasicBlock> for Body<'tcx> {
|
2016-01-07 05:49:46 -05:00
|
|
|
|
#[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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-13 21:36:57 +01:00
|
|
|
|
#[derive(Copy, Clone, Debug, HashStable, TypeFoldable)]
|
2017-12-06 09:25:29 +01:00
|
|
|
|
pub enum ClearCrossCrate<T> {
|
2017-09-13 22:33:07 +03:00
|
|
|
|
Clear,
|
2018-06-19 21:22:52 -03:00
|
|
|
|
Set(T),
|
2017-09-13 22:33:07 +03:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-03 06:50:54 -04:00
|
|
|
|
impl<T> ClearCrossCrate<T> {
|
|
|
|
|
pub fn assert_crate_local(self) -> T {
|
|
|
|
|
match self {
|
|
|
|
|
ClearCrossCrate::Clear => bug!("unwrapping cross-crate data"),
|
|
|
|
|
ClearCrossCrate::Set(v) => v,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-23 18:50:47 +03:00
|
|
|
|
impl<T: Encodable> rustc_serialize::UseSpecializedEncodable for ClearCrossCrate<T> {}
|
|
|
|
|
impl<T: Decodable> rustc_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.
|
2019-11-26 22:19:54 -05:00
|
|
|
|
// The unofficial Cranelift backend, at least as of #65828, needs `SourceInfo` to implement `Eq` and
|
2019-10-25 21:11:29 +02:00
|
|
|
|
// `Hash`. Please ping @bjorn3 if removing them.
|
|
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, RustcEncodable, RustcDecodable, Hash, HashStable)]
|
2016-06-07 19:21:56 +03:00
|
|
|
|
pub struct SourceInfo {
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// The source span for the AST pertaining to this MIR entity.
|
2016-06-07 19:21:56 +03:00
|
|
|
|
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.
|
2018-06-19 21:22:52 -03:00
|
|
|
|
pub scope: SourceScope,
|
2016-06-07 19:21:56 +03:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Mutability and borrow kinds
|
|
|
|
|
|
2018-12-03 01:14:35 +01:00
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable)]
|
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 {
|
2019-11-09 17:44:11 +01:00
|
|
|
|
Mutability::Mut => hir::Mutability::Mutable,
|
|
|
|
|
Mutability::Not => hir::Mutability::Immutable,
|
2018-06-07 15:25:08 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 22:49:15 +02:00
|
|
|
|
#[derive(
|
|
|
|
|
Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, RustcEncodable, RustcDecodable, HashStable,
|
|
|
|
|
)]
|
2015-08-18 17:59:21 -04:00
|
|
|
|
pub enum BorrowKind {
|
|
|
|
|
/// Data must be immutable and is aliasable.
|
|
|
|
|
Shared,
|
|
|
|
|
|
2018-09-10 22:33:45 +01:00
|
|
|
|
/// The immediately borrowed place must be immutable, but projections from
|
|
|
|
|
/// it don't need to be. For example, a shallow borrow of `a.b` doesn't
|
|
|
|
|
/// conflict with a mutable borrow of `a.b.c`.
|
|
|
|
|
///
|
|
|
|
|
/// This is used when lowering matches: when matching on a place we want to
|
|
|
|
|
/// ensure that place have the same value from the start of the match until
|
|
|
|
|
/// an arm is selected. This prevents this code from compiling:
|
|
|
|
|
///
|
|
|
|
|
/// let mut x = &Some(0);
|
|
|
|
|
/// match *x {
|
|
|
|
|
/// None => (),
|
|
|
|
|
/// Some(_) if { x = &None; false } => (),
|
|
|
|
|
/// Some(_) => (),
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// This can't be a shared borrow because mutably borrowing (*x as Some).0
|
2018-10-22 18:21:55 +02:00
|
|
|
|
/// should not prevent `if let None = x { ... }`, for example, because the
|
2018-09-10 22:33:45 +01:00
|
|
|
|
/// mutating `(*x as Some).0` can't affect the discriminant of `x`.
|
|
|
|
|
/// We can also report errors with this kind of borrow differently.
|
|
|
|
|
Shallow,
|
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
|
/// Data must be immutable but not aliasable. This kind of borrow
|
2015-08-18 17:59:21 -04:00
|
|
|
|
/// cannot currently be expressed by the user and is used only in
|
2018-08-01 23:53:28 -07:00
|
|
|
|
/// implicit closure bindings. It is needed when the closure is
|
|
|
|
|
/// borrowing or mutating a mutable referent, e.g.:
|
2015-08-18 17:59:21 -04:00
|
|
|
|
///
|
2018-11-04 18:05:54 +01:00
|
|
|
|
/// let x: &mut isize = ...;
|
|
|
|
|
/// let y = || *x += 5;
|
2015-08-18 17:59:21 -04:00
|
|
|
|
///
|
|
|
|
|
/// If we were to try to translate this closure into a more explicit
|
|
|
|
|
/// form, we'd encounter an error with the code as written:
|
|
|
|
|
///
|
2018-11-04 18:05:54 +01:00
|
|
|
|
/// 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; }
|
2015-08-18 17:59:21 -04:00
|
|
|
|
///
|
2018-08-01 23:53:28 -07:00
|
|
|
|
/// This is then illegal because you cannot mutate an `&mut` found
|
2015-08-18 17:59:21 -04:00
|
|
|
|
/// in an aliasable location. To solve, you'd have to translate with
|
|
|
|
|
/// an `&mut` borrow:
|
|
|
|
|
///
|
2018-11-04 18:05:54 +01:00
|
|
|
|
/// 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; }
|
2015-08-18 17:59:21 -04:00
|
|
|
|
///
|
|
|
|
|
/// 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 {
|
2019-02-08 14:53:55 +01:00
|
|
|
|
/// `true` if this borrow arose from method-call auto-ref
|
|
|
|
|
/// (i.e., `adjustment::Adjust::Borrow`).
|
2018-06-19 21:22:52 -03:00
|
|
|
|
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 {
|
2018-09-10 22:33:45 +01:00
|
|
|
|
BorrowKind::Shared | BorrowKind::Shallow | BorrowKind::Unique => false,
|
2018-10-03 15:07:18 +02:00
|
|
|
|
BorrowKind::Mut { allow_two_phase_borrow } => allow_two_phase_borrow,
|
2018-01-16 10:52:52 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Variables and temps
|
|
|
|
|
|
2019-09-26 05:38:33 +00:00
|
|
|
|
rustc_index::newtype_index! {
|
2018-07-25 13:41:32 +03:00
|
|
|
|
pub struct Local {
|
2018-12-03 01:14:35 +01:00
|
|
|
|
derive [HashStable]
|
2017-12-06 09:25:29 +01:00
|
|
|
|
DEBUG_FORMAT = "_{}",
|
|
|
|
|
const RETURN_PLACE = 0,
|
2018-07-25 13:41:32 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-25 01:38:27 +02:00
|
|
|
|
|
2019-07-12 22:48:02 +02:00
|
|
|
|
impl Atom for Local {
|
|
|
|
|
fn index(self) -> usize {
|
|
|
|
|
Idx::index(self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 23:55:04 +02:00
|
|
|
|
/// Classifies locals into categories. See `Body::local_kind`.
|
2018-12-03 01:14:35 +01:00
|
|
|
|
#[derive(PartialEq, Eq, Debug, HashStable)]
|
2016-09-25 01:38:27 +02:00
|
|
|
|
pub enum LocalKind {
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// User-declared variable binding.
|
2016-09-25 01:38:27 +02:00
|
|
|
|
Var,
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// Compiler-introduced temporary.
|
2016-09-25 01:38:27 +02:00
|
|
|
|
Temp,
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// Function argument.
|
2016-09-25 01:38:27 +02:00
|
|
|
|
Arg,
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// Location of function's return value.
|
2016-09-25 01:38:27 +02:00
|
|
|
|
ReturnPointer,
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-09 23:17:42 +01:00
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
|
2018-06-27 22:06:54 +01:00
|
|
|
|
pub struct VarBindingForm<'tcx> {
|
2018-06-07 15:25:08 +02:00
|
|
|
|
/// 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.
|
|
|
|
|
///
|
2019-02-08 14:53:55 +01:00
|
|
|
|
/// NOTE: if you want to change this to a `HirId`, be wary that
|
2018-06-07 15:25:08 +02:00
|
|
|
|
/// 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>,
|
2018-06-27 22:06:54 +01:00
|
|
|
|
/// Place of the RHS of the =, or the subject of the `match` where this
|
|
|
|
|
/// variable is initialized. None in the case of `let PATTERN;`.
|
|
|
|
|
/// Some((None, ..)) in the case of and `let [mut] x = ...` because
|
|
|
|
|
/// (a) the right-hand side isn't evaluated as a place expression.
|
|
|
|
|
/// (b) it gives a way to separate this case from the remaining cases
|
|
|
|
|
/// for diagnostics.
|
|
|
|
|
pub opt_match_place: Option<(Option<Place<'tcx>>, Span)>,
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// The span of the pattern in which this variable was bound.
|
2018-08-07 01:02:39 -07:00
|
|
|
|
pub pat_span: Span,
|
2018-06-07 15:25:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-20 15:54:53 +11:00
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
|
2018-06-27 22:06:54 +01:00
|
|
|
|
pub enum BindingForm<'tcx> {
|
2018-06-07 15:25:08 +02:00
|
|
|
|
/// This is a binding for a non-`self` binding, or a `self` that has an explicit type.
|
2018-06-27 22:06:54 +01:00
|
|
|
|
Var(VarBindingForm<'tcx>),
|
2018-06-07 15:25:08 +02:00
|
|
|
|
/// Binding for a `self`/`&self`/`&mut self` binding where the type is implicit.
|
2018-10-01 17:46:04 +02:00
|
|
|
|
ImplicitSelf(ImplicitSelfKind),
|
2018-07-15 15:00:58 +01:00
|
|
|
|
/// Reference used in a guard expression to ensure immutability.
|
|
|
|
|
RefForGuard,
|
2018-06-07 15:25:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-01 17:46:04 +02:00
|
|
|
|
/// Represents what type of implicit self a function has, if any.
|
2019-11-09 23:17:42 +01:00
|
|
|
|
#[derive(Clone, Copy, PartialEq, Debug, RustcEncodable, RustcDecodable, HashStable)]
|
2018-10-01 17:46:04 +02:00
|
|
|
|
pub enum ImplicitSelfKind {
|
|
|
|
|
/// Represents a `fn x(self);`.
|
|
|
|
|
Imm,
|
|
|
|
|
/// Represents a `fn x(mut self);`.
|
|
|
|
|
Mut,
|
|
|
|
|
/// Represents a `fn x(&self);`.
|
|
|
|
|
ImmRef,
|
|
|
|
|
/// Represents a `fn x(&mut self);`.
|
|
|
|
|
MutRef,
|
|
|
|
|
/// Represents when a function does not have a self argument or
|
|
|
|
|
/// when a function has a `self: X` argument.
|
2019-07-12 22:49:15 +02:00
|
|
|
|
None,
|
2018-10-01 17:46:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-27 22:06:54 +01:00
|
|
|
|
CloneTypeFoldableAndLiftImpls! { BindingForm<'tcx>, }
|
2018-06-07 15:25:08 +02:00
|
|
|
|
|
2018-06-27 22:06:54 +01:00
|
|
|
|
mod binding_form_impl {
|
2019-02-05 11:20:45 -06:00
|
|
|
|
use crate::ich::StableHashingContext;
|
2019-09-26 18:54:39 -04:00
|
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
2018-06-07 15:25:08 +02:00
|
|
|
|
|
2018-06-27 22:06:54 +01:00
|
|
|
|
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for super::BindingForm<'tcx> {
|
2019-09-26 18:54:39 -04:00
|
|
|
|
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
|
2018-06-27 22:06:54 +01:00
|
|
|
|
use super::BindingForm::*;
|
|
|
|
|
::std::mem::discriminant(self).hash_stable(hcx, hasher);
|
|
|
|
|
|
|
|
|
|
match self {
|
|
|
|
|
Var(binding) => binding.hash_stable(hcx, hasher),
|
2018-10-01 17:46:04 +02:00
|
|
|
|
ImplicitSelf(kind) => kind.hash_stable(hcx, hasher),
|
2018-07-15 15:00:58 +01:00
|
|
|
|
RefForGuard => (),
|
2018-06-27 22:06:54 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-07 15:25:08 +02:00
|
|
|
|
|
2018-10-05 12:26:29 +02:00
|
|
|
|
/// `BlockTailInfo` is attached to the `LocalDecl` for temporaries
|
|
|
|
|
/// created during evaluation of expressions in a block tail
|
|
|
|
|
/// expression; that is, a block like `{ STMT_1; STMT_2; EXPR }`.
|
|
|
|
|
///
|
|
|
|
|
/// It is used to improve diagnostics when such temporaries are
|
2018-11-27 02:59:49 +00:00
|
|
|
|
/// involved in borrow_check errors, e.g., explanations of where the
|
2018-10-05 12:26:29 +02:00
|
|
|
|
/// temporaries come from, when their destructors are run, and/or how
|
|
|
|
|
/// one might revise the code to satisfy the borrow checker's rules.
|
2019-11-09 23:17:42 +01:00
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
|
2018-09-22 00:51:48 +02:00
|
|
|
|
pub struct BlockTailInfo {
|
|
|
|
|
/// If `true`, then the value resulting from evaluating this tail
|
|
|
|
|
/// expression is ignored by the block's expression context.
|
|
|
|
|
///
|
|
|
|
|
/// Examples include `{ ...; tail };` and `let _ = { ...; tail };`
|
2018-11-27 02:59:49 +00:00
|
|
|
|
/// but not e.g., `let _x = { ...; tail };`
|
2018-09-22 00:51:48 +02:00
|
|
|
|
pub tail_result_is_ignored: bool,
|
|
|
|
|
}
|
|
|
|
|
|
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.
|
2019-11-13 21:36:57 +01:00
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable, TypeFoldable)]
|
2016-09-25 01:38:27 +02:00
|
|
|
|
pub struct LocalDecl<'tcx> {
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// Whether this is a mutable minding (i.e., `let x` or `let mut x`).
|
2016-09-25 01:38:27 +02:00
|
|
|
|
///
|
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
|
|
|
|
|
2019-11-18 23:04:06 +00:00
|
|
|
|
// FIXME(matthewjasper) Don't store in this in `Body`
|
|
|
|
|
pub local_info: LocalInfo<'tcx>,
|
2017-04-11 23:52:51 +03:00
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
|
/// `true` if this is an internal local.
|
2017-08-24 11:42:32 -07:00
|
|
|
|
///
|
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,
|
|
|
|
|
|
2018-09-22 00:51:48 +02:00
|
|
|
|
/// If this local is a temporary and `is_block_tail` is `Some`,
|
|
|
|
|
/// then it is a temporary created for evaluation of some
|
|
|
|
|
/// subexpression of some block's tail expression (with no
|
|
|
|
|
/// intervening statement context).
|
2019-11-18 23:04:06 +00:00
|
|
|
|
// FIXME(matthewjasper) Don't store in this in `Body`
|
2018-09-22 00:51:48 +02:00
|
|
|
|
pub is_block_tail: Option<BlockTailInfo>,
|
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// The type of this local.
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub ty: Ty<'tcx>,
|
2016-03-23 04:21:02 -04:00
|
|
|
|
|
2018-09-10 10:54:31 -04:00
|
|
|
|
/// If the user manually ascribed a type to this variable,
|
2018-11-27 02:59:49 +00:00
|
|
|
|
/// e.g., via `let x: T`, then we carry that type here. The MIR
|
2018-09-10 10:54:31 -04:00
|
|
|
|
/// borrow checker needs this information since it can affect
|
|
|
|
|
/// region inference.
|
2019-11-18 23:04:06 +00:00
|
|
|
|
// FIXME(matthewjasper) Don't store in this in `Body`
|
2019-03-28 18:00:17 -07:00
|
|
|
|
pub user_ty: UserTypeProjections,
|
2018-09-10 10:54:31 -04:00
|
|
|
|
|
2018-11-27 02:59:49 +00: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-16 18:58:54 +03:00
|
|
|
|
/// for a local. We have the `source_info.scope` represent the "syntactic"
|
|
|
|
|
/// lint scope (with a variable being under its let block) while the
|
|
|
|
|
/// `var_debug_info.source_info.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 }
|
|
|
|
|
/// │
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// │ │{ #[allow(unused_mut)] } // This is actually split into 2 scopes
|
|
|
|
|
/// │ │ // in practice because I'm lazy.
|
2017-12-21 00:35:19 +02:00
|
|
|
|
/// │ │
|
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-16 18:58:54 +03:00
|
|
|
|
/// │ │ │← y.var_debug_info.source_info.scope
|
2017-12-21 00:35:19 +02:00
|
|
|
|
/// │ │ │← `y + 2`
|
|
|
|
|
/// │
|
|
|
|
|
/// │ │{ let x: u32 }
|
2018-05-16 18:58:54 +03:00
|
|
|
|
/// │ │← x.var_debug_info.source_info.scope
|
2019-09-06 03:57:44 +01: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,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-18 23:04:06 +00:00
|
|
|
|
/// Extra information about a local that's used for diagnostics.
|
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable, TypeFoldable)]
|
|
|
|
|
pub enum LocalInfo<'tcx> {
|
|
|
|
|
/// A user-defined local variable or function parameter
|
2019-11-21 21:20:47 +00:00
|
|
|
|
///
|
|
|
|
|
/// The `BindingForm` is solely used for local diagnostics when generating
|
|
|
|
|
/// warnings/errors when compiling the current crate, and therefore it need
|
|
|
|
|
/// not be visible across crates.
|
2019-11-18 23:04:06 +00:00
|
|
|
|
User(ClearCrossCrate<BindingForm<'tcx>>),
|
|
|
|
|
/// A temporary created that references the static with the given `DefId`.
|
|
|
|
|
StaticRef { def_id: DefId, is_thread_local: bool },
|
|
|
|
|
/// Any other temporary, the return place, or an anonymous function parameter.
|
|
|
|
|
Other,
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
|
impl<'tcx> LocalDecl<'tcx> {
|
2019-02-08 14:53:55 +01:00
|
|
|
|
/// Returns `true` only if local is a binding that can itself be
|
2018-06-15 05:47:36 +02:00
|
|
|
|
/// 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) => ... }`
|
2018-06-19 21:22:52 -03:00
|
|
|
|
pub fn can_be_made_mutable(&self) -> bool {
|
2019-11-18 23:04:06 +00:00
|
|
|
|
match self.local_info {
|
|
|
|
|
LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
|
2018-06-15 05:47:36 +02:00
|
|
|
|
binding_mode: ty::BindingMode::BindByValue(_),
|
2019-09-06 21:05:37 +01:00
|
|
|
|
opt_ty_info: _,
|
|
|
|
|
opt_match_place: _,
|
|
|
|
|
pat_span: _,
|
2018-06-15 05:47:36 +02:00
|
|
|
|
}))) => true,
|
|
|
|
|
|
2019-11-18 23:04:06 +00:00
|
|
|
|
LocalInfo::User(
|
|
|
|
|
ClearCrossCrate::Set(BindingForm::ImplicitSelf(ImplicitSelfKind::Imm)),
|
|
|
|
|
) => true,
|
2018-10-01 17:46:04 +02:00
|
|
|
|
|
2018-06-15 05:47:36 +02:00
|
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
|
/// Returns `true` if local is definitely not a `ref ident` or
|
2018-06-15 05:47:36 +02:00
|
|
|
|
/// `ref mut ident` binding. (Such bindings cannot be made into
|
|
|
|
|
/// mutable bindings, but the inverse does not necessarily hold).
|
2018-06-19 21:22:52 -03:00
|
|
|
|
pub fn is_nonref_binding(&self) -> bool {
|
2019-11-18 23:04:06 +00:00
|
|
|
|
match self.local_info {
|
|
|
|
|
LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
|
2018-06-15 05:47:36 +02:00
|
|
|
|
binding_mode: ty::BindingMode::BindByValue(_),
|
2019-09-06 21:05:37 +01:00
|
|
|
|
opt_ty_info: _,
|
|
|
|
|
opt_match_place: _,
|
|
|
|
|
pat_span: _,
|
2018-06-15 05:47:36 +02:00
|
|
|
|
}))) => true,
|
|
|
|
|
|
2019-11-18 23:04:06 +00:00
|
|
|
|
LocalInfo::User(ClearCrossCrate::Set(BindingForm::ImplicitSelf(_))) => true,
|
2018-06-15 05:47:36 +02:00
|
|
|
|
|
|
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-18 23:04:06 +00:00
|
|
|
|
/// Returns `true` if this variable is a named variable or function
|
|
|
|
|
/// parameter declared by the user.
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn is_user_variable(&self) -> bool {
|
|
|
|
|
match self.local_info {
|
|
|
|
|
LocalInfo::User(_) => true,
|
2018-06-15 05:47:36 +02:00
|
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-04 22:26:59 +01:00
|
|
|
|
/// Returns `true` if this is a reference to a variable bound in a `match`
|
|
|
|
|
/// expression that is used to access said variable for the guard of the
|
|
|
|
|
/// match arm.
|
|
|
|
|
pub fn is_ref_for_guard(&self) -> bool {
|
2019-11-18 23:04:06 +00:00
|
|
|
|
match self.local_info {
|
|
|
|
|
LocalInfo::User(ClearCrossCrate::Set(BindingForm::RefForGuard)) => true,
|
|
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns `Some` if this is a reference to a static item that is used to
|
|
|
|
|
/// access that static
|
|
|
|
|
pub fn is_ref_to_static(&self) -> bool {
|
|
|
|
|
match self.local_info {
|
|
|
|
|
LocalInfo::StaticRef { .. } => true,
|
|
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns `Some` if this is a reference to a static item that is used to
|
|
|
|
|
/// access that static
|
|
|
|
|
pub fn is_ref_to_thread_local(&self) -> bool {
|
|
|
|
|
match self.local_info {
|
|
|
|
|
LocalInfo::StaticRef { is_thread_local, .. } => is_thread_local,
|
2019-05-04 22:26:59 +01:00
|
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-03 22:24:52 +01:00
|
|
|
|
/// Returns `true` is the local is from a compiler desugaring, e.g.,
|
|
|
|
|
/// `__next` from a `for` loop.
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn from_compiler_desugaring(&self) -> bool {
|
2019-06-19 01:08:45 +03:00
|
|
|
|
self.source_info.span.desugaring_kind().is_some()
|
2019-05-03 22:24:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
|
/// Creates a new `LocalDecl` for a temporary.
|
2016-09-25 01:38:27 +02:00
|
|
|
|
#[inline]
|
2017-04-11 23:52:51 +03:00
|
|
|
|
pub fn new_temp(ty: Ty<'tcx>, span: Span) -> Self {
|
2018-09-05 23:49:58 +01:00
|
|
|
|
Self::new_local(ty, Mutability::Mut, false, span)
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-22 00:51:48 +02:00
|
|
|
|
/// Converts `self` into same `LocalDecl` except tagged as immutable.
|
2018-09-05 23:49:58 +01:00
|
|
|
|
#[inline]
|
2018-09-22 00:51:48 +02:00
|
|
|
|
pub fn immutable(mut self) -> Self {
|
|
|
|
|
self.mutability = Mutability::Not;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Converts `self` into same `LocalDecl` except tagged as internal temporary.
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn block_tail(mut self, info: BlockTailInfo) -> Self {
|
|
|
|
|
assert!(self.is_block_tail.is_none());
|
|
|
|
|
self.is_block_tail = Some(info);
|
|
|
|
|
self
|
2017-07-15 22:41:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
|
/// Creates a new `LocalDecl` for a internal temporary.
|
2017-07-15 22:41:33 +02:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn new_internal(ty: Ty<'tcx>, span: Span) -> Self {
|
2018-09-05 23:49:58 +01:00
|
|
|
|
Self::new_local(ty, Mutability::Mut, true, span)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2019-07-12 22:49:15 +02:00
|
|
|
|
fn new_local(ty: Ty<'tcx>, mutability: Mutability, internal: bool, span: Span) -> Self {
|
2017-07-15 22:41:33 +02:00
|
|
|
|
LocalDecl {
|
2018-09-05 23:49:58 +01:00
|
|
|
|
mutability,
|
2017-07-15 22:41:33 +02:00
|
|
|
|
ty,
|
2018-10-22 14:23:44 +02:00
|
|
|
|
user_ty: UserTypeProjections::none(),
|
2019-07-12 22:49:15 +02:00
|
|
|
|
source_info: SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE },
|
2018-09-05 23:49:58 +01:00
|
|
|
|
internal,
|
2019-11-18 23:04:06 +00:00
|
|
|
|
local_info: LocalInfo::Other,
|
2018-09-22 00:51:48 +02:00
|
|
|
|
is_block_tail: 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]
|
2018-08-29 22:02:42 -07: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-10-22 14:23:44 +02:00
|
|
|
|
user_ty: UserTypeProjections::none(),
|
2019-07-12 22:49:15 +02:00
|
|
|
|
source_info: SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE },
|
2017-07-15 22:41:33 +02:00
|
|
|
|
internal: false,
|
2018-09-22 00:51:48 +02:00
|
|
|
|
is_block_tail: None,
|
2019-11-18 23:04:06 +00:00
|
|
|
|
local_info: LocalInfo::Other,
|
2016-09-25 01:38:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-04-16 21:51:26 +03:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-16 18:58:54 +03:00
|
|
|
|
/// Debug information pertaining to a user variable.
|
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable, TypeFoldable)]
|
|
|
|
|
pub struct VarDebugInfo<'tcx> {
|
|
|
|
|
pub name: Name,
|
2016-04-16 21:51:26 +03:00
|
|
|
|
|
2018-05-16 18:58:54 +03:00
|
|
|
|
/// Source info of the user variable, including the scope
|
|
|
|
|
/// within which the variable is visible (to debuginfo)
|
|
|
|
|
/// (see `LocalDecl`'s `source_info` field for more details).
|
|
|
|
|
pub source_info: SourceInfo,
|
|
|
|
|
|
|
|
|
|
/// Where the data for this user variable is to be found.
|
|
|
|
|
/// NOTE(eddyb) There's an unenforced invariant that this `Place` is
|
|
|
|
|
/// based on a `Local`, not a `Static`, and contains no indexing.
|
|
|
|
|
pub place: Place<'tcx>,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// BasicBlock
|
|
|
|
|
|
2019-09-26 05:38:33 +00:00
|
|
|
|
rustc_index::newtype_index! {
|
2018-07-25 13:41:32 +03:00
|
|
|
|
pub struct BasicBlock {
|
2018-12-03 01:14:35 +01:00
|
|
|
|
derive [HashStable]
|
2018-08-28 12:20:56 -04:00
|
|
|
|
DEBUG_FORMAT = "bb{}",
|
|
|
|
|
const START_BLOCK = 0,
|
2018-07-25 13:41:32 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-06 09:25:29 +01:00
|
|
|
|
|
|
|
|
|
impl BasicBlock {
|
|
|
|
|
pub fn start_location(self) -> Location {
|
2019-07-12 22:49:15 +02:00
|
|
|
|
Location { block: self, statement_index: 0 }
|
2017-12-06 09:25:29 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
|
|
2019-11-13 21:36:57 +01:00
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable, TypeFoldable)]
|
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.
|
|
|
|
|
///
|
2019-02-08 14:53:55 +01:00
|
|
|
|
/// N.B., this should generally ONLY be `None` during construction.
|
2016-03-24 06:12:19 -04:00
|
|
|
|
/// 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
|
|
|
|
}
|
|
|
|
|
|
2018-12-03 01:14:35 +01:00
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
|
2016-03-10 09:55:15 -05:00
|
|
|
|
pub struct Terminator<'tcx> {
|
2016-06-07 19:21:56 +03:00
|
|
|
|
pub source_info: SourceInfo,
|
2018-06-19 21:22:52 -03:00
|
|
|
|
pub kind: TerminatorKind<'tcx>,
|
2016-03-10 09:55:15 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-10 10:26:33 +01:00
|
|
|
|
#[derive(Clone, RustcEncodable, RustcDecodable, HashStable, PartialEq)]
|
2016-03-10 09:55:15 -05:00
|
|
|
|
pub enum TerminatorKind<'tcx> {
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// Block should have one successor in the graph; we jump there.
|
2018-06-19 21:22:52 -03:00
|
|
|
|
Goto { target: BasicBlock },
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// Operand evaluates to an integer; jump depending on its value
|
|
|
|
|
/// to one of the targets, and otherwise fallback to `otherwise`.
|
2015-10-26 14:35:18 -04:00
|
|
|
|
SwitchInt {
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// The discriminant value being tested.
|
2017-01-31 05:32:08 +02:00
|
|
|
|
discr: Operand<'tcx>,
|
2015-10-26 14:35:18 -04:00
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// The type of value being tested.
|
2015-10-26 14:35:18 -04:00
|
|
|
|
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.
|
2019-09-06 03:57:44 +01:00
|
|
|
|
//
|
2017-02-02 20:35:54 +02:00
|
|
|
|
// 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
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// continue. Emitted by `build::scope::diverge_cleanup`.
|
2015-12-15 20:46:39 +02:00
|
|
|
|
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,
|
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// Drop the `Place`.
|
2019-07-12 22:49:15 +02:00
|
|
|
|
Drop { location: Place<'tcx>, target: BasicBlock, unwind: Option<BasicBlock> },
|
2016-01-30 00:18:47 +02:00
|
|
|
|
|
2019-09-06 03:57:44 +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
|
2018-08-01 23:53:28 -07:00
|
|
|
|
/// place unwinds. Its semantics are best explained by the
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// 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-08-19 15:30:23 +02:00
|
|
|
|
/// // P is now uninitialized
|
2018-01-29 01:49:29 +02:00
|
|
|
|
/// P <- V
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// }
|
|
|
|
|
/// BB2 {
|
2018-08-19 15:30:23 +02:00
|
|
|
|
/// // P is now uninitialized -- its dtor panicked
|
2018-01-29 01:49:29 +02:00
|
|
|
|
/// 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>,
|
|
|
|
|
},
|
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// Block ends with a call of a converging function.
|
2015-10-07 14:37:42 +02:00
|
|
|
|
Call {
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// The function that’s being called.
|
2015-12-14 23:27:58 +02:00
|
|
|
|
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.
|
2018-06-19 21:22:52 -03:00
|
|
|
|
cleanup: Option<BasicBlock>,
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// `true` if this is from a call in HIR rather than from an overloaded
|
2018-09-29 10:34:12 +01:00
|
|
|
|
/// operator. True for overloaded function call.
|
|
|
|
|
from_hir_call: bool,
|
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,
|
2018-06-19 21:22:52 -03:00
|
|
|
|
cleanup: Option<BasicBlock>,
|
2016-12-26 14:34:03 +01:00
|
|
|
|
},
|
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// A suspend point.
|
2017-07-10 21:11:31 +02:00
|
|
|
|
Yield {
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// The value to return.
|
2016-12-26 14:34:03 +01:00
|
|
|
|
value: Operand<'tcx>,
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// Where to resume to.
|
2016-12-26 14:34:03 +01:00
|
|
|
|
resume: BasicBlock,
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// Cleanup to be done if the generator is dropped at this suspend point.
|
2016-12-26 14:34:03 +01:00
|
|
|
|
drop: Option<BasicBlock>,
|
|
|
|
|
},
|
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// Indicates the end of the dropping of a generator.
|
2016-12-26 14:34:03 +01:00
|
|
|
|
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 {
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// The target normal control flow will take.
|
2017-12-06 09:25:29 +01:00
|
|
|
|
real_target: BasicBlock,
|
2019-05-25 18:26:34 +01:00
|
|
|
|
/// A block control flow could conceptually jump to, but won't in
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// practice.
|
2019-05-06 15:18:57 +01:00
|
|
|
|
imaginary_target: BasicBlock,
|
2018-01-25 01:45:45 -05:00
|
|
|
|
},
|
|
|
|
|
/// 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 {
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// The target normal control flow will take.
|
2018-01-25 01:45:45 -05:00
|
|
|
|
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
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// 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.
|
2018-01-25 01:45:45 -05:00
|
|
|
|
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-08-29 22:02:42 -07:00
|
|
|
|
pub fn successors(&self) -> Successors<'_> {
|
2016-03-10 09:55:15 -05:00
|
|
|
|
self.kind.successors()
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-29 22:02:42 -07: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
|
|
|
|
|
2018-07-01 19:43:01 -03:00
|
|
|
|
pub fn unwind(&self) -> Option<&Option<BasicBlock>> {
|
|
|
|
|
self.kind.unwind()
|
|
|
|
|
}
|
|
|
|
|
|
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> {
|
2019-06-14 00:48:52 +03:00
|
|
|
|
pub fn if_(
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-06-19 21:22:52 -03:00
|
|
|
|
cond: Operand<'tcx>,
|
|
|
|
|
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-08-29 22:02:42 -07: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-06-19 21:22:52 -03:00
|
|
|
|
Resume
|
|
|
|
|
| Abort
|
|
|
|
|
| GeneratorDrop
|
|
|
|
|
| Return
|
|
|
|
|
| Unreachable
|
2019-07-12 22:49:15 +02:00
|
|
|
|
| Call { destination: None, cleanup: None, .. } => None.into_iter().chain(&[]),
|
2018-06-19 21:22:52 -03:00
|
|
|
|
Goto { target: ref t }
|
2019-07-12 22:49:15 +02:00
|
|
|
|
| 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(&[]),
|
|
|
|
|
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))
|
2018-06-19 21:22:52 -03:00
|
|
|
|
}
|
|
|
|
|
SwitchInt { ref targets, .. } => None.into_iter().chain(&targets[..]),
|
2019-07-12 22:49:15 +02:00
|
|
|
|
FalseEdges { ref real_target, ref imaginary_target } => {
|
|
|
|
|
Some(real_target).into_iter().chain(slice::from_ref(imaginary_target))
|
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-10 21:38:36 +01:00
|
|
|
|
|
2018-08-29 22:02:42 -07: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-06-19 21:22:52 -03:00
|
|
|
|
Resume
|
|
|
|
|
| Abort
|
|
|
|
|
| GeneratorDrop
|
|
|
|
|
| Return
|
|
|
|
|
| Unreachable
|
2019-07-12 22:49:15 +02:00
|
|
|
|
| Call { destination: None, cleanup: None, .. } => None.into_iter().chain(&mut []),
|
2018-06-19 21:22:52 -03:00
|
|
|
|
Goto { target: ref mut t }
|
2019-07-12 22:49:15 +02:00
|
|
|
|
| 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 [])
|
2016-05-17 01:06:52 +03:00
|
|
|
|
}
|
2019-07-12 22:49:15 +02:00
|
|
|
|
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) } => {
|
|
|
|
|
Some(t).into_iter().chain(slice::from_mut(u))
|
2017-12-06 09:25:29 +01:00
|
|
|
|
}
|
2019-07-12 22:49:15 +02:00
|
|
|
|
SwitchInt { ref mut targets, .. } => None.into_iter().chain(&mut targets[..]),
|
|
|
|
|
FalseEdges { ref mut real_target, ref mut imaginary_target } => {
|
|
|
|
|
Some(real_target).into_iter().chain(slice::from_mut(imaginary_target))
|
2018-06-19 21:22:52 -03:00
|
|
|
|
}
|
2017-12-06 09:25:29 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-01 19:43:01 -03:00
|
|
|
|
pub fn unwind(&self) -> Option<&Option<BasicBlock>> {
|
|
|
|
|
match *self {
|
|
|
|
|
TerminatorKind::Goto { .. }
|
|
|
|
|
| TerminatorKind::Resume
|
|
|
|
|
| TerminatorKind::Abort
|
|
|
|
|
| TerminatorKind::Return
|
|
|
|
|
| TerminatorKind::Unreachable
|
|
|
|
|
| TerminatorKind::GeneratorDrop
|
|
|
|
|
| TerminatorKind::Yield { .. }
|
|
|
|
|
| TerminatorKind::SwitchInt { .. }
|
|
|
|
|
| TerminatorKind::FalseEdges { .. } => None,
|
2019-07-12 22:49:15 +02:00
|
|
|
|
TerminatorKind::Call { cleanup: ref unwind, .. }
|
|
|
|
|
| TerminatorKind::Assert { cleanup: ref unwind, .. }
|
2018-07-01 19:43:01 -03:00
|
|
|
|
| TerminatorKind::DropAndReplace { ref unwind, .. }
|
|
|
|
|
| TerminatorKind::Drop { ref unwind, .. }
|
|
|
|
|
| TerminatorKind::FalseUnwind { ref unwind, .. } => Some(unwind),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
pub fn unwind_mut(&mut self) -> Option<&mut Option<BasicBlock>> {
|
|
|
|
|
match *self {
|
2018-06-19 21:22:52 -03:00
|
|
|
|
TerminatorKind::Goto { .. }
|
|
|
|
|
| TerminatorKind::Resume
|
|
|
|
|
| TerminatorKind::Abort
|
|
|
|
|
| TerminatorKind::Return
|
|
|
|
|
| TerminatorKind::Unreachable
|
|
|
|
|
| TerminatorKind::GeneratorDrop
|
|
|
|
|
| TerminatorKind::Yield { .. }
|
|
|
|
|
| TerminatorKind::SwitchInt { .. }
|
|
|
|
|
| TerminatorKind::FalseEdges { .. } => None,
|
2019-07-12 22:49:15 +02:00
|
|
|
|
TerminatorKind::Call { cleanup: ref mut unwind, .. }
|
|
|
|
|
| TerminatorKind::Assert { cleanup: ref mut unwind, .. }
|
2018-06-19 21:22:52 -03:00
|
|
|
|
| TerminatorKind::DropAndReplace { ref mut unwind, .. }
|
|
|
|
|
| TerminatorKind::Drop { ref mut unwind, .. }
|
|
|
|
|
| TerminatorKind::FalseUnwind { ref mut unwind, .. } => 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> {
|
2019-07-12 22:49:15 +02:00
|
|
|
|
BasicBlockData { statements: vec![], terminator, 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
|
|
|
|
|
2018-06-19 21:22:52 -03:00
|
|
|
|
pub fn retain_statements<F>(&mut self, mut f: F)
|
|
|
|
|
where
|
2018-08-29 22:02:42 -07:00
|
|
|
|
F: FnMut(&mut Statement<'_>) -> bool,
|
2018-06-19 21:22:52 -03:00
|
|
|
|
{
|
2016-12-26 14:34:03 +01:00
|
|
|
|
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)
|
2018-06-19 21:22:52 -03:00
|
|
|
|
where
|
|
|
|
|
F: FnMut(&mut Statement<'tcx>) -> Option<I>,
|
|
|
|
|
I: iter::TrustedLen<Item = Statement<'tcx>>,
|
2018-02-16 19:20:18 +02:00
|
|
|
|
{
|
|
|
|
|
// 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.
|
2018-06-19 21:22:52 -03:00
|
|
|
|
let mut gap = self.statements.len()..self.statements.len() + extra_stmts;
|
|
|
|
|
self.statements.resize(
|
|
|
|
|
gap.end,
|
|
|
|
|
Statement {
|
2019-07-12 22:49:15 +02:00
|
|
|
|
source_info: SourceInfo { span: DUMMY_SP, scope: OUTERMOST_SOURCE_SCOPE },
|
2018-06-19 21:22:52 -03:00
|
|
|
|
kind: StatementKind::Nop,
|
2018-02-16 19:20:18 +02:00
|
|
|
|
},
|
2018-06-19 21:22:52 -03:00
|
|
|
|
);
|
2018-02-16 19:20:18 +02:00
|
|
|
|
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> {
|
2018-08-29 22:02:42 -07: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> {
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// Writes 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"),
|
2019-07-12 22:49:15 +02: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),
|
2019-07-12 22:49:15 +02:00
|
|
|
|
DropAndReplace { ref location, ref value, .. } => {
|
|
|
|
|
write!(fmt, "replace({:?} <- {:?})", location, value)
|
|
|
|
|
}
|
|
|
|
|
Call { ref func, ref args, ref destination, .. } => {
|
2016-01-29 20:42:02 +02:00
|
|
|
|
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
|
|
|
|
}
|
2019-07-12 22:49:15 +02:00
|
|
|
|
Assert { ref cond, expected, ref msg, .. } => {
|
2016-05-25 08:39:32 +03:00
|
|
|
|
write!(fmt, "assert(")?;
|
|
|
|
|
if !expected {
|
|
|
|
|
write!(fmt, "!")?;
|
|
|
|
|
}
|
2018-05-01 12:26:58 +02:00
|
|
|
|
write!(fmt, "{:?}, \"{:?}\")", cond, msg)
|
2018-06-19 21:22:52 -03: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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
|
/// Returns 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()],
|
2019-07-12 22:49:15 +02:00
|
|
|
|
SwitchInt { ref values, switch_ty, .. } => ty::tls::with(|tcx| {
|
|
|
|
|
let param_env = ty::ParamEnv::empty();
|
2019-09-25 15:40:21 -04:00
|
|
|
|
let switch_ty = tcx.lift(&switch_ty).unwrap();
|
2019-07-12 22:49:15 +02:00
|
|
|
|
let size = tcx.layout_of(param_env.and(switch_ty)).unwrap().size;
|
|
|
|
|
values
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|&u| {
|
2019-11-08 21:32:56 +01:00
|
|
|
|
ty::Const::from_scalar(
|
|
|
|
|
tcx,
|
|
|
|
|
Scalar::from_uint(u, size).into(),
|
|
|
|
|
switch_ty,
|
|
|
|
|
)
|
2019-07-12 22:49:15 +02:00
|
|
|
|
.to_string()
|
|
|
|
|
.into()
|
|
|
|
|
})
|
|
|
|
|
.chain(iter::once("otherwise".into()))
|
|
|
|
|
.collect()
|
|
|
|
|
}),
|
|
|
|
|
Call { destination: Some(_), cleanup: Some(_), .. } => {
|
|
|
|
|
vec!["return".into(), "unwind".into()]
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
2019-07-12 22:49:15 +02:00
|
|
|
|
Call { destination: Some(_), cleanup: None, .. } => vec!["return".into()],
|
|
|
|
|
Call { destination: None, cleanup: Some(_), .. } => vec!["unwind".into()],
|
|
|
|
|
Call { destination: None, cleanup: None, .. } => vec![],
|
2018-10-03 15:07:18 +02:00
|
|
|
|
Yield { drop: Some(_), .. } => vec!["resume".into(), "drop".into()],
|
|
|
|
|
Yield { drop: None, .. } => vec!["resume".into()],
|
2018-06-19 21:22:52 -03:00
|
|
|
|
DropAndReplace { unwind: None, .. } | Drop { unwind: None, .. } => {
|
2018-10-03 15:07:18 +02:00
|
|
|
|
vec!["return".into()]
|
2018-06-19 21:22:52 -03:00
|
|
|
|
}
|
2019-07-12 22:49:15 +02:00
|
|
|
|
DropAndReplace { unwind: Some(_), .. } | Drop { unwind: Some(_), .. } => {
|
|
|
|
|
vec!["return".into(), "unwind".into()]
|
2016-05-17 01:06:52 +03:00
|
|
|
|
}
|
2016-05-25 08:39:32 +03:00
|
|
|
|
Assert { cleanup: None, .. } => vec!["".into()],
|
2018-10-03 15:07:18 +02:00
|
|
|
|
Assert { .. } => vec!["success".into(), "unwind".into()],
|
2019-07-12 22:49:15 +02:00
|
|
|
|
FalseEdges { .. } => vec!["real".into(), "imaginary".into()],
|
|
|
|
|
FalseUnwind { unwind: Some(_), .. } => vec!["real".into(), "cleanup".into()],
|
2018-01-25 01:45:45 -05:00
|
|
|
|
FalseUnwind { unwind: None, .. } => vec!["real".into()],
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Statements
|
|
|
|
|
|
2019-11-13 21:36:57 +01:00
|
|
|
|
#[derive(Clone, RustcEncodable, RustcDecodable, HashStable, TypeFoldable)]
|
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
|
|
|
|
}
|
|
|
|
|
|
2018-11-09 10:19:51 +11:00
|
|
|
|
// `Statement` is used a lot. Make sure it doesn't unintentionally get bigger.
|
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2019-09-11 16:05:45 -03:00
|
|
|
|
static_assert_size!(Statement<'_>, 32);
|
2018-11-09 10:19:51 +11:00
|
|
|
|
|
2019-08-24 14:16:56 -07:00
|
|
|
|
impl Statement<'_> {
|
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_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,
|
2018-06-19 21:22:52 -03:00
|
|
|
|
kind: mem::replace(&mut self.kind, StatementKind::Nop),
|
2018-02-16 19:20:18 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-15 18:17:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-10 10:26:33 +01:00
|
|
|
|
#[derive(Clone, Debug, PartialEq, RustcEncodable, RustcDecodable, HashStable, TypeFoldable)]
|
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.
|
2019-09-11 16:05:45 -03:00
|
|
|
|
Assign(Box<(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
|
2018-11-27 02:59:49 +00:00
|
|
|
|
/// (e.g., inspecting constants and discriminant values), and the
|
2018-09-14 21:05:31 +02:00
|
|
|
|
/// kind of pattern it comes from. This is in order to adapt potential
|
|
|
|
|
/// error messages to these specific patterns.
|
2018-11-22 14:33:08 +01:00
|
|
|
|
///
|
2018-12-12 13:09:36 +01:00
|
|
|
|
/// Note that this also is emitted for regular `let` bindings to ensure that locals that are
|
2019-02-08 14:53:55 +01:00
|
|
|
|
/// never accessed still get some sanity checks for, e.g., `let x: ! = ..;`
|
2019-09-11 16:05:45 -03:00
|
|
|
|
FakeRead(FakeReadCause, Box<Place<'tcx>>),
|
2018-05-04 12:04:33 +02:00
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// Write the discriminant for a variant to the enum Place.
|
2019-09-11 16:05:45 -03:00
|
|
|
|
SetDiscriminant { place: Box<Place<'tcx>>, variant_index: VariantIdx },
|
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
|
|
|
|
|
2019-04-02 20:07:09 +11:00
|
|
|
|
/// Executes a piece of inline Assembly. Stored in a Box to keep the size
|
|
|
|
|
/// of `StatementKind` low.
|
|
|
|
|
InlineAsm(Box<InlineAsm<'tcx>>),
|
2017-02-15 21:21:36 +02:00
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
|
/// Retag references in the given place, ensuring they got fresh tags. This is
|
2018-10-24 21:59:42 +02:00
|
|
|
|
/// part of the Stacked Borrows model. These statements are currently only interpreted
|
|
|
|
|
/// by miri and only generated when "-Z mir-emit-retag" is passed.
|
2018-10-24 11:47:17 +02:00
|
|
|
|
/// See <https://internals.rust-lang.org/t/stacked-borrows-an-aliasing-model-for-rust/8153/>
|
|
|
|
|
/// for more details.
|
2019-09-11 16:05:45 -03:00
|
|
|
|
Retag(RetagKind, Box<Place<'tcx>>),
|
2018-11-06 11:04:10 +01:00
|
|
|
|
|
2018-08-31 18:59:35 -04:00
|
|
|
|
/// Encodes a user's type ascription. These need to be preserved
|
|
|
|
|
/// intact so that NLL can respect them. For example:
|
2018-02-23 20:52:05 +00:00
|
|
|
|
///
|
2018-08-31 18:59:35 -04:00
|
|
|
|
/// let a: T = y;
|
2018-02-23 20:52:05 +00:00
|
|
|
|
///
|
2018-09-05 15:52:01 -04:00
|
|
|
|
/// The effect of this annotation is to relate the type `T_y` of the place `y`
|
|
|
|
|
/// to the user-given type `T`. The effect depends on the specified variance:
|
|
|
|
|
///
|
|
|
|
|
/// - `Covariant` -- requires that `T_y <: T`
|
|
|
|
|
/// - `Contravariant` -- requires that `T_y :> T`
|
|
|
|
|
/// - `Invariant` -- requires that `T_y == T`
|
|
|
|
|
/// - `Bivariant` -- no effect
|
2019-09-11 16:05:45 -03:00
|
|
|
|
AscribeUserType(Box<(Place<'tcx>, UserTypeProjection)>, ty::Variance),
|
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
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// Describes what kind of retag is to be performed.
|
2018-12-03 01:14:35 +01:00
|
|
|
|
#[derive(Copy, Clone, RustcEncodable, RustcDecodable, Debug, PartialEq, Eq, HashStable)]
|
2018-12-11 19:54:38 +01:00
|
|
|
|
pub enum RetagKind {
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// The initial retag when entering a function.
|
2018-12-11 19:54:38 +01:00
|
|
|
|
FnEntry,
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// Retag preparing for a two-phase borrow.
|
2018-12-11 19:54:38 +01:00
|
|
|
|
TwoPhase,
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// Retagging raw pointers.
|
2018-12-11 19:54:38 +01:00
|
|
|
|
Raw,
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// A "normal" retag.
|
2018-12-11 19:54:38 +01:00
|
|
|
|
Default,
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// The `FakeReadCause` describes the type of pattern why a FakeRead statement exists.
|
2019-11-10 10:26:33 +01:00
|
|
|
|
#[derive(Copy, Clone, RustcEncodable, RustcDecodable, Debug, HashStable, PartialEq)]
|
2018-09-14 21:05:31 +02:00
|
|
|
|
pub enum FakeReadCause {
|
2019-02-02 16:38:12 +00:00
|
|
|
|
/// Inject a fake read of the borrowed input at the end of each guards
|
|
|
|
|
/// code.
|
2018-09-14 21:54:45 +02:00
|
|
|
|
///
|
2019-02-02 16:38:12 +00:00
|
|
|
|
/// This should ensure that you cannot change the variant for an enum while
|
|
|
|
|
/// you are in the midst of matching on it.
|
2018-09-13 21:35:24 +01:00
|
|
|
|
ForMatchGuard,
|
|
|
|
|
|
|
|
|
|
/// `let x: !; match x {}` doesn't generate any read of x so we need to
|
|
|
|
|
/// generate a read of x to check that it is initialized and safe.
|
|
|
|
|
ForMatchedPlace,
|
2018-09-14 21:54:45 +02:00
|
|
|
|
|
2019-02-02 16:38:12 +00:00
|
|
|
|
/// A fake read of the RefWithinGuard version of a bind-by-value variable
|
|
|
|
|
/// in a match guard to ensure that it's value hasn't change by the time
|
|
|
|
|
/// we create the OutsideGuard version.
|
|
|
|
|
ForGuardBinding,
|
|
|
|
|
|
2018-09-14 21:54:45 +02:00
|
|
|
|
/// Officially, the semantics of
|
|
|
|
|
///
|
|
|
|
|
/// `let pattern = <expr>;`
|
|
|
|
|
///
|
|
|
|
|
/// is that `<expr>` is evaluated into a temporary and then this temporary is
|
|
|
|
|
/// into the pattern.
|
|
|
|
|
///
|
|
|
|
|
/// However, if we see the simple pattern `let var = <expr>`, we optimize this to
|
|
|
|
|
/// evaluate `<expr>` directly into the variable `var`. This is mostly unobservable,
|
|
|
|
|
/// but in some cases it can affect the borrow checker, as in #53695.
|
|
|
|
|
/// Therefore, we insert a "fake read" here to ensure that we get
|
|
|
|
|
/// appropriate errors.
|
2018-09-14 21:05:31 +02:00
|
|
|
|
ForLet,
|
2019-10-19 21:00:21 +01:00
|
|
|
|
|
|
|
|
|
/// If we have an index expression like
|
|
|
|
|
///
|
|
|
|
|
/// (*x)[1][{ x = y; 4}]
|
|
|
|
|
///
|
|
|
|
|
/// then the first bounds check is invalidated when we evaluate the second
|
|
|
|
|
/// index expression. Thus we create a fake borrow of `x` across the second
|
|
|
|
|
/// indexer, which will cause a borrow check error.
|
|
|
|
|
ForIndex,
|
2018-09-14 21:05:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-10 10:26:33 +01:00
|
|
|
|
#[derive(Clone, Debug, PartialEq, RustcEncodable, RustcDecodable, HashStable, TypeFoldable)]
|
2019-04-02 20:07:09 +11:00
|
|
|
|
pub struct InlineAsm<'tcx> {
|
2019-11-18 14:43:34 +01:00
|
|
|
|
pub asm: hir::InlineAsmInner,
|
2019-04-02 20:07:09 +11:00
|
|
|
|
pub outputs: Box<[Place<'tcx>]>,
|
|
|
|
|
pub inputs: Box<[(Span, Operand<'tcx>)]>,
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-24 14:16:56 -07:00
|
|
|
|
impl Debug for Statement<'_> {
|
2018-08-29 22:02:42 -07:00
|
|
|
|
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
|
2015-08-18 17:59:21 -04:00
|
|
|
|
use self::StatementKind::*;
|
|
|
|
|
match self.kind {
|
2019-09-11 16:05:45 -03:00
|
|
|
|
Assign(box(ref place, ref rv)) => write!(fmt, "{:?} = {:?}", place, rv),
|
2018-09-14 21:05:31 +02:00
|
|
|
|
FakeRead(ref cause, ref place) => write!(fmt, "FakeRead({:?}, {:?})", cause, place),
|
2019-07-12 22:49:15 +02:00
|
|
|
|
Retag(ref kind, ref place) => write!(
|
|
|
|
|
fmt,
|
|
|
|
|
"Retag({}{:?})",
|
|
|
|
|
match kind {
|
|
|
|
|
RetagKind::FnEntry => "[fn entry] ",
|
|
|
|
|
RetagKind::TwoPhase => "[2phase] ",
|
|
|
|
|
RetagKind::Raw => "[raw] ",
|
|
|
|
|
RetagKind::Default => "",
|
|
|
|
|
},
|
|
|
|
|
place,
|
|
|
|
|
),
|
2017-12-06 09:25:29 +01:00
|
|
|
|
StorageLive(ref place) => write!(fmt, "StorageLive({:?})", place),
|
|
|
|
|
StorageDead(ref place) => write!(fmt, "StorageDead({:?})", place),
|
2019-07-12 22:49:15 +02:00
|
|
|
|
SetDiscriminant { ref place, variant_index } => {
|
|
|
|
|
write!(fmt, "discriminant({:?}) = {:?}", place, variant_index)
|
|
|
|
|
}
|
|
|
|
|
InlineAsm(ref asm) => {
|
|
|
|
|
write!(fmt, "asm!({:?} : {:?} : {:?})", asm.asm, asm.outputs, asm.inputs)
|
|
|
|
|
}
|
2019-09-11 16:05:45 -03:00
|
|
|
|
AscribeUserType(box(ref place, ref c_ty), ref variance) => {
|
2018-09-05 15:52:01 -04:00
|
|
|
|
write!(fmt, "AscribeUserType({:?}, {:?}, {:?})", place, variance, c_ty)
|
2018-06-19 21:22:52 -03:00
|
|
|
|
}
|
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.
|
2019-07-12 22:49:15 +02:00
|
|
|
|
#[derive(
|
2019-10-20 16:11:04 -04:00
|
|
|
|
Clone, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, HashStable,
|
2019-07-12 22:49:15 +02:00
|
|
|
|
)]
|
2019-04-30 18:58:24 +02:00
|
|
|
|
pub struct Place<'tcx> {
|
|
|
|
|
pub base: PlaceBase<'tcx>,
|
2019-02-22 05:24:03 +01:00
|
|
|
|
|
|
|
|
|
/// projection out of a place (access a field, deref a pointer, etc)
|
2019-10-20 16:11:04 -04:00
|
|
|
|
pub projection: &'tcx List<PlaceElem<'tcx>>,
|
2019-02-22 05:24:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-20 16:11:04 -04:00
|
|
|
|
impl<'tcx> rustc_serialize::UseSpecializedDecodable for Place<'tcx> {}
|
|
|
|
|
|
2019-07-12 22:49:15 +02:00
|
|
|
|
#[derive(
|
|
|
|
|
Clone, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable, HashStable,
|
|
|
|
|
)]
|
2019-02-22 05:24:03 +01:00
|
|
|
|
pub enum PlaceBase<'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
|
|
|
|
}
|
|
|
|
|
|
2019-03-23 18:29:02 +05:30
|
|
|
|
/// We store the normalized type to avoid requiring normalization when reading MIR
|
2019-11-09 23:17:42 +01:00
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash,
|
|
|
|
|
RustcEncodable, RustcDecodable, HashStable)]
|
2017-03-01 02:29:57 +02:00
|
|
|
|
pub struct Static<'tcx> {
|
|
|
|
|
pub ty: Ty<'tcx>,
|
2019-08-05 21:11:55 -04:00
|
|
|
|
pub kind: StaticKind<'tcx>,
|
2019-08-26 21:07:58 -04:00
|
|
|
|
/// The `DefId` of the item this static was declared in. For promoted values, usually, this is
|
|
|
|
|
/// the same as the `DefId` of the `mir::Body` containing the `Place` this promoted appears in.
|
|
|
|
|
/// However, after inlining, that might no longer be the case as inlined `Place`s are copied
|
|
|
|
|
/// into the calling frame.
|
2019-08-05 21:11:55 -04:00
|
|
|
|
pub def_id: DefId,
|
2019-03-23 18:29:02 +05:30
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 22:49:15 +02:00
|
|
|
|
#[derive(
|
2019-08-16 20:31:28 -04:00
|
|
|
|
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable, RustcEncodable, RustcDecodable,
|
2019-07-12 22:49:15 +02:00
|
|
|
|
)]
|
2019-08-05 21:11:55 -04:00
|
|
|
|
pub enum StaticKind<'tcx> {
|
2019-08-26 21:07:58 -04:00
|
|
|
|
/// Promoted references consist of an id (`Promoted`) and the substs necessary to monomorphize
|
|
|
|
|
/// it. Usually, these substs are just the identity substs for the item. However, the inliner
|
|
|
|
|
/// will adjust these substs when it inlines a function based on the substs at the callsite.
|
2019-08-05 21:11:55 -04:00
|
|
|
|
Promoted(Promoted, SubstsRef<'tcx>),
|
|
|
|
|
Static,
|
2017-03-01 02:29:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 21:00:21 +01:00
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
|
#[derive(RustcEncodable, RustcDecodable, HashStable)]
|
2019-03-28 18:00:17 -07:00
|
|
|
|
pub enum ProjectionElem<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.
|
2019-03-28 18:00:17 -07:00
|
|
|
|
///
|
|
|
|
|
/// The included Symbol is the name of the variant, used for printing MIR.
|
|
|
|
|
Downcast(Option<Symbol>, VariantIdx),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-29 13:22:16 -07:00
|
|
|
|
impl<V, T> ProjectionElem<V, T> {
|
|
|
|
|
/// Returns `true` if the target of this projection may refer to a different region of memory
|
|
|
|
|
/// than the base.
|
|
|
|
|
fn is_indirect(&self) -> bool {
|
|
|
|
|
match self {
|
|
|
|
|
Self::Deref => true,
|
|
|
|
|
|
|
|
|
|
| Self::Field(_, _)
|
|
|
|
|
| Self::Index(_)
|
|
|
|
|
| Self::ConstantIndex { .. }
|
|
|
|
|
| Self::Subslice { .. }
|
|
|
|
|
| Self::Downcast(_, _)
|
|
|
|
|
=> false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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.
|
2019-03-28 18:00:17 -07:00
|
|
|
|
pub type PlaceElem<'tcx> = ProjectionElem<Local, Ty<'tcx>>;
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2019-10-11 16:19:48 -03:00
|
|
|
|
impl<'tcx> Copy for PlaceElem<'tcx> { }
|
|
|
|
|
|
2019-05-19 13:59:44 +03:00
|
|
|
|
// At least on 64 bit systems, `PlaceElem` should not be larger than two pointers.
|
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
|
static_assert_size!(PlaceElem<'_>, 16);
|
2018-11-01 13:30:04 +01:00
|
|
|
|
|
2018-10-26 11:28:40 +02:00
|
|
|
|
/// Alias for projections as they appear in `UserTypeProjection`, where we
|
|
|
|
|
/// need neither the `V` parameter for `Index` nor the `T` for `Field`.
|
2019-03-28 18:00:17 -07:00
|
|
|
|
pub type ProjectionKind = ProjectionElem<(), ()>;
|
2018-10-26 11:28:40 +02:00
|
|
|
|
|
2019-09-26 05:38:33 +00:00
|
|
|
|
rustc_index::newtype_index! {
|
2018-07-25 13:41:32 +03:00
|
|
|
|
pub struct Field {
|
2018-12-03 01:14:35 +01:00
|
|
|
|
derive [HashStable]
|
2018-07-25 13:41:32 +03:00
|
|
|
|
DEBUG_FORMAT = "field[{}]"
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
2019-07-19 21:33:13 +02:00
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2019-07-01 23:30:38 +02:00
|
|
|
|
pub struct PlaceRef<'a, 'tcx> {
|
|
|
|
|
pub base: &'a PlaceBase<'tcx>,
|
2019-07-30 00:07:28 +02:00
|
|
|
|
pub projection: &'a [PlaceElem<'tcx>],
|
2019-07-01 23:30:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
impl<'tcx> Place<'tcx> {
|
2019-10-20 16:11:04 -04:00
|
|
|
|
// FIXME change this to a const fn by also making List::empty a const fn.
|
2019-07-30 00:07:28 +02:00
|
|
|
|
pub fn return_place() -> Place<'tcx> {
|
|
|
|
|
Place {
|
|
|
|
|
base: PlaceBase::Local(RETURN_PLACE),
|
2019-10-20 16:11:04 -04:00
|
|
|
|
projection: List::empty(),
|
2019-04-30 18:58:24 +02:00
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
2018-09-19 02:48:47 +02:00
|
|
|
|
|
2019-08-29 13:22:16 -07:00
|
|
|
|
/// Returns `true` if this `Place` contains a `Deref` projection.
|
|
|
|
|
///
|
|
|
|
|
/// If `Place::is_indirect` returns false, the caller knows that the `Place` refers to the
|
|
|
|
|
/// same region of memory as its base.
|
|
|
|
|
pub fn is_indirect(&self) -> bool {
|
2019-07-30 00:07:28 +02:00
|
|
|
|
self.projection.iter().any(|elem| elem.is_indirect())
|
2019-08-29 13:22:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
|
/// Finds the innermost `Local` from this `Place`, *if* it is either a local itself or
|
2018-10-16 15:39:07 +02:00
|
|
|
|
/// a single deref of a local.
|
2019-02-08 14:53:55 +01:00
|
|
|
|
//
|
|
|
|
|
// FIXME: can we safely swap the semantics of `fn base_local` below in here instead?
|
2019-05-27 23:37:18 +02:00
|
|
|
|
pub fn local_or_deref_local(&self) -> Option<Local> {
|
2019-10-20 16:09:36 -04:00
|
|
|
|
match self.as_ref() {
|
|
|
|
|
PlaceRef {
|
|
|
|
|
base: &PlaceBase::Local(local),
|
|
|
|
|
projection: &[],
|
2019-04-30 18:58:24 +02:00
|
|
|
|
} |
|
2019-10-20 16:09:36 -04:00
|
|
|
|
PlaceRef {
|
|
|
|
|
base: &PlaceBase::Local(local),
|
|
|
|
|
projection: &[ProjectionElem::Deref],
|
|
|
|
|
} => Some(local),
|
2018-09-19 02:48:47 +02:00
|
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-16 15:39:07 +02:00
|
|
|
|
|
2019-09-19 11:41:10 -04:00
|
|
|
|
/// If this place represents a local variable like `_X` with no
|
|
|
|
|
/// projections, return `Some(_X)`.
|
|
|
|
|
pub fn as_local(&self) -> Option<Local> {
|
2019-10-21 20:20:47 -03:00
|
|
|
|
self.as_ref().as_local()
|
2019-09-19 11:41:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-21 22:38:30 +02:00
|
|
|
|
pub fn as_ref(&self) -> PlaceRef<'_, 'tcx> {
|
2019-07-02 21:01:20 +02:00
|
|
|
|
PlaceRef {
|
|
|
|
|
base: &self.base,
|
|
|
|
|
projection: &self.projection,
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-26 23:41:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-24 17:46:09 +02:00
|
|
|
|
impl From<Local> for Place<'_> {
|
|
|
|
|
fn from(local: Local) -> Self {
|
2019-04-30 18:58:24 +02:00
|
|
|
|
Place {
|
|
|
|
|
base: local.into(),
|
2019-10-20 16:11:04 -04:00
|
|
|
|
projection: List::empty(),
|
2019-04-30 18:58:24 +02:00
|
|
|
|
}
|
2019-06-24 17:46:09 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<Local> for PlaceBase<'_> {
|
|
|
|
|
fn from(local: Local) -> Self {
|
|
|
|
|
PlaceBase::Local(local)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-02 20:29:45 +02:00
|
|
|
|
impl<'a, 'tcx> PlaceRef<'a, 'tcx> {
|
2019-07-11 19:25:37 +02:00
|
|
|
|
/// Finds the innermost `Local` from this `Place`, *if* it is either a local itself or
|
|
|
|
|
/// a single deref of a local.
|
|
|
|
|
//
|
|
|
|
|
// FIXME: can we safely swap the semantics of `fn base_local` below in here instead?
|
|
|
|
|
pub fn local_or_deref_local(&self) -> Option<Local> {
|
|
|
|
|
match self {
|
|
|
|
|
PlaceRef {
|
|
|
|
|
base: PlaceBase::Local(local),
|
2019-07-30 00:07:28 +02:00
|
|
|
|
projection: [],
|
2019-07-11 19:25:37 +02:00
|
|
|
|
} |
|
|
|
|
|
PlaceRef {
|
|
|
|
|
base: PlaceBase::Local(local),
|
2019-07-30 00:07:28 +02:00
|
|
|
|
projection: [ProjectionElem::Deref],
|
2019-07-11 19:25:37 +02:00
|
|
|
|
} => Some(*local),
|
|
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-21 20:20:47 -03:00
|
|
|
|
|
|
|
|
|
/// If this place represents a local variable like `_X` with no
|
|
|
|
|
/// projections, return `Some(_X)`.
|
|
|
|
|
pub fn as_local(&self) -> Option<Local> {
|
|
|
|
|
match self {
|
|
|
|
|
PlaceRef { base: PlaceBase::Local(l), projection: [] } => Some(*l),
|
|
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-02 20:29:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-24 14:16:56 -07:00
|
|
|
|
impl Debug for Place<'_> {
|
2018-08-29 22:02:42 -07:00
|
|
|
|
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
|
2019-07-30 00:07:28 +02:00
|
|
|
|
for elem in self.projection.iter().rev() {
|
|
|
|
|
match elem {
|
|
|
|
|
ProjectionElem::Downcast(_, _) | ProjectionElem::Field(_, _) => {
|
|
|
|
|
write!(fmt, "(").unwrap();
|
|
|
|
|
}
|
|
|
|
|
ProjectionElem::Deref => {
|
|
|
|
|
write!(fmt, "(*").unwrap();
|
2018-06-19 21:22:52 -03:00
|
|
|
|
}
|
2019-07-30 00:07:28 +02:00
|
|
|
|
ProjectionElem::Index(_)
|
|
|
|
|
| ProjectionElem::ConstantIndex { .. }
|
|
|
|
|
| ProjectionElem::Subslice { .. } => {}
|
2019-04-24 19:21:10 -03:00
|
|
|
|
}
|
2019-07-30 00:07:28 +02:00
|
|
|
|
}
|
2019-04-24 19:21:10 -03:00
|
|
|
|
|
2019-07-30 00:07:28 +02:00
|
|
|
|
write!(fmt, "{:?}", self.base)?;
|
2019-04-24 19:21:10 -03:00
|
|
|
|
|
2019-07-30 00:07:28 +02:00
|
|
|
|
for elem in self.projection.iter() {
|
|
|
|
|
match elem {
|
|
|
|
|
ProjectionElem::Downcast(Some(name), _index) => {
|
|
|
|
|
write!(fmt, " as {})", name)?;
|
|
|
|
|
}
|
|
|
|
|
ProjectionElem::Downcast(None, index) => {
|
|
|
|
|
write!(fmt, " as variant#{:?})", index)?;
|
|
|
|
|
}
|
|
|
|
|
ProjectionElem::Deref => {
|
|
|
|
|
write!(fmt, ")")?;
|
|
|
|
|
}
|
|
|
|
|
ProjectionElem::Field(field, ty) => {
|
|
|
|
|
write!(fmt, ".{:?}: {:?})", field.index(), ty)?;
|
|
|
|
|
}
|
|
|
|
|
ProjectionElem::Index(ref index) => {
|
|
|
|
|
write!(fmt, "[{:?}]", index)?;
|
|
|
|
|
}
|
|
|
|
|
ProjectionElem::ConstantIndex { offset, min_length, from_end: false } => {
|
|
|
|
|
write!(fmt, "[{:?} of {:?}]", offset, min_length)?;
|
|
|
|
|
}
|
|
|
|
|
ProjectionElem::ConstantIndex { offset, min_length, from_end: true } => {
|
|
|
|
|
write!(fmt, "[-{:?} of {:?}]", offset, min_length)?;
|
|
|
|
|
}
|
|
|
|
|
ProjectionElem::Subslice { from, to } if *to == 0 => {
|
|
|
|
|
write!(fmt, "[{:?}:]", from)?;
|
|
|
|
|
}
|
|
|
|
|
ProjectionElem::Subslice { from, to } if *from == 0 => {
|
|
|
|
|
write!(fmt, "[:-{:?}]", to)?;
|
|
|
|
|
}
|
|
|
|
|
ProjectionElem::Subslice { from, to } => {
|
|
|
|
|
write!(fmt, "[{:?}:-{:?}]", from, to)?;
|
2018-06-19 21:22:52 -03:00
|
|
|
|
}
|
2019-04-24 19:21:10 -03:00
|
|
|
|
}
|
2019-07-30 00:07:28 +02:00
|
|
|
|
}
|
2019-04-24 19:21:10 -03:00
|
|
|
|
|
2019-07-30 00:07:28 +02:00
|
|
|
|
Ok(())
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-20 03:02:53 +02:00
|
|
|
|
impl Debug for PlaceBase<'_> {
|
|
|
|
|
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
|
match *self {
|
|
|
|
|
PlaceBase::Local(id) => write!(fmt, "{:?}", id),
|
2019-08-05 21:11:55 -04:00
|
|
|
|
PlaceBase::Static(box self::Static { ty, kind: StaticKind::Static, def_id }) => {
|
2019-07-12 22:49:15 +02:00
|
|
|
|
write!(fmt, "({}: {:?})", ty::tls::with(|tcx| tcx.def_path_str(def_id)), ty)
|
|
|
|
|
}
|
2019-08-05 21:11:55 -04:00
|
|
|
|
PlaceBase::Static(box self::Static {
|
|
|
|
|
ty, kind: StaticKind::Promoted(promoted, _), def_id: _
|
|
|
|
|
}) => {
|
2019-07-12 22:49:15 +02:00
|
|
|
|
write!(fmt, "({:?}: {:?})", promoted, ty)
|
|
|
|
|
}
|
2019-06-20 03:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-09 11:04:26 -05:00
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Scopes
|
|
|
|
|
|
2019-09-26 05:38:33 +00:00
|
|
|
|
rustc_index::newtype_index! {
|
2018-07-25 13:41:32 +03:00
|
|
|
|
pub struct SourceScope {
|
2018-12-03 01:14:35 +01:00
|
|
|
|
derive [HashStable]
|
2017-12-06 09:25:29 +01:00
|
|
|
|
DEBUG_FORMAT = "scope[{}]",
|
2018-05-28 14:16:09 +03:00
|
|
|
|
const OUTERMOST_SOURCE_SCOPE = 0,
|
2018-07-25 13:41:32 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-09 11:04:26 -05:00
|
|
|
|
|
2018-12-03 01:14:35 +01:00
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
|
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-12-03 01:14:35 +01:00
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
|
2018-05-28 17:37:48 +03:00
|
|
|
|
pub struct SourceScopeLocalData {
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// An `HirId` with lint levels equivalent to this scope's lint levels.
|
2019-02-22 15:48:14 +01:00
|
|
|
|
pub lint_root: hir::HirId,
|
2018-05-28 17:37:48 +03:00
|
|
|
|
/// 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
|
|
|
|
|
2018-10-25 08:58:12 +02:00
|
|
|
|
/// These are values that can appear inside an rvalue. They are intentionally
|
|
|
|
|
/// limited to prevent rvalues from being nested in one another.
|
2018-12-03 01:14:35 +01:00
|
|
|
|
#[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, HashStable)]
|
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>),
|
2018-08-09 06:18:00 -04:00
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// 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>),
|
2018-08-09 06:18:00 -04:00
|
|
|
|
|
|
|
|
|
/// Synthesizes a constant value.
|
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> {
|
2018-08-29 22:02:42 -07: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> {
|
2018-08-09 06:18:00 -04:00
|
|
|
|
/// Convenience helper to make a constant that refers to the fn
|
2019-02-08 14:53:55 +01:00
|
|
|
|
/// with given `DefId` and substs. Since this is used to synthesize
|
2018-08-09 06:18:00 -04:00
|
|
|
|
/// MIR, assumes `user_ty` is None.
|
2019-06-11 23:35:39 +03:00
|
|
|
|
pub fn function_handle(
|
2019-06-14 00:48:52 +03:00
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2017-03-07 16:09:01 +01:00
|
|
|
|
def_id: DefId,
|
2019-02-09 22:11:53 +08:00
|
|
|
|
substs: SubstsRef<'tcx>,
|
2017-03-07 16:09:01 +01:00
|
|
|
|
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,
|
2018-08-09 06:18:00 -04:00
|
|
|
|
user_ty: None,
|
2019-04-03 15:29:31 +02:00
|
|
|
|
literal: ty::Const::zero_sized(tcx, ty),
|
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(),
|
2018-06-19 21:22:52 -03:00
|
|
|
|
Operand::Move(ref place) => Operand::Copy(place.clone()),
|
2017-12-04 17:46:23 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
|
|
2019-11-10 10:26:33 +01:00
|
|
|
|
#[derive(Clone, RustcEncodable, RustcDecodable, HashStable, PartialEq)]
|
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.
|
|
|
|
|
///
|
2018-11-27 02:59:49 +00:00
|
|
|
|
/// Undefined (i.e., no effort is made to make it defined, but there’s no reason why it cannot
|
2017-01-31 01:10:54 +02:00
|
|
|
|
/// 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
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
|
/// Creates an aggregate value, like a tuple or struct. This is
|
2016-02-03 13:25:07 +01:00
|
|
|
|
/// 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
|
|
|
|
}
|
|
|
|
|
|
2018-12-03 01:14:35 +01:00
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable)]
|
2015-08-18 17:59:21 -04:00
|
|
|
|
pub enum CastKind {
|
|
|
|
|
Misc,
|
2019-04-15 19:20:44 +05:30
|
|
|
|
Pointer(PointerCast),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-03 01:14:35 +01:00
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable)]
|
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
|
2018-11-27 02:59:49 +00:00
|
|
|
|
/// -- e.g., for a union expression `SomeUnion { c: .. }`, the
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// active field index would identity the field `c`
|
2019-07-12 22:49:15 +02:00
|
|
|
|
Adt(&'tcx AdtDef, VariantIdx, SubstsRef<'tcx>, Option<UserTypeAnnotationIndex>, Option<usize>),
|
2017-12-06 09:25:29 +01:00
|
|
|
|
|
2019-09-26 17:30:44 +00:00
|
|
|
|
Closure(DefId, SubstsRef<'tcx>),
|
2019-11-09 18:06:57 +01:00
|
|
|
|
Generator(DefId, SubstsRef<'tcx>, hir::Movability),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-03 01:14:35 +01:00
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable)]
|
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,
|
2018-06-19 21:22:52 -03:00
|
|
|
|
_ => false,
|
2016-03-31 18:50:07 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-03 01:14:35 +01:00
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable)]
|
2017-05-18 18:43:52 +03:00
|
|
|
|
pub enum NullOp {
|
2019-02-08 14:53:55 +01:00
|
|
|
|
/// Returns the size of a value of that type
|
2017-05-18 18:43:52 +03:00
|
|
|
|
SizeOf,
|
2019-02-08 14:53:55 +01:00
|
|
|
|
/// Creates a new uninitialized box for a value of that type
|
2017-05-18 18:43:52 +03:00
|
|
|
|
Box,
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-03 01:14:35 +01:00
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable)]
|
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> {
|
2018-08-29 22:02:42 -07: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-09-10 22:33:45 +01:00
|
|
|
|
BorrowKind::Shallow => "shallow ",
|
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.
|
2019-01-18 21:45:13 +02:00
|
|
|
|
let print_region = ty::tls::with(|tcx| {
|
|
|
|
|
tcx.sess.verbose() || tcx.sess.opts.debugging_opts.identify_regions
|
|
|
|
|
});
|
|
|
|
|
let region = if print_region {
|
|
|
|
|
let mut region = region.to_string();
|
|
|
|
|
if region.len() > 0 {
|
|
|
|
|
region.push(' ');
|
|
|
|
|
}
|
|
|
|
|
region
|
|
|
|
|
} else {
|
|
|
|
|
// Do not even print 'static
|
|
|
|
|
String::new()
|
|
|
|
|
};
|
|
|
|
|
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) => {
|
2018-08-29 22:02:42 -07:00
|
|
|
|
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
|
|
|
|
|
2018-06-19 21:22:52 -03:00
|
|
|
|
AggregateKind::Tuple => match places.len() {
|
|
|
|
|
0 => write!(fmt, "()"),
|
|
|
|
|
1 => write!(fmt, "({:?},)", places[0]),
|
|
|
|
|
_ => fmt_tuple(fmt, places),
|
|
|
|
|
},
|
2015-12-31 21:38:44 -06:00
|
|
|
|
|
2018-08-09 11:56:53 -04:00
|
|
|
|
AggregateKind::Adt(adt_def, variant, substs, _user_ty, _) => {
|
2015-12-31 21:38:44 -06:00
|
|
|
|
let variant_def = &adt_def.variants[variant];
|
2016-02-23 22:04:27 +02:00
|
|
|
|
|
2019-01-18 22:14:01 +02:00
|
|
|
|
let f = &mut *fmt;
|
2019-01-25 12:11:50 +02:00
|
|
|
|
ty::tls::with(|tcx| {
|
|
|
|
|
let substs = tcx.lift(&substs).expect("could not lift for printing");
|
|
|
|
|
FmtPrinter::new(tcx, f, Namespace::ValueNS)
|
2019-03-24 12:09:44 +03:00
|
|
|
|
.print_def_path(variant_def.def_id, substs)?;
|
2019-01-18 22:14:01 +02:00
|
|
|
|
Ok(())
|
|
|
|
|
})?;
|
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| {
|
2019-03-04 09:00:30 +01:00
|
|
|
|
if let Some(hir_id) = tcx.hir().as_local_hir_id(def_id) {
|
2017-03-16 14:41:48 +01:00
|
|
|
|
let name = if tcx.sess.opts.debugging_opts.span_free_formats {
|
2019-03-04 09:00:30 +01:00
|
|
|
|
format!("[closure@{:?}]", hir_id)
|
2017-03-16 14:41:48 +01:00
|
|
|
|
} else {
|
2019-06-14 18:58:55 +02:00
|
|
|
|
format!("[closure@{:?}]", tcx.hir().span(hir_id))
|
2017-03-16 14:41:48 +01:00
|
|
|
|
};
|
2015-12-31 21:38:44 -06:00
|
|
|
|
let mut struct_fmt = fmt.debug_struct(&name);
|
|
|
|
|
|
2019-05-04 03:57:46 +03:00
|
|
|
|
if let Some(upvars) = tcx.upvars(def_id) {
|
2019-05-14 19:42:57 +03:00
|
|
|
|
for (&var_id, place) in upvars.keys().zip(places) {
|
2019-06-19 15:44:51 +02:00
|
|
|
|
let var_name = tcx.hir().name(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
|
|
|
|
}
|
2019-05-04 03:47:16 +03:00
|
|
|
|
}
|
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| {
|
2019-03-04 09:00:30 +01:00
|
|
|
|
if let Some(hir_id) = tcx.hir().as_local_hir_id(def_id) {
|
2019-07-12 22:49:15 +02:00
|
|
|
|
let name = format!("[generator@{:?}]", tcx.hir().span(hir_id));
|
2016-12-26 14:34:03 +01:00
|
|
|
|
let mut struct_fmt = fmt.debug_struct(&name);
|
|
|
|
|
|
2019-05-04 03:57:46 +03:00
|
|
|
|
if let Some(upvars) = tcx.upvars(def_id) {
|
2019-05-14 19:42:57 +03:00
|
|
|
|
for (&var_id, place) in upvars.keys().zip(places) {
|
2019-06-19 15:44:51 +02:00
|
|
|
|
let var_name = tcx.hir().name(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
|
|
|
|
}
|
2019-05-04 03:47:16 +03:00
|
|
|
|
}
|
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
|
|
|
|
|
2019-10-20 15:54:53 +11:00
|
|
|
|
#[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, HashStable)]
|
2015-10-05 12:31:48 -04:00
|
|
|
|
pub struct Constant<'tcx> {
|
|
|
|
|
pub span: Span,
|
2018-08-09 06:18:00 -04:00
|
|
|
|
|
|
|
|
|
/// Optional user-given type: for something like
|
|
|
|
|
/// `collect::<Vec<_>>`, this would be present and would
|
|
|
|
|
/// indicate that `Vec<_>` was explicitly specified.
|
|
|
|
|
///
|
|
|
|
|
/// Needed for NLL to impose user-given type constraints.
|
2018-11-16 22:56:18 +01:00
|
|
|
|
pub user_ty: Option<UserTypeAnnotationIndex>,
|
2018-08-09 06:18:00 -04:00
|
|
|
|
|
2019-03-14 10:19:31 +01:00
|
|
|
|
pub literal: &'tcx ty::Const<'tcx>,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-11 12:15:38 +01:00
|
|
|
|
impl Constant<'tcx> {
|
|
|
|
|
pub fn check_static_ptr(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
|
2019-11-18 22:15:33 +00:00
|
|
|
|
match self.literal.val.try_to_scalar() {
|
|
|
|
|
Some(Scalar::Ptr(ptr)) => match tcx.alloc_map.lock().get(ptr.alloc_id) {
|
2019-11-11 12:15:38 +01:00
|
|
|
|
Some(GlobalAlloc::Static(def_id)) => Some(def_id),
|
|
|
|
|
Some(_) => None,
|
|
|
|
|
None => {
|
|
|
|
|
tcx.sess.delay_span_bug(
|
|
|
|
|
DUMMY_SP, "MIR cannot contain dangling const pointers",
|
|
|
|
|
);
|
|
|
|
|
None
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-22 14:23:44 +02:00
|
|
|
|
/// A collection of projections into user types.
|
|
|
|
|
///
|
|
|
|
|
/// They are projections because a binding can occur a part of a
|
|
|
|
|
/// parent pattern that has been ascribed a type.
|
|
|
|
|
///
|
|
|
|
|
/// Its a collection because there can be multiple type ascriptions on
|
|
|
|
|
/// the path from the root of the pattern down to the binding itself.
|
2018-10-22 22:50:10 +02:00
|
|
|
|
///
|
|
|
|
|
/// An example:
|
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
|
|
|
|
/// struct S<'a>((i32, &'a str), String);
|
|
|
|
|
/// let S((_, w): (i32, &'static str), _): S = ...;
|
|
|
|
|
/// // ------ ^^^^^^^^^^^^^^^^^^^ (1)
|
|
|
|
|
/// // --------------------------------- ^ (2)
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// The highlights labelled `(1)` show the subpattern `(_, w)` being
|
|
|
|
|
/// ascribed the type `(i32, &'static str)`.
|
|
|
|
|
///
|
|
|
|
|
/// The highlights labelled `(2)` show the whole pattern being
|
|
|
|
|
/// ascribed the type `S`.
|
|
|
|
|
///
|
|
|
|
|
/// In this example, when we descend to `w`, we will have built up the
|
|
|
|
|
/// following two projected types:
|
|
|
|
|
///
|
|
|
|
|
/// * base: `S`, projection: `(base.0).1`
|
|
|
|
|
/// * base: `(i32, &'static str)`, projection: `base.1`
|
|
|
|
|
///
|
|
|
|
|
/// The first will lead to the constraint `w: &'1 str` (for some
|
|
|
|
|
/// inferred region `'1`). The second will lead to the constraint `w:
|
|
|
|
|
/// &'static str`.
|
2019-11-13 21:36:57 +01:00
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable, TypeFoldable)]
|
2019-03-28 18:00:17 -07:00
|
|
|
|
pub struct UserTypeProjections {
|
|
|
|
|
pub(crate) contents: Vec<(UserTypeProjection, Span)>,
|
2018-10-22 14:23:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-28 18:00:17 -07:00
|
|
|
|
impl<'tcx> UserTypeProjections {
|
2018-10-22 14:23:44 +02:00
|
|
|
|
pub fn none() -> Self {
|
|
|
|
|
UserTypeProjections { contents: vec![] }
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 22:49:15 +02:00
|
|
|
|
pub fn from_projections(projs: impl Iterator<Item = (UserTypeProjection, Span)>) -> Self {
|
2018-10-22 14:23:44 +02:00
|
|
|
|
UserTypeProjections { contents: projs.collect() }
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 22:49:15 +02:00
|
|
|
|
pub fn projections_and_spans(&self) -> impl Iterator<Item = &(UserTypeProjection, Span)> {
|
2018-10-22 14:23:44 +02:00
|
|
|
|
self.contents.iter()
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 22:49:15 +02:00
|
|
|
|
pub fn projections(&self) -> impl Iterator<Item = &UserTypeProjection> {
|
2018-10-22 14:23:44 +02:00
|
|
|
|
self.contents.iter().map(|&(ref user_type, _span)| user_type)
|
|
|
|
|
}
|
2018-12-19 16:47:06 +01:00
|
|
|
|
|
2019-07-12 22:49:15 +02:00
|
|
|
|
pub fn push_projection(mut self, user_ty: &UserTypeProjection, span: Span) -> Self {
|
2018-12-19 16:47:06 +01:00
|
|
|
|
self.contents.push((user_ty.clone(), span));
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn map_projections(
|
|
|
|
|
mut self,
|
2019-07-12 22:49:15 +02:00
|
|
|
|
mut f: impl FnMut(UserTypeProjection) -> UserTypeProjection,
|
2018-12-19 16:47:06 +01:00
|
|
|
|
) -> Self {
|
|
|
|
|
self.contents = self.contents.drain(..).map(|(proj, span)| (f(proj), span)).collect();
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn index(self) -> Self {
|
|
|
|
|
self.map_projections(|pat_ty_proj| pat_ty_proj.index())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn subslice(self, from: u32, to: u32) -> Self {
|
|
|
|
|
self.map_projections(|pat_ty_proj| pat_ty_proj.subslice(from, to))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn deref(self) -> Self {
|
|
|
|
|
self.map_projections(|pat_ty_proj| pat_ty_proj.deref())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn leaf(self, field: Field) -> Self {
|
|
|
|
|
self.map_projections(|pat_ty_proj| pat_ty_proj.leaf(field))
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 22:49:15 +02:00
|
|
|
|
pub fn variant(self, adt_def: &'tcx AdtDef, variant_index: VariantIdx, field: Field) -> Self {
|
2018-12-19 16:47:06 +01:00
|
|
|
|
self.map_projections(|pat_ty_proj| pat_ty_proj.variant(adt_def, variant_index, field))
|
|
|
|
|
}
|
2018-10-22 14:23:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-22 22:50:10 +02:00
|
|
|
|
/// Encodes the effect of a user-supplied type annotation on the
|
|
|
|
|
/// subcomponents of a pattern. The effect is determined by applying the
|
|
|
|
|
/// given list of proejctions to some underlying base type. Often,
|
|
|
|
|
/// the projection element list `projs` is empty, in which case this
|
|
|
|
|
/// directly encodes a type in `base`. But in the case of complex patterns with
|
|
|
|
|
/// subpatterns and bindings, we want to apply only a *part* of the type to a variable,
|
|
|
|
|
/// in which case the `projs` vector is used.
|
|
|
|
|
///
|
|
|
|
|
/// Examples:
|
|
|
|
|
///
|
|
|
|
|
/// * `let x: T = ...` -- here, the `projs` vector is empty.
|
|
|
|
|
///
|
|
|
|
|
/// * `let (x, _): T = ...` -- here, the `projs` vector would contain
|
|
|
|
|
/// `field[0]` (aka `.0`), indicating that the type of `s` is
|
|
|
|
|
/// determined by finding the type of the `.0` field from `T`.
|
2019-11-10 10:26:33 +01:00
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable, PartialEq)]
|
2019-03-28 18:00:17 -07:00
|
|
|
|
pub struct UserTypeProjection {
|
2018-11-16 22:56:18 +01:00
|
|
|
|
pub base: UserTypeAnnotationIndex,
|
2019-05-01 15:34:51 +01:00
|
|
|
|
pub projs: Vec<ProjectionKind>,
|
2018-10-22 11:58:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 22:49:15 +02:00
|
|
|
|
impl Copy for ProjectionKind {}
|
2018-10-26 11:28:40 +02:00
|
|
|
|
|
2019-03-28 18:00:17 -07:00
|
|
|
|
impl UserTypeProjection {
|
2018-12-19 16:47:06 +01:00
|
|
|
|
pub(crate) fn index(mut self) -> Self {
|
|
|
|
|
self.projs.push(ProjectionElem::Index(()));
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn subslice(mut self, from: u32, to: u32) -> Self {
|
|
|
|
|
self.projs.push(ProjectionElem::Subslice { from, to });
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn deref(mut self) -> Self {
|
|
|
|
|
self.projs.push(ProjectionElem::Deref);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn leaf(mut self, field: Field) -> Self {
|
|
|
|
|
self.projs.push(ProjectionElem::Field(field, ()));
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn variant(
|
|
|
|
|
mut self,
|
|
|
|
|
adt_def: &'tcx AdtDef,
|
|
|
|
|
variant_index: VariantIdx,
|
|
|
|
|
field: Field,
|
|
|
|
|
) -> Self {
|
2019-03-28 18:00:17 -07:00
|
|
|
|
self.projs.push(ProjectionElem::Downcast(
|
|
|
|
|
Some(adt_def.variants[variant_index].ident.name),
|
2019-07-12 22:49:15 +02:00
|
|
|
|
variant_index,
|
|
|
|
|
));
|
2018-12-19 16:47:06 +01:00
|
|
|
|
self.projs.push(ProjectionElem::Field(field, ()));
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-28 18:00:17 -07:00
|
|
|
|
CloneTypeFoldableAndLiftImpls! { ProjectionKind, }
|
2018-10-26 11:28:40 +02:00
|
|
|
|
|
2019-03-28 18:00:17 -07:00
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for UserTypeProjection {
|
2019-06-14 00:48:52 +03:00
|
|
|
|
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
|
2019-02-05 11:20:45 -06:00
|
|
|
|
use crate::mir::ProjectionElem::*;
|
2018-10-22 22:50:10 +02:00
|
|
|
|
|
|
|
|
|
let base = self.base.fold_with(folder);
|
2019-07-12 22:49:15 +02:00
|
|
|
|
let projs: Vec<_> = self
|
|
|
|
|
.projs
|
2018-10-22 22:50:10 +02:00
|
|
|
|
.iter()
|
2019-07-12 22:49:15 +02:00
|
|
|
|
.map(|elem| match elem {
|
|
|
|
|
Deref => Deref,
|
|
|
|
|
Field(f, ()) => Field(f.clone(), ()),
|
|
|
|
|
Index(()) => Index(()),
|
|
|
|
|
elem => elem.clone(),
|
|
|
|
|
})
|
2018-10-22 22:50:10 +02:00
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
UserTypeProjection { base, projs }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn super_visit_with<Vs: TypeVisitor<'tcx>>(&self, visitor: &mut Vs) -> bool {
|
|
|
|
|
self.base.visit_with(visitor)
|
|
|
|
|
// Note: there's nothing in `self.proj` to visit.
|
2018-10-22 11:58:06 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-26 05:38:33 +00:00
|
|
|
|
rustc_index::newtype_index! {
|
2018-07-25 13:41:32 +03:00
|
|
|
|
pub struct Promoted {
|
2018-12-03 01:14:35 +01:00
|
|
|
|
derive [HashStable]
|
2018-07-25 13:41:32 +03:00
|
|
|
|
DEBUG_FORMAT = "promoted[{}]"
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-06 09:25:29 +01:00
|
|
|
|
|
2015-12-18 19:29:03 -06:00
|
|
|
|
impl<'tcx> Debug for Constant<'tcx> {
|
2018-08-29 22:02:42 -07:00
|
|
|
|
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
|
2019-03-29 10:52:09 +01:00
|
|
|
|
write!(fmt, "{}", self)
|
2015-12-18 19:29:03 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-05 23:06:33 -06:00
|
|
|
|
|
2019-03-29 10:52:09 +01:00
|
|
|
|
impl<'tcx> Display for Constant<'tcx> {
|
|
|
|
|
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
|
write!(fmt, "const ")?;
|
2019-10-10 22:01:55 +13:00
|
|
|
|
// FIXME make the default pretty printing of raw pointers more detailed. Here we output the
|
|
|
|
|
// debug representation of raw pointers, so that the raw pointers in the mir dump output are
|
|
|
|
|
// detailed and just not '{pointer}'.
|
|
|
|
|
if let ty::RawPtr(_) = self.literal.ty.kind {
|
|
|
|
|
write!(fmt, "{:?} : {}", self.literal.val, self.literal.ty)
|
|
|
|
|
} else {
|
|
|
|
|
write!(fmt, "{}", self.literal)
|
|
|
|
|
}
|
2019-03-29 10:52:09 +01:00
|
|
|
|
}
|
2016-01-05 23:06:33 -06:00
|
|
|
|
}
|
2016-06-09 15:49:07 -07:00
|
|
|
|
|
2019-05-17 23:55:04 +02:00
|
|
|
|
impl<'tcx> graph::DirectedGraph for Body<'tcx> {
|
2016-06-09 15:49:07 -07:00
|
|
|
|
type Node = BasicBlock;
|
2018-07-01 16:54:01 -04:00
|
|
|
|
}
|
2016-06-09 15:49:07 -07:00
|
|
|
|
|
2019-05-17 23:55:04 +02:00
|
|
|
|
impl<'tcx> graph::WithNumNodes for Body<'tcx> {
|
2018-06-19 21:22:52 -03:00
|
|
|
|
fn num_nodes(&self) -> usize {
|
|
|
|
|
self.basic_blocks.len()
|
|
|
|
|
}
|
2018-07-01 16:54:01 -04:00
|
|
|
|
}
|
2016-06-09 15:49:07 -07:00
|
|
|
|
|
2019-05-17 23:55:04 +02:00
|
|
|
|
impl<'tcx> graph::WithStartNode for Body<'tcx> {
|
2018-06-19 21:22:52 -03:00
|
|
|
|
fn start_node(&self) -> Self::Node {
|
|
|
|
|
START_BLOCK
|
|
|
|
|
}
|
2018-07-01 16:54:01 -04:00
|
|
|
|
}
|
2016-06-09 15:49:07 -07:00
|
|
|
|
|
2019-05-17 23:55:04 +02:00
|
|
|
|
impl<'tcx> graph::WithPredecessors for Body<'tcx> {
|
2019-06-21 23:49:03 +02:00
|
|
|
|
fn predecessors(
|
|
|
|
|
&self,
|
2018-06-19 21:22:52 -03:00
|
|
|
|
node: Self::Node,
|
2019-06-21 23:49:03 +02:00
|
|
|
|
) -> <Self as GraphPredecessors<'_>>::Iter {
|
2016-06-09 15:49:07 -07:00
|
|
|
|
self.predecessors_for(node).clone().into_iter()
|
|
|
|
|
}
|
2018-07-01 16:54:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 23:55:04 +02:00
|
|
|
|
impl<'tcx> graph::WithSuccessors for Body<'tcx> {
|
2019-06-21 23:49:03 +02:00
|
|
|
|
fn successors(
|
|
|
|
|
&self,
|
2018-06-19 21:22:52 -03:00
|
|
|
|
node: Self::Node,
|
2019-06-21 23:49:03 +02:00
|
|
|
|
) -> <Self as GraphSuccessors<'_>>::Iter {
|
2018-04-27 14:02:09 +03:00
|
|
|
|
self.basic_blocks[node].terminator().successors().cloned()
|
2016-06-09 15:49:07 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 23:55:04 +02:00
|
|
|
|
impl<'a, 'b> graph::GraphPredecessors<'b> for Body<'a> {
|
2016-06-09 15:49:07 -07:00
|
|
|
|
type Item = BasicBlock;
|
|
|
|
|
type Iter = IntoIter<BasicBlock>;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 23:55:04 +02:00
|
|
|
|
impl<'a, 'b> graph::GraphSuccessors<'b> for Body<'a> {
|
2016-06-09 15:49:07 -07:00
|
|
|
|
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
|
|
|
|
|
2018-12-03 01:14:35 +01:00
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Ord, PartialOrd, HashStable)]
|
2016-08-08 18:46:06 -07:00
|
|
|
|
pub struct Location {
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// The block that the location is within.
|
2016-08-08 18:46:06 -07:00
|
|
|
|
pub block: BasicBlock,
|
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// The location is the position of the start of the statement; or, if
|
|
|
|
|
/// `statement_index` equals the number of statements, then the start of the
|
|
|
|
|
/// terminator.
|
2016-08-08 18:46:06 -07:00
|
|
|
|
pub statement_index: usize,
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-11 23:47:28 +03:00
|
|
|
|
impl fmt::Debug for Location {
|
2018-08-29 22:02:42 -07:00
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-06-11 23:47:28 +03:00
|
|
|
|
write!(fmt, "{:?}[{}]", self.block, self.statement_index)
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-15 18:18:40 -07:00
|
|
|
|
|
|
|
|
|
impl Location {
|
2019-07-12 22:49:15 +02:00
|
|
|
|
pub const START: Location = Location { block: START_BLOCK, statement_index: 0 };
|
2018-05-01 09:46:11 -04:00
|
|
|
|
|
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 {
|
2019-07-12 22:49:15 +02:00
|
|
|
|
Location { block: self.block, statement_index: self.statement_index + 1 }
|
2017-12-06 09:25:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-05 15:14:28 +01:00
|
|
|
|
/// Returns `true` if `other` is earlier in the control flow graph than `self`.
|
2019-06-03 18:26:48 -04:00
|
|
|
|
pub fn is_predecessor_of<'tcx>(&self, other: Location, body: &Body<'tcx>) -> bool {
|
2018-11-05 15:14:28 +01:00
|
|
|
|
// If we are in the same block as the other location and are an earlier statement
|
|
|
|
|
// then we are a predecessor of `other`.
|
|
|
|
|
if self.block == other.block && self.statement_index < other.statement_index {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we're in another block, then we want to check that block is a predecessor of `other`.
|
2019-06-03 18:26:48 -04:00
|
|
|
|
let mut queue: Vec<BasicBlock> = body.predecessors_for(other.block).clone();
|
2018-11-05 15:14:28 +01:00
|
|
|
|
let mut visited = FxHashSet::default();
|
|
|
|
|
|
|
|
|
|
while let Some(block) = queue.pop() {
|
|
|
|
|
// If we haven't visited this block before, then make sure we visit it's predecessors.
|
|
|
|
|
if visited.insert(block) {
|
2019-06-03 18:26:48 -04:00
|
|
|
|
queue.append(&mut body.predecessors_for(block).clone());
|
2018-11-05 15:14:28 +01:00
|
|
|
|
} else {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we found the block that `self` is in, then we are a predecessor of `other` (since
|
|
|
|
|
// we found that block by looking at the predecessors of `other`).
|
|
|
|
|
if self.block == block {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
2019-10-20 15:54:53 +11:00
|
|
|
|
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable, HashStable)]
|
2017-12-06 09:25:29 +01:00
|
|
|
|
pub enum UnsafetyViolationKind {
|
|
|
|
|
General,
|
2019-09-06 21:05:37 +01:00
|
|
|
|
/// Permitted both in `const fn`s and regular `fn`s.
|
2018-11-05 18:06:26 +01:00
|
|
|
|
GeneralAndConstFn,
|
2019-02-22 15:48:14 +01:00
|
|
|
|
BorrowPacked(hir::HirId),
|
2017-12-06 09:25:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-20 15:54:53 +11:00
|
|
|
|
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable, HashStable)]
|
2017-09-19 16:20:02 +03:00
|
|
|
|
pub struct UnsafetyViolation {
|
|
|
|
|
pub source_info: SourceInfo,
|
2019-10-18 15:01:51 +11:00
|
|
|
|
pub description: Symbol,
|
|
|
|
|
pub details: Symbol,
|
2017-12-06 09:25:29 +01:00
|
|
|
|
pub kind: UnsafetyViolationKind,
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-20 15:54:53 +11:00
|
|
|
|
#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
|
2017-12-06 09:25:29 +01:00
|
|
|
|
pub struct UnsafetyCheckResult {
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// Violations that are propagated *upwards* from this function.
|
2018-02-27 17:11:14 +01:00
|
|
|
|
pub violations: Lrc<[UnsafetyViolation]>,
|
2019-09-06 21:05:37 +01:00
|
|
|
|
/// `unsafe` blocks in this function, along with whether they are used. This is
|
2017-12-06 09:25:29 +01:00
|
|
|
|
/// used for the "unused_unsafe" lint.
|
2019-02-22 15:48:14 +01:00
|
|
|
|
pub unsafe_blocks: Lrc<[(hir::HirId, bool)]>,
|
2017-09-19 16:20:02 +03:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-26 05:38:33 +00:00
|
|
|
|
rustc_index::newtype_index! {
|
2019-05-03 16:11:39 -07:00
|
|
|
|
pub struct GeneratorSavedLocal {
|
2019-05-03 16:03:05 -07:00
|
|
|
|
derive [HashStable]
|
|
|
|
|
DEBUG_FORMAT = "_{}",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// The layout of generator state.
|
2019-11-13 21:36:57 +01:00
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable, TypeFoldable)]
|
2016-12-26 14:34:03 +01:00
|
|
|
|
pub struct GeneratorLayout<'tcx> {
|
2019-05-03 16:03:05 -07:00
|
|
|
|
/// The type of every local stored inside the generator.
|
2019-05-03 16:11:39 -07:00
|
|
|
|
pub field_tys: IndexVec<GeneratorSavedLocal, Ty<'tcx>>,
|
2019-05-03 16:03:05 -07:00
|
|
|
|
|
|
|
|
|
/// Which of the above fields are in each variant. Note that one field may
|
|
|
|
|
/// be stored in multiple variants.
|
2019-05-03 16:11:39 -07:00
|
|
|
|
pub variant_fields: IndexVec<VariantIdx, IndexVec<Field, GeneratorSavedLocal>>,
|
2019-05-03 16:03:05 -07:00
|
|
|
|
|
2019-05-30 14:27:12 -07:00
|
|
|
|
/// Which saved locals are storage-live at the same time. Locals that do not
|
|
|
|
|
/// have conflicts with each other are allowed to overlap in the computed
|
|
|
|
|
/// layout.
|
2019-05-31 16:53:27 -07:00
|
|
|
|
pub storage_conflicts: BitMatrix<GeneratorSavedLocal, GeneratorSavedLocal>,
|
2016-12-26 14:34:03 +01:00
|
|
|
|
}
|
2017-02-08 22:24:49 +13:00
|
|
|
|
|
2018-12-03 01:14:35 +01:00
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
|
2019-06-14 00:48:52 +03:00
|
|
|
|
pub struct BorrowCheckResult<'tcx> {
|
|
|
|
|
pub closure_requirements: Option<ClosureRegionRequirements<'tcx>>,
|
2018-03-02 20:42:37 -08:00
|
|
|
|
pub used_mut_upvars: SmallVec<[Field; 8]>,
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-14 09:15:23 -08:00
|
|
|
|
/// The result of the `mir_const_qualif` query.
|
2019-11-14 11:58:50 -08:00
|
|
|
|
///
|
|
|
|
|
/// Each field corresponds to an implementer of the `Qualif` trait in
|
|
|
|
|
/// `librustc_mir/transform/check_consts/qualifs.rs`. See that file for more information on each
|
|
|
|
|
/// `Qualif`.
|
2019-11-14 09:15:23 -08:00
|
|
|
|
#[derive(Clone, Copy, Debug, Default, RustcEncodable, RustcDecodable, HashStable)]
|
2019-11-14 11:58:50 -08:00
|
|
|
|
pub struct ConstQualifs {
|
2019-11-14 09:15:23 -08:00
|
|
|
|
pub has_mut_interior: bool,
|
|
|
|
|
pub needs_drop: bool,
|
|
|
|
|
}
|
|
|
|
|
|
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
|
2019-02-08 14:53:55 +01:00
|
|
|
|
/// appear in the closure's signature or on its field types. These
|
2017-11-22 17:38:51 -05:00
|
|
|
|
/// 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
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// `TyCtxt`, and hence we cannot use `ReVar` (which is what we use
|
2017-12-05 05:18:58 -05:00
|
|
|
|
/// internally within the rest of the NLL code).
|
2018-12-03 01:14:35 +01:00
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
|
2019-06-14 00:48:52 +03:00
|
|
|
|
pub struct ClosureRegionRequirements<'tcx> {
|
2019-02-08 14:53:55 +01:00
|
|
|
|
/// The number of external regions defined on the closure. In our
|
2017-11-22 17:38:51 -05:00
|
|
|
|
/// 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.
|
2019-06-14 00:48:52 +03:00
|
|
|
|
pub outlives_requirements: Vec<ClosureOutlivesRequirement<'tcx>>,
|
2017-11-22 17:38:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// Indicates an outlives-constraint between a type or between two
|
|
|
|
|
/// free regions declared on the closure.
|
2018-12-03 01:14:35 +01:00
|
|
|
|
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
|
2017-12-04 11:37:34 -05:00
|
|
|
|
pub struct ClosureOutlivesRequirement<'tcx> {
|
|
|
|
|
// This region or type ...
|
|
|
|
|
pub subject: ClosureOutlivesSubject<'tcx>,
|
2017-11-22 17:38:51 -05:00
|
|
|
|
|
2018-10-03 21:56:11 +01:00
|
|
|
|
// ... must outlive this one.
|
2017-11-22 17:38:51 -05:00
|
|
|
|
pub outlived_free_region: ty::RegionVid,
|
|
|
|
|
|
2018-10-03 21:56:11 +01:00
|
|
|
|
// If not, report an error here ...
|
2017-11-22 17:38:51 -05:00
|
|
|
|
pub blame_span: Span,
|
2018-10-03 21:56:11 +01:00
|
|
|
|
|
|
|
|
|
// ... due to this reason.
|
|
|
|
|
pub category: ConstraintCategory,
|
2017-11-22 17:38:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// Outlives-constraints can be categorized to determine whether and why they
|
2018-10-03 21:33:22 +01:00
|
|
|
|
/// are interesting (for error reporting). Order of variants indicates sort
|
|
|
|
|
/// order of the category, thereby influencing diagnostic output.
|
|
|
|
|
///
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// See also [rustc_mir::borrow_check::nll::constraints].
|
2019-07-12 22:49:15 +02:00
|
|
|
|
#[derive(
|
|
|
|
|
Copy,
|
|
|
|
|
Clone,
|
|
|
|
|
Debug,
|
|
|
|
|
Eq,
|
|
|
|
|
PartialEq,
|
|
|
|
|
PartialOrd,
|
|
|
|
|
Ord,
|
|
|
|
|
Hash,
|
|
|
|
|
RustcEncodable,
|
|
|
|
|
RustcDecodable,
|
|
|
|
|
HashStable,
|
|
|
|
|
)]
|
2018-10-03 21:33:22 +01:00
|
|
|
|
pub enum ConstraintCategory {
|
|
|
|
|
Return,
|
2018-12-03 14:11:53 +01:00
|
|
|
|
Yield,
|
2018-10-13 14:23:23 +01:00
|
|
|
|
UseAsConst,
|
|
|
|
|
UseAsStatic,
|
2018-10-03 21:33:22 +01:00
|
|
|
|
TypeAnnotation,
|
|
|
|
|
Cast,
|
|
|
|
|
|
|
|
|
|
/// A constraint that came from checking the body of a closure.
|
|
|
|
|
///
|
|
|
|
|
/// We try to get the category that the closure used when reporting this.
|
|
|
|
|
ClosureBounds,
|
|
|
|
|
CallArgument,
|
|
|
|
|
CopyBound,
|
|
|
|
|
SizedBound,
|
|
|
|
|
Assignment,
|
|
|
|
|
OpaqueType,
|
|
|
|
|
|
|
|
|
|
/// A "boring" constraint (caused by the given location) is one that
|
|
|
|
|
/// the user probably doesn't want to see described in diagnostics,
|
|
|
|
|
/// because it is kind of an artifact of the type system setup.
|
|
|
|
|
/// Example: `x = Foo { field: y }` technically creates
|
|
|
|
|
/// intermediate regions representing the "type of `Foo { field: y
|
|
|
|
|
/// }`", and data flows from `y` into those variables, but they
|
|
|
|
|
/// are not very interesting. The assignment into `x` on the other
|
|
|
|
|
/// hand might be.
|
|
|
|
|
Boring,
|
|
|
|
|
// Boring and applicable everywhere.
|
|
|
|
|
BoringNoLocation,
|
|
|
|
|
|
|
|
|
|
/// A constraint that doesn't correspond to anything the user sees.
|
|
|
|
|
Internal,
|
2017-11-22 17:38:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// The subject of a `ClosureOutlivesRequirement` -- that is, the thing
|
2017-12-04 11:37:34 -05:00
|
|
|
|
/// that must outlive some region.
|
2018-12-03 01:14:35 +01:00
|
|
|
|
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
|
2017-12-04 11:37:34 -05:00
|
|
|
|
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
|
|
|
|
/*
|
2019-09-06 03:57:44 +01:00
|
|
|
|
* `TypeFoldable` implementations for MIR types
|
2019-06-12 00:11:55 +03:00
|
|
|
|
*/
|
2017-02-08 22:24:49 +13:00
|
|
|
|
|
2018-02-09 10:34:23 -05:00
|
|
|
|
CloneTypeFoldableAndLiftImpls! {
|
2018-09-22 00:51:48 +02:00
|
|
|
|
BlockTailInfo,
|
2018-10-20 16:18:17 -04:00
|
|
|
|
MirPhase,
|
2018-02-09 10:34:23 -05:00
|
|
|
|
Mutability,
|
|
|
|
|
SourceInfo,
|
2018-09-14 21:05:31 +02:00
|
|
|
|
FakeReadCause,
|
2018-12-11 19:54:38 +01:00
|
|
|
|
RetagKind,
|
2018-05-28 14:16:09 +03:00
|
|
|
|
SourceScope,
|
2018-05-28 17:37:48 +03:00
|
|
|
|
SourceScopeData,
|
|
|
|
|
SourceScopeLocalData,
|
2018-11-16 22:56:18 +01:00
|
|
|
|
UserTypeAnnotationIndex,
|
2017-02-08 22:24:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
|
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
|
2019-02-05 11:20:45 -06:00
|
|
|
|
use crate::mir::TerminatorKind::*;
|
2017-02-08 22:24:49 +13:00
|
|
|
|
|
|
|
|
|
let kind = match self.kind {
|
2018-11-06 15:05:44 -05:00
|
|
|
|
Goto { target } => Goto { target },
|
2019-07-12 22:49:15 +02:00
|
|
|
|
SwitchInt { ref discr, switch_ty, ref values, ref targets } => SwitchInt {
|
2017-02-08 22:24:49 +13:00
|
|
|
|
discr: discr.fold_with(folder),
|
|
|
|
|
switch_ty: switch_ty.fold_with(folder),
|
|
|
|
|
values: values.clone(),
|
2018-06-19 21:22:52 -03:00
|
|
|
|
targets: targets.clone(),
|
2017-02-08 22:24:49 +13:00
|
|
|
|
},
|
2019-07-12 22:49:15 +02:00
|
|
|
|
Drop { ref location, target, unwind } => {
|
|
|
|
|
Drop { location: location.fold_with(folder), target, unwind }
|
|
|
|
|
}
|
|
|
|
|
DropAndReplace { ref location, ref value, target, unwind } => DropAndReplace {
|
2017-02-08 22:24:49 +13:00
|
|
|
|
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
|
|
|
|
},
|
2019-07-12 22:49:15 +02:00
|
|
|
|
Yield { ref value, resume, drop } => {
|
|
|
|
|
Yield { value: value.fold_with(folder), resume: resume, drop: drop }
|
|
|
|
|
}
|
|
|
|
|
Call { ref func, ref args, ref destination, cleanup, from_hir_call } => {
|
|
|
|
|
let dest =
|
|
|
|
|
destination.as_ref().map(|&(ref loc, dest)| (loc.fold_with(folder), dest));
|
2017-02-08 22:24:49 +13:00
|
|
|
|
|
|
|
|
|
Call {
|
|
|
|
|
func: func.fold_with(folder),
|
|
|
|
|
args: args.fold_with(folder),
|
|
|
|
|
destination: dest,
|
2017-07-03 11:19:51 -07:00
|
|
|
|
cleanup,
|
2018-09-29 10:34:12 +01:00
|
|
|
|
from_hir_call,
|
2017-02-08 22:24:49 +13:00
|
|
|
|
}
|
2018-06-19 21:22:52 -03:00
|
|
|
|
}
|
2019-07-12 22:49:15 +02:00
|
|
|
|
Assert { ref cond, expected, ref msg, target, cleanup } => {
|
2019-07-29 13:28:55 +05:30
|
|
|
|
use PanicInfo::*;
|
2019-07-24 10:24:55 +02:00
|
|
|
|
let msg = match msg {
|
|
|
|
|
BoundsCheck { ref len, ref index } =>
|
|
|
|
|
BoundsCheck {
|
|
|
|
|
len: len.fold_with(folder),
|
|
|
|
|
index: index.fold_with(folder),
|
|
|
|
|
},
|
|
|
|
|
Panic { .. } | Overflow(_) | OverflowNeg | DivisionByZero | RemainderByZero |
|
2019-11-26 00:30:07 +00:00
|
|
|
|
ResumedAfterReturn(_) | ResumedAfterPanic(_) =>
|
2019-07-24 10:24:55 +02:00
|
|
|
|
msg.clone(),
|
2017-02-08 22:24:49 +13:00
|
|
|
|
};
|
2019-07-12 22:49:15 +02:00
|
|
|
|
Assert { cond: cond.fold_with(folder), expected, msg, target, cleanup }
|
2018-06-19 21:22:52 -03: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,
|
2019-07-12 22:49:15 +02:00
|
|
|
|
FalseEdges { real_target, imaginary_target } => {
|
|
|
|
|
FalseEdges { real_target, imaginary_target }
|
|
|
|
|
}
|
|
|
|
|
FalseUnwind { real_target, unwind } => FalseUnwind { real_target, unwind },
|
2017-02-08 22:24:49 +13:00
|
|
|
|
};
|
2019-07-12 22:49:15 +02:00
|
|
|
|
Terminator { source_info: self.source_info, kind }
|
2017-02-08 22:24:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
|
2019-02-05 11:20:45 -06:00
|
|
|
|
use crate::mir::TerminatorKind::*;
|
2017-02-08 22:24:49 +13:00
|
|
|
|
|
|
|
|
|
match self.kind {
|
2019-07-12 22:49:15 +02:00
|
|
|
|
SwitchInt { ref discr, switch_ty, .. } => {
|
|
|
|
|
discr.visit_with(visitor) || switch_ty.visit_with(visitor)
|
|
|
|
|
}
|
2018-06-19 21:22:52 -03:00
|
|
|
|
Drop { ref location, .. } => location.visit_with(visitor),
|
2019-07-12 22:49:15 +02:00
|
|
|
|
DropAndReplace { ref location, ref value, .. } => {
|
|
|
|
|
location.visit_with(visitor) || value.visit_with(visitor)
|
|
|
|
|
}
|
2018-06-19 21:22:52 -03:00
|
|
|
|
Yield { ref value, .. } => value.visit_with(visitor),
|
2019-07-12 22:49:15 +02:00
|
|
|
|
Call { ref func, ref args, ref destination, .. } => {
|
2017-02-08 22:24:49 +13:00
|
|
|
|
let dest = if let Some((ref loc, _)) = *destination {
|
|
|
|
|
loc.visit_with(visitor)
|
2018-06-19 21:22:52 -03:00
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
};
|
2017-02-08 22:24:49 +13:00
|
|
|
|
dest || func.visit_with(visitor) || args.visit_with(visitor)
|
2018-06-19 21:22:52 -03:00
|
|
|
|
}
|
2019-07-12 22:49:15 +02:00
|
|
|
|
Assert { ref cond, ref msg, .. } => {
|
2017-02-08 22:24:49 +13:00
|
|
|
|
if cond.visit_with(visitor) {
|
2019-07-29 13:28:55 +05:30
|
|
|
|
use PanicInfo::*;
|
2019-07-24 10:24:55 +02:00
|
|
|
|
match msg {
|
|
|
|
|
BoundsCheck { ref len, ref index } =>
|
|
|
|
|
len.visit_with(visitor) || index.visit_with(visitor),
|
|
|
|
|
Panic { .. } | Overflow(_) | OverflowNeg |
|
|
|
|
|
DivisionByZero | RemainderByZero |
|
2019-11-26 00:30:07 +00:00
|
|
|
|
ResumedAfterReturn(_) | ResumedAfterPanic(_) =>
|
2019-07-24 10:24:55 +02:00
|
|
|
|
false
|
2017-02-08 22:24:49 +13:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
2018-06-19 21:22:52 -03:00
|
|
|
|
}
|
|
|
|
|
Goto { .. }
|
|
|
|
|
| Resume
|
|
|
|
|
| Abort
|
|
|
|
|
| Return
|
|
|
|
|
| GeneratorDrop
|
|
|
|
|
| Unreachable
|
|
|
|
|
| FalseEdges { .. }
|
|
|
|
|
| FalseUnwind { .. } => false,
|
2017-02-08 22:24:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 15:27:42 +00:00
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for GeneratorKind {
|
|
|
|
|
fn super_fold_with<F: TypeFolder<'tcx>>(&self, _: &mut F) -> Self {
|
|
|
|
|
*self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> bool {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for Place<'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
|
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
|
2019-04-30 18:58:24 +02:00
|
|
|
|
Place {
|
2019-08-16 20:31:28 -04:00
|
|
|
|
base: self.base.fold_with(folder),
|
2019-04-30 18:58:24 +02:00
|
|
|
|
projection: self.projection.fold_with(folder),
|
2017-02-08 22:24:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
|
2019-08-16 20:31:28 -04:00
|
|
|
|
self.base.visit_with(visitor) || self.projection.visit_with(visitor)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for PlaceBase<'tcx> {
|
|
|
|
|
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
|
|
|
|
|
match self {
|
|
|
|
|
PlaceBase::Local(local) => PlaceBase::Local(local.fold_with(folder)),
|
|
|
|
|
PlaceBase::Static(static_) => PlaceBase::Static(static_.fold_with(folder)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
|
|
|
|
|
match self {
|
|
|
|
|
PlaceBase::Local(local) => local.visit_with(visitor),
|
|
|
|
|
PlaceBase::Static(static_) => (**static_).visit_with(visitor),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-20 16:11:04 -04:00
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<PlaceElem<'tcx>> {
|
|
|
|
|
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
|
|
|
|
|
let v = self.iter().map(|t| t.fold_with(folder)).collect::<Vec<_>>();
|
|
|
|
|
folder.tcx().intern_place_elems(&v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
|
|
|
|
|
self.iter().any(|t| t.visit_with(visitor))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-16 20:31:28 -04:00
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for Static<'tcx> {
|
|
|
|
|
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
|
|
|
|
|
Static {
|
|
|
|
|
ty: self.ty.fold_with(folder),
|
|
|
|
|
kind: self.kind.fold_with(folder),
|
|
|
|
|
def_id: self.def_id,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
|
|
|
|
|
let Static { ty, kind, def_id: _ } = self;
|
|
|
|
|
|
|
|
|
|
ty.visit_with(visitor) || kind.visit_with(visitor)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for StaticKind<'tcx> {
|
|
|
|
|
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
|
|
|
|
|
match self {
|
|
|
|
|
StaticKind::Promoted(promoted, substs) =>
|
|
|
|
|
StaticKind::Promoted(promoted.fold_with(folder), substs.fold_with(folder)),
|
|
|
|
|
StaticKind::Static => StaticKind::Static
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
|
|
|
|
|
match self {
|
|
|
|
|
StaticKind::Promoted(promoted, substs) =>
|
|
|
|
|
promoted.visit_with(visitor) || substs.visit_with(visitor),
|
|
|
|
|
StaticKind::Static => { false }
|
|
|
|
|
}
|
2017-02-08 22:24:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for Rvalue<'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
|
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
|
2019-02-05 11:20:45 -06:00
|
|
|
|
use crate::mir::Rvalue::*;
|
2017-02-08 22:24:49 +13:00
|
|
|
|
match *self {
|
|
|
|
|
Use(ref op) => Use(op.fold_with(folder)),
|
|
|
|
|
Repeat(ref op, len) => Repeat(op.fold_with(folder), len),
|
2018-06-19 21:22:52 -03:00
|
|
|
|
Ref(region, bk, ref place) => {
|
|
|
|
|
Ref(region.fold_with(folder), bk, place.fold_with(folder))
|
|
|
|
|
}
|
2017-12-06 09:25:29 +01:00
|
|
|
|
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)),
|
2018-06-19 21:22:52 -03:00
|
|
|
|
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))
|
|
|
|
|
}
|
2017-02-08 22:24:49 +13:00
|
|
|
|
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,
|
2018-08-30 15:06:27 -03:00
|
|
|
|
AggregateKind::Adt(def, v, substs, user_ty, n) => AggregateKind::Adt(
|
|
|
|
|
def,
|
|
|
|
|
v,
|
|
|
|
|
substs.fold_with(folder),
|
|
|
|
|
user_ty.fold_with(folder),
|
|
|
|
|
n,
|
|
|
|
|
),
|
2018-06-19 21:22:52 -03:00
|
|
|
|
AggregateKind::Closure(id, substs) => {
|
|
|
|
|
AggregateKind::Closure(id, substs.fold_with(folder))
|
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-02-05 11:20:45 -06:00
|
|
|
|
use crate::mir::Rvalue::*;
|
2017-02-08 22:24:49 +13:00
|
|
|
|
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),
|
2018-06-19 21:22:52 -03:00
|
|
|
|
BinaryOp(_, ref rhs, ref lhs) | CheckedBinaryOp(_, ref rhs, ref lhs) => {
|
|
|
|
|
rhs.visit_with(visitor) || lhs.visit_with(visitor)
|
|
|
|
|
}
|
2017-02-08 22:24:49 +13:00
|
|
|
|
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,
|
2018-08-30 15:06:27 -03:00
|
|
|
|
AggregateKind::Adt(_, _, substs, user_ty, _) => {
|
|
|
|
|
substs.visit_with(visitor) || user_ty.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> {
|
2019-06-14 00:48:52 +03:00
|
|
|
|
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
|
2017-02-08 22:24:49 +13:00
|
|
|
|
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 {
|
2018-06-19 21:22:52 -03:00
|
|
|
|
Operand::Copy(ref place) | Operand::Move(ref place) => place.visit_with(visitor),
|
|
|
|
|
Operand::Constant(ref c) => c.visit_with(visitor),
|
2017-02-08 22:24:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-30 00:07:28 +02:00
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for PlaceElem<'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
|
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
|
2019-02-05 11:20:45 -06:00
|
|
|
|
use crate::mir::ProjectionElem::*;
|
2017-02-08 22:24:49 +13:00
|
|
|
|
|
2019-07-30 00:07:28 +02:00
|
|
|
|
match self {
|
2017-02-08 22:24:49 +13:00
|
|
|
|
Deref => Deref,
|
2019-07-30 00:07:28 +02:00
|
|
|
|
Field(f, ty) => Field(*f, ty.fold_with(folder)),
|
|
|
|
|
Index(v) => Index(v.fold_with(folder)),
|
|
|
|
|
elem => elem.clone(),
|
|
|
|
|
}
|
2017-02-08 22:24:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn super_visit_with<Vs: TypeVisitor<'tcx>>(&self, visitor: &mut Vs) -> bool {
|
2019-02-05 11:20:45 -06:00
|
|
|
|
use crate::mir::ProjectionElem::*;
|
2017-02-08 22:24:49 +13:00
|
|
|
|
|
2019-07-30 00:07:28 +02:00
|
|
|
|
match self {
|
|
|
|
|
Field(_, ty) => ty.visit_with(visitor),
|
|
|
|
|
Index(v) => v.visit_with(visitor),
|
|
|
|
|
_ => false,
|
|
|
|
|
}
|
2017-02-08 22:24:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-16 09:24:38 +01:00
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for Field {
|
2019-06-14 00:48:52 +03:00
|
|
|
|
fn super_fold_with<F: TypeFolder<'tcx>>(&self, _: &mut F) -> Self {
|
2018-01-16 09:24:38 +01:00
|
|
|
|
*self
|
|
|
|
|
}
|
|
|
|
|
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> bool {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-03 16:11:39 -07:00
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for GeneratorSavedLocal {
|
2019-06-14 00:48:52 +03:00
|
|
|
|
fn super_fold_with<F: TypeFolder<'tcx>>(&self, _: &mut F) -> Self {
|
2019-05-03 16:03:05 -07:00
|
|
|
|
*self
|
|
|
|
|
}
|
|
|
|
|
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> bool {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-31 16:53:27 -07:00
|
|
|
|
impl<'tcx, R: Idx, C: Idx> TypeFoldable<'tcx> for BitMatrix<R, C> {
|
2019-06-14 00:48:52 +03:00
|
|
|
|
fn super_fold_with<F: TypeFolder<'tcx>>(&self, _: &mut F) -> Self {
|
2019-05-30 14:27:12 -07:00
|
|
|
|
self.clone()
|
|
|
|
|
}
|
|
|
|
|
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> {
|
2019-06-14 00:48:52 +03:00
|
|
|
|
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
|
2017-02-08 22:24:49 +13:00
|
|
|
|
Constant {
|
|
|
|
|
span: self.span.clone(),
|
2018-08-09 06:18:00 -04:00
|
|
|
|
user_ty: self.user_ty.fold_with(folder),
|
2018-06-19 21:22:52 -03:00
|
|
|
|
literal: self.literal.fold_with(folder),
|
2017-02-08 22:24:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
|
2019-08-12 18:15:13 +03:00
|
|
|
|
self.literal.visit_with(visitor)
|
2017-02-08 22:24:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|