Skip to content

Commit 11ae63e

Browse files
committed
Auto merge of #152747 - nnethercote:bring-back-enum-DepKind, r=<try>
Bring back `enum DepKind`.
2 parents 1210e9f + 9b53ce9 commit 11ae63e

19 files changed

Lines changed: 79 additions & 145 deletions

File tree

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
@@ -70,59 +70,30 @@ use crate::ich::StableHashingContext;
7070
use crate::mir::mono::MonoItem;
7171
use crate::ty::{TyCtxt, tls};
7272

73-
/// This serves as an index into arrays built by `make_dep_kind_array`.
74-
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
75-
pub struct DepKind {
76-
variant: u16,
77-
}
78-
73+
// `enum DepKind` is generated by `define_dep_nodes!` below.
7974
impl DepKind {
8075
#[inline]
81-
pub const fn new(variant: u16) -> Self {
82-
Self { variant }
76+
pub(crate) fn from_u16(u: u16) -> Self {
77+
if u > Self::MAX {
78+
panic!("Invalid DepKind {u}");
79+
}
80+
// SAFETY: See comment on DEP_KIND_NUM_VARIANTS
81+
unsafe { std::mem::transmute(u) }
8382
}
8483

8584
#[inline]
86-
pub const fn as_inner(&self) -> u16 {
87-
self.variant
85+
pub(crate) const fn as_u16(&self) -> u16 {
86+
*self as u16
8887
}
8988

9089
#[inline]
9190
pub const fn as_usize(&self) -> usize {
92-
self.variant as usize
91+
*self as usize
9392
}
9493

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

12899
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
@@ -310,9 +281,6 @@ pub struct DepKindVTable<'tcx> {
310281

311282
/// Invoke a query to put the on-disk cached value in memory.
312283
pub try_load_from_on_disk_cache: Option<fn(TyCtxt<'tcx>, DepNode)>,
313-
314-
/// The name of this dep kind.
315-
pub name: &'static &'static str,
316284
}
317285

318286
/// A "work product" corresponds to a `.o` (or other) file that we
@@ -372,45 +340,32 @@ macro_rules! define_dep_nodes {
372340
// encoding. The derived Encodable/Decodable uses leb128 encoding which is
373341
// dense when only considering this enum. But DepKind is encoded in a larger
374342
// struct, and there we can take advantage of the unused bits in the u16.
343+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
375344
#[allow(non_camel_case_types)]
376-
#[repr(u16)] // Must be kept in sync with the inner type of `DepKind`.
377-
enum DepKindDefs {
345+
#[repr(u16)] // Must be kept in sync with the rest of `DepKind`.
346+
pub enum DepKind {
378347
$( $( #[$attr] )* $variant),*
379348
}
380349

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

405-
/// List containing the name of each dep kind as a static string,
406-
/// indexable by `DepKind`.
407-
pub(crate) const DEP_KIND_NAMES: &[&str] = &[
408-
$( self::label_strs::$variant, )*
409-
];
410-
411366
pub(super) fn dep_kind_from_label_string(label: &str) -> Result<DepKind, ()> {
412367
match label {
413-
$( self::label_strs::$variant => Ok(self::dep_kinds::$variant), )*
368+
$( stringify!($variant) => Ok(self::DepKind::$variant), )*
414369
_ => Err(()),
415370
}
416371
}
@@ -431,7 +386,9 @@ rustc_with_all_queries!(define_dep_nodes![
431386
[] fn Null() -> (),
432387
/// We use this to create a forever-red node.
433388
[] fn Red() -> (),
389+
/// We use this to create a side effect node.
434390
[] fn SideEffect() -> (),
391+
/// We use this to create the anon node with zero dependencies.
435392
[] fn AnonZeroDeps() -> (),
436393
[] fn TraitSelect() -> (),
437394
[] fn CompileCodegenUnit() -> (),
@@ -442,7 +399,7 @@ rustc_with_all_queries!(define_dep_nodes![
442399
// WARNING: `construct` is generic and does not know that `CompileCodegenUnit` takes `Symbol`s as keys.
443400
// Be very careful changing this type signature!
444401
pub(crate) fn make_compile_codegen_unit(tcx: TyCtxt<'_>, name: Symbol) -> DepNode {
445-
DepNode::construct(tcx, dep_kinds::CompileCodegenUnit, &name)
402+
DepNode::construct(tcx, DepKind::CompileCodegenUnit, &name)
446403
}
447404

448405
// WARNING: `construct` is generic and does not know that `CompileMonoItem` takes `MonoItem`s as keys.
@@ -451,13 +408,13 @@ pub(crate) fn make_compile_mono_item<'tcx>(
451408
tcx: TyCtxt<'tcx>,
452409
mono_item: &MonoItem<'tcx>,
453410
) -> DepNode {
454-
DepNode::construct(tcx, dep_kinds::CompileMonoItem, mono_item)
411+
DepNode::construct(tcx, DepKind::CompileMonoItem, mono_item)
455412
}
456413

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

463420
impl DepNode {

compiler/rustc_middle/src/dep_graph/graph.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,15 @@ 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, hash: current.anon_id_seed.into() },
145+
DepNode { kind: DepKind::AnonZeroDeps, hash: current.anon_id_seed.into() },
146146
EdgesVec::new(),
147147
Fingerprint::ZERO,
148148
);
149149
assert_eq!(_green_node_index, DepNodeIndex::SINGLETON_ZERO_DEPS_ANON_NODE);
150150

151151
// Instantiate a dependy-less red node only once for anonymous queries.
152152
let red_node_index = current.alloc_new_node(
153-
DepNode { kind: DepKind::RED, hash: Fingerprint::ZERO.into() },
153+
DepNode { kind: DepKind::Red, hash: Fingerprint::ZERO.into() },
154154
EdgesVec::new(),
155155
Fingerprint::ZERO,
156156
);
@@ -693,10 +693,7 @@ impl DepGraphData {
693693
fn encode_diagnostic<'tcx>(&self, tcx: TyCtxt<'tcx>, diagnostic: &DiagInner) -> DepNodeIndex {
694694
// Use `send_new` so we get an unique index, even though the dep node is not.
695695
let dep_node_index = self.current.encoder.send_new(
696-
DepNode {
697-
kind: DepKind::SIDE_EFFECT,
698-
hash: PackedFingerprint::from(Fingerprint::ZERO),
699-
},
696+
DepNode { kind: DepKind::SideEffect, hash: PackedFingerprint::from(Fingerprint::ZERO) },
700697
Fingerprint::ZERO,
701698
// We want the side effect node to always be red so it will be forced and emit the
702699
// diagnostic.
@@ -727,7 +724,7 @@ impl DepGraphData {
727724
prev_index,
728725
&self.colors,
729726
DepNode {
730-
kind: DepKind::SIDE_EFFECT,
727+
kind: DepKind::SideEffect,
731728
hash: PackedFingerprint::from(Fingerprint::ZERO),
732729
},
733730
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)