about summary refs log tree commit diff
path: root/compiler/rustc_middle/src/dep_graph/dep_node.rs
diff options
context:
space:
mode:
authorJohn Kåre Alsaker <john.kare.alsaker@gmail.com>2023-09-15 15:39:11 +0200
committerJohn Kåre Alsaker <john.kare.alsaker@gmail.com>2023-09-21 17:06:14 +0200
commit1806efe7f2757c5fbcf99d1ac9ca2d26fb0a7c4e (patch)
tree261c5f818f32a5c4ccc366874279d766c8f66a2f /compiler/rustc_middle/src/dep_graph/dep_node.rs
parent66ab7e6883c563ceacf8386a50e851e9855cd6b4 (diff)
downloadrust-1806efe7f2757c5fbcf99d1ac9ca2d26fb0a7c4e.tar.gz
rust-1806efe7f2757c5fbcf99d1ac9ca2d26fb0a7c4e.zip
Move `DepKind` to `rustc_query_system` and define it as `u16`
Diffstat (limited to 'compiler/rustc_middle/src/dep_graph/dep_node.rs')
-rw-r--r--compiler/rustc_middle/src/dep_graph/dep_node.rs79
1 files changed, 25 insertions, 54 deletions
diff --git a/compiler/rustc_middle/src/dep_graph/dep_node.rs b/compiler/rustc_middle/src/dep_graph/dep_node.rs
index 78a0f82db13..39d82c489d5 100644
--- a/compiler/rustc_middle/src/dep_graph/dep_node.rs
+++ b/compiler/rustc_middle/src/dep_graph/dep_node.rs
@@ -65,9 +65,9 @@ use rustc_hir::definitions::DefPathHash;
 use rustc_hir::{HirId, ItemLocalId, OwnerId};
 use rustc_query_system::dep_graph::FingerprintStyle;
 use rustc_span::symbol::Symbol;
-use std::hash::Hash;
 
-pub use rustc_query_system::dep_graph::{DepContext, DepNodeParams};
+pub use rustc_query_system::dep_graph::dep_node::DepKind;
+pub use rustc_query_system::dep_graph::{DepContext, DepNode, DepNodeParams};
 
 macro_rules! define_dep_nodes {
     (
@@ -84,55 +84,39 @@ macro_rules! define_dep_nodes {
         // encoding. The derived Encodable/Decodable uses leb128 encoding which is
         // dense when only considering this enum. But DepKind is encoded in a larger
         // struct, and there we can take advantage of the unused bits in the u16.
-        #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
         #[allow(non_camel_case_types)]
-        #[repr(u16)]
-        pub enum DepKind {
+        #[repr(u16)] // Must be kept in sync with the inner type of `DepKind`.
+        enum DepKindDefs {
             $( $( #[$attr] )* $variant),*
         }
 
-        impl DepKind {
-            // This const implements two things: A bounds check so that we can decode
-            // a DepKind from a u16 with just one check, and a const check that the
-            // discriminants of the variants have been assigned consecutively from 0
-            // so that just the one comparison suffices to check that the u16 can be
-            // transmuted to a DepKind.
-            pub const VARIANTS: u16 = {
-                let deps: &[DepKind] = &[$(DepKind::$variant,)*];
-                let mut i = 0;
-                while i < deps.len() {
-                    if i as u16 != deps[i] as u16 {
-                        panic!();
-                    }
-                    i += 1;
-                }
-                deps.len() as u16
-            };
-        }
+        #[allow(non_upper_case_globals)]
+        pub mod dep_kinds {
+            use super::*;
 
-        impl<S: rustc_serialize::Encoder> rustc_serialize::Encodable<S> for DepKind {
-            #[inline]
-            fn encode(&self, s: &mut S) {
-                s.emit_u16(*self as u16);
-            }
+            $(
+                // The `as u16` cast must be kept in sync with the inner type of `DepKind`.
+                pub const $variant: DepKind = DepKind::new(DepKindDefs::$variant as u16);
+            )*
         }
 
-        impl<D: rustc_serialize::Decoder> rustc_serialize::Decodable<D> for DepKind {
-            #[inline]
-            fn decode(d: &mut D) -> DepKind {
-                let discrim = d.read_u16();
-                assert!(discrim < DepKind::VARIANTS);
-                // SAFETY: DepKind::VARIANTS checks that the discriminant values permit
-                // this one check to soundly guard the transmute.
-                unsafe {
-                    std::mem::transmute::<u16, DepKind>(discrim)
+        // This checks that the discriminants of the variants have been assigned consecutively
+        // from 0 so that they can be used as a dense index.
+        pub const DEP_KIND_VARIANTS: u16 = {
+            let deps = &[$(dep_kinds::$variant,)*];
+            let mut i = 0;
+            while i < deps.len() {
+                if i != deps[i].as_usize() {
+                    panic!();
                 }
+                i += 1;
             }
-        }
+            deps.len() as u16
+        };
 
         pub(super) fn dep_kind_from_label_string(label: &str) -> Result<DepKind, ()> {
             match label {
-                $(stringify!($variant) => Ok(DepKind::$variant),)*
+                $(stringify!($variant) => Ok(dep_kinds::$variant),)*
                 _ => Err(()),
             }
         }
@@ -158,12 +142,10 @@ rustc_query_append!(define_dep_nodes![
     [] fn CompileMonoItem() -> (),
 ]);
 
-static_assert_size!(DepKind, 2);
-
 // WARNING: `construct` is generic and does not know that `CompileCodegenUnit` takes `Symbol`s as keys.
 // Be very careful changing this type signature!
 pub(crate) fn make_compile_codegen_unit(tcx: TyCtxt<'_>, name: Symbol) -> DepNode {
-    DepNode::construct(tcx, DepKind::CompileCodegenUnit, &name)
+    DepNode::construct(tcx, dep_kinds::CompileCodegenUnit, &name)
 }
 
 // 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>(
     tcx: TyCtxt<'tcx>,
     mono_item: &MonoItem<'tcx>,
 ) -> DepNode {
-    DepNode::construct(tcx, DepKind::CompileMonoItem, mono_item)
+    DepNode::construct(tcx, dep_kinds::CompileMonoItem, mono_item)
 }
 
-pub type DepNode = rustc_query_system::dep_graph::DepNode<DepKind>;
-
-// We keep a lot of `DepNode`s in memory during compilation. It's not
-// required that their size stay the same, but we don't want to change
-// it inadvertently. This assert just ensures we're aware of any change.
-#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
-static_assert_size!(DepNode, 18);
-
-#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
-static_assert_size!(DepNode, 24);
-
 pub trait DepNodeExt: Sized {
     /// Extracts the DefId corresponding to this DepNode. This will work
     /// if two conditions are met: