@@ -70,59 +70,30 @@ use crate::ich::StableHashingContext;
7070use crate :: mir:: mono:: MonoItem ;
7171use 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.
7974impl 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!
444401pub ( 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!
459416pub ( crate ) fn make_metadata ( tcx : TyCtxt < ' _ > ) -> DepNode {
460- DepNode :: construct ( tcx, dep_kinds :: Metadata , & ( ) )
417+ DepNode :: construct ( tcx, DepKind :: Metadata , & ( ) )
461418}
462419
463420impl DepNode {
0 commit comments