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::*;
45 pub mod generic_graphviz;
46 mod graph_cyclic_cache;
57 pub use terminator::*;
62 pub use self::generic_graph::graphviz_safe_def_name;
63 pub use self::graphviz::write_mir_graphviz;
64 pub use self::pretty::{
65 create_dump_file, display_allocation, dump_enabled, dump_mir, write_mir_pretty, PassWhere,
69 pub type LocalDecls<'tcx> = IndexVec<Local, LocalDecl<'tcx>>;
71 pub trait HasLocalDecls<'tcx> {
72 fn local_decls(&self) -> &LocalDecls<'tcx>;
75 impl<'tcx> HasLocalDecls<'tcx> for LocalDecls<'tcx> {
77 fn local_decls(&self) -> &LocalDecls<'tcx> {
82 impl<'tcx> HasLocalDecls<'tcx> for Body<'tcx> {
84 fn local_decls(&self) -> &LocalDecls<'tcx> {
89 /// A streamlined trait that you can implement to create a pass; the
90 /// pass will be named after the type, and it will consist of a main
91 /// loop that goes over each available MIR and applies `run_pass`.
92 pub trait MirPass<'tcx> {
93 fn name(&self) -> Cow<'_, str> {
94 let name = std::any::type_name::<Self>();
95 if let Some(tail) = name.rfind(':') {
96 Cow::from(&name[tail + 1..])
102 fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>);
105 /// The various "big phases" that MIR goes through.
107 /// These phases all describe dialects of MIR. Since all MIR uses the same datastructures, the
108 /// dialects forbid certain variants or values in certain phases.
110 /// Note: Each phase's validation checks all invariants of the *previous* phases' dialects. A phase
111 /// that changes the dialect documents what invariants must be upheld *after* that phase finishes.
113 /// Warning: ordering of variants is significant.
114 #[derive(Copy, Clone, TyEncodable, TyDecodable, Debug, PartialEq, Eq, PartialOrd, Ord)]
115 #[derive(HashStable)]
118 // FIXME(oli-obk): it's unclear whether we still need this phase (and its corresponding query).
119 // We used to have this for pre-miri MIR based const eval.
121 /// This phase checks the MIR for promotable elements and takes them out of the main MIR body
122 /// by creating a new MIR body per promoted element. After this phase (and thus the termination
123 /// of the `mir_promoted` query), these promoted elements are available in the `promoted_mir`
127 /// * the only `AggregateKind`s allowed are `Array` and `Generator`,
128 /// * `DropAndReplace` is gone for good
129 /// * `Drop` now uses explicit drop flags visible in the MIR and reaching a `Drop` terminator
130 /// means that the auto-generated drop glue will be invoked.
132 /// After this phase, generators are explicit state machines (no more `Yield`).
133 /// `AggregateKind::Generator` is gone for good.
134 GeneratorLowering = 4,
139 /// Gets the index of the current MirPhase within the set of all `MirPhase`s.
140 pub fn phase_index(&self) -> usize {
145 /// Where a specific `mir::Body` comes from.
146 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
147 #[derive(HashStable, TyEncodable, TyDecodable, TypeFoldable)]
148 pub struct MirSource<'tcx> {
149 pub instance: InstanceDef<'tcx>,
151 /// If `Some`, this is a promoted rvalue within the parent function.
152 pub promoted: Option<Promoted>,
155 impl<'tcx> MirSource<'tcx> {
156 pub fn item(def_id: DefId) -> Self {
158 instance: InstanceDef::Item(ty::WithOptConstParam::unknown(def_id)),
163 pub fn from_instance(instance: InstanceDef<'tcx>) -> Self {
164 MirSource { instance, promoted: None }
167 pub fn with_opt_param(self) -> ty::WithOptConstParam<DefId> {
168 self.instance.with_opt_param()
172 pub fn def_id(&self) -> DefId {
173 self.instance.def_id()
177 #[derive(Clone, TyEncodable, TyDecodable, Debug, HashStable, TypeFoldable)]
178 pub struct GeneratorInfo<'tcx> {
179 /// The yield type of the function, if it is a generator.
180 pub yield_ty: Option<Ty<'tcx>>,
182 /// Generator drop glue.
183 pub generator_drop: Option<Body<'tcx>>,
185 /// The layout of a generator. Produced by the state transformation.
186 pub generator_layout: Option<GeneratorLayout<'tcx>>,
188 /// If this is a generator then record the type of source expression that caused this generator
190 pub generator_kind: GeneratorKind,
193 /// The lowered representation of a single function.
194 #[derive(Clone, TyEncodable, TyDecodable, Debug, HashStable, TypeFoldable)]
195 pub struct Body<'tcx> {
196 /// A list of basic blocks. References to basic block use a newtyped index type [`BasicBlock`]
197 /// that indexes into this vector.
198 basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
200 /// Records how far through the "desugaring and optimization" process this particular
201 /// MIR has traversed. This is particularly useful when inlining, since in that context
202 /// we instantiate the promoted constants and add them to our promoted vector -- but those
203 /// promoted items have already been optimized, whereas ours have not. This field allows
204 /// us to see the difference and forego optimization on the inlined promoted items.
207 pub source: MirSource<'tcx>,
209 /// A list of source scopes; these are referenced by statements
210 /// and used for debuginfo. Indexed by a `SourceScope`.
211 pub source_scopes: IndexVec<SourceScope, SourceScopeData<'tcx>>,
213 pub generator: Option<Box<GeneratorInfo<'tcx>>>,
215 /// Declarations of locals.
217 /// The first local is the return value pointer, followed by `arg_count`
218 /// locals for the function arguments, followed by any user-declared
219 /// variables and temporaries.
220 pub local_decls: LocalDecls<'tcx>,
222 /// User type annotations.
223 pub user_type_annotations: ty::CanonicalUserTypeAnnotations<'tcx>,
225 /// The number of arguments this function takes.
227 /// Starting at local 1, `arg_count` locals will be provided by the caller
228 /// and can be assumed to be initialized.
230 /// If this MIR was built for a constant, this will be 0.
231 pub arg_count: usize,
233 /// Mark an argument local (which must be a tuple) as getting passed as
234 /// its individual components at the LLVM level.
236 /// This is used for the "rust-call" ABI.
237 pub spread_arg: Option<Local>,
239 /// Debug information pertaining to user variables, including captures.
240 pub var_debug_info: Vec<VarDebugInfo<'tcx>>,
242 /// A span representing this MIR, for error reporting.
245 /// Constants that are required to evaluate successfully for this MIR to be well-formed.
246 /// We hold in this field all the constants we are not able to evaluate yet.
247 pub required_consts: Vec<Constant<'tcx>>,
249 /// Does this body use generic parameters. This is used for the `ConstEvaluatable` check.
251 /// Note that this does not actually mean that this body is not computable right now.
252 /// The repeat count in the following example is polymorphic, but can still be evaluated
253 /// without knowing anything about the type parameter `T`.
257 /// let _ = [0; std::mem::size_of::<*mut T>()];
261 /// **WARNING**: Do not change this flags after the MIR was originally created, even if an optimization
262 /// removed the last mention of all generic params. We do not want to rely on optimizations and
263 /// potentially allow things like `[u8; std::mem::size_of::<T>() * 0]` due to this.
264 pub is_polymorphic: bool,
266 predecessor_cache: PredecessorCache,
267 is_cyclic: GraphIsCyclicCache,
270 impl<'tcx> Body<'tcx> {
273 source: MirSource<'tcx>,
274 basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
275 source_scopes: IndexVec<SourceScope, SourceScopeData<'tcx>>,
276 local_decls: LocalDecls<'tcx>,
277 user_type_annotations: ty::CanonicalUserTypeAnnotations<'tcx>,
279 var_debug_info: Vec<VarDebugInfo<'tcx>>,
281 generator_kind: Option<GeneratorKind>,
283 // We need `arg_count` locals, and one for the return place.
285 local_decls.len() > arg_count,
286 "expected at least {} locals, got {}",
291 let mut body = Body {
292 phase: MirPhase::Build,
296 generator: generator_kind.map(|generator_kind| {
297 Box::new(GeneratorInfo {
299 generator_drop: None,
300 generator_layout: None,
305 user_type_annotations,
310 required_consts: Vec::new(),
311 is_polymorphic: false,
312 predecessor_cache: PredecessorCache::new(),
313 is_cyclic: GraphIsCyclicCache::new(),
315 body.is_polymorphic = body.definitely_has_param_types_or_consts(tcx);
319 /// Returns a partially initialized MIR body containing only a list of basic blocks.
321 /// The returned MIR contains no `LocalDecl`s (even for the return place) or source scopes. It
322 /// is only useful for testing but cannot be `#[cfg(test)]` because it is used in a different
324 pub fn new_cfg_only(basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>) -> Self {
326 phase: MirPhase::Build,
327 source: MirSource::item(DefId::local(CRATE_DEF_INDEX)),
329 source_scopes: IndexVec::new(),
331 local_decls: IndexVec::new(),
332 user_type_annotations: IndexVec::new(),
336 required_consts: Vec::new(),
337 var_debug_info: Vec::new(),
338 is_polymorphic: false,
339 predecessor_cache: PredecessorCache::new(),
340 is_cyclic: GraphIsCyclicCache::new(),
345 pub fn basic_blocks(&self) -> &IndexVec<BasicBlock, BasicBlockData<'tcx>> {
350 pub fn basic_blocks_mut(&mut self) -> &mut IndexVec<BasicBlock, BasicBlockData<'tcx>> {
351 // Because the user could mutate basic block terminators via this reference, we need to
352 // invalidate the caches.
354 // FIXME: Use a finer-grained API for this, so only transformations that alter terminators
355 // invalidate the caches.
356 self.predecessor_cache.invalidate();
357 self.is_cyclic.invalidate();
358 &mut self.basic_blocks
362 pub fn basic_blocks_and_local_decls_mut(
364 ) -> (&mut IndexVec<BasicBlock, BasicBlockData<'tcx>>, &mut LocalDecls<'tcx>) {
365 self.predecessor_cache.invalidate();
366 self.is_cyclic.invalidate();
367 (&mut self.basic_blocks, &mut self.local_decls)
371 pub fn basic_blocks_local_decls_mut_and_var_debug_info(
374 &mut IndexVec<BasicBlock, BasicBlockData<'tcx>>,
375 &mut LocalDecls<'tcx>,
376 &mut Vec<VarDebugInfo<'tcx>>,
378 self.predecessor_cache.invalidate();
379 self.is_cyclic.invalidate();
380 (&mut self.basic_blocks, &mut self.local_decls, &mut self.var_debug_info)
383 /// Returns `true` if a cycle exists in the control-flow graph that is reachable from the
385 pub fn is_cfg_cyclic(&self) -> bool {
386 self.is_cyclic.is_cyclic(self)
390 pub fn local_kind(&self, local: Local) -> LocalKind {
391 let index = local.as_usize();
394 self.local_decls[local].mutability == Mutability::Mut,
395 "return place should be mutable"
398 LocalKind::ReturnPointer
399 } else if index < self.arg_count + 1 {
401 } else if self.local_decls[local].is_user_variable() {
408 /// Returns an iterator over all user-declared mutable locals.
410 pub fn mut_vars_iter<'a>(&'a self) -> impl Iterator<Item = Local> + 'a {
411 (self.arg_count + 1..self.local_decls.len()).filter_map(move |index| {
412 let local = Local::new(index);
413 let decl = &self.local_decls[local];
414 if decl.is_user_variable() && decl.mutability == Mutability::Mut {
422 /// Returns an iterator over all user-declared mutable arguments and locals.
424 pub fn mut_vars_and_args_iter<'a>(&'a self) -> impl Iterator<Item = Local> + 'a {
425 (1..self.local_decls.len()).filter_map(move |index| {
426 let local = Local::new(index);
427 let decl = &self.local_decls[local];
428 if (decl.is_user_variable() || index < self.arg_count + 1)
429 && decl.mutability == Mutability::Mut
438 /// Returns an iterator over all function arguments.
440 pub fn args_iter(&self) -> impl Iterator<Item = Local> + ExactSizeIterator {
441 (1..self.arg_count + 1).map(Local::new)
444 /// Returns an iterator over all user-defined variables and compiler-generated temporaries (all
445 /// locals that are neither arguments nor the return place).
447 pub fn vars_and_temps_iter(
449 ) -> impl DoubleEndedIterator<Item = Local> + ExactSizeIterator {
450 (self.arg_count + 1..self.local_decls.len()).map(Local::new)
454 pub fn drain_vars_and_temps<'a>(&'a mut self) -> impl Iterator<Item = LocalDecl<'tcx>> + 'a {
455 self.local_decls.drain(self.arg_count + 1..)
458 /// Changes a statement to a nop. This is both faster than deleting instructions and avoids
459 /// invalidating statement indices in `Location`s.
460 pub fn make_statement_nop(&mut self, location: Location) {
461 let block = &mut self.basic_blocks[location.block];
462 debug_assert!(location.statement_index < block.statements.len());
463 block.statements[location.statement_index].make_nop()
466 /// Returns the source info associated with `location`.
467 pub fn source_info(&self, location: Location) -> &SourceInfo {
468 let block = &self[location.block];
469 let stmts = &block.statements;
470 let idx = location.statement_index;
471 if idx < stmts.len() {
472 &stmts[idx].source_info
474 assert_eq!(idx, stmts.len());
475 &block.terminator().source_info
479 /// Returns the return type; it always return first element from `local_decls` array.
481 pub fn return_ty(&self) -> Ty<'tcx> {
482 self.local_decls[RETURN_PLACE].ty
485 /// Gets the location of the terminator for the given block.
487 pub fn terminator_loc(&self, bb: BasicBlock) -> Location {
488 Location { block: bb, statement_index: self[bb].statements.len() }
492 pub fn predecessors(&self) -> &Predecessors {
493 self.predecessor_cache.compute(&self.basic_blocks)
497 pub fn dominators(&self) -> Dominators<BasicBlock> {
502 pub fn yield_ty(&self) -> Option<Ty<'tcx>> {
503 self.generator.as_ref().and_then(|generator| generator.yield_ty)
507 pub fn generator_layout(&self) -> Option<&GeneratorLayout<'tcx>> {
508 self.generator.as_ref().and_then(|generator| generator.generator_layout.as_ref())
512 pub fn generator_drop(&self) -> Option<&Body<'tcx>> {
513 self.generator.as_ref().and_then(|generator| generator.generator_drop.as_ref())
517 pub fn generator_kind(&self) -> Option<GeneratorKind> {
518 self.generator.as_ref().map(|generator| generator.generator_kind)
522 #[derive(Copy, Clone, PartialEq, Eq, Debug, TyEncodable, TyDecodable, HashStable)]
525 /// Unsafe because of compiler-generated unsafe code, like `await` desugaring
527 /// Unsafe because of an unsafe fn
529 /// Unsafe because of an `unsafe` block
530 ExplicitUnsafe(hir::HirId),
533 impl<'tcx> Index<BasicBlock> for Body<'tcx> {
534 type Output = BasicBlockData<'tcx>;
537 fn index(&self, index: BasicBlock) -> &BasicBlockData<'tcx> {
538 &self.basic_blocks()[index]
542 impl<'tcx> IndexMut<BasicBlock> for Body<'tcx> {
544 fn index_mut(&mut self, index: BasicBlock) -> &mut BasicBlockData<'tcx> {
545 &mut self.basic_blocks_mut()[index]
549 #[derive(Copy, Clone, Debug, HashStable, TypeFoldable)]
550 pub enum ClearCrossCrate<T> {
555 impl<T> ClearCrossCrate<T> {
556 pub fn as_ref(&self) -> ClearCrossCrate<&T> {
558 ClearCrossCrate::Clear => ClearCrossCrate::Clear,
559 ClearCrossCrate::Set(v) => ClearCrossCrate::Set(v),
563 pub fn assert_crate_local(self) -> T {
565 ClearCrossCrate::Clear => bug!("unwrapping cross-crate data"),
566 ClearCrossCrate::Set(v) => v,
571 const TAG_CLEAR_CROSS_CRATE_CLEAR: u8 = 0;
572 const TAG_CLEAR_CROSS_CRATE_SET: u8 = 1;
574 impl<'tcx, E: TyEncoder<'tcx>, T: Encodable<E>> Encodable<E> for ClearCrossCrate<T> {
576 fn encode(&self, e: &mut E) -> Result<(), E::Error> {
577 if E::CLEAR_CROSS_CRATE {
582 ClearCrossCrate::Clear => TAG_CLEAR_CROSS_CRATE_CLEAR.encode(e),
583 ClearCrossCrate::Set(ref val) => {
584 TAG_CLEAR_CROSS_CRATE_SET.encode(e)?;
590 impl<'tcx, D: TyDecoder<'tcx>, T: Decodable<D>> Decodable<D> for ClearCrossCrate<T> {
592 fn decode(d: &mut D) -> Result<ClearCrossCrate<T>, D::Error> {
593 if D::CLEAR_CROSS_CRATE {
594 return Ok(ClearCrossCrate::Clear);
597 let discr = u8::decode(d)?;
600 TAG_CLEAR_CROSS_CRATE_CLEAR => Ok(ClearCrossCrate::Clear),
601 TAG_CLEAR_CROSS_CRATE_SET => {
602 let val = T::decode(d)?;
603 Ok(ClearCrossCrate::Set(val))
605 tag => Err(d.error(&format!("Invalid tag for ClearCrossCrate: {:?}", tag))),
610 /// Grouped information about the source code origin of a MIR entity.
611 /// Intended to be inspected by diagnostics and debuginfo.
612 /// Most passes can work with it as a whole, within a single function.
613 // The unofficial Cranelift backend, at least as of #65828, needs `SourceInfo` to implement `Eq` and
614 // `Hash`. Please ping @bjorn3 if removing them.
615 #[derive(Copy, Clone, Debug, Eq, PartialEq, TyEncodable, TyDecodable, Hash, HashStable)]
616 pub struct SourceInfo {
617 /// The source span for the AST pertaining to this MIR entity.
620 /// The source scope, keeping track of which bindings can be
621 /// seen by debuginfo, active lint levels, `unsafe {...}`, etc.
622 pub scope: SourceScope,
627 pub fn outermost(span: Span) -> Self {
628 SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE }
632 ///////////////////////////////////////////////////////////////////////////
635 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, TyEncodable, TyDecodable)]
636 #[derive(Hash, HashStable)]
637 pub enum BorrowKind {
638 /// Data must be immutable and is aliasable.
641 /// The immediately borrowed place must be immutable, but projections from
642 /// it don't need to be. For example, a shallow borrow of `a.b` doesn't
643 /// conflict with a mutable borrow of `a.b.c`.
645 /// This is used when lowering matches: when matching on a place we want to
646 /// ensure that place have the same value from the start of the match until
647 /// an arm is selected. This prevents this code from compiling:
649 /// let mut x = &Some(0);
652 /// Some(_) if { x = &None; false } => (),
656 /// This can't be a shared borrow because mutably borrowing (*x as Some).0
657 /// should not prevent `if let None = x { ... }`, for example, because the
658 /// mutating `(*x as Some).0` can't affect the discriminant of `x`.
659 /// We can also report errors with this kind of borrow differently.
662 /// Data must be immutable but not aliasable. This kind of borrow
663 /// cannot currently be expressed by the user and is used only in
664 /// implicit closure bindings. It is needed when the closure is
665 /// borrowing or mutating a mutable referent, e.g.:
667 /// let x: &mut isize = ...;
668 /// let y = || *x += 5;
670 /// If we were to try to translate this closure into a more explicit
671 /// form, we'd encounter an error with the code as written:
673 /// struct Env { x: & &mut isize }
674 /// let x: &mut isize = ...;
675 /// let y = (&mut Env { &x }, fn_ptr); // Closure is pair of env and fn
676 /// fn fn_ptr(env: &mut Env) { **env.x += 5; }
678 /// This is then illegal because you cannot mutate an `&mut` found
679 /// in an aliasable location. To solve, you'd have to translate with
680 /// an `&mut` borrow:
682 /// struct Env { x: &mut &mut isize }
683 /// let x: &mut isize = ...;
684 /// let y = (&mut Env { &mut x }, fn_ptr); // changed from &x to &mut x
685 /// fn fn_ptr(env: &mut Env) { **env.x += 5; }
687 /// Now the assignment to `**env.x` is legal, but creating a
688 /// mutable pointer to `x` is not because `x` is not mutable. We
689 /// could fix this by declaring `x` as `let mut x`. This is ok in
690 /// user code, if awkward, but extra weird for closures, since the
691 /// borrow is hidden.
693 /// So we introduce a "unique imm" borrow -- the referent is
694 /// immutable, but not aliasable. This solves the problem. For
695 /// simplicity, we don't give users the way to express this
696 /// borrow, it's just used when translating closures.
699 /// Data is mutable and not aliasable.
701 /// `true` if this borrow arose from method-call auto-ref
702 /// (i.e., `adjustment::Adjust::Borrow`).
703 allow_two_phase_borrow: bool,
708 pub fn allows_two_phase_borrow(&self) -> bool {
710 BorrowKind::Shared | BorrowKind::Shallow | BorrowKind::Unique => false,
711 BorrowKind::Mut { allow_two_phase_borrow } => allow_two_phase_borrow,
715 pub fn describe_mutability(&self) -> String {
717 BorrowKind::Shared | BorrowKind::Shallow | BorrowKind::Unique => {
718 "immutable".to_string()
720 BorrowKind::Mut { .. } => "mutable".to_string(),
725 ///////////////////////////////////////////////////////////////////////////
726 // Variables and temps
728 rustc_index::newtype_index! {
731 DEBUG_FORMAT = "_{}",
732 const RETURN_PLACE = 0,
736 impl Atom for Local {
737 fn index(self) -> usize {
742 /// Classifies locals into categories. See `Body::local_kind`.
743 #[derive(Clone, Copy, PartialEq, Eq, Debug, HashStable)]
745 /// User-declared variable binding.
747 /// Compiler-introduced temporary.
749 /// Function argument.
751 /// Location of function's return value.
755 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
756 pub struct VarBindingForm<'tcx> {
757 /// Is variable bound via `x`, `mut x`, `ref x`, or `ref mut x`?
758 pub binding_mode: ty::BindingMode,
759 /// If an explicit type was provided for this variable binding,
760 /// this holds the source Span of that type.
762 /// NOTE: if you want to change this to a `HirId`, be wary that
763 /// doing so breaks incremental compilation (as of this writing),
764 /// while a `Span` does not cause our tests to fail.
765 pub opt_ty_info: Option<Span>,
766 /// Place of the RHS of the =, or the subject of the `match` where this
767 /// variable is initialized. None in the case of `let PATTERN;`.
768 /// Some((None, ..)) in the case of and `let [mut] x = ...` because
769 /// (a) the right-hand side isn't evaluated as a place expression.
770 /// (b) it gives a way to separate this case from the remaining cases
772 pub opt_match_place: Option<(Option<Place<'tcx>>, Span)>,
773 /// The span of the pattern in which this variable was bound.
777 #[derive(Clone, Debug, TyEncodable, TyDecodable)]
778 pub enum BindingForm<'tcx> {
779 /// This is a binding for a non-`self` binding, or a `self` that has an explicit type.
780 Var(VarBindingForm<'tcx>),
781 /// Binding for a `self`/`&self`/`&mut self` binding where the type is implicit.
782 ImplicitSelf(ImplicitSelfKind),
783 /// Reference used in a guard expression to ensure immutability.
787 /// Represents what type of implicit self a function has, if any.
788 #[derive(Clone, Copy, PartialEq, Debug, TyEncodable, TyDecodable, HashStable)]
789 pub enum ImplicitSelfKind {
790 /// Represents a `fn x(self);`.
792 /// Represents a `fn x(mut self);`.
794 /// Represents a `fn x(&self);`.
796 /// Represents a `fn x(&mut self);`.
798 /// Represents when a function does not have a self argument or
799 /// when a function has a `self: X` argument.
803 TrivialTypeFoldableAndLiftImpls! { BindingForm<'tcx>, }
805 mod binding_form_impl {
806 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
807 use rustc_query_system::ich::StableHashingContext;
809 impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for super::BindingForm<'tcx> {
810 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
811 use super::BindingForm::*;
812 std::mem::discriminant(self).hash_stable(hcx, hasher);
815 Var(binding) => binding.hash_stable(hcx, hasher),
816 ImplicitSelf(kind) => kind.hash_stable(hcx, hasher),
823 /// `BlockTailInfo` is attached to the `LocalDecl` for temporaries
824 /// created during evaluation of expressions in a block tail
825 /// expression; that is, a block like `{ STMT_1; STMT_2; EXPR }`.
827 /// It is used to improve diagnostics when such temporaries are
828 /// involved in borrow_check errors, e.g., explanations of where the
829 /// temporaries come from, when their destructors are run, and/or how
830 /// one might revise the code to satisfy the borrow checker's rules.
831 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
832 pub struct BlockTailInfo {
833 /// If `true`, then the value resulting from evaluating this tail
834 /// expression is ignored by the block's expression context.
836 /// Examples include `{ ...; tail };` and `let _ = { ...; tail };`
837 /// but not e.g., `let _x = { ...; tail };`
838 pub tail_result_is_ignored: bool,
840 /// `Span` of the tail expression.
846 /// This can be a binding declared by the user, a temporary inserted by the compiler, a function
847 /// argument, or the return place.
848 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
849 pub struct LocalDecl<'tcx> {
850 /// Whether this is a mutable binding (i.e., `let x` or `let mut x`).
852 /// Temporaries and the return place are always mutable.
853 pub mutability: Mutability,
855 // FIXME(matthewjasper) Don't store in this in `Body`
856 pub local_info: Option<Box<LocalInfo<'tcx>>>,
858 /// `true` if this is an internal local.
860 /// These locals are not based on types in the source code and are only used
861 /// for a few desugarings at the moment.
863 /// The generator transformation will sanity check the locals which are live
864 /// across a suspension point against the type components of the generator
865 /// which type checking knows are live across a suspension point. We need to
866 /// flag drop flags to avoid triggering this check as they are introduced
869 /// This should be sound because the drop flags are fully algebraic, and
870 /// therefore don't affect the auto-trait or outlives properties of the
874 /// If this local is a temporary and `is_block_tail` is `Some`,
875 /// then it is a temporary created for evaluation of some
876 /// subexpression of some block's tail expression (with no
877 /// intervening statement context).
878 // FIXME(matthewjasper) Don't store in this in `Body`
879 pub is_block_tail: Option<BlockTailInfo>,
881 /// The type of this local.
884 /// If the user manually ascribed a type to this variable,
885 /// e.g., via `let x: T`, then we carry that type here. The MIR
886 /// borrow checker needs this information since it can affect
887 /// region inference.
888 // FIXME(matthewjasper) Don't store in this in `Body`
889 pub user_ty: Option<Box<UserTypeProjections>>,
891 /// The *syntactic* (i.e., not visibility) source scope the local is defined
892 /// in. If the local was defined in a let-statement, this
893 /// is *within* the let-statement, rather than outside
896 /// This is needed because the visibility source scope of locals within
897 /// a let-statement is weird.
899 /// The reason is that we want the local to be *within* the let-statement
900 /// for lint purposes, but we want the local to be *after* the let-statement
901 /// for names-in-scope purposes.
903 /// That's it, if we have a let-statement like the one in this
907 /// fn foo(x: &str) {
908 /// #[allow(unused_mut)]
909 /// let mut x: u32 = { // <- one unused mut
910 /// let mut y: u32 = x.parse().unwrap();
917 /// Then, from a lint point of view, the declaration of `x: u32`
918 /// (and `y: u32`) are within the `#[allow(unused_mut)]` scope - the
919 /// lint scopes are the same as the AST/HIR nesting.
921 /// However, from a name lookup point of view, the scopes look more like
922 /// as if the let-statements were `match` expressions:
925 /// fn foo(x: &str) {
927 /// match x.parse().unwrap() {
936 /// We care about the name-lookup scopes for debuginfo - if the
937 /// debuginfo instruction pointer is at the call to `x.parse()`, we
938 /// want `x` to refer to `x: &str`, but if it is at the call to
939 /// `drop(x)`, we want it to refer to `x: u32`.
941 /// To allow both uses to work, we need to have more than a single scope
942 /// for a local. We have the `source_info.scope` represent the "syntactic"
943 /// lint scope (with a variable being under its let block) while the
944 /// `var_debug_info.source_info.scope` represents the "local variable"
945 /// scope (where the "rest" of a block is under all prior let-statements).
947 /// The end result looks like this:
951 /// │{ argument x: &str }
953 /// │ │{ #[allow(unused_mut)] } // This is actually split into 2 scopes
954 /// │ │ // in practice because I'm lazy.
956 /// │ │← x.source_info.scope
957 /// │ │← `x.parse().unwrap()`
959 /// │ │ │← y.source_info.scope
961 /// │ │ │{ let y: u32 }
963 /// │ │ │← y.var_debug_info.source_info.scope
966 /// │ │{ let x: u32 }
967 /// │ │← x.var_debug_info.source_info.scope
968 /// │ │← `drop(x)` // This accesses `x: u32`.
970 pub source_info: SourceInfo,
973 // `LocalDecl` is used a lot. Make sure it doesn't unintentionally get bigger.
974 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
975 static_assert_size!(LocalDecl<'_>, 56);
977 /// Extra information about a some locals that's used for diagnostics and for
978 /// classifying variables into local variables, statics, etc, which is needed e.g.
979 /// for unsafety checking.
981 /// Not used for non-StaticRef temporaries, the return place, or anonymous
982 /// function parameters.
983 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
984 pub enum LocalInfo<'tcx> {
985 /// A user-defined local variable or function parameter
987 /// The `BindingForm` is solely used for local diagnostics when generating
988 /// warnings/errors when compiling the current crate, and therefore it need
989 /// not be visible across crates.
990 User(ClearCrossCrate<BindingForm<'tcx>>),
991 /// A temporary created that references the static with the given `DefId`.
992 StaticRef { def_id: DefId, is_thread_local: bool },
993 /// A temporary created that references the const with the given `DefId`
994 ConstRef { def_id: DefId },
995 /// A temporary created during the creation of an aggregate
996 /// (e.g. a temporary for `foo` in `MyStruct { my_field: foo }`)
1000 impl<'tcx> LocalDecl<'tcx> {
1001 /// Returns `true` only if local is a binding that can itself be
1002 /// made mutable via the addition of the `mut` keyword, namely
1003 /// something like the occurrences of `x` in:
1004 /// - `fn foo(x: Type) { ... }`,
1005 /// - `let x = ...`,
1006 /// - or `match ... { C(x) => ... }`
1007 pub fn can_be_made_mutable(&self) -> bool {
1010 Some(box LocalInfo::User(ClearCrossCrate::Set(
1011 BindingForm::Var(VarBindingForm {
1012 binding_mode: ty::BindingMode::BindByValue(_),
1016 }) | BindingForm::ImplicitSelf(ImplicitSelfKind::Imm),
1021 /// Returns `true` if local is definitely not a `ref ident` or
1022 /// `ref mut ident` binding. (Such bindings cannot be made into
1023 /// mutable bindings, but the inverse does not necessarily hold).
1024 pub fn is_nonref_binding(&self) -> bool {
1027 Some(box LocalInfo::User(ClearCrossCrate::Set(
1028 BindingForm::Var(VarBindingForm {
1029 binding_mode: ty::BindingMode::BindByValue(_),
1033 }) | BindingForm::ImplicitSelf(_),
1038 /// Returns `true` if this variable is a named variable or function
1039 /// parameter declared by the user.
1041 pub fn is_user_variable(&self) -> bool {
1042 matches!(self.local_info, Some(box LocalInfo::User(_)))
1045 /// Returns `true` if this is a reference to a variable bound in a `match`
1046 /// expression that is used to access said variable for the guard of the
1048 pub fn is_ref_for_guard(&self) -> bool {
1051 Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::RefForGuard)))
1055 /// Returns `Some` if this is a reference to a static item that is used to
1056 /// access that static.
1057 pub fn is_ref_to_static(&self) -> bool {
1058 matches!(self.local_info, Some(box LocalInfo::StaticRef { .. }))
1061 /// Returns `Some` if this is a reference to a thread-local static item that is used to
1062 /// access that static.
1063 pub fn is_ref_to_thread_local(&self) -> bool {
1064 match self.local_info {
1065 Some(box LocalInfo::StaticRef { is_thread_local, .. }) => is_thread_local,
1070 /// Returns `true` is the local is from a compiler desugaring, e.g.,
1071 /// `__next` from a `for` loop.
1073 pub fn from_compiler_desugaring(&self) -> bool {
1074 self.source_info.span.desugaring_kind().is_some()
1077 /// Creates a new `LocalDecl` for a temporary: mutable, non-internal.
1079 pub fn new(ty: Ty<'tcx>, span: Span) -> Self {
1080 Self::with_source_info(ty, SourceInfo::outermost(span))
1083 /// Like `LocalDecl::new`, but takes a `SourceInfo` instead of a `Span`.
1085 pub fn with_source_info(ty: Ty<'tcx>, source_info: SourceInfo) -> Self {
1087 mutability: Mutability::Mut,
1090 is_block_tail: None,
1097 /// Converts `self` into same `LocalDecl` except tagged as internal.
1099 pub fn internal(mut self) -> Self {
1100 self.internal = true;
1104 /// Converts `self` into same `LocalDecl` except tagged as immutable.
1106 pub fn immutable(mut self) -> Self {
1107 self.mutability = Mutability::Not;
1111 /// Converts `self` into same `LocalDecl` except tagged as internal temporary.
1113 pub fn block_tail(mut self, info: BlockTailInfo) -> Self {
1114 assert!(self.is_block_tail.is_none());
1115 self.is_block_tail = Some(info);
1120 #[derive(Clone, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
1121 pub enum VarDebugInfoContents<'tcx> {
1122 /// NOTE(eddyb) There's an unenforced invariant that this `Place` is
1123 /// based on a `Local`, not a `Static`, and contains no indexing.
1125 Const(Constant<'tcx>),
1128 impl<'tcx> Debug for VarDebugInfoContents<'tcx> {
1129 fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
1131 VarDebugInfoContents::Const(c) => write!(fmt, "{}", c),
1132 VarDebugInfoContents::Place(p) => write!(fmt, "{:?}", p),
1137 /// Debug information pertaining to a user variable.
1138 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
1139 pub struct VarDebugInfo<'tcx> {
1142 /// Source info of the user variable, including the scope
1143 /// within which the variable is visible (to debuginfo)
1144 /// (see `LocalDecl`'s `source_info` field for more details).
1145 pub source_info: SourceInfo,
1147 /// Where the data for this user variable is to be found.
1148 pub value: VarDebugInfoContents<'tcx>,
1151 ///////////////////////////////////////////////////////////////////////////
1154 rustc_index::newtype_index! {
1155 /// A node in the MIR [control-flow graph][CFG].
1157 /// There are no branches (e.g., `if`s, function calls, etc.) within a basic block, which makes
1158 /// it easier to do [data-flow analyses] and optimizations. Instead, branches are represented
1159 /// as an edge in a graph between basic blocks.
1161 /// Basic blocks consist of a series of [statements][Statement], ending with a
1162 /// [terminator][Terminator]. Basic blocks can have multiple predecessors and successors,
1163 /// however there is a MIR pass ([`CriticalCallEdges`]) that removes *critical edges*, which
1164 /// are edges that go from a multi-successor node to a multi-predecessor node. This pass is
1165 /// needed because some analyses require that there are no critical edges in the CFG.
1167 /// Note that this type is just an index into [`Body.basic_blocks`](Body::basic_blocks);
1168 /// the actual data that a basic block holds is in [`BasicBlockData`].
1170 /// Read more about basic blocks in the [rustc-dev-guide][guide-mir].
1172 /// [CFG]: https://rustc-dev-guide.rust-lang.org/appendix/background.html#cfg
1173 /// [data-flow analyses]:
1174 /// https://rustc-dev-guide.rust-lang.org/appendix/background.html#what-is-a-dataflow-analysis
1175 /// [`CriticalCallEdges`]: ../../rustc_const_eval/transform/add_call_guards/enum.AddCallGuards.html#variant.CriticalCallEdges
1176 /// [guide-mir]: https://rustc-dev-guide.rust-lang.org/mir/
1177 pub struct BasicBlock {
1179 DEBUG_FORMAT = "bb{}",
1180 const START_BLOCK = 0,
1185 pub fn start_location(self) -> Location {
1186 Location { block: self, statement_index: 0 }
1190 ///////////////////////////////////////////////////////////////////////////
1191 // BasicBlockData and Terminator
1193 /// See [`BasicBlock`] for documentation on what basic blocks are at a high level.
1194 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
1195 pub struct BasicBlockData<'tcx> {
1196 /// List of statements in this block.
1197 pub statements: Vec<Statement<'tcx>>,
1199 /// Terminator for this block.
1201 /// N.B., this should generally ONLY be `None` during construction.
1202 /// Therefore, you should generally access it via the
1203 /// `terminator()` or `terminator_mut()` methods. The only
1204 /// exception is that certain passes, such as `simplify_cfg`, swap
1205 /// out the terminator temporarily with `None` while they continue
1206 /// to recurse over the set of basic blocks.
1207 pub terminator: Option<Terminator<'tcx>>,
1209 /// If true, this block lies on an unwind path. This is used
1210 /// during codegen where distinct kinds of basic blocks may be
1211 /// generated (particularly for MSVC cleanup). Unwind blocks must
1212 /// only branch to other unwind blocks.
1213 pub is_cleanup: bool,
1216 /// Information about an assertion failure.
1217 #[derive(Clone, TyEncodable, TyDecodable, Hash, HashStable, PartialEq, PartialOrd)]
1218 pub enum AssertKind<O> {
1219 BoundsCheck { len: O, index: O },
1220 Overflow(BinOp, O, O),
1224 ResumedAfterReturn(GeneratorKind),
1225 ResumedAfterPanic(GeneratorKind),
1239 pub enum InlineAsmOperand<'tcx> {
1241 reg: InlineAsmRegOrRegClass,
1242 value: Operand<'tcx>,
1245 reg: InlineAsmRegOrRegClass,
1247 place: Option<Place<'tcx>>,
1250 reg: InlineAsmRegOrRegClass,
1252 in_value: Operand<'tcx>,
1253 out_place: Option<Place<'tcx>>,
1256 value: Box<Constant<'tcx>>,
1259 value: Box<Constant<'tcx>>,
1266 /// Type for MIR `Assert` terminator error messages.
1267 pub type AssertMessage<'tcx> = AssertKind<Operand<'tcx>>;
1269 pub type Successors<'a> =
1270 iter::Chain<option::IntoIter<&'a BasicBlock>, slice::Iter<'a, BasicBlock>>;
1271 pub type SuccessorsMut<'a> =
1272 iter::Chain<option::IntoIter<&'a mut BasicBlock>, slice::IterMut<'a, BasicBlock>>;
1274 impl<'tcx> BasicBlockData<'tcx> {
1275 pub fn new(terminator: Option<Terminator<'tcx>>) -> BasicBlockData<'tcx> {
1276 BasicBlockData { statements: vec![], terminator, is_cleanup: false }
1279 /// Accessor for terminator.
1281 /// Terminator may not be None after construction of the basic block is complete. This accessor
1282 /// provides a convenience way to reach the terminator.
1284 pub fn terminator(&self) -> &Terminator<'tcx> {
1285 self.terminator.as_ref().expect("invalid terminator state")
1289 pub fn terminator_mut(&mut self) -> &mut Terminator<'tcx> {
1290 self.terminator.as_mut().expect("invalid terminator state")
1293 pub fn retain_statements<F>(&mut self, mut f: F)
1295 F: FnMut(&mut Statement<'_>) -> bool,
1297 for s in &mut self.statements {
1304 pub fn expand_statements<F, I>(&mut self, mut f: F)
1306 F: FnMut(&mut Statement<'tcx>) -> Option<I>,
1307 I: iter::TrustedLen<Item = Statement<'tcx>>,
1309 // Gather all the iterators we'll need to splice in, and their positions.
1310 let mut splices: Vec<(usize, I)> = vec![];
1311 let mut extra_stmts = 0;
1312 for (i, s) in self.statements.iter_mut().enumerate() {
1313 if let Some(mut new_stmts) = f(s) {
1314 if let Some(first) = new_stmts.next() {
1315 // We can already store the first new statement.
1318 // Save the other statements for optimized splicing.
1319 let remaining = new_stmts.size_hint().0;
1321 splices.push((i + 1 + extra_stmts, new_stmts));
1322 extra_stmts += remaining;
1330 // Splice in the new statements, from the end of the block.
1331 // FIXME(eddyb) This could be more efficient with a "gap buffer"
1332 // where a range of elements ("gap") is left uninitialized, with
1333 // splicing adding new elements to the end of that gap and moving
1334 // existing elements from before the gap to the end of the gap.
1335 // For now, this is safe code, emulating a gap but initializing it.
1336 let mut gap = self.statements.len()..self.statements.len() + extra_stmts;
1337 self.statements.resize(
1339 Statement { source_info: SourceInfo::outermost(DUMMY_SP), kind: StatementKind::Nop },
1341 for (splice_start, new_stmts) in splices.into_iter().rev() {
1342 let splice_end = splice_start + new_stmts.size_hint().0;
1343 while gap.end > splice_end {
1346 self.statements.swap(gap.start, gap.end);
1348 self.statements.splice(splice_start..splice_end, new_stmts);
1349 gap.end = splice_start;
1353 pub fn visitable(&self, index: usize) -> &dyn MirVisitable<'tcx> {
1354 if index < self.statements.len() { &self.statements[index] } else { &self.terminator }
1358 impl<O> AssertKind<O> {
1359 /// Getting a description does not require `O` to be printable, and does not
1360 /// require allocation.
1361 /// The caller is expected to handle `BoundsCheck` separately.
1362 pub fn description(&self) -> &'static str {
1365 Overflow(BinOp::Add, _, _) => "attempt to add with overflow",
1366 Overflow(BinOp::Sub, _, _) => "attempt to subtract with overflow",
1367 Overflow(BinOp::Mul, _, _) => "attempt to multiply with overflow",
1368 Overflow(BinOp::Div, _, _) => "attempt to divide with overflow",
1369 Overflow(BinOp::Rem, _, _) => "attempt to calculate the remainder with overflow",
1370 OverflowNeg(_) => "attempt to negate with overflow",
1371 Overflow(BinOp::Shr, _, _) => "attempt to shift right with overflow",
1372 Overflow(BinOp::Shl, _, _) => "attempt to shift left with overflow",
1373 Overflow(op, _, _) => bug!("{:?} cannot overflow", op),
1374 DivisionByZero(_) => "attempt to divide by zero",
1375 RemainderByZero(_) => "attempt to calculate the remainder with a divisor of zero",
1376 ResumedAfterReturn(GeneratorKind::Gen) => "generator resumed after completion",
1377 ResumedAfterReturn(GeneratorKind::Async(_)) => "`async fn` resumed after completion",
1378 ResumedAfterPanic(GeneratorKind::Gen) => "generator resumed after panicking",
1379 ResumedAfterPanic(GeneratorKind::Async(_)) => "`async fn` resumed after panicking",
1380 BoundsCheck { .. } => bug!("Unexpected AssertKind"),
1384 /// Format the message arguments for the `assert(cond, msg..)` terminator in MIR printing.
1385 pub fn fmt_assert_args<W: Write>(&self, f: &mut W) -> fmt::Result
1391 BoundsCheck { ref len, ref index } => write!(
1393 "\"index out of bounds: the length is {{}} but the index is {{}}\", {:?}, {:?}",
1397 OverflowNeg(op) => {
1398 write!(f, "\"attempt to negate `{{}}`, which would overflow\", {:?}", op)
1400 DivisionByZero(op) => write!(f, "\"attempt to divide `{{}}` by zero\", {:?}", op),
1401 RemainderByZero(op) => write!(
1403 "\"attempt to calculate the remainder of `{{}}` with a divisor of zero\", {:?}",
1406 Overflow(BinOp::Add, l, r) => write!(
1408 "\"attempt to compute `{{}} + {{}}`, which would overflow\", {:?}, {:?}",
1411 Overflow(BinOp::Sub, l, r) => write!(
1413 "\"attempt to compute `{{}} - {{}}`, which would overflow\", {:?}, {:?}",
1416 Overflow(BinOp::Mul, l, r) => write!(
1418 "\"attempt to compute `{{}} * {{}}`, which would overflow\", {:?}, {:?}",
1421 Overflow(BinOp::Div, l, r) => write!(
1423 "\"attempt to compute `{{}} / {{}}`, which would overflow\", {:?}, {:?}",
1426 Overflow(BinOp::Rem, l, r) => write!(
1428 "\"attempt to compute the remainder of `{{}} % {{}}`, which would overflow\", {:?}, {:?}",
1431 Overflow(BinOp::Shr, _, r) => {
1432 write!(f, "\"attempt to shift right by `{{}}`, which would overflow\", {:?}", r)
1434 Overflow(BinOp::Shl, _, r) => {
1435 write!(f, "\"attempt to shift left by `{{}}`, which would overflow\", {:?}", r)
1437 _ => write!(f, "\"{}\"", self.description()),
1442 impl<O: fmt::Debug> fmt::Debug for AssertKind<O> {
1443 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1446 BoundsCheck { ref len, ref index } => write!(
1448 "index out of bounds: the length is {:?} but the index is {:?}",
1451 OverflowNeg(op) => write!(f, "attempt to negate `{:#?}`, which would overflow", op),
1452 DivisionByZero(op) => write!(f, "attempt to divide `{:#?}` by zero", op),
1453 RemainderByZero(op) => write!(
1455 "attempt to calculate the remainder of `{:#?}` with a divisor of zero",
1458 Overflow(BinOp::Add, l, r) => {
1459 write!(f, "attempt to compute `{:#?} + {:#?}`, which would overflow", l, r)
1461 Overflow(BinOp::Sub, l, r) => {
1462 write!(f, "attempt to compute `{:#?} - {:#?}`, which would overflow", l, r)
1464 Overflow(BinOp::Mul, l, r) => {
1465 write!(f, "attempt to compute `{:#?} * {:#?}`, which would overflow", l, r)
1467 Overflow(BinOp::Div, l, r) => {
1468 write!(f, "attempt to compute `{:#?} / {:#?}`, which would overflow", l, r)
1470 Overflow(BinOp::Rem, l, r) => write!(
1472 "attempt to compute the remainder of `{:#?} % {:#?}`, which would overflow",
1475 Overflow(BinOp::Shr, _, r) => {
1476 write!(f, "attempt to shift right by `{:#?}`, which would overflow", r)
1478 Overflow(BinOp::Shl, _, r) => {
1479 write!(f, "attempt to shift left by `{:#?}`, which would overflow", r)
1481 _ => write!(f, "{}", self.description()),
1486 ///////////////////////////////////////////////////////////////////////////
1489 #[derive(Clone, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
1490 pub struct Statement<'tcx> {
1491 pub source_info: SourceInfo,
1492 pub kind: StatementKind<'tcx>,
1495 // `Statement` is used a lot. Make sure it doesn't unintentionally get bigger.
1496 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
1497 static_assert_size!(Statement<'_>, 32);
1499 impl Statement<'_> {
1500 /// Changes a statement to a nop. This is both faster than deleting instructions and avoids
1501 /// invalidating statement indices in `Location`s.
1502 pub fn make_nop(&mut self) {
1503 self.kind = StatementKind::Nop
1506 /// Changes a statement to a nop and returns the original statement.
1507 pub fn replace_nop(&mut self) -> Self {
1509 source_info: self.source_info,
1510 kind: mem::replace(&mut self.kind, StatementKind::Nop),
1515 #[derive(Clone, Debug, PartialEq, TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable)]
1516 pub enum StatementKind<'tcx> {
1517 /// Write the RHS Rvalue to the LHS Place.
1518 Assign(Box<(Place<'tcx>, Rvalue<'tcx>)>),
1520 /// This represents all the reading that a pattern match may do
1521 /// (e.g., inspecting constants and discriminant values), and the
1522 /// kind of pattern it comes from. This is in order to adapt potential
1523 /// error messages to these specific patterns.
1525 /// Note that this also is emitted for regular `let` bindings to ensure that locals that are
1526 /// never accessed still get some sanity checks for, e.g., `let x: ! = ..;`
1527 FakeRead(Box<(FakeReadCause, Place<'tcx>)>),
1529 /// Write the discriminant for a variant to the enum Place.
1530 SetDiscriminant { place: Box<Place<'tcx>>, variant_index: VariantIdx },
1532 /// Start a live range for the storage of the local.
1535 /// End the current live range for the storage of the local.
1538 /// Executes a piece of inline Assembly. Stored in a Box to keep the size
1539 /// of `StatementKind` low.
1540 LlvmInlineAsm(Box<LlvmInlineAsm<'tcx>>),
1542 /// Retag references in the given place, ensuring they got fresh tags. This is
1543 /// part of the Stacked Borrows model. These statements are currently only interpreted
1544 /// by miri and only generated when "-Z mir-emit-retag" is passed.
1545 /// See <https://internals.rust-lang.org/t/stacked-borrows-an-aliasing-model-for-rust/8153/>
1546 /// for more details.
1547 Retag(RetagKind, Box<Place<'tcx>>),
1549 /// Encodes a user's type ascription. These need to be preserved
1550 /// intact so that NLL can respect them. For example:
1554 /// The effect of this annotation is to relate the type `T_y` of the place `y`
1555 /// to the user-given type `T`. The effect depends on the specified variance:
1557 /// - `Covariant` -- requires that `T_y <: T`
1558 /// - `Contravariant` -- requires that `T_y :> T`
1559 /// - `Invariant` -- requires that `T_y == T`
1560 /// - `Bivariant` -- no effect
1561 AscribeUserType(Box<(Place<'tcx>, UserTypeProjection)>, ty::Variance),
1563 /// Marks the start of a "coverage region", injected with '-Zinstrument-coverage'. A
1564 /// `Coverage` statement carries metadata about the coverage region, used to inject a coverage
1565 /// map into the binary. If `Coverage::kind` is a `Counter`, the statement also generates
1566 /// executable code, to increment a counter variable at runtime, each time the code region is
1568 Coverage(Box<Coverage>),
1570 /// Denotes a call to the intrinsic function copy_overlapping, where `src_dst` denotes the
1571 /// memory being read from and written to(one field to save memory), and size
1572 /// indicates how many bytes are being copied over.
1573 CopyNonOverlapping(Box<CopyNonOverlapping<'tcx>>),
1575 /// No-op. Useful for deleting instructions without affecting statement indices.
1579 impl<'tcx> StatementKind<'tcx> {
1580 pub fn as_assign_mut(&mut self) -> Option<&mut (Place<'tcx>, Rvalue<'tcx>)> {
1582 StatementKind::Assign(x) => Some(x),
1587 pub fn as_assign(&self) -> Option<&(Place<'tcx>, Rvalue<'tcx>)> {
1589 StatementKind::Assign(x) => Some(x),
1595 /// Describes what kind of retag is to be performed.
1596 #[derive(Copy, Clone, TyEncodable, TyDecodable, Debug, PartialEq, Eq, Hash, HashStable)]
1597 pub enum RetagKind {
1598 /// The initial retag when entering a function.
1600 /// Retag preparing for a two-phase borrow.
1602 /// Retagging raw pointers.
1604 /// A "normal" retag.
1608 /// The `FakeReadCause` describes the type of pattern why a FakeRead statement exists.
1609 #[derive(Copy, Clone, TyEncodable, TyDecodable, Debug, Hash, HashStable, PartialEq)]
1610 pub enum FakeReadCause {
1611 /// Inject a fake read of the borrowed input at the end of each guards
1614 /// This should ensure that you cannot change the variant for an enum while
1615 /// you are in the midst of matching on it.
1618 /// `let x: !; match x {}` doesn't generate any read of x so we need to
1619 /// generate a read of x to check that it is initialized and safe.
1621 /// If a closure pattern matches a Place starting with an Upvar, then we introduce a
1622 /// FakeRead for that Place outside the closure, in such a case this option would be
1623 /// Some(closure_def_id).
1624 /// Otherwise, the value of the optional DefId will be None.
1625 ForMatchedPlace(Option<DefId>),
1627 /// A fake read of the RefWithinGuard version of a bind-by-value variable
1628 /// in a match guard to ensure that it's value hasn't change by the time
1629 /// we create the OutsideGuard version.
1632 /// Officially, the semantics of
1634 /// `let pattern = <expr>;`
1636 /// is that `<expr>` is evaluated into a temporary and then this temporary is
1637 /// into the pattern.
1639 /// However, if we see the simple pattern `let var = <expr>`, we optimize this to
1640 /// evaluate `<expr>` directly into the variable `var`. This is mostly unobservable,
1641 /// but in some cases it can affect the borrow checker, as in #53695.
1642 /// Therefore, we insert a "fake read" here to ensure that we get
1643 /// appropriate errors.
1645 /// If a closure pattern matches a Place starting with an Upvar, then we introduce a
1646 /// FakeRead for that Place outside the closure, in such a case this option would be
1647 /// Some(closure_def_id).
1648 /// Otherwise, the value of the optional DefId will be None.
1649 ForLet(Option<DefId>),
1651 /// If we have an index expression like
1653 /// (*x)[1][{ x = y; 4}]
1655 /// then the first bounds check is invalidated when we evaluate the second
1656 /// index expression. Thus we create a fake borrow of `x` across the second
1657 /// indexer, which will cause a borrow check error.
1661 #[derive(Clone, Debug, PartialEq, TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable)]
1662 pub struct LlvmInlineAsm<'tcx> {
1663 pub asm: hir::LlvmInlineAsmInner,
1664 pub outputs: Box<[Place<'tcx>]>,
1665 pub inputs: Box<[(Span, Operand<'tcx>)]>,
1668 impl Debug for Statement<'_> {
1669 fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
1670 use self::StatementKind::*;
1672 Assign(box (ref place, ref rv)) => write!(fmt, "{:?} = {:?}", place, rv),
1673 FakeRead(box (ref cause, ref place)) => {
1674 write!(fmt, "FakeRead({:?}, {:?})", cause, place)
1676 Retag(ref kind, ref place) => write!(
1680 RetagKind::FnEntry => "[fn entry] ",
1681 RetagKind::TwoPhase => "[2phase] ",
1682 RetagKind::Raw => "[raw] ",
1683 RetagKind::Default => "",
1687 StorageLive(ref place) => write!(fmt, "StorageLive({:?})", place),
1688 StorageDead(ref place) => write!(fmt, "StorageDead({:?})", place),
1689 SetDiscriminant { ref place, variant_index } => {
1690 write!(fmt, "discriminant({:?}) = {:?}", place, variant_index)
1692 LlvmInlineAsm(ref asm) => {
1693 write!(fmt, "llvm_asm!({:?} : {:?} : {:?})", asm.asm, asm.outputs, asm.inputs)
1695 AscribeUserType(box (ref place, ref c_ty), ref variance) => {
1696 write!(fmt, "AscribeUserType({:?}, {:?}, {:?})", place, variance, c_ty)
1698 Coverage(box self::Coverage { ref kind, code_region: Some(ref rgn) }) => {
1699 write!(fmt, "Coverage::{:?} for {:?}", kind, rgn)
1701 Coverage(box ref coverage) => write!(fmt, "Coverage::{:?}", coverage.kind),
1702 CopyNonOverlapping(box crate::mir::CopyNonOverlapping {
1707 write!(fmt, "copy_nonoverlapping(src={:?}, dst={:?}, count={:?})", src, dst, count)
1709 Nop => write!(fmt, "nop"),
1714 #[derive(Clone, Debug, PartialEq, TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable)]
1715 pub struct Coverage {
1716 pub kind: CoverageKind,
1717 pub code_region: Option<CodeRegion>,
1720 #[derive(Clone, Debug, PartialEq, TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable)]
1721 pub struct CopyNonOverlapping<'tcx> {
1722 pub src: Operand<'tcx>,
1723 pub dst: Operand<'tcx>,
1724 /// Number of elements to copy from src to dest, not bytes.
1725 pub count: Operand<'tcx>,
1728 ///////////////////////////////////////////////////////////////////////////
1731 /// A path to a value; something that can be evaluated without
1732 /// changing or disturbing program state.
1733 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, TyEncodable, HashStable)]
1734 pub struct Place<'tcx> {
1737 /// projection out of a place (access a field, deref a pointer, etc)
1738 pub projection: &'tcx List<PlaceElem<'tcx>>,
1741 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
1742 static_assert_size!(Place<'_>, 16);
1744 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
1745 #[derive(TyEncodable, TyDecodable, HashStable)]
1746 pub enum ProjectionElem<V, T> {
1751 /// These indices are generated by slice patterns. Easiest to explain
1755 /// [X, _, .._, _, _] => { offset: 0, min_length: 4, from_end: false },
1756 /// [_, X, .._, _, _] => { offset: 1, min_length: 4, from_end: false },
1757 /// [_, _, .._, X, _] => { offset: 2, min_length: 4, from_end: true },
1758 /// [_, _, .._, _, X] => { offset: 1, min_length: 4, from_end: true },
1761 /// index or -index (in Python terms), depending on from_end
1763 /// The thing being indexed must be at least this long. For arrays this
1764 /// is always the exact length.
1766 /// Counting backwards from end? This is always false when indexing an
1771 /// These indices are generated by slice patterns.
1773 /// If `from_end` is true `slice[from..slice.len() - to]`.
1774 /// Otherwise `array[from..to]`.
1778 /// Whether `to` counts from the start or end of the array/slice.
1779 /// For `PlaceElem`s this is `true` if and only if the base is a slice.
1780 /// For `ProjectionKind`, this can also be `true` for arrays.
1784 /// "Downcast" to a variant of an ADT. Currently, we only introduce
1785 /// this for ADTs with more than one variant. It may be better to
1786 /// just introduce it always, or always for enums.
1788 /// The included Symbol is the name of the variant, used for printing MIR.
1789 Downcast(Option<Symbol>, VariantIdx),
1792 impl<V, T> ProjectionElem<V, T> {
1793 /// Returns `true` if the target of this projection may refer to a different region of memory
1795 fn is_indirect(&self) -> bool {
1797 Self::Deref => true,
1801 | Self::ConstantIndex { .. }
1802 | Self::Subslice { .. }
1803 | Self::Downcast(_, _) => false,
1808 /// Alias for projections as they appear in places, where the base is a place
1809 /// and the index is a local.
1810 pub type PlaceElem<'tcx> = ProjectionElem<Local, Ty<'tcx>>;
1812 // At least on 64 bit systems, `PlaceElem` should not be larger than two pointers.
1813 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
1814 static_assert_size!(PlaceElem<'_>, 24);
1816 /// Alias for projections as they appear in `UserTypeProjection`, where we
1817 /// need neither the `V` parameter for `Index` nor the `T` for `Field`.
1818 pub type ProjectionKind = ProjectionElem<(), ()>;
1820 rustc_index::newtype_index! {
1823 DEBUG_FORMAT = "field[{}]"
1827 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
1828 pub struct PlaceRef<'tcx> {
1830 pub projection: &'tcx [PlaceElem<'tcx>],
1833 impl<'tcx> Place<'tcx> {
1834 // FIXME change this to a const fn by also making List::empty a const fn.
1835 pub fn return_place() -> Place<'tcx> {
1836 Place { local: RETURN_PLACE, projection: List::empty() }
1839 /// Returns `true` if this `Place` contains a `Deref` projection.
1841 /// If `Place::is_indirect` returns false, the caller knows that the `Place` refers to the
1842 /// same region of memory as its base.
1843 pub fn is_indirect(&self) -> bool {
1844 self.projection.iter().any(|elem| elem.is_indirect())
1847 /// Finds the innermost `Local` from this `Place`, *if* it is either a local itself or
1848 /// a single deref of a local.
1850 pub fn local_or_deref_local(&self) -> Option<Local> {
1851 self.as_ref().local_or_deref_local()
1854 /// If this place represents a local variable like `_X` with no
1855 /// projections, return `Some(_X)`.
1857 pub fn as_local(&self) -> Option<Local> {
1858 self.as_ref().as_local()
1862 pub fn as_ref(&self) -> PlaceRef<'tcx> {
1863 PlaceRef { local: self.local, projection: &self.projection }
1866 /// Iterate over the projections in evaluation order, i.e., the first element is the base with
1867 /// its projection and then subsequently more projections are added.
1868 /// As a concrete example, given the place a.b.c, this would yield:
1872 /// Given a place without projections, the iterator is empty.
1874 pub fn iter_projections(
1876 ) -> impl Iterator<Item = (PlaceRef<'tcx>, PlaceElem<'tcx>)> + DoubleEndedIterator {
1877 self.projection.iter().enumerate().map(move |(i, proj)| {
1878 let base = PlaceRef { local: self.local, projection: &self.projection[..i] };
1884 impl From<Local> for Place<'_> {
1885 fn from(local: Local) -> Self {
1886 Place { local, projection: List::empty() }
1890 impl<'tcx> PlaceRef<'tcx> {
1891 /// Finds the innermost `Local` from this `Place`, *if* it is either a local itself or
1892 /// a single deref of a local.
1893 pub fn local_or_deref_local(&self) -> Option<Local> {
1895 PlaceRef { local, projection: [] }
1896 | PlaceRef { local, projection: [ProjectionElem::Deref] } => Some(local),
1901 /// If this place represents a local variable like `_X` with no
1902 /// projections, return `Some(_X)`.
1904 pub fn as_local(&self) -> Option<Local> {
1906 PlaceRef { local, projection: [] } => Some(local),
1912 pub fn last_projection(&self) -> Option<(PlaceRef<'tcx>, PlaceElem<'tcx>)> {
1913 if let &[ref proj_base @ .., elem] = self.projection {
1914 Some((PlaceRef { local: self.local, projection: proj_base }, elem))
1921 impl Debug for Place<'_> {
1922 fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
1923 for elem in self.projection.iter().rev() {
1925 ProjectionElem::Downcast(_, _) | ProjectionElem::Field(_, _) => {
1926 write!(fmt, "(").unwrap();
1928 ProjectionElem::Deref => {
1929 write!(fmt, "(*").unwrap();
1931 ProjectionElem::Index(_)
1932 | ProjectionElem::ConstantIndex { .. }
1933 | ProjectionElem::Subslice { .. } => {}
1937 write!(fmt, "{:?}", self.local)?;
1939 for elem in self.projection.iter() {
1941 ProjectionElem::Downcast(Some(name), _index) => {
1942 write!(fmt, " as {})", name)?;
1944 ProjectionElem::Downcast(None, index) => {
1945 write!(fmt, " as variant#{:?})", index)?;
1947 ProjectionElem::Deref => {
1950 ProjectionElem::Field(field, ty) => {
1951 write!(fmt, ".{:?}: {:?})", field.index(), ty)?;
1953 ProjectionElem::Index(ref index) => {
1954 write!(fmt, "[{:?}]", index)?;
1956 ProjectionElem::ConstantIndex { offset, min_length, from_end: false } => {
1957 write!(fmt, "[{:?} of {:?}]", offset, min_length)?;
1959 ProjectionElem::ConstantIndex { offset, min_length, from_end: true } => {
1960 write!(fmt, "[-{:?} of {:?}]", offset, min_length)?;
1962 ProjectionElem::Subslice { from, to, from_end: true } if to == 0 => {
1963 write!(fmt, "[{:?}:]", from)?;
1965 ProjectionElem::Subslice { from, to, from_end: true } if from == 0 => {
1966 write!(fmt, "[:-{:?}]", to)?;
1968 ProjectionElem::Subslice { from, to, from_end: true } => {
1969 write!(fmt, "[{:?}:-{:?}]", from, to)?;
1971 ProjectionElem::Subslice { from, to, from_end: false } => {
1972 write!(fmt, "[{:?}..{:?}]", from, to)?;
1981 ///////////////////////////////////////////////////////////////////////////
1984 rustc_index::newtype_index! {
1985 pub struct SourceScope {
1987 DEBUG_FORMAT = "scope[{}]",
1988 const OUTERMOST_SOURCE_SCOPE = 0,
1993 /// Finds the original HirId this MIR item came from.
1994 /// This is necessary after MIR optimizations, as otherwise we get a HirId
1995 /// from the function that was inlined instead of the function call site.
1998 source_scopes: &IndexVec<SourceScope, SourceScopeData<'tcx>>,
1999 ) -> Option<HirId> {
2000 let mut data = &source_scopes[self];
2001 // FIXME(oli-obk): we should be able to just walk the `inlined_parent_scope`, but it
2002 // does not work as I thought it would. Needs more investigation and documentation.
2003 while data.inlined.is_some() {
2005 data = &source_scopes[data.parent_scope.unwrap()];
2008 match &data.local_data {
2009 ClearCrossCrate::Set(data) => Some(data.lint_root),
2010 ClearCrossCrate::Clear => None,
2015 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
2016 pub struct SourceScopeData<'tcx> {
2018 pub parent_scope: Option<SourceScope>,
2020 /// Whether this scope is the root of a scope tree of another body,
2021 /// inlined into this body by the MIR inliner.
2022 /// `ty::Instance` is the callee, and the `Span` is the call site.
2023 pub inlined: Option<(ty::Instance<'tcx>, Span)>,
2025 /// Nearest (transitive) parent scope (if any) which is inlined.
2026 /// This is an optimization over walking up `parent_scope`
2027 /// until a scope with `inlined: Some(...)` is found.
2028 pub inlined_parent_scope: Option<SourceScope>,
2030 /// Crate-local information for this source scope, that can't (and
2031 /// needn't) be tracked across crates.
2032 pub local_data: ClearCrossCrate<SourceScopeLocalData>,
2035 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
2036 pub struct SourceScopeLocalData {
2037 /// An `HirId` with lint levels equivalent to this scope's lint levels.
2038 pub lint_root: hir::HirId,
2039 /// The unsafe block that contains this node.
2043 ///////////////////////////////////////////////////////////////////////////
2046 /// These are values that can appear inside an rvalue. They are intentionally
2047 /// limited to prevent rvalues from being nested in one another.
2048 #[derive(Clone, PartialEq, PartialOrd, TyEncodable, TyDecodable, Hash, HashStable)]
2049 pub enum Operand<'tcx> {
2050 /// Copy: The value must be available for use afterwards.
2052 /// This implies that the type of the place must be `Copy`; this is true
2053 /// by construction during build, but also checked by the MIR type checker.
2056 /// Move: The value (including old borrows of it) will not be used again.
2058 /// Safe for values of all types (modulo future developments towards `?Move`).
2059 /// Correct usage patterns are enforced by the borrow checker for safe code.
2060 /// `Copy` may be converted to `Move` to enable "last-use" optimizations.
2063 /// Synthesizes a constant value.
2064 Constant(Box<Constant<'tcx>>),
2067 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
2068 static_assert_size!(Operand<'_>, 24);
2070 impl<'tcx> Debug for Operand<'tcx> {
2071 fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
2072 use self::Operand::*;
2074 Constant(ref a) => write!(fmt, "{:?}", a),
2075 Copy(ref place) => write!(fmt, "{:?}", place),
2076 Move(ref place) => write!(fmt, "move {:?}", place),
2081 impl<'tcx> Operand<'tcx> {
2082 /// Convenience helper to make a constant that refers to the fn
2083 /// with given `DefId` and substs. Since this is used to synthesize
2084 /// MIR, assumes `user_ty` is None.
2085 pub fn function_handle(
2088 substs: SubstsRef<'tcx>,
2091 let ty = tcx.type_of(def_id).subst(tcx, substs);
2092 Operand::Constant(Box::new(Constant {
2095 literal: ConstantKind::Ty(ty::Const::zero_sized(tcx, ty)),
2099 pub fn is_move(&self) -> bool {
2100 matches!(self, Operand::Move(..))
2103 /// Convenience helper to make a literal-like constant from a given scalar value.
2104 /// Since this is used to synthesize MIR, assumes `user_ty` is None.
2105 pub fn const_from_scalar(
2110 ) -> Operand<'tcx> {
2112 let param_env_and_ty = ty::ParamEnv::empty().and(ty);
2114 .layout_of(param_env_and_ty)
2115 .unwrap_or_else(|e| panic!("could not compute layout for {:?}: {:?}", ty, e))
2117 let scalar_size = match val {
2118 Scalar::Int(int) => int.size(),
2119 _ => panic!("Invalid scalar type {:?}", val),
2121 scalar_size == type_size
2123 Operand::Constant(Box::new(Constant {
2126 literal: ConstantKind::Val(ConstValue::Scalar(val), ty),
2130 pub fn to_copy(&self) -> Self {
2132 Operand::Copy(_) | Operand::Constant(_) => self.clone(),
2133 Operand::Move(place) => Operand::Copy(place),
2137 /// Returns the `Place` that is the target of this `Operand`, or `None` if this `Operand` is a
2139 pub fn place(&self) -> Option<Place<'tcx>> {
2141 Operand::Copy(place) | Operand::Move(place) => Some(*place),
2142 Operand::Constant(_) => None,
2146 /// Returns the `Constant` that is the target of this `Operand`, or `None` if this `Operand` is a
2148 pub fn constant(&self) -> Option<&Constant<'tcx>> {
2150 Operand::Constant(x) => Some(&**x),
2151 Operand::Copy(_) | Operand::Move(_) => None,
2156 ///////////////////////////////////////////////////////////////////////////
2159 #[derive(Clone, TyEncodable, TyDecodable, Hash, HashStable, PartialEq)]
2160 pub enum Rvalue<'tcx> {
2161 /// x (either a move or copy, depending on type of x)
2165 Repeat(Operand<'tcx>, &'tcx ty::Const<'tcx>),
2168 Ref(Region<'tcx>, BorrowKind, Place<'tcx>),
2170 /// Accessing a thread local static. This is inherently a runtime operation, even if llvm
2171 /// treats it as an access to a static. This `Rvalue` yields a reference to the thread local
2173 ThreadLocalRef(DefId),
2175 /// Create a raw pointer to the given place
2176 /// Can be generated by raw address of expressions (`&raw const x`),
2177 /// or when casting a reference to a raw pointer.
2178 AddressOf(Mutability, Place<'tcx>),
2180 /// length of a `[X]` or `[X;n]` value
2183 Cast(CastKind, Operand<'tcx>, Ty<'tcx>),
2185 BinaryOp(BinOp, Box<(Operand<'tcx>, Operand<'tcx>)>),
2186 CheckedBinaryOp(BinOp, Box<(Operand<'tcx>, Operand<'tcx>)>),
2188 NullaryOp(NullOp, Ty<'tcx>),
2189 UnaryOp(UnOp, Operand<'tcx>),
2191 /// Read the discriminant of an ADT.
2193 /// Undefined (i.e., no effort is made to make it defined, but there’s no reason why it cannot
2194 /// be defined to return, say, a 0) if ADT is not an enum.
2195 Discriminant(Place<'tcx>),
2197 /// Creates an aggregate value, like a tuple or struct. This is
2198 /// only needed because we want to distinguish `dest = Foo { x:
2199 /// ..., y: ... }` from `dest.x = ...; dest.y = ...;` in the case
2200 /// that `Foo` has a destructor. These rvalues can be optimized
2201 /// away after type-checking and before lowering.
2202 Aggregate(Box<AggregateKind<'tcx>>, Vec<Operand<'tcx>>),
2204 /// Transmutes a `*mut u8` into shallow-initialized `Box<T>`.
2206 /// This is different a normal transmute because dataflow analysis will treat the box
2207 /// as initialized but its content as uninitialized.
2208 ShallowInitBox(Operand<'tcx>, Ty<'tcx>),
2211 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
2212 static_assert_size!(Rvalue<'_>, 40);
2214 #[derive(Clone, Copy, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
2217 Pointer(PointerCast),
2220 #[derive(Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
2221 pub enum AggregateKind<'tcx> {
2222 /// The type is of the element
2226 /// The second field is the variant index. It's equal to 0 for struct
2227 /// and union expressions. The fourth field is
2228 /// active field number and is present only for union expressions
2229 /// -- e.g., for a union expression `SomeUnion { c: .. }`, the
2230 /// active field index would identity the field `c`
2231 Adt(&'tcx AdtDef, VariantIdx, SubstsRef<'tcx>, Option<UserTypeAnnotationIndex>, Option<usize>),
2233 Closure(DefId, SubstsRef<'tcx>),
2234 Generator(DefId, SubstsRef<'tcx>, hir::Movability),
2237 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
2238 static_assert_size!(AggregateKind<'_>, 48);
2240 #[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
2242 /// The `+` operator (addition)
2244 /// The `-` operator (subtraction)
2246 /// The `*` operator (multiplication)
2248 /// The `/` operator (division)
2250 /// The `%` operator (modulus)
2252 /// The `^` operator (bitwise xor)
2254 /// The `&` operator (bitwise and)
2256 /// The `|` operator (bitwise or)
2258 /// The `<<` operator (shift left)
2260 /// The `>>` operator (shift right)
2262 /// The `==` operator (equality)
2264 /// The `<` operator (less than)
2266 /// The `<=` operator (less than or equal to)
2268 /// The `!=` operator (not equal to)
2270 /// The `>=` operator (greater than or equal to)
2272 /// The `>` operator (greater than)
2274 /// The `ptr.offset` operator
2279 pub fn is_checkable(self) -> bool {
2281 matches!(self, Add | Sub | Mul | Shl | Shr)
2285 #[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
2287 /// Returns the size of a value of that type
2289 /// Returns the minimum alignment of a type
2291 /// Creates a new uninitialized box for a value of that type
2295 #[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
2297 /// The `!` operator for logical inversion
2299 /// The `-` operator for negation
2303 impl<'tcx> Debug for Rvalue<'tcx> {
2304 fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
2305 use self::Rvalue::*;
2308 Use(ref place) => write!(fmt, "{:?}", place),
2309 Repeat(ref a, ref b) => {
2310 write!(fmt, "[{:?}; ", a)?;
2311 pretty_print_const(b, fmt, false)?;
2314 Len(ref a) => write!(fmt, "Len({:?})", a),
2315 Cast(ref kind, ref place, ref ty) => {
2316 write!(fmt, "{:?} as {:?} ({:?})", place, ty, kind)
2318 BinaryOp(ref op, box (ref a, ref b)) => write!(fmt, "{:?}({:?}, {:?})", op, a, b),
2319 CheckedBinaryOp(ref op, box (ref a, ref b)) => {
2320 write!(fmt, "Checked{:?}({:?}, {:?})", op, a, b)
2322 UnaryOp(ref op, ref a) => write!(fmt, "{:?}({:?})", op, a),
2323 Discriminant(ref place) => write!(fmt, "discriminant({:?})", place),
2324 NullaryOp(ref op, ref t) => write!(fmt, "{:?}({:?})", op, t),
2325 ThreadLocalRef(did) => ty::tls::with(|tcx| {
2326 let muta = tcx.static_mutability(did).unwrap().prefix_str();
2327 write!(fmt, "&/*tls*/ {}{}", muta, tcx.def_path_str(did))
2329 Ref(region, borrow_kind, ref place) => {
2330 let kind_str = match borrow_kind {
2331 BorrowKind::Shared => "",
2332 BorrowKind::Shallow => "shallow ",
2333 BorrowKind::Mut { .. } | BorrowKind::Unique => "mut ",
2336 // When printing regions, add trailing space if necessary.
2337 let print_region = ty::tls::with(|tcx| {
2338 tcx.sess.verbose() || tcx.sess.opts.debugging_opts.identify_regions
2340 let region = if print_region {
2341 let mut region = region.to_string();
2342 if !region.is_empty() {
2347 // Do not even print 'static
2350 write!(fmt, "&{}{}{:?}", region, kind_str, place)
2353 AddressOf(mutability, ref place) => {
2354 let kind_str = match mutability {
2355 Mutability::Mut => "mut",
2356 Mutability::Not => "const",
2359 write!(fmt, "&raw {} {:?}", kind_str, place)
2362 Aggregate(ref kind, ref places) => {
2363 let fmt_tuple = |fmt: &mut Formatter<'_>, name: &str| {
2364 let mut tuple_fmt = fmt.debug_tuple(name);
2365 for place in places {
2366 tuple_fmt.field(place);
2372 AggregateKind::Array(_) => write!(fmt, "{:?}", places),
2374 AggregateKind::Tuple => {
2375 if places.is_empty() {
2382 AggregateKind::Adt(adt_def, variant, substs, _user_ty, _) => {
2383 let variant_def = &adt_def.variants[variant];
2385 let name = ty::tls::with(|tcx| {
2386 let mut name = String::new();
2387 let substs = tcx.lift(substs).expect("could not lift for printing");
2388 FmtPrinter::new(tcx, &mut name, Namespace::ValueNS)
2389 .print_def_path(variant_def.def_id, substs)?;
2393 match variant_def.ctor_kind {
2394 CtorKind::Const => fmt.write_str(&name),
2395 CtorKind::Fn => fmt_tuple(fmt, &name),
2396 CtorKind::Fictive => {
2397 let mut struct_fmt = fmt.debug_struct(&name);
2398 for (field, place) in iter::zip(&variant_def.fields, places) {
2399 struct_fmt.field(&field.ident.as_str(), place);
2406 AggregateKind::Closure(def_id, substs) => ty::tls::with(|tcx| {
2407 if let Some(def_id) = def_id.as_local() {
2408 let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
2409 let name = if tcx.sess.opts.debugging_opts.span_free_formats {
2410 let substs = tcx.lift(substs).unwrap();
2413 tcx.def_path_str_with_substs(def_id.to_def_id(), substs),
2416 let span = tcx.hir().span(hir_id);
2419 tcx.sess.source_map().span_to_diagnostic_string(span)
2422 let mut struct_fmt = fmt.debug_struct(&name);
2424 // FIXME(project-rfc-2229#48): This should be a list of capture names/places
2425 if let Some(upvars) = tcx.upvars_mentioned(def_id) {
2426 for (&var_id, place) in iter::zip(upvars.keys(), places) {
2427 let var_name = tcx.hir().name(var_id);
2428 struct_fmt.field(&var_name.as_str(), place);
2434 write!(fmt, "[closure]")
2438 AggregateKind::Generator(def_id, _, _) => ty::tls::with(|tcx| {
2439 if let Some(def_id) = def_id.as_local() {
2440 let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
2441 let name = format!("[generator@{:?}]", tcx.hir().span(hir_id));
2442 let mut struct_fmt = fmt.debug_struct(&name);
2444 // FIXME(project-rfc-2229#48): This should be a list of capture names/places
2445 if let Some(upvars) = tcx.upvars_mentioned(def_id) {
2446 for (&var_id, place) in iter::zip(upvars.keys(), places) {
2447 let var_name = tcx.hir().name(var_id);
2448 struct_fmt.field(&var_name.as_str(), place);
2454 write!(fmt, "[generator]")
2460 ShallowInitBox(ref place, ref ty) => {
2461 write!(fmt, "ShallowInitBox({:?}, {:?})", place, ty)
2467 ///////////////////////////////////////////////////////////////////////////
2470 /// Two constants are equal if they are the same constant. Note that
2471 /// this does not necessarily mean that they are `==` in Rust. In
2472 /// particular, one must be wary of `NaN`!
2474 #[derive(Clone, Copy, PartialEq, PartialOrd, TyEncodable, TyDecodable, Hash, HashStable)]
2475 pub struct Constant<'tcx> {
2478 /// Optional user-given type: for something like
2479 /// `collect::<Vec<_>>`, this would be present and would
2480 /// indicate that `Vec<_>` was explicitly specified.
2482 /// Needed for NLL to impose user-given type constraints.
2483 pub user_ty: Option<UserTypeAnnotationIndex>,
2485 pub literal: ConstantKind<'tcx>,
2488 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, TyEncodable, TyDecodable, Hash, HashStable, Debug)]
2490 pub enum ConstantKind<'tcx> {
2491 /// This constant came from the type system
2492 Ty(&'tcx ty::Const<'tcx>),
2493 /// This constant cannot go back into the type system, as it represents
2494 /// something the type system cannot handle (e.g. pointers).
2495 Val(interpret::ConstValue<'tcx>, Ty<'tcx>),
2498 impl Constant<'tcx> {
2499 pub fn check_static_ptr(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
2500 match self.literal.const_for_ty()?.val.try_to_scalar() {
2501 Some(Scalar::Ptr(ptr, _size)) => match tcx.global_alloc(ptr.provenance) {
2502 GlobalAlloc::Static(def_id) => {
2503 assert!(!tcx.is_thread_local_static(def_id));
2512 pub fn ty(&self) -> Ty<'tcx> {
2517 impl From<&'tcx ty::Const<'tcx>> for ConstantKind<'tcx> {
2519 fn from(ct: &'tcx ty::Const<'tcx>) -> Self {
2524 impl ConstantKind<'tcx> {
2525 /// Returns `None` if the constant is not trivially safe for use in the type system.
2526 pub fn const_for_ty(&self) -> Option<&'tcx ty::Const<'tcx>> {
2528 ConstantKind::Ty(c) => Some(c),
2529 ConstantKind::Val(..) => None,
2533 pub fn ty(&self) -> Ty<'tcx> {
2535 ConstantKind::Ty(c) => c.ty,
2536 ConstantKind::Val(_, ty) => ty,
2541 pub fn try_to_value(self) -> Option<interpret::ConstValue<'tcx>> {
2543 ConstantKind::Ty(c) => c.val.try_to_value(),
2544 ConstantKind::Val(val, _) => Some(val),
2549 pub fn try_to_scalar(self) -> Option<Scalar> {
2550 self.try_to_value()?.try_to_scalar()
2554 pub fn try_to_scalar_int(self) -> Option<ScalarInt> {
2555 Some(self.try_to_value()?.try_to_scalar()?.assert_int())
2559 pub fn try_to_bits(self, size: Size) -> Option<u128> {
2560 self.try_to_scalar_int()?.to_bits(size).ok()
2564 pub fn try_to_bool(self) -> Option<bool> {
2565 self.try_to_scalar_int()?.try_into().ok()
2569 pub fn try_eval_bits(
2572 param_env: ty::ParamEnv<'tcx>,
2576 Self::Ty(ct) => ct.try_eval_bits(tcx, param_env, ty),
2577 Self::Val(val, t) => {
2580 tcx.layout_of(param_env.with_reveal_all_normalized(tcx).and(ty)).ok()?.size;
2581 val.try_to_bits(size)
2587 pub fn try_eval_bool(&self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Option<bool> {
2589 Self::Ty(ct) => ct.try_eval_bool(tcx, param_env),
2590 Self::Val(val, _) => val.try_to_bool(),
2595 pub fn try_eval_usize(&self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Option<u64> {
2597 Self::Ty(ct) => ct.try_eval_usize(tcx, param_env),
2598 Self::Val(val, _) => val.try_to_machine_usize(tcx),
2603 /// A collection of projections into user types.
2605 /// They are projections because a binding can occur a part of a
2606 /// parent pattern that has been ascribed a type.
2608 /// Its a collection because there can be multiple type ascriptions on
2609 /// the path from the root of the pattern down to the binding itself.
2614 /// struct S<'a>((i32, &'a str), String);
2615 /// let S((_, w): (i32, &'static str), _): S = ...;
2616 /// // ------ ^^^^^^^^^^^^^^^^^^^ (1)
2617 /// // --------------------------------- ^ (2)
2620 /// The highlights labelled `(1)` show the subpattern `(_, w)` being
2621 /// ascribed the type `(i32, &'static str)`.
2623 /// The highlights labelled `(2)` show the whole pattern being
2624 /// ascribed the type `S`.
2626 /// In this example, when we descend to `w`, we will have built up the
2627 /// following two projected types:
2629 /// * base: `S`, projection: `(base.0).1`
2630 /// * base: `(i32, &'static str)`, projection: `base.1`
2632 /// The first will lead to the constraint `w: &'1 str` (for some
2633 /// inferred region `'1`). The second will lead to the constraint `w:
2635 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
2636 pub struct UserTypeProjections {
2637 pub contents: Vec<(UserTypeProjection, Span)>,
2640 impl<'tcx> UserTypeProjections {
2641 pub fn none() -> Self {
2642 UserTypeProjections { contents: vec![] }
2645 pub fn is_empty(&self) -> bool {
2646 self.contents.is_empty()
2649 pub fn projections_and_spans(
2651 ) -> impl Iterator<Item = &(UserTypeProjection, Span)> + ExactSizeIterator {
2652 self.contents.iter()
2655 pub fn projections(&self) -> impl Iterator<Item = &UserTypeProjection> + ExactSizeIterator {
2656 self.contents.iter().map(|&(ref user_type, _span)| user_type)
2659 pub fn push_projection(mut self, user_ty: &UserTypeProjection, span: Span) -> Self {
2660 self.contents.push((user_ty.clone(), span));
2666 mut f: impl FnMut(UserTypeProjection) -> UserTypeProjection,
2668 self.contents = self.contents.drain(..).map(|(proj, span)| (f(proj), span)).collect();
2672 pub fn index(self) -> Self {
2673 self.map_projections(|pat_ty_proj| pat_ty_proj.index())
2676 pub fn subslice(self, from: u64, to: u64) -> Self {
2677 self.map_projections(|pat_ty_proj| pat_ty_proj.subslice(from, to))
2680 pub fn deref(self) -> Self {
2681 self.map_projections(|pat_ty_proj| pat_ty_proj.deref())
2684 pub fn leaf(self, field: Field) -> Self {
2685 self.map_projections(|pat_ty_proj| pat_ty_proj.leaf(field))
2688 pub fn variant(self, adt_def: &'tcx AdtDef, variant_index: VariantIdx, field: Field) -> Self {
2689 self.map_projections(|pat_ty_proj| pat_ty_proj.variant(adt_def, variant_index, field))
2693 /// Encodes the effect of a user-supplied type annotation on the
2694 /// subcomponents of a pattern. The effect is determined by applying the
2695 /// given list of proejctions to some underlying base type. Often,
2696 /// the projection element list `projs` is empty, in which case this
2697 /// directly encodes a type in `base`. But in the case of complex patterns with
2698 /// subpatterns and bindings, we want to apply only a *part* of the type to a variable,
2699 /// in which case the `projs` vector is used.
2703 /// * `let x: T = ...` -- here, the `projs` vector is empty.
2705 /// * `let (x, _): T = ...` -- here, the `projs` vector would contain
2706 /// `field[0]` (aka `.0`), indicating that the type of `s` is
2707 /// determined by finding the type of the `.0` field from `T`.
2708 #[derive(Clone, Debug, TyEncodable, TyDecodable, Hash, HashStable, PartialEq)]
2709 pub struct UserTypeProjection {
2710 pub base: UserTypeAnnotationIndex,
2711 pub projs: Vec<ProjectionKind>,
2714 impl Copy for ProjectionKind {}
2716 impl UserTypeProjection {
2717 pub(crate) fn index(mut self) -> Self {
2718 self.projs.push(ProjectionElem::Index(()));
2722 pub(crate) fn subslice(mut self, from: u64, to: u64) -> Self {
2723 self.projs.push(ProjectionElem::Subslice { from, to, from_end: true });
2727 pub(crate) fn deref(mut self) -> Self {
2728 self.projs.push(ProjectionElem::Deref);
2732 pub(crate) fn leaf(mut self, field: Field) -> Self {
2733 self.projs.push(ProjectionElem::Field(field, ()));
2737 pub(crate) fn variant(
2740 variant_index: VariantIdx,
2743 self.projs.push(ProjectionElem::Downcast(
2744 Some(adt_def.variants[variant_index].ident.name),
2747 self.projs.push(ProjectionElem::Field(field, ()));
2752 TrivialTypeFoldableAndLiftImpls! { ProjectionKind, }
2754 impl<'tcx> TypeFoldable<'tcx> for UserTypeProjection {
2755 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
2756 UserTypeProjection {
2757 base: self.base.fold_with(folder),
2758 projs: self.projs.fold_with(folder),
2762 fn super_visit_with<Vs: TypeVisitor<'tcx>>(
2765 ) -> ControlFlow<Vs::BreakTy> {
2766 self.base.visit_with(visitor)
2767 // Note: there's nothing in `self.proj` to visit.
2771 rustc_index::newtype_index! {
2772 pub struct Promoted {
2774 DEBUG_FORMAT = "promoted[{}]"
2778 impl<'tcx> Debug for Constant<'tcx> {
2779 fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
2780 write!(fmt, "{}", self)
2784 impl<'tcx> Display for Constant<'tcx> {
2785 fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
2786 match self.ty().kind() {
2788 _ => write!(fmt, "const ")?,
2790 Display::fmt(&self.literal, fmt)
2794 impl<'tcx> Display for ConstantKind<'tcx> {
2795 fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
2797 ConstantKind::Ty(c) => pretty_print_const(c, fmt, true),
2798 ConstantKind::Val(val, ty) => pretty_print_const_value(val, ty, fmt, true),
2803 fn pretty_print_const(
2804 c: &ty::Const<'tcx>,
2805 fmt: &mut Formatter<'_>,
2808 use crate::ty::print::PrettyPrinter;
2809 ty::tls::with(|tcx| {
2810 let literal = tcx.lift(c).unwrap();
2811 let mut cx = FmtPrinter::new(tcx, fmt, Namespace::ValueNS);
2812 cx.print_alloc_ids = true;
2813 cx.pretty_print_const(literal, print_types)?;
2818 fn pretty_print_const_value(
2819 val: interpret::ConstValue<'tcx>,
2821 fmt: &mut Formatter<'_>,
2824 use crate::ty::print::PrettyPrinter;
2825 ty::tls::with(|tcx| {
2826 let val = tcx.lift(val).unwrap();
2827 let ty = tcx.lift(ty).unwrap();
2828 let mut cx = FmtPrinter::new(tcx, fmt, Namespace::ValueNS);
2829 cx.print_alloc_ids = true;
2830 cx.pretty_print_const_value(val, ty, print_types)?;
2835 impl<'tcx> graph::DirectedGraph for Body<'tcx> {
2836 type Node = BasicBlock;
2839 impl<'tcx> graph::WithNumNodes for Body<'tcx> {
2841 fn num_nodes(&self) -> usize {
2842 self.basic_blocks.len()
2846 impl<'tcx> graph::WithStartNode for Body<'tcx> {
2848 fn start_node(&self) -> Self::Node {
2853 impl<'tcx> graph::WithSuccessors for Body<'tcx> {
2855 fn successors(&self, node: Self::Node) -> <Self as GraphSuccessors<'_>>::Iter {
2856 self.basic_blocks[node].terminator().successors().cloned()
2860 impl<'a, 'b> graph::GraphSuccessors<'b> for Body<'a> {
2861 type Item = BasicBlock;
2862 type Iter = iter::Cloned<Successors<'b>>;
2865 impl graph::GraphPredecessors<'graph> for Body<'tcx> {
2866 type Item = BasicBlock;
2867 type Iter = std::iter::Copied<std::slice::Iter<'graph, BasicBlock>>;
2870 impl graph::WithPredecessors for Body<'tcx> {
2872 fn predecessors(&self, node: Self::Node) -> <Self as graph::GraphPredecessors<'_>>::Iter {
2873 self.predecessors()[node].iter().copied()
2877 /// `Location` represents the position of the start of the statement; or, if
2878 /// `statement_index` equals the number of statements, then the start of the
2880 #[derive(Copy, Clone, PartialEq, Eq, Hash, Ord, PartialOrd, HashStable)]
2881 pub struct Location {
2882 /// The block that the location is within.
2883 pub block: BasicBlock,
2885 pub statement_index: usize,
2888 impl fmt::Debug for Location {
2889 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2890 write!(fmt, "{:?}[{}]", self.block, self.statement_index)
2895 pub const START: Location = Location { block: START_BLOCK, statement_index: 0 };
2897 /// Returns the location immediately after this one within the enclosing block.
2899 /// Note that if this location represents a terminator, then the
2900 /// resulting location would be out of bounds and invalid.
2901 pub fn successor_within_block(&self) -> Location {
2902 Location { block: self.block, statement_index: self.statement_index + 1 }
2905 /// Returns `true` if `other` is earlier in the control flow graph than `self`.
2906 pub fn is_predecessor_of<'tcx>(&self, other: Location, body: &Body<'tcx>) -> bool {
2907 // If we are in the same block as the other location and are an earlier statement
2908 // then we are a predecessor of `other`.
2909 if self.block == other.block && self.statement_index < other.statement_index {
2913 let predecessors = body.predecessors();
2915 // If we're in another block, then we want to check that block is a predecessor of `other`.
2916 let mut queue: Vec<BasicBlock> = predecessors[other.block].to_vec();
2917 let mut visited = FxHashSet::default();
2919 while let Some(block) = queue.pop() {
2920 // If we haven't visited this block before, then make sure we visit it's predecessors.
2921 if visited.insert(block) {
2922 queue.extend(predecessors[block].iter().cloned());
2927 // If we found the block that `self` is in, then we are a predecessor of `other` (since
2928 // we found that block by looking at the predecessors of `other`).
2929 if self.block == block {
2937 pub fn dominates(&self, other: Location, dominators: &Dominators<BasicBlock>) -> bool {
2938 if self.block == other.block {
2939 self.statement_index <= other.statement_index
2941 dominators.is_dominated_by(other.block, self.block)