Rollup merge of #136465 - nnethercote:rustc_middle-MORE, r=jieyouxu
Some `rustc_middle` cleanups Small cleanups I found while looking closely at this code. r? `@jieyouxu`
This commit is contained in:
commit
87af17c978
7 changed files with 74 additions and 151 deletions
|
@ -1,61 +1,3 @@
|
|||
//! Nodes in the dependency graph.
|
||||
//!
|
||||
//! A node in the [dependency graph] is represented by a [`DepNode`].
|
||||
//! A `DepNode` consists of a [`DepKind`] (which
|
||||
//! specifies the kind of thing it represents, like a piece of HIR, MIR, etc.)
|
||||
//! and a [`Fingerprint`], a 128-bit hash value, the exact meaning of which
|
||||
//! depends on the node's `DepKind`. Together, the kind and the fingerprint
|
||||
//! fully identify a dependency node, even across multiple compilation sessions.
|
||||
//! In other words, the value of the fingerprint does not depend on anything
|
||||
//! that is specific to a given compilation session, like an unpredictable
|
||||
//! interning key (e.g., `NodeId`, `DefId`, `Symbol`) or the numeric value of a
|
||||
//! pointer. The concept behind this could be compared to how git commit hashes
|
||||
//! uniquely identify a given commit. The fingerprinting approach has
|
||||
//! a few advantages:
|
||||
//!
|
||||
//! * A `DepNode` can simply be serialized to disk and loaded in another session
|
||||
//! without the need to do any "rebasing" (like we have to do for Spans and
|
||||
//! NodeIds) or "retracing" (like we had to do for `DefId` in earlier
|
||||
//! implementations of the dependency graph).
|
||||
//! * A `Fingerprint` is just a bunch of bits, which allows `DepNode` to
|
||||
//! implement `Copy`, `Sync`, `Send`, `Freeze`, etc.
|
||||
//! * Since we just have a bit pattern, `DepNode` can be mapped from disk into
|
||||
//! memory without any post-processing (e.g., "abomination-style" pointer
|
||||
//! reconstruction).
|
||||
//! * Because a `DepNode` is self-contained, we can instantiate `DepNodes` that
|
||||
//! refer to things that do not exist anymore. In previous implementations
|
||||
//! `DepNode` contained a `DefId`. A `DepNode` referring to something that
|
||||
//! had been removed between the previous and the current compilation session
|
||||
//! could not be instantiated because the current compilation session
|
||||
//! contained no `DefId` for thing that had been removed.
|
||||
//!
|
||||
//! `DepNode` definition happens in the `define_dep_nodes!()` macro. This macro
|
||||
//! defines the `DepKind` enum. Each `DepKind` has its own parameters that are
|
||||
//! needed at runtime in order to construct a valid `DepNode` fingerprint.
|
||||
//! However, only `CompileCodegenUnit` and `CompileMonoItem` are constructed
|
||||
//! explicitly (with `make_compile_codegen_unit` cq `make_compile_mono_item`).
|
||||
//!
|
||||
//! Because the macro sees what parameters a given `DepKind` requires, it can
|
||||
//! "infer" some properties for each kind of `DepNode`:
|
||||
//!
|
||||
//! * Whether a `DepNode` of a given kind has any parameters at all. Some
|
||||
//! `DepNode`s could represent global concepts with only one value.
|
||||
//! * Whether it is possible, in principle, to reconstruct a query key from a
|
||||
//! given `DepNode`. Many `DepKind`s only require a single `DefId` parameter,
|
||||
//! in which case it is possible to map the node's fingerprint back to the
|
||||
//! `DefId` it was computed from. In other cases, too much information gets
|
||||
//! lost during fingerprint computation.
|
||||
//!
|
||||
//! `make_compile_codegen_unit` and `make_compile_mono_items`, together with
|
||||
//! `DepNode::new()`, ensures that only valid `DepNode` instances can be
|
||||
//! constructed. For example, the API does not allow for constructing
|
||||
//! parameterless `DepNode`s with anything other than a zeroed out fingerprint.
|
||||
//! More generally speaking, it relieves the user of the `DepNode` API of
|
||||
//! having to know how to compute the expected fingerprint for a given set of
|
||||
//! node parameters.
|
||||
//!
|
||||
//! [dependency graph]: https://rustc-dev-guide.rust-lang.org/query.html
|
||||
|
||||
use rustc_data_structures::fingerprint::Fingerprint;
|
||||
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalModDefId, ModDefId};
|
||||
use rustc_hir::definitions::DefPathHash;
|
||||
|
@ -158,26 +100,14 @@ pub(crate) fn make_compile_mono_item<'tcx>(
|
|||
}
|
||||
|
||||
pub trait DepNodeExt: Sized {
|
||||
/// Extracts the DefId corresponding to this DepNode. This will work
|
||||
/// if two conditions are met:
|
||||
///
|
||||
/// 1. The Fingerprint of the DepNode actually is a DefPathHash, and
|
||||
/// 2. the item that the DefPath refers to exists in the current tcx.
|
||||
///
|
||||
/// Condition (1) is determined by the DepKind variant of the
|
||||
/// DepNode. Condition (2) might not be fulfilled if a DepNode
|
||||
/// refers to something from the previous compilation session that
|
||||
/// has been removed.
|
||||
fn extract_def_id(&self, tcx: TyCtxt<'_>) -> Option<DefId>;
|
||||
|
||||
/// Used in testing
|
||||
fn from_label_string(
|
||||
tcx: TyCtxt<'_>,
|
||||
label: &str,
|
||||
def_path_hash: DefPathHash,
|
||||
) -> Result<Self, ()>;
|
||||
|
||||
/// Used in testing
|
||||
fn has_label_string(label: &str) -> bool;
|
||||
}
|
||||
|
||||
|
@ -399,52 +329,46 @@ impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for HirId {
|
|||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_for_typed_def_id {
|
||||
($Name:ident, $LocalName:ident) => {
|
||||
impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for $Name {
|
||||
#[inline(always)]
|
||||
fn fingerprint_style() -> FingerprintStyle {
|
||||
FingerprintStyle::DefPathHash
|
||||
}
|
||||
impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for ModDefId {
|
||||
#[inline(always)]
|
||||
fn fingerprint_style() -> FingerprintStyle {
|
||||
FingerprintStyle::DefPathHash
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
|
||||
self.to_def_id().to_fingerprint(tcx)
|
||||
}
|
||||
#[inline(always)]
|
||||
fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
|
||||
self.to_def_id().to_fingerprint(tcx)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
|
||||
self.to_def_id().to_debug_str(tcx)
|
||||
}
|
||||
#[inline(always)]
|
||||
fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
|
||||
self.to_def_id().to_debug_str(tcx)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
|
||||
DefId::recover(tcx, dep_node).map($Name::new_unchecked)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for $LocalName {
|
||||
#[inline(always)]
|
||||
fn fingerprint_style() -> FingerprintStyle {
|
||||
FingerprintStyle::DefPathHash
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
|
||||
self.to_def_id().to_fingerprint(tcx)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
|
||||
self.to_def_id().to_debug_str(tcx)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
|
||||
LocalDefId::recover(tcx, dep_node).map($LocalName::new_unchecked)
|
||||
}
|
||||
}
|
||||
};
|
||||
#[inline(always)]
|
||||
fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
|
||||
DefId::recover(tcx, dep_node).map(ModDefId::new_unchecked)
|
||||
}
|
||||
}
|
||||
|
||||
impl_for_typed_def_id! { ModDefId, LocalModDefId }
|
||||
impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for LocalModDefId {
|
||||
#[inline(always)]
|
||||
fn fingerprint_style() -> FingerprintStyle {
|
||||
FingerprintStyle::DefPathHash
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
|
||||
self.to_def_id().to_fingerprint(tcx)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
|
||||
self.to_def_id().to_debug_str(tcx)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
|
||||
LocalDefId::recover(tcx, dep_node).map(LocalModDefId::new_unchecked)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
//! The "main crate" of the Rust compiler. This crate contains common
|
||||
//! type definitions that are used by the other crates in the rustc
|
||||
//! "family". Some prominent examples (note that each of these modules
|
||||
//! has their own README with further details).
|
||||
//! "family". The following are some prominent examples.
|
||||
//!
|
||||
//! - **HIR.** The "high-level (H) intermediate representation (IR)" is
|
||||
//! defined in the [`hir`] module.
|
||||
//! - **THIR.** The "typed high-level (H) intermediate representation (IR)"
|
||||
//! is defined in the [`thir`] module.
|
||||
//! - **MIR.** The "mid-level (M) intermediate representation (IR)" is
|
||||
//! defined in the [`mir`] module. This module contains only the
|
||||
//! *definition* of the MIR; the passes that transform and operate
|
||||
|
@ -37,7 +38,6 @@
|
|||
#![feature(box_as_ptr)]
|
||||
#![feature(box_patterns)]
|
||||
#![feature(closure_track_caller)]
|
||||
#![feature(const_type_name)]
|
||||
#![feature(core_intrinsics)]
|
||||
#![feature(coroutines)]
|
||||
#![feature(debug_closure_helpers)]
|
||||
|
@ -50,7 +50,6 @@
|
|||
#![feature(intra_doc_pointers)]
|
||||
#![feature(iter_from_coroutine)]
|
||||
#![feature(let_chains)]
|
||||
#![feature(macro_metavar_expr)]
|
||||
#![feature(min_specialization)]
|
||||
#![feature(negative_impls)]
|
||||
#![feature(never_type)]
|
||||
|
|
|
@ -37,9 +37,6 @@ pub mod visit;
|
|||
|
||||
macro_rules! thir_with_elements {
|
||||
(
|
||||
$($field_name:ident: $field_ty:ty,)*
|
||||
|
||||
@elements:
|
||||
$($name:ident: $id:ty => $value:ty => $format:literal,)*
|
||||
) => {
|
||||
$(
|
||||
|
@ -55,20 +52,16 @@ macro_rules! thir_with_elements {
|
|||
/// This can be indexed directly by any THIR index (e.g. [`ExprId`]).
|
||||
#[derive(Debug, HashStable)]
|
||||
pub struct Thir<'tcx> {
|
||||
$(
|
||||
pub $field_name: $field_ty,
|
||||
)*
|
||||
pub body_type: BodyTy<'tcx>,
|
||||
$(
|
||||
pub $name: IndexVec<$id, $value>,
|
||||
)*
|
||||
}
|
||||
|
||||
impl<'tcx> Thir<'tcx> {
|
||||
pub fn new($($field_name: $field_ty,)*) -> Thir<'tcx> {
|
||||
pub fn new(body_type: BodyTy<'tcx>) -> Thir<'tcx> {
|
||||
Thir {
|
||||
$(
|
||||
$field_name,
|
||||
)*
|
||||
body_type,
|
||||
$(
|
||||
$name: IndexVec::new(),
|
||||
)*
|
||||
|
@ -88,9 +81,6 @@ macro_rules! thir_with_elements {
|
|||
}
|
||||
|
||||
thir_with_elements! {
|
||||
body_type: BodyTy<'tcx>,
|
||||
|
||||
@elements:
|
||||
arms: ArmId => Arm<'tcx> => "a{}",
|
||||
blocks: BlockId => Block => "b{}",
|
||||
exprs: ExprId => Expr<'tcx> => "e{}",
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
use super::{
|
||||
AdtExpr, Arm, Block, ClosureExpr, Expr, ExprKind, InlineAsmExpr, InlineAsmOperand, Pat,
|
||||
PatKind, Stmt, StmtKind, Thir,
|
||||
AdtExpr, AdtExprBase, Arm, Block, ClosureExpr, Expr, ExprKind, InlineAsmExpr, InlineAsmOperand,
|
||||
Pat, PatKind, Stmt, StmtKind, Thir,
|
||||
};
|
||||
use crate::thir::AdtExprBase;
|
||||
|
||||
pub trait Visitor<'thir, 'tcx: 'thir>: Sized {
|
||||
fn thir(&self) -> &'thir Thir<'tcx>;
|
||||
|
|
|
@ -21,13 +21,12 @@ use rustc_macros::{
|
|||
};
|
||||
use rustc_span::def_id::{CRATE_DEF_ID, LocalDefId};
|
||||
use rustc_span::{DUMMY_SP, Span, Symbol};
|
||||
// FIXME: Remove this import and import via `solve::`
|
||||
pub use rustc_type_ir::solve::BuiltinImplSource;
|
||||
use smallvec::{SmallVec, smallvec};
|
||||
use thin_vec::ThinVec;
|
||||
|
||||
pub use self::select::{EvaluationCache, EvaluationResult, OverflowError, SelectionCache};
|
||||
use crate::mir::ConstraintCategory;
|
||||
pub use crate::traits::solve::BuiltinImplSource;
|
||||
use crate::ty::abstract_const::NotConstEvaluatable;
|
||||
use crate::ty::{self, AdtKind, GenericArgsRef, Ty};
|
||||
|
||||
|
|
|
@ -7,11 +7,10 @@
|
|||
|
||||
use rustc_macros::{HashStable, TypeFoldable, TypeVisitable};
|
||||
use rustc_span::Span;
|
||||
// FIXME: Remove this import and import via `traits::solve`.
|
||||
pub use rustc_type_ir::solve::NoSolution;
|
||||
|
||||
use crate::error::DropCheckOverflow;
|
||||
use crate::infer::canonical::{Canonical, CanonicalQueryInput, QueryResponse};
|
||||
pub use crate::traits::solve::NoSolution;
|
||||
use crate::ty::{self, GenericArg, Ty, TyCtxt};
|
||||
|
||||
pub mod type_op {
|
||||
|
|
|
@ -1,19 +1,20 @@
|
|||
//! This module defines the `DepNode` type which the compiler uses to represent
|
||||
//! nodes in the dependency graph. A `DepNode` consists of a `DepKind` (which
|
||||
//! specifies the kind of thing it represents, like a piece of HIR, MIR, etc)
|
||||
//! and a `Fingerprint`, a 128 bit hash value the exact meaning of which
|
||||
//! This module defines the [`DepNode`] type which the compiler uses to represent
|
||||
//! nodes in the [dependency graph]. A `DepNode` consists of a [`DepKind`] (which
|
||||
//! specifies the kind of thing it represents, like a piece of HIR, MIR, etc.)
|
||||
//! and a [`Fingerprint`], a 128-bit hash value, the exact meaning of which
|
||||
//! depends on the node's `DepKind`. Together, the kind and the fingerprint
|
||||
//! fully identify a dependency node, even across multiple compilation sessions.
|
||||
//! In other words, the value of the fingerprint does not depend on anything
|
||||
//! that is specific to a given compilation session, like an unpredictable
|
||||
//! interning key (e.g., NodeId, DefId, Symbol) or the numeric value of a
|
||||
//! interning key (e.g., `NodeId`, `DefId`, `Symbol`) or the numeric value of a
|
||||
//! pointer. The concept behind this could be compared to how git commit hashes
|
||||
//! uniquely identify a given commit and has a few advantages:
|
||||
//! uniquely identify a given commit. The fingerprinting approach has
|
||||
//! a few advantages:
|
||||
//!
|
||||
//! * A `DepNode` can simply be serialized to disk and loaded in another session
|
||||
//! without the need to do any "rebasing (like we have to do for Spans and
|
||||
//! NodeIds) or "retracing" like we had to do for `DefId` in earlier
|
||||
//! implementations of the dependency graph.
|
||||
//! without the need to do any "rebasing" (like we have to do for Spans and
|
||||
//! NodeIds) or "retracing" (like we had to do for `DefId` in earlier
|
||||
//! implementations of the dependency graph).
|
||||
//! * A `Fingerprint` is just a bunch of bits, which allows `DepNode` to
|
||||
//! implement `Copy`, `Sync`, `Send`, `Freeze`, etc.
|
||||
//! * Since we just have a bit pattern, `DepNode` can be mapped from disk into
|
||||
|
@ -26,10 +27,12 @@
|
|||
//! could not be instantiated because the current compilation session
|
||||
//! contained no `DefId` for thing that had been removed.
|
||||
//!
|
||||
//! `DepNode` definition happens in `rustc_middle` with the `define_dep_nodes!()` macro.
|
||||
//! This macro defines the `DepKind` enum and a corresponding `DepConstructor` enum. The
|
||||
//! `DepConstructor` enum links a `DepKind` to the parameters that are needed at runtime in order
|
||||
//! to construct a valid `DepNode` fingerprint.
|
||||
//! `DepNode` definition happens in `rustc_middle` with the
|
||||
//! `define_dep_nodes!()` macro. This macro defines the `DepKind` enum. Each
|
||||
//! `DepKind` has its own parameters that are needed at runtime in order to
|
||||
//! construct a valid `DepNode` fingerprint. However, only `CompileCodegenUnit`
|
||||
//! and `CompileMonoItem` are constructed explicitly (with
|
||||
//! `make_compile_codegen_unit` and `make_compile_mono_item`).
|
||||
//!
|
||||
//! Because the macro sees what parameters a given `DepKind` requires, it can
|
||||
//! "infer" some properties for each kind of `DepNode`:
|
||||
|
@ -41,6 +44,16 @@
|
|||
//! in which case it is possible to map the node's fingerprint back to the
|
||||
//! `DefId` it was computed from. In other cases, too much information gets
|
||||
//! lost during fingerprint computation.
|
||||
//!
|
||||
//! `make_compile_codegen_unit` and `make_compile_mono_items`, together with
|
||||
//! `DepNode::new()`, ensure that only valid `DepNode` instances can be
|
||||
//! constructed. For example, the API does not allow for constructing
|
||||
//! parameterless `DepNode`s with anything other than a zeroed out fingerprint.
|
||||
//! More generally speaking, it relieves the user of the `DepNode` API of
|
||||
//! having to know how to compute the expected fingerprint for a given set of
|
||||
//! node parameters.
|
||||
//!
|
||||
//! [dependency graph]: https://rustc-dev-guide.rust-lang.org/query.html
|
||||
|
||||
use std::fmt;
|
||||
use std::hash::Hash;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue