Skip to content

Commit f48ec0b

Browse files
authored
Rollup merge of #160406 - nnethercote:DepKind-cleanups, r=Zalathar
`DepKind` cleanups Details in individual commits. r? Zalathar
2 parents 45b9518 + a707cbf commit f48ec0b

5 files changed

Lines changed: 48 additions & 69 deletions

File tree

‎compiler/rustc_incremental/src/persist/clean.rs‎

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use rustc_hir::{
2727
Attribute, ImplItemKind, ItemKind as HirItem, Node as HirNode, TraitItemKind, find_attr,
2828
intravisit,
2929
};
30-
use rustc_middle::dep_graph::{DepNode, dep_kind_from_label, label_strs};
30+
use rustc_middle::dep_graph::{DepKind, DepNode, dep_kind_from_label};
3131
use rustc_middle::hir::nested_filter;
3232
use rustc_middle::ty::TyCtxt;
3333
use rustc_span::{Span, Symbol};
@@ -38,81 +38,78 @@ use crate::diagnostics;
3838
// Base and Extra labels to build up the labels
3939

4040
/// For typedef, constants, and statics
41-
const BASE_CONST: &[&str] = &[label_strs::type_of];
41+
const BASE_CONST: &[DepKind] = &[DepKind::type_of];
4242

4343
/// DepNodes for functions + methods
44-
const BASE_FN: &[&str] = &[
44+
const BASE_FN: &[DepKind] = &[
4545
// Callers will depend on the signature of these items, so we better test
46-
label_strs::fn_sig,
47-
label_strs::generics_of,
48-
label_strs::clauses_of,
49-
label_strs::type_of,
46+
DepKind::fn_sig,
47+
DepKind::generics_of,
48+
DepKind::clauses_of,
49+
DepKind::type_of,
5050
// And a big part of compilation (that we eventually want to cache) is type inference
5151
// information:
52-
label_strs::typeck_root,
52+
DepKind::typeck_root,
5353
];
5454

5555
/// DepNodes for Hir, which is pretty much everything
56-
const BASE_HIR: &[&str] = &[
56+
const BASE_HIR: &[DepKind] = &[
5757
// hir_owner should be computed for all nodes
58-
label_strs::hir_owner,
58+
DepKind::hir_owner,
5959
];
6060

6161
/// `impl` implementation of struct/trait
62-
const BASE_IMPL: &[&str] =
63-
&[label_strs::associated_item_def_ids, label_strs::generics_of, label_strs::impl_trait_header];
62+
const BASE_IMPL: &[DepKind] =
63+
&[DepKind::associated_item_def_ids, DepKind::generics_of, DepKind::impl_trait_header];
6464

6565
/// DepNodes for exported mir bodies, which is relevant in "executable"
6666
/// code, i.e., functions+methods
67-
const BASE_MIR: &[&str] = &[label_strs::optimized_mir, label_strs::promoted_mir];
67+
const BASE_MIR: &[DepKind] = &[DepKind::optimized_mir, DepKind::promoted_mir];
6868

6969
/// Struct, Enum and Union DepNodes
7070
///
7171
/// Note that changing the type of a field does not change the type of the struct or enum, but
7272
/// adding/removing fields or changing a fields name or visibility does.
73-
const BASE_STRUCT: &[&str] =
74-
&[label_strs::generics_of, label_strs::clauses_of, label_strs::type_of];
73+
const BASE_STRUCT: &[DepKind] = &[DepKind::generics_of, DepKind::clauses_of, DepKind::type_of];
7574

7675
/// Trait definition `DepNode`s.
7776
/// Extra `DepNode`s for functions and methods.
78-
const EXTRA_ASSOCIATED: &[&str] = &[label_strs::associated_item];
77+
const EXTRA_ASSOCIATED: &[DepKind] = &[DepKind::associated_item];
7978

80-
const EXTRA_TRAIT: &[&str] = &[];
79+
const EXTRA_TRAIT: &[DepKind] = &[];
8180

8281
// Fully Built Labels
8382

84-
const LABELS_CONST: &[&[&str]] = &[BASE_HIR, BASE_CONST];
83+
const LABELS_CONST: &[&[DepKind]] = &[BASE_HIR, BASE_CONST];
8584

8685
/// Constant/Typedef in an impl
87-
const LABELS_CONST_IN_IMPL: &[&[&str]] = &[BASE_HIR, BASE_CONST, EXTRA_ASSOCIATED];
86+
const LABELS_CONST_IN_IMPL: &[&[DepKind]] = &[BASE_HIR, BASE_CONST, EXTRA_ASSOCIATED];
8887

8988
/// Trait-Const/Typedef DepNodes
90-
const LABELS_CONST_IN_TRAIT: &[&[&str]] = &[BASE_HIR, BASE_CONST, EXTRA_ASSOCIATED, EXTRA_TRAIT];
89+
const LABELS_CONST_IN_TRAIT: &[&[DepKind]] = &[BASE_HIR, BASE_CONST, EXTRA_ASSOCIATED, EXTRA_TRAIT];
9190

9291
/// Function `DepNode`s.
93-
const LABELS_FN: &[&[&str]] = &[BASE_HIR, BASE_MIR, BASE_FN];
92+
const LABELS_FN: &[&[DepKind]] = &[BASE_HIR, BASE_MIR, BASE_FN];
9493

9594
/// Method `DepNode`s.
96-
const LABELS_FN_IN_IMPL: &[&[&str]] = &[BASE_HIR, BASE_MIR, BASE_FN, EXTRA_ASSOCIATED];
95+
const LABELS_FN_IN_IMPL: &[&[DepKind]] = &[BASE_HIR, BASE_MIR, BASE_FN, EXTRA_ASSOCIATED];
9796

9897
/// Trait method `DepNode`s.
99-
const LABELS_FN_IN_TRAIT: &[&[&str]] =
98+
const LABELS_FN_IN_TRAIT: &[&[DepKind]] =
10099
&[BASE_HIR, BASE_MIR, BASE_FN, EXTRA_ASSOCIATED, EXTRA_TRAIT];
101100

102101
/// For generic cases like inline-assembly, modules, etc.
103-
const LABELS_HIR_ONLY: &[&[&str]] = &[BASE_HIR];
102+
const LABELS_HIR_ONLY: &[&[DepKind]] = &[BASE_HIR];
104103

105104
/// Impl `DepNode`s.
106-
const LABELS_TRAIT: &[&[&str]] = &[
107-
BASE_HIR,
108-
&[label_strs::associated_item_def_ids, label_strs::clauses_of, label_strs::generics_of],
109-
];
105+
const LABELS_TRAIT: &[&[DepKind]] =
106+
&[BASE_HIR, &[DepKind::associated_item_def_ids, DepKind::clauses_of, DepKind::generics_of]];
110107

111108
/// Impl `DepNode`s.
112-
const LABELS_IMPL: &[&[&str]] = &[BASE_HIR, BASE_IMPL];
109+
const LABELS_IMPL: &[&[DepKind]] = &[BASE_HIR, BASE_IMPL];
113110

114111
/// Abstract data type (struct, enum, union) `DepNode`s.
115-
const LABELS_ADT: &[&[&str]] = &[BASE_HIR, BASE_STRUCT];
112+
const LABELS_ADT: &[&[DepKind]] = &[BASE_HIR, BASE_STRUCT];
116113

117114
// FIXME: Struct/Enum/Unions Fields (there is currently no way to attach these)
118115
//
@@ -289,7 +286,7 @@ impl<'tcx> CleanVisitor<'tcx> {
289286
.emit_fatal(diagnostics::UndefinedCleanDirty { span, kind: format!("{node:?}") }),
290287
};
291288
let labels =
292-
Labels::from_iter(labels.iter().flat_map(|s| s.iter().map(|l| (*l).to_string())));
289+
Labels::from_iter(labels.iter().flat_map(|s| s.iter().map(|l| format!("{l:?}"))));
293290
(name, labels)
294291
}
295292

‎compiler/rustc_middle/src/dep_graph/dep_node.rs‎

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ impl DepKind {
6969
if u > Self::MAX {
7070
panic!("Invalid DepKind {u}");
7171
}
72-
// SAFETY: See comment on DEP_KIND_NUM_VARIANTS
72+
// SAFETY: `DepKind` is `repr(u16)`, its variants are `0..=MAX`, and `u` was checked
73+
// against `MAX` above.
7374
unsafe { std::mem::transmute(u) }
7475
}
7576

@@ -83,9 +84,16 @@ impl DepKind {
8384
*self as usize
8485
}
8586

87+
/// The number of dep kind variants.
88+
pub(crate) const NUM_VARIANTS: usize = std::mem::variant_count::<DepKind>();
89+
8690
/// This is the highest value a `DepKind` can have. It's used during encoding to
87-
/// pack information into the unused bits.
88-
pub(crate) const MAX: u16 = DEP_KIND_NUM_VARIANTS - 1;
91+
/// pack information into the unused bits. u16 matches the `repr(u16)` on `DepKind`.
92+
pub(crate) const MAX: u16 = {
93+
let max = Self::NUM_VARIANTS - 1;
94+
assert!(max < u16::MAX as usize);
95+
max as u16
96+
};
8997
}
9098

9199
/// Combination of a [`DepKind`] and a key fingerprint that uniquely identifies
@@ -279,40 +287,15 @@ macro_rules! define_dep_nodes {
279287
$( $(#[$q_attr])* $q_name, )*
280288
}
281289

282-
// This computes the number of dep kind variants. Along the way, it sanity-checks that the
283-
// discriminants of the variants have been assigned consecutively from 0 so that they can
284-
// be used as a dense index, and that all discriminants fit in a `u16`.
285-
pub(crate) const DEP_KIND_NUM_VARIANTS: u16 = {
286-
let deps = &[
287-
$(DepKind::$nq_name,)*
288-
$(DepKind::$q_name,)*
289-
];
290-
let mut i = 0;
291-
while i < deps.len() {
292-
if i != deps[i].as_usize() {
293-
panic!();
294-
}
295-
i += 1;
296-
}
297-
assert!(deps.len() <= u16::MAX as usize);
298-
deps.len() as u16
299-
};
300-
301-
pub(super) fn dep_kind_from_label_string(label: &str) -> Result<DepKind, ()> {
290+
/// Converts a string to a `DepKind`. Used for handling attributes like `rustc_clean` that
291+
/// name dep kinds.
292+
fn dep_kind_from_label_string(label: &str) -> Result<DepKind, ()> {
302293
match label {
303294
$( stringify!($nq_name) => Ok(self::DepKind::$nq_name), )*
304295
$( stringify!($q_name) => Ok(self::DepKind::$q_name), )*
305296
_ => Err(()),
306297
}
307298
}
308-
309-
/// Contains variant => str representations for constructing
310-
/// DepNode groups for tests.
311-
#[expect(non_upper_case_globals)]
312-
pub mod label_strs {
313-
$( pub const $nq_name: &str = stringify!($nq_name); )*
314-
$( pub const $q_name: &str = stringify!($q_name); )*
315-
}
316299
};
317300
}
318301

‎compiler/rustc_middle/src/dep_graph/mod.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ use std::panic;
22

33
use tracing::instrument;
44

5-
pub use self::dep_node::{
6-
DepKind, DepKindVTable, DepNode, WorkProductId, dep_kind_from_label, label_strs,
7-
};
5+
pub use self::dep_node::{DepKind, DepKindVTable, DepNode, WorkProductId, dep_kind_from_label};
86
pub use self::dep_node_key::DepNodeKey;
97
pub use self::graph::{
108
DepGraph, DepGraphData, DepNodeIndex, QuerySideEffect, TaskDepsRef, WorkProduct,

‎compiler/rustc_middle/src/dep_graph/serialized.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,9 @@ impl SerializedDepGraph {
387387

388388
// Read the number of nodes of each dep kind, and perform
389389
// counting sort for `LazyNodeIndex`.
390-
let mut kinds = Vec::with_capacity(DepKind::MAX as usize + 1);
390+
let mut kinds = Vec::with_capacity(DepKind::NUM_VARIANTS);
391391
let mut offset = 0u32;
392-
for _ in 0..(DepKind::MAX + 1) {
392+
for _ in 0..(DepKind::NUM_VARIANTS) {
393393
let len = d.read_u32();
394394
kinds.push(LazyKindIndex { start: offset, len, map: OnceLock::new() });
395395
offset += len;
@@ -654,7 +654,7 @@ impl EncoderState {
654654
edge_count: 0,
655655
node_count: 0,
656656
encoder: MemEncoder::new(),
657-
kind_stats: iter::repeat_n(0, DepKind::MAX as usize + 1).collect(),
657+
kind_stats: iter::repeat_n(0, DepKind::NUM_VARIANTS).collect(),
658658
})
659659
}),
660660
}
@@ -792,7 +792,7 @@ impl EncoderState {
792792

793793
let mut encoder = self.file.lock().take().unwrap();
794794

795-
let mut kind_stats: Vec<u32> = iter::repeat_n(0, DepKind::MAX as usize + 1).collect();
795+
let mut kind_stats: Vec<u32> = iter::repeat_n(0, DepKind::NUM_VARIANTS).collect();
796796

797797
let mut node_max = 0;
798798
let mut node_count = 0;

‎compiler/rustc_middle/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#![feature(try_trait_v2_residual)]
5757
#![feature(try_trait_v2_yeet)]
5858
#![feature(type_alias_impl_trait)]
59+
#![feature(variant_count)]
5960
#![feature(yeet_expr)]
6061
#![recursion_limit = "256"]
6162
// tidy-alphabetical-end

0 commit comments

Comments
 (0)