@@ -62,59 +62,30 @@ use crate::ich::StableHashingContext;
6262use crate :: mir:: mono:: MonoItem ;
6363use 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.
7166impl 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!
446403pub ( 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!
461418pub ( crate ) fn make_metadata ( tcx : TyCtxt < ' _ > ) -> DepNode {
462- DepNode :: construct ( tcx, dep_kinds :: Metadata , & ( ) )
419+ DepNode :: construct ( tcx, DepKind :: Metadata , & ( ) )
463420}
464421
465422impl DepNode {
0 commit comments