@@ -62,59 +62,34 @@ 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
8786 pub ( crate ) fn name ( self ) -> & ' static str {
8887 DEP_KIND_NAMES [ self . as_usize ( ) ]
8988 }
9089
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-
10390 /// This is the highest value a `DepKind` can have. It's used during encoding to
10491 /// 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- }
92+ pub ( crate ) const MAX : u16 = DEP_KIND_NUM_VARIANTS - 1 ;
11893}
11994
12095/// Combination of a [`DepKind`] and a key fingerprint that uniquely identifies
@@ -374,33 +349,26 @@ macro_rules! define_dep_nodes {
374349 // encoding. The derived Encodable/Decodable uses leb128 encoding which is
375350 // dense when only considering this enum. But DepKind is encoded in a larger
376351 // struct, and there we can take advantage of the unused bits in the u16.
352+ #[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash ) ]
377353 #[ allow( non_camel_case_types) ]
378- #[ repr( u16 ) ] // Must be kept in sync with the inner type of `DepKind`.
379- enum DepKindDefs {
354+ #[ repr( u16 ) ] // Must be kept in sync with the rest of `DepKind`.
355+ pub enum DepKind {
380356 $( $( #[ $attr] ) * $variant) ,*
381357 }
382358
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, ) * ] ;
359+ // This computes the number of dep kind variants. Along the way, it sanity-checks that the
360+ // discriminants of the variants have been assigned consecutively from 0 so that they can
361+ // be used as a dense index, and that all discriminants fit in a `u16`.
362+ pub ( crate ) const DEP_KIND_NUM_VARIANTS : u16 = {
363+ let deps = & [ $( DepKind :: $variant, ) * ] ;
397364 let mut i = 0 ;
398365 while i < deps. len( ) {
399366 if i != deps[ i] . as_usize( ) {
400367 panic!( ) ;
401368 }
402369 i += 1 ;
403370 }
371+ assert!( deps. len( ) <= u16 :: MAX as usize ) ;
404372 deps. len( ) as u16
405373 } ;
406374
@@ -412,7 +380,7 @@ macro_rules! define_dep_nodes {
412380
413381 pub ( super ) fn dep_kind_from_label_string( label: & str ) -> Result <DepKind , ( ) > {
414382 match label {
415- $( self :: label_strs :: $variant => Ok ( self :: dep_kinds :: $variant) , ) *
383+ $( stringify! ( $variant) => Ok ( self :: DepKind :: $variant) , ) *
416384 _ => Err ( ( ) ) ,
417385 }
418386 }
@@ -433,7 +401,9 @@ rustc_with_all_queries!(define_dep_nodes![
433401 [ ] fn Null ( ) -> ( ) ,
434402 /// We use this to create a forever-red node.
435403 [ ] fn Red ( ) -> ( ) ,
404+ /// We use this to create a side effect node.
436405 [ ] fn SideEffect ( ) -> ( ) ,
406+ /// We use this to create the anon node with zero dependencies.
437407 [ ] fn AnonZeroDeps ( ) -> ( ) ,
438408 [ ] fn TraitSelect ( ) -> ( ) ,
439409 [ ] fn CompileCodegenUnit ( ) -> ( ) ,
@@ -444,7 +414,7 @@ rustc_with_all_queries!(define_dep_nodes![
444414// WARNING: `construct` is generic and does not know that `CompileCodegenUnit` takes `Symbol`s as keys.
445415// Be very careful changing this type signature!
446416pub ( crate ) fn make_compile_codegen_unit ( tcx : TyCtxt < ' _ > , name : Symbol ) -> DepNode {
447- DepNode :: construct ( tcx, dep_kinds :: CompileCodegenUnit , & name)
417+ DepNode :: construct ( tcx, DepKind :: CompileCodegenUnit , & name)
448418}
449419
450420// WARNING: `construct` is generic and does not know that `CompileMonoItem` takes `MonoItem`s as keys.
@@ -453,13 +423,13 @@ pub(crate) fn make_compile_mono_item<'tcx>(
453423 tcx : TyCtxt < ' tcx > ,
454424 mono_item : & MonoItem < ' tcx > ,
455425) -> DepNode {
456- DepNode :: construct ( tcx, dep_kinds :: CompileMonoItem , mono_item)
426+ DepNode :: construct ( tcx, DepKind :: CompileMonoItem , mono_item)
457427}
458428
459429// WARNING: `construct` is generic and does not know that `Metadata` takes `()`s as keys.
460430// Be very careful changing this type signature!
461431pub ( crate ) fn make_metadata ( tcx : TyCtxt < ' _ > ) -> DepNode {
462- DepNode :: construct ( tcx, dep_kinds :: Metadata , & ( ) )
432+ DepNode :: construct ( tcx, DepKind :: Metadata , & ( ) )
463433}
464434
465435impl DepNode {
0 commit comments