Skip to content

Commit b93eefa

Browse files
committed
Some tweaks and addressing of comments
1 parent aa95dcf commit b93eefa

File tree

5 files changed

+51
-41
lines changed

5 files changed

+51
-41
lines changed

compiler/rustc_interface/src/callbacks.rs

+38-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
//! origin crate when the `TyCtxt` is not present in TLS.
1111
1212
use rustc_errors::{Diagnostic, TRACK_DIAGNOSTICS};
13-
use rustc_middle::dep_graph::{dep_kind_debug, dep_node_debug, TaskDepsRef};
13+
use rustc_middle::dep_graph::{DepNodeExt, TaskDepsRef};
1414
use rustc_middle::ty::tls;
15+
use rustc_query_system::dep_graph::dep_node::default_dep_kind_debug;
16+
use rustc_query_system::dep_graph::{DepContext, DepKind, DepNode};
1517
use std::fmt;
1618

1719
fn track_span_parent(def_id: rustc_span::def_id::LocalDefId) {
@@ -59,6 +61,41 @@ fn def_id_debug(def_id: rustc_hir::def_id::DefId, f: &mut fmt::Formatter<'_>) ->
5961
write!(f, ")")
6062
}
6163

64+
/// This is a callback from `rustc_query_system` as it cannot access the implicit state
65+
/// in `rustc_middle` otherwise.
66+
pub fn dep_kind_debug(kind: DepKind, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67+
tls::with_opt(|opt_tcx| {
68+
if let Some(tcx) = opt_tcx {
69+
write!(f, "{}", tcx.dep_kind_info(kind).name)
70+
} else {
71+
default_dep_kind_debug(kind, f)
72+
}
73+
})
74+
}
75+
76+
/// This is a callback from `rustc_query_system` as it cannot access the implicit state
77+
/// in `rustc_middle` otherwise.
78+
pub fn dep_node_debug(node: DepNode, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
79+
write!(f, "{:?}(", node.kind)?;
80+
81+
tls::with_opt(|opt_tcx| {
82+
if let Some(tcx) = opt_tcx {
83+
if let Some(def_id) = node.extract_def_id(tcx) {
84+
write!(f, "{}", tcx.def_path_debug_str(def_id))?;
85+
} else if let Some(ref s) = tcx.dep_graph.dep_node_debug_str(node) {
86+
write!(f, "{s}")?;
87+
} else {
88+
write!(f, "{}", node.hash)?;
89+
}
90+
} else {
91+
write!(f, "{}", node.hash)?;
92+
}
93+
Ok(())
94+
})?;
95+
96+
write!(f, ")")
97+
}
98+
6299
/// Sets up the callbacks in prior crates which we want to refer to the
63100
/// TyCtxt in.
64101
pub fn setup_callbacks() {

compiler/rustc_middle/src/dep_graph/dep_node.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ use rustc_hir::definitions::DefPathHash;
6565
use rustc_hir::{HirId, ItemLocalId, OwnerId};
6666
use rustc_query_system::dep_graph::FingerprintStyle;
6767
use rustc_span::symbol::Symbol;
68-
use std::hash::Hash;
6968

7069
pub use rustc_query_system::dep_graph::dep_node::DepKind;
7170
pub use rustc_query_system::dep_graph::{DepContext, DepNode, DepNodeParams};
@@ -85,9 +84,8 @@ macro_rules! define_dep_nodes {
8584
// encoding. The derived Encodable/Decodable uses leb128 encoding which is
8685
// dense when only considering this enum. But DepKind is encoded in a larger
8786
// struct, and there we can take advantage of the unused bits in the u16.
88-
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
8987
#[allow(non_camel_case_types)]
90-
#[repr(u16)]
88+
#[repr(u16)] // Must be kept in sync with the inner type of `DepKind`.
9189
enum DepKindDefs {
9290
$( $( #[$attr] )* $variant),*
9391
}
@@ -97,14 +95,15 @@ macro_rules! define_dep_nodes {
9795
use super::*;
9896

9997
$(
98+
// The `as u16` cast must be kept in sync with the inner type of `DepKind`.
10099
pub const $variant: DepKind = DepKind::new(DepKindDefs::$variant as u16);
101100
)*
102101
}
103102

104103
// This checks that the discriminants of the variants have been assigned consecutively
105104
// from 0 so that they can be used as a dense index.
106105
pub const DEP_KIND_VARIANTS: u16 = {
107-
let deps: &[DepKind] = &[$(dep_kinds::$variant,)*];
106+
let deps = &[$(dep_kinds::$variant,)*];
108107
let mut i = 0;
109108
while i < deps.len() {
110109
if i != deps[i].as_usize() {
@@ -143,8 +142,6 @@ rustc_query_append!(define_dep_nodes![
143142
[] fn CompileMonoItem() -> (),
144143
]);
145144

146-
static_assert_size!(DepKind, 2);
147-
148145
// WARNING: `construct` is generic and does not know that `CompileCodegenUnit` takes `Symbol`s as keys.
149146
// Be very careful changing this type signature!
150147
pub(crate) fn make_compile_codegen_unit(tcx: TyCtxt<'_>, name: Symbol) -> DepNode {

compiler/rustc_middle/src/dep_graph/mod.rs

-32
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use rustc_session::Session;
77
mod dep_node;
88

99
pub use rustc_query_system::dep_graph::debug::EdgeFilter;
10-
use rustc_query_system::dep_graph::dep_node::default_dep_kind_debug;
1110
pub use rustc_query_system::dep_graph::{
1211
debug::DepNodeFilter, hash_result, DepContext, DepGraphQuery, DepNodeColor, DepNodeIndex, Deps,
1312
SerializedDepGraph, SerializedDepNodeIndex, TaskDeps, TaskDepsRef, WorkProduct, WorkProductId,
@@ -79,34 +78,3 @@ impl<'tcx> DepContext for TyCtxt<'tcx> {
7978
&self.query_kinds[dk.as_usize()]
8079
}
8180
}
82-
83-
pub fn dep_kind_debug(kind: DepKind, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
84-
ty::tls::with_opt(|opt_tcx| {
85-
if let Some(tcx) = opt_tcx {
86-
write!(f, "{}", tcx.dep_kind_info(kind).name)
87-
} else {
88-
default_dep_kind_debug(kind, f)
89-
}
90-
})
91-
}
92-
93-
pub fn dep_node_debug(node: DepNode, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
94-
write!(f, "{:?}(", node.kind)?;
95-
96-
ty::tls::with_opt(|opt_tcx| {
97-
if let Some(tcx) = opt_tcx {
98-
if let Some(def_id) = node.extract_def_id(tcx) {
99-
write!(f, "{}", tcx.def_path_debug_str(def_id))?;
100-
} else if let Some(ref s) = tcx.dep_graph.dep_node_debug_str(node) {
101-
write!(f, "{s}")?;
102-
} else {
103-
write!(f, "{}", node.hash)?;
104-
}
105-
} else {
106-
write!(f, "{}", node.hash)?;
107-
}
108-
Ok(())
109-
})?;
110-
111-
write!(f, ")")
112-
}

compiler/rustc_query_system/src/dep_graph/dep_node.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use std::fmt;
5353
use std::hash::Hash;
5454

5555
/// This serves as an index into arrays built by `make_dep_kind_array`.
56-
#[derive(Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Encodable, Decodable)]
56+
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
5757
pub struct DepKind {
5858
variant: u16,
5959
}
@@ -75,6 +75,8 @@ impl DepKind {
7575
}
7676
}
7777

78+
static_assert_size!(DepKind, 2);
79+
7880
pub fn default_dep_kind_debug(kind: DepKind, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7981
f.debug_struct("DepKind").field("variant", &kind.variant).finish()
8082
}
@@ -88,7 +90,7 @@ impl fmt::Debug for DepKind {
8890
}
8991
}
9092

91-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
93+
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
9294
pub struct DepNode {
9395
pub kind: DepKind,
9496
pub hash: PackedFingerprint,

compiler/rustc_query_system/src/dep_graph/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,14 @@ pub trait Deps {
9494
where
9595
OP: for<'a> FnOnce(TaskDepsRef<'a>);
9696

97+
/// We use this for most things when incr. comp. is turned off.
9798
const DEP_KIND_NULL: DepKind;
99+
100+
/// We use this to create a forever-red node.
98101
const DEP_KIND_RED: DepKind;
102+
103+
/// This is the highest value a `DepKind` can have. It's used during encoding to
104+
/// pack information into the unused bits.
99105
const DEP_KIND_MAX: u16;
100106
}
101107

0 commit comments

Comments
 (0)