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};
13 use crate::ty::{self, List, Ty, TyCtxt};
14 use crate::ty::{AdtDef, InstanceDef, Region, ScalarInt, UserTypeAnnotationIndex};
15 use rustc_hir::def::{CtorKind, Namespace};
16 use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX};
17 use rustc_hir::{self, GeneratorKind};
18 use rustc_hir::{self as hir, HirId};
19 use rustc_target::abi::{Size, VariantIdx};
21 use polonius_engine::Atom;
22 pub use rustc_ast::Mutability;
23 use rustc_data_structures::fx::FxHashSet;
24 use rustc_data_structures::graph::dominators::{dominators, Dominators};
25 use rustc_data_structures::graph::{self, GraphSuccessors};
26 use rustc_index::bit_set::BitMatrix;
27 use rustc_index::vec::{Idx, IndexVec};
28 use rustc_serialize::{Decodable, Encodable};
29 use rustc_span::symbol::Symbol;
30 use rustc_span::{Span, DUMMY_SP};
31 use rustc_target::asm::InlineAsmRegOrRegClass;
33 use std::convert::TryInto;
34 use std::fmt::{self, Debug, Display, Formatter, Write};
35 use std::ops::{ControlFlow, Index, IndexMut};
37 use std::{iter, mem, option};
39 use self::graph_cyclic_cache::GraphIsCyclicCache;
40 use self::predecessors::{PredecessorCache, Predecessors};
41 pub use self::query::*;
43 pub mod abstract_const;
45 mod graph_cyclic_cache;
52 pub use terminator::*;
58 pub type LocalDecls<'tcx> = IndexVec<Local, LocalDecl<'tcx>>;
60 pub trait HasLocalDecls<'tcx> {
61 fn local_decls(&self) -> &LocalDecls<'tcx>;
64 impl<'tcx> HasLocalDecls<'tcx> for LocalDecls<'tcx> {
66 fn local_decls(&self) -> &LocalDecls<'tcx> {
71 impl<'tcx> HasLocalDecls<'tcx> for Body<'tcx> {
73 fn local_decls(&self) -> &LocalDecls<'tcx> {
78 /// The various "big phases" that MIR goes through.
80 /// These phases all describe dialects of MIR. Since all MIR uses the same datastructures, the
81 /// dialects forbid certain variants or values in certain phases.
83 /// Note: Each phase's validation checks all invariants of the *previous* phases' dialects. A phase
84 /// that changes the dialect documents what invariants must be upheld *after* that phase finishes.
86 /// Warning: ordering of variants is significant.
87 #[derive(Copy, Clone, TyEncodable, TyDecodable, Debug, PartialEq, Eq, PartialOrd, Ord)]
91 // FIXME(oli-obk): it's unclear whether we still need this phase (and its corresponding query).
92 // We used to have this for pre-miri MIR based const eval.
94 /// This phase checks the MIR for promotable elements and takes them out of the main MIR body
95 /// by creating a new MIR body per promoted element. After this phase (and thus the termination
96 /// of the `mir_promoted` query), these promoted elements are available in the `promoted_mir`
100 /// * the only `AggregateKind`s allowed are `Array` and `Generator`,
101 /// * `DropAndReplace` is gone for good
102 /// * `Drop` now uses explicit drop flags visible in the MIR and reaching a `Drop` terminator
103 /// means that the auto-generated drop glue will be invoked.
105 /// After this phase, generators are explicit state machines (no more `Yield`).
106 /// `AggregateKind::Generator` is gone for good.
107 GeneratorLowering = 4,
112 /// Gets the index of the current MirPhase within the set of all `MirPhase`s.
113 pub fn phase_index(&self) -> usize {
118 /// Where a specific `mir::Body` comes from.
119 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
120 #[derive(HashStable, TyEncodable, TyDecodable, TypeFoldable)]
121 pub struct MirSource<'tcx> {
122 pub instance: InstanceDef<'tcx>,
124 /// If `Some`, this is a promoted rvalue within the parent function.
125 pub promoted: Option<Promoted>,
128 impl<'tcx> MirSource<'tcx> {
129 pub fn item(def_id: DefId) -> Self {
131 instance: InstanceDef::Item(ty::WithOptConstParam::unknown(def_id)),
136 pub fn from_instance(instance: InstanceDef<'tcx>) -> Self {
137 MirSource { instance, promoted: None }
140 pub fn with_opt_param(self) -> ty::WithOptConstParam<DefId> {
141 self.instance.with_opt_param()
145 pub fn def_id(&self) -> DefId {
146 self.instance.def_id()
150 #[derive(Clone, TyEncodable, TyDecodable, Debug, HashStable, TypeFoldable)]
151 pub struct GeneratorInfo<'tcx> {
152 /// The yield type of the function, if it is a generator.
153 pub yield_ty: Option<Ty<'tcx>>,
155 /// Generator drop glue.
156 pub generator_drop: Option<Body<'tcx>>,
158 /// The layout of a generator. Produced by the state transformation.
159 pub generator_layout: Option<GeneratorLayout<'tcx>>,
161 /// If this is a generator then record the type of source expression that caused this generator
163 pub generator_kind: GeneratorKind,
166 /// The lowered representation of a single function.
167 #[derive(Clone, TyEncodable, TyDecodable, Debug, HashStable, TypeFoldable)]
168 pub struct Body<'tcx> {
169 /// A list of basic blocks. References to basic block use a newtyped index type [`BasicBlock`]
170 /// that indexes into this vector.
171 basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
173 /// Records how far through the "desugaring and optimization" process this particular
174 /// MIR has traversed. This is particularly useful when inlining, since in that context
175 /// we instantiate the promoted constants and add them to our promoted vector -- but those
176 /// promoted items have already been optimized, whereas ours have not. This field allows
177 /// us to see the difference and forego optimization on the inlined promoted items.
180 pub source: MirSource<'tcx>,
182 /// A list of source scopes; these are referenced by statements
183 /// and used for debuginfo. Indexed by a `SourceScope`.
184 pub source_scopes: IndexVec<SourceScope, SourceScopeData<'tcx>>,
186 pub generator: Option<Box<GeneratorInfo<'tcx>>>,
188 /// Declarations of locals.
190 /// The first local is the return value pointer, followed by `arg_count`
191 /// locals for the function arguments, followed by any user-declared
192 /// variables and temporaries.
193 pub local_decls: LocalDecls<'tcx>,
195 /// User type annotations.
196 pub user_type_annotations: ty::CanonicalUserTypeAnnotations<'tcx>,
198 /// The number of arguments this function takes.
200 /// Starting at local 1, `arg_count` locals will be provided by the caller
201 /// and can be assumed to be initialized.
203 /// If this MIR was built for a constant, this will be 0.
204 pub arg_count: usize,
206 /// Mark an argument local (which must be a tuple) as getting passed as
207 /// its individual components at the LLVM level.
209 /// This is used for the "rust-call" ABI.
210 pub spread_arg: Option<Local>,
212 /// Debug information pertaining to user variables, including captures.
213 pub var_debug_info: Vec<VarDebugInfo<'tcx>>,
215 /// A span representing this MIR, for error reporting.
218 /// Constants that are required to evaluate successfully for this MIR to be well-formed.
219 /// We hold in this field all the constants we are not able to evaluate yet.
220 pub required_consts: Vec<Constant<'tcx>>,
222 /// Does this body use generic parameters. This is used for the `ConstEvaluatable` check.
224 /// Note that this does not actually mean that this body is not computable right now.
225 /// The repeat count in the following example is polymorphic, but can still be evaluated
226 /// without knowing anything about the type parameter `T`.
230 /// let _ = [0; std::mem::size_of::<*mut T>()];
234 /// **WARNING**: Do not change this flags after the MIR was originally created, even if an optimization
235 /// removed the last mention of all generic params. We do not want to rely on optimizations and
236 /// potentially allow things like `[u8; std::mem::size_of::<T>() * 0]` due to this.
237 pub is_polymorphic: bool,
239 predecessor_cache: PredecessorCache,
240 is_cyclic: GraphIsCyclicCache,
243 impl<'tcx> Body<'tcx> {
246 source: MirSource<'tcx>,
247 basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
248 source_scopes: IndexVec<SourceScope, SourceScopeData<'tcx>>,
249 local_decls: LocalDecls<'tcx>,
250 user_type_annotations: ty::CanonicalUserTypeAnnotations<'tcx>,
252 var_debug_info: Vec<VarDebugInfo<'tcx>>,
254 generator_kind: Option<GeneratorKind>,
256 // We need `arg_count` locals, and one for the return place.
258 local_decls.len() > arg_count,
259 "expected at least {} locals, got {}",
264 let mut body = Body {
265 phase: MirPhase::Build,
269 generator: generator_kind.map(|generator_kind| {
270 Box::new(GeneratorInfo {
272 generator_drop: None,
273 generator_layout: None,
278 user_type_annotations,
283 required_consts: Vec::new(),
284 is_polymorphic: false,
285 predecessor_cache: PredecessorCache::new(),
286 is_cyclic: GraphIsCyclicCache::new(),
288 body.is_polymorphic = body.definitely_has_param_types_or_consts(tcx);
292 /// Returns a partially initialized MIR body containing only a list of basic blocks.
294 /// The returned MIR contains no `LocalDecl`s (even for the return place) or source scopes. It
295 /// is only useful for testing but cannot be `#[cfg(test)]` because it is used in a different
297 pub fn new_cfg_only(basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>) -> Self {
299 phase: MirPhase::Build,
300 source: MirSource::item(DefId::local(CRATE_DEF_INDEX)),
302 source_scopes: IndexVec::new(),
304 local_decls: IndexVec::new(),
305 user_type_annotations: IndexVec::new(),
309 required_consts: Vec::new(),
310 var_debug_info: Vec::new(),
311 is_polymorphic: false,
312 predecessor_cache: PredecessorCache::new(),
313 is_cyclic: GraphIsCyclicCache::new(),
318 pub fn basic_blocks(&self) -> &IndexVec<BasicBlock, BasicBlockData<'tcx>> {
323 pub fn basic_blocks_mut(&mut self) -> &mut IndexVec<BasicBlock, BasicBlockData<'tcx>> {
324 // Because the user could mutate basic block terminators via this reference, we need to
325 // invalidate the caches.
327 // FIXME: Use a finer-grained API for this, so only transformations that alter terminators
328 // invalidate the caches.
329 self.predecessor_cache.invalidate();
330 self.is_cyclic.invalidate();
331 &mut self.basic_blocks
335 pub fn basic_blocks_and_local_decls_mut(
337 ) -> (&mut IndexVec<BasicBlock, BasicBlockData<'tcx>>, &mut LocalDecls<'tcx>) {
338 self.predecessor_cache.invalidate();
339 self.is_cyclic.invalidate();
340 (&mut self.basic_blocks, &mut self.local_decls)
344 pub fn basic_blocks_local_decls_mut_and_var_debug_info(
347 &mut IndexVec<BasicBlock, BasicBlockData<'tcx>>,
348 &mut LocalDecls<'tcx>,
349 &mut Vec<VarDebugInfo<'tcx>>,
351 self.predecessor_cache.invalidate();
352 self.is_cyclic.invalidate();
353 (&mut self.basic_blocks, &mut self.local_decls, &mut self.var_debug_info)
356 /// Returns `true` if a cycle exists in the control-flow graph that is reachable from the
358 pub fn is_cfg_cyclic(&self) -> bool {
359 self.is_cyclic.is_cyclic(self)
363 pub fn local_kind(&self, local: Local) -> LocalKind {
364 let index = local.as_usize();
367 self.local_decls[local].mutability == Mutability::Mut,
368 "return place should be mutable"
371 LocalKind::ReturnPointer
372 } else if index < self.arg_count + 1 {
374 } else if self.local_decls[local].is_user_variable() {
381 /// Returns an iterator over all user-declared mutable locals.
383 pub fn mut_vars_iter<'a>(&'a self) -> impl Iterator<Item = Local> + 'a {
384 (self.arg_count + 1..self.local_decls.len()).filter_map(move |index| {
385 let local = Local::new(index);
386 let decl = &self.local_decls[local];
387 if decl.is_user_variable() && decl.mutability == Mutability::Mut {
395 /// Returns an iterator over all user-declared mutable arguments and locals.
397 pub fn mut_vars_and_args_iter<'a>(&'a self) -> impl Iterator<Item = Local> + 'a {
398 (1..self.local_decls.len()).filter_map(move |index| {
399 let local = Local::new(index);
400 let decl = &self.local_decls[local];
401 if (decl.is_user_variable() || index < self.arg_count + 1)
402 && decl.mutability == Mutability::Mut
411 /// Returns an iterator over all function arguments.
413 pub fn args_iter(&self) -> impl Iterator<Item = Local> + ExactSizeIterator {
414 (1..self.arg_count + 1).map(Local::new)
417 /// Returns an iterator over all user-defined variables and compiler-generated temporaries (all
418 /// locals that are neither arguments nor the return place).
420 pub fn vars_and_temps_iter(
422 ) -> impl DoubleEndedIterator<Item = Local> + ExactSizeIterator {
423 (self.arg_count + 1..self.local_decls.len()).map(Local::new)
427 pub fn drain_vars_and_temps<'a>(&'a mut self) -> impl Iterator<Item = LocalDecl<'tcx>> + 'a {
428 self.local_decls.drain(self.arg_count + 1..)
431 /// Changes a statement to a nop. This is both faster than deleting instructions and avoids
432 /// invalidating statement indices in `Location`s.
433 pub fn make_statement_nop(&mut self, location: Location) {
434 let block = &mut self.basic_blocks[location.block];
435 debug_assert!(location.statement_index < block.statements.len());
436 block.statements[location.statement_index].make_nop()
439 /// Returns the source info associated with `location`.
440 pub fn source_info(&self, location: Location) -> &SourceInfo {
441 let block = &self[location.block];
442 let stmts = &block.statements;
443 let idx = location.statement_index;
444 if idx < stmts.len() {
445 &stmts[idx].source_info
447 assert_eq!(idx, stmts.len());
448 &block.terminator().source_info
452 /// Returns the return type; it always return first element from `local_decls` array.
454 pub fn return_ty(&self) -> Ty<'tcx> {
455 self.local_decls[RETURN_PLACE].ty
458 /// Gets the location of the terminator for the given block.
460 pub fn terminator_loc(&self, bb: BasicBlock) -> Location {
461 Location { block: bb, statement_index: self[bb].statements.len() }
465 pub fn predecessors(&self) -> &Predecessors {
466 self.predecessor_cache.compute(&self.basic_blocks)
470 pub fn dominators(&self) -> Dominators<BasicBlock> {
475 pub fn yield_ty(&self) -> Option<Ty<'tcx>> {
476 self.generator.as_ref().and_then(|generator| generator.yield_ty)
480 pub fn generator_layout(&self) -> Option<&GeneratorLayout<'tcx>> {
481 self.generator.as_ref().and_then(|generator| generator.generator_layout.as_ref())
485 pub fn generator_drop(&self) -> Option<&Body<'tcx>> {
486 self.generator.as_ref().and_then(|generator| generator.generator_drop.as_ref())
490 pub fn generator_kind(&self) -> Option<GeneratorKind> {
491 self.generator.as_ref().map(|generator| generator.generator_kind)
495 #[derive(Copy, Clone, PartialEq, Eq, Debug, TyEncodable, TyDecodable, HashStable)]
498 /// Unsafe because of compiler-generated unsafe code, like `await` desugaring
500 /// Unsafe because of an unsafe fn
502 /// Unsafe because of an `unsafe` block
503 ExplicitUnsafe(hir::HirId),
506 impl<'tcx> Index<BasicBlock> for Body<'tcx> {
507 type Output = BasicBlockData<'tcx>;
510 fn index(&self, index: BasicBlock) -> &BasicBlockData<'tcx> {
511 &self.basic_blocks()[index]
515 impl<'tcx> IndexMut<BasicBlock> for Body<'tcx> {
517 fn index_mut(&mut self, index: BasicBlock) -> &mut BasicBlockData<'tcx> {
518 &mut self.basic_blocks_mut()[index]
522 #[derive(Copy, Clone, Debug, HashStable, TypeFoldable)]
523 pub enum ClearCrossCrate<T> {
528 impl<T> ClearCrossCrate<T> {
529 pub fn as_ref(&self) -> ClearCrossCrate<&T> {
531 ClearCrossCrate::Clear => ClearCrossCrate::Clear,
532 ClearCrossCrate::Set(v) => ClearCrossCrate::Set(v),
536 pub fn assert_crate_local(self) -> T {
538 ClearCrossCrate::Clear => bug!("unwrapping cross-crate data"),
539 ClearCrossCrate::Set(v) => v,
544 const TAG_CLEAR_CROSS_CRATE_CLEAR: u8 = 0;
545 const TAG_CLEAR_CROSS_CRATE_SET: u8 = 1;
547 impl<'tcx, E: TyEncoder<'tcx>, T: Encodable<E>> Encodable<E> for ClearCrossCrate<T> {
549 fn encode(&self, e: &mut E) -> Result<(), E::Error> {
550 if E::CLEAR_CROSS_CRATE {
555 ClearCrossCrate::Clear => TAG_CLEAR_CROSS_CRATE_CLEAR.encode(e),
556 ClearCrossCrate::Set(ref val) => {
557 TAG_CLEAR_CROSS_CRATE_SET.encode(e)?;
563 impl<'tcx, D: TyDecoder<'tcx>, T: Decodable<D>> Decodable<D> for ClearCrossCrate<T> {
565 fn decode(d: &mut D) -> Result<ClearCrossCrate<T>, D::Error> {
566 if D::CLEAR_CROSS_CRATE {
567 return Ok(ClearCrossCrate::Clear);
570 let discr = u8::decode(d)?;
573 TAG_CLEAR_CROSS_CRATE_CLEAR => Ok(ClearCrossCrate::Clear),
574 TAG_CLEAR_CROSS_CRATE_SET => {
575 let val = T::decode(d)?;
576 Ok(ClearCrossCrate::Set(val))
578 tag => Err(d.error(&format!("Invalid tag for ClearCrossCrate: {:?}", tag))),
583 /// Grouped information about the source code origin of a MIR entity.
584 /// Intended to be inspected by diagnostics and debuginfo.
585 /// Most passes can work with it as a whole, within a single function.
586 // The unofficial Cranelift backend, at least as of #65828, needs `SourceInfo` to implement `Eq` and
587 // `Hash`. Please ping @bjorn3 if removing them.
588 #[derive(Copy, Clone, Debug, Eq, PartialEq, TyEncodable, TyDecodable, Hash, HashStable)]
589 pub struct SourceInfo {
590 /// The source span for the AST pertaining to this MIR entity.
593 /// The source scope, keeping track of which bindings can be
594 /// seen by debuginfo, active lint levels, `unsafe {...}`, etc.
595 pub scope: SourceScope,
600 pub fn outermost(span: Span) -> Self {
601 SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE }
605 ///////////////////////////////////////////////////////////////////////////
608 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, TyEncodable, TyDecodable)]
609 #[derive(Hash, HashStable)]
610 pub enum BorrowKind {
611 /// Data must be immutable and is aliasable.
614 /// The immediately borrowed place must be immutable, but projections from
615 /// it don't need to be. For example, a shallow borrow of `a.b` doesn't
616 /// conflict with a mutable borrow of `a.b.c`.
618 /// This is used when lowering matches: when matching on a place we want to
619 /// ensure that place have the same value from the start of the match until
620 /// an arm is selected. This prevents this code from compiling:
622 /// let mut x = &Some(0);
625 /// Some(_) if { x = &None; false } => (),
629 /// This can't be a shared borrow because mutably borrowing (*x as Some).0
630 /// should not prevent `if let None = x { ... }`, for example, because the
631 /// mutating `(*x as Some).0` can't affect the discriminant of `x`.
632 /// We can also report errors with this kind of borrow differently.
635 /// Data must be immutable but not aliasable. This kind of borrow
636 /// cannot currently be expressed by the user and is used only in
637 /// implicit closure bindings. It is needed when the closure is
638 /// borrowing or mutating a mutable referent, e.g.:
640 /// let x: &mut isize = ...;
641 /// let y = || *x += 5;
643 /// If we were to try to translate this closure into a more explicit
644 /// form, we'd encounter an error with the code as written:
646 /// struct Env { x: & &mut isize }
647 /// let x: &mut isize = ...;
648 /// let y = (&mut Env { &x }, fn_ptr); // Closure is pair of env and fn
649 /// fn fn_ptr(env: &mut Env) { **env.x += 5; }
651 /// This is then illegal because you cannot mutate an `&mut` found
652 /// in an aliasable location. To solve, you'd have to translate with
653 /// an `&mut` borrow:
655 /// struct Env { x: &mut &mut isize }
656 /// let x: &mut isize = ...;
657 /// let y = (&mut Env { &mut x }, fn_ptr); // changed from &x to &mut x
658 /// fn fn_ptr(env: &mut Env) { **env.x += 5; }
660 /// Now the assignment to `**env.x` is legal, but creating a
661 /// mutable pointer to `x` is not because `x` is not mutable. We
662 /// could fix this by declaring `x` as `let mut x`. This is ok in
663 /// user code, if awkward, but extra weird for closures, since the
664 /// borrow is hidden.
666 /// So we introduce a "unique imm" borrow -- the referent is
667 /// immutable, but not aliasable. This solves the problem. For
668 /// simplicity, we don't give users the way to express this
669 /// borrow, it's just used when translating closures.
672 /// Data is mutable and not aliasable.
674 /// `true` if this borrow arose from method-call auto-ref
675 /// (i.e., `adjustment::Adjust::Borrow`).
676 allow_two_phase_borrow: bool,
681 pub fn allows_two_phase_borrow(&self) -> bool {
683 BorrowKind::Shared | BorrowKind::Shallow | BorrowKind::Unique => false,
684 BorrowKind::Mut { allow_two_phase_borrow } => allow_two_phase_borrow,
688 pub fn describe_mutability(&self) -> String {
690 BorrowKind::Shared | BorrowKind::Shallow | BorrowKind::Unique => {
691 "immutable".to_string()
693 BorrowKind::Mut { .. } => "mutable".to_string(),
698 ///////////////////////////////////////////////////////////////////////////
699 // Variables and temps
701 rustc_index::newtype_index! {
704 DEBUG_FORMAT = "_{}",
705 const RETURN_PLACE = 0,
709 impl Atom for Local {
710 fn index(self) -> usize {
715 /// Classifies locals into categories. See `Body::local_kind`.
716 #[derive(Clone, Copy, PartialEq, Eq, Debug, HashStable)]
718 /// User-declared variable binding.
720 /// Compiler-introduced temporary.
722 /// Function argument.
724 /// Location of function's return value.
728 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
729 pub struct VarBindingForm<'tcx> {
730 /// Is variable bound via `x`, `mut x`, `ref x`, or `ref mut x`?
731 pub binding_mode: ty::BindingMode,
732 /// If an explicit type was provided for this variable binding,
733 /// this holds the source Span of that type.
735 /// NOTE: if you want to change this to a `HirId`, be wary that
736 /// doing so breaks incremental compilation (as of this writing),
737 /// while a `Span` does not cause our tests to fail.
738 pub opt_ty_info: Option<Span>,
739 /// Place of the RHS of the =, or the subject of the `match` where this
740 /// variable is initialized. None in the case of `let PATTERN;`.
741 /// Some((None, ..)) in the case of and `let [mut] x = ...` because
742 /// (a) the right-hand side isn't evaluated as a place expression.
743 /// (b) it gives a way to separate this case from the remaining cases
745 pub opt_match_place: Option<(Option<Place<'tcx>>, Span)>,
746 /// The span of the pattern in which this variable was bound.
750 #[derive(Clone, Debug, TyEncodable, TyDecodable)]
751 pub enum BindingForm<'tcx> {
752 /// This is a binding for a non-`self` binding, or a `self` that has an explicit type.
753 Var(VarBindingForm<'tcx>),
754 /// Binding for a `self`/`&self`/`&mut self` binding where the type is implicit.
755 ImplicitSelf(ImplicitSelfKind),
756 /// Reference used in a guard expression to ensure immutability.
760 /// Represents what type of implicit self a function has, if any.
761 #[derive(Clone, Copy, PartialEq, Debug, TyEncodable, TyDecodable, HashStable)]
762 pub enum ImplicitSelfKind {
763 /// Represents a `fn x(self);`.
765 /// Represents a `fn x(mut self);`.
767 /// Represents a `fn x(&self);`.
769 /// Represents a `fn x(&mut self);`.
771 /// Represents when a function does not have a self argument or
772 /// when a function has a `self: X` argument.
776 TrivialTypeFoldableAndLiftImpls! { BindingForm<'tcx>, }
778 mod binding_form_impl {
779 use crate::ich::StableHashingContext;
780 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
782 impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for super::BindingForm<'tcx> {
783 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
784 use super::BindingForm::*;
785 std::mem::discriminant(self).hash_stable(hcx, hasher);
788 Var(binding) => binding.hash_stable(hcx, hasher),
789 ImplicitSelf(kind) => kind.hash_stable(hcx, hasher),
796 /// `BlockTailInfo` is attached to the `LocalDecl` for temporaries
797 /// created during evaluation of expressions in a block tail
798 /// expression; that is, a block like `{ STMT_1; STMT_2; EXPR }`.
800 /// It is used to improve diagnostics when such temporaries are
801 /// involved in borrow_check errors, e.g., explanations of where the
802 /// temporaries come from, when their destructors are run, and/or how
803 /// one might revise the code to satisfy the borrow checker's rules.
804 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
805 pub struct BlockTailInfo {
806 /// If `true`, then the value resulting from evaluating this tail
807 /// expression is ignored by the block's expression context.
809 /// Examples include `{ ...; tail };` and `let _ = { ...; tail };`
810 /// but not e.g., `let _x = { ...; tail };`
811 pub tail_result_is_ignored: bool,
813 /// `Span` of the tail expression.
819 /// This can be a binding declared by the user, a temporary inserted by the compiler, a function
820 /// argument, or the return place.
821 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
822 pub struct LocalDecl<'tcx> {
823 /// Whether this is a mutable binding (i.e., `let x` or `let mut x`).
825 /// Temporaries and the return place are always mutable.
826 pub mutability: Mutability,
828 // FIXME(matthewjasper) Don't store in this in `Body`
829 pub local_info: Option<Box<LocalInfo<'tcx>>>,
831 /// `true` if this is an internal local.
833 /// These locals are not based on types in the source code and are only used
834 /// for a few desugarings at the moment.
836 /// The generator transformation will sanity check the locals which are live
837 /// across a suspension point against the type components of the generator
838 /// which type checking knows are live across a suspension point. We need to
839 /// flag drop flags to avoid triggering this check as they are introduced
842 /// This should be sound because the drop flags are fully algebraic, and
843 /// therefore don't affect the auto-trait or outlives properties of the
847 /// If this local is a temporary and `is_block_tail` is `Some`,
848 /// then it is a temporary created for evaluation of some
849 /// subexpression of some block's tail expression (with no
850 /// intervening statement context).
851 // FIXME(matthewjasper) Don't store in this in `Body`
852 pub is_block_tail: Option<BlockTailInfo>,
854 /// The type of this local.
857 /// If the user manually ascribed a type to this variable,
858 /// e.g., via `let x: T`, then we carry that type here. The MIR
859 /// borrow checker needs this information since it can affect
860 /// region inference.
861 // FIXME(matthewjasper) Don't store in this in `Body`
862 pub user_ty: Option<Box<UserTypeProjections>>,
864 /// The *syntactic* (i.e., not visibility) source scope the local is defined
865 /// in. If the local was defined in a let-statement, this
866 /// is *within* the let-statement, rather than outside
869 /// This is needed because the visibility source scope of locals within
870 /// a let-statement is weird.
872 /// The reason is that we want the local to be *within* the let-statement
873 /// for lint purposes, but we want the local to be *after* the let-statement
874 /// for names-in-scope purposes.
876 /// That's it, if we have a let-statement like the one in this
880 /// fn foo(x: &str) {
881 /// #[allow(unused_mut)]
882 /// let mut x: u32 = { // <- one unused mut
883 /// let mut y: u32 = x.parse().unwrap();
890 /// Then, from a lint point of view, the declaration of `x: u32`
891 /// (and `y: u32`) are within the `#[allow(unused_mut)]` scope - the
892 /// lint scopes are the same as the AST/HIR nesting.
894 /// However, from a name lookup point of view, the scopes look more like
895 /// as if the let-statements were `match` expressions:
898 /// fn foo(x: &str) {
900 /// match x.parse().unwrap() {
909 /// We care about the name-lookup scopes for debuginfo - if the
910 /// debuginfo instruction pointer is at the call to `x.parse()`, we
911 /// want `x` to refer to `x: &str`, but if it is at the call to
912 /// `drop(x)`, we want it to refer to `x: u32`.
914 /// To allow both uses to work, we need to have more than a single scope
915 /// for a local. We have the `source_info.scope` represent the "syntactic"
916 /// lint scope (with a variable being under its let block) while the
917 /// `var_debug_info.source_info.scope` represents the "local variable"
918 /// scope (where the "rest" of a block is under all prior let-statements).
920 /// The end result looks like this:
924 /// │{ argument x: &str }
926 /// │ │{ #[allow(unused_mut)] } // This is actually split into 2 scopes
927 /// │ │ // in practice because I'm lazy.
929 /// │ │← x.source_info.scope
930 /// │ │← `x.parse().unwrap()`
932 /// │ │ │← y.source_info.scope
934 /// │ │ │{ let y: u32 }
936 /// │ │ │← y.var_debug_info.source_info.scope
939 /// │ │{ let x: u32 }
940 /// │ │← x.var_debug_info.source_info.scope
941 /// │ │← `drop(x)` // This accesses `x: u32`.
943 pub source_info: SourceInfo,
946 // `LocalDecl` is used a lot. Make sure it doesn't unintentionally get bigger.
947 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
948 static_assert_size!(LocalDecl<'_>, 56);
950 /// Extra information about a some locals that's used for diagnostics and for
951 /// classifying variables into local variables, statics, etc, which is needed e.g.
952 /// for unsafety checking.
954 /// Not used for non-StaticRef temporaries, the return place, or anonymous
955 /// function parameters.
956 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
957 pub enum LocalInfo<'tcx> {
958 /// A user-defined local variable or function parameter
960 /// The `BindingForm` is solely used for local diagnostics when generating
961 /// warnings/errors when compiling the current crate, and therefore it need
962 /// not be visible across crates.
963 User(ClearCrossCrate<BindingForm<'tcx>>),
964 /// A temporary created that references the static with the given `DefId`.
965 StaticRef { def_id: DefId, is_thread_local: bool },
966 /// A temporary created that references the const with the given `DefId`
967 ConstRef { def_id: DefId },
970 impl<'tcx> LocalDecl<'tcx> {
971 /// Returns `true` only if local is a binding that can itself be
972 /// made mutable via the addition of the `mut` keyword, namely
973 /// something like the occurrences of `x` in:
974 /// - `fn foo(x: Type) { ... }`,
976 /// - or `match ... { C(x) => ... }`
977 pub fn can_be_made_mutable(&self) -> bool {
980 Some(box LocalInfo::User(ClearCrossCrate::Set(
981 BindingForm::Var(VarBindingForm {
982 binding_mode: ty::BindingMode::BindByValue(_),
986 }) | BindingForm::ImplicitSelf(ImplicitSelfKind::Imm),
991 /// Returns `true` if local is definitely not a `ref ident` or
992 /// `ref mut ident` binding. (Such bindings cannot be made into
993 /// mutable bindings, but the inverse does not necessarily hold).
994 pub fn is_nonref_binding(&self) -> bool {
997 Some(box LocalInfo::User(ClearCrossCrate::Set(
998 BindingForm::Var(VarBindingForm {
999 binding_mode: ty::BindingMode::BindByValue(_),
1003 }) | BindingForm::ImplicitSelf(_),
1008 /// Returns `true` if this variable is a named variable or function
1009 /// parameter declared by the user.
1011 pub fn is_user_variable(&self) -> bool {
1012 matches!(self.local_info, Some(box LocalInfo::User(_)))
1015 /// Returns `true` if this is a reference to a variable bound in a `match`
1016 /// expression that is used to access said variable for the guard of the
1018 pub fn is_ref_for_guard(&self) -> bool {
1021 Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::RefForGuard)))
1025 /// Returns `Some` if this is a reference to a static item that is used to
1026 /// access that static.
1027 pub fn is_ref_to_static(&self) -> bool {
1028 matches!(self.local_info, Some(box LocalInfo::StaticRef { .. }))
1031 /// Returns `Some` if this is a reference to a thread-local static item that is used to
1032 /// access that static.
1033 pub fn is_ref_to_thread_local(&self) -> bool {
1034 match self.local_info {
1035 Some(box LocalInfo::StaticRef { is_thread_local, .. }) => is_thread_local,
1040 /// Returns `true` is the local is from a compiler desugaring, e.g.,
1041 /// `__next` from a `for` loop.
1043 pub fn from_compiler_desugaring(&self) -> bool {
1044 self.source_info.span.desugaring_kind().is_some()
1047 /// Creates a new `LocalDecl` for a temporary: mutable, non-internal.
1049 pub fn new(ty: Ty<'tcx>, span: Span) -> Self {
1050 Self::with_source_info(ty, SourceInfo::outermost(span))
1053 /// Like `LocalDecl::new`, but takes a `SourceInfo` instead of a `Span`.
1055 pub fn with_source_info(ty: Ty<'tcx>, source_info: SourceInfo) -> Self {
1057 mutability: Mutability::Mut,
1060 is_block_tail: None,
1067 /// Converts `self` into same `LocalDecl` except tagged as internal.
1069 pub fn internal(mut self) -> Self {
1070 self.internal = true;
1074 /// Converts `self` into same `LocalDecl` except tagged as immutable.
1076 pub fn immutable(mut self) -> Self {
1077 self.mutability = Mutability::Not;
1081 /// Converts `self` into same `LocalDecl` except tagged as internal temporary.
1083 pub fn block_tail(mut self, info: BlockTailInfo) -> Self {
1084 assert!(self.is_block_tail.is_none());
1085 self.is_block_tail = Some(info);
1090 #[derive(Clone, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
1091 pub enum VarDebugInfoContents<'tcx> {
1092 /// NOTE(eddyb) There's an unenforced invariant that this `Place` is
1093 /// based on a `Local`, not a `Static`, and contains no indexing.
1095 Const(Constant<'tcx>),
1098 impl<'tcx> Debug for VarDebugInfoContents<'tcx> {
1099 fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
1101 VarDebugInfoContents::Const(c) => write!(fmt, "{}", c),
1102 VarDebugInfoContents::Place(p) => write!(fmt, "{:?}", p),
1107 /// Debug information pertaining to a user variable.
1108 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
1109 pub struct VarDebugInfo<'tcx> {
1112 /// Source info of the user variable, including the scope
1113 /// within which the variable is visible (to debuginfo)
1114 /// (see `LocalDecl`'s `source_info` field for more details).
1115 pub source_info: SourceInfo,
1117 /// Where the data for this user variable is to be found.
1118 pub value: VarDebugInfoContents<'tcx>,
1121 ///////////////////////////////////////////////////////////////////////////
1124 rustc_index::newtype_index! {
1125 /// A node in the MIR [control-flow graph][CFG].
1127 /// There are no branches (e.g., `if`s, function calls, etc.) within a basic block, which makes
1128 /// it easier to do [data-flow analyses] and optimizations. Instead, branches are represented
1129 /// as an edge in a graph between basic blocks.
1131 /// Basic blocks consist of a series of [statements][Statement], ending with a
1132 /// [terminator][Terminator]. Basic blocks can have multiple predecessors and successors,
1133 /// however there is a MIR pass ([`CriticalCallEdges`]) that removes *critical edges*, which
1134 /// are edges that go from a multi-successor node to a multi-predecessor node. This pass is
1135 /// needed because some analyses require that there are no critical edges in the CFG.
1137 /// Note that this type is just an index into [`Body.basic_blocks`](Body::basic_blocks);
1138 /// the actual data that a basic block holds is in [`BasicBlockData`].
1140 /// Read more about basic blocks in the [rustc-dev-guide][guide-mir].
1142 /// [CFG]: https://rustc-dev-guide.rust-lang.org/appendix/background.html#cfg
1143 /// [data-flow analyses]:
1144 /// https://rustc-dev-guide.rust-lang.org/appendix/background.html#what-is-a-dataflow-analysis
1145 /// [`CriticalCallEdges`]: ../../rustc_mir/transform/add_call_guards/enum.AddCallGuards.html#variant.CriticalCallEdges
1146 /// [guide-mir]: https://rustc-dev-guide.rust-lang.org/mir/
1147 pub struct BasicBlock {
1149 DEBUG_FORMAT = "bb{}",
1150 const START_BLOCK = 0,
1155 pub fn start_location(self) -> Location {
1156 Location { block: self, statement_index: 0 }
1160 ///////////////////////////////////////////////////////////////////////////
1161 // BasicBlockData and Terminator
1163 /// See [`BasicBlock`] for documentation on what basic blocks are at a high level.
1164 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
1165 pub struct BasicBlockData<'tcx> {
1166 /// List of statements in this block.
1167 pub statements: Vec<Statement<'tcx>>,
1169 /// Terminator for this block.
1171 /// N.B., this should generally ONLY be `None` during construction.
1172 /// Therefore, you should generally access it via the
1173 /// `terminator()` or `terminator_mut()` methods. The only
1174 /// exception is that certain passes, such as `simplify_cfg`, swap
1175 /// out the terminator temporarily with `None` while they continue
1176 /// to recurse over the set of basic blocks.
1177 pub terminator: Option<Terminator<'tcx>>,
1179 /// If true, this block lies on an unwind path. This is used
1180 /// during codegen where distinct kinds of basic blocks may be
1181 /// generated (particularly for MSVC cleanup). Unwind blocks must
1182 /// only branch to other unwind blocks.
1183 pub is_cleanup: bool,
1186 /// Information about an assertion failure.
1187 #[derive(Clone, TyEncodable, TyDecodable, Hash, HashStable, PartialEq, PartialOrd)]
1188 pub enum AssertKind<O> {
1189 BoundsCheck { len: O, index: O },
1190 Overflow(BinOp, O, O),
1194 ResumedAfterReturn(GeneratorKind),
1195 ResumedAfterPanic(GeneratorKind),
1209 pub enum InlineAsmOperand<'tcx> {
1211 reg: InlineAsmRegOrRegClass,
1212 value: Operand<'tcx>,
1215 reg: InlineAsmRegOrRegClass,
1217 place: Option<Place<'tcx>>,
1220 reg: InlineAsmRegOrRegClass,
1222 in_value: Operand<'tcx>,
1223 out_place: Option<Place<'tcx>>,
1226 value: Box<Constant<'tcx>>,
1229 value: Box<Constant<'tcx>>,
1236 /// Type for MIR `Assert` terminator error messages.
1237 pub type AssertMessage<'tcx> = AssertKind<Operand<'tcx>>;
1239 pub type Successors<'a> =
1240 iter::Chain<option::IntoIter<&'a BasicBlock>, slice::Iter<'a, BasicBlock>>;
1241 pub type SuccessorsMut<'a> =
1242 iter::Chain<option::IntoIter<&'a mut BasicBlock>, slice::IterMut<'a, BasicBlock>>;
1244 impl<'tcx> BasicBlockData<'tcx> {
1245 pub fn new(terminator: Option<Terminator<'tcx>>) -> BasicBlockData<'tcx> {
1246 BasicBlockData { statements: vec![], terminator, is_cleanup: false }
1249 /// Accessor for terminator.
1251 /// Terminator may not be None after construction of the basic block is complete. This accessor
1252 /// provides a convenience way to reach the terminator.
1254 pub fn terminator(&self) -> &Terminator<'tcx> {
1255 self.terminator.as_ref().expect("invalid terminator state")
1259 pub fn terminator_mut(&mut self) -> &mut Terminator<'tcx> {
1260 self.terminator.as_mut().expect("invalid terminator state")
1263 pub fn retain_statements<F>(&mut self, mut f: F)
1265 F: FnMut(&mut Statement<'_>) -> bool,
1267 for s in &mut self.statements {
1274 pub fn expand_statements<F, I>(&mut self, mut f: F)
1276 F: FnMut(&mut Statement<'tcx>) -> Option<I>,
1277 I: iter::TrustedLen<Item = Statement<'tcx>>,
1279 // Gather all the iterators we'll need to splice in, and their positions.
1280 let mut splices: Vec<(usize, I)> = vec![];
1281 let mut extra_stmts = 0;
1282 for (i, s) in self.statements.iter_mut().enumerate() {
1283 if let Some(mut new_stmts) = f(s) {
1284 if let Some(first) = new_stmts.next() {
1285 // We can already store the first new statement.
1288 // Save the other statements for optimized splicing.
1289 let remaining = new_stmts.size_hint().0;
1291 splices.push((i + 1 + extra_stmts, new_stmts));
1292 extra_stmts += remaining;
1300 // Splice in the new statements, from the end of the block.
1301 // FIXME(eddyb) This could be more efficient with a "gap buffer"
1302 // where a range of elements ("gap") is left uninitialized, with
1303 // splicing adding new elements to the end of that gap and moving
1304 // existing elements from before the gap to the end of the gap.
1305 // For now, this is safe code, emulating a gap but initializing it.
1306 let mut gap = self.statements.len()..self.statements.len() + extra_stmts;
1307 self.statements.resize(
1309 Statement { source_info: SourceInfo::outermost(DUMMY_SP), kind: StatementKind::Nop },
1311 for (splice_start, new_stmts) in splices.into_iter().rev() {
1312 let splice_end = splice_start + new_stmts.size_hint().0;
1313 while gap.end > splice_end {
1316 self.statements.swap(gap.start, gap.end);
1318 self.statements.splice(splice_start..splice_end, new_stmts);
1319 gap.end = splice_start;
1323 pub fn visitable(&self, index: usize) -> &dyn MirVisitable<'tcx> {
1324 if index < self.statements.len() { &self.statements[index] } else { &self.terminator }
1328 impl<O> AssertKind<O> {
1329 /// Getting a description does not require `O` to be printable, and does not
1330 /// require allocation.
1331 /// The caller is expected to handle `BoundsCheck` separately.
1332 pub fn description(&self) -> &'static str {
1335 Overflow(BinOp::Add, _, _) => "attempt to add with overflow",
1336 Overflow(BinOp::Sub, _, _) => "attempt to subtract with overflow",
1337 Overflow(BinOp::Mul, _, _) => "attempt to multiply with overflow",
1338 Overflow(BinOp::Div, _, _) => "attempt to divide with overflow",
1339 Overflow(BinOp::Rem, _, _) => "attempt to calculate the remainder with overflow",
1340 OverflowNeg(_) => "attempt to negate with overflow",
1341 Overflow(BinOp::Shr, _, _) => "attempt to shift right with overflow",
1342 Overflow(BinOp::Shl, _, _) => "attempt to shift left with overflow",
1343 Overflow(op, _, _) => bug!("{:?} cannot overflow", op),
1344 DivisionByZero(_) => "attempt to divide by zero",
1345 RemainderByZero(_) => "attempt to calculate the remainder with a divisor of zero",
1346 ResumedAfterReturn(GeneratorKind::Gen) => "generator resumed after completion",
1347 ResumedAfterReturn(GeneratorKind::Async(_)) => "`async fn` resumed after completion",
1348 ResumedAfterPanic(GeneratorKind::Gen) => "generator resumed after panicking",
1349 ResumedAfterPanic(GeneratorKind::Async(_)) => "`async fn` resumed after panicking",
1350 BoundsCheck { .. } => bug!("Unexpected AssertKind"),
1354 /// Format the message arguments for the `assert(cond, msg..)` terminator in MIR printing.
1355 pub fn fmt_assert_args<W: Write>(&self, f: &mut W) -> fmt::Result
1361 BoundsCheck { ref len, ref index } => write!(
1363 "\"index out of bounds: the length is {{}} but the index is {{}}\", {:?}, {:?}",
1367 OverflowNeg(op) => {
1368 write!(f, "\"attempt to negate `{{}}`, which would overflow\", {:?}", op)
1370 DivisionByZero(op) => write!(f, "\"attempt to divide `{{}}` by zero\", {:?}", op),
1371 RemainderByZero(op) => write!(
1373 "\"attempt to calculate the remainder of `{{}}` with a divisor of zero\", {:?}",
1376 Overflow(BinOp::Add, l, r) => write!(
1378 "\"attempt to compute `{{}} + {{}}`, which would overflow\", {:?}, {:?}",
1381 Overflow(BinOp::Sub, l, r) => write!(
1383 "\"attempt to compute `{{}} - {{}}`, which would overflow\", {:?}, {:?}",
1386 Overflow(BinOp::Mul, l, r) => write!(
1388 "\"attempt to compute `{{}} * {{}}`, which would overflow\", {:?}, {:?}",
1391 Overflow(BinOp::Div, l, r) => write!(
1393 "\"attempt to compute `{{}} / {{}}`, which would overflow\", {:?}, {:?}",
1396 Overflow(BinOp::Rem, l, r) => write!(
1398 "\"attempt to compute the remainder of `{{}} % {{}}`, which would overflow\", {:?}, {:?}",
1401 Overflow(BinOp::Shr, _, r) => {
1402 write!(f, "\"attempt to shift right by `{{}}`, which would overflow\", {:?}", r)
1404 Overflow(BinOp::Shl, _, r) => {
1405 write!(f, "\"attempt to shift left by `{{}}`, which would overflow\", {:?}", r)
1407 _ => write!(f, "\"{}\"", self.description()),
1412 impl<O: fmt::Debug> fmt::Debug for AssertKind<O> {
1413 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1416 BoundsCheck { ref len, ref index } => write!(
1418 "index out of bounds: the length is {:?} but the index is {:?}",
1421 OverflowNeg(op) => write!(f, "attempt to negate `{:#?}`, which would overflow", op),
1422 DivisionByZero(op) => write!(f, "attempt to divide `{:#?}` by zero", op),
1423 RemainderByZero(op) => write!(
1425 "attempt to calculate the remainder of `{:#?}` with a divisor of zero",
1428 Overflow(BinOp::Add, l, r) => {
1429 write!(f, "attempt to compute `{:#?} + {:#?}`, which would overflow", l, r)
1431 Overflow(BinOp::Sub, l, r) => {
1432 write!(f, "attempt to compute `{:#?} - {:#?}`, which would overflow", l, r)
1434 Overflow(BinOp::Mul, l, r) => {
1435 write!(f, "attempt to compute `{:#?} * {:#?}`, which would overflow", l, r)
1437 Overflow(BinOp::Div, l, r) => {
1438 write!(f, "attempt to compute `{:#?} / {:#?}`, which would overflow", l, r)
1440 Overflow(BinOp::Rem, l, r) => write!(
1442 "attempt to compute the remainder of `{:#?} % {:#?}`, which would overflow",
1445 Overflow(BinOp::Shr, _, r) => {
1446 write!(f, "attempt to shift right by `{:#?}`, which would overflow", r)
1448 Overflow(BinOp::Shl, _, r) => {
1449 write!(f, "attempt to shift left by `{:#?}`, which would overflow", r)
1451 _ => write!(f, "{}", self.description()),
1456 ///////////////////////////////////////////////////////////////////////////
1459 #[derive(Clone, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
1460 pub struct Statement<'tcx> {
1461 pub source_info: SourceInfo,
1462 pub kind: StatementKind<'tcx>,
1465 // `Statement` is used a lot. Make sure it doesn't unintentionally get bigger.
1466 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
1467 static_assert_size!(Statement<'_>, 32);
1469 impl Statement<'_> {
1470 /// Changes a statement to a nop. This is both faster than deleting instructions and avoids
1471 /// invalidating statement indices in `Location`s.
1472 pub fn make_nop(&mut self) {
1473 self.kind = StatementKind::Nop
1476 /// Changes a statement to a nop and returns the original statement.
1477 pub fn replace_nop(&mut self) -> Self {
1479 source_info: self.source_info,
1480 kind: mem::replace(&mut self.kind, StatementKind::Nop),
1485 #[derive(Clone, Debug, PartialEq, TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable)]
1486 pub enum StatementKind<'tcx> {
1487 /// Write the RHS Rvalue to the LHS Place.
1488 Assign(Box<(Place<'tcx>, Rvalue<'tcx>)>),
1490 /// This represents all the reading that a pattern match may do
1491 /// (e.g., inspecting constants and discriminant values), and the
1492 /// kind of pattern it comes from. This is in order to adapt potential
1493 /// error messages to these specific patterns.
1495 /// Note that this also is emitted for regular `let` bindings to ensure that locals that are
1496 /// never accessed still get some sanity checks for, e.g., `let x: ! = ..;`
1497 FakeRead(Box<(FakeReadCause, Place<'tcx>)>),
1499 /// Write the discriminant for a variant to the enum Place.
1500 SetDiscriminant { place: Box<Place<'tcx>>, variant_index: VariantIdx },
1502 /// Start a live range for the storage of the local.
1505 /// End the current live range for the storage of the local.
1508 /// Executes a piece of inline Assembly. Stored in a Box to keep the size
1509 /// of `StatementKind` low.
1510 LlvmInlineAsm(Box<LlvmInlineAsm<'tcx>>),
1512 /// Retag references in the given place, ensuring they got fresh tags. This is
1513 /// part of the Stacked Borrows model. These statements are currently only interpreted
1514 /// by miri and only generated when "-Z mir-emit-retag" is passed.
1515 /// See <https://internals.rust-lang.org/t/stacked-borrows-an-aliasing-model-for-rust/8153/>
1516 /// for more details.
1517 Retag(RetagKind, Box<Place<'tcx>>),
1519 /// Encodes a user's type ascription. These need to be preserved
1520 /// intact so that NLL can respect them. For example:
1524 /// The effect of this annotation is to relate the type `T_y` of the place `y`
1525 /// to the user-given type `T`. The effect depends on the specified variance:
1527 /// - `Covariant` -- requires that `T_y <: T`
1528 /// - `Contravariant` -- requires that `T_y :> T`
1529 /// - `Invariant` -- requires that `T_y == T`
1530 /// - `Bivariant` -- no effect
1531 AscribeUserType(Box<(Place<'tcx>, UserTypeProjection)>, ty::Variance),
1533 /// Marks the start of a "coverage region", injected with '-Zinstrument-coverage'. A
1534 /// `Coverage` statement carries metadata about the coverage region, used to inject a coverage
1535 /// map into the binary. If `Coverage::kind` is a `Counter`, the statement also generates
1536 /// executable code, to increment a counter variable at runtime, each time the code region is
1538 Coverage(Box<Coverage>),
1540 /// Denotes a call to the intrinsic function copy_overlapping, where `src_dst` denotes the
1541 /// memory being read from and written to(one field to save memory), and size
1542 /// indicates how many bytes are being copied over.
1543 CopyNonOverlapping(Box<CopyNonOverlapping<'tcx>>),
1545 /// No-op. Useful for deleting instructions without affecting statement indices.
1549 impl<'tcx> StatementKind<'tcx> {
1550 pub fn as_assign_mut(&mut self) -> Option<&mut (Place<'tcx>, Rvalue<'tcx>)> {
1552 StatementKind::Assign(x) => Some(x),
1557 pub fn as_assign(&self) -> Option<&(Place<'tcx>, Rvalue<'tcx>)> {
1559 StatementKind::Assign(x) => Some(x),
1565 /// Describes what kind of retag is to be performed.
1566 #[derive(Copy, Clone, TyEncodable, TyDecodable, Debug, PartialEq, Eq, Hash, HashStable)]
1567 pub enum RetagKind {
1568 /// The initial retag when entering a function.
1570 /// Retag preparing for a two-phase borrow.
1572 /// Retagging raw pointers.
1574 /// A "normal" retag.
1578 /// The `FakeReadCause` describes the type of pattern why a FakeRead statement exists.
1579 #[derive(Copy, Clone, TyEncodable, TyDecodable, Debug, Hash, HashStable, PartialEq)]
1580 pub enum FakeReadCause {
1581 /// Inject a fake read of the borrowed input at the end of each guards
1584 /// This should ensure that you cannot change the variant for an enum while
1585 /// you are in the midst of matching on it.
1588 /// `let x: !; match x {}` doesn't generate any read of x so we need to
1589 /// generate a read of x to check that it is initialized and safe.
1591 /// If a closure pattern matches a Place starting with an Upvar, then we introduce a
1592 /// FakeRead for that Place outside the closure, in such a case this option would be
1593 /// Some(closure_def_id).
1594 /// Otherwise, the value of the optional DefId will be None.
1595 ForMatchedPlace(Option<DefId>),
1597 /// A fake read of the RefWithinGuard version of a bind-by-value variable
1598 /// in a match guard to ensure that it's value hasn't change by the time
1599 /// we create the OutsideGuard version.
1602 /// Officially, the semantics of
1604 /// `let pattern = <expr>;`
1606 /// is that `<expr>` is evaluated into a temporary and then this temporary is
1607 /// into the pattern.
1609 /// However, if we see the simple pattern `let var = <expr>`, we optimize this to
1610 /// evaluate `<expr>` directly into the variable `var`. This is mostly unobservable,
1611 /// but in some cases it can affect the borrow checker, as in #53695.
1612 /// Therefore, we insert a "fake read" here to ensure that we get
1613 /// appropriate errors.
1615 /// If a closure pattern matches a Place starting with an Upvar, then we introduce a
1616 /// FakeRead for that Place outside the closure, in such a case this option would be
1617 /// Some(closure_def_id).
1618 /// Otherwise, the value of the optional DefId will be None.
1619 ForLet(Option<DefId>),
1621 /// If we have an index expression like
1623 /// (*x)[1][{ x = y; 4}]
1625 /// then the first bounds check is invalidated when we evaluate the second
1626 /// index expression. Thus we create a fake borrow of `x` across the second
1627 /// indexer, which will cause a borrow check error.
1631 #[derive(Clone, Debug, PartialEq, TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable)]
1632 pub struct LlvmInlineAsm<'tcx> {
1633 pub asm: hir::LlvmInlineAsmInner,
1634 pub outputs: Box<[Place<'tcx>]>,
1635 pub inputs: Box<[(Span, Operand<'tcx>)]>,
1638 impl Debug for Statement<'_> {
1639 fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
1640 use self::StatementKind::*;
1642 Assign(box (ref place, ref rv)) => write!(fmt, "{:?} = {:?}", place, rv),
1643 FakeRead(box (ref cause, ref place)) => {
1644 write!(fmt, "FakeRead({:?}, {:?})", cause, place)
1646 Retag(ref kind, ref place) => write!(
1650 RetagKind::FnEntry => "[fn entry] ",
1651 RetagKind::TwoPhase => "[2phase] ",
1652 RetagKind::Raw => "[raw] ",
1653 RetagKind::Default => "",
1657 StorageLive(ref place) => write!(fmt, "StorageLive({:?})", place),
1658 StorageDead(ref place) => write!(fmt, "StorageDead({:?})", place),
1659 SetDiscriminant { ref place, variant_index } => {
1660 write!(fmt, "discriminant({:?}) = {:?}", place, variant_index)
1662 LlvmInlineAsm(ref asm) => {
1663 write!(fmt, "llvm_asm!({:?} : {:?} : {:?})", asm.asm, asm.outputs, asm.inputs)
1665 AscribeUserType(box (ref place, ref c_ty), ref variance) => {
1666 write!(fmt, "AscribeUserType({:?}, {:?}, {:?})", place, variance, c_ty)
1668 Coverage(box self::Coverage { ref kind, code_region: Some(ref rgn) }) => {
1669 write!(fmt, "Coverage::{:?} for {:?}", kind, rgn)
1671 Coverage(box ref coverage) => write!(fmt, "Coverage::{:?}", coverage.kind),
1672 CopyNonOverlapping(box crate::mir::CopyNonOverlapping {
1677 write!(fmt, "copy_nonoverlapping(src={:?}, dst={:?}, count={:?})", src, dst, count)
1679 Nop => write!(fmt, "nop"),
1684 #[derive(Clone, Debug, PartialEq, TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable)]
1685 pub struct Coverage {
1686 pub kind: CoverageKind,
1687 pub code_region: Option<CodeRegion>,
1690 #[derive(Clone, Debug, PartialEq, TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable)]
1691 pub struct CopyNonOverlapping<'tcx> {
1692 pub src: Operand<'tcx>,
1693 pub dst: Operand<'tcx>,
1694 /// Number of elements to copy from src to dest, not bytes.
1695 pub count: Operand<'tcx>,
1698 ///////////////////////////////////////////////////////////////////////////
1701 /// A path to a value; something that can be evaluated without
1702 /// changing or disturbing program state.
1703 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, TyEncodable, HashStable)]
1704 pub struct Place<'tcx> {
1707 /// projection out of a place (access a field, deref a pointer, etc)
1708 pub projection: &'tcx List<PlaceElem<'tcx>>,
1711 #[cfg(target_arch = "x86_64")]
1712 static_assert_size!(Place<'_>, 16);
1714 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
1715 #[derive(TyEncodable, TyDecodable, HashStable)]
1716 pub enum ProjectionElem<V, T> {
1721 /// These indices are generated by slice patterns. Easiest to explain
1725 /// [X, _, .._, _, _] => { offset: 0, min_length: 4, from_end: false },
1726 /// [_, X, .._, _, _] => { offset: 1, min_length: 4, from_end: false },
1727 /// [_, _, .._, X, _] => { offset: 2, min_length: 4, from_end: true },
1728 /// [_, _, .._, _, X] => { offset: 1, min_length: 4, from_end: true },
1731 /// index or -index (in Python terms), depending on from_end
1733 /// The thing being indexed must be at least this long. For arrays this
1734 /// is always the exact length.
1736 /// Counting backwards from end? This is always false when indexing an
1741 /// These indices are generated by slice patterns.
1743 /// If `from_end` is true `slice[from..slice.len() - to]`.
1744 /// Otherwise `array[from..to]`.
1748 /// Whether `to` counts from the start or end of the array/slice.
1749 /// For `PlaceElem`s this is `true` if and only if the base is a slice.
1750 /// For `ProjectionKind`, this can also be `true` for arrays.
1754 /// "Downcast" to a variant of an ADT. Currently, we only introduce
1755 /// this for ADTs with more than one variant. It may be better to
1756 /// just introduce it always, or always for enums.
1758 /// The included Symbol is the name of the variant, used for printing MIR.
1759 Downcast(Option<Symbol>, VariantIdx),
1762 impl<V, T> ProjectionElem<V, T> {
1763 /// Returns `true` if the target of this projection may refer to a different region of memory
1765 fn is_indirect(&self) -> bool {
1767 Self::Deref => true,
1771 | Self::ConstantIndex { .. }
1772 | Self::Subslice { .. }
1773 | Self::Downcast(_, _) => false,
1778 /// Alias for projections as they appear in places, where the base is a place
1779 /// and the index is a local.
1780 pub type PlaceElem<'tcx> = ProjectionElem<Local, Ty<'tcx>>;
1782 // At least on 64 bit systems, `PlaceElem` should not be larger than two pointers.
1783 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
1784 static_assert_size!(PlaceElem<'_>, 24);
1786 /// Alias for projections as they appear in `UserTypeProjection`, where we
1787 /// need neither the `V` parameter for `Index` nor the `T` for `Field`.
1788 pub type ProjectionKind = ProjectionElem<(), ()>;
1790 rustc_index::newtype_index! {
1793 DEBUG_FORMAT = "field[{}]"
1797 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
1798 pub struct PlaceRef<'tcx> {
1800 pub projection: &'tcx [PlaceElem<'tcx>],
1803 impl<'tcx> Place<'tcx> {
1804 // FIXME change this to a const fn by also making List::empty a const fn.
1805 pub fn return_place() -> Place<'tcx> {
1806 Place { local: RETURN_PLACE, projection: List::empty() }
1809 /// Returns `true` if this `Place` contains a `Deref` projection.
1811 /// If `Place::is_indirect` returns false, the caller knows that the `Place` refers to the
1812 /// same region of memory as its base.
1813 pub fn is_indirect(&self) -> bool {
1814 self.projection.iter().any(|elem| elem.is_indirect())
1817 /// Finds the innermost `Local` from this `Place`, *if* it is either a local itself or
1818 /// a single deref of a local.
1820 pub fn local_or_deref_local(&self) -> Option<Local> {
1821 self.as_ref().local_or_deref_local()
1824 /// If this place represents a local variable like `_X` with no
1825 /// projections, return `Some(_X)`.
1827 pub fn as_local(&self) -> Option<Local> {
1828 self.as_ref().as_local()
1832 pub fn as_ref(&self) -> PlaceRef<'tcx> {
1833 PlaceRef { local: self.local, projection: &self.projection }
1836 /// Iterate over the projections in evaluation order, i.e., the first element is the base with
1837 /// its projection and then subsequently more projections are added.
1838 /// As a concrete example, given the place a.b.c, this would yield:
1842 /// Given a place without projections, the iterator is empty.
1844 pub fn iter_projections(
1846 ) -> impl Iterator<Item = (PlaceRef<'tcx>, PlaceElem<'tcx>)> + DoubleEndedIterator {
1847 self.projection.iter().enumerate().map(move |(i, proj)| {
1848 let base = PlaceRef { local: self.local, projection: &self.projection[..i] };
1854 impl From<Local> for Place<'_> {
1855 fn from(local: Local) -> Self {
1856 Place { local, projection: List::empty() }
1860 impl<'tcx> PlaceRef<'tcx> {
1861 /// Finds the innermost `Local` from this `Place`, *if* it is either a local itself or
1862 /// a single deref of a local.
1863 pub fn local_or_deref_local(&self) -> Option<Local> {
1865 PlaceRef { local, projection: [] }
1866 | PlaceRef { local, projection: [ProjectionElem::Deref] } => Some(local),
1871 /// If this place represents a local variable like `_X` with no
1872 /// projections, return `Some(_X)`.
1874 pub fn as_local(&self) -> Option<Local> {
1876 PlaceRef { local, projection: [] } => Some(local),
1882 pub fn last_projection(&self) -> Option<(PlaceRef<'tcx>, PlaceElem<'tcx>)> {
1883 if let &[ref proj_base @ .., elem] = self.projection {
1884 Some((PlaceRef { local: self.local, projection: proj_base }, elem))
1891 impl Debug for Place<'_> {
1892 fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
1893 for elem in self.projection.iter().rev() {
1895 ProjectionElem::Downcast(_, _) | ProjectionElem::Field(_, _) => {
1896 write!(fmt, "(").unwrap();
1898 ProjectionElem::Deref => {
1899 write!(fmt, "(*").unwrap();
1901 ProjectionElem::Index(_)
1902 | ProjectionElem::ConstantIndex { .. }
1903 | ProjectionElem::Subslice { .. } => {}
1907 write!(fmt, "{:?}", self.local)?;
1909 for elem in self.projection.iter() {
1911 ProjectionElem::Downcast(Some(name), _index) => {
1912 write!(fmt, " as {})", name)?;
1914 ProjectionElem::Downcast(None, index) => {
1915 write!(fmt, " as variant#{:?})", index)?;
1917 ProjectionElem::Deref => {
1920 ProjectionElem::Field(field, ty) => {
1921 write!(fmt, ".{:?}: {:?})", field.index(), ty)?;
1923 ProjectionElem::Index(ref index) => {
1924 write!(fmt, "[{:?}]", index)?;
1926 ProjectionElem::ConstantIndex { offset, min_length, from_end: false } => {
1927 write!(fmt, "[{:?} of {:?}]", offset, min_length)?;
1929 ProjectionElem::ConstantIndex { offset, min_length, from_end: true } => {
1930 write!(fmt, "[-{:?} of {:?}]", offset, min_length)?;
1932 ProjectionElem::Subslice { from, to, from_end: true } if to == 0 => {
1933 write!(fmt, "[{:?}:]", from)?;
1935 ProjectionElem::Subslice { from, to, from_end: true } if from == 0 => {
1936 write!(fmt, "[:-{:?}]", to)?;
1938 ProjectionElem::Subslice { from, to, from_end: true } => {
1939 write!(fmt, "[{:?}:-{:?}]", from, to)?;
1941 ProjectionElem::Subslice { from, to, from_end: false } => {
1942 write!(fmt, "[{:?}..{:?}]", from, to)?;
1951 ///////////////////////////////////////////////////////////////////////////
1954 rustc_index::newtype_index! {
1955 pub struct SourceScope {
1957 DEBUG_FORMAT = "scope[{}]",
1958 const OUTERMOST_SOURCE_SCOPE = 0,
1963 /// Finds the original HirId this MIR item came from.
1964 /// This is necessary after MIR optimizations, as otherwise we get a HirId
1965 /// from the function that was inlined instead of the function call site.
1968 source_scopes: &IndexVec<SourceScope, SourceScopeData<'tcx>>,
1969 ) -> Option<HirId> {
1970 let mut data = &source_scopes[self];
1971 // FIXME(oli-obk): we should be able to just walk the `inlined_parent_scope`, but it
1972 // does not work as I thought it would. Needs more investigation and documentation.
1973 while data.inlined.is_some() {
1975 data = &source_scopes[data.parent_scope.unwrap()];
1978 match &data.local_data {
1979 ClearCrossCrate::Set(data) => Some(data.lint_root),
1980 ClearCrossCrate::Clear => None,
1985 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
1986 pub struct SourceScopeData<'tcx> {
1988 pub parent_scope: Option<SourceScope>,
1990 /// Whether this scope is the root of a scope tree of another body,
1991 /// inlined into this body by the MIR inliner.
1992 /// `ty::Instance` is the callee, and the `Span` is the call site.
1993 pub inlined: Option<(ty::Instance<'tcx>, Span)>,
1995 /// Nearest (transitive) parent scope (if any) which is inlined.
1996 /// This is an optimization over walking up `parent_scope`
1997 /// until a scope with `inlined: Some(...)` is found.
1998 pub inlined_parent_scope: Option<SourceScope>,
2000 /// Crate-local information for this source scope, that can't (and
2001 /// needn't) be tracked across crates.
2002 pub local_data: ClearCrossCrate<SourceScopeLocalData>,
2005 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
2006 pub struct SourceScopeLocalData {
2007 /// An `HirId` with lint levels equivalent to this scope's lint levels.
2008 pub lint_root: hir::HirId,
2009 /// The unsafe block that contains this node.
2013 ///////////////////////////////////////////////////////////////////////////
2016 /// These are values that can appear inside an rvalue. They are intentionally
2017 /// limited to prevent rvalues from being nested in one another.
2018 #[derive(Clone, PartialEq, PartialOrd, TyEncodable, TyDecodable, Hash, HashStable)]
2019 pub enum Operand<'tcx> {
2020 /// Copy: The value must be available for use afterwards.
2022 /// This implies that the type of the place must be `Copy`; this is true
2023 /// by construction during build, but also checked by the MIR type checker.
2026 /// Move: The value (including old borrows of it) will not be used again.
2028 /// Safe for values of all types (modulo future developments towards `?Move`).
2029 /// Correct usage patterns are enforced by the borrow checker for safe code.
2030 /// `Copy` may be converted to `Move` to enable "last-use" optimizations.
2033 /// Synthesizes a constant value.
2034 Constant(Box<Constant<'tcx>>),
2037 #[cfg(target_arch = "x86_64")]
2038 static_assert_size!(Operand<'_>, 24);
2040 impl<'tcx> Debug for Operand<'tcx> {
2041 fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
2042 use self::Operand::*;
2044 Constant(ref a) => write!(fmt, "{:?}", a),
2045 Copy(ref place) => write!(fmt, "{:?}", place),
2046 Move(ref place) => write!(fmt, "move {:?}", place),
2051 impl<'tcx> Operand<'tcx> {
2052 /// Convenience helper to make a constant that refers to the fn
2053 /// with given `DefId` and substs. Since this is used to synthesize
2054 /// MIR, assumes `user_ty` is None.
2055 pub fn function_handle(
2058 substs: SubstsRef<'tcx>,
2061 let ty = tcx.type_of(def_id).subst(tcx, substs);
2062 Operand::Constant(Box::new(Constant {
2065 literal: ConstantKind::Ty(ty::Const::zero_sized(tcx, ty)),
2069 pub fn is_move(&self) -> bool {
2070 matches!(self, Operand::Move(..))
2073 /// Convenience helper to make a literal-like constant from a given scalar value.
2074 /// Since this is used to synthesize MIR, assumes `user_ty` is None.
2075 pub fn const_from_scalar(
2080 ) -> Operand<'tcx> {
2082 let param_env_and_ty = ty::ParamEnv::empty().and(ty);
2084 .layout_of(param_env_and_ty)
2085 .unwrap_or_else(|e| panic!("could not compute layout for {:?}: {:?}", ty, e))
2087 let scalar_size = match val {
2088 Scalar::Int(int) => int.size(),
2089 _ => panic!("Invalid scalar type {:?}", val),
2091 scalar_size == type_size
2093 Operand::Constant(Box::new(Constant {
2096 literal: ConstantKind::Val(ConstValue::Scalar(val), ty),
2100 pub fn to_copy(&self) -> Self {
2102 Operand::Copy(_) | Operand::Constant(_) => self.clone(),
2103 Operand::Move(place) => Operand::Copy(place),
2107 /// Returns the `Place` that is the target of this `Operand`, or `None` if this `Operand` is a
2109 pub fn place(&self) -> Option<Place<'tcx>> {
2111 Operand::Copy(place) | Operand::Move(place) => Some(*place),
2112 Operand::Constant(_) => None,
2116 /// Returns the `Constant` that is the target of this `Operand`, or `None` if this `Operand` is a
2118 pub fn constant(&self) -> Option<&Constant<'tcx>> {
2120 Operand::Constant(x) => Some(&**x),
2121 Operand::Copy(_) | Operand::Move(_) => None,
2126 ///////////////////////////////////////////////////////////////////////////
2129 #[derive(Clone, TyEncodable, TyDecodable, Hash, HashStable, PartialEq)]
2130 pub enum Rvalue<'tcx> {
2131 /// x (either a move or copy, depending on type of x)
2135 Repeat(Operand<'tcx>, &'tcx ty::Const<'tcx>),
2138 Ref(Region<'tcx>, BorrowKind, Place<'tcx>),
2140 /// Accessing a thread local static. This is inherently a runtime operation, even if llvm
2141 /// treats it as an access to a static. This `Rvalue` yields a reference to the thread local
2143 ThreadLocalRef(DefId),
2145 /// Create a raw pointer to the given place
2146 /// Can be generated by raw address of expressions (`&raw const x`),
2147 /// or when casting a reference to a raw pointer.
2148 AddressOf(Mutability, Place<'tcx>),
2150 /// length of a `[X]` or `[X;n]` value
2153 Cast(CastKind, Operand<'tcx>, Ty<'tcx>),
2155 BinaryOp(BinOp, Box<(Operand<'tcx>, Operand<'tcx>)>),
2156 CheckedBinaryOp(BinOp, Box<(Operand<'tcx>, Operand<'tcx>)>),
2158 NullaryOp(NullOp, Ty<'tcx>),
2159 UnaryOp(UnOp, Operand<'tcx>),
2161 /// Read the discriminant of an ADT.
2163 /// Undefined (i.e., no effort is made to make it defined, but there’s no reason why it cannot
2164 /// be defined to return, say, a 0) if ADT is not an enum.
2165 Discriminant(Place<'tcx>),
2167 /// Creates an aggregate value, like a tuple or struct. This is
2168 /// only needed because we want to distinguish `dest = Foo { x:
2169 /// ..., y: ... }` from `dest.x = ...; dest.y = ...;` in the case
2170 /// that `Foo` has a destructor. These rvalues can be optimized
2171 /// away after type-checking and before lowering.
2172 Aggregate(Box<AggregateKind<'tcx>>, Vec<Operand<'tcx>>),
2175 #[cfg(target_arch = "x86_64")]
2176 static_assert_size!(Rvalue<'_>, 40);
2178 #[derive(Clone, Copy, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
2181 Pointer(PointerCast),
2184 #[derive(Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
2185 pub enum AggregateKind<'tcx> {
2186 /// The type is of the element
2190 /// The second field is the variant index. It's equal to 0 for struct
2191 /// and union expressions. The fourth field is
2192 /// active field number and is present only for union expressions
2193 /// -- e.g., for a union expression `SomeUnion { c: .. }`, the
2194 /// active field index would identity the field `c`
2195 Adt(&'tcx AdtDef, VariantIdx, SubstsRef<'tcx>, Option<UserTypeAnnotationIndex>, Option<usize>),
2197 Closure(DefId, SubstsRef<'tcx>),
2198 Generator(DefId, SubstsRef<'tcx>, hir::Movability),
2201 #[cfg(target_arch = "x86_64")]
2202 static_assert_size!(AggregateKind<'_>, 48);
2204 #[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
2206 /// The `+` operator (addition)
2208 /// The `-` operator (subtraction)
2210 /// The `*` operator (multiplication)
2212 /// The `/` operator (division)
2214 /// The `%` operator (modulus)
2216 /// The `^` operator (bitwise xor)
2218 /// The `&` operator (bitwise and)
2220 /// The `|` operator (bitwise or)
2222 /// The `<<` operator (shift left)
2224 /// The `>>` operator (shift right)
2226 /// The `==` operator (equality)
2228 /// The `<` operator (less than)
2230 /// The `<=` operator (less than or equal to)
2232 /// The `!=` operator (not equal to)
2234 /// The `>=` operator (greater than or equal to)
2236 /// The `>` operator (greater than)
2238 /// The `ptr.offset` operator
2243 pub fn is_checkable(self) -> bool {
2245 matches!(self, Add | Sub | Mul | Shl | Shr)
2249 #[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
2251 /// Returns the size of a value of that type
2253 /// Creates a new uninitialized box for a value of that type
2257 #[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
2259 /// The `!` operator for logical inversion
2261 /// The `-` operator for negation
2265 impl<'tcx> Debug for Rvalue<'tcx> {
2266 fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
2267 use self::Rvalue::*;
2270 Use(ref place) => write!(fmt, "{:?}", place),
2271 Repeat(ref a, ref b) => {
2272 write!(fmt, "[{:?}; ", a)?;
2273 pretty_print_const(b, fmt, false)?;
2276 Len(ref a) => write!(fmt, "Len({:?})", a),
2277 Cast(ref kind, ref place, ref ty) => {
2278 write!(fmt, "{:?} as {:?} ({:?})", place, ty, kind)
2280 BinaryOp(ref op, box (ref a, ref b)) => write!(fmt, "{:?}({:?}, {:?})", op, a, b),
2281 CheckedBinaryOp(ref op, box (ref a, ref b)) => {
2282 write!(fmt, "Checked{:?}({:?}, {:?})", op, a, b)
2284 UnaryOp(ref op, ref a) => write!(fmt, "{:?}({:?})", op, a),
2285 Discriminant(ref place) => write!(fmt, "discriminant({:?})", place),
2286 NullaryOp(ref op, ref t) => write!(fmt, "{:?}({:?})", op, t),
2287 ThreadLocalRef(did) => ty::tls::with(|tcx| {
2288 let muta = tcx.static_mutability(did).unwrap().prefix_str();
2289 write!(fmt, "&/*tls*/ {}{}", muta, tcx.def_path_str(did))
2291 Ref(region, borrow_kind, ref place) => {
2292 let kind_str = match borrow_kind {
2293 BorrowKind::Shared => "",
2294 BorrowKind::Shallow => "shallow ",
2295 BorrowKind::Mut { .. } | BorrowKind::Unique => "mut ",
2298 // When printing regions, add trailing space if necessary.
2299 let print_region = ty::tls::with(|tcx| {
2300 tcx.sess.verbose() || tcx.sess.opts.debugging_opts.identify_regions
2302 let region = if print_region {
2303 let mut region = region.to_string();
2304 if !region.is_empty() {
2309 // Do not even print 'static
2312 write!(fmt, "&{}{}{:?}", region, kind_str, place)
2315 AddressOf(mutability, ref place) => {
2316 let kind_str = match mutability {
2317 Mutability::Mut => "mut",
2318 Mutability::Not => "const",
2321 write!(fmt, "&raw {} {:?}", kind_str, place)
2324 Aggregate(ref kind, ref places) => {
2325 let fmt_tuple = |fmt: &mut Formatter<'_>, name: &str| {
2326 let mut tuple_fmt = fmt.debug_tuple(name);
2327 for place in places {
2328 tuple_fmt.field(place);
2334 AggregateKind::Array(_) => write!(fmt, "{:?}", places),
2336 AggregateKind::Tuple => {
2337 if places.is_empty() {
2344 AggregateKind::Adt(adt_def, variant, substs, _user_ty, _) => {
2345 let variant_def = &adt_def.variants[variant];
2347 let name = ty::tls::with(|tcx| {
2348 let mut name = String::new();
2349 let substs = tcx.lift(substs).expect("could not lift for printing");
2350 FmtPrinter::new(tcx, &mut name, Namespace::ValueNS)
2351 .print_def_path(variant_def.def_id, substs)?;
2355 match variant_def.ctor_kind {
2356 CtorKind::Const => fmt.write_str(&name),
2357 CtorKind::Fn => fmt_tuple(fmt, &name),
2358 CtorKind::Fictive => {
2359 let mut struct_fmt = fmt.debug_struct(&name);
2360 for (field, place) in iter::zip(&variant_def.fields, places) {
2361 struct_fmt.field(&field.ident.as_str(), place);
2368 AggregateKind::Closure(def_id, substs) => ty::tls::with(|tcx| {
2369 if let Some(def_id) = def_id.as_local() {
2370 let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
2371 let name = if tcx.sess.opts.debugging_opts.span_free_formats {
2372 let substs = tcx.lift(substs).unwrap();
2375 tcx.def_path_str_with_substs(def_id.to_def_id(), substs),
2378 let span = tcx.hir().span(hir_id);
2381 tcx.sess.source_map().span_to_diagnostic_string(span)
2384 let mut struct_fmt = fmt.debug_struct(&name);
2386 // FIXME(project-rfc-2229#48): This should be a list of capture names/places
2387 if let Some(upvars) = tcx.upvars_mentioned(def_id) {
2388 for (&var_id, place) in iter::zip(upvars.keys(), places) {
2389 let var_name = tcx.hir().name(var_id);
2390 struct_fmt.field(&var_name.as_str(), place);
2396 write!(fmt, "[closure]")
2400 AggregateKind::Generator(def_id, _, _) => ty::tls::with(|tcx| {
2401 if let Some(def_id) = def_id.as_local() {
2402 let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
2403 let name = format!("[generator@{:?}]", tcx.hir().span(hir_id));
2404 let mut struct_fmt = fmt.debug_struct(&name);
2406 // FIXME(project-rfc-2229#48): This should be a list of capture names/places
2407 if let Some(upvars) = tcx.upvars_mentioned(def_id) {
2408 for (&var_id, place) in iter::zip(upvars.keys(), places) {
2409 let var_name = tcx.hir().name(var_id);
2410 struct_fmt.field(&var_name.as_str(), place);
2416 write!(fmt, "[generator]")
2425 ///////////////////////////////////////////////////////////////////////////
2428 /// Two constants are equal if they are the same constant. Note that
2429 /// this does not necessarily mean that they are `==` in Rust. In
2430 /// particular, one must be wary of `NaN`!
2432 #[derive(Clone, Copy, PartialEq, PartialOrd, TyEncodable, TyDecodable, Hash, HashStable)]
2433 pub struct Constant<'tcx> {
2436 /// Optional user-given type: for something like
2437 /// `collect::<Vec<_>>`, this would be present and would
2438 /// indicate that `Vec<_>` was explicitly specified.
2440 /// Needed for NLL to impose user-given type constraints.
2441 pub user_ty: Option<UserTypeAnnotationIndex>,
2443 pub literal: ConstantKind<'tcx>,
2446 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, TyEncodable, TyDecodable, Hash, HashStable, Debug)]
2448 pub enum ConstantKind<'tcx> {
2449 /// This constant came from the type system
2450 Ty(&'tcx ty::Const<'tcx>),
2451 /// This constant cannot go back into the type system, as it represents
2452 /// something the type system cannot handle (e.g. pointers).
2453 Val(interpret::ConstValue<'tcx>, Ty<'tcx>),
2456 impl Constant<'tcx> {
2457 pub fn check_static_ptr(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
2458 match self.literal.const_for_ty()?.val.try_to_scalar() {
2459 Some(Scalar::Ptr(ptr, _size)) => match tcx.global_alloc(ptr.provenance) {
2460 GlobalAlloc::Static(def_id) => {
2461 assert!(!tcx.is_thread_local_static(def_id));
2470 pub fn ty(&self) -> Ty<'tcx> {
2475 impl From<&'tcx ty::Const<'tcx>> for ConstantKind<'tcx> {
2477 fn from(ct: &'tcx ty::Const<'tcx>) -> Self {
2482 impl ConstantKind<'tcx> {
2483 /// Returns `None` if the constant is not trivially safe for use in the type system.
2484 pub fn const_for_ty(&self) -> Option<&'tcx ty::Const<'tcx>> {
2486 ConstantKind::Ty(c) => Some(c),
2487 ConstantKind::Val(..) => None,
2491 pub fn ty(&self) -> Ty<'tcx> {
2493 ConstantKind::Ty(c) => c.ty,
2494 ConstantKind::Val(_, ty) => ty,
2499 pub fn try_to_value(self) -> Option<interpret::ConstValue<'tcx>> {
2501 ConstantKind::Ty(c) => c.val.try_to_value(),
2502 ConstantKind::Val(val, _) => Some(val),
2507 pub fn try_to_scalar(self) -> Option<Scalar> {
2508 self.try_to_value()?.try_to_scalar()
2512 pub fn try_to_scalar_int(self) -> Option<ScalarInt> {
2513 Some(self.try_to_value()?.try_to_scalar()?.assert_int())
2517 pub fn try_to_bits(self, size: Size) -> Option<u128> {
2518 self.try_to_scalar_int()?.to_bits(size).ok()
2522 pub fn try_to_bool(self) -> Option<bool> {
2523 self.try_to_scalar_int()?.try_into().ok()
2527 pub fn try_eval_bits(
2530 param_env: ty::ParamEnv<'tcx>,
2534 Self::Ty(ct) => ct.try_eval_bits(tcx, param_env, ty),
2535 Self::Val(val, t) => {
2538 tcx.layout_of(param_env.with_reveal_all_normalized(tcx).and(ty)).ok()?.size;
2539 val.try_to_bits(size)
2545 pub fn try_eval_bool(&self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Option<bool> {
2547 Self::Ty(ct) => ct.try_eval_bool(tcx, param_env),
2548 Self::Val(val, _) => val.try_to_bool(),
2553 pub fn try_eval_usize(&self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Option<u64> {
2555 Self::Ty(ct) => ct.try_eval_usize(tcx, param_env),
2556 Self::Val(val, _) => val.try_to_machine_usize(tcx),
2561 /// A collection of projections into user types.
2563 /// They are projections because a binding can occur a part of a
2564 /// parent pattern that has been ascribed a type.
2566 /// Its a collection because there can be multiple type ascriptions on
2567 /// the path from the root of the pattern down to the binding itself.
2572 /// struct S<'a>((i32, &'a str), String);
2573 /// let S((_, w): (i32, &'static str), _): S = ...;
2574 /// // ------ ^^^^^^^^^^^^^^^^^^^ (1)
2575 /// // --------------------------------- ^ (2)
2578 /// The highlights labelled `(1)` show the subpattern `(_, w)` being
2579 /// ascribed the type `(i32, &'static str)`.
2581 /// The highlights labelled `(2)` show the whole pattern being
2582 /// ascribed the type `S`.
2584 /// In this example, when we descend to `w`, we will have built up the
2585 /// following two projected types:
2587 /// * base: `S`, projection: `(base.0).1`
2588 /// * base: `(i32, &'static str)`, projection: `base.1`
2590 /// The first will lead to the constraint `w: &'1 str` (for some
2591 /// inferred region `'1`). The second will lead to the constraint `w:
2593 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
2594 pub struct UserTypeProjections {
2595 pub contents: Vec<(UserTypeProjection, Span)>,
2598 impl<'tcx> UserTypeProjections {
2599 pub fn none() -> Self {
2600 UserTypeProjections { contents: vec![] }
2603 pub fn is_empty(&self) -> bool {
2604 self.contents.is_empty()
2607 pub fn projections_and_spans(
2609 ) -> impl Iterator<Item = &(UserTypeProjection, Span)> + ExactSizeIterator {
2610 self.contents.iter()
2613 pub fn projections(&self) -> impl Iterator<Item = &UserTypeProjection> + ExactSizeIterator {
2614 self.contents.iter().map(|&(ref user_type, _span)| user_type)
2617 pub fn push_projection(mut self, user_ty: &UserTypeProjection, span: Span) -> Self {
2618 self.contents.push((user_ty.clone(), span));
2624 mut f: impl FnMut(UserTypeProjection) -> UserTypeProjection,
2626 self.contents = self.contents.drain(..).map(|(proj, span)| (f(proj), span)).collect();
2630 pub fn index(self) -> Self {
2631 self.map_projections(|pat_ty_proj| pat_ty_proj.index())
2634 pub fn subslice(self, from: u64, to: u64) -> Self {
2635 self.map_projections(|pat_ty_proj| pat_ty_proj.subslice(from, to))
2638 pub fn deref(self) -> Self {
2639 self.map_projections(|pat_ty_proj| pat_ty_proj.deref())
2642 pub fn leaf(self, field: Field) -> Self {
2643 self.map_projections(|pat_ty_proj| pat_ty_proj.leaf(field))
2646 pub fn variant(self, adt_def: &'tcx AdtDef, variant_index: VariantIdx, field: Field) -> Self {
2647 self.map_projections(|pat_ty_proj| pat_ty_proj.variant(adt_def, variant_index, field))
2651 /// Encodes the effect of a user-supplied type annotation on the
2652 /// subcomponents of a pattern. The effect is determined by applying the
2653 /// given list of proejctions to some underlying base type. Often,
2654 /// the projection element list `projs` is empty, in which case this
2655 /// directly encodes a type in `base`. But in the case of complex patterns with
2656 /// subpatterns and bindings, we want to apply only a *part* of the type to a variable,
2657 /// in which case the `projs` vector is used.
2661 /// * `let x: T = ...` -- here, the `projs` vector is empty.
2663 /// * `let (x, _): T = ...` -- here, the `projs` vector would contain
2664 /// `field[0]` (aka `.0`), indicating that the type of `s` is
2665 /// determined by finding the type of the `.0` field from `T`.
2666 #[derive(Clone, Debug, TyEncodable, TyDecodable, Hash, HashStable, PartialEq)]
2667 pub struct UserTypeProjection {
2668 pub base: UserTypeAnnotationIndex,
2669 pub projs: Vec<ProjectionKind>,
2672 impl Copy for ProjectionKind {}
2674 impl UserTypeProjection {
2675 pub(crate) fn index(mut self) -> Self {
2676 self.projs.push(ProjectionElem::Index(()));
2680 pub(crate) fn subslice(mut self, from: u64, to: u64) -> Self {
2681 self.projs.push(ProjectionElem::Subslice { from, to, from_end: true });
2685 pub(crate) fn deref(mut self) -> Self {
2686 self.projs.push(ProjectionElem::Deref);
2690 pub(crate) fn leaf(mut self, field: Field) -> Self {
2691 self.projs.push(ProjectionElem::Field(field, ()));
2695 pub(crate) fn variant(
2698 variant_index: VariantIdx,
2701 self.projs.push(ProjectionElem::Downcast(
2702 Some(adt_def.variants[variant_index].ident.name),
2705 self.projs.push(ProjectionElem::Field(field, ()));
2710 TrivialTypeFoldableAndLiftImpls! { ProjectionKind, }
2712 impl<'tcx> TypeFoldable<'tcx> for UserTypeProjection {
2713 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
2714 UserTypeProjection {
2715 base: self.base.fold_with(folder),
2716 projs: self.projs.fold_with(folder),
2720 fn super_visit_with<Vs: TypeVisitor<'tcx>>(
2723 ) -> ControlFlow<Vs::BreakTy> {
2724 self.base.visit_with(visitor)
2725 // Note: there's nothing in `self.proj` to visit.
2729 rustc_index::newtype_index! {
2730 pub struct Promoted {
2732 DEBUG_FORMAT = "promoted[{}]"
2736 impl<'tcx> Debug for Constant<'tcx> {
2737 fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
2738 write!(fmt, "{}", self)
2742 impl<'tcx> Display for Constant<'tcx> {
2743 fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
2744 match self.ty().kind() {
2746 _ => write!(fmt, "const ")?,
2748 Display::fmt(&self.literal, fmt)
2752 impl<'tcx> Display for ConstantKind<'tcx> {
2753 fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
2755 ConstantKind::Ty(c) => pretty_print_const(c, fmt, true),
2756 ConstantKind::Val(val, ty) => pretty_print_const_value(val, ty, fmt, true),
2761 fn pretty_print_const(
2762 c: &ty::Const<'tcx>,
2763 fmt: &mut Formatter<'_>,
2766 use crate::ty::print::PrettyPrinter;
2767 ty::tls::with(|tcx| {
2768 let literal = tcx.lift(c).unwrap();
2769 let mut cx = FmtPrinter::new(tcx, fmt, Namespace::ValueNS);
2770 cx.print_alloc_ids = true;
2771 cx.pretty_print_const(literal, print_types)?;
2776 fn pretty_print_const_value(
2777 val: interpret::ConstValue<'tcx>,
2779 fmt: &mut Formatter<'_>,
2782 use crate::ty::print::PrettyPrinter;
2783 ty::tls::with(|tcx| {
2784 let val = tcx.lift(val).unwrap();
2785 let ty = tcx.lift(ty).unwrap();
2786 let mut cx = FmtPrinter::new(tcx, fmt, Namespace::ValueNS);
2787 cx.print_alloc_ids = true;
2788 cx.pretty_print_const_value(val, ty, print_types)?;
2793 impl<'tcx> graph::DirectedGraph for Body<'tcx> {
2794 type Node = BasicBlock;
2797 impl<'tcx> graph::WithNumNodes for Body<'tcx> {
2799 fn num_nodes(&self) -> usize {
2800 self.basic_blocks.len()
2804 impl<'tcx> graph::WithStartNode for Body<'tcx> {
2806 fn start_node(&self) -> Self::Node {
2811 impl<'tcx> graph::WithSuccessors for Body<'tcx> {
2813 fn successors(&self, node: Self::Node) -> <Self as GraphSuccessors<'_>>::Iter {
2814 self.basic_blocks[node].terminator().successors().cloned()
2818 impl<'a, 'b> graph::GraphSuccessors<'b> for Body<'a> {
2819 type Item = BasicBlock;
2820 type Iter = iter::Cloned<Successors<'b>>;
2823 impl graph::GraphPredecessors<'graph> for Body<'tcx> {
2824 type Item = BasicBlock;
2825 type Iter = std::iter::Copied<std::slice::Iter<'graph, BasicBlock>>;
2828 impl graph::WithPredecessors for Body<'tcx> {
2830 fn predecessors(&self, node: Self::Node) -> <Self as graph::GraphPredecessors<'_>>::Iter {
2831 self.predecessors()[node].iter().copied()
2835 /// `Location` represents the position of the start of the statement; or, if
2836 /// `statement_index` equals the number of statements, then the start of the
2838 #[derive(Copy, Clone, PartialEq, Eq, Hash, Ord, PartialOrd, HashStable)]
2839 pub struct Location {
2840 /// The block that the location is within.
2841 pub block: BasicBlock,
2843 pub statement_index: usize,
2846 impl fmt::Debug for Location {
2847 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2848 write!(fmt, "{:?}[{}]", self.block, self.statement_index)
2853 pub const START: Location = Location { block: START_BLOCK, statement_index: 0 };
2855 /// Returns the location immediately after this one within the enclosing block.
2857 /// Note that if this location represents a terminator, then the
2858 /// resulting location would be out of bounds and invalid.
2859 pub fn successor_within_block(&self) -> Location {
2860 Location { block: self.block, statement_index: self.statement_index + 1 }
2863 /// Returns `true` if `other` is earlier in the control flow graph than `self`.
2864 pub fn is_predecessor_of<'tcx>(&self, other: Location, body: &Body<'tcx>) -> bool {
2865 // If we are in the same block as the other location and are an earlier statement
2866 // then we are a predecessor of `other`.
2867 if self.block == other.block && self.statement_index < other.statement_index {
2871 let predecessors = body.predecessors();
2873 // If we're in another block, then we want to check that block is a predecessor of `other`.
2874 let mut queue: Vec<BasicBlock> = predecessors[other.block].to_vec();
2875 let mut visited = FxHashSet::default();
2877 while let Some(block) = queue.pop() {
2878 // If we haven't visited this block before, then make sure we visit it's predecessors.
2879 if visited.insert(block) {
2880 queue.extend(predecessors[block].iter().cloned());
2885 // If we found the block that `self` is in, then we are a predecessor of `other` (since
2886 // we found that block by looking at the predecessors of `other`).
2887 if self.block == block {
2895 pub fn dominates(&self, other: Location, dominators: &Dominators<BasicBlock>) -> bool {
2896 if self.block == other.block {
2897 self.statement_index <= other.statement_index
2899 dominators.is_dominated_by(other.block, self.block)