1 //! MIR datatypes and passes. See the [rustc dev guide] for more info.
3 //! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/mir/index.html
5 use crate::mir::coverage::{CodeRegion, CoverageKind};
6 use crate::mir::interpret::{Allocation, ConstValue, GlobalAlloc, Scalar};
7 use crate::mir::visit::MirVisitable;
8 use crate::ty::adjustment::PointerCast;
9 use crate::ty::codec::{TyDecoder, TyEncoder};
10 use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
11 use crate::ty::print::{FmtPrinter, Printer};
12 use crate::ty::subst::{Subst, SubstsRef};
14 self, AdtDef, CanonicalUserTypeAnnotations, List, Region, Ty, TyCtxt, UserTypeAnnotationIndex,
17 use rustc_hir::def::{CtorKind, Namespace};
18 use rustc_hir::def_id::DefId;
19 use rustc_hir::{self, GeneratorKind};
20 use rustc_target::abi::VariantIdx;
22 use polonius_engine::Atom;
23 pub use rustc_ast::Mutability;
24 use rustc_data_structures::fx::FxHashSet;
25 use rustc_data_structures::graph::dominators::{dominators, Dominators};
26 use rustc_data_structures::graph::{self, GraphSuccessors};
27 use rustc_index::bit_set::BitMatrix;
28 use rustc_index::vec::{Idx, IndexVec};
29 use rustc_serialize::{Decodable, Encodable};
30 use rustc_span::symbol::Symbol;
31 use rustc_span::{Span, DUMMY_SP};
32 use rustc_target::abi;
33 use rustc_target::asm::InlineAsmRegOrRegClass;
35 use std::fmt::{self, Debug, Display, Formatter, Write};
36 use std::ops::{Index, IndexMut};
38 use std::{iter, mem, option};
40 use self::predecessors::{PredecessorCache, Predecessors};
41 pub use self::query::*;
50 pub use terminator::*;
56 type LocalDecls<'tcx> = IndexVec<Local, LocalDecl<'tcx>>;
58 pub trait HasLocalDecls<'tcx> {
59 fn local_decls(&self) -> &LocalDecls<'tcx>;
62 impl<'tcx> HasLocalDecls<'tcx> for LocalDecls<'tcx> {
63 fn local_decls(&self) -> &LocalDecls<'tcx> {
68 impl<'tcx> HasLocalDecls<'tcx> for Body<'tcx> {
69 fn local_decls(&self) -> &LocalDecls<'tcx> {
74 /// The various "big phases" that MIR goes through.
76 /// Warning: ordering of variants is significant.
77 #[derive(Copy, Clone, TyEncodable, TyDecodable, Debug, PartialEq, Eq, PartialOrd, Ord)]
88 /// Gets the index of the current MirPhase within the set of all `MirPhase`s.
89 pub fn phase_index(&self) -> usize {
94 /// The lowered representation of a single function.
95 #[derive(Clone, TyEncodable, TyDecodable, Debug, HashStable, TypeFoldable)]
96 pub struct Body<'tcx> {
97 /// A list of basic blocks. References to basic block use a newtyped index type `BasicBlock`
98 /// that indexes into this vector.
99 basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
101 /// Records how far through the "desugaring and optimization" process this particular
102 /// MIR has traversed. This is particularly useful when inlining, since in that context
103 /// we instantiate the promoted constants and add them to our promoted vector -- but those
104 /// promoted items have already been optimized, whereas ours have not. This field allows
105 /// us to see the difference and forego optimization on the inlined promoted items.
108 /// A list of source scopes; these are referenced by statements
109 /// and used for debuginfo. Indexed by a `SourceScope`.
110 pub source_scopes: IndexVec<SourceScope, SourceScopeData>,
112 /// The yield type of the function, if it is a generator.
113 pub yield_ty: Option<Ty<'tcx>>,
115 /// Generator drop glue.
116 pub generator_drop: Option<Box<Body<'tcx>>>,
118 /// The layout of a generator. Produced by the state transformation.
119 pub generator_layout: Option<GeneratorLayout<'tcx>>,
121 /// If this is a generator then record the type of source expression that caused this generator
123 pub generator_kind: Option<GeneratorKind>,
125 /// Declarations of locals.
127 /// The first local is the return value pointer, followed by `arg_count`
128 /// locals for the function arguments, followed by any user-declared
129 /// variables and temporaries.
130 pub local_decls: LocalDecls<'tcx>,
132 /// User type annotations.
133 pub user_type_annotations: CanonicalUserTypeAnnotations<'tcx>,
135 /// The number of arguments this function takes.
137 /// Starting at local 1, `arg_count` locals will be provided by the caller
138 /// and can be assumed to be initialized.
140 /// If this MIR was built for a constant, this will be 0.
141 pub arg_count: usize,
143 /// Mark an argument local (which must be a tuple) as getting passed as
144 /// its individual components at the LLVM level.
146 /// This is used for the "rust-call" ABI.
147 pub spread_arg: Option<Local>,
149 /// Debug information pertaining to user variables, including captures.
150 pub var_debug_info: Vec<VarDebugInfo<'tcx>>,
152 /// A span representing this MIR, for error reporting.
155 /// Constants that are required to evaluate successfully for this MIR to be well-formed.
156 /// We hold in this field all the constants we are not able to evaluate yet.
157 pub required_consts: Vec<Constant<'tcx>>,
159 /// The user may be writing e.g. `&[(SOME_CELL, 42)][i].1` and this would get promoted, because
160 /// we'd statically know that no thing with interior mutability will ever be available to the
161 /// user without some serious unsafe code. Now this means that our promoted is actually
162 /// `&[(SOME_CELL, 42)]` and the MIR using it will do the `&promoted[i].1` projection because
163 /// the index may be a runtime value. Such a promoted value is illegal because it has reachable
164 /// interior mutability. This flag just makes this situation very obvious where the previous
165 /// implementation without the flag hid this situation silently.
166 /// FIXME(oli-obk): rewrite the promoted during promotion to eliminate the cell components.
167 pub ignore_interior_mut_in_const_validation: bool,
169 predecessor_cache: PredecessorCache,
172 impl<'tcx> Body<'tcx> {
174 basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
175 source_scopes: IndexVec<SourceScope, SourceScopeData>,
176 local_decls: LocalDecls<'tcx>,
177 user_type_annotations: CanonicalUserTypeAnnotations<'tcx>,
179 var_debug_info: Vec<VarDebugInfo<'tcx>>,
181 generator_kind: Option<GeneratorKind>,
183 // We need `arg_count` locals, and one for the return place.
185 local_decls.len() > arg_count,
186 "expected at least {} locals, got {}",
192 phase: MirPhase::Build,
196 generator_drop: None,
197 generator_layout: None,
200 user_type_annotations,
205 required_consts: Vec::new(),
206 ignore_interior_mut_in_const_validation: false,
207 predecessor_cache: PredecessorCache::new(),
211 /// Returns a partially initialized MIR body containing only a list of basic blocks.
213 /// The returned MIR contains no `LocalDecl`s (even for the return place) or source scopes. It
214 /// is only useful for testing but cannot be `#[cfg(test)]` because it is used in a different
216 pub fn new_cfg_only(basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>) -> Self {
218 phase: MirPhase::Build,
220 source_scopes: IndexVec::new(),
222 generator_drop: None,
223 generator_layout: None,
224 local_decls: IndexVec::new(),
225 user_type_annotations: IndexVec::new(),
229 required_consts: Vec::new(),
230 generator_kind: None,
231 var_debug_info: Vec::new(),
232 ignore_interior_mut_in_const_validation: false,
233 predecessor_cache: PredecessorCache::new(),
238 pub fn basic_blocks(&self) -> &IndexVec<BasicBlock, BasicBlockData<'tcx>> {
243 pub fn basic_blocks_mut(&mut self) -> &mut IndexVec<BasicBlock, BasicBlockData<'tcx>> {
244 // Because the user could mutate basic block terminators via this reference, we need to
245 // invalidate the predecessor cache.
247 // FIXME: Use a finer-grained API for this, so only transformations that alter terminators
248 // invalidate the predecessor cache.
249 self.predecessor_cache.invalidate();
250 &mut self.basic_blocks
254 pub fn basic_blocks_and_local_decls_mut(
256 ) -> (&mut IndexVec<BasicBlock, BasicBlockData<'tcx>>, &mut LocalDecls<'tcx>) {
257 self.predecessor_cache.invalidate();
258 (&mut self.basic_blocks, &mut self.local_decls)
262 pub fn basic_blocks_local_decls_mut_and_var_debug_info(
265 &mut IndexVec<BasicBlock, BasicBlockData<'tcx>>,
266 &mut LocalDecls<'tcx>,
267 &mut Vec<VarDebugInfo<'tcx>>,
269 self.predecessor_cache.invalidate();
270 (&mut self.basic_blocks, &mut self.local_decls, &mut self.var_debug_info)
273 /// Returns `true` if a cycle exists in the control-flow graph that is reachable from the
275 pub fn is_cfg_cyclic(&self) -> bool {
276 graph::is_cyclic(self)
280 pub fn local_kind(&self, local: Local) -> LocalKind {
281 let index = local.as_usize();
284 self.local_decls[local].mutability == Mutability::Mut,
285 "return place should be mutable"
288 LocalKind::ReturnPointer
289 } else if index < self.arg_count + 1 {
291 } else if self.local_decls[local].is_user_variable() {
298 /// Returns an iterator over all temporaries.
300 pub fn temps_iter<'a>(&'a self) -> impl Iterator<Item = Local> + 'a {
301 (self.arg_count + 1..self.local_decls.len()).filter_map(move |index| {
302 let local = Local::new(index);
303 if self.local_decls[local].is_user_variable() { None } else { Some(local) }
307 /// Returns an iterator over all user-declared locals.
309 pub fn vars_iter<'a>(&'a self) -> impl Iterator<Item = Local> + 'a {
310 (self.arg_count + 1..self.local_decls.len()).filter_map(move |index| {
311 let local = Local::new(index);
312 self.local_decls[local].is_user_variable().then_some(local)
316 /// Returns an iterator over all user-declared mutable locals.
318 pub fn mut_vars_iter<'a>(&'a self) -> impl Iterator<Item = Local> + 'a {
319 (self.arg_count + 1..self.local_decls.len()).filter_map(move |index| {
320 let local = Local::new(index);
321 let decl = &self.local_decls[local];
322 if decl.is_user_variable() && decl.mutability == Mutability::Mut {
330 /// Returns an iterator over all user-declared mutable arguments and locals.
332 pub fn mut_vars_and_args_iter<'a>(&'a self) -> impl Iterator<Item = Local> + 'a {
333 (1..self.local_decls.len()).filter_map(move |index| {
334 let local = Local::new(index);
335 let decl = &self.local_decls[local];
336 if (decl.is_user_variable() || index < self.arg_count + 1)
337 && decl.mutability == Mutability::Mut
346 /// Returns an iterator over all function arguments.
348 pub fn args_iter(&self) -> impl Iterator<Item = Local> + ExactSizeIterator {
349 let arg_count = self.arg_count;
350 (1..arg_count + 1).map(Local::new)
353 /// Returns an iterator over all user-defined variables and compiler-generated temporaries (all
354 /// locals that are neither arguments nor the return place).
356 pub fn vars_and_temps_iter(&self) -> impl Iterator<Item = Local> + ExactSizeIterator {
357 let arg_count = self.arg_count;
358 let local_count = self.local_decls.len();
359 (arg_count + 1..local_count).map(Local::new)
362 /// Changes a statement to a nop. This is both faster than deleting instructions and avoids
363 /// invalidating statement indices in `Location`s.
364 pub fn make_statement_nop(&mut self, location: Location) {
365 let block = &mut self.basic_blocks[location.block];
366 debug_assert!(location.statement_index < block.statements.len());
367 block.statements[location.statement_index].make_nop()
370 /// Returns the source info associated with `location`.
371 pub fn source_info(&self, location: Location) -> &SourceInfo {
372 let block = &self[location.block];
373 let stmts = &block.statements;
374 let idx = location.statement_index;
375 if idx < stmts.len() {
376 &stmts[idx].source_info
378 assert_eq!(idx, stmts.len());
379 &block.terminator().source_info
383 /// Checks if `sub` is a sub scope of `sup`
384 pub fn is_sub_scope(&self, mut sub: SourceScope, sup: SourceScope) -> bool {
386 match self.source_scopes[sub].parent_scope {
387 None => return false,
394 /// Returns the return type; it always return first element from `local_decls` array.
396 pub fn return_ty(&self) -> Ty<'tcx> {
397 self.local_decls[RETURN_PLACE].ty
400 /// Gets the location of the terminator for the given block.
402 pub fn terminator_loc(&self, bb: BasicBlock) -> Location {
403 Location { block: bb, statement_index: self[bb].statements.len() }
407 pub fn predecessors(&self) -> impl std::ops::Deref<Target = Predecessors> + '_ {
408 self.predecessor_cache.compute(&self.basic_blocks)
412 pub fn dominators(&self) -> Dominators<BasicBlock> {
417 #[derive(Copy, Clone, PartialEq, Eq, Debug, TyEncodable, TyDecodable, HashStable)]
420 /// Unsafe because of a PushUnsafeBlock
422 /// Unsafe because of an unsafe fn
424 /// Unsafe because of an `unsafe` block
425 ExplicitUnsafe(hir::HirId),
428 impl<'tcx> Index<BasicBlock> for Body<'tcx> {
429 type Output = BasicBlockData<'tcx>;
432 fn index(&self, index: BasicBlock) -> &BasicBlockData<'tcx> {
433 &self.basic_blocks()[index]
437 impl<'tcx> IndexMut<BasicBlock> for Body<'tcx> {
439 fn index_mut(&mut self, index: BasicBlock) -> &mut BasicBlockData<'tcx> {
440 &mut self.basic_blocks_mut()[index]
444 #[derive(Copy, Clone, Debug, HashStable, TypeFoldable)]
445 pub enum ClearCrossCrate<T> {
450 impl<T> ClearCrossCrate<T> {
451 pub fn as_ref(&self) -> ClearCrossCrate<&T> {
453 ClearCrossCrate::Clear => ClearCrossCrate::Clear,
454 ClearCrossCrate::Set(v) => ClearCrossCrate::Set(v),
458 pub fn assert_crate_local(self) -> T {
460 ClearCrossCrate::Clear => bug!("unwrapping cross-crate data"),
461 ClearCrossCrate::Set(v) => v,
466 const TAG_CLEAR_CROSS_CRATE_CLEAR: u8 = 0;
467 const TAG_CLEAR_CROSS_CRATE_SET: u8 = 1;
469 impl<'tcx, E: TyEncoder<'tcx>, T: Encodable<E>> Encodable<E> for ClearCrossCrate<T> {
471 fn encode(&self, e: &mut E) -> Result<(), E::Error> {
472 if E::CLEAR_CROSS_CRATE {
477 ClearCrossCrate::Clear => TAG_CLEAR_CROSS_CRATE_CLEAR.encode(e),
478 ClearCrossCrate::Set(ref val) => {
479 TAG_CLEAR_CROSS_CRATE_SET.encode(e)?;
485 impl<'tcx, D: TyDecoder<'tcx>, T: Decodable<D>> Decodable<D> for ClearCrossCrate<T> {
487 fn decode(d: &mut D) -> Result<ClearCrossCrate<T>, D::Error> {
488 if D::CLEAR_CROSS_CRATE {
489 return Ok(ClearCrossCrate::Clear);
492 let discr = u8::decode(d)?;
495 TAG_CLEAR_CROSS_CRATE_CLEAR => Ok(ClearCrossCrate::Clear),
496 TAG_CLEAR_CROSS_CRATE_SET => {
497 let val = T::decode(d)?;
498 Ok(ClearCrossCrate::Set(val))
500 tag => Err(d.error(&format!("Invalid tag for ClearCrossCrate: {:?}", tag))),
505 /// Grouped information about the source code origin of a MIR entity.
506 /// Intended to be inspected by diagnostics and debuginfo.
507 /// Most passes can work with it as a whole, within a single function.
508 // The unofficial Cranelift backend, at least as of #65828, needs `SourceInfo` to implement `Eq` and
509 // `Hash`. Please ping @bjorn3 if removing them.
510 #[derive(Copy, Clone, Debug, Eq, PartialEq, TyEncodable, TyDecodable, Hash, HashStable)]
511 pub struct SourceInfo {
512 /// The source span for the AST pertaining to this MIR entity.
515 /// The source scope, keeping track of which bindings can be
516 /// seen by debuginfo, active lint levels, `unsafe {...}`, etc.
517 pub scope: SourceScope,
522 pub fn outermost(span: Span) -> Self {
523 SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE }
527 ///////////////////////////////////////////////////////////////////////////
530 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, TyEncodable, TyDecodable)]
531 #[derive(HashStable)]
532 pub enum BorrowKind {
533 /// Data must be immutable and is aliasable.
536 /// The immediately borrowed place must be immutable, but projections from
537 /// it don't need to be. For example, a shallow borrow of `a.b` doesn't
538 /// conflict with a mutable borrow of `a.b.c`.
540 /// This is used when lowering matches: when matching on a place we want to
541 /// ensure that place have the same value from the start of the match until
542 /// an arm is selected. This prevents this code from compiling:
544 /// let mut x = &Some(0);
547 /// Some(_) if { x = &None; false } => (),
551 /// This can't be a shared borrow because mutably borrowing (*x as Some).0
552 /// should not prevent `if let None = x { ... }`, for example, because the
553 /// mutating `(*x as Some).0` can't affect the discriminant of `x`.
554 /// We can also report errors with this kind of borrow differently.
557 /// Data must be immutable but not aliasable. This kind of borrow
558 /// cannot currently be expressed by the user and is used only in
559 /// implicit closure bindings. It is needed when the closure is
560 /// borrowing or mutating a mutable referent, e.g.:
562 /// let x: &mut isize = ...;
563 /// let y = || *x += 5;
565 /// If we were to try to translate this closure into a more explicit
566 /// form, we'd encounter an error with the code as written:
568 /// struct Env { x: & &mut isize }
569 /// let x: &mut isize = ...;
570 /// let y = (&mut Env { &x }, fn_ptr); // Closure is pair of env and fn
571 /// fn fn_ptr(env: &mut Env) { **env.x += 5; }
573 /// This is then illegal because you cannot mutate an `&mut` found
574 /// in an aliasable location. To solve, you'd have to translate with
575 /// an `&mut` borrow:
577 /// struct Env { x: & &mut isize }
578 /// let x: &mut isize = ...;
579 /// let y = (&mut Env { &mut x }, fn_ptr); // changed from &x to &mut x
580 /// fn fn_ptr(env: &mut Env) { **env.x += 5; }
582 /// Now the assignment to `**env.x` is legal, but creating a
583 /// mutable pointer to `x` is not because `x` is not mutable. We
584 /// could fix this by declaring `x` as `let mut x`. This is ok in
585 /// user code, if awkward, but extra weird for closures, since the
586 /// borrow is hidden.
588 /// So we introduce a "unique imm" borrow -- the referent is
589 /// immutable, but not aliasable. This solves the problem. For
590 /// simplicity, we don't give users the way to express this
591 /// borrow, it's just used when translating closures.
594 /// Data is mutable and not aliasable.
596 /// `true` if this borrow arose from method-call auto-ref
597 /// (i.e., `adjustment::Adjust::Borrow`).
598 allow_two_phase_borrow: bool,
603 pub fn allows_two_phase_borrow(&self) -> bool {
605 BorrowKind::Shared | BorrowKind::Shallow | BorrowKind::Unique => false,
606 BorrowKind::Mut { allow_two_phase_borrow } => allow_two_phase_borrow,
611 ///////////////////////////////////////////////////////////////////////////
612 // Variables and temps
614 rustc_index::newtype_index! {
617 DEBUG_FORMAT = "_{}",
618 const RETURN_PLACE = 0,
622 impl Atom for Local {
623 fn index(self) -> usize {
628 /// Classifies locals into categories. See `Body::local_kind`.
629 #[derive(PartialEq, Eq, Debug, HashStable)]
631 /// User-declared variable binding.
633 /// Compiler-introduced temporary.
635 /// Function argument.
637 /// Location of function's return value.
641 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
642 pub struct VarBindingForm<'tcx> {
643 /// Is variable bound via `x`, `mut x`, `ref x`, or `ref mut x`?
644 pub binding_mode: ty::BindingMode,
645 /// If an explicit type was provided for this variable binding,
646 /// this holds the source Span of that type.
648 /// NOTE: if you want to change this to a `HirId`, be wary that
649 /// doing so breaks incremental compilation (as of this writing),
650 /// while a `Span` does not cause our tests to fail.
651 pub opt_ty_info: Option<Span>,
652 /// Place of the RHS of the =, or the subject of the `match` where this
653 /// variable is initialized. None in the case of `let PATTERN;`.
654 /// Some((None, ..)) in the case of and `let [mut] x = ...` because
655 /// (a) the right-hand side isn't evaluated as a place expression.
656 /// (b) it gives a way to separate this case from the remaining cases
658 pub opt_match_place: Option<(Option<Place<'tcx>>, Span)>,
659 /// The span of the pattern in which this variable was bound.
663 #[derive(Clone, Debug, TyEncodable, TyDecodable)]
664 pub enum BindingForm<'tcx> {
665 /// This is a binding for a non-`self` binding, or a `self` that has an explicit type.
666 Var(VarBindingForm<'tcx>),
667 /// Binding for a `self`/`&self`/`&mut self` binding where the type is implicit.
668 ImplicitSelf(ImplicitSelfKind),
669 /// Reference used in a guard expression to ensure immutability.
673 /// Represents what type of implicit self a function has, if any.
674 #[derive(Clone, Copy, PartialEq, Debug, TyEncodable, TyDecodable, HashStable)]
675 pub enum ImplicitSelfKind {
676 /// Represents a `fn x(self);`.
678 /// Represents a `fn x(mut self);`.
680 /// Represents a `fn x(&self);`.
682 /// Represents a `fn x(&mut self);`.
684 /// Represents when a function does not have a self argument or
685 /// when a function has a `self: X` argument.
689 CloneTypeFoldableAndLiftImpls! { BindingForm<'tcx>, }
691 mod binding_form_impl {
692 use crate::ich::StableHashingContext;
693 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
695 impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for super::BindingForm<'tcx> {
696 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
697 use super::BindingForm::*;
698 ::std::mem::discriminant(self).hash_stable(hcx, hasher);
701 Var(binding) => binding.hash_stable(hcx, hasher),
702 ImplicitSelf(kind) => kind.hash_stable(hcx, hasher),
709 /// `BlockTailInfo` is attached to the `LocalDecl` for temporaries
710 /// created during evaluation of expressions in a block tail
711 /// expression; that is, a block like `{ STMT_1; STMT_2; EXPR }`.
713 /// It is used to improve diagnostics when such temporaries are
714 /// involved in borrow_check errors, e.g., explanations of where the
715 /// temporaries come from, when their destructors are run, and/or how
716 /// one might revise the code to satisfy the borrow checker's rules.
717 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
718 pub struct BlockTailInfo {
719 /// If `true`, then the value resulting from evaluating this tail
720 /// expression is ignored by the block's expression context.
722 /// Examples include `{ ...; tail };` and `let _ = { ...; tail };`
723 /// but not e.g., `let _x = { ...; tail };`
724 pub tail_result_is_ignored: bool,
726 /// `Span` of the tail expression.
732 /// This can be a binding declared by the user, a temporary inserted by the compiler, a function
733 /// argument, or the return place.
734 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
735 pub struct LocalDecl<'tcx> {
736 /// Whether this is a mutable minding (i.e., `let x` or `let mut x`).
738 /// Temporaries and the return place are always mutable.
739 pub mutability: Mutability,
741 // FIXME(matthewjasper) Don't store in this in `Body`
742 pub local_info: Option<Box<LocalInfo<'tcx>>>,
744 /// `true` if this is an internal local.
746 /// These locals are not based on types in the source code and are only used
747 /// for a few desugarings at the moment.
749 /// The generator transformation will sanity check the locals which are live
750 /// across a suspension point against the type components of the generator
751 /// which type checking knows are live across a suspension point. We need to
752 /// flag drop flags to avoid triggering this check as they are introduced
755 /// Unsafety checking will also ignore dereferences of these locals,
756 /// so they can be used for raw pointers only used in a desugaring.
758 /// This should be sound because the drop flags are fully algebraic, and
759 /// therefore don't affect the OIBIT or outlives properties of the
763 /// If this local is a temporary and `is_block_tail` is `Some`,
764 /// then it is a temporary created for evaluation of some
765 /// subexpression of some block's tail expression (with no
766 /// intervening statement context).
767 // FIXME(matthewjasper) Don't store in this in `Body`
768 pub is_block_tail: Option<BlockTailInfo>,
770 /// The type of this local.
773 /// If the user manually ascribed a type to this variable,
774 /// e.g., via `let x: T`, then we carry that type here. The MIR
775 /// borrow checker needs this information since it can affect
776 /// region inference.
777 // FIXME(matthewjasper) Don't store in this in `Body`
778 pub user_ty: Option<Box<UserTypeProjections>>,
780 /// The *syntactic* (i.e., not visibility) source scope the local is defined
781 /// in. If the local was defined in a let-statement, this
782 /// is *within* the let-statement, rather than outside
785 /// This is needed because the visibility source scope of locals within
786 /// a let-statement is weird.
788 /// The reason is that we want the local to be *within* the let-statement
789 /// for lint purposes, but we want the local to be *after* the let-statement
790 /// for names-in-scope purposes.
792 /// That's it, if we have a let-statement like the one in this
796 /// fn foo(x: &str) {
797 /// #[allow(unused_mut)]
798 /// let mut x: u32 = { // <- one unused mut
799 /// let mut y: u32 = x.parse().unwrap();
806 /// Then, from a lint point of view, the declaration of `x: u32`
807 /// (and `y: u32`) are within the `#[allow(unused_mut)]` scope - the
808 /// lint scopes are the same as the AST/HIR nesting.
810 /// However, from a name lookup point of view, the scopes look more like
811 /// as if the let-statements were `match` expressions:
814 /// fn foo(x: &str) {
816 /// match x.parse().unwrap() {
825 /// We care about the name-lookup scopes for debuginfo - if the
826 /// debuginfo instruction pointer is at the call to `x.parse()`, we
827 /// want `x` to refer to `x: &str`, but if it is at the call to
828 /// `drop(x)`, we want it to refer to `x: u32`.
830 /// To allow both uses to work, we need to have more than a single scope
831 /// for a local. We have the `source_info.scope` represent the "syntactic"
832 /// lint scope (with a variable being under its let block) while the
833 /// `var_debug_info.source_info.scope` represents the "local variable"
834 /// scope (where the "rest" of a block is under all prior let-statements).
836 /// The end result looks like this:
840 /// │{ argument x: &str }
842 /// │ │{ #[allow(unused_mut)] } // This is actually split into 2 scopes
843 /// │ │ // in practice because I'm lazy.
845 /// │ │← x.source_info.scope
846 /// │ │← `x.parse().unwrap()`
848 /// │ │ │← y.source_info.scope
850 /// │ │ │{ let y: u32 }
852 /// │ │ │← y.var_debug_info.source_info.scope
855 /// │ │{ let x: u32 }
856 /// │ │← x.var_debug_info.source_info.scope
857 /// │ │← `drop(x)` // This accesses `x: u32`.
859 pub source_info: SourceInfo,
862 // `LocalDecl` is used a lot. Make sure it doesn't unintentionally get bigger.
863 #[cfg(target_arch = "x86_64")]
864 static_assert_size!(LocalDecl<'_>, 56);
866 /// Extra information about a some locals that's used for diagnostics and for
867 /// classifying variables into local variables, statics, etc, which is needed e.g.
868 /// for unsafety checking.
870 /// Not used for non-StaticRef temporaries, the return place, or anonymous
871 /// function parameters.
872 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
873 pub enum LocalInfo<'tcx> {
874 /// A user-defined local variable or function parameter
876 /// The `BindingForm` is solely used for local diagnostics when generating
877 /// warnings/errors when compiling the current crate, and therefore it need
878 /// not be visible across crates.
879 User(ClearCrossCrate<BindingForm<'tcx>>),
880 /// A temporary created that references the static with the given `DefId`.
881 StaticRef { def_id: DefId, is_thread_local: bool },
884 impl<'tcx> LocalDecl<'tcx> {
885 /// Returns `true` only if local is a binding that can itself be
886 /// made mutable via the addition of the `mut` keyword, namely
887 /// something like the occurrences of `x` in:
888 /// - `fn foo(x: Type) { ... }`,
890 /// - or `match ... { C(x) => ... }`
891 pub fn can_be_made_mutable(&self) -> bool {
892 match self.local_info {
893 Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
894 binding_mode: ty::BindingMode::BindByValue(_),
900 Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::ImplicitSelf(
901 ImplicitSelfKind::Imm,
908 /// Returns `true` if local is definitely not a `ref ident` or
909 /// `ref mut ident` binding. (Such bindings cannot be made into
910 /// mutable bindings, but the inverse does not necessarily hold).
911 pub fn is_nonref_binding(&self) -> bool {
912 match self.local_info {
913 Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
914 binding_mode: ty::BindingMode::BindByValue(_),
920 Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::ImplicitSelf(_)))) => true,
926 /// Returns `true` if this variable is a named variable or function
927 /// parameter declared by the user.
929 pub fn is_user_variable(&self) -> bool {
930 match self.local_info {
931 Some(box LocalInfo::User(_)) => true,
936 /// Returns `true` if this is a reference to a variable bound in a `match`
937 /// expression that is used to access said variable for the guard of the
939 pub fn is_ref_for_guard(&self) -> bool {
940 match self.local_info {
941 Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::RefForGuard))) => true,
946 /// Returns `Some` if this is a reference to a static item that is used to
947 /// access that static
948 pub fn is_ref_to_static(&self) -> bool {
949 match self.local_info {
950 Some(box LocalInfo::StaticRef { .. }) => true,
955 /// Returns `Some` if this is a reference to a static item that is used to
956 /// access that static
957 pub fn is_ref_to_thread_local(&self) -> bool {
958 match self.local_info {
959 Some(box LocalInfo::StaticRef { is_thread_local, .. }) => is_thread_local,
964 /// Returns `true` is the local is from a compiler desugaring, e.g.,
965 /// `__next` from a `for` loop.
967 pub fn from_compiler_desugaring(&self) -> bool {
968 self.source_info.span.desugaring_kind().is_some()
971 /// Creates a new `LocalDecl` for a temporary: mutable, non-internal.
973 pub fn new(ty: Ty<'tcx>, span: Span) -> Self {
974 Self::with_source_info(ty, SourceInfo::outermost(span))
977 /// Like `LocalDecl::new`, but takes a `SourceInfo` instead of a `Span`.
979 pub fn with_source_info(ty: Ty<'tcx>, source_info: SourceInfo) -> Self {
981 mutability: Mutability::Mut,
991 /// Converts `self` into same `LocalDecl` except tagged as internal.
993 pub fn internal(mut self) -> Self {
994 self.internal = true;
998 /// Converts `self` into same `LocalDecl` except tagged as immutable.
1000 pub fn immutable(mut self) -> Self {
1001 self.mutability = Mutability::Not;
1005 /// Converts `self` into same `LocalDecl` except tagged as internal temporary.
1007 pub fn block_tail(mut self, info: BlockTailInfo) -> Self {
1008 assert!(self.is_block_tail.is_none());
1009 self.is_block_tail = Some(info);
1014 /// Debug information pertaining to a user variable.
1015 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
1016 pub struct VarDebugInfo<'tcx> {
1019 /// Source info of the user variable, including the scope
1020 /// within which the variable is visible (to debuginfo)
1021 /// (see `LocalDecl`'s `source_info` field for more details).
1022 pub source_info: SourceInfo,
1024 /// Where the data for this user variable is to be found.
1025 /// NOTE(eddyb) There's an unenforced invariant that this `Place` is
1026 /// based on a `Local`, not a `Static`, and contains no indexing.
1027 pub place: Place<'tcx>,
1030 ///////////////////////////////////////////////////////////////////////////
1033 rustc_index::newtype_index! {
1034 pub struct BasicBlock {
1036 DEBUG_FORMAT = "bb{}",
1037 const START_BLOCK = 0,
1042 pub fn start_location(self) -> Location {
1043 Location { block: self, statement_index: 0 }
1047 ///////////////////////////////////////////////////////////////////////////
1048 // BasicBlockData and Terminator
1050 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
1051 pub struct BasicBlockData<'tcx> {
1052 /// List of statements in this block.
1053 pub statements: Vec<Statement<'tcx>>,
1055 /// Terminator for this block.
1057 /// N.B., this should generally ONLY be `None` during construction.
1058 /// Therefore, you should generally access it via the
1059 /// `terminator()` or `terminator_mut()` methods. The only
1060 /// exception is that certain passes, such as `simplify_cfg`, swap
1061 /// out the terminator temporarily with `None` while they continue
1062 /// to recurse over the set of basic blocks.
1063 pub terminator: Option<Terminator<'tcx>>,
1065 /// If true, this block lies on an unwind path. This is used
1066 /// during codegen where distinct kinds of basic blocks may be
1067 /// generated (particularly for MSVC cleanup). Unwind blocks must
1068 /// only branch to other unwind blocks.
1069 pub is_cleanup: bool,
1072 /// Information about an assertion failure.
1073 #[derive(Clone, TyEncodable, TyDecodable, HashStable, PartialEq)]
1074 pub enum AssertKind<O> {
1075 BoundsCheck { len: O, index: O },
1076 Overflow(BinOp, O, O),
1080 ResumedAfterReturn(GeneratorKind),
1081 ResumedAfterPanic(GeneratorKind),
1084 #[derive(Clone, Debug, PartialEq, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
1085 pub enum InlineAsmOperand<'tcx> {
1087 reg: InlineAsmRegOrRegClass,
1088 value: Operand<'tcx>,
1091 reg: InlineAsmRegOrRegClass,
1093 place: Option<Place<'tcx>>,
1096 reg: InlineAsmRegOrRegClass,
1098 in_value: Operand<'tcx>,
1099 out_place: Option<Place<'tcx>>,
1102 value: Operand<'tcx>,
1105 value: Box<Constant<'tcx>>,
1112 /// Type for MIR `Assert` terminator error messages.
1113 pub type AssertMessage<'tcx> = AssertKind<Operand<'tcx>>;
1115 pub type Successors<'a> =
1116 iter::Chain<option::IntoIter<&'a BasicBlock>, slice::Iter<'a, BasicBlock>>;
1117 pub type SuccessorsMut<'a> =
1118 iter::Chain<option::IntoIter<&'a mut BasicBlock>, slice::IterMut<'a, BasicBlock>>;
1120 impl<'tcx> BasicBlockData<'tcx> {
1121 pub fn new(terminator: Option<Terminator<'tcx>>) -> BasicBlockData<'tcx> {
1122 BasicBlockData { statements: vec![], terminator, is_cleanup: false }
1125 /// Accessor for terminator.
1127 /// Terminator may not be None after construction of the basic block is complete. This accessor
1128 /// provides a convenience way to reach the terminator.
1129 pub fn terminator(&self) -> &Terminator<'tcx> {
1130 self.terminator.as_ref().expect("invalid terminator state")
1133 pub fn terminator_mut(&mut self) -> &mut Terminator<'tcx> {
1134 self.terminator.as_mut().expect("invalid terminator state")
1137 pub fn retain_statements<F>(&mut self, mut f: F)
1139 F: FnMut(&mut Statement<'_>) -> bool,
1141 for s in &mut self.statements {
1148 pub fn expand_statements<F, I>(&mut self, mut f: F)
1150 F: FnMut(&mut Statement<'tcx>) -> Option<I>,
1151 I: iter::TrustedLen<Item = Statement<'tcx>>,
1153 // Gather all the iterators we'll need to splice in, and their positions.
1154 let mut splices: Vec<(usize, I)> = vec![];
1155 let mut extra_stmts = 0;
1156 for (i, s) in self.statements.iter_mut().enumerate() {
1157 if let Some(mut new_stmts) = f(s) {
1158 if let Some(first) = new_stmts.next() {
1159 // We can already store the first new statement.
1162 // Save the other statements for optimized splicing.
1163 let remaining = new_stmts.size_hint().0;
1165 splices.push((i + 1 + extra_stmts, new_stmts));
1166 extra_stmts += remaining;
1174 // Splice in the new statements, from the end of the block.
1175 // FIXME(eddyb) This could be more efficient with a "gap buffer"
1176 // where a range of elements ("gap") is left uninitialized, with
1177 // splicing adding new elements to the end of that gap and moving
1178 // existing elements from before the gap to the end of the gap.
1179 // For now, this is safe code, emulating a gap but initializing it.
1180 let mut gap = self.statements.len()..self.statements.len() + extra_stmts;
1181 self.statements.resize(
1183 Statement { source_info: SourceInfo::outermost(DUMMY_SP), kind: StatementKind::Nop },
1185 for (splice_start, new_stmts) in splices.into_iter().rev() {
1186 let splice_end = splice_start + new_stmts.size_hint().0;
1187 while gap.end > splice_end {
1190 self.statements.swap(gap.start, gap.end);
1192 self.statements.splice(splice_start..splice_end, new_stmts);
1193 gap.end = splice_start;
1197 pub fn visitable(&self, index: usize) -> &dyn MirVisitable<'tcx> {
1198 if index < self.statements.len() { &self.statements[index] } else { &self.terminator }
1202 impl<O> AssertKind<O> {
1203 /// Getting a description does not require `O` to be printable, and does not
1204 /// require allocation.
1205 /// The caller is expected to handle `BoundsCheck` separately.
1206 pub fn description(&self) -> &'static str {
1209 Overflow(BinOp::Add, _, _) => "attempt to add with overflow",
1210 Overflow(BinOp::Sub, _, _) => "attempt to subtract with overflow",
1211 Overflow(BinOp::Mul, _, _) => "attempt to multiply with overflow",
1212 Overflow(BinOp::Div, _, _) => "attempt to divide with overflow",
1213 Overflow(BinOp::Rem, _, _) => "attempt to calculate the remainder with overflow",
1214 OverflowNeg(_) => "attempt to negate with overflow",
1215 Overflow(BinOp::Shr, _, _) => "attempt to shift right with overflow",
1216 Overflow(BinOp::Shl, _, _) => "attempt to shift left with overflow",
1217 Overflow(op, _, _) => bug!("{:?} cannot overflow", op),
1218 DivisionByZero(_) => "attempt to divide by zero",
1219 RemainderByZero(_) => "attempt to calculate the remainder with a divisor of zero",
1220 ResumedAfterReturn(GeneratorKind::Gen) => "generator resumed after completion",
1221 ResumedAfterReturn(GeneratorKind::Async(_)) => "`async fn` resumed after completion",
1222 ResumedAfterPanic(GeneratorKind::Gen) => "generator resumed after panicking",
1223 ResumedAfterPanic(GeneratorKind::Async(_)) => "`async fn` resumed after panicking",
1224 BoundsCheck { .. } => bug!("Unexpected AssertKind"),
1228 /// Format the message arguments for the `assert(cond, msg..)` terminator in MIR printing.
1229 fn fmt_assert_args<W: Write>(&self, f: &mut W) -> fmt::Result
1235 BoundsCheck { ref len, ref index } => write!(
1237 "\"index out of bounds: the len is {{}} but the index is {{}}\", {:?}, {:?}",
1241 OverflowNeg(op) => {
1242 write!(f, "\"attempt to negate {{}} which would overflow\", {:?}", op)
1244 DivisionByZero(op) => write!(f, "\"attempt to divide {{}} by zero\", {:?}", op),
1245 RemainderByZero(op) => write!(
1247 "\"attempt to calculate the remainder of {{}} with a divisor of zero\", {:?}",
1250 Overflow(BinOp::Add, l, r) => write!(
1252 "\"attempt to compute `{{}} + {{}}` which would overflow\", {:?}, {:?}",
1255 Overflow(BinOp::Sub, l, r) => write!(
1257 "\"attempt to compute `{{}} - {{}}` which would overflow\", {:?}, {:?}",
1260 Overflow(BinOp::Mul, l, r) => write!(
1262 "\"attempt to compute `{{}} * {{}}` which would overflow\", {:?}, {:?}",
1265 Overflow(BinOp::Div, l, r) => write!(
1267 "\"attempt to compute `{{}} / {{}}` which would overflow\", {:?}, {:?}",
1270 Overflow(BinOp::Rem, l, r) => write!(
1272 "\"attempt to compute the remainder of `{{}} % {{}}` which would overflow\", {:?}, {:?}",
1275 Overflow(BinOp::Shr, _, r) => {
1276 write!(f, "\"attempt to shift right by {{}} which would overflow\", {:?}", r)
1278 Overflow(BinOp::Shl, _, r) => {
1279 write!(f, "\"attempt to shift left by {{}} which would overflow\", {:?}", r)
1281 _ => write!(f, "\"{}\"", self.description()),
1286 impl<O: fmt::Debug> fmt::Debug for AssertKind<O> {
1287 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1290 BoundsCheck { ref len, ref index } => {
1291 write!(f, "index out of bounds: the len is {:?} but the index is {:?}", len, index)
1293 OverflowNeg(op) => write!(f, "attempt to negate {:#?} which would overflow", op),
1294 DivisionByZero(op) => write!(f, "attempt to divide {:#?} by zero", op),
1295 RemainderByZero(op) => {
1296 write!(f, "attempt to calculate the remainder of {:#?} with a divisor of zero", op)
1298 Overflow(BinOp::Add, l, r) => {
1299 write!(f, "attempt to compute `{:#?} + {:#?}` which would overflow", l, r)
1301 Overflow(BinOp::Sub, l, r) => {
1302 write!(f, "attempt to compute `{:#?} - {:#?}` which would overflow", l, r)
1304 Overflow(BinOp::Mul, l, r) => {
1305 write!(f, "attempt to compute `{:#?} * {:#?}` which would overflow", l, r)
1307 Overflow(BinOp::Div, l, r) => {
1308 write!(f, "attempt to compute `{:#?} / {:#?}` which would overflow", l, r)
1310 Overflow(BinOp::Rem, l, r) => write!(
1312 "attempt to compute the remainder of `{:#?} % {:#?}` which would overflow",
1315 Overflow(BinOp::Shr, _, r) => {
1316 write!(f, "attempt to shift right by {:#?} which would overflow", r)
1318 Overflow(BinOp::Shl, _, r) => {
1319 write!(f, "attempt to shift left by {:#?} which would overflow", r)
1321 _ => write!(f, "{}", self.description()),
1326 ///////////////////////////////////////////////////////////////////////////
1329 #[derive(Clone, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
1330 pub struct Statement<'tcx> {
1331 pub source_info: SourceInfo,
1332 pub kind: StatementKind<'tcx>,
1335 // `Statement` is used a lot. Make sure it doesn't unintentionally get bigger.
1336 #[cfg(target_arch = "x86_64")]
1337 static_assert_size!(Statement<'_>, 32);
1339 impl Statement<'_> {
1340 /// Changes a statement to a nop. This is both faster than deleting instructions and avoids
1341 /// invalidating statement indices in `Location`s.
1342 pub fn make_nop(&mut self) {
1343 self.kind = StatementKind::Nop
1346 /// Changes a statement to a nop and returns the original statement.
1347 pub fn replace_nop(&mut self) -> Self {
1349 source_info: self.source_info,
1350 kind: mem::replace(&mut self.kind, StatementKind::Nop),
1355 #[derive(Clone, Debug, PartialEq, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
1356 pub enum StatementKind<'tcx> {
1357 /// Write the RHS Rvalue to the LHS Place.
1358 Assign(Box<(Place<'tcx>, Rvalue<'tcx>)>),
1360 /// This represents all the reading that a pattern match may do
1361 /// (e.g., inspecting constants and discriminant values), and the
1362 /// kind of pattern it comes from. This is in order to adapt potential
1363 /// error messages to these specific patterns.
1365 /// Note that this also is emitted for regular `let` bindings to ensure that locals that are
1366 /// never accessed still get some sanity checks for, e.g., `let x: ! = ..;`
1367 FakeRead(FakeReadCause, Box<Place<'tcx>>),
1369 /// Write the discriminant for a variant to the enum Place.
1370 SetDiscriminant { place: Box<Place<'tcx>>, variant_index: VariantIdx },
1372 /// Start a live range for the storage of the local.
1375 /// End the current live range for the storage of the local.
1378 /// Executes a piece of inline Assembly. Stored in a Box to keep the size
1379 /// of `StatementKind` low.
1380 LlvmInlineAsm(Box<LlvmInlineAsm<'tcx>>),
1382 /// Retag references in the given place, ensuring they got fresh tags. This is
1383 /// part of the Stacked Borrows model. These statements are currently only interpreted
1384 /// by miri and only generated when "-Z mir-emit-retag" is passed.
1385 /// See <https://internals.rust-lang.org/t/stacked-borrows-an-aliasing-model-for-rust/8153/>
1386 /// for more details.
1387 Retag(RetagKind, Box<Place<'tcx>>),
1389 /// Encodes a user's type ascription. These need to be preserved
1390 /// intact so that NLL can respect them. For example:
1394 /// The effect of this annotation is to relate the type `T_y` of the place `y`
1395 /// to the user-given type `T`. The effect depends on the specified variance:
1397 /// - `Covariant` -- requires that `T_y <: T`
1398 /// - `Contravariant` -- requires that `T_y :> T`
1399 /// - `Invariant` -- requires that `T_y == T`
1400 /// - `Bivariant` -- no effect
1401 AscribeUserType(Box<(Place<'tcx>, UserTypeProjection)>, ty::Variance),
1403 /// Marks the start of a "coverage region", injected with '-Zinstrument-coverage'. A
1404 /// `CoverageInfo` statement carries metadata about the coverage region, used to inject a coverage
1405 /// map into the binary. The `Counter` kind also generates executable code, to increment a
1406 /// counter varible at runtime, each time the code region is executed.
1407 Coverage(Box<Coverage>),
1409 /// No-op. Useful for deleting instructions without affecting statement indices.
1413 /// Describes what kind of retag is to be performed.
1414 #[derive(Copy, Clone, TyEncodable, TyDecodable, Debug, PartialEq, Eq, HashStable)]
1415 pub enum RetagKind {
1416 /// The initial retag when entering a function.
1418 /// Retag preparing for a two-phase borrow.
1420 /// Retagging raw pointers.
1422 /// A "normal" retag.
1426 /// The `FakeReadCause` describes the type of pattern why a FakeRead statement exists.
1427 #[derive(Copy, Clone, TyEncodable, TyDecodable, Debug, HashStable, PartialEq)]
1428 pub enum FakeReadCause {
1429 /// Inject a fake read of the borrowed input at the end of each guards
1432 /// This should ensure that you cannot change the variant for an enum while
1433 /// you are in the midst of matching on it.
1436 /// `let x: !; match x {}` doesn't generate any read of x so we need to
1437 /// generate a read of x to check that it is initialized and safe.
1440 /// A fake read of the RefWithinGuard version of a bind-by-value variable
1441 /// in a match guard to ensure that it's value hasn't change by the time
1442 /// we create the OutsideGuard version.
1445 /// Officially, the semantics of
1447 /// `let pattern = <expr>;`
1449 /// is that `<expr>` is evaluated into a temporary and then this temporary is
1450 /// into the pattern.
1452 /// However, if we see the simple pattern `let var = <expr>`, we optimize this to
1453 /// evaluate `<expr>` directly into the variable `var`. This is mostly unobservable,
1454 /// but in some cases it can affect the borrow checker, as in #53695.
1455 /// Therefore, we insert a "fake read" here to ensure that we get
1456 /// appropriate errors.
1459 /// If we have an index expression like
1461 /// (*x)[1][{ x = y; 4}]
1463 /// then the first bounds check is invalidated when we evaluate the second
1464 /// index expression. Thus we create a fake borrow of `x` across the second
1465 /// indexer, which will cause a borrow check error.
1469 #[derive(Clone, Debug, PartialEq, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
1470 pub struct LlvmInlineAsm<'tcx> {
1471 pub asm: hir::LlvmInlineAsmInner,
1472 pub outputs: Box<[Place<'tcx>]>,
1473 pub inputs: Box<[(Span, Operand<'tcx>)]>,
1476 impl Debug for Statement<'_> {
1477 fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
1478 use self::StatementKind::*;
1480 Assign(box (ref place, ref rv)) => write!(fmt, "{:?} = {:?}", place, rv),
1481 FakeRead(ref cause, ref place) => write!(fmt, "FakeRead({:?}, {:?})", cause, place),
1482 Retag(ref kind, ref place) => write!(
1486 RetagKind::FnEntry => "[fn entry] ",
1487 RetagKind::TwoPhase => "[2phase] ",
1488 RetagKind::Raw => "[raw] ",
1489 RetagKind::Default => "",
1493 StorageLive(ref place) => write!(fmt, "StorageLive({:?})", place),
1494 StorageDead(ref place) => write!(fmt, "StorageDead({:?})", place),
1495 SetDiscriminant { ref place, variant_index } => {
1496 write!(fmt, "discriminant({:?}) = {:?}", place, variant_index)
1498 LlvmInlineAsm(ref asm) => {
1499 write!(fmt, "llvm_asm!({:?} : {:?} : {:?})", asm.asm, asm.outputs, asm.inputs)
1501 AscribeUserType(box (ref place, ref c_ty), ref variance) => {
1502 write!(fmt, "AscribeUserType({:?}, {:?}, {:?})", place, variance, c_ty)
1504 Coverage(box ref coverage) => write!(fmt, "{:?}", coverage),
1505 Nop => write!(fmt, "nop"),
1510 #[derive(Clone, Debug, PartialEq, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
1511 pub struct Coverage {
1512 pub kind: CoverageKind,
1513 pub code_region: CodeRegion,
1516 ///////////////////////////////////////////////////////////////////////////
1519 /// A path to a value; something that can be evaluated without
1520 /// changing or disturbing program state.
1521 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, TyEncodable, HashStable)]
1522 pub struct Place<'tcx> {
1525 /// projection out of a place (access a field, deref a pointer, etc)
1526 pub projection: &'tcx List<PlaceElem<'tcx>>,
1529 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
1530 #[derive(TyEncodable, TyDecodable, HashStable)]
1531 pub enum ProjectionElem<V, T> {
1536 /// These indices are generated by slice patterns. Easiest to explain
1540 /// [X, _, .._, _, _] => { offset: 0, min_length: 4, from_end: false },
1541 /// [_, X, .._, _, _] => { offset: 1, min_length: 4, from_end: false },
1542 /// [_, _, .._, X, _] => { offset: 2, min_length: 4, from_end: true },
1543 /// [_, _, .._, _, X] => { offset: 1, min_length: 4, from_end: true },
1546 /// index or -index (in Python terms), depending on from_end
1548 /// The thing being indexed must be at least this long. For arrays this
1549 /// is always the exact length.
1551 /// Counting backwards from end? This is always false when indexing an
1556 /// These indices are generated by slice patterns.
1558 /// If `from_end` is true `slice[from..slice.len() - to]`.
1559 /// Otherwise `array[from..to]`.
1563 /// Whether `to` counts from the start or end of the array/slice.
1564 /// For `PlaceElem`s this is `true` if and only if the base is a slice.
1565 /// For `ProjectionKind`, this can also be `true` for arrays.
1569 /// "Downcast" to a variant of an ADT. Currently, we only introduce
1570 /// this for ADTs with more than one variant. It may be better to
1571 /// just introduce it always, or always for enums.
1573 /// The included Symbol is the name of the variant, used for printing MIR.
1574 Downcast(Option<Symbol>, VariantIdx),
1577 impl<V, T> ProjectionElem<V, T> {
1578 /// Returns `true` if the target of this projection may refer to a different region of memory
1580 fn is_indirect(&self) -> bool {
1582 Self::Deref => true,
1586 | Self::ConstantIndex { .. }
1587 | Self::Subslice { .. }
1588 | Self::Downcast(_, _) => false,
1593 /// Alias for projections as they appear in places, where the base is a place
1594 /// and the index is a local.
1595 pub type PlaceElem<'tcx> = ProjectionElem<Local, Ty<'tcx>>;
1597 // At least on 64 bit systems, `PlaceElem` should not be larger than two pointers.
1598 #[cfg(target_arch = "x86_64")]
1599 static_assert_size!(PlaceElem<'_>, 16);
1601 /// Alias for projections as they appear in `UserTypeProjection`, where we
1602 /// need neither the `V` parameter for `Index` nor the `T` for `Field`.
1603 pub type ProjectionKind = ProjectionElem<(), ()>;
1605 rustc_index::newtype_index! {
1608 DEBUG_FORMAT = "field[{}]"
1612 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
1613 pub struct PlaceRef<'tcx> {
1615 pub projection: &'tcx [PlaceElem<'tcx>],
1618 impl<'tcx> Place<'tcx> {
1619 // FIXME change this to a const fn by also making List::empty a const fn.
1620 pub fn return_place() -> Place<'tcx> {
1621 Place { local: RETURN_PLACE, projection: List::empty() }
1624 /// Returns `true` if this `Place` contains a `Deref` projection.
1626 /// If `Place::is_indirect` returns false, the caller knows that the `Place` refers to the
1627 /// same region of memory as its base.
1628 pub fn is_indirect(&self) -> bool {
1629 self.projection.iter().any(|elem| elem.is_indirect())
1632 /// Finds the innermost `Local` from this `Place`, *if* it is either a local itself or
1633 /// a single deref of a local.
1635 // FIXME: can we safely swap the semantics of `fn base_local` below in here instead?
1636 pub fn local_or_deref_local(&self) -> Option<Local> {
1637 match self.as_ref() {
1638 PlaceRef { local, projection: [] }
1639 | PlaceRef { local, projection: [ProjectionElem::Deref] } => Some(local),
1644 /// If this place represents a local variable like `_X` with no
1645 /// projections, return `Some(_X)`.
1646 pub fn as_local(&self) -> Option<Local> {
1647 self.as_ref().as_local()
1650 pub fn as_ref(&self) -> PlaceRef<'tcx> {
1651 PlaceRef { local: self.local, projection: &self.projection }
1655 impl From<Local> for Place<'_> {
1656 fn from(local: Local) -> Self {
1657 Place { local, projection: List::empty() }
1661 impl<'tcx> PlaceRef<'tcx> {
1662 /// Finds the innermost `Local` from this `Place`, *if* it is either a local itself or
1663 /// a single deref of a local.
1665 // FIXME: can we safely swap the semantics of `fn base_local` below in here instead?
1666 pub fn local_or_deref_local(&self) -> Option<Local> {
1668 PlaceRef { local, projection: [] }
1669 | PlaceRef { local, projection: [ProjectionElem::Deref] } => Some(local),
1674 /// If this place represents a local variable like `_X` with no
1675 /// projections, return `Some(_X)`.
1676 pub fn as_local(&self) -> Option<Local> {
1678 PlaceRef { local, projection: [] } => Some(local),
1684 impl Debug for Place<'_> {
1685 fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
1686 for elem in self.projection.iter().rev() {
1688 ProjectionElem::Downcast(_, _) | ProjectionElem::Field(_, _) => {
1689 write!(fmt, "(").unwrap();
1691 ProjectionElem::Deref => {
1692 write!(fmt, "(*").unwrap();
1694 ProjectionElem::Index(_)
1695 | ProjectionElem::ConstantIndex { .. }
1696 | ProjectionElem::Subslice { .. } => {}
1700 write!(fmt, "{:?}", self.local)?;
1702 for elem in self.projection.iter() {
1704 ProjectionElem::Downcast(Some(name), _index) => {
1705 write!(fmt, " as {})", name)?;
1707 ProjectionElem::Downcast(None, index) => {
1708 write!(fmt, " as variant#{:?})", index)?;
1710 ProjectionElem::Deref => {
1713 ProjectionElem::Field(field, ty) => {
1714 write!(fmt, ".{:?}: {:?})", field.index(), ty)?;
1716 ProjectionElem::Index(ref index) => {
1717 write!(fmt, "[{:?}]", index)?;
1719 ProjectionElem::ConstantIndex { offset, min_length, from_end: false } => {
1720 write!(fmt, "[{:?} of {:?}]", offset, min_length)?;
1722 ProjectionElem::ConstantIndex { offset, min_length, from_end: true } => {
1723 write!(fmt, "[-{:?} of {:?}]", offset, min_length)?;
1725 ProjectionElem::Subslice { from, to, from_end: true } if to == 0 => {
1726 write!(fmt, "[{:?}:]", from)?;
1728 ProjectionElem::Subslice { from, to, from_end: true } if from == 0 => {
1729 write!(fmt, "[:-{:?}]", to)?;
1731 ProjectionElem::Subslice { from, to, from_end: true } => {
1732 write!(fmt, "[{:?}:-{:?}]", from, to)?;
1734 ProjectionElem::Subslice { from, to, from_end: false } => {
1735 write!(fmt, "[{:?}..{:?}]", from, to)?;
1744 ///////////////////////////////////////////////////////////////////////////
1747 rustc_index::newtype_index! {
1748 pub struct SourceScope {
1750 DEBUG_FORMAT = "scope[{}]",
1751 const OUTERMOST_SOURCE_SCOPE = 0,
1755 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
1756 pub struct SourceScopeData {
1758 pub parent_scope: Option<SourceScope>,
1760 /// Crate-local information for this source scope, that can't (and
1761 /// needn't) be tracked across crates.
1762 pub local_data: ClearCrossCrate<SourceScopeLocalData>,
1765 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
1766 pub struct SourceScopeLocalData {
1767 /// An `HirId` with lint levels equivalent to this scope's lint levels.
1768 pub lint_root: hir::HirId,
1769 /// The unsafe block that contains this node.
1773 ///////////////////////////////////////////////////////////////////////////
1776 /// These are values that can appear inside an rvalue. They are intentionally
1777 /// limited to prevent rvalues from being nested in one another.
1778 #[derive(Clone, PartialEq, TyEncodable, TyDecodable, HashStable)]
1779 pub enum Operand<'tcx> {
1780 /// Copy: The value must be available for use afterwards.
1782 /// This implies that the type of the place must be `Copy`; this is true
1783 /// by construction during build, but also checked by the MIR type checker.
1786 /// Move: The value (including old borrows of it) will not be used again.
1788 /// Safe for values of all types (modulo future developments towards `?Move`).
1789 /// Correct usage patterns are enforced by the borrow checker for safe code.
1790 /// `Copy` may be converted to `Move` to enable "last-use" optimizations.
1793 /// Synthesizes a constant value.
1794 Constant(Box<Constant<'tcx>>),
1797 impl<'tcx> Debug for Operand<'tcx> {
1798 fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
1799 use self::Operand::*;
1801 Constant(ref a) => write!(fmt, "{:?}", a),
1802 Copy(ref place) => write!(fmt, "{:?}", place),
1803 Move(ref place) => write!(fmt, "move {:?}", place),
1808 impl<'tcx> Operand<'tcx> {
1809 /// Convenience helper to make a constant that refers to the fn
1810 /// with given `DefId` and substs. Since this is used to synthesize
1811 /// MIR, assumes `user_ty` is None.
1812 pub fn function_handle(
1815 substs: SubstsRef<'tcx>,
1818 let ty = tcx.type_of(def_id).subst(tcx, substs);
1819 Operand::Constant(box Constant {
1822 literal: ty::Const::zero_sized(tcx, ty),
1826 /// Convenience helper to make a literal-like constant from a given scalar value.
1827 /// Since this is used to synthesize MIR, assumes `user_ty` is None.
1828 pub fn const_from_scalar(
1833 ) -> Operand<'tcx> {
1835 let param_env_and_ty = ty::ParamEnv::empty().and(ty);
1837 .layout_of(param_env_and_ty)
1838 .unwrap_or_else(|e| panic!("could not compute layout for {:?}: {:?}", ty, e))
1840 let scalar_size = abi::Size::from_bytes(match val {
1841 Scalar::Raw { size, .. } => size,
1842 _ => panic!("Invalid scalar type {:?}", val),
1844 scalar_size == type_size
1846 Operand::Constant(box Constant {
1849 literal: ty::Const::from_scalar(tcx, val, ty),
1853 /// Convenience helper to make a `Scalar` from the given `Operand`, assuming that `Operand`
1854 /// wraps a constant literal value. Panics if this is not the case.
1855 pub fn scalar_from_const(operand: &Operand<'tcx>) -> Scalar {
1857 Operand::Constant(constant) => match constant.literal.val.try_to_scalar() {
1858 Some(scalar) => scalar,
1859 _ => panic!("{:?}: Scalar value expected", constant.literal.val),
1861 _ => panic!("{:?}: Constant expected", operand),
1865 /// Convenience helper to make a literal-like constant from a given `&str` slice.
1866 /// Since this is used to synthesize MIR, assumes `user_ty` is None.
1867 pub fn const_from_str(tcx: TyCtxt<'tcx>, val: &str, span: Span) -> Operand<'tcx> {
1869 let allocation = Allocation::from_byte_aligned_bytes(val.as_bytes());
1870 let allocation = tcx.intern_const_alloc(allocation);
1871 let const_val = ConstValue::Slice { data: allocation, start: 0, end: val.len() };
1872 let ty = tcx.mk_imm_ref(tcx.lifetimes.re_erased, tcx.types.str_);
1873 Operand::Constant(box Constant {
1876 literal: ty::Const::from_value(tcx, const_val, ty),
1880 /// Convenience helper to make a `ConstValue` from the given `Operand`, assuming that `Operand`
1881 /// wraps a constant value (such as a `&str` slice). Panics if this is not the case.
1882 pub fn value_from_const(operand: &Operand<'tcx>) -> ConstValue<'tcx> {
1884 Operand::Constant(constant) => match constant.literal.val.try_to_value() {
1885 Some(const_value) => const_value,
1886 _ => panic!("{:?}: ConstValue expected", constant.literal.val),
1888 _ => panic!("{:?}: Constant expected", operand),
1892 pub fn to_copy(&self) -> Self {
1894 Operand::Copy(_) | Operand::Constant(_) => self.clone(),
1895 Operand::Move(place) => Operand::Copy(place),
1899 /// Returns the `Place` that is the target of this `Operand`, or `None` if this `Operand` is a
1901 pub fn place(&self) -> Option<Place<'tcx>> {
1903 Operand::Copy(place) | Operand::Move(place) => Some(*place),
1904 Operand::Constant(_) => None,
1909 ///////////////////////////////////////////////////////////////////////////
1912 #[derive(Clone, TyEncodable, TyDecodable, HashStable, PartialEq)]
1913 pub enum Rvalue<'tcx> {
1914 /// x (either a move or copy, depending on type of x)
1918 Repeat(Operand<'tcx>, &'tcx ty::Const<'tcx>),
1921 Ref(Region<'tcx>, BorrowKind, Place<'tcx>),
1923 /// Accessing a thread local static. This is inherently a runtime operation, even if llvm
1924 /// treats it as an access to a static. This `Rvalue` yields a reference to the thread local
1926 ThreadLocalRef(DefId),
1928 /// Create a raw pointer to the given place
1929 /// Can be generated by raw address of expressions (`&raw const x`),
1930 /// or when casting a reference to a raw pointer.
1931 AddressOf(Mutability, Place<'tcx>),
1933 /// length of a `[X]` or `[X;n]` value
1936 Cast(CastKind, Operand<'tcx>, Ty<'tcx>),
1938 BinaryOp(BinOp, Operand<'tcx>, Operand<'tcx>),
1939 CheckedBinaryOp(BinOp, Operand<'tcx>, Operand<'tcx>),
1941 NullaryOp(NullOp, Ty<'tcx>),
1942 UnaryOp(UnOp, Operand<'tcx>),
1944 /// Read the discriminant of an ADT.
1946 /// Undefined (i.e., no effort is made to make it defined, but there’s no reason why it cannot
1947 /// be defined to return, say, a 0) if ADT is not an enum.
1948 Discriminant(Place<'tcx>),
1950 /// Creates an aggregate value, like a tuple or struct. This is
1951 /// only needed because we want to distinguish `dest = Foo { x:
1952 /// ..., y: ... }` from `dest.x = ...; dest.y = ...;` in the case
1953 /// that `Foo` has a destructor. These rvalues can be optimized
1954 /// away after type-checking and before lowering.
1955 Aggregate(Box<AggregateKind<'tcx>>, Vec<Operand<'tcx>>),
1958 #[derive(Clone, Copy, Debug, PartialEq, Eq, TyEncodable, TyDecodable, HashStable)]
1961 Pointer(PointerCast),
1964 #[derive(Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, HashStable)]
1965 pub enum AggregateKind<'tcx> {
1966 /// The type is of the element
1970 /// The second field is the variant index. It's equal to 0 for struct
1971 /// and union expressions. The fourth field is
1972 /// active field number and is present only for union expressions
1973 /// -- e.g., for a union expression `SomeUnion { c: .. }`, the
1974 /// active field index would identity the field `c`
1975 Adt(&'tcx AdtDef, VariantIdx, SubstsRef<'tcx>, Option<UserTypeAnnotationIndex>, Option<usize>),
1977 Closure(DefId, SubstsRef<'tcx>),
1978 Generator(DefId, SubstsRef<'tcx>, hir::Movability),
1981 #[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, HashStable)]
1983 /// The `+` operator (addition)
1985 /// The `-` operator (subtraction)
1987 /// The `*` operator (multiplication)
1989 /// The `/` operator (division)
1991 /// The `%` operator (modulus)
1993 /// The `^` operator (bitwise xor)
1995 /// The `&` operator (bitwise and)
1997 /// The `|` operator (bitwise or)
1999 /// The `<<` operator (shift left)
2001 /// The `>>` operator (shift right)
2003 /// The `==` operator (equality)
2005 /// The `<` operator (less than)
2007 /// The `<=` operator (less than or equal to)
2009 /// The `!=` operator (not equal to)
2011 /// The `>=` operator (greater than or equal to)
2013 /// The `>` operator (greater than)
2015 /// The `ptr.offset` operator
2020 pub fn is_checkable(self) -> bool {
2023 Add | Sub | Mul | Shl | Shr => true,
2029 #[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, HashStable)]
2031 /// Returns the size of a value of that type
2033 /// Creates a new uninitialized box for a value of that type
2037 #[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, HashStable)]
2039 /// The `!` operator for logical inversion
2041 /// The `-` operator for negation
2045 impl<'tcx> Debug for Rvalue<'tcx> {
2046 fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
2047 use self::Rvalue::*;
2050 Use(ref place) => write!(fmt, "{:?}", place),
2051 Repeat(ref a, ref b) => {
2052 write!(fmt, "[{:?}; ", a)?;
2053 pretty_print_const(b, fmt, false)?;
2056 Len(ref a) => write!(fmt, "Len({:?})", a),
2057 Cast(ref kind, ref place, ref ty) => {
2058 write!(fmt, "{:?} as {:?} ({:?})", place, ty, kind)
2060 BinaryOp(ref op, ref a, ref b) => write!(fmt, "{:?}({:?}, {:?})", op, a, b),
2061 CheckedBinaryOp(ref op, ref a, ref b) => {
2062 write!(fmt, "Checked{:?}({:?}, {:?})", op, a, b)
2064 UnaryOp(ref op, ref a) => write!(fmt, "{:?}({:?})", op, a),
2065 Discriminant(ref place) => write!(fmt, "discriminant({:?})", place),
2066 NullaryOp(ref op, ref t) => write!(fmt, "{:?}({:?})", op, t),
2067 ThreadLocalRef(did) => ty::tls::with(|tcx| {
2068 let muta = tcx.static_mutability(did).unwrap().prefix_str();
2069 write!(fmt, "&/*tls*/ {}{}", muta, tcx.def_path_str(did))
2071 Ref(region, borrow_kind, ref place) => {
2072 let kind_str = match borrow_kind {
2073 BorrowKind::Shared => "",
2074 BorrowKind::Shallow => "shallow ",
2075 BorrowKind::Mut { .. } | BorrowKind::Unique => "mut ",
2078 // When printing regions, add trailing space if necessary.
2079 let print_region = ty::tls::with(|tcx| {
2080 tcx.sess.verbose() || tcx.sess.opts.debugging_opts.identify_regions
2082 let region = if print_region {
2083 let mut region = region.to_string();
2084 if !region.is_empty() {
2089 // Do not even print 'static
2092 write!(fmt, "&{}{}{:?}", region, kind_str, place)
2095 AddressOf(mutability, ref place) => {
2096 let kind_str = match mutability {
2097 Mutability::Mut => "mut",
2098 Mutability::Not => "const",
2101 write!(fmt, "&raw {} {:?}", kind_str, place)
2104 Aggregate(ref kind, ref places) => {
2105 let fmt_tuple = |fmt: &mut Formatter<'_>, name: &str| {
2106 let mut tuple_fmt = fmt.debug_tuple(name);
2107 for place in places {
2108 tuple_fmt.field(place);
2114 AggregateKind::Array(_) => write!(fmt, "{:?}", places),
2116 AggregateKind::Tuple => {
2117 if places.is_empty() {
2124 AggregateKind::Adt(adt_def, variant, substs, _user_ty, _) => {
2125 let variant_def = &adt_def.variants[variant];
2127 let name = ty::tls::with(|tcx| {
2128 let mut name = String::new();
2129 let substs = tcx.lift(&substs).expect("could not lift for printing");
2130 FmtPrinter::new(tcx, &mut name, Namespace::ValueNS)
2131 .print_def_path(variant_def.def_id, substs)?;
2135 match variant_def.ctor_kind {
2136 CtorKind::Const => fmt.write_str(&name),
2137 CtorKind::Fn => fmt_tuple(fmt, &name),
2138 CtorKind::Fictive => {
2139 let mut struct_fmt = fmt.debug_struct(&name);
2140 for (field, place) in variant_def.fields.iter().zip(places) {
2141 struct_fmt.field(&field.ident.as_str(), place);
2148 AggregateKind::Closure(def_id, substs) => ty::tls::with(|tcx| {
2149 if let Some(def_id) = def_id.as_local() {
2150 let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
2151 let name = if tcx.sess.opts.debugging_opts.span_free_formats {
2152 let substs = tcx.lift(&substs).unwrap();
2155 tcx.def_path_str_with_substs(def_id.to_def_id(), substs),
2158 let span = tcx.hir().span(hir_id);
2159 format!("[closure@{}]", tcx.sess.source_map().span_to_string(span))
2161 let mut struct_fmt = fmt.debug_struct(&name);
2163 if let Some(upvars) = tcx.upvars_mentioned(def_id) {
2164 for (&var_id, place) in upvars.keys().zip(places) {
2165 let var_name = tcx.hir().name(var_id);
2166 struct_fmt.field(&var_name.as_str(), place);
2172 write!(fmt, "[closure]")
2176 AggregateKind::Generator(def_id, _, _) => ty::tls::with(|tcx| {
2177 if let Some(def_id) = def_id.as_local() {
2178 let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
2179 let name = format!("[generator@{:?}]", tcx.hir().span(hir_id));
2180 let mut struct_fmt = fmt.debug_struct(&name);
2182 if let Some(upvars) = tcx.upvars_mentioned(def_id) {
2183 for (&var_id, place) in upvars.keys().zip(places) {
2184 let var_name = tcx.hir().name(var_id);
2185 struct_fmt.field(&var_name.as_str(), place);
2191 write!(fmt, "[generator]")
2200 ///////////////////////////////////////////////////////////////////////////
2203 /// Two constants are equal if they are the same constant. Note that
2204 /// this does not necessarily mean that they are "==" in Rust -- in
2205 /// particular one must be wary of `NaN`!
2207 #[derive(Clone, Copy, PartialEq, TyEncodable, TyDecodable, HashStable)]
2208 pub struct Constant<'tcx> {
2211 /// Optional user-given type: for something like
2212 /// `collect::<Vec<_>>`, this would be present and would
2213 /// indicate that `Vec<_>` was explicitly specified.
2215 /// Needed for NLL to impose user-given type constraints.
2216 pub user_ty: Option<UserTypeAnnotationIndex>,
2218 pub literal: &'tcx ty::Const<'tcx>,
2221 impl Constant<'tcx> {
2222 pub fn check_static_ptr(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
2223 match self.literal.val.try_to_scalar() {
2224 Some(Scalar::Ptr(ptr)) => match tcx.global_alloc(ptr.alloc_id) {
2225 GlobalAlloc::Static(def_id) => {
2226 assert!(!tcx.is_thread_local_static(def_id));
2236 /// A collection of projections into user types.
2238 /// They are projections because a binding can occur a part of a
2239 /// parent pattern that has been ascribed a type.
2241 /// Its a collection because there can be multiple type ascriptions on
2242 /// the path from the root of the pattern down to the binding itself.
2247 /// struct S<'a>((i32, &'a str), String);
2248 /// let S((_, w): (i32, &'static str), _): S = ...;
2249 /// // ------ ^^^^^^^^^^^^^^^^^^^ (1)
2250 /// // --------------------------------- ^ (2)
2253 /// The highlights labelled `(1)` show the subpattern `(_, w)` being
2254 /// ascribed the type `(i32, &'static str)`.
2256 /// The highlights labelled `(2)` show the whole pattern being
2257 /// ascribed the type `S`.
2259 /// In this example, when we descend to `w`, we will have built up the
2260 /// following two projected types:
2262 /// * base: `S`, projection: `(base.0).1`
2263 /// * base: `(i32, &'static str)`, projection: `base.1`
2265 /// The first will lead to the constraint `w: &'1 str` (for some
2266 /// inferred region `'1`). The second will lead to the constraint `w:
2268 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
2269 pub struct UserTypeProjections {
2270 pub contents: Vec<(UserTypeProjection, Span)>,
2273 impl<'tcx> UserTypeProjections {
2274 pub fn none() -> Self {
2275 UserTypeProjections { contents: vec![] }
2278 pub fn is_empty(&self) -> bool {
2279 self.contents.is_empty()
2282 pub fn from_projections(projs: impl Iterator<Item = (UserTypeProjection, Span)>) -> Self {
2283 UserTypeProjections { contents: projs.collect() }
2286 pub fn projections_and_spans(
2288 ) -> impl Iterator<Item = &(UserTypeProjection, Span)> + ExactSizeIterator {
2289 self.contents.iter()
2292 pub fn projections(&self) -> impl Iterator<Item = &UserTypeProjection> + ExactSizeIterator {
2293 self.contents.iter().map(|&(ref user_type, _span)| user_type)
2296 pub fn push_projection(mut self, user_ty: &UserTypeProjection, span: Span) -> Self {
2297 self.contents.push((user_ty.clone(), span));
2303 mut f: impl FnMut(UserTypeProjection) -> UserTypeProjection,
2305 self.contents = self.contents.drain(..).map(|(proj, span)| (f(proj), span)).collect();
2309 pub fn index(self) -> Self {
2310 self.map_projections(|pat_ty_proj| pat_ty_proj.index())
2313 pub fn subslice(self, from: u32, to: u32) -> Self {
2314 self.map_projections(|pat_ty_proj| pat_ty_proj.subslice(from, to))
2317 pub fn deref(self) -> Self {
2318 self.map_projections(|pat_ty_proj| pat_ty_proj.deref())
2321 pub fn leaf(self, field: Field) -> Self {
2322 self.map_projections(|pat_ty_proj| pat_ty_proj.leaf(field))
2325 pub fn variant(self, adt_def: &'tcx AdtDef, variant_index: VariantIdx, field: Field) -> Self {
2326 self.map_projections(|pat_ty_proj| pat_ty_proj.variant(adt_def, variant_index, field))
2330 /// Encodes the effect of a user-supplied type annotation on the
2331 /// subcomponents of a pattern. The effect is determined by applying the
2332 /// given list of proejctions to some underlying base type. Often,
2333 /// the projection element list `projs` is empty, in which case this
2334 /// directly encodes a type in `base`. But in the case of complex patterns with
2335 /// subpatterns and bindings, we want to apply only a *part* of the type to a variable,
2336 /// in which case the `projs` vector is used.
2340 /// * `let x: T = ...` -- here, the `projs` vector is empty.
2342 /// * `let (x, _): T = ...` -- here, the `projs` vector would contain
2343 /// `field[0]` (aka `.0`), indicating that the type of `s` is
2344 /// determined by finding the type of the `.0` field from `T`.
2345 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, PartialEq)]
2346 pub struct UserTypeProjection {
2347 pub base: UserTypeAnnotationIndex,
2348 pub projs: Vec<ProjectionKind>,
2351 impl Copy for ProjectionKind {}
2353 impl UserTypeProjection {
2354 pub(crate) fn index(mut self) -> Self {
2355 self.projs.push(ProjectionElem::Index(()));
2359 pub(crate) fn subslice(mut self, from: u32, to: u32) -> Self {
2360 self.projs.push(ProjectionElem::Subslice { from, to, from_end: true });
2364 pub(crate) fn deref(mut self) -> Self {
2365 self.projs.push(ProjectionElem::Deref);
2369 pub(crate) fn leaf(mut self, field: Field) -> Self {
2370 self.projs.push(ProjectionElem::Field(field, ()));
2374 pub(crate) fn variant(
2377 variant_index: VariantIdx,
2380 self.projs.push(ProjectionElem::Downcast(
2381 Some(adt_def.variants[variant_index].ident.name),
2384 self.projs.push(ProjectionElem::Field(field, ()));
2389 CloneTypeFoldableAndLiftImpls! { ProjectionKind, }
2391 impl<'tcx> TypeFoldable<'tcx> for UserTypeProjection {
2392 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
2393 use crate::mir::ProjectionElem::*;
2395 let base = self.base.fold_with(folder);
2396 let projs: Vec<_> = self
2399 .map(|&elem| match elem {
2401 Field(f, ()) => Field(f, ()),
2402 Index(()) => Index(()),
2403 Downcast(symbol, variantidx) => Downcast(symbol, variantidx),
2404 ConstantIndex { offset, min_length, from_end } => {
2405 ConstantIndex { offset, min_length, from_end }
2407 Subslice { from, to, from_end } => Subslice { from, to, from_end },
2411 UserTypeProjection { base, projs }
2414 fn super_visit_with<Vs: TypeVisitor<'tcx>>(&self, visitor: &mut Vs) -> bool {
2415 self.base.visit_with(visitor)
2416 // Note: there's nothing in `self.proj` to visit.
2420 rustc_index::newtype_index! {
2421 pub struct Promoted {
2423 DEBUG_FORMAT = "promoted[{}]"
2427 impl<'tcx> Debug for Constant<'tcx> {
2428 fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
2429 write!(fmt, "{}", self)
2433 impl<'tcx> Display for Constant<'tcx> {
2434 fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
2435 write!(fmt, "const ")?;
2436 pretty_print_const(self.literal, fmt, true)
2440 fn pretty_print_const(
2441 c: &ty::Const<'tcx>,
2442 fmt: &mut Formatter<'_>,
2445 use crate::ty::print::PrettyPrinter;
2446 ty::tls::with(|tcx| {
2447 let literal = tcx.lift(&c).unwrap();
2448 let mut cx = FmtPrinter::new(tcx, fmt, Namespace::ValueNS);
2449 cx.print_alloc_ids = true;
2450 cx.pretty_print_const(literal, print_types)?;
2455 impl<'tcx> graph::DirectedGraph for Body<'tcx> {
2456 type Node = BasicBlock;
2459 impl<'tcx> graph::WithNumNodes for Body<'tcx> {
2461 fn num_nodes(&self) -> usize {
2462 self.basic_blocks.len()
2466 impl<'tcx> graph::WithStartNode for Body<'tcx> {
2468 fn start_node(&self) -> Self::Node {
2473 impl<'tcx> graph::WithSuccessors for Body<'tcx> {
2475 fn successors(&self, node: Self::Node) -> <Self as GraphSuccessors<'_>>::Iter {
2476 self.basic_blocks[node].terminator().successors().cloned()
2480 impl<'a, 'b> graph::GraphSuccessors<'b> for Body<'a> {
2481 type Item = BasicBlock;
2482 type Iter = iter::Cloned<Successors<'b>>;
2485 impl graph::GraphPredecessors<'graph> for Body<'tcx> {
2486 type Item = BasicBlock;
2487 type Iter = smallvec::IntoIter<[BasicBlock; 4]>;
2490 impl graph::WithPredecessors for Body<'tcx> {
2492 fn predecessors(&self, node: Self::Node) -> <Self as graph::GraphPredecessors<'_>>::Iter {
2493 self.predecessors()[node].clone().into_iter()
2497 /// `Location` represents the position of the start of the statement; or, if
2498 /// `statement_index` equals the number of statements, then the start of the
2500 #[derive(Copy, Clone, PartialEq, Eq, Hash, Ord, PartialOrd, HashStable)]
2501 pub struct Location {
2502 /// The block that the location is within.
2503 pub block: BasicBlock,
2505 pub statement_index: usize,
2508 impl fmt::Debug for Location {
2509 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2510 write!(fmt, "{:?}[{}]", self.block, self.statement_index)
2515 pub const START: Location = Location { block: START_BLOCK, statement_index: 0 };
2517 /// Returns the location immediately after this one within the enclosing block.
2519 /// Note that if this location represents a terminator, then the
2520 /// resulting location would be out of bounds and invalid.
2521 pub fn successor_within_block(&self) -> Location {
2522 Location { block: self.block, statement_index: self.statement_index + 1 }
2525 /// Returns `true` if `other` is earlier in the control flow graph than `self`.
2526 pub fn is_predecessor_of<'tcx>(&self, other: Location, body: &Body<'tcx>) -> bool {
2527 // If we are in the same block as the other location and are an earlier statement
2528 // then we are a predecessor of `other`.
2529 if self.block == other.block && self.statement_index < other.statement_index {
2533 let predecessors = body.predecessors();
2535 // If we're in another block, then we want to check that block is a predecessor of `other`.
2536 let mut queue: Vec<BasicBlock> = predecessors[other.block].to_vec();
2537 let mut visited = FxHashSet::default();
2539 while let Some(block) = queue.pop() {
2540 // If we haven't visited this block before, then make sure we visit it's predecessors.
2541 if visited.insert(block) {
2542 queue.extend(predecessors[block].iter().cloned());
2547 // If we found the block that `self` is in, then we are a predecessor of `other` (since
2548 // we found that block by looking at the predecessors of `other`).
2549 if self.block == block {
2557 pub fn dominates(&self, other: Location, dominators: &Dominators<BasicBlock>) -> bool {
2558 if self.block == other.block {
2559 self.statement_index <= other.statement_index
2561 dominators.is_dominated_by(other.block, self.block)