diff --git a/compiler/rustc_incremental/src/assert_dep_graph.rs b/compiler/rustc_incremental/src/assert_dep_graph.rs index a51b86eb72bf..945f1ab797e7 100644 --- a/compiler/rustc_incremental/src/assert_dep_graph.rs +++ b/compiler/rustc_incremental/src/assert_dep_graph.rs @@ -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) { diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 3c14fd859b98..8c4f2e4a36b1 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -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); } diff --git a/compiler/rustc_middle/src/dep_graph/dep_node.rs b/compiler/rustc_middle/src/dep_graph/dep_node.rs index 52faebcba025..2f637dfe0cbd 100644 --- a/compiler/rustc_middle/src/dep_graph/dep_node.rs +++ b/compiler/rustc_middle/src/dep_graph/dep_node.rs @@ -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 { 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 { diff --git a/compiler/rustc_middle/src/dep_graph/graph.rs b/compiler/rustc_middle/src/dep_graph/graph.rs index f5787716a94e..f54cd3c85fb4 100644 --- a/compiler/rustc_middle/src/dep_graph/graph.rs +++ b/compiler/rustc_middle/src/dep_graph/graph.rs @@ -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, diff --git a/compiler/rustc_middle/src/dep_graph/mod.rs b/compiler/rustc_middle/src/dep_graph/mod.rs index dda336fcd0b0..5df877a7eff0 100644 --- a/compiler/rustc_middle/src/dep_graph/mod.rs +++ b/compiler/rustc_middle/src/dep_graph/mod.rs @@ -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, diff --git a/compiler/rustc_middle/src/dep_graph/serialized.rs b/compiler/rustc_middle/src/dep_graph/serialized.rs index 4da96c5c9933..a22af8a296dd 100644 --- a/compiler/rustc_middle/src/dep_graph/serialized.rs +++ b/compiler/rustc_middle/src/dep_graph/serialized.rs @@ -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 { 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 { 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 { 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), diff --git a/compiler/rustc_middle/src/query/plumbing.rs b/compiler/rustc_middle/src/query/plumbing.rs index 47b6aea077d1..03618ee6acd4 100644 --- a/compiler/rustc_middle/src/query/plumbing.rs +++ b/compiler/rustc_middle/src/query/plumbing.rs @@ -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, diff --git a/compiler/rustc_middle/src/ty/context/impl_interner.rs b/compiler/rustc_middle/src/ty/context/impl_interner.rs index 7580cc65d530..a4aeaacbf05d 100644 --- a/compiler/rustc_middle/src/ty/context/impl_interner.rs +++ b/compiler/rustc_middle/src/ty/context/impl_interner.rs @@ -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(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>; diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index 5981e7e0f526..31f27efdb43f 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -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), diff --git a/compiler/rustc_query_impl/src/values.rs b/compiler/rustc_query_impl/src/values.rs index 67bc6893a320..8f55e98df867 100644 --- a/compiler/rustc_query_impl/src/values.rs +++ b/compiler/rustc_query_impl/src/values.rs @@ -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 { diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index 3c3160bc533e..c5a96fa963f8 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -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(&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