1 //! MIR datatypes and passes. See the [rustc dev guide] for more info.
2 //!
3 //! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/mir/index.html
4 
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};
20 
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;
32 use std::borrow::Cow;
33 use std::convert::TryInto;
34 use std::fmt::{self, Debug, Display, Formatter, Write};
35 use std::ops::{ControlFlow, Index, IndexMut};
36 use std::slice;
37 use std::{iter, mem, option};
38 
39 use self::graph_cyclic_cache::GraphIsCyclicCache;
40 use self::predecessors::{PredecessorCache, Predecessors};
41 pub use self::query::*;
42 
43 pub mod coverage;
44 mod generic_graph;
45 pub mod generic_graphviz;
46 mod graph_cyclic_cache;
47 pub mod graphviz;
48 pub mod interpret;
49 pub mod mono;
50 pub mod patch;
51 mod predecessors;
52 pub mod pretty;
53 mod query;
54 pub mod spanview;
55 pub mod tcx;
56 pub mod terminator;
57 pub use terminator::*;
58 pub mod traversal;
59 mod type_foldable;
60 pub mod visit;
61 
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,
66 };
67 
68 /// Types for locals
69 pub type LocalDecls<'tcx> = IndexVec<Local, LocalDecl<'tcx>>;
70 
71 pub trait HasLocalDecls<'tcx> {
local_decls(&self) -> &LocalDecls<'tcx>72     fn local_decls(&self) -> &LocalDecls<'tcx>;
73 }
74 
75 impl<'tcx> HasLocalDecls<'tcx> for LocalDecls<'tcx> {
76     #[inline]
local_decls(&self) -> &LocalDecls<'tcx>77     fn local_decls(&self) -> &LocalDecls<'tcx> {
78         self
79     }
80 }
81 
82 impl<'tcx> HasLocalDecls<'tcx> for Body<'tcx> {
83     #[inline]
local_decls(&self) -> &LocalDecls<'tcx>84     fn local_decls(&self) -> &LocalDecls<'tcx> {
85         &self.local_decls
86     }
87 }
88 
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> {
name(&self) -> Cow<'_, str>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..])
97         } else {
98             Cow::from(name)
99         }
100     }
101 
run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>)102     fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>);
103 }
104 
105 /// The various "big phases" that MIR goes through.
106 ///
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.
109 ///
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.
112 ///
113 /// Warning: ordering of variants is significant.
114 #[derive(Copy, Clone, TyEncodable, TyDecodable, Debug, PartialEq, Eq, PartialOrd, Ord)]
115 #[derive(HashStable)]
116 pub enum MirPhase {
117     Build = 0,
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.
120     Const = 1,
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`
124     /// query.
125     ConstPromotion = 2,
126     /// After this phase
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.
131     DropLowering = 3,
132     /// After this phase, generators are explicit state machines (no more `Yield`).
133     /// `AggregateKind::Generator` is gone for good.
134     GeneratorLowering = 4,
135     Optimization = 5,
136 }
137 
138 impl MirPhase {
139     /// Gets the index of the current MirPhase within the set of all `MirPhase`s.
phase_index(&self) -> usize140     pub fn phase_index(&self) -> usize {
141         *self as usize
142     }
143 }
144 
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>,
150 
151     /// If `Some`, this is a promoted rvalue within the parent function.
152     pub promoted: Option<Promoted>,
153 }
154 
155 impl<'tcx> MirSource<'tcx> {
item(def_id: DefId) -> Self156     pub fn item(def_id: DefId) -> Self {
157         MirSource {
158             instance: InstanceDef::Item(ty::WithOptConstParam::unknown(def_id)),
159             promoted: None,
160         }
161     }
162 
from_instance(instance: InstanceDef<'tcx>) -> Self163     pub fn from_instance(instance: InstanceDef<'tcx>) -> Self {
164         MirSource { instance, promoted: None }
165     }
166 
with_opt_param(self) -> ty::WithOptConstParam<DefId>167     pub fn with_opt_param(self) -> ty::WithOptConstParam<DefId> {
168         self.instance.with_opt_param()
169     }
170 
171     #[inline]
def_id(&self) -> DefId172     pub fn def_id(&self) -> DefId {
173         self.instance.def_id()
174     }
175 }
176 
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>>,
181 
182     /// Generator drop glue.
183     pub generator_drop: Option<Body<'tcx>>,
184 
185     /// The layout of a generator. Produced by the state transformation.
186     pub generator_layout: Option<GeneratorLayout<'tcx>>,
187 
188     /// If this is a generator then record the type of source expression that caused this generator
189     /// to be created.
190     pub generator_kind: GeneratorKind,
191 }
192 
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>>,
199 
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.
205     pub phase: MirPhase,
206 
207     pub source: MirSource<'tcx>,
208 
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>>,
212 
213     pub generator: Option<Box<GeneratorInfo<'tcx>>>,
214 
215     /// Declarations of locals.
216     ///
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>,
221 
222     /// User type annotations.
223     pub user_type_annotations: ty::CanonicalUserTypeAnnotations<'tcx>,
224 
225     /// The number of arguments this function takes.
226     ///
227     /// Starting at local 1, `arg_count` locals will be provided by the caller
228     /// and can be assumed to be initialized.
229     ///
230     /// If this MIR was built for a constant, this will be 0.
231     pub arg_count: usize,
232 
233     /// Mark an argument local (which must be a tuple) as getting passed as
234     /// its individual components at the LLVM level.
235     ///
236     /// This is used for the "rust-call" ABI.
237     pub spread_arg: Option<Local>,
238 
239     /// Debug information pertaining to user variables, including captures.
240     pub var_debug_info: Vec<VarDebugInfo<'tcx>>,
241 
242     /// A span representing this MIR, for error reporting.
243     pub span: Span,
244 
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>>,
248 
249     /// Does this body use generic parameters. This is used for the `ConstEvaluatable` check.
250     ///
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`.
254     ///
255     /// ```rust
256     /// fn test<T>() {
257     ///     let _ = [0; std::mem::size_of::<*mut T>()];
258     /// }
259     /// ```
260     ///
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,
265 
266     predecessor_cache: PredecessorCache,
267     is_cyclic: GraphIsCyclicCache,
268 }
269 
270 impl<'tcx> Body<'tcx> {
new( tcx: TyCtxt<'tcx>, source: MirSource<'tcx>, basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>, source_scopes: IndexVec<SourceScope, SourceScopeData<'tcx>>, local_decls: LocalDecls<'tcx>, user_type_annotations: ty::CanonicalUserTypeAnnotations<'tcx>, arg_count: usize, var_debug_info: Vec<VarDebugInfo<'tcx>>, span: Span, generator_kind: Option<GeneratorKind>, ) -> Self271     pub fn new(
272         tcx: TyCtxt<'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>,
278         arg_count: usize,
279         var_debug_info: Vec<VarDebugInfo<'tcx>>,
280         span: Span,
281         generator_kind: Option<GeneratorKind>,
282     ) -> Self {
283         // We need `arg_count` locals, and one for the return place.
284         assert!(
285             local_decls.len() > arg_count,
286             "expected at least {} locals, got {}",
287             arg_count + 1,
288             local_decls.len()
289         );
290 
291         let mut body = Body {
292             phase: MirPhase::Build,
293             source,
294             basic_blocks,
295             source_scopes,
296             generator: generator_kind.map(|generator_kind| {
297                 Box::new(GeneratorInfo {
298                     yield_ty: None,
299                     generator_drop: None,
300                     generator_layout: None,
301                     generator_kind,
302                 })
303             }),
304             local_decls,
305             user_type_annotations,
306             arg_count,
307             spread_arg: None,
308             var_debug_info,
309             span,
310             required_consts: Vec::new(),
311             is_polymorphic: false,
312             predecessor_cache: PredecessorCache::new(),
313             is_cyclic: GraphIsCyclicCache::new(),
314         };
315         body.is_polymorphic = body.definitely_has_param_types_or_consts(tcx);
316         body
317     }
318 
319     /// Returns a partially initialized MIR body containing only a list of basic blocks.
320     ///
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
323     /// crate.
new_cfg_only(basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>) -> Self324     pub fn new_cfg_only(basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>) -> Self {
325         Body {
326             phase: MirPhase::Build,
327             source: MirSource::item(DefId::local(CRATE_DEF_INDEX)),
328             basic_blocks,
329             source_scopes: IndexVec::new(),
330             generator: None,
331             local_decls: IndexVec::new(),
332             user_type_annotations: IndexVec::new(),
333             arg_count: 0,
334             spread_arg: None,
335             span: DUMMY_SP,
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(),
341         }
342     }
343 
344     #[inline]
basic_blocks(&self) -> &IndexVec<BasicBlock, BasicBlockData<'tcx>>345     pub fn basic_blocks(&self) -> &IndexVec<BasicBlock, BasicBlockData<'tcx>> {
346         &self.basic_blocks
347     }
348 
349     #[inline]
basic_blocks_mut(&mut self) -> &mut 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.
353         //
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
359     }
360 
361     #[inline]
basic_blocks_and_local_decls_mut( &mut self, ) -> (&mut IndexVec<BasicBlock, BasicBlockData<'tcx>>, &mut LocalDecls<'tcx>)362     pub fn basic_blocks_and_local_decls_mut(
363         &mut self,
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)
368     }
369 
370     #[inline]
basic_blocks_local_decls_mut_and_var_debug_info( &mut self, ) -> ( &mut IndexVec<BasicBlock, BasicBlockData<'tcx>>, &mut LocalDecls<'tcx>, &mut Vec<VarDebugInfo<'tcx>>, )371     pub fn basic_blocks_local_decls_mut_and_var_debug_info(
372         &mut self,
373     ) -> (
374         &mut IndexVec<BasicBlock, BasicBlockData<'tcx>>,
375         &mut LocalDecls<'tcx>,
376         &mut Vec<VarDebugInfo<'tcx>>,
377     ) {
378         self.predecessor_cache.invalidate();
379         self.is_cyclic.invalidate();
380         (&mut self.basic_blocks, &mut self.local_decls, &mut self.var_debug_info)
381     }
382 
383     /// Returns `true` if a cycle exists in the control-flow graph that is reachable from the
384     /// `START_BLOCK`.
is_cfg_cyclic(&self) -> bool385     pub fn is_cfg_cyclic(&self) -> bool {
386         self.is_cyclic.is_cyclic(self)
387     }
388 
389     #[inline]
local_kind(&self, local: Local) -> LocalKind390     pub fn local_kind(&self, local: Local) -> LocalKind {
391         let index = local.as_usize();
392         if index == 0 {
393             debug_assert!(
394                 self.local_decls[local].mutability == Mutability::Mut,
395                 "return place should be mutable"
396             );
397 
398             LocalKind::ReturnPointer
399         } else if index < self.arg_count + 1 {
400             LocalKind::Arg
401         } else if self.local_decls[local].is_user_variable() {
402             LocalKind::Var
403         } else {
404             LocalKind::Temp
405         }
406     }
407 
408     /// Returns an iterator over all user-declared mutable locals.
409     #[inline]
mut_vars_iter<'a>(&'a self) -> impl Iterator<Item = Local> + 'a410     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 {
415                 Some(local)
416             } else {
417                 None
418             }
419         })
420     }
421 
422     /// Returns an iterator over all user-declared mutable arguments and locals.
423     #[inline]
mut_vars_and_args_iter<'a>(&'a self) -> impl Iterator<Item = Local> + 'a424     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
430             {
431                 Some(local)
432             } else {
433                 None
434             }
435         })
436     }
437 
438     /// Returns an iterator over all function arguments.
439     #[inline]
args_iter(&self) -> impl Iterator<Item = Local> + ExactSizeIterator440     pub fn args_iter(&self) -> impl Iterator<Item = Local> + ExactSizeIterator {
441         (1..self.arg_count + 1).map(Local::new)
442     }
443 
444     /// Returns an iterator over all user-defined variables and compiler-generated temporaries (all
445     /// locals that are neither arguments nor the return place).
446     #[inline]
vars_and_temps_iter( &self, ) -> impl DoubleEndedIterator<Item = Local> + ExactSizeIterator447     pub fn vars_and_temps_iter(
448         &self,
449     ) -> impl DoubleEndedIterator<Item = Local> + ExactSizeIterator {
450         (self.arg_count + 1..self.local_decls.len()).map(Local::new)
451     }
452 
453     #[inline]
drain_vars_and_temps<'a>(&'a mut self) -> impl Iterator<Item = LocalDecl<'tcx>> + 'a454     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..)
456     }
457 
458     /// Changes a statement to a nop. This is both faster than deleting instructions and avoids
459     /// invalidating statement indices in `Location`s.
make_statement_nop(&mut self, location: Location)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()
464     }
465 
466     /// Returns the source info associated with `location`.
source_info(&self, location: Location) -> &SourceInfo467     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
473         } else {
474             assert_eq!(idx, stmts.len());
475             &block.terminator().source_info
476         }
477     }
478 
479     /// Returns the return type; it always return first element from `local_decls` array.
480     #[inline]
return_ty(&self) -> Ty<'tcx>481     pub fn return_ty(&self) -> Ty<'tcx> {
482         self.local_decls[RETURN_PLACE].ty
483     }
484 
485     /// Gets the location of the terminator for the given block.
486     #[inline]
terminator_loc(&self, bb: BasicBlock) -> Location487     pub fn terminator_loc(&self, bb: BasicBlock) -> Location {
488         Location { block: bb, statement_index: self[bb].statements.len() }
489     }
490 
491     #[inline]
predecessors(&self) -> &Predecessors492     pub fn predecessors(&self) -> &Predecessors {
493         self.predecessor_cache.compute(&self.basic_blocks)
494     }
495 
496     #[inline]
dominators(&self) -> Dominators<BasicBlock>497     pub fn dominators(&self) -> Dominators<BasicBlock> {
498         dominators(self)
499     }
500 
501     #[inline]
yield_ty(&self) -> Option<Ty<'tcx>>502     pub fn yield_ty(&self) -> Option<Ty<'tcx>> {
503         self.generator.as_ref().and_then(|generator| generator.yield_ty)
504     }
505 
506     #[inline]
generator_layout(&self) -> Option<&GeneratorLayout<'tcx>>507     pub fn generator_layout(&self) -> Option<&GeneratorLayout<'tcx>> {
508         self.generator.as_ref().and_then(|generator| generator.generator_layout.as_ref())
509     }
510 
511     #[inline]
generator_drop(&self) -> Option<&Body<'tcx>>512     pub fn generator_drop(&self) -> Option<&Body<'tcx>> {
513         self.generator.as_ref().and_then(|generator| generator.generator_drop.as_ref())
514     }
515 
516     #[inline]
generator_kind(&self) -> Option<GeneratorKind>517     pub fn generator_kind(&self) -> Option<GeneratorKind> {
518         self.generator.as_ref().map(|generator| generator.generator_kind)
519     }
520 }
521 
522 #[derive(Copy, Clone, PartialEq, Eq, Debug, TyEncodable, TyDecodable, HashStable)]
523 pub enum Safety {
524     Safe,
525     /// Unsafe because of compiler-generated unsafe code, like `await` desugaring
526     BuiltinUnsafe,
527     /// Unsafe because of an unsafe fn
528     FnUnsafe,
529     /// Unsafe because of an `unsafe` block
530     ExplicitUnsafe(hir::HirId),
531 }
532 
533 impl<'tcx> Index<BasicBlock> for Body<'tcx> {
534     type Output = BasicBlockData<'tcx>;
535 
536     #[inline]
index(&self, index: BasicBlock) -> &BasicBlockData<'tcx>537     fn index(&self, index: BasicBlock) -> &BasicBlockData<'tcx> {
538         &self.basic_blocks()[index]
539     }
540 }
541 
542 impl<'tcx> IndexMut<BasicBlock> for Body<'tcx> {
543     #[inline]
index_mut(&mut self, index: BasicBlock) -> &mut BasicBlockData<'tcx>544     fn index_mut(&mut self, index: BasicBlock) -> &mut BasicBlockData<'tcx> {
545         &mut self.basic_blocks_mut()[index]
546     }
547 }
548 
549 #[derive(Copy, Clone, Debug, HashStable, TypeFoldable)]
550 pub enum ClearCrossCrate<T> {
551     Clear,
552     Set(T),
553 }
554 
555 impl<T> ClearCrossCrate<T> {
as_ref(&self) -> ClearCrossCrate<&T>556     pub fn as_ref(&self) -> ClearCrossCrate<&T> {
557         match self {
558             ClearCrossCrate::Clear => ClearCrossCrate::Clear,
559             ClearCrossCrate::Set(v) => ClearCrossCrate::Set(v),
560         }
561     }
562 
assert_crate_local(self) -> T563     pub fn assert_crate_local(self) -> T {
564         match self {
565             ClearCrossCrate::Clear => bug!("unwrapping cross-crate data"),
566             ClearCrossCrate::Set(v) => v,
567         }
568     }
569 }
570 
571 const TAG_CLEAR_CROSS_CRATE_CLEAR: u8 = 0;
572 const TAG_CLEAR_CROSS_CRATE_SET: u8 = 1;
573 
574 impl<'tcx, E: TyEncoder<'tcx>, T: Encodable<E>> Encodable<E> for ClearCrossCrate<T> {
575     #[inline]
encode(&self, e: &mut E) -> Result<(), E::Error>576     fn encode(&self, e: &mut E) -> Result<(), E::Error> {
577         if E::CLEAR_CROSS_CRATE {
578             return Ok(());
579         }
580 
581         match *self {
582             ClearCrossCrate::Clear => TAG_CLEAR_CROSS_CRATE_CLEAR.encode(e),
583             ClearCrossCrate::Set(ref val) => {
584                 TAG_CLEAR_CROSS_CRATE_SET.encode(e)?;
585                 val.encode(e)
586             }
587         }
588     }
589 }
590 impl<'tcx, D: TyDecoder<'tcx>, T: Decodable<D>> Decodable<D> for ClearCrossCrate<T> {
591     #[inline]
decode(d: &mut D) -> Result<ClearCrossCrate<T>, D::Error>592     fn decode(d: &mut D) -> Result<ClearCrossCrate<T>, D::Error> {
593         if D::CLEAR_CROSS_CRATE {
594             return Ok(ClearCrossCrate::Clear);
595         }
596 
597         let discr = u8::decode(d)?;
598 
599         match discr {
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))
604             }
605             tag => Err(d.error(&format!("Invalid tag for ClearCrossCrate: {:?}", tag))),
606         }
607     }
608 }
609 
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.
618     pub span: Span,
619 
620     /// The source scope, keeping track of which bindings can be
621     /// seen by debuginfo, active lint levels, `unsafe {...}`, etc.
622     pub scope: SourceScope,
623 }
624 
625 impl SourceInfo {
626     #[inline]
outermost(span: Span) -> Self627     pub fn outermost(span: Span) -> Self {
628         SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE }
629     }
630 }
631 
632 ///////////////////////////////////////////////////////////////////////////
633 // Borrow kinds
634 
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.
639     Shared,
640 
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`.
644     ///
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:
648     ///
649     ///     let mut x = &Some(0);
650     ///     match *x {
651     ///         None => (),
652     ///         Some(_) if { x = &None; false } => (),
653     ///         Some(_) => (),
654     ///     }
655     ///
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.
660     Shallow,
661 
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.:
666     ///
667     ///     let x: &mut isize = ...;
668     ///     let y = || *x += 5;
669     ///
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:
672     ///
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; }
677     ///
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:
681     ///
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; }
686     ///
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.
692     ///
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.
697     Unique,
698 
699     /// Data is mutable and not aliasable.
700     Mut {
701         /// `true` if this borrow arose from method-call auto-ref
702         /// (i.e., `adjustment::Adjust::Borrow`).
703         allow_two_phase_borrow: bool,
704     },
705 }
706 
707 impl BorrowKind {
allows_two_phase_borrow(&self) -> bool708     pub fn allows_two_phase_borrow(&self) -> bool {
709         match *self {
710             BorrowKind::Shared | BorrowKind::Shallow | BorrowKind::Unique => false,
711             BorrowKind::Mut { allow_two_phase_borrow } => allow_two_phase_borrow,
712         }
713     }
714 
describe_mutability(&self) -> String715     pub fn describe_mutability(&self) -> String {
716         match *self {
717             BorrowKind::Shared | BorrowKind::Shallow | BorrowKind::Unique => {
718                 "immutable".to_string()
719             }
720             BorrowKind::Mut { .. } => "mutable".to_string(),
721         }
722     }
723 }
724 
725 ///////////////////////////////////////////////////////////////////////////
726 // Variables and temps
727 
728 rustc_index::newtype_index! {
729     pub struct Local {
730         derive [HashStable]
731         DEBUG_FORMAT = "_{}",
732         const RETURN_PLACE = 0,
733     }
734 }
735 
736 impl Atom for Local {
index(self) -> usize737     fn index(self) -> usize {
738         Idx::index(self)
739     }
740 }
741 
742 /// Classifies locals into categories. See `Body::local_kind`.
743 #[derive(Clone, Copy, PartialEq, Eq, Debug, HashStable)]
744 pub enum LocalKind {
745     /// User-declared variable binding.
746     Var,
747     /// Compiler-introduced temporary.
748     Temp,
749     /// Function argument.
750     Arg,
751     /// Location of function's return value.
752     ReturnPointer,
753 }
754 
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.
761     ///
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
771     ///     for diagnostics.
772     pub opt_match_place: Option<(Option<Place<'tcx>>, Span)>,
773     /// The span of the pattern in which this variable was bound.
774     pub pat_span: Span,
775 }
776 
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.
784     RefForGuard,
785 }
786 
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);`.
791     Imm,
792     /// Represents a `fn x(mut self);`.
793     Mut,
794     /// Represents a `fn x(&self);`.
795     ImmRef,
796     /// Represents a `fn x(&mut self);`.
797     MutRef,
798     /// Represents when a function does not have a self argument or
799     /// when a function has a `self: X` argument.
800     None,
801 }
802 
803 TrivialTypeFoldableAndLiftImpls! { BindingForm<'tcx>, }
804 
805 mod binding_form_impl {
806     use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
807     use rustc_query_system::ich::StableHashingContext;
808 
809     impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for super::BindingForm<'tcx> {
hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher)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);
813 
814             match self {
815                 Var(binding) => binding.hash_stable(hcx, hasher),
816                 ImplicitSelf(kind) => kind.hash_stable(hcx, hasher),
817                 RefForGuard => (),
818             }
819         }
820     }
821 }
822 
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 }`.
826 ///
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.
835     ///
836     /// Examples include `{ ...; tail };` and `let _ = { ...; tail };`
837     /// but not e.g., `let _x = { ...; tail };`
838     pub tail_result_is_ignored: bool,
839 
840     /// `Span` of the tail expression.
841     pub span: Span,
842 }
843 
844 /// A MIR local.
845 ///
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`).
851     ///
852     /// Temporaries and the return place are always mutable.
853     pub mutability: Mutability,
854 
855     // FIXME(matthewjasper) Don't store in this in `Body`
856     pub local_info: Option<Box<LocalInfo<'tcx>>>,
857 
858     /// `true` if this is an internal local.
859     ///
860     /// These locals are not based on types in the source code and are only used
861     /// for a few desugarings at the moment.
862     ///
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
867     /// after typeck.
868     ///
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
871     /// generator.
872     pub internal: bool,
873 
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>,
880 
881     /// The type of this local.
882     pub ty: Ty<'tcx>,
883 
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>>,
890 
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
894     /// of it.
895     ///
896     /// This is needed because the visibility source scope of locals within
897     /// a let-statement is weird.
898     ///
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.
902     ///
903     /// That's it, if we have a let-statement like the one in this
904     /// function:
905     ///
906     /// ```
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();
911     ///         y + 2
912     ///     };
913     ///     drop(x);
914     /// }
915     /// ```
916     ///
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.
920     ///
921     /// However, from a name lookup point of view, the scopes look more like
922     /// as if the let-statements were `match` expressions:
923     ///
924     /// ```
925     /// fn foo(x: &str) {
926     ///     match {
927     ///         match x.parse().unwrap() {
928     ///             y => y + 2
929     ///         }
930     ///     } {
931     ///         x => drop(x)
932     ///     };
933     /// }
934     /// ```
935     ///
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`.
940     ///
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).
946     ///
947     /// The end result looks like this:
948     ///
949     /// ```text
950     /// ROOT SCOPE
951     ///  │{ argument x: &str }
952     ///  │
953     ///  │ │{ #[allow(unused_mut)] } // This is actually split into 2 scopes
954     ///  │ │                         // in practice because I'm lazy.
955     ///  │ │
956     ///  │ │← x.source_info.scope
957     ///  │ │← `x.parse().unwrap()`
958     ///  │ │
959     ///  │ │ │← y.source_info.scope
960     ///  │ │
961     ///  │ │ │{ let y: u32 }
962     ///  │ │ │
963     ///  │ │ │← y.var_debug_info.source_info.scope
964     ///  │ │ │← `y + 2`
965     ///  │
966     ///  │ │{ let x: u32 }
967     ///  │ │← x.var_debug_info.source_info.scope
968     ///  │ │← `drop(x)` // This accesses `x: u32`.
969     /// ```
970     pub source_info: SourceInfo,
971 }
972 
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);
976 
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.
980 ///
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
986     ///
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 }`)
997     AggregateTemp,
998 }
999 
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) => ... }`
can_be_made_mutable(&self) -> bool1007     pub fn can_be_made_mutable(&self) -> bool {
1008         matches!(
1009             self.local_info,
1010             Some(box LocalInfo::User(ClearCrossCrate::Set(
1011                 BindingForm::Var(VarBindingForm {
1012                     binding_mode: ty::BindingMode::BindByValue(_),
1013                     opt_ty_info: _,
1014                     opt_match_place: _,
1015                     pat_span: _,
1016                 }) | BindingForm::ImplicitSelf(ImplicitSelfKind::Imm),
1017             )))
1018         )
1019     }
1020 
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).
is_nonref_binding(&self) -> bool1024     pub fn is_nonref_binding(&self) -> bool {
1025         matches!(
1026             self.local_info,
1027             Some(box LocalInfo::User(ClearCrossCrate::Set(
1028                 BindingForm::Var(VarBindingForm {
1029                     binding_mode: ty::BindingMode::BindByValue(_),
1030                     opt_ty_info: _,
1031                     opt_match_place: _,
1032                     pat_span: _,
1033                 }) | BindingForm::ImplicitSelf(_),
1034             )))
1035         )
1036     }
1037 
1038     /// Returns `true` if this variable is a named variable or function
1039     /// parameter declared by the user.
1040     #[inline]
is_user_variable(&self) -> bool1041     pub fn is_user_variable(&self) -> bool {
1042         matches!(self.local_info, Some(box LocalInfo::User(_)))
1043     }
1044 
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
1047     /// match arm.
is_ref_for_guard(&self) -> bool1048     pub fn is_ref_for_guard(&self) -> bool {
1049         matches!(
1050             self.local_info,
1051             Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::RefForGuard)))
1052         )
1053     }
1054 
1055     /// Returns `Some` if this is a reference to a static item that is used to
1056     /// access that static.
is_ref_to_static(&self) -> bool1057     pub fn is_ref_to_static(&self) -> bool {
1058         matches!(self.local_info, Some(box LocalInfo::StaticRef { .. }))
1059     }
1060 
1061     /// Returns `Some` if this is a reference to a thread-local static item that is used to
1062     /// access that static.
is_ref_to_thread_local(&self) -> bool1063     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,
1066             _ => false,
1067         }
1068     }
1069 
1070     /// Returns `true` is the local is from a compiler desugaring, e.g.,
1071     /// `__next` from a `for` loop.
1072     #[inline]
from_compiler_desugaring(&self) -> bool1073     pub fn from_compiler_desugaring(&self) -> bool {
1074         self.source_info.span.desugaring_kind().is_some()
1075     }
1076 
1077     /// Creates a new `LocalDecl` for a temporary: mutable, non-internal.
1078     #[inline]
new(ty: Ty<'tcx>, span: Span) -> Self1079     pub fn new(ty: Ty<'tcx>, span: Span) -> Self {
1080         Self::with_source_info(ty, SourceInfo::outermost(span))
1081     }
1082 
1083     /// Like `LocalDecl::new`, but takes a `SourceInfo` instead of a `Span`.
1084     #[inline]
with_source_info(ty: Ty<'tcx>, source_info: SourceInfo) -> Self1085     pub fn with_source_info(ty: Ty<'tcx>, source_info: SourceInfo) -> Self {
1086         LocalDecl {
1087             mutability: Mutability::Mut,
1088             local_info: None,
1089             internal: false,
1090             is_block_tail: None,
1091             ty,
1092             user_ty: None,
1093             source_info,
1094         }
1095     }
1096 
1097     /// Converts `self` into same `LocalDecl` except tagged as internal.
1098     #[inline]
internal(mut self) -> Self1099     pub fn internal(mut self) -> Self {
1100         self.internal = true;
1101         self
1102     }
1103 
1104     /// Converts `self` into same `LocalDecl` except tagged as immutable.
1105     #[inline]
immutable(mut self) -> Self1106     pub fn immutable(mut self) -> Self {
1107         self.mutability = Mutability::Not;
1108         self
1109     }
1110 
1111     /// Converts `self` into same `LocalDecl` except tagged as internal temporary.
1112     #[inline]
block_tail(mut self, info: BlockTailInfo) -> Self1113     pub fn block_tail(mut self, info: BlockTailInfo) -> Self {
1114         assert!(self.is_block_tail.is_none());
1115         self.is_block_tail = Some(info);
1116         self
1117     }
1118 }
1119 
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.
1124     Place(Place<'tcx>),
1125     Const(Constant<'tcx>),
1126 }
1127 
1128 impl<'tcx> Debug for VarDebugInfoContents<'tcx> {
fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result1129     fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
1130         match self {
1131             VarDebugInfoContents::Const(c) => write!(fmt, "{}", c),
1132             VarDebugInfoContents::Place(p) => write!(fmt, "{:?}", p),
1133         }
1134     }
1135 }
1136 
1137 /// Debug information pertaining to a user variable.
1138 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
1139 pub struct VarDebugInfo<'tcx> {
1140     pub name: Symbol,
1141 
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,
1146 
1147     /// Where the data for this user variable is to be found.
1148     pub value: VarDebugInfoContents<'tcx>,
1149 }
1150 
1151 ///////////////////////////////////////////////////////////////////////////
1152 // BasicBlock
1153 
1154 rustc_index::newtype_index! {
1155     /// A node in the MIR [control-flow graph][CFG].
1156     ///
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.
1160     ///
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.
1166     ///
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`].
1169     ///
1170     /// Read more about basic blocks in the [rustc-dev-guide][guide-mir].
1171     ///
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 {
1178         derive [HashStable]
1179         DEBUG_FORMAT = "bb{}",
1180         const START_BLOCK = 0,
1181     }
1182 }
1183 
1184 impl BasicBlock {
start_location(self) -> Location1185     pub fn start_location(self) -> Location {
1186         Location { block: self, statement_index: 0 }
1187     }
1188 }
1189 
1190 ///////////////////////////////////////////////////////////////////////////
1191 // BasicBlockData and Terminator
1192 
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>>,
1198 
1199     /// Terminator for this block.
1200     ///
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>>,
1208 
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,
1214 }
1215 
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),
1221     OverflowNeg(O),
1222     DivisionByZero(O),
1223     RemainderByZero(O),
1224     ResumedAfterReturn(GeneratorKind),
1225     ResumedAfterPanic(GeneratorKind),
1226 }
1227 
1228 #[derive(
1229     Clone,
1230     Debug,
1231     PartialEq,
1232     PartialOrd,
1233     TyEncodable,
1234     TyDecodable,
1235     Hash,
1236     HashStable,
1237     TypeFoldable
1238 )]
1239 pub enum InlineAsmOperand<'tcx> {
1240     In {
1241         reg: InlineAsmRegOrRegClass,
1242         value: Operand<'tcx>,
1243     },
1244     Out {
1245         reg: InlineAsmRegOrRegClass,
1246         late: bool,
1247         place: Option<Place<'tcx>>,
1248     },
1249     InOut {
1250         reg: InlineAsmRegOrRegClass,
1251         late: bool,
1252         in_value: Operand<'tcx>,
1253         out_place: Option<Place<'tcx>>,
1254     },
1255     Const {
1256         value: Box<Constant<'tcx>>,
1257     },
1258     SymFn {
1259         value: Box<Constant<'tcx>>,
1260     },
1261     SymStatic {
1262         def_id: DefId,
1263     },
1264 }
1265 
1266 /// Type for MIR `Assert` terminator error messages.
1267 pub type AssertMessage<'tcx> = AssertKind<Operand<'tcx>>;
1268 
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>>;
1273 
1274 impl<'tcx> BasicBlockData<'tcx> {
new(terminator: Option<Terminator<'tcx>>) -> BasicBlockData<'tcx>1275     pub fn new(terminator: Option<Terminator<'tcx>>) -> BasicBlockData<'tcx> {
1276         BasicBlockData { statements: vec![], terminator, is_cleanup: false }
1277     }
1278 
1279     /// Accessor for terminator.
1280     ///
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.
1283     #[inline]
terminator(&self) -> &Terminator<'tcx>1284     pub fn terminator(&self) -> &Terminator<'tcx> {
1285         self.terminator.as_ref().expect("invalid terminator state")
1286     }
1287 
1288     #[inline]
terminator_mut(&mut self) -> &mut Terminator<'tcx>1289     pub fn terminator_mut(&mut self) -> &mut Terminator<'tcx> {
1290         self.terminator.as_mut().expect("invalid terminator state")
1291     }
1292 
retain_statements<F>(&mut self, mut f: F) where F: FnMut(&mut Statement<'_>) -> bool,1293     pub fn retain_statements<F>(&mut self, mut f: F)
1294     where
1295         F: FnMut(&mut Statement<'_>) -> bool,
1296     {
1297         for s in &mut self.statements {
1298             if !f(s) {
1299                 s.make_nop();
1300             }
1301         }
1302     }
1303 
expand_statements<F, I>(&mut self, mut f: F) where F: FnMut(&mut Statement<'tcx>) -> Option<I>, I: iter::TrustedLen<Item = Statement<'tcx>>,1304     pub fn expand_statements<F, I>(&mut self, mut f: F)
1305     where
1306         F: FnMut(&mut Statement<'tcx>) -> Option<I>,
1307         I: iter::TrustedLen<Item = Statement<'tcx>>,
1308     {
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.
1316                     *s = first;
1317 
1318                     // Save the other statements for optimized splicing.
1319                     let remaining = new_stmts.size_hint().0;
1320                     if remaining > 0 {
1321                         splices.push((i + 1 + extra_stmts, new_stmts));
1322                         extra_stmts += remaining;
1323                     }
1324                 } else {
1325                     s.make_nop();
1326                 }
1327             }
1328         }
1329 
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(
1338             gap.end,
1339             Statement { source_info: SourceInfo::outermost(DUMMY_SP), kind: StatementKind::Nop },
1340         );
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 {
1344                 gap.start -= 1;
1345                 gap.end -= 1;
1346                 self.statements.swap(gap.start, gap.end);
1347             }
1348             self.statements.splice(splice_start..splice_end, new_stmts);
1349             gap.end = splice_start;
1350         }
1351     }
1352 
visitable(&self, index: usize) -> &dyn MirVisitable<'tcx>1353     pub fn visitable(&self, index: usize) -> &dyn MirVisitable<'tcx> {
1354         if index < self.statements.len() { &self.statements[index] } else { &self.terminator }
1355     }
1356 }
1357 
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.
description(&self) -> &'static str1362     pub fn description(&self) -> &'static str {
1363         use AssertKind::*;
1364         match self {
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"),
1381         }
1382     }
1383 
1384     /// Format the message arguments for the `assert(cond, msg..)` terminator in MIR printing.
fmt_assert_args<W: Write>(&self, f: &mut W) -> fmt::Result where O: Debug,1385     pub fn fmt_assert_args<W: Write>(&self, f: &mut W) -> fmt::Result
1386     where
1387         O: Debug,
1388     {
1389         use AssertKind::*;
1390         match self {
1391             BoundsCheck { ref len, ref index } => write!(
1392                 f,
1393                 "\"index out of bounds: the length is {{}} but the index is {{}}\", {:?}, {:?}",
1394                 len, index
1395             ),
1396 
1397             OverflowNeg(op) => {
1398                 write!(f, "\"attempt to negate `{{}}`, which would overflow\", {:?}", op)
1399             }
1400             DivisionByZero(op) => write!(f, "\"attempt to divide `{{}}` by zero\", {:?}", op),
1401             RemainderByZero(op) => write!(
1402                 f,
1403                 "\"attempt to calculate the remainder of `{{}}` with a divisor of zero\", {:?}",
1404                 op
1405             ),
1406             Overflow(BinOp::Add, l, r) => write!(
1407                 f,
1408                 "\"attempt to compute `{{}} + {{}}`, which would overflow\", {:?}, {:?}",
1409                 l, r
1410             ),
1411             Overflow(BinOp::Sub, l, r) => write!(
1412                 f,
1413                 "\"attempt to compute `{{}} - {{}}`, which would overflow\", {:?}, {:?}",
1414                 l, r
1415             ),
1416             Overflow(BinOp::Mul, l, r) => write!(
1417                 f,
1418                 "\"attempt to compute `{{}} * {{}}`, which would overflow\", {:?}, {:?}",
1419                 l, r
1420             ),
1421             Overflow(BinOp::Div, l, r) => write!(
1422                 f,
1423                 "\"attempt to compute `{{}} / {{}}`, which would overflow\", {:?}, {:?}",
1424                 l, r
1425             ),
1426             Overflow(BinOp::Rem, l, r) => write!(
1427                 f,
1428                 "\"attempt to compute the remainder of `{{}} % {{}}`, which would overflow\", {:?}, {:?}",
1429                 l, r
1430             ),
1431             Overflow(BinOp::Shr, _, r) => {
1432                 write!(f, "\"attempt to shift right by `{{}}`, which would overflow\", {:?}", r)
1433             }
1434             Overflow(BinOp::Shl, _, r) => {
1435                 write!(f, "\"attempt to shift left by `{{}}`, which would overflow\", {:?}", r)
1436             }
1437             _ => write!(f, "\"{}\"", self.description()),
1438         }
1439     }
1440 }
1441 
1442 impl<O: fmt::Debug> fmt::Debug for AssertKind<O> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result1443     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1444         use AssertKind::*;
1445         match self {
1446             BoundsCheck { ref len, ref index } => write!(
1447                 f,
1448                 "index out of bounds: the length is {:?} but the index is {:?}",
1449                 len, index
1450             ),
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!(
1454                 f,
1455                 "attempt to calculate the remainder of `{:#?}` with a divisor of zero",
1456                 op
1457             ),
1458             Overflow(BinOp::Add, l, r) => {
1459                 write!(f, "attempt to compute `{:#?} + {:#?}`, which would overflow", l, r)
1460             }
1461             Overflow(BinOp::Sub, l, r) => {
1462                 write!(f, "attempt to compute `{:#?} - {:#?}`, which would overflow", l, r)
1463             }
1464             Overflow(BinOp::Mul, l, r) => {
1465                 write!(f, "attempt to compute `{:#?} * {:#?}`, which would overflow", l, r)
1466             }
1467             Overflow(BinOp::Div, l, r) => {
1468                 write!(f, "attempt to compute `{:#?} / {:#?}`, which would overflow", l, r)
1469             }
1470             Overflow(BinOp::Rem, l, r) => write!(
1471                 f,
1472                 "attempt to compute the remainder of `{:#?} % {:#?}`, which would overflow",
1473                 l, r
1474             ),
1475             Overflow(BinOp::Shr, _, r) => {
1476                 write!(f, "attempt to shift right by `{:#?}`, which would overflow", r)
1477             }
1478             Overflow(BinOp::Shl, _, r) => {
1479                 write!(f, "attempt to shift left by `{:#?}`, which would overflow", r)
1480             }
1481             _ => write!(f, "{}", self.description()),
1482         }
1483     }
1484 }
1485 
1486 ///////////////////////////////////////////////////////////////////////////
1487 // Statements
1488 
1489 #[derive(Clone, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
1490 pub struct Statement<'tcx> {
1491     pub source_info: SourceInfo,
1492     pub kind: StatementKind<'tcx>,
1493 }
1494 
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);
1498 
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.
make_nop(&mut self)1502     pub fn make_nop(&mut self) {
1503         self.kind = StatementKind::Nop
1504     }
1505 
1506     /// Changes a statement to a nop and returns the original statement.
replace_nop(&mut self) -> Self1507     pub fn replace_nop(&mut self) -> Self {
1508         Statement {
1509             source_info: self.source_info,
1510             kind: mem::replace(&mut self.kind, StatementKind::Nop),
1511         }
1512     }
1513 }
1514 
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>)>),
1519 
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.
1524     ///
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>)>),
1528 
1529     /// Write the discriminant for a variant to the enum Place.
1530     SetDiscriminant { place: Box<Place<'tcx>>, variant_index: VariantIdx },
1531 
1532     /// Start a live range for the storage of the local.
1533     StorageLive(Local),
1534 
1535     /// End the current live range for the storage of the local.
1536     StorageDead(Local),
1537 
1538     /// Executes a piece of inline Assembly. Stored in a Box to keep the size
1539     /// of `StatementKind` low.
1540     LlvmInlineAsm(Box<LlvmInlineAsm<'tcx>>),
1541 
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>>),
1548 
1549     /// Encodes a user's type ascription. These need to be preserved
1550     /// intact so that NLL can respect them. For example:
1551     ///
1552     ///     let a: T = y;
1553     ///
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:
1556     ///
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),
1562 
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
1567     /// executed.
1568     Coverage(Box<Coverage>),
1569 
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>>),
1574 
1575     /// No-op. Useful for deleting instructions without affecting statement indices.
1576     Nop,
1577 }
1578 
1579 impl<'tcx> StatementKind<'tcx> {
as_assign_mut(&mut self) -> Option<&mut (Place<'tcx>, Rvalue<'tcx>)>1580     pub fn as_assign_mut(&mut self) -> Option<&mut (Place<'tcx>, Rvalue<'tcx>)> {
1581         match self {
1582             StatementKind::Assign(x) => Some(x),
1583             _ => None,
1584         }
1585     }
1586 
as_assign(&self) -> Option<&(Place<'tcx>, Rvalue<'tcx>)>1587     pub fn as_assign(&self) -> Option<&(Place<'tcx>, Rvalue<'tcx>)> {
1588         match self {
1589             StatementKind::Assign(x) => Some(x),
1590             _ => None,
1591         }
1592     }
1593 }
1594 
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.
1599     FnEntry,
1600     /// Retag preparing for a two-phase borrow.
1601     TwoPhase,
1602     /// Retagging raw pointers.
1603     Raw,
1604     /// A "normal" retag.
1605     Default,
1606 }
1607 
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
1612     /// code.
1613     ///
1614     /// This should ensure that you cannot change the variant for an enum while
1615     /// you are in the midst of matching on it.
1616     ForMatchGuard,
1617 
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.
1620     ///
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>),
1626 
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.
1630     ForGuardBinding,
1631 
1632     /// Officially, the semantics of
1633     ///
1634     /// `let pattern = <expr>;`
1635     ///
1636     /// is that `<expr>` is evaluated into a temporary and then this temporary is
1637     /// into the pattern.
1638     ///
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.
1644     ///
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>),
1650 
1651     /// If we have an index expression like
1652     ///
1653     /// (*x)[1][{ x = y; 4}]
1654     ///
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.
1658     ForIndex,
1659 }
1660 
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>)]>,
1666 }
1667 
1668 impl Debug for Statement<'_> {
fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result1669     fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
1670         use self::StatementKind::*;
1671         match self.kind {
1672             Assign(box (ref place, ref rv)) => write!(fmt, "{:?} = {:?}", place, rv),
1673             FakeRead(box (ref cause, ref place)) => {
1674                 write!(fmt, "FakeRead({:?}, {:?})", cause, place)
1675             }
1676             Retag(ref kind, ref place) => write!(
1677                 fmt,
1678                 "Retag({}{:?})",
1679                 match kind {
1680                     RetagKind::FnEntry => "[fn entry] ",
1681                     RetagKind::TwoPhase => "[2phase] ",
1682                     RetagKind::Raw => "[raw] ",
1683                     RetagKind::Default => "",
1684                 },
1685                 place,
1686             ),
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)
1691             }
1692             LlvmInlineAsm(ref asm) => {
1693                 write!(fmt, "llvm_asm!({:?} : {:?} : {:?})", asm.asm, asm.outputs, asm.inputs)
1694             }
1695             AscribeUserType(box (ref place, ref c_ty), ref variance) => {
1696                 write!(fmt, "AscribeUserType({:?}, {:?}, {:?})", place, variance, c_ty)
1697             }
1698             Coverage(box self::Coverage { ref kind, code_region: Some(ref rgn) }) => {
1699                 write!(fmt, "Coverage::{:?} for {:?}", kind, rgn)
1700             }
1701             Coverage(box ref coverage) => write!(fmt, "Coverage::{:?}", coverage.kind),
1702             CopyNonOverlapping(box crate::mir::CopyNonOverlapping {
1703                 ref src,
1704                 ref dst,
1705                 ref count,
1706             }) => {
1707                 write!(fmt, "copy_nonoverlapping(src={:?}, dst={:?}, count={:?})", src, dst, count)
1708             }
1709             Nop => write!(fmt, "nop"),
1710         }
1711     }
1712 }
1713 
1714 #[derive(Clone, Debug, PartialEq, TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable)]
1715 pub struct Coverage {
1716     pub kind: CoverageKind,
1717     pub code_region: Option<CodeRegion>,
1718 }
1719 
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>,
1726 }
1727 
1728 ///////////////////////////////////////////////////////////////////////////
1729 // Places
1730 
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> {
1735     pub local: Local,
1736 
1737     /// projection out of a place (access a field, deref a pointer, etc)
1738     pub projection: &'tcx List<PlaceElem<'tcx>>,
1739 }
1740 
1741 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
1742 static_assert_size!(Place<'_>, 16);
1743 
1744 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
1745 #[derive(TyEncodable, TyDecodable, HashStable)]
1746 pub enum ProjectionElem<V, T> {
1747     Deref,
1748     Field(Field, T),
1749     Index(V),
1750 
1751     /// These indices are generated by slice patterns. Easiest to explain
1752     /// by example:
1753     ///
1754     /// ```
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 },
1759     /// ```
1760     ConstantIndex {
1761         /// index or -index (in Python terms), depending on from_end
1762         offset: u64,
1763         /// The thing being indexed must be at least this long. For arrays this
1764         /// is always the exact length.
1765         min_length: u64,
1766         /// Counting backwards from end? This is always false when indexing an
1767         /// array.
1768         from_end: bool,
1769     },
1770 
1771     /// These indices are generated by slice patterns.
1772     ///
1773     /// If `from_end` is true `slice[from..slice.len() - to]`.
1774     /// Otherwise `array[from..to]`.
1775     Subslice {
1776         from: u64,
1777         to: u64,
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.
1781         from_end: bool,
1782     },
1783 
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.
1787     ///
1788     /// The included Symbol is the name of the variant, used for printing MIR.
1789     Downcast(Option<Symbol>, VariantIdx),
1790 }
1791 
1792 impl<V, T> ProjectionElem<V, T> {
1793     /// Returns `true` if the target of this projection may refer to a different region of memory
1794     /// than the base.
is_indirect(&self) -> bool1795     fn is_indirect(&self) -> bool {
1796         match self {
1797             Self::Deref => true,
1798 
1799             Self::Field(_, _)
1800             | Self::Index(_)
1801             | Self::ConstantIndex { .. }
1802             | Self::Subslice { .. }
1803             | Self::Downcast(_, _) => false,
1804         }
1805     }
1806 }
1807 
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>>;
1811 
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);
1815 
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<(), ()>;
1819 
1820 rustc_index::newtype_index! {
1821     pub struct Field {
1822         derive [HashStable]
1823         DEBUG_FORMAT = "field[{}]"
1824     }
1825 }
1826 
1827 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
1828 pub struct PlaceRef<'tcx> {
1829     pub local: Local,
1830     pub projection: &'tcx [PlaceElem<'tcx>],
1831 }
1832 
1833 impl<'tcx> Place<'tcx> {
1834     // FIXME change this to a const fn by also making List::empty a const fn.
return_place() -> Place<'tcx>1835     pub fn return_place() -> Place<'tcx> {
1836         Place { local: RETURN_PLACE, projection: List::empty() }
1837     }
1838 
1839     /// Returns `true` if this `Place` contains a `Deref` projection.
1840     ///
1841     /// If `Place::is_indirect` returns false, the caller knows that the `Place` refers to the
1842     /// same region of memory as its base.
is_indirect(&self) -> bool1843     pub fn is_indirect(&self) -> bool {
1844         self.projection.iter().any(|elem| elem.is_indirect())
1845     }
1846 
1847     /// Finds the innermost `Local` from this `Place`, *if* it is either a local itself or
1848     /// a single deref of a local.
1849     #[inline(always)]
local_or_deref_local(&self) -> Option<Local>1850     pub fn local_or_deref_local(&self) -> Option<Local> {
1851         self.as_ref().local_or_deref_local()
1852     }
1853 
1854     /// If this place represents a local variable like `_X` with no
1855     /// projections, return `Some(_X)`.
1856     #[inline(always)]
as_local(&self) -> Option<Local>1857     pub fn as_local(&self) -> Option<Local> {
1858         self.as_ref().as_local()
1859     }
1860 
1861     #[inline]
as_ref(&self) -> PlaceRef<'tcx>1862     pub fn as_ref(&self) -> PlaceRef<'tcx> {
1863         PlaceRef { local: self.local, projection: &self.projection }
1864     }
1865 
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:
1869     /// - (a, .b)
1870     /// - (a.b, .c)
1871     ///
1872     /// Given a place without projections, the iterator is empty.
1873     #[inline]
iter_projections( self, ) -> impl Iterator<Item = (PlaceRef<'tcx>, PlaceElem<'tcx>)> + DoubleEndedIterator1874     pub fn iter_projections(
1875         self,
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] };
1879             (base, proj)
1880         })
1881     }
1882 }
1883 
1884 impl From<Local> for Place<'_> {
from(local: Local) -> Self1885     fn from(local: Local) -> Self {
1886         Place { local, projection: List::empty() }
1887     }
1888 }
1889 
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.
local_or_deref_local(&self) -> Option<Local>1893     pub fn local_or_deref_local(&self) -> Option<Local> {
1894         match *self {
1895             PlaceRef { local, projection: [] }
1896             | PlaceRef { local, projection: [ProjectionElem::Deref] } => Some(local),
1897             _ => None,
1898         }
1899     }
1900 
1901     /// If this place represents a local variable like `_X` with no
1902     /// projections, return `Some(_X)`.
1903     #[inline]
as_local(&self) -> Option<Local>1904     pub fn as_local(&self) -> Option<Local> {
1905         match *self {
1906             PlaceRef { local, projection: [] } => Some(local),
1907             _ => None,
1908         }
1909     }
1910 
1911     #[inline]
last_projection(&self) -> Option<(PlaceRef<'tcx>, PlaceElem<'tcx>)>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))
1915         } else {
1916             None
1917         }
1918     }
1919 }
1920 
1921 impl Debug for Place<'_> {
fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result1922     fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
1923         for elem in self.projection.iter().rev() {
1924             match elem {
1925                 ProjectionElem::Downcast(_, _) | ProjectionElem::Field(_, _) => {
1926                     write!(fmt, "(").unwrap();
1927                 }
1928                 ProjectionElem::Deref => {
1929                     write!(fmt, "(*").unwrap();
1930                 }
1931                 ProjectionElem::Index(_)
1932                 | ProjectionElem::ConstantIndex { .. }
1933                 | ProjectionElem::Subslice { .. } => {}
1934             }
1935         }
1936 
1937         write!(fmt, "{:?}", self.local)?;
1938 
1939         for elem in self.projection.iter() {
1940             match elem {
1941                 ProjectionElem::Downcast(Some(name), _index) => {
1942                     write!(fmt, " as {})", name)?;
1943                 }
1944                 ProjectionElem::Downcast(None, index) => {
1945                     write!(fmt, " as variant#{:?})", index)?;
1946                 }
1947                 ProjectionElem::Deref => {
1948                     write!(fmt, ")")?;
1949                 }
1950                 ProjectionElem::Field(field, ty) => {
1951                     write!(fmt, ".{:?}: {:?})", field.index(), ty)?;
1952                 }
1953                 ProjectionElem::Index(ref index) => {
1954                     write!(fmt, "[{:?}]", index)?;
1955                 }
1956                 ProjectionElem::ConstantIndex { offset, min_length, from_end: false } => {
1957                     write!(fmt, "[{:?} of {:?}]", offset, min_length)?;
1958                 }
1959                 ProjectionElem::ConstantIndex { offset, min_length, from_end: true } => {
1960                     write!(fmt, "[-{:?} of {:?}]", offset, min_length)?;
1961                 }
1962                 ProjectionElem::Subslice { from, to, from_end: true } if to == 0 => {
1963                     write!(fmt, "[{:?}:]", from)?;
1964                 }
1965                 ProjectionElem::Subslice { from, to, from_end: true } if from == 0 => {
1966                     write!(fmt, "[:-{:?}]", to)?;
1967                 }
1968                 ProjectionElem::Subslice { from, to, from_end: true } => {
1969                     write!(fmt, "[{:?}:-{:?}]", from, to)?;
1970                 }
1971                 ProjectionElem::Subslice { from, to, from_end: false } => {
1972                     write!(fmt, "[{:?}..{:?}]", from, to)?;
1973                 }
1974             }
1975         }
1976 
1977         Ok(())
1978     }
1979 }
1980 
1981 ///////////////////////////////////////////////////////////////////////////
1982 // Scopes
1983 
1984 rustc_index::newtype_index! {
1985     pub struct SourceScope {
1986         derive [HashStable]
1987         DEBUG_FORMAT = "scope[{}]",
1988         const OUTERMOST_SOURCE_SCOPE = 0,
1989     }
1990 }
1991 
1992 impl SourceScope {
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.
lint_root( self, source_scopes: &IndexVec<SourceScope, SourceScopeData<'tcx>>, ) -> Option<HirId>1996     pub fn lint_root(
1997         self,
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() {
2004             trace!(?data);
2005             data = &source_scopes[data.parent_scope.unwrap()];
2006         }
2007         trace!(?data);
2008         match &data.local_data {
2009             ClearCrossCrate::Set(data) => Some(data.lint_root),
2010             ClearCrossCrate::Clear => None,
2011         }
2012     }
2013 }
2014 
2015 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
2016 pub struct SourceScopeData<'tcx> {
2017     pub span: Span,
2018     pub parent_scope: Option<SourceScope>,
2019 
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)>,
2024 
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>,
2029 
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>,
2033 }
2034 
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.
2040     pub safety: Safety,
2041 }
2042 
2043 ///////////////////////////////////////////////////////////////////////////
2044 // Operands
2045 
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.
2051     ///
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.
2054     Copy(Place<'tcx>),
2055 
2056     /// Move: The value (including old borrows of it) will not be used again.
2057     ///
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.
2061     Move(Place<'tcx>),
2062 
2063     /// Synthesizes a constant value.
2064     Constant(Box<Constant<'tcx>>),
2065 }
2066 
2067 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
2068 static_assert_size!(Operand<'_>, 24);
2069 
2070 impl<'tcx> Debug for Operand<'tcx> {
fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result2071     fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
2072         use self::Operand::*;
2073         match *self {
2074             Constant(ref a) => write!(fmt, "{:?}", a),
2075             Copy(ref place) => write!(fmt, "{:?}", place),
2076             Move(ref place) => write!(fmt, "move {:?}", place),
2077         }
2078     }
2079 }
2080 
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.
function_handle( tcx: TyCtxt<'tcx>, def_id: DefId, substs: SubstsRef<'tcx>, span: Span, ) -> Self2085     pub fn function_handle(
2086         tcx: TyCtxt<'tcx>,
2087         def_id: DefId,
2088         substs: SubstsRef<'tcx>,
2089         span: Span,
2090     ) -> Self {
2091         let ty = tcx.type_of(def_id).subst(tcx, substs);
2092         Operand::Constant(Box::new(Constant {
2093             span,
2094             user_ty: None,
2095             literal: ConstantKind::Ty(ty::Const::zero_sized(tcx, ty)),
2096         }))
2097     }
2098 
is_move(&self) -> bool2099     pub fn is_move(&self) -> bool {
2100         matches!(self, Operand::Move(..))
2101     }
2102 
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.
const_from_scalar( tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, val: Scalar, span: Span, ) -> Operand<'tcx>2105     pub fn const_from_scalar(
2106         tcx: TyCtxt<'tcx>,
2107         ty: Ty<'tcx>,
2108         val: Scalar,
2109         span: Span,
2110     ) -> Operand<'tcx> {
2111         debug_assert!({
2112             let param_env_and_ty = ty::ParamEnv::empty().and(ty);
2113             let type_size = tcx
2114                 .layout_of(param_env_and_ty)
2115                 .unwrap_or_else(|e| panic!("could not compute layout for {:?}: {:?}", ty, e))
2116                 .size;
2117             let scalar_size = match val {
2118                 Scalar::Int(int) => int.size(),
2119                 _ => panic!("Invalid scalar type {:?}", val),
2120             };
2121             scalar_size == type_size
2122         });
2123         Operand::Constant(Box::new(Constant {
2124             span,
2125             user_ty: None,
2126             literal: ConstantKind::Val(ConstValue::Scalar(val), ty),
2127         }))
2128     }
2129 
to_copy(&self) -> Self2130     pub fn to_copy(&self) -> Self {
2131         match *self {
2132             Operand::Copy(_) | Operand::Constant(_) => self.clone(),
2133             Operand::Move(place) => Operand::Copy(place),
2134         }
2135     }
2136 
2137     /// Returns the `Place` that is the target of this `Operand`, or `None` if this `Operand` is a
2138     /// constant.
place(&self) -> Option<Place<'tcx>>2139     pub fn place(&self) -> Option<Place<'tcx>> {
2140         match self {
2141             Operand::Copy(place) | Operand::Move(place) => Some(*place),
2142             Operand::Constant(_) => None,
2143         }
2144     }
2145 
2146     /// Returns the `Constant` that is the target of this `Operand`, or `None` if this `Operand` is a
2147     /// place.
constant(&self) -> Option<&Constant<'tcx>>2148     pub fn constant(&self) -> Option<&Constant<'tcx>> {
2149         match self {
2150             Operand::Constant(x) => Some(&**x),
2151             Operand::Copy(_) | Operand::Move(_) => None,
2152         }
2153     }
2154 }
2155 
2156 ///////////////////////////////////////////////////////////////////////////
2157 /// Rvalues
2158 
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)
2162     Use(Operand<'tcx>),
2163 
2164     /// [x; 32]
2165     Repeat(Operand<'tcx>, &'tcx ty::Const<'tcx>),
2166 
2167     /// &x or &mut x
2168     Ref(Region<'tcx>, BorrowKind, Place<'tcx>),
2169 
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
2172     /// static.
2173     ThreadLocalRef(DefId),
2174 
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>),
2179 
2180     /// length of a `[X]` or `[X;n]` value
2181     Len(Place<'tcx>),
2182 
2183     Cast(CastKind, Operand<'tcx>, Ty<'tcx>),
2184 
2185     BinaryOp(BinOp, Box<(Operand<'tcx>, Operand<'tcx>)>),
2186     CheckedBinaryOp(BinOp, Box<(Operand<'tcx>, Operand<'tcx>)>),
2187 
2188     NullaryOp(NullOp, Ty<'tcx>),
2189     UnaryOp(UnOp, Operand<'tcx>),
2190 
2191     /// Read the discriminant of an ADT.
2192     ///
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>),
2196 
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>>),
2203 
2204     /// Transmutes a `*mut u8` into shallow-initialized `Box<T>`.
2205     ///
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>),
2209 }
2210 
2211 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
2212 static_assert_size!(Rvalue<'_>, 40);
2213 
2214 #[derive(Clone, Copy, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
2215 pub enum CastKind {
2216     Misc,
2217     Pointer(PointerCast),
2218 }
2219 
2220 #[derive(Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
2221 pub enum AggregateKind<'tcx> {
2222     /// The type is of the element
2223     Array(Ty<'tcx>),
2224     Tuple,
2225 
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>),
2232 
2233     Closure(DefId, SubstsRef<'tcx>),
2234     Generator(DefId, SubstsRef<'tcx>, hir::Movability),
2235 }
2236 
2237 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
2238 static_assert_size!(AggregateKind<'_>, 48);
2239 
2240 #[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
2241 pub enum BinOp {
2242     /// The `+` operator (addition)
2243     Add,
2244     /// The `-` operator (subtraction)
2245     Sub,
2246     /// The `*` operator (multiplication)
2247     Mul,
2248     /// The `/` operator (division)
2249     ///
2250     /// Division by zero is UB.
2251     Div,
2252     /// The `%` operator (modulus)
2253     ///
2254     /// Using zero as the modulus (second operand) is UB.
2255     Rem,
2256     /// The `^` operator (bitwise xor)
2257     BitXor,
2258     /// The `&` operator (bitwise and)
2259     BitAnd,
2260     /// The `|` operator (bitwise or)
2261     BitOr,
2262     /// The `<<` operator (shift left)
2263     ///
2264     /// The offset is truncated to the size of the first operand before shifting.
2265     Shl,
2266     /// The `>>` operator (shift right)
2267     ///
2268     /// The offset is truncated to the size of the first operand before shifting.
2269     Shr,
2270     /// The `==` operator (equality)
2271     Eq,
2272     /// The `<` operator (less than)
2273     Lt,
2274     /// The `<=` operator (less than or equal to)
2275     Le,
2276     /// The `!=` operator (not equal to)
2277     Ne,
2278     /// The `>=` operator (greater than or equal to)
2279     Ge,
2280     /// The `>` operator (greater than)
2281     Gt,
2282     /// The `ptr.offset` operator
2283     Offset,
2284 }
2285 
2286 impl BinOp {
is_checkable(self) -> bool2287     pub fn is_checkable(self) -> bool {
2288         use self::BinOp::*;
2289         matches!(self, Add | Sub | Mul | Shl | Shr)
2290     }
2291 }
2292 
2293 #[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
2294 pub enum NullOp {
2295     /// Returns the size of a value of that type
2296     SizeOf,
2297     /// Returns the minimum alignment of a type
2298     AlignOf,
2299     /// Creates a new uninitialized box for a value of that type
2300     Box,
2301 }
2302 
2303 #[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
2304 pub enum UnOp {
2305     /// The `!` operator for logical inversion
2306     Not,
2307     /// The `-` operator for negation
2308     Neg,
2309 }
2310 
2311 impl<'tcx> Debug for Rvalue<'tcx> {
fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result2312     fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
2313         use self::Rvalue::*;
2314 
2315         match *self {
2316             Use(ref place) => write!(fmt, "{:?}", place),
2317             Repeat(ref a, ref b) => {
2318                 write!(fmt, "[{:?}; ", a)?;
2319                 pretty_print_const(b, fmt, false)?;
2320                 write!(fmt, "]")
2321             }
2322             Len(ref a) => write!(fmt, "Len({:?})", a),
2323             Cast(ref kind, ref place, ref ty) => {
2324                 write!(fmt, "{:?} as {:?} ({:?})", place, ty, kind)
2325             }
2326             BinaryOp(ref op, box (ref a, ref b)) => write!(fmt, "{:?}({:?}, {:?})", op, a, b),
2327             CheckedBinaryOp(ref op, box (ref a, ref b)) => {
2328                 write!(fmt, "Checked{:?}({:?}, {:?})", op, a, b)
2329             }
2330             UnaryOp(ref op, ref a) => write!(fmt, "{:?}({:?})", op, a),
2331             Discriminant(ref place) => write!(fmt, "discriminant({:?})", place),
2332             NullaryOp(ref op, ref t) => write!(fmt, "{:?}({:?})", op, t),
2333             ThreadLocalRef(did) => ty::tls::with(|tcx| {
2334                 let muta = tcx.static_mutability(did).unwrap().prefix_str();
2335                 write!(fmt, "&/*tls*/ {}{}", muta, tcx.def_path_str(did))
2336             }),
2337             Ref(region, borrow_kind, ref place) => {
2338                 let kind_str = match borrow_kind {
2339                     BorrowKind::Shared => "",
2340                     BorrowKind::Shallow => "shallow ",
2341                     BorrowKind::Mut { .. } | BorrowKind::Unique => "mut ",
2342                 };
2343 
2344                 // When printing regions, add trailing space if necessary.
2345                 let print_region = ty::tls::with(|tcx| {
2346                     tcx.sess.verbose() || tcx.sess.opts.debugging_opts.identify_regions
2347                 });
2348                 let region = if print_region {
2349                     let mut region = region.to_string();
2350                     if !region.is_empty() {
2351                         region.push(' ');
2352                     }
2353                     region
2354                 } else {
2355                     // Do not even print 'static
2356                     String::new()
2357                 };
2358                 write!(fmt, "&{}{}{:?}", region, kind_str, place)
2359             }
2360 
2361             AddressOf(mutability, ref place) => {
2362                 let kind_str = match mutability {
2363                     Mutability::Mut => "mut",
2364                     Mutability::Not => "const",
2365                 };
2366 
2367                 write!(fmt, "&raw {} {:?}", kind_str, place)
2368             }
2369 
2370             Aggregate(ref kind, ref places) => {
2371                 let fmt_tuple = |fmt: &mut Formatter<'_>, name: &str| {
2372                     let mut tuple_fmt = fmt.debug_tuple(name);
2373                     for place in places {
2374                         tuple_fmt.field(place);
2375                     }
2376                     tuple_fmt.finish()
2377                 };
2378 
2379                 match **kind {
2380                     AggregateKind::Array(_) => write!(fmt, "{:?}", places),
2381 
2382                     AggregateKind::Tuple => {
2383                         if places.is_empty() {
2384                             write!(fmt, "()")
2385                         } else {
2386                             fmt_tuple(fmt, "")
2387                         }
2388                     }
2389 
2390                     AggregateKind::Adt(adt_def, variant, substs, _user_ty, _) => {
2391                         let variant_def = &adt_def.variants[variant];
2392 
2393                         let name = ty::tls::with(|tcx| {
2394                             let mut name = String::new();
2395                             let substs = tcx.lift(substs).expect("could not lift for printing");
2396                             FmtPrinter::new(tcx, &mut name, Namespace::ValueNS)
2397                                 .print_def_path(variant_def.def_id, substs)?;
2398                             Ok(name)
2399                         })?;
2400 
2401                         match variant_def.ctor_kind {
2402                             CtorKind::Const => fmt.write_str(&name),
2403                             CtorKind::Fn => fmt_tuple(fmt, &name),
2404                             CtorKind::Fictive => {
2405                                 let mut struct_fmt = fmt.debug_struct(&name);
2406                                 for (field, place) in iter::zip(&variant_def.fields, places) {
2407                                     struct_fmt.field(&field.ident.as_str(), place);
2408                                 }
2409                                 struct_fmt.finish()
2410                             }
2411                         }
2412                     }
2413 
2414                     AggregateKind::Closure(def_id, substs) => ty::tls::with(|tcx| {
2415                         if let Some(def_id) = def_id.as_local() {
2416                             let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
2417                             let name = if tcx.sess.opts.debugging_opts.span_free_formats {
2418                                 let substs = tcx.lift(substs).unwrap();
2419                                 format!(
2420                                     "[closure@{}]",
2421                                     tcx.def_path_str_with_substs(def_id.to_def_id(), substs),
2422                                 )
2423                             } else {
2424                                 let span = tcx.hir().span(hir_id);
2425                                 format!(
2426                                     "[closure@{}]",
2427                                     tcx.sess.source_map().span_to_diagnostic_string(span)
2428                                 )
2429                             };
2430                             let mut struct_fmt = fmt.debug_struct(&name);
2431 
2432                             // FIXME(project-rfc-2229#48): This should be a list of capture names/places
2433                             if let Some(upvars) = tcx.upvars_mentioned(def_id) {
2434                                 for (&var_id, place) in iter::zip(upvars.keys(), places) {
2435                                     let var_name = tcx.hir().name(var_id);
2436                                     struct_fmt.field(&var_name.as_str(), place);
2437                                 }
2438                             }
2439 
2440                             struct_fmt.finish()
2441                         } else {
2442                             write!(fmt, "[closure]")
2443                         }
2444                     }),
2445 
2446                     AggregateKind::Generator(def_id, _, _) => ty::tls::with(|tcx| {
2447                         if let Some(def_id) = def_id.as_local() {
2448                             let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
2449                             let name = format!("[generator@{:?}]", tcx.hir().span(hir_id));
2450                             let mut struct_fmt = fmt.debug_struct(&name);
2451 
2452                             // FIXME(project-rfc-2229#48): This should be a list of capture names/places
2453                             if let Some(upvars) = tcx.upvars_mentioned(def_id) {
2454                                 for (&var_id, place) in iter::zip(upvars.keys(), places) {
2455                                     let var_name = tcx.hir().name(var_id);
2456                                     struct_fmt.field(&var_name.as_str(), place);
2457                                 }
2458                             }
2459 
2460                             struct_fmt.finish()
2461                         } else {
2462                             write!(fmt, "[generator]")
2463                         }
2464                     }),
2465                 }
2466             }
2467 
2468             ShallowInitBox(ref place, ref ty) => {
2469                 write!(fmt, "ShallowInitBox({:?}, {:?})", place, ty)
2470             }
2471         }
2472     }
2473 }
2474 
2475 ///////////////////////////////////////////////////////////////////////////
2476 /// Constants
2477 ///
2478 /// Two constants are equal if they are the same constant. Note that
2479 /// this does not necessarily mean that they are `==` in Rust. In
2480 /// particular, one must be wary of `NaN`!
2481 
2482 #[derive(Clone, Copy, PartialEq, PartialOrd, TyEncodable, TyDecodable, Hash, HashStable)]
2483 pub struct Constant<'tcx> {
2484     pub span: Span,
2485 
2486     /// Optional user-given type: for something like
2487     /// `collect::<Vec<_>>`, this would be present and would
2488     /// indicate that `Vec<_>` was explicitly specified.
2489     ///
2490     /// Needed for NLL to impose user-given type constraints.
2491     pub user_ty: Option<UserTypeAnnotationIndex>,
2492 
2493     pub literal: ConstantKind<'tcx>,
2494 }
2495 
2496 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, TyEncodable, TyDecodable, Hash, HashStable, Debug)]
2497 #[derive(Lift)]
2498 pub enum ConstantKind<'tcx> {
2499     /// This constant came from the type system
2500     Ty(&'tcx ty::Const<'tcx>),
2501     /// This constant cannot go back into the type system, as it represents
2502     /// something the type system cannot handle (e.g. pointers).
2503     Val(interpret::ConstValue<'tcx>, Ty<'tcx>),
2504 }
2505 
2506 impl Constant<'tcx> {
check_static_ptr(&self, tcx: TyCtxt<'_>) -> Option<DefId>2507     pub fn check_static_ptr(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
2508         match self.literal.const_for_ty()?.val.try_to_scalar() {
2509             Some(Scalar::Ptr(ptr, _size)) => match tcx.global_alloc(ptr.provenance) {
2510                 GlobalAlloc::Static(def_id) => {
2511                     assert!(!tcx.is_thread_local_static(def_id));
2512                     Some(def_id)
2513                 }
2514                 _ => None,
2515             },
2516             _ => None,
2517         }
2518     }
2519     #[inline]
ty(&self) -> Ty<'tcx>2520     pub fn ty(&self) -> Ty<'tcx> {
2521         self.literal.ty()
2522     }
2523 }
2524 
2525 impl From<&'tcx ty::Const<'tcx>> for ConstantKind<'tcx> {
2526     #[inline]
from(ct: &'tcx ty::Const<'tcx>) -> Self2527     fn from(ct: &'tcx ty::Const<'tcx>) -> Self {
2528         Self::Ty(ct)
2529     }
2530 }
2531 
2532 impl ConstantKind<'tcx> {
2533     /// Returns `None` if the constant is not trivially safe for use in the type system.
const_for_ty(&self) -> Option<&'tcx ty::Const<'tcx>>2534     pub fn const_for_ty(&self) -> Option<&'tcx ty::Const<'tcx>> {
2535         match self {
2536             ConstantKind::Ty(c) => Some(c),
2537             ConstantKind::Val(..) => None,
2538         }
2539     }
2540 
ty(&self) -> Ty<'tcx>2541     pub fn ty(&self) -> Ty<'tcx> {
2542         match self {
2543             ConstantKind::Ty(c) => c.ty,
2544             ConstantKind::Val(_, ty) => ty,
2545         }
2546     }
2547 
2548     #[inline]
try_to_value(self) -> Option<interpret::ConstValue<'tcx>>2549     pub fn try_to_value(self) -> Option<interpret::ConstValue<'tcx>> {
2550         match self {
2551             ConstantKind::Ty(c) => c.val.try_to_value(),
2552             ConstantKind::Val(val, _) => Some(val),
2553         }
2554     }
2555 
2556     #[inline]
try_to_scalar(self) -> Option<Scalar>2557     pub fn try_to_scalar(self) -> Option<Scalar> {
2558         self.try_to_value()?.try_to_scalar()
2559     }
2560 
2561     #[inline]
try_to_scalar_int(self) -> Option<ScalarInt>2562     pub fn try_to_scalar_int(self) -> Option<ScalarInt> {
2563         Some(self.try_to_value()?.try_to_scalar()?.assert_int())
2564     }
2565 
2566     #[inline]
try_to_bits(self, size: Size) -> Option<u128>2567     pub fn try_to_bits(self, size: Size) -> Option<u128> {
2568         self.try_to_scalar_int()?.to_bits(size).ok()
2569     }
2570 
2571     #[inline]
try_to_bool(self) -> Option<bool>2572     pub fn try_to_bool(self) -> Option<bool> {
2573         self.try_to_scalar_int()?.try_into().ok()
2574     }
2575 
2576     #[inline]
try_eval_bits( &self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, ty: Ty<'tcx>, ) -> Option<u128>2577     pub fn try_eval_bits(
2578         &self,
2579         tcx: TyCtxt<'tcx>,
2580         param_env: ty::ParamEnv<'tcx>,
2581         ty: Ty<'tcx>,
2582     ) -> Option<u128> {
2583         match self {
2584             Self::Ty(ct) => ct.try_eval_bits(tcx, param_env, ty),
2585             Self::Val(val, t) => {
2586                 assert_eq!(*t, ty);
2587                 let size =
2588                     tcx.layout_of(param_env.with_reveal_all_normalized(tcx).and(ty)).ok()?.size;
2589                 val.try_to_bits(size)
2590             }
2591         }
2592     }
2593 
2594     #[inline]
try_eval_bool(&self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Option<bool>2595     pub fn try_eval_bool(&self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Option<bool> {
2596         match self {
2597             Self::Ty(ct) => ct.try_eval_bool(tcx, param_env),
2598             Self::Val(val, _) => val.try_to_bool(),
2599         }
2600     }
2601 
2602     #[inline]
try_eval_usize(&self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Option<u64>2603     pub fn try_eval_usize(&self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Option<u64> {
2604         match self {
2605             Self::Ty(ct) => ct.try_eval_usize(tcx, param_env),
2606             Self::Val(val, _) => val.try_to_machine_usize(tcx),
2607         }
2608     }
2609 }
2610 
2611 /// A collection of projections into user types.
2612 ///
2613 /// They are projections because a binding can occur a part of a
2614 /// parent pattern that has been ascribed a type.
2615 ///
2616 /// Its a collection because there can be multiple type ascriptions on
2617 /// the path from the root of the pattern down to the binding itself.
2618 ///
2619 /// An example:
2620 ///
2621 /// ```rust
2622 /// struct S<'a>((i32, &'a str), String);
2623 /// let S((_, w): (i32, &'static str), _): S = ...;
2624 /// //    ------  ^^^^^^^^^^^^^^^^^^^ (1)
2625 /// //  ---------------------------------  ^ (2)
2626 /// ```
2627 ///
2628 /// The highlights labelled `(1)` show the subpattern `(_, w)` being
2629 /// ascribed the type `(i32, &'static str)`.
2630 ///
2631 /// The highlights labelled `(2)` show the whole pattern being
2632 /// ascribed the type `S`.
2633 ///
2634 /// In this example, when we descend to `w`, we will have built up the
2635 /// following two projected types:
2636 ///
2637 ///   * base: `S`,                   projection: `(base.0).1`
2638 ///   * base: `(i32, &'static str)`, projection: `base.1`
2639 ///
2640 /// The first will lead to the constraint `w: &'1 str` (for some
2641 /// inferred region `'1`). The second will lead to the constraint `w:
2642 /// &'static str`.
2643 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
2644 pub struct UserTypeProjections {
2645     pub contents: Vec<(UserTypeProjection, Span)>,
2646 }
2647 
2648 impl<'tcx> UserTypeProjections {
none() -> Self2649     pub fn none() -> Self {
2650         UserTypeProjections { contents: vec![] }
2651     }
2652 
is_empty(&self) -> bool2653     pub fn is_empty(&self) -> bool {
2654         self.contents.is_empty()
2655     }
2656 
projections_and_spans( &self, ) -> impl Iterator<Item = &(UserTypeProjection, Span)> + ExactSizeIterator2657     pub fn projections_and_spans(
2658         &self,
2659     ) -> impl Iterator<Item = &(UserTypeProjection, Span)> + ExactSizeIterator {
2660         self.contents.iter()
2661     }
2662 
projections(&self) -> impl Iterator<Item = &UserTypeProjection> + ExactSizeIterator2663     pub fn projections(&self) -> impl Iterator<Item = &UserTypeProjection> + ExactSizeIterator {
2664         self.contents.iter().map(|&(ref user_type, _span)| user_type)
2665     }
2666 
push_projection(mut self, user_ty: &UserTypeProjection, span: Span) -> Self2667     pub fn push_projection(mut self, user_ty: &UserTypeProjection, span: Span) -> Self {
2668         self.contents.push((user_ty.clone(), span));
2669         self
2670     }
2671 
map_projections( mut self, mut f: impl FnMut(UserTypeProjection) -> UserTypeProjection, ) -> Self2672     fn map_projections(
2673         mut self,
2674         mut f: impl FnMut(UserTypeProjection) -> UserTypeProjection,
2675     ) -> Self {
2676         self.contents = self.contents.into_iter().map(|(proj, span)| (f(proj), span)).collect();
2677         self
2678     }
2679 
index(self) -> Self2680     pub fn index(self) -> Self {
2681         self.map_projections(|pat_ty_proj| pat_ty_proj.index())
2682     }
2683 
subslice(self, from: u64, to: u64) -> Self2684     pub fn subslice(self, from: u64, to: u64) -> Self {
2685         self.map_projections(|pat_ty_proj| pat_ty_proj.subslice(from, to))
2686     }
2687 
deref(self) -> Self2688     pub fn deref(self) -> Self {
2689         self.map_projections(|pat_ty_proj| pat_ty_proj.deref())
2690     }
2691 
leaf(self, field: Field) -> Self2692     pub fn leaf(self, field: Field) -> Self {
2693         self.map_projections(|pat_ty_proj| pat_ty_proj.leaf(field))
2694     }
2695 
variant(self, adt_def: &'tcx AdtDef, variant_index: VariantIdx, field: Field) -> Self2696     pub fn variant(self, adt_def: &'tcx AdtDef, variant_index: VariantIdx, field: Field) -> Self {
2697         self.map_projections(|pat_ty_proj| pat_ty_proj.variant(adt_def, variant_index, field))
2698     }
2699 }
2700 
2701 /// Encodes the effect of a user-supplied type annotation on the
2702 /// subcomponents of a pattern. The effect is determined by applying the
2703 /// given list of proejctions to some underlying base type. Often,
2704 /// the projection element list `projs` is empty, in which case this
2705 /// directly encodes a type in `base`. But in the case of complex patterns with
2706 /// subpatterns and bindings, we want to apply only a *part* of the type to a variable,
2707 /// in which case the `projs` vector is used.
2708 ///
2709 /// Examples:
2710 ///
2711 /// * `let x: T = ...` -- here, the `projs` vector is empty.
2712 ///
2713 /// * `let (x, _): T = ...` -- here, the `projs` vector would contain
2714 ///   `field[0]` (aka `.0`), indicating that the type of `s` is
2715 ///   determined by finding the type of the `.0` field from `T`.
2716 #[derive(Clone, Debug, TyEncodable, TyDecodable, Hash, HashStable, PartialEq)]
2717 pub struct UserTypeProjection {
2718     pub base: UserTypeAnnotationIndex,
2719     pub projs: Vec<ProjectionKind>,
2720 }
2721 
2722 impl Copy for ProjectionKind {}
2723 
2724 impl UserTypeProjection {
index(mut self) -> Self2725     pub(crate) fn index(mut self) -> Self {
2726         self.projs.push(ProjectionElem::Index(()));
2727         self
2728     }
2729 
subslice(mut self, from: u64, to: u64) -> Self2730     pub(crate) fn subslice(mut self, from: u64, to: u64) -> Self {
2731         self.projs.push(ProjectionElem::Subslice { from, to, from_end: true });
2732         self
2733     }
2734 
deref(mut self) -> Self2735     pub(crate) fn deref(mut self) -> Self {
2736         self.projs.push(ProjectionElem::Deref);
2737         self
2738     }
2739 
leaf(mut self, field: Field) -> Self2740     pub(crate) fn leaf(mut self, field: Field) -> Self {
2741         self.projs.push(ProjectionElem::Field(field, ()));
2742         self
2743     }
2744 
variant( mut self, adt_def: &AdtDef, variant_index: VariantIdx, field: Field, ) -> Self2745     pub(crate) fn variant(
2746         mut self,
2747         adt_def: &AdtDef,
2748         variant_index: VariantIdx,
2749         field: Field,
2750     ) -> Self {
2751         self.projs.push(ProjectionElem::Downcast(
2752             Some(adt_def.variants[variant_index].ident.name),
2753             variant_index,
2754         ));
2755         self.projs.push(ProjectionElem::Field(field, ()));
2756         self
2757     }
2758 }
2759 
2760 TrivialTypeFoldableAndLiftImpls! { ProjectionKind, }
2761 
2762 impl<'tcx> TypeFoldable<'tcx> for UserTypeProjection {
super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self2763     fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
2764         UserTypeProjection {
2765             base: self.base.fold_with(folder),
2766             projs: self.projs.fold_with(folder),
2767         }
2768     }
2769 
super_visit_with<Vs: TypeVisitor<'tcx>>( &self, visitor: &mut Vs, ) -> ControlFlow<Vs::BreakTy>2770     fn super_visit_with<Vs: TypeVisitor<'tcx>>(
2771         &self,
2772         visitor: &mut Vs,
2773     ) -> ControlFlow<Vs::BreakTy> {
2774         self.base.visit_with(visitor)
2775         // Note: there's nothing in `self.proj` to visit.
2776     }
2777 }
2778 
2779 rustc_index::newtype_index! {
2780     pub struct Promoted {
2781         derive [HashStable]
2782         DEBUG_FORMAT = "promoted[{}]"
2783     }
2784 }
2785 
2786 impl<'tcx> Debug for Constant<'tcx> {
fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result2787     fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
2788         write!(fmt, "{}", self)
2789     }
2790 }
2791 
2792 impl<'tcx> Display for Constant<'tcx> {
fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result2793     fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
2794         match self.ty().kind() {
2795             ty::FnDef(..) => {}
2796             _ => write!(fmt, "const ")?,
2797         }
2798         Display::fmt(&self.literal, fmt)
2799     }
2800 }
2801 
2802 impl<'tcx> Display for ConstantKind<'tcx> {
fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result2803     fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
2804         match *self {
2805             ConstantKind::Ty(c) => pretty_print_const(c, fmt, true),
2806             ConstantKind::Val(val, ty) => pretty_print_const_value(val, ty, fmt, true),
2807         }
2808     }
2809 }
2810 
pretty_print_const( c: &ty::Const<'tcx>, fmt: &mut Formatter<'_>, print_types: bool, ) -> fmt::Result2811 fn pretty_print_const(
2812     c: &ty::Const<'tcx>,
2813     fmt: &mut Formatter<'_>,
2814     print_types: bool,
2815 ) -> fmt::Result {
2816     use crate::ty::print::PrettyPrinter;
2817     ty::tls::with(|tcx| {
2818         let literal = tcx.lift(c).unwrap();
2819         let mut cx = FmtPrinter::new(tcx, fmt, Namespace::ValueNS);
2820         cx.print_alloc_ids = true;
2821         cx.pretty_print_const(literal, print_types)?;
2822         Ok(())
2823     })
2824 }
2825 
pretty_print_const_value( val: interpret::ConstValue<'tcx>, ty: Ty<'tcx>, fmt: &mut Formatter<'_>, print_types: bool, ) -> fmt::Result2826 fn pretty_print_const_value(
2827     val: interpret::ConstValue<'tcx>,
2828     ty: Ty<'tcx>,
2829     fmt: &mut Formatter<'_>,
2830     print_types: bool,
2831 ) -> fmt::Result {
2832     use crate::ty::print::PrettyPrinter;
2833     ty::tls::with(|tcx| {
2834         let val = tcx.lift(val).unwrap();
2835         let ty = tcx.lift(ty).unwrap();
2836         let mut cx = FmtPrinter::new(tcx, fmt, Namespace::ValueNS);
2837         cx.print_alloc_ids = true;
2838         cx.pretty_print_const_value(val, ty, print_types)?;
2839         Ok(())
2840     })
2841 }
2842 
2843 impl<'tcx> graph::DirectedGraph for Body<'tcx> {
2844     type Node = BasicBlock;
2845 }
2846 
2847 impl<'tcx> graph::WithNumNodes for Body<'tcx> {
2848     #[inline]
num_nodes(&self) -> usize2849     fn num_nodes(&self) -> usize {
2850         self.basic_blocks.len()
2851     }
2852 }
2853 
2854 impl<'tcx> graph::WithStartNode for Body<'tcx> {
2855     #[inline]
start_node(&self) -> Self::Node2856     fn start_node(&self) -> Self::Node {
2857         START_BLOCK
2858     }
2859 }
2860 
2861 impl<'tcx> graph::WithSuccessors for Body<'tcx> {
2862     #[inline]
successors(&self, node: Self::Node) -> <Self as GraphSuccessors<'_>>::Iter2863     fn successors(&self, node: Self::Node) -> <Self as GraphSuccessors<'_>>::Iter {
2864         self.basic_blocks[node].terminator().successors().cloned()
2865     }
2866 }
2867 
2868 impl<'a, 'b> graph::GraphSuccessors<'b> for Body<'a> {
2869     type Item = BasicBlock;
2870     type Iter = iter::Cloned<Successors<'b>>;
2871 }
2872 
2873 impl graph::GraphPredecessors<'graph> for Body<'tcx> {
2874     type Item = BasicBlock;
2875     type Iter = std::iter::Copied<std::slice::Iter<'graph, BasicBlock>>;
2876 }
2877 
2878 impl graph::WithPredecessors for Body<'tcx> {
2879     #[inline]
predecessors(&self, node: Self::Node) -> <Self as graph::GraphPredecessors<'_>>::Iter2880     fn predecessors(&self, node: Self::Node) -> <Self as graph::GraphPredecessors<'_>>::Iter {
2881         self.predecessors()[node].iter().copied()
2882     }
2883 }
2884 
2885 /// `Location` represents the position of the start of the statement; or, if
2886 /// `statement_index` equals the number of statements, then the start of the
2887 /// terminator.
2888 #[derive(Copy, Clone, PartialEq, Eq, Hash, Ord, PartialOrd, HashStable)]
2889 pub struct Location {
2890     /// The block that the location is within.
2891     pub block: BasicBlock,
2892 
2893     pub statement_index: usize,
2894 }
2895 
2896 impl fmt::Debug for Location {
fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result2897     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2898         write!(fmt, "{:?}[{}]", self.block, self.statement_index)
2899     }
2900 }
2901 
2902 impl Location {
2903     pub const START: Location = Location { block: START_BLOCK, statement_index: 0 };
2904 
2905     /// Returns the location immediately after this one within the enclosing block.
2906     ///
2907     /// Note that if this location represents a terminator, then the
2908     /// resulting location would be out of bounds and invalid.
successor_within_block(&self) -> Location2909     pub fn successor_within_block(&self) -> Location {
2910         Location { block: self.block, statement_index: self.statement_index + 1 }
2911     }
2912 
2913     /// Returns `true` if `other` is earlier in the control flow graph than `self`.
is_predecessor_of<'tcx>(&self, other: Location, body: &Body<'tcx>) -> bool2914     pub fn is_predecessor_of<'tcx>(&self, other: Location, body: &Body<'tcx>) -> bool {
2915         // If we are in the same block as the other location and are an earlier statement
2916         // then we are a predecessor of `other`.
2917         if self.block == other.block && self.statement_index < other.statement_index {
2918             return true;
2919         }
2920 
2921         let predecessors = body.predecessors();
2922 
2923         // If we're in another block, then we want to check that block is a predecessor of `other`.
2924         let mut queue: Vec<BasicBlock> = predecessors[other.block].to_vec();
2925         let mut visited = FxHashSet::default();
2926 
2927         while let Some(block) = queue.pop() {
2928             // If we haven't visited this block before, then make sure we visit it's predecessors.
2929             if visited.insert(block) {
2930                 queue.extend(predecessors[block].iter().cloned());
2931             } else {
2932                 continue;
2933             }
2934 
2935             // If we found the block that `self` is in, then we are a predecessor of `other` (since
2936             // we found that block by looking at the predecessors of `other`).
2937             if self.block == block {
2938                 return true;
2939             }
2940         }
2941 
2942         false
2943     }
2944 
dominates(&self, other: Location, dominators: &Dominators<BasicBlock>) -> bool2945     pub fn dominates(&self, other: Location, dominators: &Dominators<BasicBlock>) -> bool {
2946         if self.block == other.block {
2947             self.statement_index <= other.statement_index
2948         } else {
2949             dominators.is_dominated_by(other.block, self.block)
2950         }
2951     }
2952 }
2953