Bring back enum DepKind.

It was removed in #115920 to enable it being moved to
`rustc_query_system`, a move that has recently been reversed. It's much
simpler as an enum.

Also:
- Remove the overly complicated `Debug` impl for `DepKind`.
- Remove the trivial `DepKind` associated constants (`NULL` et al.)
- Add an assertion to ensure that the number of `DepKinds` fits within a
  `u16`.
- Rename `DEP_KIND_VARIANTS` as `DEP_KIND_NUM_VARIANTS`, to make it
  clearer that it's a count, not a collection.
- Use `stringify!` in one place to make the code clearer.
This commit is contained in:
Nicholas Nethercote
2026-02-17 11:36:48 +11:00
parent 90b994b883
commit c4a69d4712
11 changed files with 59 additions and 96 deletions
@@ -44,9 +44,7 @@
use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId};
use rustc_hir::intravisit::{self, Visitor};
use rustc_middle::bug;
use rustc_middle::dep_graph::{
DepGraphQuery, DepKind, DepNode, DepNodeFilter, EdgeFilter, dep_kinds,
};
use rustc_middle::dep_graph::{DepGraphQuery, DepKind, DepNode, DepNodeFilter, EdgeFilter};
use rustc_middle::hir::nested_filter;
use rustc_middle::ty::TyCtxt;
use rustc_span::{Span, Symbol, sym};
@@ -117,7 +115,7 @@ fn process_attrs(&mut self, def_id: LocalDefId) {
None => DepNode::from_def_path_hash(
self.tcx,
def_path_hash,
dep_kinds::opt_hir_owner_nodes,
DepKind::opt_hir_owner_nodes,
),
Some(n) => {
match DepNode::from_label_string(self.tcx, n.as_str(), def_path_hash) {
@@ -147,8 +147,8 @@ fn $name<'tcx>(
// External query providers call `crate_hash` in order to register a dependency
// on the crate metadata. The exception is `crate_hash` itself, which obviously
// doesn't need to do this (and can't, as it would cause a query cycle).
use rustc_middle::dep_graph::dep_kinds;
if dep_kinds::$name != dep_kinds::crate_hash && $tcx.dep_graph.is_fully_enabled() {
use rustc_middle::dep_graph::DepKind;
if DepKind::$name != DepKind::crate_hash && $tcx.dep_graph.is_fully_enabled() {
$tcx.ensure_ok().crate_hash($def_id.krate);
}
+26 -56
View File
@@ -62,59 +62,34 @@
use crate::mir::mono::MonoItem;
use crate::ty::{TyCtxt, tls};
/// This serves as an index into arrays built by `make_dep_kind_array`.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct DepKind {
variant: u16,
}
// `enum DepKind` is generated by `define_dep_nodes!` below.
impl DepKind {
#[inline]
pub const fn new(variant: u16) -> Self {
Self { variant }
pub(crate) fn from_u16(u: u16) -> Self {
if u > Self::MAX {
panic!("Invalid DepKind {u}");
}
// SAFETY: See comment on DEP_KIND_NUM_VARIANTS
unsafe { std::mem::transmute(u) }
}
#[inline]
pub const fn as_inner(&self) -> u16 {
self.variant
pub(crate) const fn as_u16(&self) -> u16 {
*self as u16
}
#[inline]
pub const fn as_usize(&self) -> usize {
self.variant as usize
*self as usize
}
pub(crate) fn name(self) -> &'static str {
DEP_KIND_NAMES[self.as_usize()]
}
/// We use this for most things when incr. comp. is turned off.
pub(crate) const NULL: DepKind = dep_kinds::Null;
/// We use this to create a forever-red node.
pub(crate) const RED: DepKind = dep_kinds::Red;
/// We use this to create a side effect node.
pub(crate) const SIDE_EFFECT: DepKind = dep_kinds::SideEffect;
/// We use this to create the anon node with zero dependencies.
pub(crate) const ANON_ZERO_DEPS: DepKind = dep_kinds::AnonZeroDeps;
/// This is the highest value a `DepKind` can have. It's used during encoding to
/// pack information into the unused bits.
pub(crate) const MAX: u16 = DEP_KIND_VARIANTS - 1;
}
impl fmt::Debug for DepKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
tls::with_opt(|opt_tcx| {
if let Some(tcx) = opt_tcx {
write!(f, "{}", tcx.dep_kind_vtable(*self).name)
} else {
f.debug_struct("DepKind").field("variant", &self.variant).finish()
}
})
}
pub(crate) const MAX: u16 = DEP_KIND_NUM_VARIANTS - 1;
}
/// Combination of a [`DepKind`] and a key fingerprint that uniquely identifies
@@ -374,26 +349,18 @@ macro_rules! make_dep_kind_array {
// encoding. The derived Encodable/Decodable uses leb128 encoding which is
// dense when only considering this enum. But DepKind is encoded in a larger
// struct, and there we can take advantage of the unused bits in the u16.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[allow(non_camel_case_types)]
#[repr(u16)] // Must be kept in sync with the inner type of `DepKind`.
enum DepKindDefs {
#[repr(u16)] // Must be kept in sync with the rest of `DepKind`.
pub enum DepKind {
$( $( #[$attr] )* $variant),*
}
#[allow(non_upper_case_globals)]
pub mod dep_kinds {
use super::*;
$(
// The `as u16` cast must be kept in sync with the inner type of `DepKind`.
pub const $variant: DepKind = DepKind::new(DepKindDefs::$variant as u16);
)*
}
// This checks that the discriminants of the variants have been assigned consecutively
// from 0 so that they can be used as a dense index.
pub(crate) const DEP_KIND_VARIANTS: u16 = {
let deps = &[$(dep_kinds::$variant,)*];
// This computes the number of dep kind variants. Along the way, it sanity-checks that the
// discriminants of the variants have been assigned consecutively from 0 so that they can
// be used as a dense index, and that all discriminants fit in a `u16`.
pub(crate) const DEP_KIND_NUM_VARIANTS: u16 = {
let deps = &[$(DepKind::$variant,)*];
let mut i = 0;
while i < deps.len() {
if i != deps[i].as_usize() {
@@ -401,6 +368,7 @@ pub mod dep_kinds {
}
i += 1;
}
assert!(deps.len() <= u16::MAX as usize);
deps.len() as u16
};
@@ -412,7 +380,7 @@ pub mod dep_kinds {
pub(super) fn dep_kind_from_label_string(label: &str) -> Result<DepKind, ()> {
match label {
$( self::label_strs::$variant => Ok(self::dep_kinds::$variant), )*
$( stringify!($variant) => Ok(self::DepKind::$variant), )*
_ => Err(()),
}
}
@@ -433,7 +401,9 @@ pub mod label_strs {
[] fn Null() -> (),
/// We use this to create a forever-red node.
[] fn Red() -> (),
/// We use this to create a side effect node.
[] fn SideEffect() -> (),
/// We use this to create the anon node with zero dependencies.
[] fn AnonZeroDeps() -> (),
[] fn TraitSelect() -> (),
[] fn CompileCodegenUnit() -> (),
@@ -444,7 +414,7 @@ pub mod label_strs {
// WARNING: `construct` is generic and does not know that `CompileCodegenUnit` takes `Symbol`s as keys.
// Be very careful changing this type signature!
pub(crate) fn make_compile_codegen_unit(tcx: TyCtxt<'_>, name: Symbol) -> DepNode {
DepNode::construct(tcx, dep_kinds::CompileCodegenUnit, &name)
DepNode::construct(tcx, DepKind::CompileCodegenUnit, &name)
}
// WARNING: `construct` is generic and does not know that `CompileMonoItem` takes `MonoItem`s as keys.
@@ -453,13 +423,13 @@ pub(crate) fn make_compile_mono_item<'tcx>(
tcx: TyCtxt<'tcx>,
mono_item: &MonoItem<'tcx>,
) -> DepNode {
DepNode::construct(tcx, dep_kinds::CompileMonoItem, mono_item)
DepNode::construct(tcx, DepKind::CompileMonoItem, mono_item)
}
// WARNING: `construct` is generic and does not know that `Metadata` takes `()`s as keys.
// Be very careful changing this type signature!
pub(crate) fn make_metadata(tcx: TyCtxt<'_>) -> DepNode {
DepNode::construct(tcx, dep_kinds::Metadata, &())
DepNode::construct(tcx, DepKind::Metadata, &())
}
impl DepNode {
+4 -4
View File
@@ -142,7 +142,7 @@ pub fn new(
// Instantiate a node with zero dependencies only once for anonymous queries.
let _green_node_index = current.alloc_new_node(
DepNode { kind: DepKind::ANON_ZERO_DEPS, key_fingerprint: current.anon_id_seed.into() },
DepNode { kind: DepKind::AnonZeroDeps, key_fingerprint: current.anon_id_seed.into() },
EdgesVec::new(),
Fingerprint::ZERO,
);
@@ -152,7 +152,7 @@ pub fn new(
// Other nodes can use the always-red node as a fake dependency, to
// ensure that their dependency list will never be all-green.
let red_node_index = current.alloc_new_node(
DepNode { kind: DepKind::RED, key_fingerprint: Fingerprint::ZERO.into() },
DepNode { kind: DepKind::Red, key_fingerprint: Fingerprint::ZERO.into() },
EdgesVec::new(),
Fingerprint::ZERO,
);
@@ -680,7 +680,7 @@ fn encode_diagnostic<'tcx>(&self, tcx: TyCtxt<'tcx>, diagnostic: &DiagInner) ->
// Use `send_new` so we get an unique index, even though the dep node is not.
let dep_node_index = self.current.encoder.send_new(
DepNode {
kind: DepKind::SIDE_EFFECT,
kind: DepKind::SideEffect,
key_fingerprint: PackedFingerprint::from(Fingerprint::ZERO),
},
Fingerprint::ZERO,
@@ -713,7 +713,7 @@ fn force_diagnostic_node<'tcx>(&self, tcx: TyCtxt<'tcx>, prev_index: SerializedD
prev_index,
&self.colors,
DepNode {
kind: DepKind::SIDE_EFFECT,
kind: DepKind::SideEffect,
key_fingerprint: PackedFingerprint::from(Fingerprint::ZERO),
},
Fingerprint::ZERO,
+1 -2
View File
@@ -3,8 +3,7 @@
use tracing::instrument;
pub use self::dep_node::{
DepKind, DepKindVTable, DepNode, DepNodeKey, WorkProductId, dep_kind_from_label, dep_kinds,
label_strs,
DepKind, DepKindVTable, DepNode, DepNodeKey, WorkProductId, dep_kind_from_label, label_strs,
};
pub use self::graph::{
DepGraph, DepGraphData, DepNodeIndex, QuerySideEffect, TaskDepsRef, WorkProduct,
@@ -84,7 +84,7 @@ pub struct SerializedDepNodeIndex {}
/// Data for use when recompiling the **current crate**.
///
/// There may be unused indices with DEP_KIND_NULL in this graph due to batch allocation of
/// There may be unused indices with DepKind::Null in this graph due to batch allocation of
/// indices to threads.
#[derive(Debug, Default)]
pub struct SerializedDepGraph {
@@ -220,7 +220,7 @@ pub fn decode(d: &mut MemDecoder<'_>) -> Arc<SerializedDepGraph> {
let mut nodes = IndexVec::from_elem_n(
DepNode {
kind: DepKind::NULL,
kind: DepKind::Null,
key_fingerprint: PackedFingerprint::from(Fingerprint::ZERO),
},
node_max,
@@ -250,7 +250,7 @@ pub fn decode(d: &mut MemDecoder<'_>) -> Arc<SerializedDepGraph> {
let node = &mut nodes[index];
// Make sure there's no duplicate indices in the dep graph.
assert!(node_header.node().kind != DepKind::NULL && node.kind == DepKind::NULL);
assert!(node_header.node().kind != DepKind::Null && node.kind == DepKind::Null);
*node = node_header.node();
value_fingerprints[index] = node_header.value_fingerprint();
@@ -287,7 +287,7 @@ pub fn decode(d: &mut MemDecoder<'_>) -> Arc<SerializedDepGraph> {
for (idx, node) in nodes.iter_enumerated() {
if index[node.kind.as_usize()].insert(node.key_fingerprint, idx).is_some() {
// Empty nodes and side effect nodes can have duplicates
if node.kind != DepKind::NULL && node.kind != DepKind::SIDE_EFFECT {
if node.kind != DepKind::Null && node.kind != DepKind::SideEffect {
let name = node.kind.name();
panic!(
"Error: A dep graph node ({name}) does not have an unique index. \
@@ -361,7 +361,7 @@ fn new(
) -> Self {
debug_assert_eq!(Self::TOTAL_BITS, Self::LEN_BITS + Self::WIDTH_BITS + Self::KIND_BITS);
let mut head = node.kind.as_inner();
let mut head = node.kind.as_u16();
let free_bytes = edge_max_index.leading_zeros() as usize / 8;
let bytes_per_index = (DEP_NODE_SIZE - free_bytes).saturating_sub(1);
@@ -408,7 +408,7 @@ fn unpack(&self) -> Unpacked {
Unpacked {
len: len.checked_sub(1),
bytes_per_index: bytes_per_index as usize + 1,
kind: DepKind::new(kind),
kind: DepKind::from_u16(kind),
index: SerializedDepNodeIndex::from_u32(index),
key_fingerprint: Fingerprint::from_le_bytes(key_fingerprint).into(),
value_fingerprint: Fingerprint::from_le_bytes(value_fingerprint),
+1 -3
View File
@@ -588,11 +588,9 @@ pub fn $name(self, value: $name::ProvidedValue<'tcx>) {
let tcx = self.tcx;
let erased_value = $name::provided_to_erased(tcx, value);
let dep_kind: dep_graph::DepKind = dep_graph::dep_kinds::$name;
$crate::query::inner::query_feed(
tcx,
dep_kind,
dep_graph::DepKind::$name,
&tcx.query_system.query_vtables.$name,
&tcx.query_system.caches.$name,
key,
@@ -13,7 +13,7 @@
use rustc_type_ir::lang_items::{SolverAdtLangItem, SolverLangItem, SolverTraitLangItem};
use rustc_type_ir::{CollectAndApply, Interner, TypeFoldable, search_graph};
use crate::dep_graph::DepNodeIndex;
use crate::dep_graph::{DepKind, DepNodeIndex};
use crate::infer::canonical::CanonicalVarKinds;
use crate::query::IntoQueryParam;
use crate::traits::cache::WithDepNode;
@@ -77,7 +77,7 @@ fn mk_external_constraints(
}
type DepNodeIndex = DepNodeIndex;
fn with_cached_task<T>(self, task: impl FnOnce() -> T) -> (T, DepNodeIndex) {
self.dep_graph.with_anon_task(self, crate::dep_graph::dep_kinds::TraitSelect, task)
self.dep_graph.with_anon_task(self, DepKind::TraitSelect, task)
}
type Ty = Ty<'tcx>;
type Tys = &'tcx List<Ty<'tcx>>;
+6 -8
View File
@@ -14,9 +14,7 @@
use rustc_middle::bug;
#[expect(unused_imports, reason = "used by doc comments")]
use rustc_middle::dep_graph::DepKindVTable;
use rustc_middle::dep_graph::{
self, DepNode, DepNodeIndex, DepNodeKey, SerializedDepNodeIndex, dep_kinds,
};
use rustc_middle::dep_graph::{DepKind, DepNode, DepNodeIndex, DepNodeKey, SerializedDepNodeIndex};
use rustc_middle::query::on_disk_cache::{
AbsoluteBytePos, CacheDecoder, CacheEncoder, EncodedDepNodeIndex,
};
@@ -123,7 +121,7 @@ pub fn collect_active_jobs_from_all_queries<'tcx>(
if complete { Ok(job_map_out) } else { Err(job_map_out) }
}
pub(super) fn try_mark_green<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepNode) -> bool {
pub(super) fn try_mark_green<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> bool {
tcx.dep_graph.try_mark_green(tcx, dep_node).is_some()
}
@@ -293,7 +291,7 @@ fn mk_query_stack_frame_extra<'tcx, Cache>(
} else {
description
};
let span = if vtable.dep_kind == dep_graph::dep_kinds::def_span || reduce_queries {
let span = if vtable.dep_kind == DepKind::def_span || reduce_queries {
// The `def_span` query is used to calculate `default_span`,
// so exit to avoid infinite recursion.
None
@@ -301,7 +299,7 @@ fn mk_query_stack_frame_extra<'tcx, Cache>(
Some(key.default_span(tcx))
};
let def_kind = if vtable.dep_kind == dep_graph::dep_kinds::def_kind || reduce_queries {
let def_kind = if vtable.dep_kind == DepKind::def_kind || reduce_queries {
// Try to avoid infinite recursion.
None
} else {
@@ -461,7 +459,7 @@ pub(crate) fn force_from_dep_node_inner<'tcx, C: QueryCache, const FLAGS: QueryF
// hit the cache instead of having to go through `force_from_dep_node`.
// This assertion makes sure, we actually keep applying the solution above.
debug_assert!(
dep_node.kind != dep_kinds::codegen_unit,
dep_node.kind != DepKind::codegen_unit,
"calling force_from_dep_node() on dep_kinds::codegen_unit"
);
@@ -567,7 +565,7 @@ pub(crate) fn make_query_vtable<'tcx>()
QueryVTable {
name: stringify!($name),
eval_always: is_eval_always!([$($modifiers)*]),
dep_kind: dep_graph::dep_kinds::$name,
dep_kind: dep_graph::DepKind::$name,
cycle_error_handling: cycle_error_handling!([$($modifiers)*]),
query_state: std::mem::offset_of!(QueryStates<'tcx>, $name),
query_cache: std::mem::offset_of!(QueryCaches<'tcx>, $name),
+7 -7
View File
@@ -7,7 +7,7 @@
use rustc_errors::{Applicability, MultiSpan, pluralize, struct_span_code_err};
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_middle::dep_graph::dep_kinds;
use rustc_middle::dep_graph::DepKind;
use rustc_middle::query::CycleError;
use rustc_middle::query::plumbing::CyclePlaceholder;
use rustc_middle::ty::{self, Representability, Ty, TyCtxt};
@@ -72,7 +72,7 @@ fn from_cycle_error(
let err = Ty::new_error(tcx, guar);
let arity = if let Some(info) = cycle_error.cycle.get(0)
&& info.frame.dep_kind == dep_kinds::fn_sig
&& info.frame.dep_kind == DepKind::fn_sig
&& let Some(def_id) = info.frame.def_id
&& let Some(node) = tcx.hir_get_if_local(def_id)
&& let Some(sig) = node.fn_sig()
@@ -106,7 +106,7 @@ fn from_cycle_error(
let mut item_and_field_ids = Vec::new();
let mut representable_ids = FxHashSet::default();
for info in &cycle_error.cycle {
if info.frame.dep_kind == dep_kinds::representability
if info.frame.dep_kind == DepKind::representability
&& let Some(field_id) = info.frame.def_id
&& let Some(field_id) = field_id.as_local()
&& let Some(DefKind::Field) = info.frame.info.def_kind
@@ -120,7 +120,7 @@ fn from_cycle_error(
}
}
for info in &cycle_error.cycle {
if info.frame.dep_kind == dep_kinds::representability_adt_ty
if info.frame.dep_kind == DepKind::representability_adt_ty
&& let Some(def_id) = info.frame.def_id_for_ty_in_cycle
&& let Some(def_id) = def_id.as_local()
&& !item_and_field_ids.iter().any(|&(id, _)| id == def_id)
@@ -163,7 +163,7 @@ fn from_cycle_error(
&cycle_error.cycle,
|cycle| {
if let Some(info) = cycle.get(0)
&& info.frame.dep_kind == dep_kinds::variances_of
&& info.frame.dep_kind == DepKind::variances_of
&& let Some(def_id) = info.frame.def_id
{
let n = tcx.generics_of(def_id).own_params.len();
@@ -210,7 +210,7 @@ fn from_cycle_error(
let diag = search_for_cycle_permutation(
&cycle_error.cycle,
|cycle| {
if cycle[0].frame.dep_kind == dep_kinds::layout_of
if cycle[0].frame.dep_kind == DepKind::layout_of
&& let Some(def_id) = cycle[0].frame.def_id_for_ty_in_cycle
&& let Some(def_id) = def_id.as_local()
&& let def_kind = tcx.def_kind(def_id)
@@ -235,7 +235,7 @@ fn from_cycle_error(
tcx.def_kind_descr(def_kind, def_id.to_def_id()),
);
for (i, info) in cycle.iter().enumerate() {
if info.frame.dep_kind != dep_kinds::layout_of {
if info.frame.dep_kind != DepKind::layout_of {
continue;
}
let Some(frame_def_id) = info.frame.def_id_for_ty_in_cycle else {
@@ -22,7 +22,7 @@
use rustc_infer::traits::{PredicateObligations, TraitObligation};
use rustc_macros::{TypeFoldable, TypeVisitable};
use rustc_middle::bug;
use rustc_middle::dep_graph::{DepNodeIndex, dep_kinds};
use rustc_middle::dep_graph::{DepKind, DepNodeIndex};
pub use rustc_middle::traits::select::*;
use rustc_middle::ty::abstract_const::NotConstEvaluatable;
use rustc_middle::ty::error::TypeErrorToStringExt;
@@ -1399,7 +1399,7 @@ fn in_task<OP, R>(&mut self, op: OP) -> (R, DepNodeIndex)
where
OP: FnOnce(&mut Self) -> R,
{
self.tcx().dep_graph.with_anon_task(self.tcx(), dep_kinds::TraitSelect, || op(self))
self.tcx().dep_graph.with_anon_task(self.tcx(), DepKind::TraitSelect, || op(self))
}
/// filter_impls filters candidates that have a positive impl for a negative