2020-03-05 18:07:42 -03:00
|
|
|
//! MIR datatypes and passes. See the [rustc dev guide] for more info.
|
2017-12-31 17:08:04 +01:00
|
|
|
//!
|
2020-03-09 18:33:04 -03:00
|
|
|
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/mir/index.html
|
2017-04-29 05:28:35 -04:00
|
|
|
|
2020-08-15 04:42:13 -07:00
|
|
|
use crate::mir::coverage::{CodeRegion, CoverageKind};
|
2022-04-12 18:14:28 +02:00
|
|
|
use crate::mir::interpret::{ConstAllocation, ConstValue, GlobalAlloc, LitToConstInput, Scalar};
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::mir::visit::MirVisitable;
|
2019-07-12 22:49:15 +02:00
|
|
|
use crate::ty::adjustment::PointerCast;
|
2020-06-11 15:49:57 +01:00
|
|
|
use crate::ty::codec::{TyDecoder, TyEncoder};
|
2021-12-01 15:11:24 +00:00
|
|
|
use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeVisitor};
|
2019-07-12 22:49:15 +02:00
|
|
|
use crate::ty::print::{FmtPrinter, Printer};
|
2022-04-02 12:09:22 +02:00
|
|
|
use crate::ty::subst::{GenericArg, InternalSubsts, Subst, SubstsRef};
|
2020-10-04 10:59:11 -07:00
|
|
|
use crate::ty::{self, List, Ty, TyCtxt};
|
2021-03-08 16:18:03 +00:00
|
|
|
use crate::ty::{AdtDef, InstanceDef, Region, ScalarInt, UserTypeAnnotationIndex};
|
2021-03-15 15:09:06 -07:00
|
|
|
|
2022-05-22 12:48:19 -07:00
|
|
|
use rustc_data_structures::captures::Captures;
|
2022-01-23 12:34:26 -06:00
|
|
|
use rustc_errors::ErrorGuaranteed;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir::def::{CtorKind, Namespace};
|
2022-04-15 19:27:53 +02:00
|
|
|
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID};
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir::{self, GeneratorKind};
|
2021-03-26 13:39:04 +00:00
|
|
|
use rustc_hir::{self as hir, HirId};
|
2021-11-29 22:46:32 -08:00
|
|
|
use rustc_session::Session;
|
2021-03-08 16:18:03 +00:00
|
|
|
use rustc_target::abi::{Size, VariantIdx};
|
2019-09-06 03:57:44 +01:00
|
|
|
|
2019-07-12 22:48:02 +02:00
|
|
|
use polonius_engine::Atom;
|
2020-04-27 23:26:11 +05:30
|
|
|
pub use rustc_ast::Mutability;
|
2018-11-05 15:14:28 +01:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2020-04-12 10:30:07 -07:00
|
|
|
use rustc_data_structures::graph::dominators::{dominators, Dominators};
|
2019-10-09 23:22:58 -04:00
|
|
|
use rustc_data_structures::graph::{self, GraphSuccessors};
|
2019-09-26 05:30:10 +00:00
|
|
|
use rustc_index::bit_set::BitMatrix;
|
2019-09-26 05:38:33 +00:00
|
|
|
use rustc_index::vec::{Idx, IndexVec};
|
2019-07-23 18:50:47 +03:00
|
|
|
use rustc_serialize::{Decodable, Encodable};
|
2020-01-01 19:30:57 +01:00
|
|
|
use rustc_span::symbol::Symbol;
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::{Span, DUMMY_SP};
|
2020-05-06 14:46:01 +01:00
|
|
|
use rustc_target::asm::InlineAsmRegOrRegClass;
|
2021-03-15 15:09:06 -07:00
|
|
|
|
|
|
|
use either::Either;
|
|
|
|
|
2018-06-19 21:22:52 -03:00
|
|
|
use std::borrow::Cow;
|
2021-03-08 16:18:03 +00:00
|
|
|
use std::convert::TryInto;
|
2019-07-12 22:49:15 +02:00
|
|
|
use std::fmt::{self, Debug, Display, Formatter, Write};
|
2020-10-21 14:22:44 +02:00
|
|
|
use std::ops::{ControlFlow, Index, IndexMut};
|
2018-06-19 21:22:52 -03:00
|
|
|
use std::slice;
|
2020-04-04 17:25:45 +02:00
|
|
|
use std::{iter, mem, option};
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2020-12-20 10:29:02 +01:00
|
|
|
use self::graph_cyclic_cache::GraphIsCyclicCache;
|
2020-04-12 10:30:07 -07:00
|
|
|
use self::predecessors::{PredecessorCache, Predecessors};
|
2020-01-05 15:46:44 +00:00
|
|
|
pub use self::query::*;
|
2022-03-25 21:11:49 -04:00
|
|
|
use self::switch_sources::{SwitchSourceCache, SwitchSources};
|
2018-04-27 15:21:31 +02:00
|
|
|
|
2020-06-21 23:29:08 -07:00
|
|
|
pub mod coverage;
|
2021-01-05 19:53:07 +01:00
|
|
|
mod generic_graph;
|
|
|
|
pub mod generic_graphviz;
|
2020-12-20 10:29:02 +01:00
|
|
|
mod graph_cyclic_cache;
|
2021-01-05 19:53:07 +01:00
|
|
|
pub mod graphviz;
|
2017-09-18 16:18:23 +02:00
|
|
|
pub mod interpret;
|
2017-10-27 10:50:39 +02:00
|
|
|
pub mod mono;
|
2021-01-05 19:53:07 +01:00
|
|
|
pub mod patch;
|
2020-04-12 10:30:07 -07:00
|
|
|
mod predecessors;
|
2021-01-05 19:53:07 +01:00
|
|
|
pub mod pretty;
|
2020-01-05 15:46:44 +00:00
|
|
|
mod query;
|
2021-01-05 19:53:07 +01:00
|
|
|
pub mod spanview;
|
2022-03-25 21:11:49 -04:00
|
|
|
mod switch_sources;
|
2018-06-19 21:22:52 -03:00
|
|
|
pub mod tcx;
|
2020-06-26 19:07:36 +00:00
|
|
|
pub mod terminator;
|
2022-04-28 11:31:08 +08:00
|
|
|
use crate::mir::traversal::PostorderCache;
|
2020-06-26 19:07:36 +00:00
|
|
|
pub use terminator::*;
|
2022-04-28 11:31:08 +08:00
|
|
|
|
2018-06-19 21:22:52 -03:00
|
|
|
pub mod traversal;
|
2020-03-23 22:39:59 +01:00
|
|
|
mod type_foldable;
|
2018-06-19 21:22:52 -03:00
|
|
|
pub mod visit;
|
2016-06-07 22:02:08 +03:00
|
|
|
|
2021-01-05 19:53:07 +01:00
|
|
|
pub use self::generic_graph::graphviz_safe_def_name;
|
|
|
|
pub use self::graphviz::write_mir_graphviz;
|
|
|
|
pub use self::pretty::{
|
|
|
|
create_dump_file, display_allocation, dump_enabled, dump_mir, write_mir_pretty, PassWhere,
|
|
|
|
};
|
|
|
|
|
2017-07-11 16:02:06 -07:00
|
|
|
/// Types for locals
|
2021-01-18 00:00:00 +00:00
|
|
|
pub type LocalDecls<'tcx> = IndexVec<Local, LocalDecl<'tcx>>;
|
2017-07-11 16:02:06 -07:00
|
|
|
|
2017-07-12 13:15:29 -07:00
|
|
|
pub trait HasLocalDecls<'tcx> {
|
|
|
|
fn local_decls(&self) -> &LocalDecls<'tcx>;
|
2017-07-12 12:59:05 -07:00
|
|
|
}
|
|
|
|
|
2017-07-12 13:15:29 -07:00
|
|
|
impl<'tcx> HasLocalDecls<'tcx> for LocalDecls<'tcx> {
|
2021-02-26 00:00:00 +00:00
|
|
|
#[inline]
|
2017-07-12 13:15:29 -07:00
|
|
|
fn local_decls(&self) -> &LocalDecls<'tcx> {
|
2017-07-12 12:59:05 -07:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 23:55:04 +02:00
|
|
|
impl<'tcx> HasLocalDecls<'tcx> for Body<'tcx> {
|
2021-02-26 00:00:00 +00:00
|
|
|
#[inline]
|
2017-07-12 13:15:29 -07:00
|
|
|
fn local_decls(&self) -> &LocalDecls<'tcx> {
|
2017-07-12 12:59:05 -07:00
|
|
|
&self.local_decls
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-05 19:53:07 +01:00
|
|
|
/// A streamlined trait that you can implement to create a pass; the
|
|
|
|
/// pass will be named after the type, and it will consist of a main
|
|
|
|
/// loop that goes over each available MIR and applies `run_pass`.
|
|
|
|
pub trait MirPass<'tcx> {
|
|
|
|
fn name(&self) -> Cow<'_, str> {
|
|
|
|
let name = std::any::type_name::<Self>();
|
|
|
|
if let Some(tail) = name.rfind(':') {
|
|
|
|
Cow::from(&name[tail + 1..])
|
|
|
|
} else {
|
|
|
|
Cow::from(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-29 22:46:32 -08:00
|
|
|
/// Returns `true` if this pass is enabled with the current combination of compiler flags.
|
|
|
|
fn is_enabled(&self, _sess: &Session) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2021-01-05 19:53:07 +01:00
|
|
|
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>);
|
2021-11-29 22:46:32 -08:00
|
|
|
|
|
|
|
/// If this pass causes the MIR to enter a new phase, return that phase.
|
|
|
|
fn phase_change(&self) -> Option<MirPhase> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_mir_dump_enabled(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
2021-01-05 19:53:07 +01:00
|
|
|
}
|
|
|
|
|
2018-10-20 16:18:17 -04:00
|
|
|
/// The various "big phases" that MIR goes through.
|
|
|
|
///
|
2020-08-14 18:01:14 +02:00
|
|
|
/// These phases all describe dialects of MIR. Since all MIR uses the same datastructures, the
|
2022-03-25 20:08:35 -04:00
|
|
|
/// dialects forbid certain variants or values in certain phases. The sections below summarize the
|
|
|
|
/// changes, but do not document them thoroughly. The full documentation is found in the appropriate
|
|
|
|
/// documentation for the thing the change is affecting.
|
2020-08-14 18:01:14 +02:00
|
|
|
///
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Warning: ordering of variants is significant.
|
2020-06-11 15:49:57 +01:00
|
|
|
#[derive(Copy, Clone, TyEncodable, TyDecodable, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
2020-03-23 14:48:59 +00:00
|
|
|
#[derive(HashStable)]
|
2018-10-20 16:18:17 -04:00
|
|
|
pub enum MirPhase {
|
2022-03-25 20:08:35 -04:00
|
|
|
/// The dialect of MIR used during all phases before `DropsLowered` is the same. This is also
|
|
|
|
/// the MIR that analysis such as borrowck uses.
|
|
|
|
///
|
|
|
|
/// One important thing to remember about the behavior of this section of MIR is that drop terminators
|
|
|
|
/// (including drop and replace) are *conditional*. The elaborate drops pass will then replace each
|
|
|
|
/// instance of a drop terminator with a nop, an unconditional drop, or a drop conditioned on a drop
|
|
|
|
/// flag. Of course, this means that it is important that the drop elaboration can accurately recognize
|
|
|
|
/// when things are initialized and when things are de-initialized. That means any code running on this
|
|
|
|
/// version of MIR must be sure to produce output that drop elaboration can reason about. See the
|
|
|
|
/// section on the drop terminatorss for more details.
|
2022-03-05 20:37:04 -05:00
|
|
|
Built = 0,
|
2020-08-20 16:56:19 +02:00
|
|
|
// FIXME(oli-obk): it's unclear whether we still need this phase (and its corresponding query).
|
2020-08-14 18:01:14 +02:00
|
|
|
// We used to have this for pre-miri MIR based const eval.
|
2018-10-25 08:35:53 -04:00
|
|
|
Const = 1,
|
2020-08-14 18:01:14 +02:00
|
|
|
/// This phase checks the MIR for promotable elements and takes them out of the main MIR body
|
|
|
|
/// by creating a new MIR body per promoted element. After this phase (and thus the termination
|
|
|
|
/// of the `mir_promoted` query), these promoted elements are available in the `promoted_mir`
|
|
|
|
/// query.
|
2022-03-05 20:37:04 -05:00
|
|
|
ConstsPromoted = 2,
|
|
|
|
/// Beginning with this phase, the following variants are disallowed:
|
|
|
|
/// * [`TerminatorKind::DropAndReplace`](terminator::TerminatorKind::DropAndReplace)
|
|
|
|
/// * [`TerminatorKind::FalseUnwind`](terminator::TerminatorKind::FalseUnwind)
|
|
|
|
/// * [`TerminatorKind::FalseEdge`](terminator::TerminatorKind::FalseEdge)
|
|
|
|
/// * [`StatementKind::FakeRead`]
|
|
|
|
/// * [`StatementKind::AscribeUserType`]
|
|
|
|
/// * [`Rvalue::Ref`] with `BorrowKind::Shallow`
|
|
|
|
///
|
|
|
|
/// And the following variant is allowed:
|
|
|
|
/// * [`StatementKind::Retag`]
|
|
|
|
///
|
|
|
|
/// Furthermore, `Drop` now uses explicit drop flags visible in the MIR and reaching a `Drop`
|
2022-04-22 16:43:26 -04:00
|
|
|
/// terminator means that the auto-generated drop glue will be invoked. Also, `Copy` operands
|
|
|
|
/// are allowed for non-`Copy` types.
|
2022-03-05 20:37:04 -05:00
|
|
|
DropsLowered = 3,
|
|
|
|
/// Beginning with this phase, the following variant is disallowed:
|
|
|
|
/// * [`Rvalue::Aggregate`] for any `AggregateKind` except `Array`
|
|
|
|
///
|
|
|
|
/// And the following variant is allowed:
|
|
|
|
/// * [`StatementKind::SetDiscriminant`]
|
|
|
|
Deaggregated = 4,
|
2022-03-25 20:08:35 -04:00
|
|
|
/// Before this phase, generators are in the "source code" form, featuring `yield` statements
|
|
|
|
/// and such. With this phase change, they are transformed into a proper state machine. Running
|
|
|
|
/// optimizations before this change can be potentially dangerous because the source code is to
|
|
|
|
/// some extent a "lie." In particular, `yield` terminators effectively make the value of all
|
|
|
|
/// locals visible to the caller. This means that dead store elimination before them, or code
|
|
|
|
/// motion across them, is not correct in general. This is also exasperated by type checking
|
|
|
|
/// having pre-computed a list of the types that it thinks are ok to be live across a yield
|
|
|
|
/// point - this is necessary to decide eg whether autotraits are implemented. Introducing new
|
|
|
|
/// types across a yield point will lead to ICEs becaues of this.
|
|
|
|
///
|
2022-03-05 20:37:04 -05:00
|
|
|
/// Beginning with this phase, the following variants are disallowed:
|
|
|
|
/// * [`TerminatorKind::Yield`](terminator::TerminatorKind::Yield)
|
2022-05-21 16:21:44 -05:00
|
|
|
/// * [`TerminatorKind::GeneratorDrop`](terminator::TerminatorKind::GeneratorDrop)
|
2022-03-05 20:37:04 -05:00
|
|
|
GeneratorsLowered = 5,
|
|
|
|
Optimized = 6,
|
2018-10-20 16:18:17 -04:00
|
|
|
}
|
|
|
|
|
2018-10-21 10:47:01 -04:00
|
|
|
impl MirPhase {
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Gets the index of the current MirPhase within the set of all `MirPhase`s.
|
2018-10-21 10:47:01 -04:00
|
|
|
pub fn phase_index(&self) -> usize {
|
2018-10-25 08:35:53 -04:00
|
|
|
*self as usize
|
2018-10-21 10:47:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-04 11:01:13 -07:00
|
|
|
/// Where a specific `mir::Body` comes from.
|
2022-01-18 10:05:41 -06:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
2020-10-04 11:01:13 -07:00
|
|
|
#[derive(HashStable, TyEncodable, TyDecodable, TypeFoldable)]
|
|
|
|
pub struct MirSource<'tcx> {
|
|
|
|
pub instance: InstanceDef<'tcx>,
|
|
|
|
|
|
|
|
/// If `Some`, this is a promoted rvalue within the parent function.
|
|
|
|
pub promoted: Option<Promoted>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> MirSource<'tcx> {
|
|
|
|
pub fn item(def_id: DefId) -> Self {
|
|
|
|
MirSource {
|
|
|
|
instance: InstanceDef::Item(ty::WithOptConstParam::unknown(def_id)),
|
|
|
|
promoted: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_instance(instance: InstanceDef<'tcx>) -> Self {
|
|
|
|
MirSource { instance, promoted: None }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_opt_param(self) -> ty::WithOptConstParam<DefId> {
|
|
|
|
self.instance.with_opt_param()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn def_id(&self) -> DefId {
|
|
|
|
self.instance.def_id()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 13:27:05 +01:00
|
|
|
#[derive(Clone, TyEncodable, TyDecodable, Debug, HashStable, TypeFoldable)]
|
|
|
|
pub struct GeneratorInfo<'tcx> {
|
|
|
|
/// The yield type of the function, if it is a generator.
|
|
|
|
pub yield_ty: Option<Ty<'tcx>>,
|
|
|
|
|
|
|
|
/// Generator drop glue.
|
|
|
|
pub generator_drop: Option<Body<'tcx>>,
|
|
|
|
|
|
|
|
/// The layout of a generator. Produced by the state transformation.
|
|
|
|
pub generator_layout: Option<GeneratorLayout<'tcx>>,
|
|
|
|
|
|
|
|
/// If this is a generator then record the type of source expression that caused this generator
|
|
|
|
/// to be created.
|
|
|
|
pub generator_kind: GeneratorKind,
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// The lowered representation of a single function.
|
2020-06-11 15:49:57 +01:00
|
|
|
#[derive(Clone, TyEncodable, TyDecodable, Debug, HashStable, TypeFoldable)]
|
2019-05-17 23:55:04 +02:00
|
|
|
pub struct Body<'tcx> {
|
2020-10-16 20:35:56 -07:00
|
|
|
/// A list of basic blocks. References to basic block use a newtyped index type [`BasicBlock`]
|
2015-11-12 14:29:23 -05:00
|
|
|
/// that indexes into this vector.
|
2016-06-07 21:20:50 +03:00
|
|
|
basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2018-10-20 16:18:17 -04:00
|
|
|
/// Records how far through the "desugaring and optimization" process this particular
|
|
|
|
/// MIR has traversed. This is particularly useful when inlining, since in that context
|
|
|
|
/// we instantiate the promoted constants and add them to our promoted vector -- but those
|
|
|
|
/// promoted items have already been optimized, whereas ours have not. This field allows
|
|
|
|
/// us to see the difference and forego optimization on the inlined promoted items.
|
|
|
|
pub phase: MirPhase,
|
|
|
|
|
2020-10-04 11:01:38 -07:00
|
|
|
pub source: MirSource<'tcx>,
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// A list of source scopes; these are referenced by statements
|
2018-05-28 14:16:09 +03:00
|
|
|
/// and used for debuginfo. Indexed by a `SourceScope`.
|
2020-02-08 21:31:09 +02:00
|
|
|
pub source_scopes: IndexVec<SourceScope, SourceScopeData<'tcx>>,
|
2016-03-09 11:04:26 -05:00
|
|
|
|
2021-01-17 13:27:05 +01:00
|
|
|
pub generator: Option<Box<GeneratorInfo<'tcx>>>,
|
2019-11-25 12:58:40 +00:00
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
/// Declarations of locals.
|
|
|
|
///
|
|
|
|
/// The first local is the return value pointer, followed by `arg_count`
|
|
|
|
/// locals for the function arguments, followed by any user-declared
|
|
|
|
/// variables and temporaries.
|
2017-07-11 16:02:06 -07:00
|
|
|
pub local_decls: LocalDecls<'tcx>,
|
2015-11-12 14:29:23 -05:00
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// User type annotations.
|
2020-10-04 10:59:11 -07:00
|
|
|
pub user_type_annotations: ty::CanonicalUserTypeAnnotations<'tcx>,
|
2018-11-16 22:56:18 +01:00
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// The number of arguments this function takes.
|
2016-09-25 01:38:27 +02:00
|
|
|
///
|
2016-09-29 01:11:54 +02:00
|
|
|
/// Starting at local 1, `arg_count` locals will be provided by the caller
|
2016-09-25 01:38:27 +02:00
|
|
|
/// and can be assumed to be initialized.
|
|
|
|
///
|
|
|
|
/// If this MIR was built for a constant, this will be 0.
|
|
|
|
pub arg_count: usize,
|
2016-02-07 21:13:00 +02:00
|
|
|
|
2016-09-26 22:44:01 +02:00
|
|
|
/// Mark an argument local (which must be a tuple) as getting passed as
|
|
|
|
/// its individual components at the LLVM level.
|
2016-09-21 00:45:30 +02:00
|
|
|
///
|
|
|
|
/// This is used for the "rust-call" ABI.
|
2016-09-26 22:44:01 +02:00
|
|
|
pub spread_arg: Option<Local>,
|
2016-09-21 00:45:30 +02:00
|
|
|
|
2018-05-16 18:58:54 +03:00
|
|
|
/// Debug information pertaining to user variables, including captures.
|
|
|
|
pub var_debug_info: Vec<VarDebugInfo<'tcx>>,
|
2018-05-16 15:40:32 +03:00
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// A span representing this MIR, for error reporting.
|
2016-02-07 21:13:00 +02:00
|
|
|
pub span: Span,
|
2019-11-22 17:26:09 -03:00
|
|
|
|
2020-04-22 11:16:06 -03:00
|
|
|
/// Constants that are required to evaluate successfully for this MIR to be well-formed.
|
|
|
|
/// We hold in this field all the constants we are not able to evaluate yet.
|
|
|
|
pub required_consts: Vec<Constant<'tcx>>,
|
2020-04-03 18:26:24 -03:00
|
|
|
|
2020-08-06 10:00:08 +02:00
|
|
|
/// Does this body use generic parameters. This is used for the `ConstEvaluatable` check.
|
|
|
|
///
|
|
|
|
/// Note that this does not actually mean that this body is not computable right now.
|
|
|
|
/// The repeat count in the following example is polymorphic, but can still be evaluated
|
|
|
|
/// without knowing anything about the type parameter `T`.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// fn test<T>() {
|
|
|
|
/// let _ = [0; std::mem::size_of::<*mut T>()];
|
|
|
|
/// }
|
|
|
|
/// ```
|
2020-08-06 22:12:21 +02:00
|
|
|
///
|
|
|
|
/// **WARNING**: Do not change this flags after the MIR was originally created, even if an optimization
|
|
|
|
/// removed the last mention of all generic params. We do not want to rely on optimizations and
|
|
|
|
/// potentially allow things like `[u8; std::mem::size_of::<T>() * 0]` due to this.
|
2020-08-06 10:00:08 +02:00
|
|
|
pub is_polymorphic: bool,
|
|
|
|
|
2020-04-22 18:56:23 -07:00
|
|
|
predecessor_cache: PredecessorCache,
|
2022-03-25 21:11:49 -04:00
|
|
|
switch_source_cache: SwitchSourceCache,
|
2020-12-20 10:29:02 +01:00
|
|
|
is_cyclic: GraphIsCyclicCache,
|
2022-04-28 11:31:08 +08:00
|
|
|
postorder_cache: PostorderCache,
|
2022-02-07 22:00:15 -08:00
|
|
|
|
2022-01-23 12:34:26 -06:00
|
|
|
pub tainted_by_errors: Option<ErrorGuaranteed>,
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2019-05-17 23:55:04 +02:00
|
|
|
impl<'tcx> Body<'tcx> {
|
2018-06-19 21:22:52 -03:00
|
|
|
pub fn new(
|
2020-10-04 11:01:38 -07:00
|
|
|
source: MirSource<'tcx>,
|
2018-06-19 21:22:52 -03:00
|
|
|
basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
|
2020-02-08 21:31:09 +02:00
|
|
|
source_scopes: IndexVec<SourceScope, SourceScopeData<'tcx>>,
|
2018-11-16 22:56:18 +01:00
|
|
|
local_decls: LocalDecls<'tcx>,
|
2020-10-04 10:59:11 -07:00
|
|
|
user_type_annotations: ty::CanonicalUserTypeAnnotations<'tcx>,
|
2018-06-19 21:22:52 -03:00
|
|
|
arg_count: usize,
|
2018-05-16 18:58:54 +03:00
|
|
|
var_debug_info: Vec<VarDebugInfo<'tcx>>,
|
2018-06-19 21:22:52 -03:00
|
|
|
span: Span,
|
2019-11-25 12:58:40 +00:00
|
|
|
generator_kind: Option<GeneratorKind>,
|
2022-01-23 12:34:26 -06:00
|
|
|
tainted_by_errors: Option<ErrorGuaranteed>,
|
2018-06-19 21:22:52 -03:00
|
|
|
) -> Self {
|
2019-09-06 03:57:44 +01:00
|
|
|
// We need `arg_count` locals, and one for the return place.
|
2018-06-19 21:22:52 -03:00
|
|
|
assert!(
|
2020-03-02 18:53:56 +01:00
|
|
|
local_decls.len() > arg_count,
|
2018-06-19 21:22:52 -03:00
|
|
|
"expected at least {} locals, got {}",
|
|
|
|
arg_count + 1,
|
|
|
|
local_decls.len()
|
|
|
|
);
|
2016-09-25 01:38:27 +02:00
|
|
|
|
2020-08-06 10:00:08 +02:00
|
|
|
let mut body = Body {
|
2022-03-05 20:37:04 -05:00
|
|
|
phase: MirPhase::Built,
|
2020-10-04 11:01:38 -07:00
|
|
|
source,
|
2017-07-03 11:19:51 -07:00
|
|
|
basic_blocks,
|
2018-05-28 14:16:09 +03:00
|
|
|
source_scopes,
|
2021-01-17 13:27:05 +01:00
|
|
|
generator: generator_kind.map(|generator_kind| {
|
|
|
|
Box::new(GeneratorInfo {
|
|
|
|
yield_ty: None,
|
|
|
|
generator_drop: None,
|
|
|
|
generator_layout: None,
|
|
|
|
generator_kind,
|
|
|
|
})
|
|
|
|
}),
|
2017-07-03 11:19:51 -07:00
|
|
|
local_decls,
|
2018-11-16 22:56:18 +01:00
|
|
|
user_type_annotations,
|
2017-07-03 11:19:51 -07:00
|
|
|
arg_count,
|
2016-09-26 22:44:01 +02:00
|
|
|
spread_arg: None,
|
2018-05-16 18:58:54 +03:00
|
|
|
var_debug_info,
|
2017-07-03 11:19:51 -07:00
|
|
|
span,
|
2020-04-22 11:16:06 -03:00
|
|
|
required_consts: Vec::new(),
|
2020-08-06 10:00:08 +02:00
|
|
|
is_polymorphic: false,
|
2020-04-12 10:30:07 -07:00
|
|
|
predecessor_cache: PredecessorCache::new(),
|
2022-03-25 21:11:49 -04:00
|
|
|
switch_source_cache: SwitchSourceCache::new(),
|
2020-12-20 10:29:02 +01:00
|
|
|
is_cyclic: GraphIsCyclicCache::new(),
|
2022-04-28 11:31:08 +08:00
|
|
|
postorder_cache: PostorderCache::new(),
|
2022-02-07 22:00:15 -08:00
|
|
|
tainted_by_errors,
|
2020-08-06 10:00:08 +02:00
|
|
|
};
|
2022-01-12 03:19:52 +00:00
|
|
|
body.is_polymorphic = body.has_param_types_or_consts();
|
2020-08-06 10:00:08 +02:00
|
|
|
body
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2019-11-12 16:12:15 -08:00
|
|
|
/// Returns a partially initialized MIR body containing only a list of basic blocks.
|
|
|
|
///
|
|
|
|
/// The returned MIR contains no `LocalDecl`s (even for the return place) or source scopes. It
|
|
|
|
/// is only useful for testing but cannot be `#[cfg(test)]` because it is used in a different
|
|
|
|
/// crate.
|
2021-07-19 17:20:24 +02:00
|
|
|
pub fn new_cfg_only(basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>) -> Self {
|
2022-01-12 03:19:52 +00:00
|
|
|
let mut body = Body {
|
2022-03-05 20:37:04 -05:00
|
|
|
phase: MirPhase::Built,
|
2022-04-15 19:27:53 +02:00
|
|
|
source: MirSource::item(CRATE_DEF_ID.to_def_id()),
|
2019-11-12 16:12:15 -08:00
|
|
|
basic_blocks,
|
|
|
|
source_scopes: IndexVec::new(),
|
2021-01-17 13:27:05 +01:00
|
|
|
generator: None,
|
2019-11-12 16:12:15 -08:00
|
|
|
local_decls: IndexVec::new(),
|
|
|
|
user_type_annotations: IndexVec::new(),
|
|
|
|
arg_count: 0,
|
|
|
|
spread_arg: None,
|
|
|
|
span: DUMMY_SP,
|
2020-04-22 11:16:06 -03:00
|
|
|
required_consts: Vec::new(),
|
2019-11-12 16:12:15 -08:00
|
|
|
var_debug_info: Vec::new(),
|
2020-08-06 10:00:08 +02:00
|
|
|
is_polymorphic: false,
|
2020-04-12 10:30:07 -07:00
|
|
|
predecessor_cache: PredecessorCache::new(),
|
2022-03-25 21:11:49 -04:00
|
|
|
switch_source_cache: SwitchSourceCache::new(),
|
2020-12-20 10:29:02 +01:00
|
|
|
is_cyclic: GraphIsCyclicCache::new(),
|
2022-04-28 11:31:08 +08:00
|
|
|
postorder_cache: PostorderCache::new(),
|
2022-02-07 22:00:15 -08:00
|
|
|
tainted_by_errors: None,
|
2022-01-12 03:19:52 +00:00
|
|
|
};
|
|
|
|
body.is_polymorphic = body.has_param_types_or_consts();
|
|
|
|
body
|
2019-11-12 16:12:15 -08:00
|
|
|
}
|
|
|
|
|
2016-06-07 21:20:50 +03:00
|
|
|
#[inline]
|
|
|
|
pub fn basic_blocks(&self) -> &IndexVec<BasicBlock, BasicBlockData<'tcx>> {
|
|
|
|
&self.basic_blocks
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2020-04-12 10:30:07 -07:00
|
|
|
#[inline]
|
|
|
|
pub fn basic_blocks_mut(&mut self) -> &mut IndexVec<BasicBlock, BasicBlockData<'tcx>> {
|
|
|
|
// Because the user could mutate basic block terminators via this reference, we need to
|
2020-12-20 10:29:02 +01:00
|
|
|
// invalidate the caches.
|
2020-04-12 10:30:07 -07:00
|
|
|
//
|
|
|
|
// FIXME: Use a finer-grained API for this, so only transformations that alter terminators
|
2020-12-20 10:29:02 +01:00
|
|
|
// invalidate the caches.
|
2020-04-12 10:30:07 -07:00
|
|
|
self.predecessor_cache.invalidate();
|
2022-03-25 21:11:49 -04:00
|
|
|
self.switch_source_cache.invalidate();
|
2020-12-20 10:29:02 +01:00
|
|
|
self.is_cyclic.invalidate();
|
2022-04-28 11:31:08 +08:00
|
|
|
self.postorder_cache.invalidate();
|
2020-04-12 10:30:07 -07:00
|
|
|
&mut self.basic_blocks
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn basic_blocks_and_local_decls_mut(
|
|
|
|
&mut self,
|
|
|
|
) -> (&mut IndexVec<BasicBlock, BasicBlockData<'tcx>>, &mut LocalDecls<'tcx>) {
|
|
|
|
self.predecessor_cache.invalidate();
|
2022-03-25 21:11:49 -04:00
|
|
|
self.switch_source_cache.invalidate();
|
2020-12-20 10:29:02 +01:00
|
|
|
self.is_cyclic.invalidate();
|
2022-04-28 11:31:08 +08:00
|
|
|
self.postorder_cache.invalidate();
|
2020-04-12 10:30:07 -07:00
|
|
|
(&mut self.basic_blocks, &mut self.local_decls)
|
|
|
|
}
|
|
|
|
|
2020-06-21 19:34:54 -04:00
|
|
|
#[inline]
|
|
|
|
pub fn basic_blocks_local_decls_mut_and_var_debug_info(
|
|
|
|
&mut self,
|
|
|
|
) -> (
|
|
|
|
&mut IndexVec<BasicBlock, BasicBlockData<'tcx>>,
|
|
|
|
&mut LocalDecls<'tcx>,
|
|
|
|
&mut Vec<VarDebugInfo<'tcx>>,
|
|
|
|
) {
|
|
|
|
self.predecessor_cache.invalidate();
|
2022-03-25 21:11:49 -04:00
|
|
|
self.switch_source_cache.invalidate();
|
2020-12-20 10:29:02 +01:00
|
|
|
self.is_cyclic.invalidate();
|
2022-04-28 11:31:08 +08:00
|
|
|
self.postorder_cache.invalidate();
|
2020-06-21 19:34:54 -04:00
|
|
|
(&mut self.basic_blocks, &mut self.local_decls, &mut self.var_debug_info)
|
|
|
|
}
|
|
|
|
|
2019-09-18 14:53:55 -07:00
|
|
|
/// Returns `true` if a cycle exists in the control-flow graph that is reachable from the
|
|
|
|
/// `START_BLOCK`.
|
|
|
|
pub fn is_cfg_cyclic(&self) -> bool {
|
2020-12-20 10:29:02 +01:00
|
|
|
self.is_cyclic.is_cyclic(self)
|
2019-09-18 14:53:55 -07:00
|
|
|
}
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
#[inline]
|
|
|
|
pub fn local_kind(&self, local: Local) -> LocalKind {
|
2018-08-28 12:20:56 -04:00
|
|
|
let index = local.as_usize();
|
2016-09-25 01:38:27 +02:00
|
|
|
if index == 0 {
|
2018-06-19 21:22:52 -03:00
|
|
|
debug_assert!(
|
|
|
|
self.local_decls[local].mutability == Mutability::Mut,
|
|
|
|
"return place should be mutable"
|
|
|
|
);
|
2016-09-25 01:38:27 +02:00
|
|
|
|
|
|
|
LocalKind::ReturnPointer
|
|
|
|
} else if index < self.arg_count + 1 {
|
|
|
|
LocalKind::Arg
|
2018-05-16 18:58:54 +03:00
|
|
|
} else if self.local_decls[local].is_user_variable() {
|
2016-09-25 01:38:27 +02:00
|
|
|
LocalKind::Var
|
|
|
|
} else {
|
|
|
|
LocalKind::Temp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 13:40:55 +01:00
|
|
|
/// Returns an iterator over all user-declared mutable locals.
|
|
|
|
#[inline]
|
2022-05-22 12:48:19 -07:00
|
|
|
pub fn mut_vars_iter<'a>(&'a self) -> impl Iterator<Item = Local> + Captures<'tcx> + 'a {
|
2018-11-07 13:40:55 +01:00
|
|
|
(self.arg_count + 1..self.local_decls.len()).filter_map(move |index| {
|
|
|
|
let local = Local::new(index);
|
|
|
|
let decl = &self.local_decls[local];
|
2019-11-18 23:04:06 +00:00
|
|
|
if decl.is_user_variable() && decl.mutability == Mutability::Mut {
|
2018-11-07 13:40:55 +01:00
|
|
|
Some(local)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-12 08:47:44 -07:00
|
|
|
/// Returns an iterator over all user-declared mutable arguments and locals.
|
2018-02-28 01:09:08 -08:00
|
|
|
#[inline]
|
2022-05-22 12:48:19 -07:00
|
|
|
pub fn mut_vars_and_args_iter<'a>(
|
|
|
|
&'a self,
|
|
|
|
) -> impl Iterator<Item = Local> + Captures<'tcx> + 'a {
|
2018-03-12 08:47:44 -07:00
|
|
|
(1..self.local_decls.len()).filter_map(move |index| {
|
2018-02-28 01:09:08 -08:00
|
|
|
let local = Local::new(index);
|
|
|
|
let decl = &self.local_decls[local];
|
2019-11-18 23:04:06 +00:00
|
|
|
if (decl.is_user_variable() || index < self.arg_count + 1)
|
2018-06-19 21:22:52 -03:00
|
|
|
&& decl.mutability == Mutability::Mut
|
2018-03-12 08:47:44 -07:00
|
|
|
{
|
2018-02-28 01:09:08 -08:00
|
|
|
Some(local)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
/// Returns an iterator over all function arguments.
|
|
|
|
#[inline]
|
2019-12-07 19:37:09 +01:00
|
|
|
pub fn args_iter(&self) -> impl Iterator<Item = Local> + ExactSizeIterator {
|
2021-08-31 15:53:50 +02:00
|
|
|
(1..self.arg_count + 1).map(Local::new)
|
2016-09-25 01:38:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator over all user-defined variables and compiler-generated temporaries (all
|
2017-12-06 09:25:29 +01:00
|
|
|
/// locals that are neither arguments nor the return place).
|
2016-09-25 01:38:27 +02:00
|
|
|
#[inline]
|
2020-11-15 00:00:00 +00:00
|
|
|
pub fn vars_and_temps_iter(
|
|
|
|
&self,
|
|
|
|
) -> impl DoubleEndedIterator<Item = Local> + ExactSizeIterator {
|
2021-08-31 15:53:50 +02:00
|
|
|
(self.arg_count + 1..self.local_decls.len()).map(Local::new)
|
2016-09-15 18:18:40 -07:00
|
|
|
}
|
|
|
|
|
2021-06-03 19:24:48 +02:00
|
|
|
#[inline]
|
|
|
|
pub fn drain_vars_and_temps<'a>(&'a mut self) -> impl Iterator<Item = LocalDecl<'tcx>> + 'a {
|
|
|
|
self.local_decls.drain(self.arg_count + 1..)
|
|
|
|
}
|
|
|
|
|
2016-09-15 18:17:58 -07:00
|
|
|
/// Changes a statement to a nop. This is both faster than deleting instructions and avoids
|
|
|
|
/// invalidating statement indices in `Location`s.
|
|
|
|
pub fn make_statement_nop(&mut self, location: Location) {
|
2019-10-09 23:22:58 -04:00
|
|
|
let block = &mut self.basic_blocks[location.block];
|
2016-09-15 18:17:58 -07:00
|
|
|
debug_assert!(location.statement_index < block.statements.len());
|
|
|
|
block.statements[location.statement_index].make_nop()
|
|
|
|
}
|
2017-12-06 09:25:29 +01:00
|
|
|
|
|
|
|
/// Returns the source info associated with `location`.
|
|
|
|
pub fn source_info(&self, location: Location) -> &SourceInfo {
|
|
|
|
let block = &self[location.block];
|
|
|
|
let stmts = &block.statements;
|
|
|
|
let idx = location.statement_index;
|
|
|
|
if idx < stmts.len() {
|
|
|
|
&stmts[idx].source_info
|
|
|
|
} else {
|
2018-10-03 15:07:18 +02:00
|
|
|
assert_eq!(idx, stmts.len());
|
2017-12-06 09:25:29 +01:00
|
|
|
&block.terminator().source_info
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Returns the return type; it always return first element from `local_decls` array.
|
2020-04-12 10:48:56 -07:00
|
|
|
#[inline]
|
2017-12-06 09:25:29 +01:00
|
|
|
pub fn return_ty(&self) -> Ty<'tcx> {
|
|
|
|
self.local_decls[RETURN_PLACE].ty
|
|
|
|
}
|
2018-08-12 13:07:14 -04:00
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Gets the location of the terminator for the given block.
|
2020-04-12 10:48:56 -07:00
|
|
|
#[inline]
|
2018-08-12 13:07:14 -04:00
|
|
|
pub fn terminator_loc(&self, bb: BasicBlock) -> Location {
|
2019-07-12 22:49:15 +02:00
|
|
|
Location { block: bb, statement_index: self[bb].statements.len() }
|
2018-08-12 13:07:14 -04:00
|
|
|
}
|
2020-04-12 10:30:07 -07:00
|
|
|
|
2021-03-15 15:09:06 -07:00
|
|
|
pub fn stmt_at(&self, location: Location) -> Either<&Statement<'tcx>, &Terminator<'tcx>> {
|
|
|
|
let Location { block, statement_index } = location;
|
|
|
|
let block_data = &self.basic_blocks[block];
|
|
|
|
block_data
|
|
|
|
.statements
|
|
|
|
.get(statement_index)
|
|
|
|
.map(Either::Left)
|
|
|
|
.unwrap_or_else(|| Either::Right(block_data.terminator()))
|
|
|
|
}
|
|
|
|
|
2020-04-12 10:48:56 -07:00
|
|
|
#[inline]
|
2021-05-07 21:00:03 -04:00
|
|
|
pub fn predecessors(&self) -> &Predecessors {
|
2020-04-12 10:30:07 -07:00
|
|
|
self.predecessor_cache.compute(&self.basic_blocks)
|
|
|
|
}
|
|
|
|
|
2022-05-08 00:00:00 +00:00
|
|
|
/// `body.switch_sources()[&(target, switch)]` returns a list of switch
|
|
|
|
/// values that lead to a `target` block from a `switch` block.
|
2022-03-25 21:11:49 -04:00
|
|
|
#[inline]
|
|
|
|
pub fn switch_sources(&self) -> &SwitchSources {
|
|
|
|
self.switch_source_cache.compute(&self.basic_blocks)
|
|
|
|
}
|
|
|
|
|
2020-04-12 10:30:07 -07:00
|
|
|
#[inline]
|
|
|
|
pub fn dominators(&self) -> Dominators<BasicBlock> {
|
|
|
|
dominators(self)
|
|
|
|
}
|
2021-01-17 13:27:05 +01:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn yield_ty(&self) -> Option<Ty<'tcx>> {
|
|
|
|
self.generator.as_ref().and_then(|generator| generator.yield_ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn generator_layout(&self) -> Option<&GeneratorLayout<'tcx>> {
|
|
|
|
self.generator.as_ref().and_then(|generator| generator.generator_layout.as_ref())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn generator_drop(&self) -> Option<&Body<'tcx>> {
|
|
|
|
self.generator.as_ref().and_then(|generator| generator.generator_drop.as_ref())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn generator_kind(&self) -> Option<GeneratorKind> {
|
|
|
|
self.generator.as_ref().map(|generator| generator.generator_kind)
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2020-06-11 15:49:57 +01:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug, TyEncodable, TyDecodable, HashStable)]
|
2017-09-19 16:20:02 +03:00
|
|
|
pub enum Safety {
|
|
|
|
Safe,
|
2021-04-29 20:54:22 -05:00
|
|
|
/// Unsafe because of compiler-generated unsafe code, like `await` desugaring
|
|
|
|
BuiltinUnsafe,
|
2017-09-19 16:20:02 +03:00
|
|
|
/// Unsafe because of an unsafe fn
|
|
|
|
FnUnsafe,
|
|
|
|
/// Unsafe because of an `unsafe` block
|
2019-02-22 15:48:14 +01:00
|
|
|
ExplicitUnsafe(hir::HirId),
|
2017-09-13 22:33:07 +03:00
|
|
|
}
|
|
|
|
|
2019-05-17 23:55:04 +02:00
|
|
|
impl<'tcx> Index<BasicBlock> for Body<'tcx> {
|
2016-01-07 05:49:46 -05:00
|
|
|
type Output = BasicBlockData<'tcx>;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: BasicBlock) -> &BasicBlockData<'tcx> {
|
2016-06-07 21:20:50 +03:00
|
|
|
&self.basic_blocks()[index]
|
2016-01-07 05:49:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 10:30:07 -07:00
|
|
|
impl<'tcx> IndexMut<BasicBlock> for Body<'tcx> {
|
|
|
|
#[inline]
|
|
|
|
fn index_mut(&mut self, index: BasicBlock) -> &mut BasicBlockData<'tcx> {
|
|
|
|
&mut self.basic_blocks_mut()[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 21:36:57 +01:00
|
|
|
#[derive(Copy, Clone, Debug, HashStable, TypeFoldable)]
|
2017-12-06 09:25:29 +01:00
|
|
|
pub enum ClearCrossCrate<T> {
|
2017-09-13 22:33:07 +03:00
|
|
|
Clear,
|
2018-06-19 21:22:52 -03:00
|
|
|
Set(T),
|
2017-09-13 22:33:07 +03:00
|
|
|
}
|
|
|
|
|
2018-07-03 06:50:54 -04:00
|
|
|
impl<T> ClearCrossCrate<T> {
|
2020-03-07 00:56:32 +01:00
|
|
|
pub fn as_ref(&self) -> ClearCrossCrate<&T> {
|
2019-11-26 19:55:03 +02:00
|
|
|
match self {
|
|
|
|
ClearCrossCrate::Clear => ClearCrossCrate::Clear,
|
|
|
|
ClearCrossCrate::Set(v) => ClearCrossCrate::Set(v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 06:50:54 -04:00
|
|
|
pub fn assert_crate_local(self) -> T {
|
|
|
|
match self {
|
|
|
|
ClearCrossCrate::Clear => bug!("unwrapping cross-crate data"),
|
|
|
|
ClearCrossCrate::Set(v) => v,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-01 18:58:18 +01:00
|
|
|
const TAG_CLEAR_CROSS_CRATE_CLEAR: u8 = 0;
|
|
|
|
const TAG_CLEAR_CROSS_CRATE_SET: u8 = 1;
|
|
|
|
|
2020-06-11 15:49:57 +01:00
|
|
|
impl<'tcx, E: TyEncoder<'tcx>, T: Encodable<E>> Encodable<E> for ClearCrossCrate<T> {
|
2020-06-01 18:58:18 +01:00
|
|
|
#[inline]
|
2020-06-11 15:49:57 +01:00
|
|
|
fn encode(&self, e: &mut E) -> Result<(), E::Error> {
|
|
|
|
if E::CLEAR_CROSS_CRATE {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2020-06-01 18:58:18 +01:00
|
|
|
match *self {
|
|
|
|
ClearCrossCrate::Clear => TAG_CLEAR_CROSS_CRATE_CLEAR.encode(e),
|
|
|
|
ClearCrossCrate::Set(ref val) => {
|
|
|
|
TAG_CLEAR_CROSS_CRATE_SET.encode(e)?;
|
|
|
|
val.encode(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-11 15:49:57 +01:00
|
|
|
impl<'tcx, D: TyDecoder<'tcx>, T: Decodable<D>> Decodable<D> for ClearCrossCrate<T> {
|
2020-06-01 18:58:18 +01:00
|
|
|
#[inline]
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
fn decode(d: &mut D) -> ClearCrossCrate<T> {
|
2020-06-11 15:49:57 +01:00
|
|
|
if D::CLEAR_CROSS_CRATE {
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
return ClearCrossCrate::Clear;
|
2020-06-11 15:49:57 +01:00
|
|
|
}
|
|
|
|
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
let discr = u8::decode(d);
|
2020-06-01 18:58:18 +01:00
|
|
|
|
|
|
|
match discr {
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
TAG_CLEAR_CROSS_CRATE_CLEAR => ClearCrossCrate::Clear,
|
2020-06-01 18:58:18 +01:00
|
|
|
TAG_CLEAR_CROSS_CRATE_SET => {
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
let val = T::decode(d);
|
|
|
|
ClearCrossCrate::Set(val)
|
2020-06-01 18:58:18 +01:00
|
|
|
}
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
tag => panic!("Invalid tag for ClearCrossCrate: {:?}", tag),
|
2020-06-01 18:58:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-13 22:33:07 +03:00
|
|
|
|
2016-06-07 19:21:56 +03:00
|
|
|
/// Grouped information about the source code origin of a MIR entity.
|
|
|
|
/// Intended to be inspected by diagnostics and debuginfo.
|
|
|
|
/// Most passes can work with it as a whole, within a single function.
|
2019-11-26 22:19:54 -05:00
|
|
|
// The unofficial Cranelift backend, at least as of #65828, needs `SourceInfo` to implement `Eq` and
|
2019-10-25 21:11:29 +02:00
|
|
|
// `Hash`. Please ping @bjorn3 if removing them.
|
2020-06-11 15:49:57 +01:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, TyEncodable, TyDecodable, Hash, HashStable)]
|
2016-06-07 19:21:56 +03:00
|
|
|
pub struct SourceInfo {
|
2019-09-06 03:57:44 +01:00
|
|
|
/// The source span for the AST pertaining to this MIR entity.
|
2016-06-07 19:21:56 +03:00
|
|
|
pub span: Span,
|
|
|
|
|
2018-05-28 14:16:09 +03:00
|
|
|
/// The source scope, keeping track of which bindings can be
|
|
|
|
/// seen by debuginfo, active lint levels, `unsafe {...}`, etc.
|
2018-06-19 21:22:52 -03:00
|
|
|
pub scope: SourceScope,
|
2016-06-07 19:21:56 +03:00
|
|
|
}
|
|
|
|
|
2020-05-06 10:30:11 +10:00
|
|
|
impl SourceInfo {
|
|
|
|
#[inline]
|
|
|
|
pub fn outermost(span: Span) -> Self {
|
|
|
|
SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2019-12-16 17:28:40 +01:00
|
|
|
// Borrow kinds
|
2018-06-07 15:25:08 +02:00
|
|
|
|
2020-06-11 15:49:57 +01:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, TyEncodable, TyDecodable)]
|
2020-10-04 15:52:14 +02:00
|
|
|
#[derive(Hash, HashStable)]
|
2015-08-18 17:59:21 -04:00
|
|
|
pub enum BorrowKind {
|
|
|
|
/// Data must be immutable and is aliasable.
|
|
|
|
Shared,
|
|
|
|
|
2018-09-10 22:33:45 +01:00
|
|
|
/// The immediately borrowed place must be immutable, but projections from
|
|
|
|
/// it don't need to be. For example, a shallow borrow of `a.b` doesn't
|
|
|
|
/// conflict with a mutable borrow of `a.b.c`.
|
|
|
|
///
|
|
|
|
/// This is used when lowering matches: when matching on a place we want to
|
|
|
|
/// ensure that place have the same value from the start of the match until
|
|
|
|
/// an arm is selected. This prevents this code from compiling:
|
2022-04-15 15:04:34 -07:00
|
|
|
/// ```compile_fail,E0510
|
|
|
|
/// let mut x = &Some(0);
|
|
|
|
/// match *x {
|
|
|
|
/// None => (),
|
|
|
|
/// Some(_) if { x = &None; false } => (),
|
|
|
|
/// Some(_) => (),
|
|
|
|
/// }
|
|
|
|
/// ```
|
2018-09-10 22:33:45 +01:00
|
|
|
/// This can't be a shared borrow because mutably borrowing (*x as Some).0
|
2018-10-22 18:21:55 +02:00
|
|
|
/// should not prevent `if let None = x { ... }`, for example, because the
|
2018-09-10 22:33:45 +01:00
|
|
|
/// mutating `(*x as Some).0` can't affect the discriminant of `x`.
|
|
|
|
/// We can also report errors with this kind of borrow differently.
|
|
|
|
Shallow,
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Data must be immutable but not aliasable. This kind of borrow
|
2015-08-18 17:59:21 -04:00
|
|
|
/// cannot currently be expressed by the user and is used only in
|
2018-08-01 23:53:28 -07:00
|
|
|
/// implicit closure bindings. It is needed when the closure is
|
|
|
|
/// borrowing or mutating a mutable referent, e.g.:
|
2022-04-15 15:04:34 -07:00
|
|
|
/// ```
|
|
|
|
/// let mut z = 3;
|
|
|
|
/// let x: &mut isize = &mut z;
|
|
|
|
/// let y = || *x += 5;
|
|
|
|
/// ```
|
2015-08-18 17:59:21 -04:00
|
|
|
/// If we were to try to translate this closure into a more explicit
|
|
|
|
/// form, we'd encounter an error with the code as written:
|
2022-04-15 15:04:34 -07:00
|
|
|
/// ```compile_fail,E0594
|
|
|
|
/// struct Env<'a> { x: &'a &'a mut isize }
|
|
|
|
/// let mut z = 3;
|
|
|
|
/// let x: &mut isize = &mut z;
|
|
|
|
/// let y = (&mut Env { x: &x }, fn_ptr); // Closure is pair of env and fn
|
|
|
|
/// fn fn_ptr(env: &mut Env) { **env.x += 5; }
|
|
|
|
/// ```
|
2018-08-01 23:53:28 -07:00
|
|
|
/// This is then illegal because you cannot mutate an `&mut` found
|
2015-08-18 17:59:21 -04:00
|
|
|
/// in an aliasable location. To solve, you'd have to translate with
|
|
|
|
/// an `&mut` borrow:
|
2022-04-15 15:04:34 -07:00
|
|
|
/// ```compile_fail,E0596
|
|
|
|
/// struct Env<'a> { x: &'a mut &'a mut isize }
|
|
|
|
/// let mut z = 3;
|
|
|
|
/// let x: &mut isize = &mut z;
|
|
|
|
/// let y = (&mut Env { x: &mut x }, fn_ptr); // changed from &x to &mut x
|
|
|
|
/// fn fn_ptr(env: &mut Env) { **env.x += 5; }
|
|
|
|
/// ```
|
2015-08-18 17:59:21 -04:00
|
|
|
/// Now the assignment to `**env.x` is legal, but creating a
|
|
|
|
/// mutable pointer to `x` is not because `x` is not mutable. We
|
|
|
|
/// could fix this by declaring `x` as `let mut x`. This is ok in
|
|
|
|
/// user code, if awkward, but extra weird for closures, since the
|
|
|
|
/// borrow is hidden.
|
|
|
|
///
|
|
|
|
/// So we introduce a "unique imm" borrow -- the referent is
|
|
|
|
/// immutable, but not aliasable. This solves the problem. For
|
|
|
|
/// simplicity, we don't give users the way to express this
|
|
|
|
/// borrow, it's just used when translating closures.
|
|
|
|
Unique,
|
|
|
|
|
|
|
|
/// Data is mutable and not aliasable.
|
2018-01-15 12:47:26 +01:00
|
|
|
Mut {
|
2019-02-08 14:53:55 +01:00
|
|
|
/// `true` if this borrow arose from method-call auto-ref
|
|
|
|
/// (i.e., `adjustment::Adjust::Borrow`).
|
2018-06-19 21:22:52 -03:00
|
|
|
allow_two_phase_borrow: bool,
|
|
|
|
},
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2018-01-16 10:52:52 +01:00
|
|
|
impl BorrowKind {
|
|
|
|
pub fn allows_two_phase_borrow(&self) -> bool {
|
|
|
|
match *self {
|
2018-09-10 22:33:45 +01:00
|
|
|
BorrowKind::Shared | BorrowKind::Shallow | BorrowKind::Unique => false,
|
2018-10-03 15:07:18 +02:00
|
|
|
BorrowKind::Mut { allow_two_phase_borrow } => allow_two_phase_borrow,
|
2018-01-16 10:52:52 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-17 02:51:27 -04:00
|
|
|
|
|
|
|
pub fn describe_mutability(&self) -> String {
|
|
|
|
match *self {
|
|
|
|
BorrowKind::Shared | BorrowKind::Shallow | BorrowKind::Unique => {
|
|
|
|
"immutable".to_string()
|
|
|
|
}
|
|
|
|
BorrowKind::Mut { .. } => "mutable".to_string(),
|
|
|
|
}
|
|
|
|
}
|
2018-01-16 10:52:52 +01:00
|
|
|
}
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Variables and temps
|
|
|
|
|
2019-09-26 05:38:33 +00:00
|
|
|
rustc_index::newtype_index! {
|
2018-07-25 13:41:32 +03:00
|
|
|
pub struct Local {
|
2018-12-03 01:14:35 +01:00
|
|
|
derive [HashStable]
|
2017-12-06 09:25:29 +01:00
|
|
|
DEBUG_FORMAT = "_{}",
|
|
|
|
const RETURN_PLACE = 0,
|
2018-07-25 13:41:32 +03:00
|
|
|
}
|
|
|
|
}
|
2016-09-25 01:38:27 +02:00
|
|
|
|
2019-07-12 22:48:02 +02:00
|
|
|
impl Atom for Local {
|
|
|
|
fn index(self) -> usize {
|
|
|
|
Idx::index(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 23:55:04 +02:00
|
|
|
/// Classifies locals into categories. See `Body::local_kind`.
|
2020-09-29 17:27:59 -07:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug, HashStable)]
|
2016-09-25 01:38:27 +02:00
|
|
|
pub enum LocalKind {
|
2019-09-06 03:57:44 +01:00
|
|
|
/// User-declared variable binding.
|
2016-09-25 01:38:27 +02:00
|
|
|
Var,
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Compiler-introduced temporary.
|
2016-09-25 01:38:27 +02:00
|
|
|
Temp,
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Function argument.
|
2016-09-25 01:38:27 +02:00
|
|
|
Arg,
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Location of function's return value.
|
2016-09-25 01:38:27 +02:00
|
|
|
ReturnPointer,
|
|
|
|
}
|
|
|
|
|
2020-06-11 15:49:57 +01:00
|
|
|
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
|
2018-06-27 22:06:54 +01:00
|
|
|
pub struct VarBindingForm<'tcx> {
|
2018-06-07 15:25:08 +02:00
|
|
|
/// Is variable bound via `x`, `mut x`, `ref x`, or `ref mut x`?
|
|
|
|
pub binding_mode: ty::BindingMode,
|
|
|
|
/// If an explicit type was provided for this variable binding,
|
|
|
|
/// this holds the source Span of that type.
|
|
|
|
///
|
2019-02-08 14:53:55 +01:00
|
|
|
/// NOTE: if you want to change this to a `HirId`, be wary that
|
2018-06-07 15:25:08 +02:00
|
|
|
/// doing so breaks incremental compilation (as of this writing),
|
|
|
|
/// while a `Span` does not cause our tests to fail.
|
|
|
|
pub opt_ty_info: Option<Span>,
|
2018-06-27 22:06:54 +01:00
|
|
|
/// Place of the RHS of the =, or the subject of the `match` where this
|
|
|
|
/// variable is initialized. None in the case of `let PATTERN;`.
|
|
|
|
/// Some((None, ..)) in the case of and `let [mut] x = ...` because
|
|
|
|
/// (a) the right-hand side isn't evaluated as a place expression.
|
|
|
|
/// (b) it gives a way to separate this case from the remaining cases
|
|
|
|
/// for diagnostics.
|
|
|
|
pub opt_match_place: Option<(Option<Place<'tcx>>, Span)>,
|
2019-09-06 03:57:44 +01:00
|
|
|
/// The span of the pattern in which this variable was bound.
|
2018-08-07 01:02:39 -07:00
|
|
|
pub pat_span: Span,
|
2018-06-07 15:25:08 +02:00
|
|
|
}
|
|
|
|
|
2020-06-11 15:49:57 +01:00
|
|
|
#[derive(Clone, Debug, TyEncodable, TyDecodable)]
|
2018-06-27 22:06:54 +01:00
|
|
|
pub enum BindingForm<'tcx> {
|
2018-06-07 15:25:08 +02:00
|
|
|
/// This is a binding for a non-`self` binding, or a `self` that has an explicit type.
|
2018-06-27 22:06:54 +01:00
|
|
|
Var(VarBindingForm<'tcx>),
|
2018-06-07 15:25:08 +02:00
|
|
|
/// Binding for a `self`/`&self`/`&mut self` binding where the type is implicit.
|
2018-10-01 17:46:04 +02:00
|
|
|
ImplicitSelf(ImplicitSelfKind),
|
2018-07-15 15:00:58 +01:00
|
|
|
/// Reference used in a guard expression to ensure immutability.
|
|
|
|
RefForGuard,
|
2018-06-07 15:25:08 +02:00
|
|
|
}
|
|
|
|
|
2018-10-01 17:46:04 +02:00
|
|
|
/// Represents what type of implicit self a function has, if any.
|
2020-06-11 15:49:57 +01:00
|
|
|
#[derive(Clone, Copy, PartialEq, Debug, TyEncodable, TyDecodable, HashStable)]
|
2018-10-01 17:46:04 +02:00
|
|
|
pub enum ImplicitSelfKind {
|
|
|
|
/// Represents a `fn x(self);`.
|
|
|
|
Imm,
|
|
|
|
/// Represents a `fn x(mut self);`.
|
|
|
|
Mut,
|
|
|
|
/// Represents a `fn x(&self);`.
|
|
|
|
ImmRef,
|
|
|
|
/// Represents a `fn x(&mut self);`.
|
|
|
|
MutRef,
|
|
|
|
/// Represents when a function does not have a self argument or
|
|
|
|
/// when a function has a `self: X` argument.
|
2019-07-12 22:49:15 +02:00
|
|
|
None,
|
2018-10-01 17:46:04 +02:00
|
|
|
}
|
|
|
|
|
2020-10-24 09:27:15 +02:00
|
|
|
TrivialTypeFoldableAndLiftImpls! { BindingForm<'tcx>, }
|
2018-06-07 15:25:08 +02:00
|
|
|
|
2018-06-27 22:06:54 +01:00
|
|
|
mod binding_form_impl {
|
2019-09-26 18:54:39 -04:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
2020-11-14 16:48:54 +01:00
|
|
|
use rustc_query_system::ich::StableHashingContext;
|
2018-06-07 15:25:08 +02:00
|
|
|
|
2018-06-27 22:06:54 +01:00
|
|
|
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for super::BindingForm<'tcx> {
|
2019-09-26 18:54:39 -04:00
|
|
|
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
|
2018-06-27 22:06:54 +01:00
|
|
|
use super::BindingForm::*;
|
2020-10-13 10:17:05 +02:00
|
|
|
std::mem::discriminant(self).hash_stable(hcx, hasher);
|
2018-06-27 22:06:54 +01:00
|
|
|
|
|
|
|
match self {
|
|
|
|
Var(binding) => binding.hash_stable(hcx, hasher),
|
2018-10-01 17:46:04 +02:00
|
|
|
ImplicitSelf(kind) => kind.hash_stable(hcx, hasher),
|
2018-07-15 15:00:58 +01:00
|
|
|
RefForGuard => (),
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-07 15:25:08 +02:00
|
|
|
|
2018-10-05 12:26:29 +02:00
|
|
|
/// `BlockTailInfo` is attached to the `LocalDecl` for temporaries
|
|
|
|
/// created during evaluation of expressions in a block tail
|
|
|
|
/// expression; that is, a block like `{ STMT_1; STMT_2; EXPR }`.
|
|
|
|
///
|
|
|
|
/// It is used to improve diagnostics when such temporaries are
|
2018-11-27 02:59:49 +00:00
|
|
|
/// involved in borrow_check errors, e.g., explanations of where the
|
2018-10-05 12:26:29 +02:00
|
|
|
/// temporaries come from, when their destructors are run, and/or how
|
|
|
|
/// one might revise the code to satisfy the borrow checker's rules.
|
2020-06-11 15:49:57 +01:00
|
|
|
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
|
2018-09-22 00:51:48 +02:00
|
|
|
pub struct BlockTailInfo {
|
|
|
|
/// If `true`, then the value resulting from evaluating this tail
|
|
|
|
/// expression is ignored by the block's expression context.
|
|
|
|
///
|
|
|
|
/// Examples include `{ ...; tail };` and `let _ = { ...; tail };`
|
2018-11-27 02:59:49 +00:00
|
|
|
/// but not e.g., `let _x = { ...; tail };`
|
2018-09-22 00:51:48 +02:00
|
|
|
pub tail_result_is_ignored: bool,
|
2020-04-16 12:43:40 -07:00
|
|
|
|
|
|
|
/// `Span` of the tail expression.
|
|
|
|
pub span: Span,
|
2018-09-22 00:51:48 +02:00
|
|
|
}
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
/// A MIR local.
|
|
|
|
///
|
|
|
|
/// This can be a binding declared by the user, a temporary inserted by the compiler, a function
|
2017-12-06 09:25:29 +01:00
|
|
|
/// argument, or the return place.
|
2020-06-11 15:49:57 +01:00
|
|
|
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
|
2016-09-25 01:38:27 +02:00
|
|
|
pub struct LocalDecl<'tcx> {
|
2020-10-08 14:21:12 +00:00
|
|
|
/// Whether this is a mutable binding (i.e., `let x` or `let mut x`).
|
2016-09-25 01:38:27 +02:00
|
|
|
///
|
2017-12-06 09:25:29 +01:00
|
|
|
/// Temporaries and the return place are always mutable.
|
2015-08-18 17:59:21 -04:00
|
|
|
pub mutability: Mutability,
|
2016-03-23 04:21:02 -04:00
|
|
|
|
2019-11-18 23:04:06 +00:00
|
|
|
// FIXME(matthewjasper) Don't store in this in `Body`
|
2020-05-06 12:29:00 +10:00
|
|
|
pub local_info: Option<Box<LocalInfo<'tcx>>>,
|
2017-04-11 23:52:51 +03:00
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// `true` if this is an internal local.
|
2017-08-24 11:42:32 -07:00
|
|
|
///
|
2017-08-11 06:20:28 +02:00
|
|
|
/// These locals are not based on types in the source code and are only used
|
2017-09-19 16:20:02 +03:00
|
|
|
/// for a few desugarings at the moment.
|
2017-08-24 11:42:32 -07:00
|
|
|
///
|
|
|
|
/// The generator transformation will sanity check the locals which are live
|
|
|
|
/// across a suspension point against the type components of the generator
|
|
|
|
/// which type checking knows are live across a suspension point. We need to
|
|
|
|
/// flag drop flags to avoid triggering this check as they are introduced
|
2021-10-25 15:43:07 -05:00
|
|
|
/// outside of type inference.
|
2017-08-24 11:42:32 -07:00
|
|
|
///
|
|
|
|
/// This should be sound because the drop flags are fully algebraic, and
|
2020-11-24 15:44:04 -08:00
|
|
|
/// therefore don't affect the auto-trait or outlives properties of the
|
2017-08-24 11:42:32 -07:00
|
|
|
/// generator.
|
2017-07-15 22:41:33 +02:00
|
|
|
pub internal: bool,
|
|
|
|
|
2018-09-22 00:51:48 +02:00
|
|
|
/// If this local is a temporary and `is_block_tail` is `Some`,
|
|
|
|
/// then it is a temporary created for evaluation of some
|
|
|
|
/// subexpression of some block's tail expression (with no
|
|
|
|
/// intervening statement context).
|
2019-11-18 23:04:06 +00:00
|
|
|
// FIXME(matthewjasper) Don't store in this in `Body`
|
2018-09-22 00:51:48 +02:00
|
|
|
pub is_block_tail: Option<BlockTailInfo>,
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// The type of this local.
|
2015-10-05 12:31:48 -04:00
|
|
|
pub ty: Ty<'tcx>,
|
2016-03-23 04:21:02 -04:00
|
|
|
|
2018-09-10 10:54:31 -04:00
|
|
|
/// If the user manually ascribed a type to this variable,
|
2018-11-27 02:59:49 +00:00
|
|
|
/// e.g., via `let x: T`, then we carry that type here. The MIR
|
2018-09-10 10:54:31 -04:00
|
|
|
/// borrow checker needs this information since it can affect
|
|
|
|
/// region inference.
|
2019-11-18 23:04:06 +00:00
|
|
|
// FIXME(matthewjasper) Don't store in this in `Body`
|
2020-05-06 12:41:15 +10:00
|
|
|
pub user_ty: Option<Box<UserTypeProjections>>,
|
2018-09-10 10:54:31 -04:00
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
/// The *syntactic* (i.e., not visibility) source scope the local is defined
|
2017-09-13 22:33:07 +03:00
|
|
|
/// in. If the local was defined in a let-statement, this
|
|
|
|
/// is *within* the let-statement, rather than outside
|
2017-09-20 16:34:31 +03:00
|
|
|
/// of it.
|
2017-12-21 00:35:19 +02:00
|
|
|
///
|
2018-05-28 14:16:09 +03:00
|
|
|
/// This is needed because the visibility source scope of locals within
|
|
|
|
/// a let-statement is weird.
|
2017-12-21 00:35:19 +02:00
|
|
|
///
|
|
|
|
/// The reason is that we want the local to be *within* the let-statement
|
|
|
|
/// for lint purposes, but we want the local to be *after* the let-statement
|
|
|
|
/// for names-in-scope purposes.
|
|
|
|
///
|
|
|
|
/// That's it, if we have a let-statement like the one in this
|
|
|
|
/// function:
|
2017-12-31 17:17:01 +01:00
|
|
|
///
|
2017-12-21 00:35:19 +02:00
|
|
|
/// ```
|
|
|
|
/// fn foo(x: &str) {
|
|
|
|
/// #[allow(unused_mut)]
|
|
|
|
/// let mut x: u32 = { // <- one unused mut
|
|
|
|
/// let mut y: u32 = x.parse().unwrap();
|
|
|
|
/// y + 2
|
|
|
|
/// };
|
|
|
|
/// drop(x);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Then, from a lint point of view, the declaration of `x: u32`
|
|
|
|
/// (and `y: u32`) are within the `#[allow(unused_mut)]` scope - the
|
|
|
|
/// lint scopes are the same as the AST/HIR nesting.
|
|
|
|
///
|
|
|
|
/// However, from a name lookup point of view, the scopes look more like
|
|
|
|
/// as if the let-statements were `match` expressions:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// fn foo(x: &str) {
|
|
|
|
/// match {
|
2022-04-15 15:04:34 -07:00
|
|
|
/// match x.parse::<u32>().unwrap() {
|
2017-12-21 00:35:19 +02:00
|
|
|
/// y => y + 2
|
|
|
|
/// }
|
|
|
|
/// } {
|
|
|
|
/// x => drop(x)
|
|
|
|
/// };
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// We care about the name-lookup scopes for debuginfo - if the
|
|
|
|
/// debuginfo instruction pointer is at the call to `x.parse()`, we
|
|
|
|
/// want `x` to refer to `x: &str`, but if it is at the call to
|
|
|
|
/// `drop(x)`, we want it to refer to `x: u32`.
|
|
|
|
///
|
|
|
|
/// To allow both uses to work, we need to have more than a single scope
|
2018-05-16 18:58:54 +03:00
|
|
|
/// for a local. We have the `source_info.scope` represent the "syntactic"
|
|
|
|
/// lint scope (with a variable being under its let block) while the
|
|
|
|
/// `var_debug_info.source_info.scope` represents the "local variable"
|
2017-12-21 00:35:19 +02:00
|
|
|
/// scope (where the "rest" of a block is under all prior let-statements).
|
|
|
|
///
|
|
|
|
/// The end result looks like this:
|
|
|
|
///
|
2017-12-31 17:17:01 +01:00
|
|
|
/// ```text
|
2017-12-21 00:35:19 +02:00
|
|
|
/// ROOT SCOPE
|
|
|
|
/// │{ argument x: &str }
|
|
|
|
/// │
|
2019-09-06 03:57:44 +01:00
|
|
|
/// │ │{ #[allow(unused_mut)] } // This is actually split into 2 scopes
|
|
|
|
/// │ │ // in practice because I'm lazy.
|
2017-12-21 00:35:19 +02:00
|
|
|
/// │ │
|
2018-05-29 21:31:33 +03:00
|
|
|
/// │ │← x.source_info.scope
|
2017-12-21 00:35:19 +02:00
|
|
|
/// │ │← `x.parse().unwrap()`
|
|
|
|
/// │ │
|
2018-05-29 21:31:33 +03:00
|
|
|
/// │ │ │← y.source_info.scope
|
2017-12-21 00:35:19 +02:00
|
|
|
/// │ │
|
|
|
|
/// │ │ │{ let y: u32 }
|
|
|
|
/// │ │ │
|
2018-05-16 18:58:54 +03:00
|
|
|
/// │ │ │← y.var_debug_info.source_info.scope
|
2017-12-21 00:35:19 +02:00
|
|
|
/// │ │ │← `y + 2`
|
|
|
|
/// │
|
|
|
|
/// │ │{ let x: u32 }
|
2018-05-16 18:58:54 +03:00
|
|
|
/// │ │← x.var_debug_info.source_info.scope
|
2019-09-06 03:57:44 +01:00
|
|
|
/// │ │← `drop(x)` // This accesses `x: u32`.
|
2017-12-31 17:17:01 +01:00
|
|
|
/// ```
|
2018-05-29 21:31:33 +03:00
|
|
|
pub source_info: SourceInfo,
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2020-05-06 09:35:11 +10:00
|
|
|
// `LocalDecl` is used a lot. Make sure it doesn't unintentionally get bigger.
|
2021-03-06 16:02:48 +00:00
|
|
|
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
|
2020-05-06 12:41:15 +10:00
|
|
|
static_assert_size!(LocalDecl<'_>, 56);
|
2020-05-06 09:35:11 +10:00
|
|
|
|
2020-08-12 19:37:08 +02:00
|
|
|
/// Extra information about a some locals that's used for diagnostics and for
|
|
|
|
/// classifying variables into local variables, statics, etc, which is needed e.g.
|
|
|
|
/// for unsafety checking.
|
|
|
|
///
|
|
|
|
/// Not used for non-StaticRef temporaries, the return place, or anonymous
|
|
|
|
/// function parameters.
|
2020-06-11 15:49:57 +01:00
|
|
|
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
|
2019-11-18 23:04:06 +00:00
|
|
|
pub enum LocalInfo<'tcx> {
|
|
|
|
/// A user-defined local variable or function parameter
|
2019-11-21 21:20:47 +00:00
|
|
|
///
|
|
|
|
/// The `BindingForm` is solely used for local diagnostics when generating
|
|
|
|
/// warnings/errors when compiling the current crate, and therefore it need
|
|
|
|
/// not be visible across crates.
|
2019-11-18 23:04:06 +00:00
|
|
|
User(ClearCrossCrate<BindingForm<'tcx>>),
|
|
|
|
/// A temporary created that references the static with the given `DefId`.
|
|
|
|
StaticRef { def_id: DefId, is_thread_local: bool },
|
2020-08-10 07:16:30 -04:00
|
|
|
/// A temporary created that references the const with the given `DefId`
|
|
|
|
ConstRef { def_id: DefId },
|
2021-09-06 16:59:24 -05:00
|
|
|
/// A temporary created during the creation of an aggregate
|
|
|
|
/// (e.g. a temporary for `foo` in `MyStruct { my_field: foo }`)
|
|
|
|
AggregateTemp,
|
2022-04-28 01:03:07 +03:00
|
|
|
/// A temporary created during the pass `Derefer` to avoid it's retagging
|
2022-05-01 15:38:22 +03:00
|
|
|
DerefTemp,
|
2019-11-18 23:04:06 +00:00
|
|
|
}
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
impl<'tcx> LocalDecl<'tcx> {
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Returns `true` only if local is a binding that can itself be
|
2018-06-15 05:47:36 +02:00
|
|
|
/// made mutable via the addition of the `mut` keyword, namely
|
|
|
|
/// something like the occurrences of `x` in:
|
|
|
|
/// - `fn foo(x: Type) { ... }`,
|
|
|
|
/// - `let x = ...`,
|
|
|
|
/// - or `match ... { C(x) => ... }`
|
2018-06-19 21:22:52 -03:00
|
|
|
pub fn can_be_made_mutable(&self) -> bool {
|
2020-09-21 04:53:44 +02:00
|
|
|
matches!(
|
|
|
|
self.local_info,
|
|
|
|
Some(box LocalInfo::User(ClearCrossCrate::Set(
|
|
|
|
BindingForm::Var(VarBindingForm {
|
|
|
|
binding_mode: ty::BindingMode::BindByValue(_),
|
|
|
|
opt_ty_info: _,
|
|
|
|
opt_match_place: _,
|
|
|
|
pat_span: _,
|
2021-01-09 12:00:45 -05:00
|
|
|
}) | BindingForm::ImplicitSelf(ImplicitSelfKind::Imm),
|
2020-09-21 04:53:44 +02:00
|
|
|
)))
|
|
|
|
)
|
2018-06-15 05:47:36 +02:00
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Returns `true` if local is definitely not a `ref ident` or
|
2018-06-15 05:47:36 +02:00
|
|
|
/// `ref mut ident` binding. (Such bindings cannot be made into
|
|
|
|
/// mutable bindings, but the inverse does not necessarily hold).
|
2018-06-19 21:22:52 -03:00
|
|
|
pub fn is_nonref_binding(&self) -> bool {
|
2020-09-21 04:53:44 +02:00
|
|
|
matches!(
|
|
|
|
self.local_info,
|
|
|
|
Some(box LocalInfo::User(ClearCrossCrate::Set(
|
|
|
|
BindingForm::Var(VarBindingForm {
|
|
|
|
binding_mode: ty::BindingMode::BindByValue(_),
|
|
|
|
opt_ty_info: _,
|
|
|
|
opt_match_place: _,
|
|
|
|
pat_span: _,
|
2021-01-09 12:00:45 -05:00
|
|
|
}) | BindingForm::ImplicitSelf(_),
|
2020-09-21 04:53:44 +02:00
|
|
|
)))
|
|
|
|
)
|
2018-06-15 05:47:36 +02:00
|
|
|
}
|
|
|
|
|
2019-11-18 23:04:06 +00:00
|
|
|
/// Returns `true` if this variable is a named variable or function
|
|
|
|
/// parameter declared by the user.
|
|
|
|
#[inline]
|
|
|
|
pub fn is_user_variable(&self) -> bool {
|
2020-09-21 04:53:44 +02:00
|
|
|
matches!(self.local_info, Some(box LocalInfo::User(_)))
|
2018-06-15 05:47:36 +02:00
|
|
|
}
|
|
|
|
|
2019-05-04 22:26:59 +01:00
|
|
|
/// Returns `true` if this is a reference to a variable bound in a `match`
|
|
|
|
/// expression that is used to access said variable for the guard of the
|
|
|
|
/// match arm.
|
|
|
|
pub fn is_ref_for_guard(&self) -> bool {
|
2020-09-21 04:53:44 +02:00
|
|
|
matches!(
|
|
|
|
self.local_info,
|
|
|
|
Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::RefForGuard)))
|
|
|
|
)
|
2019-11-18 23:04:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `Some` if this is a reference to a static item that is used to
|
2020-10-19 10:53:20 +02:00
|
|
|
/// access that static.
|
2019-11-18 23:04:06 +00:00
|
|
|
pub fn is_ref_to_static(&self) -> bool {
|
2020-09-21 04:53:44 +02:00
|
|
|
matches!(self.local_info, Some(box LocalInfo::StaticRef { .. }))
|
2019-11-18 23:04:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 10:53:20 +02:00
|
|
|
/// Returns `Some` if this is a reference to a thread-local static item that is used to
|
|
|
|
/// access that static.
|
2019-11-18 23:04:06 +00:00
|
|
|
pub fn is_ref_to_thread_local(&self) -> bool {
|
|
|
|
match self.local_info {
|
2020-05-06 12:29:00 +10:00
|
|
|
Some(box LocalInfo::StaticRef { is_thread_local, .. }) => is_thread_local,
|
2019-05-04 22:26:59 +01:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-03 22:24:52 +01:00
|
|
|
/// Returns `true` is the local is from a compiler desugaring, e.g.,
|
|
|
|
/// `__next` from a `for` loop.
|
|
|
|
#[inline]
|
|
|
|
pub fn from_compiler_desugaring(&self) -> bool {
|
2019-06-19 01:08:45 +03:00
|
|
|
self.source_info.span.desugaring_kind().is_some()
|
2019-05-03 22:24:52 +01:00
|
|
|
}
|
|
|
|
|
2020-05-06 10:17:38 +10:00
|
|
|
/// Creates a new `LocalDecl` for a temporary: mutable, non-internal.
|
2016-09-25 01:38:27 +02:00
|
|
|
#[inline]
|
2020-05-06 10:17:38 +10:00
|
|
|
pub fn new(ty: Ty<'tcx>, span: Span) -> Self {
|
|
|
|
Self::with_source_info(ty, SourceInfo::outermost(span))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Like `LocalDecl::new`, but takes a `SourceInfo` instead of a `Span`.
|
|
|
|
#[inline]
|
|
|
|
pub fn with_source_info(ty: Ty<'tcx>, source_info: SourceInfo) -> Self {
|
|
|
|
LocalDecl {
|
|
|
|
mutability: Mutability::Mut,
|
2020-05-06 12:29:00 +10:00
|
|
|
local_info: None,
|
2020-05-06 10:17:38 +10:00
|
|
|
internal: false,
|
|
|
|
is_block_tail: None,
|
|
|
|
ty,
|
2020-05-06 12:41:15 +10:00
|
|
|
user_ty: None,
|
2020-05-06 10:17:38 +10:00
|
|
|
source_info,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts `self` into same `LocalDecl` except tagged as internal.
|
|
|
|
#[inline]
|
|
|
|
pub fn internal(mut self) -> Self {
|
|
|
|
self.internal = true;
|
|
|
|
self
|
2018-09-05 23:49:58 +01:00
|
|
|
}
|
|
|
|
|
2018-09-22 00:51:48 +02:00
|
|
|
/// Converts `self` into same `LocalDecl` except tagged as immutable.
|
2018-09-05 23:49:58 +01:00
|
|
|
#[inline]
|
2018-09-22 00:51:48 +02:00
|
|
|
pub fn immutable(mut self) -> Self {
|
|
|
|
self.mutability = Mutability::Not;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts `self` into same `LocalDecl` except tagged as internal temporary.
|
|
|
|
#[inline]
|
|
|
|
pub fn block_tail(mut self, info: BlockTailInfo) -> Self {
|
|
|
|
assert!(self.is_block_tail.is_none());
|
|
|
|
self.is_block_tail = Some(info);
|
|
|
|
self
|
2017-07-15 22:41:33 +02:00
|
|
|
}
|
2016-04-16 21:51:26 +03:00
|
|
|
}
|
|
|
|
|
2020-05-30 15:02:32 -04:00
|
|
|
#[derive(Clone, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
|
|
|
|
pub enum VarDebugInfoContents<'tcx> {
|
|
|
|
/// NOTE(eddyb) There's an unenforced invariant that this `Place` is
|
|
|
|
/// based on a `Local`, not a `Static`, and contains no indexing.
|
|
|
|
Place(Place<'tcx>),
|
|
|
|
Const(Constant<'tcx>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Debug for VarDebugInfoContents<'tcx> {
|
|
|
|
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
VarDebugInfoContents::Const(c) => write!(fmt, "{}", c),
|
|
|
|
VarDebugInfoContents::Place(p) => write!(fmt, "{:?}", p),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 18:58:54 +03:00
|
|
|
/// Debug information pertaining to a user variable.
|
2020-06-11 15:49:57 +01:00
|
|
|
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
|
2018-05-16 18:58:54 +03:00
|
|
|
pub struct VarDebugInfo<'tcx> {
|
2020-04-19 13:00:18 +02:00
|
|
|
pub name: Symbol,
|
2016-04-16 21:51:26 +03:00
|
|
|
|
2018-05-16 18:58:54 +03:00
|
|
|
/// Source info of the user variable, including the scope
|
|
|
|
/// within which the variable is visible (to debuginfo)
|
|
|
|
/// (see `LocalDecl`'s `source_info` field for more details).
|
|
|
|
pub source_info: SourceInfo,
|
|
|
|
|
|
|
|
/// Where the data for this user variable is to be found.
|
2020-05-30 15:02:32 -04:00
|
|
|
pub value: VarDebugInfoContents<'tcx>,
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// BasicBlock
|
|
|
|
|
2019-09-26 05:38:33 +00:00
|
|
|
rustc_index::newtype_index! {
|
2020-09-16 15:31:56 -07:00
|
|
|
/// A node in the MIR [control-flow graph][CFG].
|
2020-09-14 20:05:00 -07:00
|
|
|
///
|
2020-09-15 09:50:55 -07:00
|
|
|
/// There are no branches (e.g., `if`s, function calls, etc.) within a basic block, which makes
|
2020-09-14 20:10:29 -07:00
|
|
|
/// it easier to do [data-flow analyses] and optimizations. Instead, branches are represented
|
|
|
|
/// as an edge in a graph between basic blocks.
|
|
|
|
///
|
2020-09-15 09:50:55 -07:00
|
|
|
/// Basic blocks consist of a series of [statements][Statement], ending with a
|
2020-09-16 15:31:56 -07:00
|
|
|
/// [terminator][Terminator]. Basic blocks can have multiple predecessors and successors,
|
|
|
|
/// however there is a MIR pass ([`CriticalCallEdges`]) that removes *critical edges*, which
|
|
|
|
/// are edges that go from a multi-successor node to a multi-predecessor node. This pass is
|
|
|
|
/// needed because some analyses require that there are no critical edges in the CFG.
|
2020-09-14 20:05:00 -07:00
|
|
|
///
|
2020-10-16 20:35:56 -07:00
|
|
|
/// Note that this type is just an index into [`Body.basic_blocks`](Body::basic_blocks);
|
|
|
|
/// the actual data that a basic block holds is in [`BasicBlockData`].
|
|
|
|
///
|
2020-09-14 20:05:00 -07:00
|
|
|
/// Read more about basic blocks in the [rustc-dev-guide][guide-mir].
|
|
|
|
///
|
|
|
|
/// [CFG]: https://rustc-dev-guide.rust-lang.org/appendix/background.html#cfg
|
|
|
|
/// [data-flow analyses]:
|
|
|
|
/// https://rustc-dev-guide.rust-lang.org/appendix/background.html#what-is-a-dataflow-analysis
|
2021-01-05 20:08:11 +01:00
|
|
|
/// [`CriticalCallEdges`]: ../../rustc_const_eval/transform/add_call_guards/enum.AddCallGuards.html#variant.CriticalCallEdges
|
2020-09-14 20:05:00 -07:00
|
|
|
/// [guide-mir]: https://rustc-dev-guide.rust-lang.org/mir/
|
2018-07-25 13:41:32 +03:00
|
|
|
pub struct BasicBlock {
|
2018-12-03 01:14:35 +01:00
|
|
|
derive [HashStable]
|
2018-08-28 12:20:56 -04:00
|
|
|
DEBUG_FORMAT = "bb{}",
|
|
|
|
const START_BLOCK = 0,
|
2018-07-25 13:41:32 +03:00
|
|
|
}
|
|
|
|
}
|
2017-12-06 09:25:29 +01:00
|
|
|
|
|
|
|
impl BasicBlock {
|
|
|
|
pub fn start_location(self) -> Location {
|
2019-07-12 22:49:15 +02:00
|
|
|
Location { block: self, statement_index: 0 }
|
2017-12-06 09:25:29 +01:00
|
|
|
}
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2016-02-26 18:05:50 +02:00
|
|
|
// BasicBlockData and Terminator
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2020-09-14 20:05:00 -07:00
|
|
|
/// See [`BasicBlock`] for documentation on what basic blocks are at a high level.
|
2020-06-11 15:49:57 +01:00
|
|
|
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
|
2015-10-05 12:31:48 -04:00
|
|
|
pub struct BasicBlockData<'tcx> {
|
2016-03-24 06:12:19 -04:00
|
|
|
/// List of statements in this block.
|
2015-10-05 12:31:48 -04:00
|
|
|
pub statements: Vec<Statement<'tcx>>,
|
2016-03-24 06:12:19 -04:00
|
|
|
|
|
|
|
/// Terminator for this block.
|
|
|
|
///
|
2019-02-08 14:53:55 +01:00
|
|
|
/// N.B., this should generally ONLY be `None` during construction.
|
2016-03-24 06:12:19 -04:00
|
|
|
/// Therefore, you should generally access it via the
|
|
|
|
/// `terminator()` or `terminator_mut()` methods. The only
|
|
|
|
/// exception is that certain passes, such as `simplify_cfg`, swap
|
|
|
|
/// out the terminator temporarily with `None` while they continue
|
|
|
|
/// to recurse over the set of basic blocks.
|
2015-12-19 00:44:32 +02:00
|
|
|
pub terminator: Option<Terminator<'tcx>>,
|
2016-03-24 06:12:19 -04:00
|
|
|
|
|
|
|
/// If true, this block lies on an unwind path. This is used
|
2018-05-08 16:10:16 +03:00
|
|
|
/// during codegen where distinct kinds of basic blocks may be
|
2016-03-24 06:12:19 -04:00
|
|
|
/// generated (particularly for MSVC cleanup). Unwind blocks must
|
|
|
|
/// only branch to other unwind blocks.
|
2015-12-20 15:30:09 +02:00
|
|
|
pub is_cleanup: bool,
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2020-02-10 20:53:01 +01:00
|
|
|
/// Information about an assertion failure.
|
2020-10-04 15:52:14 +02:00
|
|
|
#[derive(Clone, TyEncodable, TyDecodable, Hash, HashStable, PartialEq, PartialOrd)]
|
2020-02-12 19:40:31 +01:00
|
|
|
pub enum AssertKind<O> {
|
2020-02-10 20:53:01 +01:00
|
|
|
BoundsCheck { len: O, index: O },
|
2020-06-19 18:57:15 +02:00
|
|
|
Overflow(BinOp, O, O),
|
|
|
|
OverflowNeg(O),
|
|
|
|
DivisionByZero(O),
|
|
|
|
RemainderByZero(O),
|
2020-02-10 20:53:01 +01:00
|
|
|
ResumedAfterReturn(GeneratorKind),
|
|
|
|
ResumedAfterPanic(GeneratorKind),
|
|
|
|
}
|
|
|
|
|
2022-01-18 10:05:41 -06:00
|
|
|
#[derive(Clone, Debug, PartialEq, TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable)]
|
2020-02-14 18:17:50 +00:00
|
|
|
pub enum InlineAsmOperand<'tcx> {
|
|
|
|
In {
|
|
|
|
reg: InlineAsmRegOrRegClass,
|
|
|
|
value: Operand<'tcx>,
|
|
|
|
},
|
|
|
|
Out {
|
|
|
|
reg: InlineAsmRegOrRegClass,
|
|
|
|
late: bool,
|
|
|
|
place: Option<Place<'tcx>>,
|
|
|
|
},
|
|
|
|
InOut {
|
|
|
|
reg: InlineAsmRegOrRegClass,
|
|
|
|
late: bool,
|
|
|
|
in_value: Operand<'tcx>,
|
|
|
|
out_place: Option<Place<'tcx>>,
|
|
|
|
},
|
|
|
|
Const {
|
2021-04-06 05:50:55 +01:00
|
|
|
value: Box<Constant<'tcx>>,
|
2020-02-14 18:17:50 +00:00
|
|
|
},
|
|
|
|
SymFn {
|
|
|
|
value: Box<Constant<'tcx>>,
|
|
|
|
},
|
|
|
|
SymStatic {
|
2020-06-05 15:52:38 +01:00
|
|
|
def_id: DefId,
|
2020-02-14 18:17:50 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-02-10 20:53:01 +01:00
|
|
|
/// Type for MIR `Assert` terminator error messages.
|
2020-02-12 19:40:31 +01:00
|
|
|
pub type AssertMessage<'tcx> = AssertKind<Operand<'tcx>>;
|
2020-02-10 20:53:01 +01:00
|
|
|
|
2022-05-17 08:41:01 +08:00
|
|
|
pub type Successors<'a> = impl Iterator<Item = BasicBlock> + 'a;
|
2018-04-27 14:02:09 +03:00
|
|
|
pub type SuccessorsMut<'a> =
|
|
|
|
iter::Chain<option::IntoIter<&'a mut BasicBlock>, slice::IterMut<'a, BasicBlock>>;
|
|
|
|
|
2015-10-05 12:31:48 -04:00
|
|
|
impl<'tcx> BasicBlockData<'tcx> {
|
2015-12-19 00:44:32 +02:00
|
|
|
pub fn new(terminator: Option<Terminator<'tcx>>) -> BasicBlockData<'tcx> {
|
2019-07-12 22:49:15 +02:00
|
|
|
BasicBlockData { statements: vec![], terminator, is_cleanup: false }
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
2015-12-19 00:44:32 +02:00
|
|
|
|
|
|
|
/// Accessor for terminator.
|
|
|
|
///
|
|
|
|
/// Terminator may not be None after construction of the basic block is complete. This accessor
|
|
|
|
/// provides a convenience way to reach the terminator.
|
2021-06-01 00:00:00 +00:00
|
|
|
#[inline]
|
2015-12-19 00:44:32 +02:00
|
|
|
pub fn terminator(&self) -> &Terminator<'tcx> {
|
|
|
|
self.terminator.as_ref().expect("invalid terminator state")
|
|
|
|
}
|
|
|
|
|
2021-06-01 00:00:00 +00:00
|
|
|
#[inline]
|
2019-10-04 00:55:28 -04:00
|
|
|
pub fn terminator_mut(&mut self) -> &mut Terminator<'tcx> {
|
2015-12-19 00:44:32 +02:00
|
|
|
self.terminator.as_mut().expect("invalid terminator state")
|
|
|
|
}
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2018-06-19 21:22:52 -03:00
|
|
|
pub fn retain_statements<F>(&mut self, mut f: F)
|
|
|
|
where
|
2018-08-29 22:02:42 -07:00
|
|
|
F: FnMut(&mut Statement<'_>) -> bool,
|
2018-06-19 21:22:52 -03:00
|
|
|
{
|
2016-12-26 14:34:03 +01:00
|
|
|
for s in &mut self.statements {
|
|
|
|
if !f(s) {
|
2018-02-16 19:20:18 +02:00
|
|
|
s.make_nop();
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-12 17:29:37 -03:00
|
|
|
|
2018-02-16 19:20:18 +02:00
|
|
|
pub fn expand_statements<F, I>(&mut self, mut f: F)
|
2018-06-19 21:22:52 -03:00
|
|
|
where
|
|
|
|
F: FnMut(&mut Statement<'tcx>) -> Option<I>,
|
|
|
|
I: iter::TrustedLen<Item = Statement<'tcx>>,
|
2018-02-16 19:20:18 +02:00
|
|
|
{
|
|
|
|
// Gather all the iterators we'll need to splice in, and their positions.
|
|
|
|
let mut splices: Vec<(usize, I)> = vec![];
|
|
|
|
let mut extra_stmts = 0;
|
|
|
|
for (i, s) in self.statements.iter_mut().enumerate() {
|
|
|
|
if let Some(mut new_stmts) = f(s) {
|
|
|
|
if let Some(first) = new_stmts.next() {
|
|
|
|
// We can already store the first new statement.
|
|
|
|
*s = first;
|
|
|
|
|
|
|
|
// Save the other statements for optimized splicing.
|
|
|
|
let remaining = new_stmts.size_hint().0;
|
|
|
|
if remaining > 0 {
|
|
|
|
splices.push((i + 1 + extra_stmts, new_stmts));
|
|
|
|
extra_stmts += remaining;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s.make_nop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Splice in the new statements, from the end of the block.
|
|
|
|
// FIXME(eddyb) This could be more efficient with a "gap buffer"
|
|
|
|
// where a range of elements ("gap") is left uninitialized, with
|
|
|
|
// splicing adding new elements to the end of that gap and moving
|
|
|
|
// existing elements from before the gap to the end of the gap.
|
|
|
|
// For now, this is safe code, emulating a gap but initializing it.
|
2018-06-19 21:22:52 -03:00
|
|
|
let mut gap = self.statements.len()..self.statements.len() + extra_stmts;
|
|
|
|
self.statements.resize(
|
|
|
|
gap.end,
|
2020-05-06 10:30:11 +10:00
|
|
|
Statement { source_info: SourceInfo::outermost(DUMMY_SP), kind: StatementKind::Nop },
|
2018-06-19 21:22:52 -03:00
|
|
|
);
|
2018-02-16 19:20:18 +02:00
|
|
|
for (splice_start, new_stmts) in splices.into_iter().rev() {
|
|
|
|
let splice_end = splice_start + new_stmts.size_hint().0;
|
|
|
|
while gap.end > splice_end {
|
|
|
|
gap.start -= 1;
|
|
|
|
gap.end -= 1;
|
|
|
|
self.statements.swap(gap.start, gap.end);
|
|
|
|
}
|
|
|
|
self.statements.splice(splice_start..splice_end, new_stmts);
|
|
|
|
gap.end = splice_start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-12 17:29:37 -03:00
|
|
|
pub fn visitable(&self, index: usize) -> &dyn MirVisitable<'tcx> {
|
|
|
|
if index < self.statements.len() { &self.statements[index] } else { &self.terminator }
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2020-02-12 19:40:31 +01:00
|
|
|
impl<O> AssertKind<O> {
|
2020-02-10 20:53:01 +01:00
|
|
|
/// Getting a description does not require `O` to be printable, and does not
|
|
|
|
/// require allocation.
|
|
|
|
/// The caller is expected to handle `BoundsCheck` separately.
|
|
|
|
pub fn description(&self) -> &'static str {
|
2020-02-12 19:40:31 +01:00
|
|
|
use AssertKind::*;
|
2020-02-10 20:53:01 +01:00
|
|
|
match self {
|
2020-06-19 18:57:15 +02:00
|
|
|
Overflow(BinOp::Add, _, _) => "attempt to add with overflow",
|
|
|
|
Overflow(BinOp::Sub, _, _) => "attempt to subtract with overflow",
|
|
|
|
Overflow(BinOp::Mul, _, _) => "attempt to multiply with overflow",
|
|
|
|
Overflow(BinOp::Div, _, _) => "attempt to divide with overflow",
|
|
|
|
Overflow(BinOp::Rem, _, _) => "attempt to calculate the remainder with overflow",
|
|
|
|
OverflowNeg(_) => "attempt to negate with overflow",
|
|
|
|
Overflow(BinOp::Shr, _, _) => "attempt to shift right with overflow",
|
|
|
|
Overflow(BinOp::Shl, _, _) => "attempt to shift left with overflow",
|
|
|
|
Overflow(op, _, _) => bug!("{:?} cannot overflow", op),
|
|
|
|
DivisionByZero(_) => "attempt to divide by zero",
|
|
|
|
RemainderByZero(_) => "attempt to calculate the remainder with a divisor of zero",
|
2020-02-10 20:53:01 +01:00
|
|
|
ResumedAfterReturn(GeneratorKind::Gen) => "generator resumed after completion",
|
|
|
|
ResumedAfterReturn(GeneratorKind::Async(_)) => "`async fn` resumed after completion",
|
|
|
|
ResumedAfterPanic(GeneratorKind::Gen) => "generator resumed after panicking",
|
|
|
|
ResumedAfterPanic(GeneratorKind::Async(_)) => "`async fn` resumed after panicking",
|
2020-02-12 19:40:31 +01:00
|
|
|
BoundsCheck { .. } => bug!("Unexpected AssertKind"),
|
2020-02-10 20:53:01 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-11 12:59:19 +02:00
|
|
|
|
|
|
|
/// Format the message arguments for the `assert(cond, msg..)` terminator in MIR printing.
|
2021-04-21 14:09:15 +02:00
|
|
|
pub fn fmt_assert_args<W: Write>(&self, f: &mut W) -> fmt::Result
|
2020-04-11 12:59:19 +02:00
|
|
|
where
|
|
|
|
O: Debug,
|
|
|
|
{
|
2020-06-19 18:57:15 +02:00
|
|
|
use AssertKind::*;
|
2020-04-11 12:59:19 +02:00
|
|
|
match self {
|
2020-06-19 18:57:15 +02:00
|
|
|
BoundsCheck { ref len, ref index } => write!(
|
2020-04-11 12:59:19 +02:00
|
|
|
f,
|
2020-09-15 20:34:50 +01:00
|
|
|
"\"index out of bounds: the length is {{}} but the index is {{}}\", {:?}, {:?}",
|
2020-04-11 12:59:19 +02:00
|
|
|
len, index
|
|
|
|
),
|
2020-06-19 18:57:15 +02:00
|
|
|
|
|
|
|
OverflowNeg(op) => {
|
2020-09-15 20:34:50 +01:00
|
|
|
write!(f, "\"attempt to negate `{{}}`, which would overflow\", {:?}", op)
|
2020-06-19 18:57:15 +02:00
|
|
|
}
|
2020-09-15 20:34:50 +01:00
|
|
|
DivisionByZero(op) => write!(f, "\"attempt to divide `{{}}` by zero\", {:?}", op),
|
2020-06-19 18:57:15 +02:00
|
|
|
RemainderByZero(op) => write!(
|
|
|
|
f,
|
2020-09-15 20:34:50 +01:00
|
|
|
"\"attempt to calculate the remainder of `{{}}` with a divisor of zero\", {:?}",
|
2020-06-19 18:57:15 +02:00
|
|
|
op
|
|
|
|
),
|
|
|
|
Overflow(BinOp::Add, l, r) => write!(
|
|
|
|
f,
|
2020-09-15 20:34:50 +01:00
|
|
|
"\"attempt to compute `{{}} + {{}}`, which would overflow\", {:?}, {:?}",
|
2020-06-19 18:57:15 +02:00
|
|
|
l, r
|
|
|
|
),
|
|
|
|
Overflow(BinOp::Sub, l, r) => write!(
|
|
|
|
f,
|
2020-09-15 20:34:50 +01:00
|
|
|
"\"attempt to compute `{{}} - {{}}`, which would overflow\", {:?}, {:?}",
|
2020-06-19 18:57:15 +02:00
|
|
|
l, r
|
|
|
|
),
|
|
|
|
Overflow(BinOp::Mul, l, r) => write!(
|
|
|
|
f,
|
2020-09-15 20:34:50 +01:00
|
|
|
"\"attempt to compute `{{}} * {{}}`, which would overflow\", {:?}, {:?}",
|
2020-06-19 18:57:15 +02:00
|
|
|
l, r
|
|
|
|
),
|
|
|
|
Overflow(BinOp::Div, l, r) => write!(
|
|
|
|
f,
|
2020-09-15 20:34:50 +01:00
|
|
|
"\"attempt to compute `{{}} / {{}}`, which would overflow\", {:?}, {:?}",
|
2020-06-19 18:57:15 +02:00
|
|
|
l, r
|
|
|
|
),
|
|
|
|
Overflow(BinOp::Rem, l, r) => write!(
|
|
|
|
f,
|
2020-09-15 20:34:50 +01:00
|
|
|
"\"attempt to compute the remainder of `{{}} % {{}}`, which would overflow\", {:?}, {:?}",
|
2020-06-19 18:57:15 +02:00
|
|
|
l, r
|
|
|
|
),
|
|
|
|
Overflow(BinOp::Shr, _, r) => {
|
2020-09-15 20:34:50 +01:00
|
|
|
write!(f, "\"attempt to shift right by `{{}}`, which would overflow\", {:?}", r)
|
2020-06-19 18:57:15 +02:00
|
|
|
}
|
|
|
|
Overflow(BinOp::Shl, _, r) => {
|
2020-09-15 20:34:50 +01:00
|
|
|
write!(f, "\"attempt to shift left by `{{}}`, which would overflow\", {:?}", r)
|
2020-06-19 18:57:15 +02:00
|
|
|
}
|
2020-04-11 12:59:19 +02:00
|
|
|
_ => write!(f, "\"{}\"", self.description()),
|
|
|
|
}
|
|
|
|
}
|
2020-02-10 20:53:01 +01:00
|
|
|
}
|
|
|
|
|
2020-02-12 19:40:31 +01:00
|
|
|
impl<O: fmt::Debug> fmt::Debug for AssertKind<O> {
|
2020-02-10 20:53:01 +01:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2020-02-12 19:40:31 +01:00
|
|
|
use AssertKind::*;
|
2020-02-10 20:53:01 +01:00
|
|
|
match self {
|
2020-09-15 20:34:50 +01:00
|
|
|
BoundsCheck { ref len, ref index } => write!(
|
|
|
|
f,
|
|
|
|
"index out of bounds: the length is {:?} but the index is {:?}",
|
|
|
|
len, index
|
|
|
|
),
|
|
|
|
OverflowNeg(op) => write!(f, "attempt to negate `{:#?}`, which would overflow", op),
|
|
|
|
DivisionByZero(op) => write!(f, "attempt to divide `{:#?}` by zero", op),
|
|
|
|
RemainderByZero(op) => write!(
|
|
|
|
f,
|
|
|
|
"attempt to calculate the remainder of `{:#?}` with a divisor of zero",
|
|
|
|
op
|
|
|
|
),
|
2020-06-19 18:57:15 +02:00
|
|
|
Overflow(BinOp::Add, l, r) => {
|
2020-09-15 20:34:50 +01:00
|
|
|
write!(f, "attempt to compute `{:#?} + {:#?}`, which would overflow", l, r)
|
2020-06-19 18:57:15 +02:00
|
|
|
}
|
|
|
|
Overflow(BinOp::Sub, l, r) => {
|
2020-09-15 20:34:50 +01:00
|
|
|
write!(f, "attempt to compute `{:#?} - {:#?}`, which would overflow", l, r)
|
2020-06-19 18:57:15 +02:00
|
|
|
}
|
|
|
|
Overflow(BinOp::Mul, l, r) => {
|
2020-09-15 20:34:50 +01:00
|
|
|
write!(f, "attempt to compute `{:#?} * {:#?}`, which would overflow", l, r)
|
2020-06-19 18:57:15 +02:00
|
|
|
}
|
|
|
|
Overflow(BinOp::Div, l, r) => {
|
2020-09-15 20:34:50 +01:00
|
|
|
write!(f, "attempt to compute `{:#?} / {:#?}`, which would overflow", l, r)
|
2020-06-19 18:57:15 +02:00
|
|
|
}
|
|
|
|
Overflow(BinOp::Rem, l, r) => write!(
|
|
|
|
f,
|
2020-09-15 20:34:50 +01:00
|
|
|
"attempt to compute the remainder of `{:#?} % {:#?}`, which would overflow",
|
2020-06-19 18:57:15 +02:00
|
|
|
l, r
|
|
|
|
),
|
|
|
|
Overflow(BinOp::Shr, _, r) => {
|
2020-09-15 20:34:50 +01:00
|
|
|
write!(f, "attempt to shift right by `{:#?}`, which would overflow", r)
|
2020-06-19 18:57:15 +02:00
|
|
|
}
|
|
|
|
Overflow(BinOp::Shl, _, r) => {
|
2020-09-15 20:34:50 +01:00
|
|
|
write!(f, "attempt to shift left by `{:#?}`, which would overflow", r)
|
2020-06-19 18:57:15 +02:00
|
|
|
}
|
2020-02-10 20:53:01 +01:00
|
|
|
_ => write!(f, "{}", self.description()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Statements
|
|
|
|
|
2020-06-11 15:49:57 +01:00
|
|
|
#[derive(Clone, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
|
2015-10-05 12:31:48 -04:00
|
|
|
pub struct Statement<'tcx> {
|
2016-06-07 19:21:56 +03:00
|
|
|
pub source_info: SourceInfo,
|
2015-10-05 12:31:48 -04:00
|
|
|
pub kind: StatementKind<'tcx>,
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2018-11-09 10:19:51 +11:00
|
|
|
// `Statement` is used a lot. Make sure it doesn't unintentionally get bigger.
|
2021-03-06 16:02:48 +00:00
|
|
|
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
|
2021-03-29 22:48:44 -04:00
|
|
|
static_assert_size!(Statement<'_>, 32);
|
2018-11-09 10:19:51 +11:00
|
|
|
|
2019-08-24 14:16:56 -07:00
|
|
|
impl Statement<'_> {
|
2016-09-15 18:17:58 -07:00
|
|
|
/// Changes a statement to a nop. This is both faster than deleting instructions and avoids
|
|
|
|
/// invalidating statement indices in `Location`s.
|
|
|
|
pub fn make_nop(&mut self) {
|
|
|
|
self.kind = StatementKind::Nop
|
|
|
|
}
|
2018-02-16 19:20:18 +02:00
|
|
|
|
|
|
|
/// Changes a statement to a nop and returns the original statement.
|
2021-11-26 18:18:14 -08:00
|
|
|
#[must_use = "If you don't need the statement, use `make_nop` instead"]
|
2018-02-16 19:20:18 +02:00
|
|
|
pub fn replace_nop(&mut self) -> Self {
|
|
|
|
Statement {
|
|
|
|
source_info: self.source_info,
|
2018-06-19 21:22:52 -03:00
|
|
|
kind: mem::replace(&mut self.kind, StatementKind::Nop),
|
2018-02-16 19:20:18 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-15 18:17:58 -07:00
|
|
|
}
|
|
|
|
|
2022-03-05 20:37:04 -05:00
|
|
|
/// The various kinds of statements that can appear in MIR.
|
|
|
|
///
|
|
|
|
/// Not all of these are allowed at every [`MirPhase`]. Check the documentation there to see which
|
|
|
|
/// ones you do not have to worry about. The MIR validator will generally enforce such restrictions,
|
|
|
|
/// causing an ICE if they are violated.
|
2020-10-04 15:52:14 +02:00
|
|
|
#[derive(Clone, Debug, PartialEq, TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable)]
|
2015-10-05 12:31:48 -04:00
|
|
|
pub enum StatementKind<'tcx> {
|
2022-03-25 02:17:05 -04:00
|
|
|
/// Assign statements roughly correspond to an assignment in Rust proper (`x = ...`) except
|
|
|
|
/// without the possibility of dropping the previous value (that must be done separately, if at
|
|
|
|
/// all). The *exact* way this works is undecided. It probably does something like evaluating
|
2022-04-08 15:53:08 -04:00
|
|
|
/// the LHS to a place and the RHS to a value, and then storing the value to the place. Various
|
|
|
|
/// parts of this may do type specific things that are more complicated than simply copying
|
|
|
|
/// bytes.
|
2022-03-06 15:53:23 -05:00
|
|
|
///
|
2022-03-25 02:17:05 -04:00
|
|
|
/// **Needs clarification**: The implication of the above idea would be that assignment implies
|
|
|
|
/// that the resulting value is initialized. I believe we could commit to this separately from
|
|
|
|
/// committing to whatever part of the memory model we would need to decide on to make the above
|
|
|
|
/// paragragh precise. Do we want to?
|
|
|
|
///
|
|
|
|
/// Assignments in which the types of the place and rvalue differ are not well-formed.
|
|
|
|
///
|
|
|
|
/// **Needs clarification**: Do we ever want to worry about non-free (in the body) lifetimes for
|
|
|
|
/// the typing requirement in post drop-elaboration MIR? I think probably not - I'm not sure we
|
|
|
|
/// could meaningfully require this anyway. How about free lifetimes? Is ignoring this
|
|
|
|
/// interesting for optimizations? Do we want to allow such optimizations?
|
|
|
|
///
|
|
|
|
/// **Needs clarification**: We currently require that the LHS place not overlap with any place
|
2022-04-08 15:53:08 -04:00
|
|
|
/// read as part of computation of the RHS for some rvalues (generally those not producing
|
|
|
|
/// primitives). This requirement is under discussion in [#68364]. As a part of this discussion,
|
|
|
|
/// it is also unclear in what order the components are evaluated.
|
2022-03-25 02:17:05 -04:00
|
|
|
///
|
|
|
|
/// [#68364]: https://github.com/rust-lang/rust/issues/68364
|
|
|
|
///
|
|
|
|
/// See [`Rvalue`] documentation for details on each of those.
|
2019-09-11 16:05:45 -03:00
|
|
|
Assign(Box<(Place<'tcx>, Rvalue<'tcx>)>),
|
2016-08-14 06:34:14 +03:00
|
|
|
|
2022-03-25 02:17:05 -04:00
|
|
|
/// This represents all the reading that a pattern match may do (e.g., inspecting constants and
|
|
|
|
/// discriminant values), and the kind of pattern it comes from. This is in order to adapt
|
|
|
|
/// potential error messages to these specific patterns.
|
2018-11-22 14:33:08 +01:00
|
|
|
///
|
2018-12-12 13:09:36 +01:00
|
|
|
/// Note that this also is emitted for regular `let` bindings to ensure that locals that are
|
2019-02-08 14:53:55 +01:00
|
|
|
/// never accessed still get some sanity checks for, e.g., `let x: ! = ..;`
|
2022-03-25 02:17:05 -04:00
|
|
|
///
|
|
|
|
/// When executed at runtime this is a nop.
|
|
|
|
///
|
|
|
|
/// Disallowed after drop elaboration.
|
2021-03-29 22:48:44 -04:00
|
|
|
FakeRead(Box<(FakeReadCause, Place<'tcx>)>),
|
2018-05-04 12:04:33 +02:00
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
/// Write the discriminant for a variant to the enum Place.
|
2022-04-05 17:22:31 -04:00
|
|
|
///
|
|
|
|
/// This is permitted for both generators and ADTs. This does not necessarily write to the
|
|
|
|
/// entire place; instead, it writes to the minimum set of bytes as required by the layout for
|
|
|
|
/// the type.
|
2019-09-11 16:05:45 -03:00
|
|
|
SetDiscriminant { place: Box<Place<'tcx>>, variant_index: VariantIdx },
|
2016-08-14 06:34:14 +03:00
|
|
|
|
2022-04-05 17:22:31 -04:00
|
|
|
/// Deinitializes the place.
|
|
|
|
///
|
|
|
|
/// This writes `uninit` bytes to the entire place.
|
2022-04-05 17:14:59 -04:00
|
|
|
Deinit(Box<Place<'tcx>>),
|
|
|
|
|
2022-03-25 02:17:05 -04:00
|
|
|
/// `StorageLive` and `StorageDead` statements mark the live range of a local.
|
|
|
|
///
|
|
|
|
/// Using a local before a `StorageLive` or after a `StorageDead` is not well-formed. These
|
|
|
|
/// statements are not required. If the entire MIR body contains no `StorageLive`/`StorageDead`
|
|
|
|
/// statements for a particular local, the local is always considered live.
|
|
|
|
///
|
2022-03-26 20:46:56 -04:00
|
|
|
/// More precisely, the MIR validator currently does a `MaybeStorageLiveLocals` analysis to
|
|
|
|
/// check validity of each use of a local. I believe this is equivalent to requiring for every
|
|
|
|
/// use of a local, there exist at least one path from the root to that use that contains a
|
2022-03-25 02:17:05 -04:00
|
|
|
/// `StorageLive` more recently than a `StorageDead`.
|
|
|
|
///
|
2022-03-26 20:46:56 -04:00
|
|
|
/// **Needs clarification**: Is it permitted to have two `StorageLive`s without an intervening
|
|
|
|
/// `StorageDead`? Two `StorageDead`s without an intervening `StorageLive`? LLVM says poison,
|
|
|
|
/// yes. If the answer to any of these is "no," is breaking that rule UB or is it an error to
|
|
|
|
/// have a path in the CFG that might do this?
|
2017-09-04 08:01:46 +03:00
|
|
|
StorageLive(Local),
|
2016-08-14 06:34:14 +03:00
|
|
|
|
2022-03-25 02:17:05 -04:00
|
|
|
/// See `StorageLive` above.
|
2017-09-04 08:01:46 +03:00
|
|
|
StorageDead(Local),
|
2016-09-15 18:17:58 -07:00
|
|
|
|
2022-03-25 02:17:05 -04:00
|
|
|
/// Retag references in the given place, ensuring they got fresh tags.
|
|
|
|
///
|
|
|
|
/// This is part of the Stacked Borrows model. These statements are currently only interpreted
|
|
|
|
/// by miri and only generated when `-Z mir-emit-retag` is passed. See
|
|
|
|
/// <https://internals.rust-lang.org/t/stacked-borrows-an-aliasing-model-for-rust/8153/> for
|
|
|
|
/// more details.
|
|
|
|
///
|
2022-03-26 20:46:56 -04:00
|
|
|
/// For code that is not specific to stacked borrows, you should consider retags to read
|
2022-03-25 02:17:05 -04:00
|
|
|
/// and modify the place in an opaque way.
|
2019-09-11 16:05:45 -03:00
|
|
|
Retag(RetagKind, Box<Place<'tcx>>),
|
2018-11-06 11:04:10 +01:00
|
|
|
|
2018-08-31 18:59:35 -04:00
|
|
|
/// Encodes a user's type ascription. These need to be preserved
|
|
|
|
/// intact so that NLL can respect them. For example:
|
2022-04-15 15:04:34 -07:00
|
|
|
/// ```ignore (illustrative)
|
|
|
|
/// let a: T = y;
|
|
|
|
/// ```
|
2018-09-05 15:52:01 -04:00
|
|
|
/// The effect of this annotation is to relate the type `T_y` of the place `y`
|
|
|
|
/// to the user-given type `T`. The effect depends on the specified variance:
|
|
|
|
///
|
|
|
|
/// - `Covariant` -- requires that `T_y <: T`
|
|
|
|
/// - `Contravariant` -- requires that `T_y :> T`
|
|
|
|
/// - `Invariant` -- requires that `T_y == T`
|
|
|
|
/// - `Bivariant` -- no effect
|
2022-03-25 02:17:05 -04:00
|
|
|
///
|
|
|
|
/// When executed at runtime this is a nop.
|
|
|
|
///
|
|
|
|
/// Disallowed after drop elaboration.
|
2019-09-11 16:05:45 -03:00
|
|
|
AscribeUserType(Box<(Place<'tcx>, UserTypeProjection)>, ty::Variance),
|
2018-02-23 20:52:05 +00:00
|
|
|
|
2021-10-21 16:04:22 +02:00
|
|
|
/// Marks the start of a "coverage region", injected with '-Cinstrument-coverage'. A
|
coverage bug fixes and optimization support
Adjusted LLVM codegen for code compiled with `-Zinstrument-coverage` to
address multiple, somewhat related issues.
Fixed a significant flaw in prior coverage solution: Every counter
generated a new counter variable, but there should have only been one
counter variable per function. This appears to have bloated .profraw
files significantly. (For a small program, it increased the size by
about 40%. I have not tested large programs, but there is anecdotal
evidence that profraw files were way too large. This is a good fix,
regardless, but hopefully it also addresses related issues.
Fixes: #82144
Invalid LLVM coverage data produced when compiled with -C opt-level=1
Existing tests now work up to at least `opt-level=3`. This required a
detailed analysis of the LLVM IR, comparisons with Clang C++ LLVM IR
when compiled with coverage, and a lot of trial and error with codegen
adjustments.
The biggest hurdle was figuring out how to continue to support coverage
results for unused functions and generics. Rust's coverage results have
three advantages over Clang's coverage results:
1. Rust's coverage map does not include any overlapping code regions,
making coverage counting unambiguous.
2. Rust generates coverage results (showing zero counts) for all unused
functions, including generics. (Clang does not generate coverage for
uninstantiated template functions.)
3. Rust's unused functions produce minimal stubbed functions in LLVM IR,
sufficient for including in the coverage results; while Clang must
generate the complete LLVM IR for each unused function, even though
it will never be called.
This PR removes the previous hack of attempting to inject coverage into
some other existing function instance, and generates dedicated instances
for each unused function. This change, and a few other adjustments
(similar to what is required for `-C link-dead-code`, but with lower
impact), makes it possible to support LLVM optimizations.
Fixes: #79651
Coverage report: "Unexecuted instantiation:..." for a generic function
from multiple crates
Fixed by removing the aforementioned hack. Some "Unexecuted
instantiation" notices are unavoidable, as explained in the
`used_crate.rs` test, but `-Zinstrument-coverage` has new options to
back off support for either unused generics, or all unused functions,
which avoids the notice, at the cost of less coverage of unused
functions.
Fixes: #82875
Invalid LLVM coverage data produced with crate brotli_decompressor
Fixed by disabling the LLVM function attribute that forces inlining, if
`-Z instrument-coverage` is enabled. This attribute is applied to
Rust functions with `#[inline(always)], and in some cases, the forced
inlining breaks coverage instrumentation and reports.
2021-03-15 16:32:45 -07:00
|
|
|
/// `Coverage` statement carries metadata about the coverage region, used to inject a coverage
|
|
|
|
/// map into the binary. If `Coverage::kind` is a `Counter`, the statement also generates
|
2021-04-19 15:57:08 +03:00
|
|
|
/// executable code, to increment a counter variable at runtime, each time the code region is
|
coverage bug fixes and optimization support
Adjusted LLVM codegen for code compiled with `-Zinstrument-coverage` to
address multiple, somewhat related issues.
Fixed a significant flaw in prior coverage solution: Every counter
generated a new counter variable, but there should have only been one
counter variable per function. This appears to have bloated .profraw
files significantly. (For a small program, it increased the size by
about 40%. I have not tested large programs, but there is anecdotal
evidence that profraw files were way too large. This is a good fix,
regardless, but hopefully it also addresses related issues.
Fixes: #82144
Invalid LLVM coverage data produced when compiled with -C opt-level=1
Existing tests now work up to at least `opt-level=3`. This required a
detailed analysis of the LLVM IR, comparisons with Clang C++ LLVM IR
when compiled with coverage, and a lot of trial and error with codegen
adjustments.
The biggest hurdle was figuring out how to continue to support coverage
results for unused functions and generics. Rust's coverage results have
three advantages over Clang's coverage results:
1. Rust's coverage map does not include any overlapping code regions,
making coverage counting unambiguous.
2. Rust generates coverage results (showing zero counts) for all unused
functions, including generics. (Clang does not generate coverage for
uninstantiated template functions.)
3. Rust's unused functions produce minimal stubbed functions in LLVM IR,
sufficient for including in the coverage results; while Clang must
generate the complete LLVM IR for each unused function, even though
it will never be called.
This PR removes the previous hack of attempting to inject coverage into
some other existing function instance, and generates dedicated instances
for each unused function. This change, and a few other adjustments
(similar to what is required for `-C link-dead-code`, but with lower
impact), makes it possible to support LLVM optimizations.
Fixes: #79651
Coverage report: "Unexecuted instantiation:..." for a generic function
from multiple crates
Fixed by removing the aforementioned hack. Some "Unexecuted
instantiation" notices are unavoidable, as explained in the
`used_crate.rs` test, but `-Zinstrument-coverage` has new options to
back off support for either unused generics, or all unused functions,
which avoids the notice, at the cost of less coverage of unused
functions.
Fixes: #82875
Invalid LLVM coverage data produced with crate brotli_decompressor
Fixed by disabling the LLVM function attribute that forces inlining, if
`-Z instrument-coverage` is enabled. This attribute is applied to
Rust functions with `#[inline(always)], and in some cases, the forced
inlining breaks coverage instrumentation and reports.
2021-03-15 16:32:45 -07:00
|
|
|
/// executed.
|
2020-08-15 04:42:13 -07:00
|
|
|
Coverage(Box<Coverage>),
|
|
|
|
|
2022-03-26 20:46:56 -04:00
|
|
|
/// Denotes a call to the intrinsic function `copy_nonoverlapping`.
|
2022-03-25 02:17:05 -04:00
|
|
|
///
|
|
|
|
/// First, all three operands are evaluated. `src` and `dest` must each be a reference, pointer,
|
|
|
|
/// or `Box` pointing to the same type `T`. `count` must evaluate to a `usize`. Then, `src` and
|
|
|
|
/// `dest` are dereferenced, and `count * size_of::<T>()` bytes beginning with the first byte of
|
|
|
|
/// the `src` place are copied to the continguous range of bytes beginning with the first byte
|
|
|
|
/// of `dest`.
|
|
|
|
///
|
|
|
|
/// **Needs clarification**: In what order are operands computed and dereferenced? It should
|
|
|
|
/// probably match the order for assignment, but that is also undecided.
|
|
|
|
///
|
2022-04-08 15:53:08 -04:00
|
|
|
/// **Needs clarification**: Is this typed or not, ie is there a typed load and store involved?
|
|
|
|
/// I vaguely remember Ralf saying somewhere that he thought it should not be.
|
2020-10-03 20:57:47 +00:00
|
|
|
CopyNonOverlapping(Box<CopyNonOverlapping<'tcx>>),
|
|
|
|
|
2016-09-15 18:17:58 -07:00
|
|
|
/// No-op. Useful for deleting instructions without affecting statement indices.
|
|
|
|
Nop,
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2020-08-10 20:27:41 +02:00
|
|
|
impl<'tcx> StatementKind<'tcx> {
|
2020-10-03 11:18:24 +02:00
|
|
|
pub fn as_assign_mut(&mut self) -> Option<&mut (Place<'tcx>, Rvalue<'tcx>)> {
|
|
|
|
match self {
|
|
|
|
StatementKind::Assign(x) => Some(x),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_assign(&self) -> Option<&(Place<'tcx>, Rvalue<'tcx>)> {
|
2020-08-10 20:27:41 +02:00
|
|
|
match self {
|
|
|
|
StatementKind::Assign(x) => Some(x),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Describes what kind of retag is to be performed.
|
2020-10-04 15:52:14 +02:00
|
|
|
#[derive(Copy, Clone, TyEncodable, TyDecodable, Debug, PartialEq, Eq, Hash, HashStable)]
|
2018-12-11 19:54:38 +01:00
|
|
|
pub enum RetagKind {
|
2019-09-06 03:57:44 +01:00
|
|
|
/// The initial retag when entering a function.
|
2018-12-11 19:54:38 +01:00
|
|
|
FnEntry,
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Retag preparing for a two-phase borrow.
|
2018-12-11 19:54:38 +01:00
|
|
|
TwoPhase,
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Retagging raw pointers.
|
2018-12-11 19:54:38 +01:00
|
|
|
Raw,
|
2019-09-06 03:57:44 +01:00
|
|
|
/// A "normal" retag.
|
2018-12-11 19:54:38 +01:00
|
|
|
Default,
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// The `FakeReadCause` describes the type of pattern why a FakeRead statement exists.
|
2020-10-04 15:52:14 +02:00
|
|
|
#[derive(Copy, Clone, TyEncodable, TyDecodable, Debug, Hash, HashStable, PartialEq)]
|
2018-09-14 21:05:31 +02:00
|
|
|
pub enum FakeReadCause {
|
2019-02-02 16:38:12 +00:00
|
|
|
/// Inject a fake read of the borrowed input at the end of each guards
|
|
|
|
/// code.
|
2018-09-14 21:54:45 +02:00
|
|
|
///
|
2019-02-02 16:38:12 +00:00
|
|
|
/// This should ensure that you cannot change the variant for an enum while
|
|
|
|
/// you are in the midst of matching on it.
|
2018-09-13 21:35:24 +01:00
|
|
|
ForMatchGuard,
|
|
|
|
|
|
|
|
/// `let x: !; match x {}` doesn't generate any read of x so we need to
|
|
|
|
/// generate a read of x to check that it is initialized and safe.
|
2021-03-25 23:03:12 -04:00
|
|
|
///
|
|
|
|
/// If a closure pattern matches a Place starting with an Upvar, then we introduce a
|
|
|
|
/// FakeRead for that Place outside the closure, in such a case this option would be
|
|
|
|
/// Some(closure_def_id).
|
|
|
|
/// Otherwise, the value of the optional DefId will be None.
|
|
|
|
ForMatchedPlace(Option<DefId>),
|
2018-09-14 21:54:45 +02:00
|
|
|
|
2019-02-02 16:38:12 +00:00
|
|
|
/// A fake read of the RefWithinGuard version of a bind-by-value variable
|
2022-02-01 22:32:02 +01:00
|
|
|
/// in a match guard to ensure that its value hasn't change by the time
|
2019-02-02 16:38:12 +00:00
|
|
|
/// we create the OutsideGuard version.
|
|
|
|
ForGuardBinding,
|
|
|
|
|
2018-09-14 21:54:45 +02:00
|
|
|
/// Officially, the semantics of
|
|
|
|
///
|
|
|
|
/// `let pattern = <expr>;`
|
|
|
|
///
|
|
|
|
/// is that `<expr>` is evaluated into a temporary and then this temporary is
|
|
|
|
/// into the pattern.
|
|
|
|
///
|
|
|
|
/// However, if we see the simple pattern `let var = <expr>`, we optimize this to
|
|
|
|
/// evaluate `<expr>` directly into the variable `var`. This is mostly unobservable,
|
|
|
|
/// but in some cases it can affect the borrow checker, as in #53695.
|
|
|
|
/// Therefore, we insert a "fake read" here to ensure that we get
|
|
|
|
/// appropriate errors.
|
2021-03-25 23:03:12 -04:00
|
|
|
///
|
|
|
|
/// If a closure pattern matches a Place starting with an Upvar, then we introduce a
|
|
|
|
/// FakeRead for that Place outside the closure, in such a case this option would be
|
|
|
|
/// Some(closure_def_id).
|
|
|
|
/// Otherwise, the value of the optional DefId will be None.
|
|
|
|
ForLet(Option<DefId>),
|
2019-10-19 21:00:21 +01:00
|
|
|
|
|
|
|
/// If we have an index expression like
|
|
|
|
///
|
|
|
|
/// (*x)[1][{ x = y; 4}]
|
|
|
|
///
|
|
|
|
/// then the first bounds check is invalidated when we evaluate the second
|
|
|
|
/// index expression. Thus we create a fake borrow of `x` across the second
|
|
|
|
/// indexer, which will cause a borrow check error.
|
|
|
|
ForIndex,
|
2018-09-14 21:05:31 +02:00
|
|
|
}
|
|
|
|
|
2019-08-24 14:16:56 -07:00
|
|
|
impl Debug for Statement<'_> {
|
2018-08-29 22:02:42 -07:00
|
|
|
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
|
2015-08-18 17:59:21 -04:00
|
|
|
use self::StatementKind::*;
|
|
|
|
match self.kind {
|
2019-09-11 16:05:45 -03:00
|
|
|
Assign(box (ref place, ref rv)) => write!(fmt, "{:?} = {:?}", place, rv),
|
2021-03-29 22:48:44 -04:00
|
|
|
FakeRead(box (ref cause, ref place)) => {
|
|
|
|
write!(fmt, "FakeRead({:?}, {:?})", cause, place)
|
|
|
|
}
|
2019-07-12 22:49:15 +02:00
|
|
|
Retag(ref kind, ref place) => write!(
|
|
|
|
fmt,
|
|
|
|
"Retag({}{:?})",
|
|
|
|
match kind {
|
|
|
|
RetagKind::FnEntry => "[fn entry] ",
|
|
|
|
RetagKind::TwoPhase => "[2phase] ",
|
|
|
|
RetagKind::Raw => "[raw] ",
|
|
|
|
RetagKind::Default => "",
|
|
|
|
},
|
|
|
|
place,
|
|
|
|
),
|
2017-12-06 09:25:29 +01:00
|
|
|
StorageLive(ref place) => write!(fmt, "StorageLive({:?})", place),
|
|
|
|
StorageDead(ref place) => write!(fmt, "StorageDead({:?})", place),
|
2019-07-12 22:49:15 +02:00
|
|
|
SetDiscriminant { ref place, variant_index } => {
|
|
|
|
write!(fmt, "discriminant({:?}) = {:?}", place, variant_index)
|
|
|
|
}
|
2022-04-05 17:14:59 -04:00
|
|
|
Deinit(ref place) => write!(fmt, "Deinit({:?})", place),
|
2019-09-11 16:05:45 -03:00
|
|
|
AscribeUserType(box (ref place, ref c_ty), ref variance) => {
|
2018-09-05 15:52:01 -04:00
|
|
|
write!(fmt, "AscribeUserType({:?}, {:?}, {:?})", place, variance, c_ty)
|
2018-06-19 21:22:52 -03:00
|
|
|
}
|
2021-08-16 17:30:23 +02:00
|
|
|
Coverage(box self::Coverage { ref kind, code_region: Some(ref rgn) }) => {
|
|
|
|
write!(fmt, "Coverage::{:?} for {:?}", kind, rgn)
|
Tools, tests, and experimenting with MIR-derived coverage counters
Adds a new mir_dump output file in HTML/CSS to visualize code regions
and the MIR features that they came from (including overlapping spans).
See example below:
Includes a basic, MIR-block-based implementation of coverage injection,
available via `-Zexperimental-coverage`. This implementation has known
flaws and omissions, but is simple enough to validate the new tools and
tests.
The existing `-Zinstrument-coverage` option currently enables
function-level coverage only, which at least appears to generate
accurate coverage reports at that level.
Experimental coverage is not accurate at this time. When branch coverage
works as intended, the `-Zexperimental-coverage` option should be
removed.
This PR replaces the bulk of PR #75828, with the remaining parts of
that PR distributed among other separate and indentpent PRs.
This PR depends on three of those other PRs: #76000, #76002, and
Rust compiler MCP rust-lang/compiler-team#278
Relevant issue: #34701 - Implement support for LLVMs code coverage
instrumentation

2020-08-27 14:13:04 -07:00
|
|
|
}
|
2021-08-16 17:30:23 +02:00
|
|
|
Coverage(box ref coverage) => write!(fmt, "Coverage::{:?}", coverage.kind),
|
2020-10-03 20:57:47 +00:00
|
|
|
CopyNonOverlapping(box crate::mir::CopyNonOverlapping {
|
|
|
|
ref src,
|
|
|
|
ref dst,
|
2020-12-29 02:00:04 +00:00
|
|
|
ref count,
|
|
|
|
}) => {
|
|
|
|
write!(fmt, "copy_nonoverlapping(src={:?}, dst={:?}, count={:?})", src, dst, count)
|
|
|
|
}
|
2016-09-15 18:17:58 -07:00
|
|
|
Nop => write!(fmt, "nop"),
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-09 13:36:04 -05:00
|
|
|
|
2020-10-04 15:52:14 +02:00
|
|
|
#[derive(Clone, Debug, PartialEq, TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable)]
|
2020-08-15 04:42:13 -07:00
|
|
|
pub struct Coverage {
|
|
|
|
pub kind: CoverageKind,
|
2020-10-05 16:36:10 -07:00
|
|
|
pub code_region: Option<CodeRegion>,
|
2020-08-15 04:42:13 -07:00
|
|
|
}
|
|
|
|
|
2021-01-23 08:57:04 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable)]
|
2020-10-03 20:57:47 +00:00
|
|
|
pub struct CopyNonOverlapping<'tcx> {
|
2020-10-05 22:53:00 +00:00
|
|
|
pub src: Operand<'tcx>,
|
|
|
|
pub dst: Operand<'tcx>,
|
2020-12-29 02:00:04 +00:00
|
|
|
/// Number of elements to copy from src to dest, not bytes.
|
|
|
|
pub count: Operand<'tcx>,
|
2020-10-03 20:57:47 +00:00
|
|
|
}
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2017-12-06 09:25:29 +01:00
|
|
|
// Places
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2022-03-24 18:25:03 -04:00
|
|
|
/// Places roughly correspond to a "location in memory." Places in MIR are the same mathematical
|
|
|
|
/// object as places in Rust. This of course means that what exactly they are is undecided and part
|
2022-04-08 15:53:08 -04:00
|
|
|
/// of the Rust memory model. However, they will likely contain at least the following pieces of
|
|
|
|
/// information in some form:
|
2022-03-24 18:25:03 -04:00
|
|
|
///
|
2022-04-08 15:53:08 -04:00
|
|
|
/// 1. The address in memory that the place refers to.
|
|
|
|
/// 2. The provenance with which the place is being accessed.
|
|
|
|
/// 3. The type of the place and an optional variant index. See [`PlaceTy`][tcx::PlaceTy].
|
|
|
|
/// 4. Optionally, some metadata. This exists if and only if the type of the place is not `Sized`.
|
2022-03-24 18:25:03 -04:00
|
|
|
///
|
2022-04-08 15:53:08 -04:00
|
|
|
/// We'll give a description below of how all pieces of the place except for the provenance are
|
|
|
|
/// calculated. We cannot give a description of the provenance, because that is part of the
|
|
|
|
/// undecided aliasing model - we only include it here at all to acknowledge its existence.
|
2022-03-24 18:25:03 -04:00
|
|
|
///
|
2022-04-08 15:53:08 -04:00
|
|
|
/// Each local naturally corresponds to the place `Place { local, projection: [] }`. This place has
|
|
|
|
/// the address of the local's allocation and the type of the local.
|
|
|
|
///
|
|
|
|
/// **Needs clarification:** Unsized locals seem to present a bit of an issue. Their allocation
|
|
|
|
/// can't actually be created on `StorageLive`, because it's unclear how big to make the allocation.
|
|
|
|
/// Furthermore, MIR produces assignments to unsized locals, although that is not permitted under
|
|
|
|
/// `#![feature(unsized_locals)]` in Rust. Besides just putting "unsized locals are special and
|
|
|
|
/// different" in a bunch of places, I (JakobDegen) don't know how to incorporate this behavior into
|
|
|
|
/// the current MIR semantics in a clean way - possibly this needs some design work first.
|
|
|
|
///
|
|
|
|
/// For places that are not locals, ie they have a non-empty list of projections, we define the
|
|
|
|
/// values as a function of the parent place, that is the place with its last [`ProjectionElem`]
|
|
|
|
/// stripped. The way this is computed of course depends on the kind of that last projection
|
|
|
|
/// element:
|
2022-03-24 18:25:03 -04:00
|
|
|
///
|
|
|
|
/// - [`Downcast`](ProjectionElem::Downcast): This projection sets the place's variant index to the
|
|
|
|
/// given one, and makes no other changes. A `Downcast` projection on a place with its variant
|
|
|
|
/// index already set is not well-formed.
|
|
|
|
/// - [`Field`](ProjectionElem::Field): `Field` projections take their parent place and create a
|
2022-04-08 15:53:08 -04:00
|
|
|
/// place referring to one of the fields of the type. The resulting address is the parent
|
|
|
|
/// address, plus the offset of the field. The type becomes the type of the field. If the parent
|
|
|
|
/// was unsized and so had metadata associated with it, then the metadata is retained if the
|
|
|
|
/// field is unsized and thrown out if it is sized.
|
2022-03-24 18:25:03 -04:00
|
|
|
///
|
|
|
|
/// These projections are only legal for tuples, ADTs, closures, and generators. If the ADT or
|
|
|
|
/// generator has more than one variant, the parent place's variant index must be set, indicating
|
|
|
|
/// which variant is being used. If it has just one variant, the variant index may or may not be
|
|
|
|
/// included - the single possible variant is inferred if it is not included.
|
|
|
|
/// - [`ConstantIndex`](ProjectionElem::ConstantIndex): Computes an offset in units of `T` into the
|
2022-04-08 15:53:08 -04:00
|
|
|
/// place as described in the documentation for the `ProjectionElem`. The resulting address is
|
|
|
|
/// the parent's address plus that offset, and the type is `T`. This is only legal if the parent
|
|
|
|
/// place has type `[T; N]` or `[T]` (*not* `&[T]`). Since such a `T` is always sized, any
|
|
|
|
/// resulting metadata is thrown out.
|
|
|
|
/// - [`Subslice`](ProjectionElem::Subslice): This projection calculates an offset and a new
|
|
|
|
/// address in a similar manner as `ConstantIndex`. It is also only legal on `[T; N]` and `[T]`.
|
|
|
|
/// However, this yields a `Place` of type `[T]`, and additionally sets the metadata to be the
|
|
|
|
/// length of the subslice.
|
2022-03-24 18:25:03 -04:00
|
|
|
/// - [`Index`](ProjectionElem::Index): Like `ConstantIndex`, only legal on `[T; N]` or `[T]`.
|
|
|
|
/// However, `Index` additionally takes a local from which the value of the index is computed at
|
|
|
|
/// runtime. Computing the value of the index involves interpreting the `Local` as a
|
|
|
|
/// `Place { local, projection: [] }`, and then computing its value as if done via
|
|
|
|
/// [`Operand::Copy`]. The array/slice is then indexed with the resulting value. The local must
|
|
|
|
/// have type `usize`.
|
|
|
|
/// - [`Deref`](ProjectionElem::Deref): Derefs are the last type of projection, and the most
|
|
|
|
/// complicated. They are only legal on parent places that are references, pointers, or `Box`. A
|
2022-04-08 15:53:08 -04:00
|
|
|
/// `Deref` projection begins by loading a value from the parent place, as if by
|
2022-03-24 18:25:03 -04:00
|
|
|
/// [`Operand::Copy`]. It then dereferences the resulting pointer, creating a place of the
|
2022-04-08 15:53:08 -04:00
|
|
|
/// pointee's type. The resulting address is the address that was stored in the pointer. If the
|
|
|
|
/// pointee type is unsized, the pointer additionally stored the value of the metadata.
|
2022-03-24 18:25:03 -04:00
|
|
|
///
|
2022-04-08 15:53:08 -04:00
|
|
|
/// Computing a place may cause UB. One possibility is that the pointer used for a `Deref` may not
|
2022-04-09 10:00:19 -04:00
|
|
|
/// be suitably aligned. Another possibility is that the place is not in bounds, meaning it does not
|
2022-04-08 15:53:08 -04:00
|
|
|
/// point to an actual allocation.
|
2022-03-24 18:25:03 -04:00
|
|
|
///
|
2022-04-08 15:53:08 -04:00
|
|
|
/// However, if this is actually UB and when the UB kicks in is undecided. This is being discussed
|
|
|
|
/// in [UCG#319]. The options include that every place must obey those rules, that only some places
|
|
|
|
/// must obey them, or that places impose no rules of their own.
|
2022-03-24 18:25:03 -04:00
|
|
|
///
|
|
|
|
/// [UCG#319]: https://github.com/rust-lang/unsafe-code-guidelines/issues/319
|
|
|
|
///
|
2022-04-08 15:53:08 -04:00
|
|
|
/// Rust currently requires that every place obey those two rules. This is checked by MIRI and taken
|
|
|
|
/// advantage of by codegen (via `gep inbounds`). That is possibly subject to change.
|
2022-01-18 10:05:41 -06:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, HashStable)]
|
2019-04-30 18:58:24 +02:00
|
|
|
pub struct Place<'tcx> {
|
2019-12-11 16:50:03 -03:00
|
|
|
pub local: Local,
|
2019-02-22 05:24:03 +01:00
|
|
|
|
|
|
|
/// projection out of a place (access a field, deref a pointer, etc)
|
2019-10-20 16:11:04 -04:00
|
|
|
pub projection: &'tcx List<PlaceElem<'tcx>>,
|
2019-02-22 05:24:03 +01:00
|
|
|
}
|
|
|
|
|
2021-09-05 16:42:36 +01:00
|
|
|
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
|
2021-03-03 16:25:37 +00:00
|
|
|
static_assert_size!(Place<'_>, 16);
|
|
|
|
|
2019-10-19 21:00:21 +01:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2020-06-11 15:49:57 +01:00
|
|
|
#[derive(TyEncodable, TyDecodable, HashStable)]
|
2019-03-28 18:00:17 -07:00
|
|
|
pub enum ProjectionElem<V, T> {
|
2015-08-18 17:59:21 -04:00
|
|
|
Deref,
|
2017-07-27 23:12:08 +03:00
|
|
|
Field(Field, T),
|
2022-03-06 15:53:23 -05:00
|
|
|
/// Index into a slice/array.
|
|
|
|
///
|
|
|
|
/// Note that this does not also dereference, and so it does not exactly correspond to slice
|
|
|
|
/// indexing in Rust. In other words, in the below Rust code:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// let x = &[1, 2, 3, 4];
|
|
|
|
/// let i = 2;
|
|
|
|
/// x[i];
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// The `x[i]` is turned into a `Deref` followed by an `Index`, not just an `Index`. The same
|
|
|
|
/// thing is true of the `ConstantIndex` and `Subslice` projections below.
|
2015-08-18 17:59:21 -04:00
|
|
|
Index(V),
|
|
|
|
|
2016-02-03 13:25:07 +01:00
|
|
|
/// These indices are generated by slice patterns. Easiest to explain
|
|
|
|
/// by example:
|
|
|
|
///
|
2022-04-15 15:04:34 -07:00
|
|
|
/// ```ignore (illustrative)
|
2016-02-03 13:25:07 +01:00
|
|
|
/// [X, _, .._, _, _] => { offset: 0, min_length: 4, from_end: false },
|
|
|
|
/// [_, X, .._, _, _] => { offset: 1, min_length: 4, from_end: false },
|
|
|
|
/// [_, _, .._, X, _] => { offset: 2, min_length: 4, from_end: true },
|
|
|
|
/// [_, _, .._, _, X] => { offset: 1, min_length: 4, from_end: true },
|
|
|
|
/// ```
|
2015-08-18 17:59:21 -04:00
|
|
|
ConstantIndex {
|
2016-02-03 13:25:07 +01:00
|
|
|
/// index or -index (in Python terms), depending on from_end
|
2020-08-23 14:54:58 +02:00
|
|
|
offset: u64,
|
2019-11-22 20:28:02 +00:00
|
|
|
/// The thing being indexed must be at least this long. For arrays this
|
|
|
|
/// is always the exact length.
|
2020-08-23 14:54:58 +02:00
|
|
|
min_length: u64,
|
2019-11-22 20:28:02 +00:00
|
|
|
/// Counting backwards from end? This is always false when indexing an
|
|
|
|
/// array.
|
2016-02-03 13:25:07 +01:00
|
|
|
from_end: bool,
|
2015-08-18 17:59:21 -04:00
|
|
|
},
|
|
|
|
|
2016-03-11 12:54:59 +02:00
|
|
|
/// These indices are generated by slice patterns.
|
|
|
|
///
|
2019-11-22 20:28:02 +00:00
|
|
|
/// If `from_end` is true `slice[from..slice.len() - to]`.
|
|
|
|
/// Otherwise `array[from..to]`.
|
2016-03-11 12:54:59 +02:00
|
|
|
Subslice {
|
2020-08-23 14:54:58 +02:00
|
|
|
from: u64,
|
|
|
|
to: u64,
|
2019-11-22 20:28:02 +00:00
|
|
|
/// Whether `to` counts from the start or end of the array/slice.
|
|
|
|
/// For `PlaceElem`s this is `true` if and only if the base is a slice.
|
|
|
|
/// For `ProjectionKind`, this can also be `true` for arrays.
|
|
|
|
from_end: bool,
|
2016-03-11 12:54:59 +02:00
|
|
|
},
|
|
|
|
|
2022-05-03 00:00:00 +00:00
|
|
|
/// "Downcast" to a variant of an enum or a generator.
|
2019-03-28 18:00:17 -07:00
|
|
|
///
|
|
|
|
/// The included Symbol is the name of the variant, used for printing MIR.
|
|
|
|
Downcast(Option<Symbol>, VariantIdx),
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2019-08-29 13:22:16 -07:00
|
|
|
impl<V, T> ProjectionElem<V, T> {
|
|
|
|
/// Returns `true` if the target of this projection may refer to a different region of memory
|
|
|
|
/// than the base.
|
|
|
|
fn is_indirect(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Self::Deref => true,
|
|
|
|
|
|
|
|
Self::Field(_, _)
|
|
|
|
| Self::Index(_)
|
|
|
|
| Self::ConstantIndex { .. }
|
|
|
|
| Self::Subslice { .. }
|
|
|
|
| Self::Downcast(_, _) => false,
|
|
|
|
}
|
|
|
|
}
|
2021-11-30 14:56:26 -08:00
|
|
|
|
|
|
|
/// Returns `true` if this is a `Downcast` projection with the given `VariantIdx`.
|
|
|
|
pub fn is_downcast_to(&self, v: VariantIdx) -> bool {
|
|
|
|
matches!(*self, Self::Downcast(_, x) if x == v)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if this is a `Field` projection with the given index.
|
|
|
|
pub fn is_field_to(&self, f: Field) -> bool {
|
|
|
|
matches!(*self, Self::Field(x, _) if x == f)
|
|
|
|
}
|
2019-08-29 13:22:16 -07:00
|
|
|
}
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
/// Alias for projections as they appear in places, where the base is a place
|
2017-09-03 21:55:41 +03:00
|
|
|
/// and the index is a local.
|
2019-03-28 18:00:17 -07:00
|
|
|
pub type PlaceElem<'tcx> = ProjectionElem<Local, Ty<'tcx>>;
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2022-03-08 15:39:52 +01:00
|
|
|
// This type is fairly frequently used, so we shouldn't unintentionally increase
|
|
|
|
// its size.
|
2021-03-06 16:02:48 +00:00
|
|
|
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
|
2020-08-23 14:54:58 +02:00
|
|
|
static_assert_size!(PlaceElem<'_>, 24);
|
2018-11-01 13:30:04 +01:00
|
|
|
|
2018-10-26 11:28:40 +02:00
|
|
|
/// Alias for projections as they appear in `UserTypeProjection`, where we
|
|
|
|
/// need neither the `V` parameter for `Index` nor the `T` for `Field`.
|
2019-03-28 18:00:17 -07:00
|
|
|
pub type ProjectionKind = ProjectionElem<(), ()>;
|
2018-10-26 11:28:40 +02:00
|
|
|
|
2019-09-26 05:38:33 +00:00
|
|
|
rustc_index::newtype_index! {
|
2022-02-18 12:37:48 +08:00
|
|
|
/// A [newtype'd][wrapper] index type in the MIR [control-flow graph][CFG]
|
|
|
|
///
|
|
|
|
/// A field (e.g., `f` in `_1.f`) is one variant of [`ProjectionElem`]. Conceptually,
|
2022-02-18 15:38:03 +08:00
|
|
|
/// rustc can identify that a field projection refers to either two different regions of memory
|
|
|
|
/// or the same one between the base and the 'projection element'.
|
2022-02-18 12:37:48 +08:00
|
|
|
/// Read more about projections in the [rustc-dev-guide][mir-datatypes]
|
2022-02-24 17:16:36 -05:00
|
|
|
///
|
2022-02-18 12:37:48 +08:00
|
|
|
/// [wrapper]: https://rustc-dev-guide.rust-lang.org/appendix/glossary.html#newtype
|
|
|
|
/// [CFG]: https://rustc-dev-guide.rust-lang.org/appendix/background.html#cfg
|
|
|
|
/// [mir-datatypes]: https://rustc-dev-guide.rust-lang.org/mir/index.html#mir-data-types
|
2018-07-25 13:41:32 +03:00
|
|
|
pub struct Field {
|
2018-12-03 01:14:35 +01:00
|
|
|
derive [HashStable]
|
2018-07-25 13:41:32 +03:00
|
|
|
DEBUG_FORMAT = "field[{}]"
|
|
|
|
}
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2022-05-09 22:48:43 -04:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
2020-03-04 18:25:03 -03:00
|
|
|
pub struct PlaceRef<'tcx> {
|
2020-01-14 02:10:05 -03:00
|
|
|
pub local: Local,
|
2020-03-04 18:25:03 -03:00
|
|
|
pub projection: &'tcx [PlaceElem<'tcx>],
|
2019-07-01 23:30:38 +02:00
|
|
|
}
|
|
|
|
|
2022-05-09 22:48:43 -04:00
|
|
|
// Once we stop implementing `Ord` for `DefId`,
|
|
|
|
// this impl will be unnecessary. Until then, we'll
|
|
|
|
// leave this impl in place to prevent re-adding a
|
|
|
|
// dependnecy on the `Ord` impl for `DefId`
|
|
|
|
impl<'tcx> !PartialOrd for PlaceRef<'tcx> {}
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
impl<'tcx> Place<'tcx> {
|
2019-10-20 16:11:04 -04:00
|
|
|
// FIXME change this to a const fn by also making List::empty a const fn.
|
2019-07-30 00:07:28 +02:00
|
|
|
pub fn return_place() -> Place<'tcx> {
|
2019-12-11 16:50:03 -03:00
|
|
|
Place { local: RETURN_PLACE, projection: List::empty() }
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
2018-09-19 02:48:47 +02:00
|
|
|
|
2019-08-29 13:22:16 -07:00
|
|
|
/// Returns `true` if this `Place` contains a `Deref` projection.
|
|
|
|
///
|
|
|
|
/// If `Place::is_indirect` returns false, the caller knows that the `Place` refers to the
|
|
|
|
/// same region of memory as its base.
|
|
|
|
pub fn is_indirect(&self) -> bool {
|
2019-07-30 00:07:28 +02:00
|
|
|
self.projection.iter().any(|elem| elem.is_indirect())
|
2019-08-29 13:22:16 -07:00
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Finds the innermost `Local` from this `Place`, *if* it is either a local itself or
|
2018-10-16 15:39:07 +02:00
|
|
|
/// a single deref of a local.
|
2021-01-02 19:31:37 +01:00
|
|
|
#[inline(always)]
|
2019-05-27 23:37:18 +02:00
|
|
|
pub fn local_or_deref_local(&self) -> Option<Local> {
|
2021-01-02 19:31:37 +01:00
|
|
|
self.as_ref().local_or_deref_local()
|
2018-09-19 02:48:47 +02:00
|
|
|
}
|
2018-10-16 15:39:07 +02:00
|
|
|
|
2019-09-19 11:41:10 -04:00
|
|
|
/// If this place represents a local variable like `_X` with no
|
|
|
|
/// projections, return `Some(_X)`.
|
2021-01-02 19:31:37 +01:00
|
|
|
#[inline(always)]
|
2019-09-19 11:41:10 -04:00
|
|
|
pub fn as_local(&self) -> Option<Local> {
|
2019-10-21 20:20:47 -03:00
|
|
|
self.as_ref().as_local()
|
2019-09-19 11:41:10 -04:00
|
|
|
}
|
|
|
|
|
2021-02-26 00:00:00 +00:00
|
|
|
#[inline]
|
2020-03-04 18:25:03 -03:00
|
|
|
pub fn as_ref(&self) -> PlaceRef<'tcx> {
|
2020-01-14 02:10:05 -03:00
|
|
|
PlaceRef { local: self.local, projection: &self.projection }
|
2019-07-02 21:01:20 +02:00
|
|
|
}
|
2020-11-20 11:50:15 +01:00
|
|
|
|
|
|
|
/// Iterate over the projections in evaluation order, i.e., the first element is the base with
|
|
|
|
/// its projection and then subsequently more projections are added.
|
2020-11-28 18:02:12 +01:00
|
|
|
/// As a concrete example, given the place a.b.c, this would yield:
|
|
|
|
/// - (a, .b)
|
|
|
|
/// - (a.b, .c)
|
2021-01-02 19:31:37 +01:00
|
|
|
///
|
2020-11-28 18:02:12 +01:00
|
|
|
/// Given a place without projections, the iterator is empty.
|
2021-02-26 00:00:00 +00:00
|
|
|
#[inline]
|
2020-11-21 13:20:34 +01:00
|
|
|
pub fn iter_projections(
|
|
|
|
self,
|
|
|
|
) -> impl Iterator<Item = (PlaceRef<'tcx>, PlaceElem<'tcx>)> + DoubleEndedIterator {
|
2020-11-20 11:50:15 +01:00
|
|
|
self.projection.iter().enumerate().map(move |(i, proj)| {
|
|
|
|
let base = PlaceRef { local: self.local, projection: &self.projection[..i] };
|
|
|
|
(base, proj)
|
|
|
|
})
|
|
|
|
}
|
2022-02-22 23:19:57 -08:00
|
|
|
|
|
|
|
/// Generates a new place by appending `more_projections` to the existing ones
|
|
|
|
/// and interning the result.
|
|
|
|
pub fn project_deeper(self, more_projections: &[PlaceElem<'tcx>], tcx: TyCtxt<'tcx>) -> Self {
|
|
|
|
if more_projections.is_empty() {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut v: Vec<PlaceElem<'tcx>>;
|
|
|
|
|
|
|
|
let new_projections = if self.projection.is_empty() {
|
|
|
|
more_projections
|
|
|
|
} else {
|
|
|
|
v = Vec::with_capacity(self.projection.len() + more_projections.len());
|
|
|
|
v.extend(self.projection);
|
|
|
|
v.extend(more_projections);
|
|
|
|
&v
|
|
|
|
};
|
|
|
|
|
|
|
|
Place { local: self.local, projection: tcx.intern_place_elems(new_projections) }
|
|
|
|
}
|
2019-02-26 23:41:38 +01:00
|
|
|
}
|
|
|
|
|
2019-06-24 17:46:09 +02:00
|
|
|
impl From<Local> for Place<'_> {
|
|
|
|
fn from(local: Local) -> Self {
|
2019-12-13 16:23:38 -03:00
|
|
|
Place { local, projection: List::empty() }
|
2019-06-24 17:46:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 18:25:03 -03:00
|
|
|
impl<'tcx> PlaceRef<'tcx> {
|
2019-07-11 19:25:37 +02:00
|
|
|
/// Finds the innermost `Local` from this `Place`, *if* it is either a local itself or
|
|
|
|
/// a single deref of a local.
|
|
|
|
pub fn local_or_deref_local(&self) -> Option<Local> {
|
2020-01-14 02:10:05 -03:00
|
|
|
match *self {
|
2019-12-11 16:50:03 -03:00
|
|
|
PlaceRef { local, projection: [] }
|
2020-01-14 02:10:05 -03:00
|
|
|
| PlaceRef { local, projection: [ProjectionElem::Deref] } => Some(local),
|
2019-07-11 19:25:37 +02:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2019-10-21 20:20:47 -03:00
|
|
|
|
|
|
|
/// If this place represents a local variable like `_X` with no
|
|
|
|
/// projections, return `Some(_X)`.
|
2021-06-01 00:00:00 +00:00
|
|
|
#[inline]
|
2019-10-21 20:20:47 -03:00
|
|
|
pub fn as_local(&self) -> Option<Local> {
|
2020-01-14 02:10:05 -03:00
|
|
|
match *self {
|
|
|
|
PlaceRef { local, projection: [] } => Some(local),
|
2019-10-21 20:20:47 -03:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2021-01-02 19:31:37 +01:00
|
|
|
|
2021-06-01 00:00:00 +00:00
|
|
|
#[inline]
|
2021-01-02 19:31:37 +01:00
|
|
|
pub fn last_projection(&self) -> Option<(PlaceRef<'tcx>, PlaceElem<'tcx>)> {
|
|
|
|
if let &[ref proj_base @ .., elem] = self.projection {
|
|
|
|
Some((PlaceRef { local: self.local, projection: proj_base }, elem))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2019-07-02 20:29:45 +02:00
|
|
|
}
|
|
|
|
|
2019-08-24 14:16:56 -07:00
|
|
|
impl Debug for Place<'_> {
|
2018-08-29 22:02:42 -07:00
|
|
|
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
|
2019-07-30 00:07:28 +02:00
|
|
|
for elem in self.projection.iter().rev() {
|
|
|
|
match elem {
|
|
|
|
ProjectionElem::Downcast(_, _) | ProjectionElem::Field(_, _) => {
|
|
|
|
write!(fmt, "(").unwrap();
|
|
|
|
}
|
|
|
|
ProjectionElem::Deref => {
|
|
|
|
write!(fmt, "(*").unwrap();
|
2018-06-19 21:22:52 -03:00
|
|
|
}
|
2019-07-30 00:07:28 +02:00
|
|
|
ProjectionElem::Index(_)
|
|
|
|
| ProjectionElem::ConstantIndex { .. }
|
|
|
|
| ProjectionElem::Subslice { .. } => {}
|
2019-04-24 19:21:10 -03:00
|
|
|
}
|
2019-07-30 00:07:28 +02:00
|
|
|
}
|
2019-04-24 19:21:10 -03:00
|
|
|
|
2019-12-11 16:50:03 -03:00
|
|
|
write!(fmt, "{:?}", self.local)?;
|
2019-04-24 19:21:10 -03:00
|
|
|
|
2019-07-30 00:07:28 +02:00
|
|
|
for elem in self.projection.iter() {
|
|
|
|
match elem {
|
|
|
|
ProjectionElem::Downcast(Some(name), _index) => {
|
|
|
|
write!(fmt, " as {})", name)?;
|
|
|
|
}
|
|
|
|
ProjectionElem::Downcast(None, index) => {
|
|
|
|
write!(fmt, " as variant#{:?})", index)?;
|
|
|
|
}
|
|
|
|
ProjectionElem::Deref => {
|
|
|
|
write!(fmt, ")")?;
|
|
|
|
}
|
|
|
|
ProjectionElem::Field(field, ty) => {
|
|
|
|
write!(fmt, ".{:?}: {:?})", field.index(), ty)?;
|
|
|
|
}
|
|
|
|
ProjectionElem::Index(ref index) => {
|
|
|
|
write!(fmt, "[{:?}]", index)?;
|
|
|
|
}
|
|
|
|
ProjectionElem::ConstantIndex { offset, min_length, from_end: false } => {
|
|
|
|
write!(fmt, "[{:?} of {:?}]", offset, min_length)?;
|
|
|
|
}
|
|
|
|
ProjectionElem::ConstantIndex { offset, min_length, from_end: true } => {
|
|
|
|
write!(fmt, "[-{:?} of {:?}]", offset, min_length)?;
|
|
|
|
}
|
2020-05-23 11:49:24 +02:00
|
|
|
ProjectionElem::Subslice { from, to, from_end: true } if to == 0 => {
|
2019-07-30 00:07:28 +02:00
|
|
|
write!(fmt, "[{:?}:]", from)?;
|
|
|
|
}
|
2020-05-23 11:49:24 +02:00
|
|
|
ProjectionElem::Subslice { from, to, from_end: true } if from == 0 => {
|
2019-07-30 00:07:28 +02:00
|
|
|
write!(fmt, "[:-{:?}]", to)?;
|
|
|
|
}
|
2019-11-22 20:28:02 +00:00
|
|
|
ProjectionElem::Subslice { from, to, from_end: true } => {
|
2019-07-30 00:07:28 +02:00
|
|
|
write!(fmt, "[{:?}:-{:?}]", from, to)?;
|
2018-06-19 21:22:52 -03:00
|
|
|
}
|
2019-11-22 20:28:02 +00:00
|
|
|
ProjectionElem::Subslice { from, to, from_end: false } => {
|
|
|
|
write!(fmt, "[{:?}..{:?}]", from, to)?;
|
|
|
|
}
|
2019-04-24 19:21:10 -03:00
|
|
|
}
|
2019-07-30 00:07:28 +02:00
|
|
|
}
|
2019-04-24 19:21:10 -03:00
|
|
|
|
2019-07-30 00:07:28 +02:00
|
|
|
Ok(())
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-09 11:04:26 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Scopes
|
|
|
|
|
2019-09-26 05:38:33 +00:00
|
|
|
rustc_index::newtype_index! {
|
2018-07-25 13:41:32 +03:00
|
|
|
pub struct SourceScope {
|
2018-12-03 01:14:35 +01:00
|
|
|
derive [HashStable]
|
2017-12-06 09:25:29 +01:00
|
|
|
DEBUG_FORMAT = "scope[{}]",
|
2018-05-28 14:16:09 +03:00
|
|
|
const OUTERMOST_SOURCE_SCOPE = 0,
|
2018-07-25 13:41:32 +03:00
|
|
|
}
|
|
|
|
}
|
2016-03-09 11:04:26 -05:00
|
|
|
|
2021-03-26 13:39:04 +00:00
|
|
|
impl SourceScope {
|
|
|
|
/// Finds the original HirId this MIR item came from.
|
|
|
|
/// This is necessary after MIR optimizations, as otherwise we get a HirId
|
|
|
|
/// from the function that was inlined instead of the function call site.
|
2021-12-15 19:32:30 -05:00
|
|
|
pub fn lint_root<'tcx>(
|
2021-03-26 16:33:14 +00:00
|
|
|
self,
|
|
|
|
source_scopes: &IndexVec<SourceScope, SourceScopeData<'tcx>>,
|
|
|
|
) -> Option<HirId> {
|
2021-03-26 13:39:04 +00:00
|
|
|
let mut data = &source_scopes[self];
|
|
|
|
// FIXME(oli-obk): we should be able to just walk the `inlined_parent_scope`, but it
|
|
|
|
// does not work as I thought it would. Needs more investigation and documentation.
|
|
|
|
while data.inlined.is_some() {
|
|
|
|
trace!(?data);
|
|
|
|
data = &source_scopes[data.parent_scope.unwrap()];
|
|
|
|
}
|
|
|
|
trace!(?data);
|
|
|
|
match &data.local_data {
|
|
|
|
ClearCrossCrate::Set(data) => Some(data.lint_root),
|
|
|
|
ClearCrossCrate::Clear => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 21:31:09 +02:00
|
|
|
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
|
|
|
|
pub struct SourceScopeData<'tcx> {
|
2016-04-06 17:17:12 +03:00
|
|
|
pub span: Span,
|
2018-05-28 14:16:09 +03:00
|
|
|
pub parent_scope: Option<SourceScope>,
|
2019-11-26 22:17:35 +02:00
|
|
|
|
2020-02-08 21:31:09 +02:00
|
|
|
/// Whether this scope is the root of a scope tree of another body,
|
|
|
|
/// inlined into this body by the MIR inliner.
|
|
|
|
/// `ty::Instance` is the callee, and the `Span` is the call site.
|
|
|
|
pub inlined: Option<(ty::Instance<'tcx>, Span)>,
|
|
|
|
|
2020-09-21 06:52:37 +03:00
|
|
|
/// Nearest (transitive) parent scope (if any) which is inlined.
|
|
|
|
/// This is an optimization over walking up `parent_scope`
|
|
|
|
/// until a scope with `inlined: Some(...)` is found.
|
|
|
|
pub inlined_parent_scope: Option<SourceScope>,
|
|
|
|
|
2019-11-26 22:17:35 +02:00
|
|
|
/// Crate-local information for this source scope, that can't (and
|
|
|
|
/// needn't) be tracked across crates.
|
|
|
|
pub local_data: ClearCrossCrate<SourceScopeLocalData>,
|
2016-03-09 11:04:26 -05:00
|
|
|
}
|
|
|
|
|
2020-06-11 15:49:57 +01:00
|
|
|
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
|
2018-05-28 17:37:48 +03:00
|
|
|
pub struct SourceScopeLocalData {
|
2019-09-06 03:57:44 +01:00
|
|
|
/// An `HirId` with lint levels equivalent to this scope's lint levels.
|
2019-02-22 15:48:14 +01:00
|
|
|
pub lint_root: hir::HirId,
|
2018-05-28 17:37:48 +03:00
|
|
|
/// The unsafe block that contains this node.
|
|
|
|
pub safety: Safety,
|
|
|
|
}
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Operands
|
2016-03-09 11:04:26 -05:00
|
|
|
|
2022-03-24 18:25:03 -04:00
|
|
|
/// An operand in MIR represents a "value" in Rust, the definition of which is undecided and part of
|
|
|
|
/// the memory model. One proposal for a definition of values can be found [on UCG][value-def].
|
|
|
|
///
|
|
|
|
/// [value-def]: https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/value-domain.md
|
|
|
|
///
|
2022-04-08 15:53:08 -04:00
|
|
|
/// The most common way to create values is via loading a place. Loading a place is an operation
|
|
|
|
/// which reads the memory of the place and converts it to a value. This is a fundamentally *typed*
|
|
|
|
/// operation. The nature of the value produced depends on the type of the conversion. Furthermore,
|
|
|
|
/// there may be other effects: if the type has a validity constraint loading the place might be UB
|
|
|
|
/// if the validity constraint is not met.
|
2022-03-24 18:25:03 -04:00
|
|
|
///
|
2022-04-08 15:53:08 -04:00
|
|
|
/// **Needs clarification:** Ralf proposes that loading a place not have side-effects.
|
2022-03-26 20:46:56 -04:00
|
|
|
/// This is what is implemented in miri today. Are these the semantics we want for MIR? Is this
|
|
|
|
/// something we can even decide without knowing more about Rust's memory model?
|
2022-03-24 18:25:03 -04:00
|
|
|
///
|
2022-04-09 10:00:19 -04:00
|
|
|
/// **Needs clarifiation:** Is loading a place that has its variant index set well-formed? Miri
|
|
|
|
/// currently implements it, but it seems like this may be something to check against in the
|
|
|
|
/// validator.
|
2022-01-18 10:05:41 -06:00
|
|
|
#[derive(Clone, PartialEq, TyEncodable, TyDecodable, Hash, HashStable)]
|
2015-10-05 12:31:48 -04:00
|
|
|
pub enum Operand<'tcx> {
|
2022-04-22 16:43:26 -04:00
|
|
|
/// Creates a value by loading the given place.
|
|
|
|
///
|
|
|
|
/// Before drop elaboration, the type of the place must be `Copy`. After drop elaboration there
|
|
|
|
/// is no such requirement.
|
2017-12-06 09:25:29 +01:00
|
|
|
Copy(Place<'tcx>),
|
2018-08-09 06:18:00 -04:00
|
|
|
|
2022-04-08 15:53:08 -04:00
|
|
|
/// Creates a value by performing loading the place, just like the `Copy` operand.
|
2022-03-24 18:25:03 -04:00
|
|
|
///
|
|
|
|
/// This *may* additionally overwrite the place with `uninit` bytes, depending on how we decide
|
2022-04-08 15:53:08 -04:00
|
|
|
/// in [UCG#188]. You should not emit MIR that may attempt a subsequent second load of this
|
|
|
|
/// place without first re-initializing it.
|
2017-12-06 09:25:29 +01:00
|
|
|
///
|
2022-03-24 18:25:03 -04:00
|
|
|
/// [UCG#188]: https://github.com/rust-lang/unsafe-code-guidelines/issues/188
|
2017-12-06 09:25:29 +01:00
|
|
|
Move(Place<'tcx>),
|
2018-08-09 06:18:00 -04:00
|
|
|
|
2022-03-24 18:25:03 -04:00
|
|
|
/// Constants are already semantically values, and remain unchanged.
|
2017-05-12 01:38:26 +03:00
|
|
|
Constant(Box<Constant<'tcx>>),
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2021-09-05 16:42:36 +01:00
|
|
|
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
|
2021-03-03 16:25:37 +00:00
|
|
|
static_assert_size!(Operand<'_>, 24);
|
|
|
|
|
2015-10-05 12:31:48 -04:00
|
|
|
impl<'tcx> Debug for Operand<'tcx> {
|
2018-08-29 22:02:42 -07:00
|
|
|
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
|
2015-08-18 17:59:21 -04:00
|
|
|
use self::Operand::*;
|
|
|
|
match *self {
|
|
|
|
Constant(ref a) => write!(fmt, "{:?}", a),
|
2017-12-06 09:25:29 +01:00
|
|
|
Copy(ref place) => write!(fmt, "{:?}", place),
|
|
|
|
Move(ref place) => write!(fmt, "move {:?}", place),
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 01:08:21 +02:00
|
|
|
impl<'tcx> Operand<'tcx> {
|
2018-08-09 06:18:00 -04:00
|
|
|
/// Convenience helper to make a constant that refers to the fn
|
2019-02-08 14:53:55 +01:00
|
|
|
/// with given `DefId` and substs. Since this is used to synthesize
|
2018-08-09 06:18:00 -04:00
|
|
|
/// MIR, assumes `user_ty` is None.
|
2019-06-11 23:35:39 +03:00
|
|
|
pub fn function_handle(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2017-03-07 16:09:01 +01:00
|
|
|
def_id: DefId,
|
2019-02-09 22:11:53 +08:00
|
|
|
substs: SubstsRef<'tcx>,
|
2017-03-07 16:09:01 +01:00
|
|
|
span: Span,
|
|
|
|
) -> Self {
|
2022-05-08 15:12:56 -04:00
|
|
|
let ty = tcx.bound_type_of(def_id).subst(tcx, substs);
|
2021-08-05 05:14:53 +02:00
|
|
|
Operand::Constant(Box::new(Constant {
|
2017-07-03 11:19:51 -07:00
|
|
|
span,
|
2018-08-09 06:18:00 -04:00
|
|
|
user_ty: None,
|
2021-03-15 11:23:44 +00:00
|
|
|
literal: ConstantKind::Ty(ty::Const::zero_sized(tcx, ty)),
|
2021-08-05 05:14:53 +02:00
|
|
|
}))
|
2017-03-14 01:08:21 +02:00
|
|
|
}
|
|
|
|
|
2020-08-10 20:27:41 +02:00
|
|
|
pub fn is_move(&self) -> bool {
|
|
|
|
matches!(self, Operand::Move(..))
|
|
|
|
}
|
|
|
|
|
2020-06-10 09:54:02 -07:00
|
|
|
/// Convenience helper to make a literal-like constant from a given scalar value.
|
|
|
|
/// Since this is used to synthesize MIR, assumes `user_ty` is None.
|
|
|
|
pub fn const_from_scalar(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
val: Scalar,
|
|
|
|
span: Span,
|
|
|
|
) -> Operand<'tcx> {
|
|
|
|
debug_assert!({
|
|
|
|
let param_env_and_ty = ty::ParamEnv::empty().and(ty);
|
|
|
|
let type_size = tcx
|
|
|
|
.layout_of(param_env_and_ty)
|
|
|
|
.unwrap_or_else(|e| panic!("could not compute layout for {:?}: {:?}", ty, e))
|
|
|
|
.size;
|
2020-09-26 15:15:35 +02:00
|
|
|
let scalar_size = match val {
|
2020-11-01 16:57:03 +00:00
|
|
|
Scalar::Int(int) => int.size(),
|
2020-06-10 09:54:02 -07:00
|
|
|
_ => panic!("Invalid scalar type {:?}", val),
|
2020-09-26 15:15:35 +02:00
|
|
|
};
|
2020-06-10 09:54:02 -07:00
|
|
|
scalar_size == type_size
|
|
|
|
});
|
2021-08-05 05:14:53 +02:00
|
|
|
Operand::Constant(Box::new(Constant {
|
2020-06-10 09:54:02 -07:00
|
|
|
span,
|
|
|
|
user_ty: None,
|
2021-07-12 18:22:15 +02:00
|
|
|
literal: ConstantKind::Val(ConstValue::Scalar(val), ty),
|
2021-08-05 05:14:53 +02:00
|
|
|
}))
|
2020-06-10 09:54:02 -07:00
|
|
|
}
|
|
|
|
|
2017-12-04 17:46:23 +02:00
|
|
|
pub fn to_copy(&self) -> Self {
|
|
|
|
match *self {
|
|
|
|
Operand::Copy(_) | Operand::Constant(_) => self.clone(),
|
2020-01-22 16:30:15 +01:00
|
|
|
Operand::Move(place) => Operand::Copy(place),
|
2017-12-04 17:46:23 +02:00
|
|
|
}
|
|
|
|
}
|
2020-01-24 15:30:51 -08:00
|
|
|
|
|
|
|
/// Returns the `Place` that is the target of this `Operand`, or `None` if this `Operand` is a
|
|
|
|
/// constant.
|
2020-03-31 13:54:20 -03:00
|
|
|
pub fn place(&self) -> Option<Place<'tcx>> {
|
2020-01-24 15:30:51 -08:00
|
|
|
match self {
|
2020-03-31 13:54:20 -03:00
|
|
|
Operand::Copy(place) | Operand::Move(place) => Some(*place),
|
2020-01-24 15:30:51 -08:00
|
|
|
Operand::Constant(_) => None,
|
|
|
|
}
|
|
|
|
}
|
2020-08-29 14:16:39 +02:00
|
|
|
|
|
|
|
/// Returns the `Constant` that is the target of this `Operand`, or `None` if this `Operand` is a
|
|
|
|
/// place.
|
|
|
|
pub fn constant(&self) -> Option<&Constant<'tcx>> {
|
|
|
|
match self {
|
|
|
|
Operand::Constant(x) => Some(&**x),
|
|
|
|
Operand::Copy(_) | Operand::Move(_) => None,
|
|
|
|
}
|
|
|
|
}
|
2022-02-22 23:19:57 -08:00
|
|
|
|
|
|
|
/// Gets the `ty::FnDef` from an operand if it's a constant function item.
|
|
|
|
///
|
|
|
|
/// While this is unlikely in general, it's the normal case of what you'll
|
|
|
|
/// find as the `func` in a [`TerminatorKind::Call`].
|
|
|
|
pub fn const_fn_def(&self) -> Option<(DefId, SubstsRef<'tcx>)> {
|
2022-03-10 01:50:47 -08:00
|
|
|
let const_ty = self.constant()?.literal.ty();
|
2022-02-22 23:19:57 -08:00
|
|
|
if let ty::FnDef(def_id, substs) = *const_ty.kind() { Some((def_id, substs)) } else { None }
|
|
|
|
}
|
2017-03-14 01:08:21 +02:00
|
|
|
}
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2016-02-03 13:25:07 +01:00
|
|
|
/// Rvalues
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2020-10-04 15:52:14 +02:00
|
|
|
#[derive(Clone, TyEncodable, TyDecodable, Hash, HashStable, PartialEq)]
|
2022-03-05 20:37:04 -05:00
|
|
|
/// The various kinds of rvalues that can appear in MIR.
|
|
|
|
///
|
2022-03-24 22:29:33 -04:00
|
|
|
/// Not all of these are allowed at every [`MirPhase`] - when this is the case, it's stated below.
|
|
|
|
///
|
2022-03-26 20:46:56 -04:00
|
|
|
/// Computing any rvalue begins by evaluating the places and operands in some order (**Needs
|
|
|
|
/// clarification**: Which order?). These are then used to produce a "value" - the same kind of
|
2022-04-08 15:53:08 -04:00
|
|
|
/// value that an [`Operand`] produces.
|
2015-10-05 12:31:48 -04:00
|
|
|
pub enum Rvalue<'tcx> {
|
2022-03-24 22:29:33 -04:00
|
|
|
/// Yields the operand unchanged
|
2015-10-05 12:31:48 -04:00
|
|
|
Use(Operand<'tcx>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2022-03-26 20:46:56 -04:00
|
|
|
/// Creates an array where each element is the value of the operand.
|
|
|
|
///
|
|
|
|
/// This is the cause of a bug in the case where the repetition count is zero because the value
|
|
|
|
/// is not dropped, see [#74836].
|
2022-03-24 22:29:33 -04:00
|
|
|
///
|
|
|
|
/// Corresponds to source code like `[x; 32]`.
|
|
|
|
///
|
|
|
|
/// [#74836]: https://github.com/rust-lang/rust/issues/74836
|
2022-02-02 14:24:45 +11:00
|
|
|
Repeat(Operand<'tcx>, ty::Const<'tcx>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2022-03-24 22:29:33 -04:00
|
|
|
/// Creates a reference of the indicated kind to the place.
|
|
|
|
///
|
|
|
|
/// There is not much to document here, because besides the obvious parts the semantics of this
|
|
|
|
/// are essentially entirely a part of the aliasing model. There are many UCG issues discussing
|
|
|
|
/// exactly what the behavior of this operation should be.
|
|
|
|
///
|
|
|
|
/// `Shallow` borrows are disallowed after drop lowering.
|
2017-12-06 09:25:29 +01:00
|
|
|
Ref(Region<'tcx>, BorrowKind, Place<'tcx>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2022-04-08 15:53:08 -04:00
|
|
|
/// Creates a pointer/reference to the given thread local.
|
2022-03-24 22:29:33 -04:00
|
|
|
///
|
|
|
|
/// The yielded type is a `*mut T` if the static is mutable, otherwise if the static is extern a
|
|
|
|
/// `*const T`, and if neither of those apply a `&T`.
|
|
|
|
///
|
|
|
|
/// **Note:** This is a runtime operation that actually executes code and is in this sense more
|
2022-04-08 15:53:08 -04:00
|
|
|
/// like a function call. Also, eliminating dead stores of this rvalue causes `fn main() {}` to
|
|
|
|
/// SIGILL for some reason that I (JakobDegen) never got a chance to look into.
|
2022-03-24 22:29:33 -04:00
|
|
|
///
|
|
|
|
/// **Needs clarification**: Are there weird additional semantics here related to the runtime
|
|
|
|
/// nature of this operation?
|
2020-05-02 21:44:25 +02:00
|
|
|
ThreadLocalRef(DefId),
|
|
|
|
|
2022-03-24 22:29:33 -04:00
|
|
|
/// Creates a pointer with the indicated mutability to the place.
|
|
|
|
///
|
|
|
|
/// This is generated by pointer casts like `&v as *const _` or raw address of expressions like
|
|
|
|
/// `&raw v` or `addr_of!(v)`.
|
|
|
|
///
|
|
|
|
/// Like with references, the semantics of this operation are heavily dependent on the aliasing
|
|
|
|
/// model.
|
2018-12-23 19:00:58 +00:00
|
|
|
AddressOf(Mutability, Place<'tcx>),
|
|
|
|
|
2022-03-24 22:29:33 -04:00
|
|
|
/// Yields the length of the place, as a `usize`.
|
|
|
|
///
|
2022-04-08 15:53:08 -04:00
|
|
|
/// If the type of the place is an array, this is the array length. For slices (`[T]`, not
|
|
|
|
/// `&[T]`) this accesses the place's metadata to determine the length. This rvalue is
|
|
|
|
/// ill-formed for places of other types.
|
2017-12-06 09:25:29 +01:00
|
|
|
Len(Place<'tcx>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2022-03-24 22:29:33 -04:00
|
|
|
/// Performs essentially all of the casts that can be performed via `as`.
|
|
|
|
///
|
|
|
|
/// This allows for casts from/to a variety of types.
|
|
|
|
///
|
|
|
|
/// **FIXME**: Document exactly which `CastKind`s allow which types of casts. Figure out why
|
|
|
|
/// `ArrayToPointer` and `MutToConstPointer` are special.
|
2015-10-05 12:31:48 -04:00
|
|
|
Cast(CastKind, Operand<'tcx>, Ty<'tcx>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2022-03-24 22:29:33 -04:00
|
|
|
/// * `Offset` has the same semantics as [`offset`](pointer::offset), except that the second
|
2022-03-26 20:46:56 -04:00
|
|
|
/// parameter may be a `usize` as well.
|
2022-03-24 22:29:33 -04:00
|
|
|
/// * The comparison operations accept `bool`s, `char`s, signed or unsigned integers, floats,
|
2022-04-13 05:42:28 -04:00
|
|
|
/// raw pointers, or function pointers and return a `bool`. The types of the operands must be
|
|
|
|
/// matching, up to the usual caveat of the lifetimes in function pointers.
|
2022-03-24 22:29:33 -04:00
|
|
|
/// * Left and right shift operations accept signed or unsigned integers not necessarily of the
|
2022-04-08 15:53:08 -04:00
|
|
|
/// same type and return a value of the same type as their LHS. Like in Rust, the RHS is
|
|
|
|
/// truncated as needed.
|
|
|
|
/// * The `Bit*` operations accept signed integers, unsigned integers, or bools with matching
|
|
|
|
/// types and return a value of that type.
|
|
|
|
/// * The remaining operations accept signed integers, unsigned integers, or floats with
|
|
|
|
/// matching types and return a value of that type.
|
2021-03-05 09:32:47 +00:00
|
|
|
BinaryOp(BinOp, Box<(Operand<'tcx>, Operand<'tcx>)>),
|
2022-03-24 22:29:33 -04:00
|
|
|
|
|
|
|
/// Same as `BinaryOp`, but yields `(T, bool)` instead of `T`. In addition to performing the
|
|
|
|
/// same computation as the matching `BinaryOp`, checks if the infinite precison result would be
|
2022-03-26 20:46:56 -04:00
|
|
|
/// unequal to the actual result and sets the `bool` if this is the case.
|
2022-03-24 22:29:33 -04:00
|
|
|
///
|
2022-04-08 15:53:08 -04:00
|
|
|
/// This only supports addition, subtraction, multiplication, and shift operations on integers.
|
2021-03-05 09:32:47 +00:00
|
|
|
CheckedBinaryOp(BinOp, Box<(Operand<'tcx>, Operand<'tcx>)>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2022-03-26 20:46:56 -04:00
|
|
|
/// Computes a value as described by the operation.
|
2017-05-18 18:43:52 +03:00
|
|
|
NullaryOp(NullOp, Ty<'tcx>),
|
2022-03-24 22:29:33 -04:00
|
|
|
|
|
|
|
/// Exactly like `BinaryOp`, but less operands.
|
|
|
|
///
|
2022-03-26 20:46:56 -04:00
|
|
|
/// Also does two's-complement arithmetic. Negation requires a signed integer or a float;
|
|
|
|
/// bitwise not requires a signed integer, unsigned integer, or bool. Both operation kinds
|
|
|
|
/// return a value with the same type as their operand.
|
2015-10-05 12:31:48 -04:00
|
|
|
UnaryOp(UnOp, Operand<'tcx>),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2022-03-24 22:29:33 -04:00
|
|
|
/// Computes the discriminant of the place, returning it as an integer of type
|
2022-05-01 00:00:00 +00:00
|
|
|
/// [`discriminant_ty`]. Returns zero for types without discriminant.
|
2022-03-24 22:29:33 -04:00
|
|
|
///
|
|
|
|
/// The validity requirements for the underlying value are undecided for this rvalue, see
|
|
|
|
/// [#91095]. Note too that the value of the discriminant is not the same thing as the
|
|
|
|
/// variant index; use [`discriminant_for_variant`] to convert.
|
|
|
|
///
|
|
|
|
/// [`discriminant_ty`]: crate::ty::Ty::discriminant_ty
|
|
|
|
/// [#91095]: https://github.com/rust-lang/rust/issues/91095
|
|
|
|
/// [`discriminant_for_variant`]: crate::ty::Ty::discriminant_for_variant
|
2017-12-06 09:25:29 +01:00
|
|
|
Discriminant(Place<'tcx>),
|
2017-01-31 01:10:54 +02:00
|
|
|
|
2022-03-24 22:29:33 -04:00
|
|
|
/// Creates an aggregate value, like a tuple or struct.
|
|
|
|
///
|
|
|
|
/// This is needed because dataflow analysis needs to distinguish
|
|
|
|
/// `dest = Foo { x: ..., y: ... }` from `dest.x = ...; dest.y = ...;` in the case that `Foo`
|
|
|
|
/// has a destructor.
|
|
|
|
///
|
|
|
|
/// Disallowed after deaggregation for all aggregate kinds except `Array` and `Generator`. After
|
|
|
|
/// generator lowering, `Generator` aggregate kinds are disallowed too.
|
2017-05-12 01:38:26 +03:00
|
|
|
Aggregate(Box<AggregateKind<'tcx>>, Vec<Operand<'tcx>>),
|
2021-09-06 18:33:23 +01:00
|
|
|
|
|
|
|
/// Transmutes a `*mut u8` into shallow-initialized `Box<T>`.
|
|
|
|
///
|
2022-04-08 15:53:08 -04:00
|
|
|
/// This is different from a normal transmute because dataflow analysis will treat the box as
|
2022-03-24 22:29:33 -04:00
|
|
|
/// initialized but its content as uninitialized. Like other pointer casts, this in general
|
|
|
|
/// affects alias analysis.
|
2021-09-06 18:33:23 +01:00
|
|
|
ShallowInitBox(Operand<'tcx>, Ty<'tcx>),
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2021-09-05 16:42:36 +01:00
|
|
|
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
|
2021-03-05 09:32:47 +00:00
|
|
|
static_assert_size!(Rvalue<'_>, 40);
|
2021-03-03 16:25:37 +00:00
|
|
|
|
2020-10-04 15:52:14 +02:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
|
2015-08-18 17:59:21 -04:00
|
|
|
pub enum CastKind {
|
|
|
|
Misc,
|
2019-04-15 19:20:44 +05:30
|
|
|
Pointer(PointerCast),
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2020-10-04 15:52:14 +02:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
|
2015-10-05 12:31:48 -04:00
|
|
|
pub enum AggregateKind<'tcx> {
|
2017-02-26 00:32:14 +02:00
|
|
|
/// The type is of the element
|
|
|
|
Array(Ty<'tcx>),
|
2015-08-18 17:59:21 -04:00
|
|
|
Tuple,
|
2017-12-06 09:25:29 +01:00
|
|
|
|
2018-01-29 14:11:32 +05:30
|
|
|
/// The second field is the variant index. It's equal to 0 for struct
|
|
|
|
/// and union expressions. The fourth field is
|
2017-12-06 09:25:29 +01:00
|
|
|
/// active field number and is present only for union expressions
|
2018-11-27 02:59:49 +00:00
|
|
|
/// -- e.g., for a union expression `SomeUnion { c: .. }`, the
|
2017-12-06 09:25:29 +01:00
|
|
|
/// active field index would identity the field `c`
|
2021-12-22 14:35:17 -05:00
|
|
|
Adt(DefId, VariantIdx, SubstsRef<'tcx>, Option<UserTypeAnnotationIndex>, Option<usize>),
|
2017-12-06 09:25:29 +01:00
|
|
|
|
2019-09-26 17:30:44 +00:00
|
|
|
Closure(DefId, SubstsRef<'tcx>),
|
2019-11-09 18:06:57 +01:00
|
|
|
Generator(DefId, SubstsRef<'tcx>, hir::Movability),
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2021-09-05 16:42:36 +01:00
|
|
|
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
|
2021-03-03 16:25:37 +00:00
|
|
|
static_assert_size!(AggregateKind<'_>, 48);
|
|
|
|
|
2020-10-04 15:52:14 +02:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
|
2015-08-18 17:59:21 -04:00
|
|
|
pub enum BinOp {
|
|
|
|
/// The `+` operator (addition)
|
|
|
|
Add,
|
|
|
|
/// The `-` operator (subtraction)
|
|
|
|
Sub,
|
|
|
|
/// The `*` operator (multiplication)
|
|
|
|
Mul,
|
|
|
|
/// The `/` operator (division)
|
2021-11-23 20:30:26 -05:00
|
|
|
///
|
2022-02-15 22:22:37 +11:00
|
|
|
/// Division by zero is UB, because the compiler should have inserted checks
|
|
|
|
/// prior to this.
|
2015-08-18 17:59:21 -04:00
|
|
|
Div,
|
|
|
|
/// The `%` operator (modulus)
|
2021-11-23 20:30:26 -05:00
|
|
|
///
|
2022-02-15 22:22:37 +11:00
|
|
|
/// Using zero as the modulus (second operand) is UB, because the compiler
|
|
|
|
/// should have inserted checks prior to this.
|
2015-08-18 17:59:21 -04:00
|
|
|
Rem,
|
|
|
|
/// The `^` operator (bitwise xor)
|
|
|
|
BitXor,
|
|
|
|
/// The `&` operator (bitwise and)
|
|
|
|
BitAnd,
|
|
|
|
/// The `|` operator (bitwise or)
|
|
|
|
BitOr,
|
|
|
|
/// The `<<` operator (shift left)
|
2021-11-23 20:30:26 -05:00
|
|
|
///
|
|
|
|
/// The offset is truncated to the size of the first operand before shifting.
|
2015-08-18 17:59:21 -04:00
|
|
|
Shl,
|
|
|
|
/// The `>>` operator (shift right)
|
2021-11-23 20:30:26 -05:00
|
|
|
///
|
|
|
|
/// The offset is truncated to the size of the first operand before shifting.
|
2015-08-18 17:59:21 -04:00
|
|
|
Shr,
|
|
|
|
/// The `==` operator (equality)
|
|
|
|
Eq,
|
|
|
|
/// The `<` operator (less than)
|
|
|
|
Lt,
|
|
|
|
/// The `<=` operator (less than or equal to)
|
|
|
|
Le,
|
|
|
|
/// The `!=` operator (not equal to)
|
|
|
|
Ne,
|
|
|
|
/// The `>=` operator (greater than or equal to)
|
|
|
|
Ge,
|
|
|
|
/// The `>` operator (greater than)
|
|
|
|
Gt,
|
2017-05-18 18:43:52 +03:00
|
|
|
/// The `ptr.offset` operator
|
|
|
|
Offset,
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2016-03-31 18:50:07 +13:00
|
|
|
impl BinOp {
|
|
|
|
pub fn is_checkable(self) -> bool {
|
|
|
|
use self::BinOp::*;
|
2020-09-21 04:53:44 +02:00
|
|
|
matches!(self, Add | Sub | Mul | Shl | Shr)
|
2016-03-31 18:50:07 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-04 15:52:14 +02:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
|
2017-05-18 18:43:52 +03:00
|
|
|
pub enum NullOp {
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Returns the size of a value of that type
|
2017-05-18 18:43:52 +03:00
|
|
|
SizeOf,
|
2021-09-07 16:06:07 +01:00
|
|
|
/// Returns the minimum alignment of a type
|
|
|
|
AlignOf,
|
2017-05-18 18:43:52 +03:00
|
|
|
}
|
|
|
|
|
2020-10-04 15:52:14 +02:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
|
2015-08-18 17:59:21 -04:00
|
|
|
pub enum UnOp {
|
|
|
|
/// The `!` operator for logical inversion
|
|
|
|
Not,
|
|
|
|
/// The `-` operator for negation
|
2015-10-07 14:37:42 +02:00
|
|
|
Neg,
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2015-10-05 12:31:48 -04:00
|
|
|
impl<'tcx> Debug for Rvalue<'tcx> {
|
2018-08-29 22:02:42 -07:00
|
|
|
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
|
2015-08-18 17:59:21 -04:00
|
|
|
use self::Rvalue::*;
|
|
|
|
|
|
|
|
match *self {
|
2017-12-06 09:25:29 +01:00
|
|
|
Use(ref place) => write!(fmt, "{:?}", place),
|
2022-02-02 14:24:45 +11:00
|
|
|
Repeat(ref a, b) => {
|
2020-03-14 15:30:35 +01:00
|
|
|
write!(fmt, "[{:?}; ", a)?;
|
|
|
|
pretty_print_const(b, fmt, false)?;
|
|
|
|
write!(fmt, "]")
|
|
|
|
}
|
2015-12-18 19:29:03 -06:00
|
|
|
Len(ref a) => write!(fmt, "Len({:?})", a),
|
2017-12-06 09:25:29 +01:00
|
|
|
Cast(ref kind, ref place, ref ty) => {
|
|
|
|
write!(fmt, "{:?} as {:?} ({:?})", place, ty, kind)
|
|
|
|
}
|
2021-03-05 09:32:47 +00:00
|
|
|
BinaryOp(ref op, box (ref a, ref b)) => write!(fmt, "{:?}({:?}, {:?})", op, a, b),
|
|
|
|
CheckedBinaryOp(ref op, box (ref a, ref b)) => {
|
2016-03-31 18:50:07 +13:00
|
|
|
write!(fmt, "Checked{:?}({:?}, {:?})", op, a, b)
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
UnaryOp(ref op, ref a) => write!(fmt, "{:?}({:?})", op, a),
|
2017-12-06 09:25:29 +01:00
|
|
|
Discriminant(ref place) => write!(fmt, "discriminant({:?})", place),
|
2017-05-18 18:43:52 +03:00
|
|
|
NullaryOp(ref op, ref t) => write!(fmt, "{:?}({:?})", op, t),
|
2020-05-02 21:44:25 +02:00
|
|
|
ThreadLocalRef(did) => ty::tls::with(|tcx| {
|
|
|
|
let muta = tcx.static_mutability(did).unwrap().prefix_str();
|
|
|
|
write!(fmt, "&/*tls*/ {}{}", muta, tcx.def_path_str(did))
|
|
|
|
}),
|
2017-12-06 09:25:29 +01:00
|
|
|
Ref(region, borrow_kind, ref place) => {
|
2016-01-01 00:39:02 -06:00
|
|
|
let kind_str = match borrow_kind {
|
|
|
|
BorrowKind::Shared => "",
|
2018-09-10 22:33:45 +01:00
|
|
|
BorrowKind::Shallow => "shallow ",
|
2018-01-15 12:47:26 +01:00
|
|
|
BorrowKind::Mut { .. } | BorrowKind::Unique => "mut ",
|
2016-01-01 00:39:02 -06:00
|
|
|
};
|
2017-03-23 11:08:08 +01:00
|
|
|
|
2017-07-24 13:43:05 -07:00
|
|
|
// When printing regions, add trailing space if necessary.
|
2019-01-18 21:45:13 +02:00
|
|
|
let print_region = ty::tls::with(|tcx| {
|
|
|
|
tcx.sess.verbose() || tcx.sess.opts.debugging_opts.identify_regions
|
|
|
|
});
|
|
|
|
let region = if print_region {
|
|
|
|
let mut region = region.to_string();
|
2020-02-28 14:20:33 +01:00
|
|
|
if !region.is_empty() {
|
2019-01-18 21:45:13 +02:00
|
|
|
region.push(' ');
|
|
|
|
}
|
|
|
|
region
|
|
|
|
} else {
|
|
|
|
// Do not even print 'static
|
|
|
|
String::new()
|
|
|
|
};
|
|
|
|
write!(fmt, "&{}{}{:?}", region, kind_str, place)
|
2016-01-01 00:39:02 -06:00
|
|
|
}
|
|
|
|
|
2018-12-23 19:00:58 +00:00
|
|
|
AddressOf(mutability, ref place) => {
|
|
|
|
let kind_str = match mutability {
|
|
|
|
Mutability::Mut => "mut",
|
|
|
|
Mutability::Not => "const",
|
|
|
|
};
|
|
|
|
|
|
|
|
write!(fmt, "&raw {} {:?}", kind_str, place)
|
|
|
|
}
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
Aggregate(ref kind, ref places) => {
|
2020-04-17 03:54:58 +03:00
|
|
|
let fmt_tuple = |fmt: &mut Formatter<'_>, name: &str| {
|
|
|
|
let mut tuple_fmt = fmt.debug_tuple(name);
|
2017-12-06 09:25:29 +01:00
|
|
|
for place in places {
|
|
|
|
tuple_fmt.field(place);
|
2015-12-31 21:38:44 -06:00
|
|
|
}
|
|
|
|
tuple_fmt.finish()
|
2020-04-17 03:54:58 +03:00
|
|
|
};
|
2015-12-31 21:38:44 -06:00
|
|
|
|
2017-05-12 01:38:26 +03:00
|
|
|
match **kind {
|
2017-12-06 09:25:29 +01:00
|
|
|
AggregateKind::Array(_) => write!(fmt, "{:?}", places),
|
2015-12-31 21:38:44 -06:00
|
|
|
|
2020-04-17 03:54:58 +03:00
|
|
|
AggregateKind::Tuple => {
|
|
|
|
if places.is_empty() {
|
|
|
|
write!(fmt, "()")
|
|
|
|
} else {
|
|
|
|
fmt_tuple(fmt, "")
|
|
|
|
}
|
|
|
|
}
|
2015-12-31 21:38:44 -06:00
|
|
|
|
2021-12-22 14:35:17 -05:00
|
|
|
AggregateKind::Adt(adt_did, variant, substs, _user_ty, _) => {
|
|
|
|
ty::tls::with(|tcx| {
|
2022-03-05 07:28:41 +11:00
|
|
|
let variant_def = &tcx.adt_def(adt_did).variant(variant);
|
2020-10-16 21:59:49 +02:00
|
|
|
let substs = tcx.lift(substs).expect("could not lift for printing");
|
2022-02-18 16:15:29 -05:00
|
|
|
let name = FmtPrinter::new(tcx, Namespace::ValueNS)
|
|
|
|
.print_def_path(variant_def.def_id, substs)?
|
|
|
|
.into_buffer();
|
2021-12-22 14:35:17 -05:00
|
|
|
|
|
|
|
match variant_def.ctor_kind {
|
|
|
|
CtorKind::Const => fmt.write_str(&name),
|
|
|
|
CtorKind::Fn => fmt_tuple(fmt, &name),
|
|
|
|
CtorKind::Fictive => {
|
|
|
|
let mut struct_fmt = fmt.debug_struct(&name);
|
|
|
|
for (field, place) in iter::zip(&variant_def.fields, places) {
|
2022-01-02 22:37:05 -05:00
|
|
|
struct_fmt.field(field.name.as_str(), place);
|
2021-12-22 14:35:17 -05:00
|
|
|
}
|
|
|
|
struct_fmt.finish()
|
2015-12-31 21:38:44 -06:00
|
|
|
}
|
|
|
|
}
|
2021-12-22 14:35:17 -05:00
|
|
|
})
|
2015-12-31 21:38:44 -06:00
|
|
|
}
|
|
|
|
|
2019-11-28 22:32:43 +02:00
|
|
|
AggregateKind::Closure(def_id, substs) => ty::tls::with(|tcx| {
|
2020-04-23 20:48:40 +01:00
|
|
|
if let Some(def_id) = def_id.as_local() {
|
2017-03-16 14:41:48 +01:00
|
|
|
let name = if tcx.sess.opts.debugging_opts.span_free_formats {
|
2020-10-16 21:59:49 +02:00
|
|
|
let substs = tcx.lift(substs).unwrap();
|
2019-11-28 22:32:43 +02:00
|
|
|
format!(
|
|
|
|
"[closure@{}]",
|
2020-04-23 20:48:40 +01:00
|
|
|
tcx.def_path_str_with_substs(def_id.to_def_id(), substs),
|
2019-11-28 22:32:43 +02:00
|
|
|
)
|
2017-03-16 14:41:48 +01:00
|
|
|
} else {
|
2021-10-20 20:59:15 +02:00
|
|
|
let span = tcx.def_span(def_id);
|
2021-05-03 01:14:25 +01:00
|
|
|
format!(
|
|
|
|
"[closure@{}]",
|
|
|
|
tcx.sess.source_map().span_to_diagnostic_string(span)
|
|
|
|
)
|
2017-03-16 14:41:48 +01:00
|
|
|
};
|
2015-12-31 21:38:44 -06:00
|
|
|
let mut struct_fmt = fmt.debug_struct(&name);
|
|
|
|
|
2021-04-28 21:53:59 -04:00
|
|
|
// FIXME(project-rfc-2229#48): This should be a list of capture names/places
|
2020-05-23 19:29:49 -04:00
|
|
|
if let Some(upvars) = tcx.upvars_mentioned(def_id) {
|
2021-03-08 15:32:41 -08:00
|
|
|
for (&var_id, place) in iter::zip(upvars.keys(), places) {
|
2019-06-19 15:44:51 +02:00
|
|
|
let var_name = tcx.hir().name(var_id);
|
2021-12-15 14:39:23 +11:00
|
|
|
struct_fmt.field(var_name.as_str(), place);
|
2015-12-31 21:38:44 -06:00
|
|
|
}
|
2019-05-04 03:47:16 +03:00
|
|
|
}
|
2015-12-31 21:38:44 -06:00
|
|
|
|
|
|
|
struct_fmt.finish()
|
|
|
|
} else {
|
|
|
|
write!(fmt, "[closure]")
|
|
|
|
}
|
|
|
|
}),
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2018-05-02 13:14:30 +02:00
|
|
|
AggregateKind::Generator(def_id, _, _) => ty::tls::with(|tcx| {
|
2020-04-23 20:48:40 +01:00
|
|
|
if let Some(def_id) = def_id.as_local() {
|
2021-10-20 20:59:15 +02:00
|
|
|
let name = format!("[generator@{:?}]", tcx.def_span(def_id));
|
2016-12-26 14:34:03 +01:00
|
|
|
let mut struct_fmt = fmt.debug_struct(&name);
|
|
|
|
|
2021-04-28 21:53:59 -04:00
|
|
|
// FIXME(project-rfc-2229#48): This should be a list of capture names/places
|
2020-05-23 19:29:49 -04:00
|
|
|
if let Some(upvars) = tcx.upvars_mentioned(def_id) {
|
2021-03-08 15:32:41 -08:00
|
|
|
for (&var_id, place) in iter::zip(upvars.keys(), places) {
|
2019-06-19 15:44:51 +02:00
|
|
|
let var_name = tcx.hir().name(var_id);
|
2021-12-15 14:39:23 +11:00
|
|
|
struct_fmt.field(var_name.as_str(), place);
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
2019-05-04 03:47:16 +03:00
|
|
|
}
|
2016-12-26 14:34:03 +01:00
|
|
|
|
|
|
|
struct_fmt.finish()
|
|
|
|
} else {
|
|
|
|
write!(fmt, "[generator]")
|
|
|
|
}
|
|
|
|
}),
|
2015-12-31 21:38:44 -06:00
|
|
|
}
|
|
|
|
}
|
2021-09-06 18:33:23 +01:00
|
|
|
|
|
|
|
ShallowInitBox(ref place, ref ty) => {
|
|
|
|
write!(fmt, "ShallowInitBox({:?}, {:?})", place, ty)
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2016-02-03 13:25:07 +01:00
|
|
|
/// Constants
|
|
|
|
///
|
|
|
|
/// Two constants are equal if they are the same constant. Note that
|
2020-09-17 09:07:19 +08:00
|
|
|
/// this does not necessarily mean that they are `==` in Rust. In
|
2020-09-14 10:39:07 -07:00
|
|
|
/// particular, one must be wary of `NaN`!
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2022-01-18 10:05:41 -06:00
|
|
|
#[derive(Clone, Copy, PartialEq, TyEncodable, TyDecodable, Hash, HashStable)]
|
2015-10-05 12:31:48 -04:00
|
|
|
pub struct Constant<'tcx> {
|
|
|
|
pub span: Span,
|
2018-08-09 06:18:00 -04:00
|
|
|
|
|
|
|
/// Optional user-given type: for something like
|
|
|
|
/// `collect::<Vec<_>>`, this would be present and would
|
|
|
|
/// indicate that `Vec<_>` was explicitly specified.
|
|
|
|
///
|
|
|
|
/// Needed for NLL to impose user-given type constraints.
|
2018-11-16 22:56:18 +01:00
|
|
|
pub user_ty: Option<UserTypeAnnotationIndex>,
|
2018-08-09 06:18:00 -04:00
|
|
|
|
2021-03-15 11:23:44 +00:00
|
|
|
pub literal: ConstantKind<'tcx>,
|
2021-03-08 16:18:03 +00:00
|
|
|
}
|
|
|
|
|
2022-01-18 10:05:41 -06:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable, Debug)]
|
2021-03-30 14:26:40 +00:00
|
|
|
#[derive(Lift)]
|
2021-03-15 11:23:44 +00:00
|
|
|
pub enum ConstantKind<'tcx> {
|
2021-03-08 16:18:03 +00:00
|
|
|
/// This constant came from the type system
|
2022-02-02 14:24:45 +11:00
|
|
|
Ty(ty::Const<'tcx>),
|
2021-03-08 16:18:03 +00:00
|
|
|
/// This constant cannot go back into the type system, as it represents
|
|
|
|
/// something the type system cannot handle (e.g. pointers).
|
|
|
|
Val(interpret::ConstValue<'tcx>, Ty<'tcx>),
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2021-12-15 19:32:30 -05:00
|
|
|
impl<'tcx> Constant<'tcx> {
|
2019-11-11 12:15:38 +01:00
|
|
|
pub fn check_static_ptr(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
|
2022-03-07 13:51:59 +01:00
|
|
|
match self.literal.try_to_scalar() {
|
2021-07-12 20:29:05 +02:00
|
|
|
Some(Scalar::Ptr(ptr, _size)) => match tcx.global_alloc(ptr.provenance) {
|
2020-05-02 21:44:25 +02:00
|
|
|
GlobalAlloc::Static(def_id) => {
|
|
|
|
assert!(!tcx.is_thread_local_static(def_id));
|
|
|
|
Some(def_id)
|
|
|
|
}
|
2020-05-08 10:58:53 +02:00
|
|
|
_ => None,
|
2019-11-11 12:15:38 +01:00
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2021-06-01 00:00:00 +00:00
|
|
|
#[inline]
|
2021-03-08 14:14:11 +00:00
|
|
|
pub fn ty(&self) -> Ty<'tcx> {
|
2021-03-08 16:18:03 +00:00
|
|
|
self.literal.ty()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-02 14:24:45 +11:00
|
|
|
impl<'tcx> From<ty::Const<'tcx>> for ConstantKind<'tcx> {
|
2021-06-01 00:00:00 +00:00
|
|
|
#[inline]
|
2022-02-02 14:24:45 +11:00
|
|
|
fn from(ct: ty::Const<'tcx>) -> Self {
|
2022-02-16 10:54:36 +01:00
|
|
|
match ct.val() {
|
|
|
|
ty::ConstKind::Value(cv) => {
|
|
|
|
// FIXME Once valtrees are introduced we need to convert those
|
|
|
|
// into `ConstValue` instances here
|
|
|
|
Self::Val(cv, ct.ty())
|
|
|
|
}
|
|
|
|
_ => Self::Ty(ct),
|
|
|
|
}
|
2021-03-08 16:18:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-15 19:32:30 -05:00
|
|
|
impl<'tcx> ConstantKind<'tcx> {
|
2021-03-08 16:18:03 +00:00
|
|
|
/// Returns `None` if the constant is not trivially safe for use in the type system.
|
2022-02-02 14:24:45 +11:00
|
|
|
pub fn const_for_ty(&self) -> Option<ty::Const<'tcx>> {
|
2021-03-08 16:18:03 +00:00
|
|
|
match self {
|
2022-02-02 14:24:45 +11:00
|
|
|
ConstantKind::Ty(c) => Some(*c),
|
2021-03-15 11:23:44 +00:00
|
|
|
ConstantKind::Val(..) => None,
|
2021-03-08 16:18:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ty(&self) -> Ty<'tcx> {
|
|
|
|
match self {
|
2022-02-02 14:24:45 +11:00
|
|
|
ConstantKind::Ty(c) => c.ty(),
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 14:13:38 +11:00
|
|
|
ConstantKind::Val(_, ty) => *ty,
|
2021-03-08 16:18:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 22:43:15 +01:00
|
|
|
pub fn try_val(&self) -> Option<ConstValue<'tcx>> {
|
|
|
|
match self {
|
|
|
|
ConstantKind::Ty(c) => match c.val() {
|
|
|
|
ty::ConstKind::Value(v) => Some(v),
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
ConstantKind::Val(v, _) => Some(*v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 16:18:03 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn try_to_value(self) -> Option<interpret::ConstValue<'tcx>> {
|
|
|
|
match self {
|
2022-02-02 14:24:45 +11:00
|
|
|
ConstantKind::Ty(c) => c.val().try_to_value(),
|
2021-03-15 11:23:44 +00:00
|
|
|
ConstantKind::Val(val, _) => Some(val),
|
2021-03-08 16:18:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn try_to_scalar(self) -> Option<Scalar> {
|
|
|
|
self.try_to_value()?.try_to_scalar()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn try_to_scalar_int(self) -> Option<ScalarInt> {
|
2021-03-15 12:06:07 +00:00
|
|
|
Some(self.try_to_value()?.try_to_scalar()?.assert_int())
|
2021-03-08 16:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn try_to_bits(self, size: Size) -> Option<u128> {
|
|
|
|
self.try_to_scalar_int()?.to_bits(size).ok()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn try_to_bool(self) -> Option<bool> {
|
|
|
|
self.try_to_scalar_int()?.try_into().ok()
|
|
|
|
}
|
|
|
|
|
2022-02-21 22:43:15 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn eval(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Self {
|
|
|
|
match self {
|
|
|
|
Self::Ty(c) => {
|
|
|
|
// FIXME Need to use a different evaluation function that directly returns a `ConstValue`
|
|
|
|
// if evaluation succeeds and does not create a ValTree first
|
|
|
|
if let Some(val) = c.val().try_eval(tcx, param_env) {
|
|
|
|
match val {
|
|
|
|
Ok(val) => Self::Val(val, c.ty()),
|
2022-03-25 10:06:10 +01:00
|
|
|
Err(_) => Self::Ty(tcx.const_error(self.ty())),
|
2022-02-21 22:43:15 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Self::Val(_, _) => self,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Panics if the value cannot be evaluated or doesn't contain a valid integer of the given type.
|
2022-04-02 12:17:07 +02:00
|
|
|
#[inline]
|
2022-02-21 22:43:15 +01:00
|
|
|
pub fn eval_bits(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, ty: Ty<'tcx>) -> u128 {
|
|
|
|
self.try_eval_bits(tcx, param_env, ty)
|
|
|
|
.unwrap_or_else(|| bug!("expected bits of {:#?}, got {:#?}", ty, self))
|
|
|
|
}
|
|
|
|
|
2021-03-08 16:18:03 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn try_eval_bits(
|
|
|
|
&self,
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
) -> Option<u128> {
|
|
|
|
match self {
|
|
|
|
Self::Ty(ct) => ct.try_eval_bits(tcx, param_env, ty),
|
|
|
|
Self::Val(val, t) => {
|
|
|
|
assert_eq!(*t, ty);
|
|
|
|
let size =
|
|
|
|
tcx.layout_of(param_env.with_reveal_all_normalized(tcx).and(ty)).ok()?.size;
|
|
|
|
val.try_to_bits(size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn try_eval_bool(&self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Option<bool> {
|
|
|
|
match self {
|
|
|
|
Self::Ty(ct) => ct.try_eval_bool(tcx, param_env),
|
|
|
|
Self::Val(val, _) => val.try_to_bool(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn try_eval_usize(&self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Option<u64> {
|
|
|
|
match self {
|
|
|
|
Self::Ty(ct) => ct.try_eval_usize(tcx, param_env),
|
|
|
|
Self::Val(val, _) => val.try_to_machine_usize(tcx),
|
|
|
|
}
|
2021-03-08 14:14:11 +00:00
|
|
|
}
|
2022-02-16 10:54:36 +01:00
|
|
|
|
2022-02-21 22:43:15 +01:00
|
|
|
pub fn from_bits(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
bits: u128,
|
|
|
|
param_env_ty: ty::ParamEnvAnd<'tcx, Ty<'tcx>>,
|
|
|
|
) -> Self {
|
|
|
|
let size = tcx
|
|
|
|
.layout_of(param_env_ty)
|
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
bug!("could not compute layout for {:?}: {:?}", param_env_ty.value, e)
|
|
|
|
})
|
|
|
|
.size;
|
|
|
|
let cv = ConstValue::Scalar(Scalar::from_uint(bits, size));
|
|
|
|
|
|
|
|
Self::Val(cv, param_env_ty.value)
|
|
|
|
}
|
|
|
|
|
2022-02-16 10:54:36 +01:00
|
|
|
pub fn from_bool(tcx: TyCtxt<'tcx>, v: bool) -> Self {
|
|
|
|
let cv = ConstValue::from_bool(v);
|
|
|
|
Self::Val(cv, tcx.types.bool)
|
|
|
|
}
|
|
|
|
|
2022-02-21 22:43:15 +01:00
|
|
|
pub fn zero_sized(ty: Ty<'tcx>) -> Self {
|
2022-02-16 10:54:36 +01:00
|
|
|
let cv = ConstValue::Scalar(Scalar::ZST);
|
|
|
|
Self::Val(cv, ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_usize(tcx: TyCtxt<'tcx>, n: u64) -> Self {
|
|
|
|
let ty = tcx.types.usize;
|
2022-02-21 22:43:15 +01:00
|
|
|
Self::from_bits(tcx, n as u128, ty::ParamEnv::empty().and(ty))
|
|
|
|
}
|
2022-02-16 10:54:36 +01:00
|
|
|
|
2022-02-21 22:43:15 +01:00
|
|
|
/// Literals are converted to `ConstantKindVal`, const generic parameters are eagerly
|
|
|
|
/// converted to a constant, everything else becomes `Unevaluated`.
|
2022-04-02 12:09:22 +02:00
|
|
|
pub fn from_anon_const(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
def_id: LocalDefId,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
) -> Self {
|
|
|
|
Self::from_opt_const_arg_anon_const(tcx, ty::WithOptConstParam::unknown(def_id), param_env)
|
2022-02-21 22:43:15 +01:00
|
|
|
}
|
|
|
|
|
2022-04-12 18:14:28 +02:00
|
|
|
#[instrument(skip(tcx), level = "debug")]
|
|
|
|
pub fn from_inline_const(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
|
|
|
|
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
|
|
|
|
let body_id = match tcx.hir().get(hir_id) {
|
|
|
|
hir::Node::AnonConst(ac) => ac.body,
|
|
|
|
_ => span_bug!(
|
|
|
|
tcx.def_span(def_id.to_def_id()),
|
|
|
|
"from_inline_const can only process anonymous constants"
|
|
|
|
),
|
|
|
|
};
|
|
|
|
let expr = &tcx.hir().body(body_id).value;
|
|
|
|
let ty = tcx.typeck(def_id).node_type(hir_id);
|
|
|
|
|
|
|
|
let lit_input = match expr.kind {
|
|
|
|
hir::ExprKind::Lit(ref lit) => Some(LitToConstInput { lit: &lit.node, ty, neg: false }),
|
|
|
|
hir::ExprKind::Unary(hir::UnOp::Neg, ref expr) => match expr.kind {
|
|
|
|
hir::ExprKind::Lit(ref lit) => {
|
|
|
|
Some(LitToConstInput { lit: &lit.node, ty, neg: true })
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
if let Some(lit_input) = lit_input {
|
|
|
|
// If an error occurred, ignore that it's a literal and leave reporting the error up to
|
|
|
|
// mir.
|
|
|
|
match tcx.at(expr.span).lit_to_mir_constant(lit_input) {
|
|
|
|
Ok(c) => return c,
|
|
|
|
Err(_) => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let typeck_root_def_id = tcx.typeck_root_def_id(def_id.to_def_id());
|
|
|
|
let parent_substs =
|
|
|
|
tcx.erase_regions(InternalSubsts::identity_for_item(tcx, typeck_root_def_id));
|
|
|
|
let substs =
|
|
|
|
ty::InlineConstSubsts::new(tcx, ty::InlineConstSubstsParts { parent_substs, ty })
|
|
|
|
.substs;
|
|
|
|
let uneval_const = tcx.mk_const(ty::ConstS {
|
|
|
|
val: ty::ConstKind::Unevaluated(ty::Unevaluated {
|
|
|
|
def: ty::WithOptConstParam::unknown(def_id).to_global(),
|
|
|
|
substs,
|
|
|
|
promoted: None,
|
|
|
|
}),
|
|
|
|
ty,
|
|
|
|
});
|
|
|
|
debug!(?uneval_const);
|
|
|
|
debug_assert!(!uneval_const.has_free_regions());
|
|
|
|
|
|
|
|
Self::Ty(uneval_const)
|
|
|
|
}
|
|
|
|
|
2022-02-21 22:43:15 +01:00
|
|
|
#[instrument(skip(tcx), level = "debug")]
|
|
|
|
fn from_opt_const_arg_anon_const(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
def: ty::WithOptConstParam<LocalDefId>,
|
2022-04-02 12:09:22 +02:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2022-02-21 22:43:15 +01:00
|
|
|
) -> Self {
|
|
|
|
let body_id = match tcx.hir().get_by_def_id(def.did) {
|
|
|
|
hir::Node::AnonConst(ac) => ac.body,
|
|
|
|
_ => span_bug!(
|
|
|
|
tcx.def_span(def.did.to_def_id()),
|
|
|
|
"from_anon_const can only process anonymous constants"
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
|
|
|
let expr = &tcx.hir().body(body_id).value;
|
|
|
|
debug!(?expr);
|
|
|
|
|
2022-04-02 12:09:22 +02:00
|
|
|
// Unwrap a block, so that e.g. `{ P }` is recognised as a parameter. Const arguments
|
|
|
|
// currently have to be wrapped in curly brackets, so it's necessary to special-case.
|
|
|
|
let expr = match &expr.kind {
|
|
|
|
hir::ExprKind::Block(block, _) if block.stmts.is_empty() && block.expr.is_some() => {
|
|
|
|
block.expr.as_ref().unwrap()
|
|
|
|
}
|
|
|
|
_ => expr,
|
|
|
|
};
|
|
|
|
|
2022-02-21 22:43:15 +01:00
|
|
|
let ty = tcx.type_of(def.def_id_for_type_of());
|
|
|
|
|
2022-04-02 12:09:22 +02:00
|
|
|
// FIXME(const_generics): We currently have to special case parameters because `min_const_generics`
|
|
|
|
// does not provide the parents generics to anonymous constants. We still allow generic const
|
|
|
|
// parameters by themselves however, e.g. `N`. These constants would cause an ICE if we were to
|
|
|
|
// ever try to substitute the generic parameters in their bodies.
|
|
|
|
//
|
|
|
|
// While this doesn't happen as these constants are always used as `ty::ConstKind::Param`, it does
|
|
|
|
// cause issues if we were to remove that special-case and try to evaluate the constant instead.
|
|
|
|
use hir::{def::DefKind::ConstParam, def::Res, ExprKind, Path, QPath};
|
|
|
|
match expr.kind {
|
|
|
|
ExprKind::Path(QPath::Resolved(_, &Path { res: Res::Def(ConstParam, def_id), .. })) => {
|
|
|
|
// Find the name and index of the const parameter by indexing the generics of
|
|
|
|
// the parent item and construct a `ParamConst`.
|
|
|
|
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
|
|
|
|
let item_id = tcx.hir().get_parent_node(hir_id);
|
|
|
|
let item_def_id = tcx.hir().local_def_id(item_id);
|
|
|
|
let generics = tcx.generics_of(item_def_id.to_def_id());
|
|
|
|
let index = generics.param_def_id_to_index[&def_id];
|
|
|
|
let name = tcx.hir().name(hir_id);
|
|
|
|
let ty_const = tcx.mk_const(ty::ConstS {
|
|
|
|
val: ty::ConstKind::Param(ty::ParamConst::new(index, name)),
|
|
|
|
ty,
|
|
|
|
});
|
|
|
|
|
|
|
|
return Self::Ty(ty_const);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
let hir_id = tcx.hir().local_def_id_to_hir_id(def.did);
|
|
|
|
let parent_substs = if let Some(parent_hir_id) = tcx.hir().find_parent_node(hir_id) {
|
|
|
|
if let Some(parent_did) = tcx.hir().opt_local_def_id(parent_hir_id) {
|
|
|
|
InternalSubsts::identity_for_item(tcx, parent_did.to_def_id())
|
|
|
|
} else {
|
|
|
|
tcx.mk_substs(Vec::<GenericArg<'tcx>>::new().into_iter())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tcx.mk_substs(Vec::<GenericArg<'tcx>>::new().into_iter())
|
|
|
|
};
|
|
|
|
debug!(?parent_substs);
|
|
|
|
|
|
|
|
let did = def.did.to_def_id();
|
|
|
|
let child_substs = InternalSubsts::identity_for_item(tcx, did);
|
|
|
|
let substs = tcx.mk_substs(parent_substs.into_iter().chain(child_substs.into_iter()));
|
|
|
|
debug!(?substs);
|
|
|
|
|
|
|
|
let hir_id = tcx.hir().local_def_id_to_hir_id(def.did);
|
|
|
|
let span = tcx.hir().span(hir_id);
|
|
|
|
let uneval = ty::Unevaluated::new(def.to_global(), substs);
|
|
|
|
debug!(?span, ?param_env);
|
|
|
|
|
|
|
|
match tcx.const_eval_resolve(param_env, uneval, Some(span)) {
|
|
|
|
Ok(val) => Self::Val(val, ty),
|
|
|
|
Err(_) => {
|
|
|
|
// Error was handled in `const_eval_resolve`. Here we just create a
|
|
|
|
// new unevaluated const and error hard later in codegen
|
2022-02-21 22:43:15 +01:00
|
|
|
let ty_const = tcx.mk_const(ty::ConstS {
|
|
|
|
val: ty::ConstKind::Unevaluated(ty::Unevaluated {
|
|
|
|
def: def.to_global(),
|
|
|
|
substs: InternalSubsts::identity_for_item(tcx, def.did.to_def_id()),
|
|
|
|
promoted: None,
|
|
|
|
}),
|
|
|
|
ty,
|
|
|
|
});
|
2022-04-02 12:09:22 +02:00
|
|
|
|
2022-02-21 22:43:15 +01:00
|
|
|
Self::Ty(ty_const)
|
|
|
|
}
|
|
|
|
}
|
2022-02-16 10:54:36 +01:00
|
|
|
}
|
2019-11-11 12:15:38 +01:00
|
|
|
}
|
|
|
|
|
2018-10-22 14:23:44 +02:00
|
|
|
/// A collection of projections into user types.
|
|
|
|
///
|
|
|
|
/// They are projections because a binding can occur a part of a
|
|
|
|
/// parent pattern that has been ascribed a type.
|
|
|
|
///
|
|
|
|
/// Its a collection because there can be multiple type ascriptions on
|
|
|
|
/// the path from the root of the pattern down to the binding itself.
|
2018-10-22 22:50:10 +02:00
|
|
|
///
|
|
|
|
/// An example:
|
|
|
|
///
|
2022-04-15 15:04:34 -07:00
|
|
|
/// ```ignore (illustrative)
|
2018-10-22 22:50:10 +02:00
|
|
|
/// struct S<'a>((i32, &'a str), String);
|
|
|
|
/// let S((_, w): (i32, &'static str), _): S = ...;
|
|
|
|
/// // ------ ^^^^^^^^^^^^^^^^^^^ (1)
|
|
|
|
/// // --------------------------------- ^ (2)
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// The highlights labelled `(1)` show the subpattern `(_, w)` being
|
|
|
|
/// ascribed the type `(i32, &'static str)`.
|
|
|
|
///
|
|
|
|
/// The highlights labelled `(2)` show the whole pattern being
|
|
|
|
/// ascribed the type `S`.
|
|
|
|
///
|
|
|
|
/// In this example, when we descend to `w`, we will have built up the
|
|
|
|
/// following two projected types:
|
|
|
|
///
|
|
|
|
/// * base: `S`, projection: `(base.0).1`
|
|
|
|
/// * base: `(i32, &'static str)`, projection: `base.1`
|
|
|
|
///
|
|
|
|
/// The first will lead to the constraint `w: &'1 str` (for some
|
|
|
|
/// inferred region `'1`). The second will lead to the constraint `w:
|
|
|
|
/// &'static str`.
|
2020-06-11 15:49:57 +01:00
|
|
|
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
|
2019-03-28 18:00:17 -07:00
|
|
|
pub struct UserTypeProjections {
|
2020-05-06 12:41:15 +10:00
|
|
|
pub contents: Vec<(UserTypeProjection, Span)>,
|
2018-10-22 14:23:44 +02:00
|
|
|
}
|
|
|
|
|
2019-03-28 18:00:17 -07:00
|
|
|
impl<'tcx> UserTypeProjections {
|
2018-10-22 14:23:44 +02:00
|
|
|
pub fn none() -> Self {
|
|
|
|
UserTypeProjections { contents: vec![] }
|
|
|
|
}
|
|
|
|
|
2020-05-06 12:41:15 +10:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.contents.is_empty()
|
|
|
|
}
|
|
|
|
|
2019-12-07 19:37:09 +01:00
|
|
|
pub fn projections_and_spans(
|
|
|
|
&self,
|
|
|
|
) -> impl Iterator<Item = &(UserTypeProjection, Span)> + ExactSizeIterator {
|
2018-10-22 14:23:44 +02:00
|
|
|
self.contents.iter()
|
|
|
|
}
|
|
|
|
|
2019-12-07 19:37:09 +01:00
|
|
|
pub fn projections(&self) -> impl Iterator<Item = &UserTypeProjection> + ExactSizeIterator {
|
2018-10-22 14:23:44 +02:00
|
|
|
self.contents.iter().map(|&(ref user_type, _span)| user_type)
|
|
|
|
}
|
2018-12-19 16:47:06 +01:00
|
|
|
|
2019-07-12 22:49:15 +02:00
|
|
|
pub fn push_projection(mut self, user_ty: &UserTypeProjection, span: Span) -> Self {
|
2018-12-19 16:47:06 +01:00
|
|
|
self.contents.push((user_ty.clone(), span));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn map_projections(
|
|
|
|
mut self,
|
2019-07-12 22:49:15 +02:00
|
|
|
mut f: impl FnMut(UserTypeProjection) -> UserTypeProjection,
|
2018-12-19 16:47:06 +01:00
|
|
|
) -> Self {
|
2021-11-06 18:42:07 +01:00
|
|
|
self.contents = self.contents.into_iter().map(|(proj, span)| (f(proj), span)).collect();
|
2018-12-19 16:47:06 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn index(self) -> Self {
|
|
|
|
self.map_projections(|pat_ty_proj| pat_ty_proj.index())
|
|
|
|
}
|
|
|
|
|
2020-08-23 14:54:58 +02:00
|
|
|
pub fn subslice(self, from: u64, to: u64) -> Self {
|
2018-12-19 16:47:06 +01:00
|
|
|
self.map_projections(|pat_ty_proj| pat_ty_proj.subslice(from, to))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deref(self) -> Self {
|
|
|
|
self.map_projections(|pat_ty_proj| pat_ty_proj.deref())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn leaf(self, field: Field) -> Self {
|
|
|
|
self.map_projections(|pat_ty_proj| pat_ty_proj.leaf(field))
|
|
|
|
}
|
|
|
|
|
2022-03-05 07:28:41 +11:00
|
|
|
pub fn variant(self, adt_def: AdtDef<'tcx>, variant_index: VariantIdx, field: Field) -> Self {
|
2018-12-19 16:47:06 +01:00
|
|
|
self.map_projections(|pat_ty_proj| pat_ty_proj.variant(adt_def, variant_index, field))
|
|
|
|
}
|
2018-10-22 14:23:44 +02:00
|
|
|
}
|
|
|
|
|
2018-10-22 22:50:10 +02:00
|
|
|
/// Encodes the effect of a user-supplied type annotation on the
|
|
|
|
/// subcomponents of a pattern. The effect is determined by applying the
|
2022-03-30 15:14:15 -04:00
|
|
|
/// given list of projections to some underlying base type. Often,
|
2018-10-22 22:50:10 +02:00
|
|
|
/// the projection element list `projs` is empty, in which case this
|
|
|
|
/// directly encodes a type in `base`. But in the case of complex patterns with
|
|
|
|
/// subpatterns and bindings, we want to apply only a *part* of the type to a variable,
|
|
|
|
/// in which case the `projs` vector is used.
|
|
|
|
///
|
|
|
|
/// Examples:
|
|
|
|
///
|
|
|
|
/// * `let x: T = ...` -- here, the `projs` vector is empty.
|
|
|
|
///
|
|
|
|
/// * `let (x, _): T = ...` -- here, the `projs` vector would contain
|
|
|
|
/// `field[0]` (aka `.0`), indicating that the type of `s` is
|
|
|
|
/// determined by finding the type of the `.0` field from `T`.
|
2020-10-04 15:52:14 +02:00
|
|
|
#[derive(Clone, Debug, TyEncodable, TyDecodable, Hash, HashStable, PartialEq)]
|
2019-03-28 18:00:17 -07:00
|
|
|
pub struct UserTypeProjection {
|
2018-11-16 22:56:18 +01:00
|
|
|
pub base: UserTypeAnnotationIndex,
|
2019-05-01 15:34:51 +01:00
|
|
|
pub projs: Vec<ProjectionKind>,
|
2018-10-22 11:58:06 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 22:49:15 +02:00
|
|
|
impl Copy for ProjectionKind {}
|
2018-10-26 11:28:40 +02:00
|
|
|
|
2019-03-28 18:00:17 -07:00
|
|
|
impl UserTypeProjection {
|
2018-12-19 16:47:06 +01:00
|
|
|
pub(crate) fn index(mut self) -> Self {
|
|
|
|
self.projs.push(ProjectionElem::Index(()));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-08-23 14:54:58 +02:00
|
|
|
pub(crate) fn subslice(mut self, from: u64, to: u64) -> Self {
|
2019-11-22 20:28:02 +00:00
|
|
|
self.projs.push(ProjectionElem::Subslice { from, to, from_end: true });
|
2018-12-19 16:47:06 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn deref(mut self) -> Self {
|
|
|
|
self.projs.push(ProjectionElem::Deref);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn leaf(mut self, field: Field) -> Self {
|
|
|
|
self.projs.push(ProjectionElem::Field(field, ()));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn variant(
|
|
|
|
mut self,
|
2022-03-05 07:28:41 +11:00
|
|
|
adt_def: AdtDef<'_>,
|
2018-12-19 16:47:06 +01:00
|
|
|
variant_index: VariantIdx,
|
|
|
|
field: Field,
|
|
|
|
) -> Self {
|
2019-03-28 18:00:17 -07:00
|
|
|
self.projs.push(ProjectionElem::Downcast(
|
2022-03-05 07:28:41 +11:00
|
|
|
Some(adt_def.variant(variant_index).name),
|
2019-07-12 22:49:15 +02:00
|
|
|
variant_index,
|
|
|
|
));
|
2018-12-19 16:47:06 +01:00
|
|
|
self.projs.push(ProjectionElem::Field(field, ()));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-24 09:27:15 +02:00
|
|
|
TrivialTypeFoldableAndLiftImpls! { ProjectionKind, }
|
2018-10-26 11:28:40 +02:00
|
|
|
|
2019-03-28 18:00:17 -07:00
|
|
|
impl<'tcx> TypeFoldable<'tcx> for UserTypeProjection {
|
2021-12-01 15:11:24 +00:00
|
|
|
fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
|
2021-12-01 00:55:57 +00:00
|
|
|
self,
|
|
|
|
folder: &mut F,
|
|
|
|
) -> Result<Self, F::Error> {
|
2021-05-19 13:34:54 +02:00
|
|
|
Ok(UserTypeProjection {
|
2021-12-01 00:55:57 +00:00
|
|
|
base: self.base.try_fold_with(folder)?,
|
|
|
|
projs: self.projs.try_fold_with(folder)?,
|
2021-05-19 13:34:54 +02:00
|
|
|
})
|
2018-10-22 22:50:10 +02:00
|
|
|
}
|
|
|
|
|
2020-11-05 17:30:39 +01:00
|
|
|
fn super_visit_with<Vs: TypeVisitor<'tcx>>(
|
|
|
|
&self,
|
|
|
|
visitor: &mut Vs,
|
|
|
|
) -> ControlFlow<Vs::BreakTy> {
|
2018-10-22 22:50:10 +02:00
|
|
|
self.base.visit_with(visitor)
|
|
|
|
// Note: there's nothing in `self.proj` to visit.
|
2018-10-22 11:58:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 05:38:33 +00:00
|
|
|
rustc_index::newtype_index! {
|
2018-07-25 13:41:32 +03:00
|
|
|
pub struct Promoted {
|
2018-12-03 01:14:35 +01:00
|
|
|
derive [HashStable]
|
2018-07-25 13:41:32 +03:00
|
|
|
DEBUG_FORMAT = "promoted[{}]"
|
|
|
|
}
|
|
|
|
}
|
2017-12-06 09:25:29 +01:00
|
|
|
|
2015-12-18 19:29:03 -06:00
|
|
|
impl<'tcx> Debug for Constant<'tcx> {
|
2018-08-29 22:02:42 -07:00
|
|
|
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
|
2019-03-29 10:52:09 +01:00
|
|
|
write!(fmt, "{}", self)
|
2015-12-18 19:29:03 -06:00
|
|
|
}
|
|
|
|
}
|
2016-01-05 23:06:33 -06:00
|
|
|
|
2019-03-29 10:52:09 +01:00
|
|
|
impl<'tcx> Display for Constant<'tcx> {
|
|
|
|
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
|
2021-03-08 16:18:03 +00:00
|
|
|
match self.ty().kind() {
|
2020-08-20 09:44:20 +00:00
|
|
|
ty::FnDef(..) => {}
|
|
|
|
_ => write!(fmt, "const ")?,
|
|
|
|
}
|
2021-03-30 14:26:40 +00:00
|
|
|
Display::fmt(&self.literal, fmt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Display for ConstantKind<'tcx> {
|
|
|
|
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
match *self {
|
2021-03-15 11:23:44 +00:00
|
|
|
ConstantKind::Ty(c) => pretty_print_const(c, fmt, true),
|
|
|
|
ConstantKind::Val(val, ty) => pretty_print_const_value(val, ty, fmt, true),
|
2021-03-08 16:18:03 +00:00
|
|
|
}
|
2019-03-29 10:52:09 +01:00
|
|
|
}
|
2016-01-05 23:06:33 -06:00
|
|
|
}
|
2016-06-09 15:49:07 -07:00
|
|
|
|
2021-12-15 19:32:30 -05:00
|
|
|
fn pretty_print_const<'tcx>(
|
2022-02-02 14:24:45 +11:00
|
|
|
c: ty::Const<'tcx>,
|
2020-03-14 15:30:35 +01:00
|
|
|
fmt: &mut Formatter<'_>,
|
|
|
|
print_types: bool,
|
|
|
|
) -> fmt::Result {
|
|
|
|
use crate::ty::print::PrettyPrinter;
|
|
|
|
ty::tls::with(|tcx| {
|
2020-10-16 21:59:49 +02:00
|
|
|
let literal = tcx.lift(c).unwrap();
|
2022-02-18 16:15:29 -05:00
|
|
|
let mut cx = FmtPrinter::new(tcx, Namespace::ValueNS);
|
2020-03-14 15:30:35 +01:00
|
|
|
cx.print_alloc_ids = true;
|
2022-02-18 16:15:29 -05:00
|
|
|
let cx = cx.pretty_print_const(literal, print_types)?;
|
|
|
|
fmt.write_str(&cx.into_buffer())?;
|
2020-03-14 15:30:35 +01:00
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-15 19:32:30 -05:00
|
|
|
fn pretty_print_const_value<'tcx>(
|
2021-03-08 16:18:03 +00:00
|
|
|
val: interpret::ConstValue<'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
fmt: &mut Formatter<'_>,
|
|
|
|
print_types: bool,
|
|
|
|
) -> fmt::Result {
|
|
|
|
use crate::ty::print::PrettyPrinter;
|
|
|
|
ty::tls::with(|tcx| {
|
2021-03-10 11:45:22 +00:00
|
|
|
let val = tcx.lift(val).unwrap();
|
2021-03-08 16:18:03 +00:00
|
|
|
let ty = tcx.lift(ty).unwrap();
|
2022-02-18 16:15:29 -05:00
|
|
|
let mut cx = FmtPrinter::new(tcx, Namespace::ValueNS);
|
2021-03-08 16:18:03 +00:00
|
|
|
cx.print_alloc_ids = true;
|
2022-02-18 16:15:29 -05:00
|
|
|
let cx = cx.pretty_print_const_value(val, ty, print_types)?;
|
|
|
|
fmt.write_str(&cx.into_buffer())?;
|
2021-03-08 16:18:03 +00:00
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-17 23:55:04 +02:00
|
|
|
impl<'tcx> graph::DirectedGraph for Body<'tcx> {
|
2016-06-09 15:49:07 -07:00
|
|
|
type Node = BasicBlock;
|
2018-07-01 16:54:01 -04:00
|
|
|
}
|
2016-06-09 15:49:07 -07:00
|
|
|
|
2019-05-17 23:55:04 +02:00
|
|
|
impl<'tcx> graph::WithNumNodes for Body<'tcx> {
|
2020-04-12 10:48:56 -07:00
|
|
|
#[inline]
|
2018-06-19 21:22:52 -03:00
|
|
|
fn num_nodes(&self) -> usize {
|
|
|
|
self.basic_blocks.len()
|
|
|
|
}
|
2018-07-01 16:54:01 -04:00
|
|
|
}
|
2016-06-09 15:49:07 -07:00
|
|
|
|
2019-05-17 23:55:04 +02:00
|
|
|
impl<'tcx> graph::WithStartNode for Body<'tcx> {
|
2020-04-12 10:48:56 -07:00
|
|
|
#[inline]
|
2018-06-19 21:22:52 -03:00
|
|
|
fn start_node(&self) -> Self::Node {
|
|
|
|
START_BLOCK
|
|
|
|
}
|
2018-07-01 16:54:01 -04:00
|
|
|
}
|
2016-06-09 15:49:07 -07:00
|
|
|
|
2019-05-17 23:55:04 +02:00
|
|
|
impl<'tcx> graph::WithSuccessors for Body<'tcx> {
|
2020-04-12 10:48:56 -07:00
|
|
|
#[inline]
|
2019-06-21 23:49:03 +02:00
|
|
|
fn successors(&self, node: Self::Node) -> <Self as GraphSuccessors<'_>>::Iter {
|
2022-05-17 08:41:01 +08:00
|
|
|
self.basic_blocks[node].terminator().successors()
|
2016-06-09 15:49:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 23:55:04 +02:00
|
|
|
impl<'a, 'b> graph::GraphSuccessors<'b> for Body<'a> {
|
2016-06-09 15:49:07 -07:00
|
|
|
type Item = BasicBlock;
|
2022-05-17 08:41:01 +08:00
|
|
|
type Iter = Successors<'b>;
|
2016-06-09 15:49:07 -07:00
|
|
|
}
|
2016-08-08 18:46:06 -07:00
|
|
|
|
2021-12-15 19:32:30 -05:00
|
|
|
impl<'tcx, 'graph> graph::GraphPredecessors<'graph> for Body<'tcx> {
|
2020-04-12 10:30:07 -07:00
|
|
|
type Item = BasicBlock;
|
2021-05-07 21:00:03 -04:00
|
|
|
type Iter = std::iter::Copied<std::slice::Iter<'graph, BasicBlock>>;
|
2020-04-12 10:30:07 -07:00
|
|
|
}
|
|
|
|
|
2021-12-15 19:32:30 -05:00
|
|
|
impl<'tcx> graph::WithPredecessors for Body<'tcx> {
|
2020-04-12 10:48:56 -07:00
|
|
|
#[inline]
|
2020-04-12 10:30:07 -07:00
|
|
|
fn predecessors(&self, node: Self::Node) -> <Self as graph::GraphPredecessors<'_>>::Iter {
|
2021-05-07 21:00:03 -04:00
|
|
|
self.predecessors()[node].iter().copied()
|
2020-04-12 10:30:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 13:24:33 -04:00
|
|
|
/// `Location` represents the position of the start of the statement; or, if
|
|
|
|
/// `statement_index` equals the number of statements, then the start of the
|
|
|
|
/// terminator.
|
2018-12-03 01:14:35 +01:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Ord, PartialOrd, HashStable)]
|
2016-08-08 18:46:06 -07:00
|
|
|
pub struct Location {
|
2019-09-06 03:57:44 +01:00
|
|
|
/// The block that the location is within.
|
2016-08-08 18:46:06 -07:00
|
|
|
pub block: BasicBlock,
|
|
|
|
|
|
|
|
pub statement_index: usize,
|
|
|
|
}
|
|
|
|
|
2016-06-11 23:47:28 +03:00
|
|
|
impl fmt::Debug for Location {
|
2018-08-29 22:02:42 -07:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-06-11 23:47:28 +03:00
|
|
|
write!(fmt, "{:?}[{}]", self.block, self.statement_index)
|
|
|
|
}
|
|
|
|
}
|
2016-09-15 18:18:40 -07:00
|
|
|
|
|
|
|
impl Location {
|
2019-07-12 22:49:15 +02:00
|
|
|
pub const START: Location = Location { block: START_BLOCK, statement_index: 0 };
|
2018-05-01 09:46:11 -04:00
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
/// Returns the location immediately after this one within the enclosing block.
|
|
|
|
///
|
|
|
|
/// Note that if this location represents a terminator, then the
|
|
|
|
/// resulting location would be out of bounds and invalid.
|
|
|
|
pub fn successor_within_block(&self) -> Location {
|
2019-07-12 22:49:15 +02:00
|
|
|
Location { block: self.block, statement_index: self.statement_index + 1 }
|
2017-12-06 09:25:29 +01:00
|
|
|
}
|
|
|
|
|
2018-11-05 15:14:28 +01:00
|
|
|
/// Returns `true` if `other` is earlier in the control flow graph than `self`.
|
2020-04-12 10:30:07 -07:00
|
|
|
pub fn is_predecessor_of<'tcx>(&self, other: Location, body: &Body<'tcx>) -> bool {
|
2018-11-05 15:14:28 +01:00
|
|
|
// If we are in the same block as the other location and are an earlier statement
|
|
|
|
// then we are a predecessor of `other`.
|
|
|
|
if self.block == other.block && self.statement_index < other.statement_index {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-19 13:56:49 -07:00
|
|
|
let predecessors = body.predecessors();
|
|
|
|
|
2018-11-05 15:14:28 +01:00
|
|
|
// If we're in another block, then we want to check that block is a predecessor of `other`.
|
2020-04-19 13:56:49 -07:00
|
|
|
let mut queue: Vec<BasicBlock> = predecessors[other.block].to_vec();
|
2018-11-05 15:14:28 +01:00
|
|
|
let mut visited = FxHashSet::default();
|
|
|
|
|
|
|
|
while let Some(block) = queue.pop() {
|
2022-02-01 22:32:02 +01:00
|
|
|
// If we haven't visited this block before, then make sure we visit its predecessors.
|
2018-11-05 15:14:28 +01:00
|
|
|
if visited.insert(block) {
|
2020-04-19 13:56:49 -07:00
|
|
|
queue.extend(predecessors[block].iter().cloned());
|
2018-11-05 15:14:28 +01:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we found the block that `self` is in, then we are a predecessor of `other` (since
|
|
|
|
// we found that block by looking at the predecessors of `other`).
|
|
|
|
if self.block == block {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2018-04-06 20:48:13 -04:00
|
|
|
pub fn dominates(&self, other: Location, dominators: &Dominators<BasicBlock>) -> bool {
|
2016-09-15 18:18:40 -07:00
|
|
|
if self.block == other.block {
|
|
|
|
self.statement_index <= other.statement_index
|
|
|
|
} else {
|
|
|
|
dominators.is_dominated_by(other.block, self.block)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|