@@ -65,9 +65,9 @@ use rustc_hir::definitions::DefPathHash;
6565use rustc_hir:: { HirId , ItemLocalId , OwnerId } ;
6666use rustc_query_system:: dep_graph:: FingerprintStyle ;
6767use rustc_span:: symbol:: Symbol ;
68- use std:: hash:: Hash ;
6968
70- pub use rustc_query_system:: dep_graph:: { DepContext , DepNodeParams } ;
69+ pub use rustc_query_system:: dep_graph:: dep_node:: DepKind ;
70+ pub use rustc_query_system:: dep_graph:: { DepContext , DepNode , DepNodeParams } ;
7171
7272macro_rules! define_dep_nodes {
7373 (
@@ -84,55 +84,39 @@ macro_rules! define_dep_nodes {
8484 // encoding. The derived Encodable/Decodable uses leb128 encoding which is
8585 // dense when only considering this enum. But DepKind is encoded in a larger
8686 // struct, and there we can take advantage of the unused bits in the u16.
87- #[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash ) ]
8887 #[ allow( non_camel_case_types) ]
89- #[ repr( u16 ) ]
90- pub enum DepKind {
88+ #[ repr( u16 ) ] // Must be kept in sync with the inner type of `DepKind`.
89+ enum DepKindDefs {
9190 $( $( #[ $attr] ) * $variant) ,*
9291 }
9392
94- impl DepKind {
95- // This const implements two things: A bounds check so that we can decode
96- // a DepKind from a u16 with just one check, and a const check that the
97- // discriminants of the variants have been assigned consecutively from 0
98- // so that just the one comparison suffices to check that the u16 can be
99- // transmuted to a DepKind.
100- pub const VARIANTS : u16 = {
101- let deps: & [ DepKind ] = & [ $( DepKind :: $variant, ) * ] ;
102- let mut i = 0 ;
103- while i < deps. len( ) {
104- if i as u16 != deps[ i] as u16 {
105- panic!( ) ;
106- }
107- i += 1 ;
108- }
109- deps. len( ) as u16
110- } ;
111- }
93+ #[ allow( non_upper_case_globals) ]
94+ pub mod dep_kinds {
95+ use super :: * ;
11296
113- impl <S : rustc_serialize:: Encoder > rustc_serialize:: Encodable <S > for DepKind {
114- #[ inline]
115- fn encode( & self , s: & mut S ) {
116- s. emit_u16( * self as u16 ) ;
117- }
97+ $(
98+ // The `as u16` cast must be kept in sync with the inner type of `DepKind`.
99+ pub const $variant: DepKind = DepKind :: new( DepKindDefs :: $variant as u16 ) ;
100+ ) *
118101 }
119102
120- impl <D : rustc_serialize:: Decoder > rustc_serialize:: Decodable <D > for DepKind {
121- #[ inline]
122- fn decode( d: & mut D ) -> DepKind {
123- let discrim = d. read_u16( ) ;
124- assert!( discrim < DepKind :: VARIANTS ) ;
125- // SAFETY: DepKind::VARIANTS checks that the discriminant values permit
126- // this one check to soundly guard the transmute.
127- unsafe {
128- std:: mem:: transmute:: <u16 , DepKind >( discrim)
103+ // This checks that the discriminants of the variants have been assigned consecutively
104+ // from 0 so that they can be used as a dense index.
105+ pub const DEP_KIND_VARIANTS : u16 = {
106+ let deps = & [ $( dep_kinds:: $variant, ) * ] ;
107+ let mut i = 0 ;
108+ while i < deps. len( ) {
109+ if i != deps[ i] . as_usize( ) {
110+ panic!( ) ;
129111 }
112+ i += 1 ;
130113 }
131- }
114+ deps. len( ) as u16
115+ } ;
132116
133117 pub ( super ) fn dep_kind_from_label_string( label: & str ) -> Result <DepKind , ( ) > {
134118 match label {
135- $( stringify!( $variant) => Ok ( DepKind :: $variant) , ) *
119+ $( stringify!( $variant) => Ok ( dep_kinds :: $variant) , ) *
136120 _ => Err ( ( ) ) ,
137121 }
138122 }
@@ -158,12 +142,10 @@ rustc_query_append!(define_dep_nodes![
158142 [ ] fn CompileMonoItem ( ) -> ( ) ,
159143] ) ;
160144
161- static_assert_size ! ( DepKind , 2 ) ;
162-
163145// WARNING: `construct` is generic and does not know that `CompileCodegenUnit` takes `Symbol`s as keys.
164146// Be very careful changing this type signature!
165147pub ( crate ) fn make_compile_codegen_unit ( tcx : TyCtxt < ' _ > , name : Symbol ) -> DepNode {
166- DepNode :: construct ( tcx, DepKind :: CompileCodegenUnit , & name)
148+ DepNode :: construct ( tcx, dep_kinds :: CompileCodegenUnit , & name)
167149}
168150
169151// WARNING: `construct` is generic and does not know that `CompileMonoItem` takes `MonoItem`s as keys.
@@ -172,20 +154,9 @@ pub(crate) fn make_compile_mono_item<'tcx>(
172154 tcx : TyCtxt < ' tcx > ,
173155 mono_item : & MonoItem < ' tcx > ,
174156) -> DepNode {
175- DepNode :: construct ( tcx, DepKind :: CompileMonoItem , mono_item)
157+ DepNode :: construct ( tcx, dep_kinds :: CompileMonoItem , mono_item)
176158}
177159
178- pub type DepNode = rustc_query_system:: dep_graph:: DepNode < DepKind > ;
179-
180- // We keep a lot of `DepNode`s in memory during compilation. It's not
181- // required that their size stay the same, but we don't want to change
182- // it inadvertently. This assert just ensures we're aware of any change.
183- #[ cfg( any( target_arch = "x86" , target_arch = "x86_64" ) ) ]
184- static_assert_size ! ( DepNode , 18 ) ;
185-
186- #[ cfg( not( any( target_arch = "x86" , target_arch = "x86_64" ) ) ) ]
187- static_assert_size ! ( DepNode , 24 ) ;
188-
189160pub trait DepNodeExt : Sized {
190161 /// Extracts the DefId corresponding to this DepNode. This will work
191162 /// if two conditions are met:
0 commit comments