Skip to content

Commit 59fd4ef

Browse files
committed
Auto merge of #152747 - nnethercote:bring-back-enum-DepKind, r=Zalathar
Bring back `enum DepKind`. *[View all comments](https://triagebot.infra.rust-lang.org/gh-comments/rust-lang/rust/pull/152747)* 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. r? @Zalathar
2 parents ef70767 + 1c8abe6 commit 59fd4ef

File tree

19 files changed

+79
-144
lines changed

19 files changed

+79
-144
lines changed

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ impl CrateInfo {
911911
.rev()
912912
.copied()
913913
.filter(|&cnum| {
914-
let link = !tcx.dep_kind(cnum).macros_only();
914+
let link = !tcx.crate_dep_kind(cnum).macros_only();
915915
if link && tcx.is_compiler_builtins(cnum) {
916916
compiler_builtins = Some(cnum);
917917
return false;

compiler/rustc_incremental/src/assert_dep_graph.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ use rustc_hir::attrs::AttributeKind;
4444
use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId};
4545
use rustc_hir::intravisit::{self, Visitor};
4646
use rustc_middle::bug;
47-
use rustc_middle::dep_graph::{
48-
DepGraphQuery, DepKind, DepNode, DepNodeFilter, EdgeFilter, dep_kinds,
49-
};
47+
use rustc_middle::dep_graph::{DepGraphQuery, DepKind, DepNode, DepNodeFilter, EdgeFilter};
5048
use rustc_middle::hir::nested_filter;
5149
use rustc_middle::ty::TyCtxt;
5250
use rustc_span::{Span, Symbol, sym};
@@ -117,7 +115,7 @@ impl<'tcx> IfThisChanged<'tcx> {
117115
None => DepNode::from_def_path_hash(
118116
self.tcx,
119117
def_path_hash,
120-
dep_kinds::opt_hir_owner_nodes,
118+
DepKind::opt_hir_owner_nodes,
121119
),
122120
Some(n) => {
123121
match DepNode::from_label_string(self.tcx, n.as_str(), def_path_hash) {

compiler/rustc_incremental/src/persist/clean.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//! - `#[rustc_clean(cfg="rev2")]` same as above, except that the
1010
//! fingerprints must be the SAME (along with all other fingerprints).
1111
//!
12-
//! - `#[rustc_clean(cfg="rev2", loaded_from_disk='typeck")]` asserts that
12+
//! - `#[rustc_clean(cfg="rev2", loaded_from_disk="typeck")]` asserts that
1313
//! the query result for `DepNode::typeck(X)` was actually
1414
//! loaded from disk (not just marked green). This can be useful
1515
//! to ensure that a test is actually exercising the deserialization

compiler/rustc_metadata/src/dependency_format.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
146146
&& !sess.target.crt_static_allows_dylibs)
147147
{
148148
for &cnum in tcx.crates(()).iter() {
149-
if tcx.dep_kind(cnum).macros_only() {
149+
if tcx.crate_dep_kind(cnum).macros_only() {
150150
continue;
151151
}
152152
let src = tcx.used_crate_source(cnum);
@@ -163,7 +163,7 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
163163

164164
let all_dylibs = || {
165165
tcx.crates(()).iter().filter(|&&cnum| {
166-
!tcx.dep_kind(cnum).macros_only()
166+
!tcx.crate_dep_kind(cnum).macros_only()
167167
&& (tcx.used_crate_source(cnum).dylib.is_some()
168168
|| tcx.used_crate_source(cnum).sdylib_interface.is_some())
169169
})
@@ -241,7 +241,7 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
241241
let src = tcx.used_crate_source(cnum);
242242
if src.dylib.is_none()
243243
&& !formats.contains_key(&cnum)
244-
&& tcx.dep_kind(cnum) == CrateDepKind::Unconditional
244+
&& tcx.crate_dep_kind(cnum) == CrateDepKind::Unconditional
245245
{
246246
assert!(src.rlib.is_some() || src.rmeta.is_some());
247247
info!("adding staticlib: {}", tcx.crate_name(cnum));
@@ -333,7 +333,7 @@ fn attempt_static(tcx: TyCtxt<'_>, unavailable: &mut Vec<CrateNum>) -> Option<De
333333
.iter()
334334
.copied()
335335
.filter_map(|cnum| {
336-
if tcx.dep_kind(cnum).macros_only() {
336+
if tcx.crate_dep_kind(cnum).macros_only() {
337337
return None;
338338
}
339339
let is_rlib = tcx.used_crate_source(cnum).rlib.is_some();
@@ -353,7 +353,7 @@ fn attempt_static(tcx: TyCtxt<'_>, unavailable: &mut Vec<CrateNum>) -> Option<De
353353
assert_eq!(ret.push(Linkage::Static), LOCAL_CRATE);
354354
for &cnum in tcx.crates(()) {
355355
assert_eq!(
356-
ret.push(match tcx.dep_kind(cnum) {
356+
ret.push(match tcx.crate_dep_kind(cnum) {
357357
CrateDepKind::Unconditional => Linkage::Static,
358358
CrateDepKind::MacrosOnly | CrateDepKind::Conditional => Linkage::NotLinked,
359359
}),

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ macro_rules! provide_one {
147147
// External query providers call `crate_hash` in order to register a dependency
148148
// on the crate metadata. The exception is `crate_hash` itself, which obviously
149149
// doesn't need to do this (and can't, as it would cause a query cycle).
150-
use rustc_middle::dep_graph::dep_kinds;
151-
if dep_kinds::$name != dep_kinds::crate_hash && $tcx.dep_graph.is_fully_enabled() {
150+
use rustc_middle::dep_graph::DepKind;
151+
if DepKind::$name != DepKind::crate_hash && $tcx.dep_graph.is_fully_enabled() {
152152
$tcx.ensure_ok().crate_hash($def_id.krate);
153153
}
154154

@@ -382,7 +382,7 @@ provide! { tcx, def_id, other, cdata,
382382
implementations_of_trait => { cdata.get_implementations_of_trait(tcx, other) }
383383
crate_incoherent_impls => { cdata.get_incoherent_impls(tcx, other) }
384384

385-
dep_kind => { cdata.dep_kind }
385+
crate_dep_kind => { cdata.dep_kind }
386386
module_children => {
387387
tcx.arena.alloc_from_iter(cdata.get_module_children(tcx, def_id.index))
388388
}

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2072,7 +2072,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
20722072
name: self.tcx.crate_name(cnum),
20732073
hash: self.tcx.crate_hash(cnum),
20742074
host_hash: self.tcx.crate_host_hash(cnum),
2075-
kind: self.tcx.dep_kind(cnum),
2075+
kind: self.tcx.crate_dep_kind(cnum),
20762076
extra_filename: self.tcx.extra_filename(cnum).clone(),
20772077
is_private: self.tcx.is_private_dep(cnum),
20782078
};

compiler/rustc_middle/src/dep_graph/dep_node.rs

Lines changed: 26 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -62,59 +62,30 @@ use crate::ich::StableHashingContext;
6262
use crate::mir::mono::MonoItem;
6363
use crate::ty::{TyCtxt, tls};
6464

65-
/// This serves as an index into arrays built by `make_dep_kind_array`.
66-
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
67-
pub struct DepKind {
68-
variant: u16,
69-
}
70-
65+
// `enum DepKind` is generated by `define_dep_nodes!` below.
7166
impl DepKind {
7267
#[inline]
73-
pub const fn new(variant: u16) -> Self {
74-
Self { variant }
68+
pub(crate) fn from_u16(u: u16) -> Self {
69+
if u > Self::MAX {
70+
panic!("Invalid DepKind {u}");
71+
}
72+
// SAFETY: See comment on DEP_KIND_NUM_VARIANTS
73+
unsafe { std::mem::transmute(u) }
7574
}
7675

7776
#[inline]
78-
pub const fn as_inner(&self) -> u16 {
79-
self.variant
77+
pub(crate) const fn as_u16(&self) -> u16 {
78+
*self as u16
8079
}
8180

8281
#[inline]
8382
pub const fn as_usize(&self) -> usize {
84-
self.variant as usize
83+
*self as usize
8584
}
8685

87-
pub(crate) fn name(self) -> &'static str {
88-
DEP_KIND_NAMES[self.as_usize()]
89-
}
90-
91-
/// We use this for most things when incr. comp. is turned off.
92-
pub(crate) const NULL: DepKind = dep_kinds::Null;
93-
94-
/// We use this to create a forever-red node.
95-
pub(crate) const RED: DepKind = dep_kinds::Red;
96-
97-
/// We use this to create a side effect node.
98-
pub(crate) const SIDE_EFFECT: DepKind = dep_kinds::SideEffect;
99-
100-
/// We use this to create the anon node with zero dependencies.
101-
pub(crate) const ANON_ZERO_DEPS: DepKind = dep_kinds::AnonZeroDeps;
102-
10386
/// This is the highest value a `DepKind` can have. It's used during encoding to
10487
/// pack information into the unused bits.
105-
pub(crate) const MAX: u16 = DEP_KIND_VARIANTS - 1;
106-
}
107-
108-
impl fmt::Debug for DepKind {
109-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110-
tls::with_opt(|opt_tcx| {
111-
if let Some(tcx) = opt_tcx {
112-
write!(f, "{}", tcx.dep_kind_vtable(*self).name)
113-
} else {
114-
f.debug_struct("DepKind").field("variant", &self.variant).finish()
115-
}
116-
})
117-
}
88+
pub(crate) const MAX: u16 = DEP_KIND_NUM_VARIANTS - 1;
11889
}
11990

12091
/// Combination of a [`DepKind`] and a key fingerprint that uniquely identifies
@@ -312,9 +283,6 @@ pub struct DepKindVTable<'tcx> {
312283

313284
/// Invoke a query to put the on-disk cached value in memory.
314285
pub try_load_from_on_disk_cache: Option<fn(TyCtxt<'tcx>, DepNode)>,
315-
316-
/// The name of this dep kind.
317-
pub name: &'static &'static str,
318286
}
319287

320288
/// A "work product" corresponds to a `.o` (or other) file that we
@@ -374,45 +342,32 @@ macro_rules! define_dep_nodes {
374342
// encoding. The derived Encodable/Decodable uses leb128 encoding which is
375343
// dense when only considering this enum. But DepKind is encoded in a larger
376344
// struct, and there we can take advantage of the unused bits in the u16.
345+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
377346
#[allow(non_camel_case_types)]
378-
#[repr(u16)] // Must be kept in sync with the inner type of `DepKind`.
379-
enum DepKindDefs {
347+
#[repr(u16)] // Must be kept in sync with the rest of `DepKind`.
348+
pub enum DepKind {
380349
$( $( #[$attr] )* $variant),*
381350
}
382351

383-
#[allow(non_upper_case_globals)]
384-
pub mod dep_kinds {
385-
use super::*;
386-
387-
$(
388-
// The `as u16` cast must be kept in sync with the inner type of `DepKind`.
389-
pub const $variant: DepKind = DepKind::new(DepKindDefs::$variant as u16);
390-
)*
391-
}
392-
393-
// This checks that the discriminants of the variants have been assigned consecutively
394-
// from 0 so that they can be used as a dense index.
395-
pub(crate) const DEP_KIND_VARIANTS: u16 = {
396-
let deps = &[$(dep_kinds::$variant,)*];
352+
// This computes the number of dep kind variants. Along the way, it sanity-checks that the
353+
// discriminants of the variants have been assigned consecutively from 0 so that they can
354+
// be used as a dense index, and that all discriminants fit in a `u16`.
355+
pub(crate) const DEP_KIND_NUM_VARIANTS: u16 = {
356+
let deps = &[$(DepKind::$variant,)*];
397357
let mut i = 0;
398358
while i < deps.len() {
399359
if i != deps[i].as_usize() {
400360
panic!();
401361
}
402362
i += 1;
403363
}
364+
assert!(deps.len() <= u16::MAX as usize);
404365
deps.len() as u16
405366
};
406367

407-
/// List containing the name of each dep kind as a static string,
408-
/// indexable by `DepKind`.
409-
pub(crate) const DEP_KIND_NAMES: &[&str] = &[
410-
$( self::label_strs::$variant, )*
411-
];
412-
413368
pub(super) fn dep_kind_from_label_string(label: &str) -> Result<DepKind, ()> {
414369
match label {
415-
$( self::label_strs::$variant => Ok(self::dep_kinds::$variant), )*
370+
$( stringify!($variant) => Ok(self::DepKind::$variant), )*
416371
_ => Err(()),
417372
}
418373
}
@@ -433,7 +388,9 @@ rustc_with_all_queries!(define_dep_nodes![
433388
[] fn Null() -> (),
434389
/// We use this to create a forever-red node.
435390
[] fn Red() -> (),
391+
/// We use this to create a side effect node.
436392
[] fn SideEffect() -> (),
393+
/// We use this to create the anon node with zero dependencies.
437394
[] fn AnonZeroDeps() -> (),
438395
[] fn TraitSelect() -> (),
439396
[] fn CompileCodegenUnit() -> (),
@@ -444,7 +401,7 @@ rustc_with_all_queries!(define_dep_nodes![
444401
// WARNING: `construct` is generic and does not know that `CompileCodegenUnit` takes `Symbol`s as keys.
445402
// Be very careful changing this type signature!
446403
pub(crate) fn make_compile_codegen_unit(tcx: TyCtxt<'_>, name: Symbol) -> DepNode {
447-
DepNode::construct(tcx, dep_kinds::CompileCodegenUnit, &name)
404+
DepNode::construct(tcx, DepKind::CompileCodegenUnit, &name)
448405
}
449406

450407
// WARNING: `construct` is generic and does not know that `CompileMonoItem` takes `MonoItem`s as keys.
@@ -453,13 +410,13 @@ pub(crate) fn make_compile_mono_item<'tcx>(
453410
tcx: TyCtxt<'tcx>,
454411
mono_item: &MonoItem<'tcx>,
455412
) -> DepNode {
456-
DepNode::construct(tcx, dep_kinds::CompileMonoItem, mono_item)
413+
DepNode::construct(tcx, DepKind::CompileMonoItem, mono_item)
457414
}
458415

459416
// WARNING: `construct` is generic and does not know that `Metadata` takes `()`s as keys.
460417
// Be very careful changing this type signature!
461418
pub(crate) fn make_metadata(tcx: TyCtxt<'_>) -> DepNode {
462-
DepNode::construct(tcx, dep_kinds::Metadata, &())
419+
DepNode::construct(tcx, DepKind::Metadata, &())
463420
}
464421

465422
impl DepNode {

compiler/rustc_middle/src/dep_graph/graph.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl DepGraph {
142142

143143
// Instantiate a node with zero dependencies only once for anonymous queries.
144144
let _green_node_index = current.alloc_new_node(
145-
DepNode { kind: DepKind::ANON_ZERO_DEPS, key_fingerprint: current.anon_id_seed.into() },
145+
DepNode { kind: DepKind::AnonZeroDeps, key_fingerprint: current.anon_id_seed.into() },
146146
EdgesVec::new(),
147147
Fingerprint::ZERO,
148148
);
@@ -152,7 +152,7 @@ impl DepGraph {
152152
// Other nodes can use the always-red node as a fake dependency, to
153153
// ensure that their dependency list will never be all-green.
154154
let red_node_index = current.alloc_new_node(
155-
DepNode { kind: DepKind::RED, key_fingerprint: Fingerprint::ZERO.into() },
155+
DepNode { kind: DepKind::Red, key_fingerprint: Fingerprint::ZERO.into() },
156156
EdgesVec::new(),
157157
Fingerprint::ZERO,
158158
);
@@ -680,7 +680,7 @@ impl DepGraphData {
680680
// Use `send_new` so we get an unique index, even though the dep node is not.
681681
let dep_node_index = self.current.encoder.send_new(
682682
DepNode {
683-
kind: DepKind::SIDE_EFFECT,
683+
kind: DepKind::SideEffect,
684684
key_fingerprint: PackedFingerprint::from(Fingerprint::ZERO),
685685
},
686686
Fingerprint::ZERO,
@@ -713,7 +713,7 @@ impl DepGraphData {
713713
prev_index,
714714
&self.colors,
715715
DepNode {
716-
kind: DepKind::SIDE_EFFECT,
716+
kind: DepKind::SideEffect,
717717
key_fingerprint: PackedFingerprint::from(Fingerprint::ZERO),
718718
},
719719
Fingerprint::ZERO,

compiler/rustc_middle/src/dep_graph/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ use std::panic;
33
use tracing::instrument;
44

55
pub use self::dep_node::{
6-
DepKind, DepKindVTable, DepNode, DepNodeKey, WorkProductId, dep_kind_from_label, dep_kinds,
7-
label_strs,
6+
DepKind, DepKindVTable, DepNode, DepNodeKey, WorkProductId, dep_kind_from_label, label_strs,
87
};
98
pub use self::graph::{
109
DepGraph, DepGraphData, DepNodeIndex, QuerySideEffect, TaskDepsRef, WorkProduct,
@@ -18,7 +17,7 @@ use crate::ty::print::with_reduced_queries;
1817
use crate::ty::{self, TyCtxt};
1918

2019
mod debug;
21-
pub mod dep_node;
20+
pub(crate) mod dep_node;
2221
mod dep_node_key;
2322
mod edges;
2423
mod graph;

0 commit comments

Comments
 (0)