From 17c5b7a4b519ebc2c5ddeb3aa36ac7bec5ca9724 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Tue, 20 Jan 2026 20:28:35 +1100 Subject: [PATCH 1/3] Replace `make_dep_kind_name_array!` with a slice constant --- compiler/rustc_interface/src/passes.rs | 2 +- .../rustc_middle/src/dep_graph/dep_node.rs | 23 ++++++++----------- compiler/rustc_middle/src/dep_graph/mod.rs | 6 +++-- compiler/rustc_query_impl/src/plumbing.rs | 4 ---- 4 files changed, 14 insertions(+), 21 deletions(-) diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index 12a6a616d64f..1684b7535e68 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -940,7 +940,7 @@ pub fn create_and_enter_global_ctxt FnOnce(TyCtxt<'tcx>) -> T>( let outputs = util::build_output_filenames(&pre_configured_attrs, sess); - let dep_type = DepsType { dep_names: rustc_query_impl::dep_kind_names() }; + let dep_type = DepsType { dep_names: rustc_middle::dep_graph::DEP_KIND_NAMES }; let dep_graph = setup_dep_graph(sess, crate_name, stable_crate_id, &dep_type); let cstore = diff --git a/compiler/rustc_middle/src/dep_graph/dep_node.rs b/compiler/rustc_middle/src/dep_graph/dep_node.rs index 3ee1db67911f..a815a03c6458 100644 --- a/compiler/rustc_middle/src/dep_graph/dep_node.rs +++ b/compiler/rustc_middle/src/dep_graph/dep_node.rs @@ -24,15 +24,6 @@ macro_rules! define_dep_nodes { ($mod:ident) => {[ $($mod::$variant()),* ]}; } - #[macro_export] - macro_rules! make_dep_kind_name_array { - ($mod:ident) => { - vec! { - $(*$mod::$variant().name),* - } - }; - } - /// This enum serves as an index into arrays built by `make_dep_kind_array`. // This enum has more than u8::MAX variants so we need some kind of multi-byte // encoding. The derived Encodable/Decodable uses leb128 encoding which is @@ -68,20 +59,24 @@ macro_rules! define_dep_nodes { deps.len() as u16 }; + /// List containing the name of each dep kind as a static string, + /// indexable by `DepKind`. + pub const DEP_KIND_NAMES: &[&str] = &[ + $( self::label_strs::$variant, )* + ]; + pub(super) fn dep_kind_from_label_string(label: &str) -> Result { match label { - $(stringify!($variant) => Ok(dep_kinds::$variant),)* + $( self::label_strs::$variant => Ok(self::dep_kinds::$variant), )* _ => Err(()), } } /// Contains variant => str representations for constructing /// DepNode groups for tests. - #[allow(dead_code, non_upper_case_globals)] + #[expect(non_upper_case_globals)] pub mod label_strs { - $( - pub const $variant: &str = stringify!($variant); - )* + $( pub const $variant: &str = stringify!($variant); )* } }; } diff --git a/compiler/rustc_middle/src/dep_graph/mod.rs b/compiler/rustc_middle/src/dep_graph/mod.rs index b24ea4acc6b7..2fef7b5a9781 100644 --- a/compiler/rustc_middle/src/dep_graph/mod.rs +++ b/compiler/rustc_middle/src/dep_graph/mod.rs @@ -8,7 +8,9 @@ use crate::ty::{self, TyCtxt}; #[macro_use] mod dep_node; -pub use dep_node::{DepKind, DepNode, DepNodeExt, dep_kind_from_label, dep_kinds, label_strs}; +pub use dep_node::{ + DEP_KIND_NAMES, DepKind, DepNode, DepNodeExt, dep_kind_from_label, dep_kinds, label_strs, +}; pub(crate) use dep_node::{make_compile_codegen_unit, make_compile_mono_item, make_metadata}; pub use rustc_query_system::dep_graph::debug::{DepNodeFilter, EdgeFilter}; pub use rustc_query_system::dep_graph::{ @@ -22,7 +24,7 @@ pub type DepKindStruct<'tcx> = rustc_query_system::dep_graph::DepKindStruct, + pub dep_names: &'static [&'static str], } impl Deps for DepsType { diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index 67ab1114af62..d6d1dc781f3e 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -921,9 +921,5 @@ macro_rules! define_queries { pub fn query_callbacks<'tcx>(arena: &'tcx Arena<'tcx>) -> &'tcx [DepKindStruct<'tcx>] { arena.alloc_from_iter(rustc_middle::make_dep_kind_array!(query_callbacks)) } - - pub fn dep_kind_names() -> Vec<&'static str> { - rustc_middle::make_dep_kind_name_array!(query_callbacks) - } } } From 154255698ac2686a24b8604fff913cd8d4db9daf Mon Sep 17 00:00:00 2001 From: Zalathar Date: Tue, 20 Jan 2026 20:41:07 +1100 Subject: [PATCH 2/3] Make `Deps::name` lookup a non-self associated function The dep names needed here are statically available from `rustc_middle`. --- compiler/rustc_incremental/src/persist/load.rs | 10 +++------- compiler/rustc_interface/src/passes.rs | 4 +--- compiler/rustc_middle/src/dep_graph/dep_node.rs | 2 +- compiler/rustc_middle/src/dep_graph/mod.rs | 12 ++++-------- compiler/rustc_query_system/src/dep_graph/mod.rs | 2 +- .../rustc_query_system/src/dep_graph/serialized.rs | 6 +++--- 6 files changed, 13 insertions(+), 23 deletions(-) diff --git a/compiler/rustc_incremental/src/persist/load.rs b/compiler/rustc_incremental/src/persist/load.rs index 1b2a283a1a0d..e93cba2c8d0f 100644 --- a/compiler/rustc_incremental/src/persist/load.rs +++ b/compiler/rustc_incremental/src/persist/load.rs @@ -91,10 +91,7 @@ fn delete_dirty_work_product(sess: &Session, swp: SerializedWorkProduct) { work_product::delete_workproduct_files(sess, &swp.work_product); } -fn load_dep_graph( - sess: &Session, - deps: &DepsType, -) -> LoadResult<(Arc, WorkProductMap)> { +fn load_dep_graph(sess: &Session) -> LoadResult<(Arc, WorkProductMap)> { let prof = sess.prof.clone(); if sess.opts.incremental.is_none() { @@ -174,7 +171,7 @@ fn load_dep_graph( return LoadResult::DataOutOfDate; } - let dep_graph = SerializedDepGraph::decode::(&mut decoder, deps); + let dep_graph = SerializedDepGraph::decode::(&mut decoder); LoadResult::Ok { data: (dep_graph, prev_work_products) } } @@ -212,12 +209,11 @@ pub fn setup_dep_graph( sess: &Session, crate_name: Symbol, stable_crate_id: StableCrateId, - deps: &DepsType, ) -> DepGraph { // `load_dep_graph` can only be called after `prepare_session_directory`. prepare_session_directory(sess, crate_name, stable_crate_id); - let res = sess.opts.build_dep_graph().then(|| load_dep_graph(sess, deps)); + let res = sess.opts.build_dep_graph().then(|| load_dep_graph(sess)); if sess.opts.incremental.is_some() { sess.time("incr_comp_garbage_collect_session_directories", || { diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index 1684b7535e68..a7e94e1c0155 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -26,7 +26,6 @@ use rustc_lint::{BufferedEarlyLint, EarlyCheckNode, LintStore, unerased_lint_sto use rustc_metadata::EncodedMetadata; use rustc_metadata::creader::CStore; use rustc_middle::arena::Arena; -use rustc_middle::dep_graph::DepsType; use rustc_middle::ty::{self, CurrentGcx, GlobalCtxt, RegisteredTools, TyCtxt}; use rustc_middle::util::Providers; use rustc_parse::lexer::StripTokens; @@ -940,8 +939,7 @@ pub fn create_and_enter_global_ctxt FnOnce(TyCtxt<'tcx>) -> T>( let outputs = util::build_output_filenames(&pre_configured_attrs, sess); - let dep_type = DepsType { dep_names: rustc_middle::dep_graph::DEP_KIND_NAMES }; - let dep_graph = setup_dep_graph(sess, crate_name, stable_crate_id, &dep_type); + let dep_graph = setup_dep_graph(sess, crate_name, stable_crate_id); let cstore = FreezeLock::new(Box::new(CStore::new(compiler.codegen_backend.metadata_loader())) as _); diff --git a/compiler/rustc_middle/src/dep_graph/dep_node.rs b/compiler/rustc_middle/src/dep_graph/dep_node.rs index a815a03c6458..f5ed0570667c 100644 --- a/compiler/rustc_middle/src/dep_graph/dep_node.rs +++ b/compiler/rustc_middle/src/dep_graph/dep_node.rs @@ -61,7 +61,7 @@ macro_rules! define_dep_nodes { /// List containing the name of each dep kind as a static string, /// indexable by `DepKind`. - pub const DEP_KIND_NAMES: &[&str] = &[ + pub(crate) const DEP_KIND_NAMES: &[&str] = &[ $( self::label_strs::$variant, )* ]; diff --git a/compiler/rustc_middle/src/dep_graph/mod.rs b/compiler/rustc_middle/src/dep_graph/mod.rs index 2fef7b5a9781..c72f75b43dab 100644 --- a/compiler/rustc_middle/src/dep_graph/mod.rs +++ b/compiler/rustc_middle/src/dep_graph/mod.rs @@ -8,9 +8,7 @@ use crate::ty::{self, TyCtxt}; #[macro_use] mod dep_node; -pub use dep_node::{ - DEP_KIND_NAMES, DepKind, DepNode, DepNodeExt, dep_kind_from_label, dep_kinds, label_strs, -}; +pub use dep_node::{DepKind, DepNode, DepNodeExt, dep_kind_from_label, dep_kinds, label_strs}; pub(crate) use dep_node::{make_compile_codegen_unit, make_compile_mono_item, make_metadata}; pub use rustc_query_system::dep_graph::debug::{DepNodeFilter, EdgeFilter}; pub use rustc_query_system::dep_graph::{ @@ -23,9 +21,7 @@ pub type DepGraph = rustc_query_system::dep_graph::DepGraph; pub type DepKindStruct<'tcx> = rustc_query_system::dep_graph::DepKindStruct>; #[derive(Clone)] -pub struct DepsType { - pub dep_names: &'static [&'static str], -} +pub struct DepsType; impl Deps for DepsType { fn with_deps(task_deps: TaskDepsRef<'_>, op: OP) -> R @@ -49,8 +45,8 @@ impl Deps for DepsType { }) } - fn name(&self, dep_kind: DepKind) -> &'static str { - self.dep_names[dep_kind.as_usize()] + fn name(dep_kind: DepKind) -> &'static str { + dep_node::DEP_KIND_NAMES[dep_kind.as_usize()] } const DEP_KIND_NULL: DepKind = dep_kinds::Null; diff --git a/compiler/rustc_query_system/src/dep_graph/mod.rs b/compiler/rustc_query_system/src/dep_graph/mod.rs index d648415c9fc6..8b9e4fe1bf29 100644 --- a/compiler/rustc_query_system/src/dep_graph/mod.rs +++ b/compiler/rustc_query_system/src/dep_graph/mod.rs @@ -103,7 +103,7 @@ pub trait Deps: DynSync { where OP: for<'a> FnOnce(TaskDepsRef<'a>); - fn name(&self, dep_kind: DepKind) -> &'static str; + fn name(dep_kind: DepKind) -> &'static str; /// We use this for most things when incr. comp. is turned off. const DEP_KIND_NULL: DepKind; diff --git a/compiler/rustc_query_system/src/dep_graph/serialized.rs b/compiler/rustc_query_system/src/dep_graph/serialized.rs index 0012bf79a1f5..403394674a02 100644 --- a/compiler/rustc_query_system/src/dep_graph/serialized.rs +++ b/compiler/rustc_query_system/src/dep_graph/serialized.rs @@ -191,8 +191,8 @@ fn mask(bits: usize) -> usize { } impl SerializedDepGraph { - #[instrument(level = "debug", skip(d, deps))] - pub fn decode(d: &mut MemDecoder<'_>, deps: &D) -> Arc { + #[instrument(level = "debug", skip(d))] + pub fn decode(d: &mut MemDecoder<'_>) -> Arc { // The last 16 bytes are the node count and edge count. debug!("position: {:?}", d.position()); @@ -280,7 +280,7 @@ impl SerializedDepGraph { if index[node.kind.as_usize()].insert(node.hash, idx).is_some() { // Empty nodes and side effect nodes can have duplicates if node.kind != D::DEP_KIND_NULL && node.kind != D::DEP_KIND_SIDE_EFFECT { - let name = deps.name(node.kind); + let name = D::name(node.kind); panic!( "Error: A dep graph node ({name}) does not have an unique index. \ Running a clean build on a nightly compiler with `-Z incremental-verify-ich` \ From 395ca3be21385c7c7cdab029798f9d5e66fd567f Mon Sep 17 00:00:00 2001 From: Zalathar Date: Wed, 21 Jan 2026 11:57:21 +1100 Subject: [PATCH 3/3] Remove `#[derive(Clone)]` from `DepsType` --- compiler/rustc_middle/src/dep_graph/mod.rs | 1 - compiler/rustc_query_system/src/dep_graph/graph.rs | 12 +++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_middle/src/dep_graph/mod.rs b/compiler/rustc_middle/src/dep_graph/mod.rs index c72f75b43dab..049e868879e9 100644 --- a/compiler/rustc_middle/src/dep_graph/mod.rs +++ b/compiler/rustc_middle/src/dep_graph/mod.rs @@ -20,7 +20,6 @@ pub type DepGraph = rustc_query_system::dep_graph::DepGraph; pub type DepKindStruct<'tcx> = rustc_query_system::dep_graph::DepKindStruct>; -#[derive(Clone)] pub struct DepsType; impl Deps for DepsType { diff --git a/compiler/rustc_query_system/src/dep_graph/graph.rs b/compiler/rustc_query_system/src/dep_graph/graph.rs index f32f3f78c04b..f0cc9636b75c 100644 --- a/compiler/rustc_query_system/src/dep_graph/graph.rs +++ b/compiler/rustc_query_system/src/dep_graph/graph.rs @@ -28,7 +28,6 @@ use crate::dep_graph::edges::EdgesVec; use crate::ich::StableHashingContext; use crate::query::{QueryContext, QuerySideEffect}; -#[derive(Clone)] pub struct DepGraph { data: Option>>, @@ -39,6 +38,17 @@ pub struct DepGraph { virtual_dep_node_index: Arc, } +/// Manual clone impl that does not require `D: Clone`. +impl Clone for DepGraph { + fn clone(&self) -> Self { + let Self { data, virtual_dep_node_index } = self; + Self { + data: Option::>::clone(data), + virtual_dep_node_index: Arc::clone(virtual_dep_node_index), + } + } +} + rustc_index::newtype_index! { pub struct DepNodeIndex {} }