]> git.lizzy.rs Git - rust.git/commitdiff
newtype_index: Support simpler serializable override, custom derive, and fix mir_opt...
authorPaul Daniel Faria <nashenas88@users.noreply.github.com>
Wed, 1 Nov 2017 03:14:13 +0000 (23:14 -0400)
committerPaul Daniel Faria <nashenas88@users.noreply.github.com>
Thu, 2 Nov 2017 00:12:53 +0000 (20:12 -0400)
src/librustc/dep_graph/serialized.rs
src/librustc/hir/def_id.rs
src/librustc/middle/region.rs
src/librustc/mir/mod.rs
src/librustc_data_structures/indexed_vec.rs
src/librustc_mir/build/mod.rs

index 120af4821e36f3496b3f7c599f89044b56bb2254..c96040ab9b6e3eab996aafdb875c90fc6d3217a4 100644 (file)
@@ -14,7 +14,7 @@
 use ich::Fingerprint;
 use rustc_data_structures::indexed_vec::{IndexVec, Idx};
 
-newtype_index!(SerializedDepNodeIndex { derive[RustcEncodable, RustcDecodable] });
+newtype_index!(SerializedDepNodeIndex);
 
 /// Data for use when recompiling the **current crate**.
 #[derive(Debug, RustcEncodable, RustcDecodable)]
index a893fb82b4892a9e75c83afe7fcc779f6c7591ca..5d488074552ed32cd6b8a51af6ba5859fc27c54a 100644 (file)
@@ -18,6 +18,9 @@
 
 newtype_index!(CrateNum nopub
     {
+        derive[Debug]
+        ENCODABLE = custom
+
         /// Item definitions in the currently-compiled crate would have the CrateNum
         /// LOCAL_CRATE in their DefId.
         const LOCAL_CRATE = 0,
index e428ff185ac898eda96a9507defd61e3df861755..d43774bf3b084310eb5d4601126691e004754ccb 100644 (file)
@@ -156,12 +156,7 @@ pub struct BlockRemainder {
     pub first_statement_index: FirstStatementIndex,
 }
 
-newtype_index!(FirstStatementIndex
-    {
-        derive[RustcEncodable, RustcDecodable]
-        DEBUG_NAME = "",
-        MAX = SCOPE_DATA_REMAINDER_MAX,
-    });
+newtype_index!(FirstStatementIndex { MAX = SCOPE_DATA_REMAINDER_MAX });
 
 impl From<ScopeData> for Scope {
     #[inline]
index 02cb6585eb2aab9722ffb7c03207e2f50bf87e8f..e79c846d4499128a39bc28f8659fac8b80244ef2 100644 (file)
@@ -417,8 +417,7 @@ pub enum BorrowKind {
 
 newtype_index!(Local
     {
-        derive[RustcEncodable, RustcDecodable]
-        DEBUG_NAME = "_",
+        DEBUG_FORMAT = "_{}",
         const RETURN_POINTER = 0,
     });
 
@@ -554,11 +553,7 @@ pub struct UpvarDecl {
 ///////////////////////////////////////////////////////////////////////////
 // BasicBlock
 
-newtype_index!(BasicBlock
-    {
-        derive[RustcEncodable, RustcDecodable]
-        DEBUG_NAME = "bb"
-    });
+newtype_index!(BasicBlock { DEBUG_FORMAT = "bb{}" });
 
 ///////////////////////////////////////////////////////////////////////////
 // BasicBlockData and Terminator
@@ -1140,11 +1135,7 @@ pub enum ProjectionElem<'tcx, V, T> {
 /// and the index is a local.
 pub type LvalueElem<'tcx> = ProjectionElem<'tcx, Local, Ty<'tcx>>;
 
-newtype_index!(Field
-    {
-        derive[RustcEncodable, RustcDecodable]
-        DEBUG_NAME = "field"
-    });
+newtype_index!(Field { DEBUG_FORMAT = "field[{}]" });
 
 impl<'tcx> Lvalue<'tcx> {
     pub fn field(self, f: Field, ty: Ty<'tcx>) -> Lvalue<'tcx> {
@@ -1211,8 +1202,7 @@ fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
 
 newtype_index!(VisibilityScope
     {
-        derive[RustcEncodable, RustcDecodable]
-        DEBUG_NAME = "scope",
+        DEBUG_FORMAT = "scope[{}]",
         const ARGUMENT_VISIBILITY_SCOPE = 0,
     });
 
@@ -1539,11 +1529,7 @@ pub struct Constant<'tcx> {
     pub literal: Literal<'tcx>,
 }
 
-newtype_index!(Promoted
-    {
-        derive[RustcEncodable, RustcDecodable]
-        DEBUG_NAME = "promoted"
-    });
+newtype_index!(Promoted { DEBUG_FORMAT = "promoted[{}]" });
 
 
 #[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
index f0b11dbc1f00be9cdf609ff2527af5a200f939c8..85045342782571531e519c821554915503c672ac 100644 (file)
@@ -46,51 +46,51 @@ macro_rules! newtype_index {
     ($name:ident) => (
         newtype_index!(
             // Leave out derives marker so we can use its absence to ensure it comes first
-            @type       [$name]
-            @pub        [pub]
-            @max        [::std::u32::MAX]
-            @debug_name [unsafe {::std::intrinsics::type_name::<$name>() }]);
+            @type         [$name]
+            @pub          [pub]
+            @max          [::std::u32::MAX]
+            @debug_format ["{}"]);
     );
 
     ($name:ident nopub) => (
         newtype_index!(
             // Leave out derives marker so we can use its absence to ensure it comes first
-            @type       [$name]
-            @pub        []
-            @max        [::std::u32::MAX]
-            @debug_name [unsafe {::std::intrinsics::type_name::<$name>() }]);
+            @type         [$name]
+            @pub          []
+            @max          [::std::u32::MAX]
+            @debug_format ["{}"]);
     );
 
     // Define any constants
     ($name:ident { $($tokens:tt)+ }) => (
         newtype_index!(
             // Leave out derives marker so we can use its absence to ensure it comes first
-            @type       [$name]
-            @pub        [pub]
-            @max        [::std::u32::MAX]
-            @debug_name [unsafe {::std::intrinsics::type_name::<$name>() }]
-                        $($tokens)+);
+            @type         [$name]
+            @pub          [pub]
+            @max          [::std::u32::MAX]
+            @debug_format ["{}"]
+                          $($tokens)+);
     );
 
     // Define any constants
     ($name:ident nopub { $($tokens:tt)+ }) => (
         newtype_index!(
             // Leave out derives marker so we can use its absence to ensure it comes first
-            @type       [$name]
-            @pub        []
-            @max        [::std::u32::MAX]
-            @debug_name [unsafe {::std::intrinsics::type_name::<$name>() }]
-                        $($tokens)+);
+            @type         [$name]
+            @pub          []
+            @max          [::std::u32::MAX]
+            @debug_format [unsafe {::std::intrinsics::type_name::<$name>() }]
+                          $($tokens)+);
     );
 
     // ---- private rules ----
 
     // Base case, user-defined constants (if any) have already been defined
-    (@derives    [$($derives:ident),*]
-     @type       [$type:ident]
-     @pub        [$($pub:tt)*]
-     @max        [$max:expr]
-     @debug_name [$debug_name:expr]) => (
+    (@derives      [$($derives:ident,)*]
+     @type         [$type:ident]
+     @pub          [$($pub:tt)*]
+     @max          [$max:expr]
+     @debug_format [$debug_format:expr]) => (
         #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, $($derives),*)]
         pub struct $type($($pub)* u32);
 
@@ -105,130 +105,217 @@ fn index(self) -> usize {
             }
         }
 
+        newtype_index!(
+            @handle_debug
+            @derives      [$($derives,)*]
+            @type         [$type]
+            @debug_format [$debug_format]);
+    );
+
+    // base case for handle_debug where format is custom. No Debug implementation is emitted.
+    (@handle_debug
+     @derives      [$($_derives:ident,)*]
+     @type         [$type:ident]
+     @debug_format [custom]) => ();
+
+    // base case for handle_debug, no debug overrides found, so use default
+    (@handle_debug
+     @derives      []
+     @type         [$type:ident]
+     @debug_format [$debug_format:expr]) => (
         impl ::std::fmt::Debug for $type {
             fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
-                write!(fmt, "{}{}", $debug_name, self.0)
+                write!(fmt, $debug_format, self.0)
             }
         }
     );
 
+    // Debug is requested for derive, don't generate any Debug implementation.
+    (@handle_debug
+     @derives      [Debug, $($derives:ident,)*]
+     @type         [$type:ident]
+     @debug_format [$debug_format:expr]) => ();
+
+    // It's not Debug, so just pop it off the front of the derives stack and check the rest.
+    (@handle_debug
+     @derives      [$_derive:ident, $($derives:ident,)*]
+     @type         [$type:ident]
+     @debug_format [$debug_format:expr]) => (
+        newtype_index!(
+            @handle_debug
+            @derives      [$($derives,)*]
+            @type         [$type]
+            @debug_format [$debug_format]);
+    );
+
+    // Append comma to end of derives list if it's missing
+    (@type         [$type:ident]
+     @pub          [$($pub:tt)*]
+     @max          [$max:expr]
+     @debug_format [$debug_format:expr]
+                   derive [$($derives:ident),*]
+                   $($tokens:tt)*) => (
+        newtype_index!(
+            @type         [$type]
+            @pub          [$($pub)*]
+            @max          [$max]
+            @debug_format [$debug_format]
+                          derive [$($derives,)*]
+                          $($tokens)*);
+    );
+
+    // By not including the @derives marker in this list nor in the default args, we can force it
+    // to come first if it exists. When encodable is custom, just use the derives list as-is.
+    (@type         [$type:ident]
+     @pub          [$($pub:tt)*]
+     @max          [$max:expr]
+     @debug_format [$debug_format:expr]
+                   derive [$($derives:ident,)+]
+                   ENCODABLE = custom
+                   $($tokens:tt)*) => (
+        newtype_index!(
+            @derives      [$($derives,)+]
+            @type         [$type]
+            @pub          [$($pub)*]
+            @max          [$max]
+            @debug_format [$debug_format]
+                          $($tokens)*);
+    );
+
     // By not including the @derives marker in this list nor in the default args, we can force it
-    // to come first if it exists
-    (@type       [$type:ident]
-     @pub        [$($pub:tt)*]
-     @max        [$max:expr]
-     @debug_name [$debug_name:expr]
-                 derive [$($derives:ident),+]
-                 $($tokens:tt)*) => (
+    // to come first if it exists. When encodable isn't custom, add serialization traits by default.
+    (@type         [$type:ident]
+     @pub          [$($pub:tt)*]
+     @max          [$max:expr]
+     @debug_format [$debug_format:expr]
+                   derive [$($derives:ident,)+]
+                   $($tokens:tt)*) => (
+        newtype_index!(
+            @derives      [$($derives,)+ RustcDecodable, RustcEncodable,]
+            @type         [$type]
+            @pub          [$($pub)*]
+            @max          [$max]
+            @debug_format [$debug_format]
+                          $($tokens)*);
+    );
+
+    // The case where no derives are added, but encodable is overriden. Don't
+    // derive serialization traits
+    (@type         [$type:ident]
+     @pub          [$($pub:tt)*]
+     @max          [$max:expr]
+     @debug_format [$debug_format:expr]
+                   ENCODABLE = custom
+                   $($tokens:tt)*) => (
         newtype_index!(
-            @derives    [$($derives),+]
-            @type       [$type]
-            @pub        [$($pub)*]
-            @max        [$max]
-            @debug_name [$debug_name]
-                        $($tokens)*);
+            @derives      []
+            @type         [$type]
+            @pub          [$($pub)*]
+            @max          [$max]
+            @debug_format [$debug_format]
+                          $($tokens)*);
     );
 
-    // The case where no derives are added
-    (@type       [$type:ident]
-     @pub        [$($pub:tt)*]
-     @max        [$max:expr]
-     @debug_name [$debug_name:expr]
-                 $($tokens:tt)*) => (
+    // The case where no derives are added, add serialization derives by default
+    (@type         [$type:ident]
+     @pub          [$($pub:tt)*]
+     @max          [$max:expr]
+     @debug_format [$debug_format:expr]
+                   $($tokens:tt)*) => (
         newtype_index!(
-            @derives    []
-            @type       [$type]
-            @pub        [$($pub)*]
-            @max        [$max]
-            @debug_name [$debug_name]
-                        $($tokens)*);
+            @derives      [RustcDecodable, RustcEncodable,]
+            @type         [$type]
+            @pub          [$($pub)*]
+            @max          [$max]
+            @debug_format [$debug_format]
+                          $($tokens)*);
     );
 
     // Rewrite final without comma to one that includes comma
-    (@derives    [$($derives:ident),*]
-     @type       [$type:ident]
-     @pub        [$($pub:tt)*]
-     @max        [$max:expr]
-     @debug_name [$debug_name:expr]
-                 $name:ident = $constant:expr) => (
+    (@derives      [$($derives:ident,)*]
+     @type         [$type:ident]
+     @pub          [$($pub:tt)*]
+     @max          [$max:expr]
+     @debug_format [$debug_format:expr]
+                   $name:ident = $constant:expr) => (
         newtype_index!(
-            @derives    [$($derives),*]
-            @type       [$type]
-            @pub        [$($pub)*]
-            @max        [$max]
-            @debug_name [$debug_name]
-                        $name = $constant,);
+            @derives      [$($derives,)*]
+            @type         [$type]
+            @pub          [$($pub)*]
+            @max          [$max]
+            @debug_format [$debug_format]
+                          $name = $constant,);
     );
 
     // Rewrite final const without comma to one that includes comma
-    (@derives    [$($derives:ident),*]
-     @type       [$type:ident]
-     @pub        [$($pub:tt)*]
-     @max        [$_max:expr]
-     @debug_name [$debug_name:expr]
-                 $(#[doc = $doc:expr])*
-                 const $name:ident = $constant:expr) => (
+    (@derives      [$($derives:ident,)*]
+     @type         [$type:ident]
+     @pub          [$($pub:tt)*]
+     @max          [$_max:expr]
+     @debug_format [$debug_format:expr]
+                   $(#[doc = $doc:expr])*
+                   const $name:ident = $constant:expr) => (
         newtype_index!(
-            @derives    [$($derives),*]
-            @type       [$type]
-            @pub        [$($pub)*]
-            @max        [$max]
-            @debug_name [$debug_name]
-                        $(#[doc = $doc])* const $name = $constant,);
+            @derives      [$($derives,)*]
+            @type         [$type]
+            @pub          [$($pub)*]
+            @max          [$max]
+            @debug_format [$debug_format]
+                          $(#[doc = $doc])* const $name = $constant,);
     );
 
     // Replace existing default for max
-    (@derives    [$($derives:ident),*]
-     @type       [$type:ident]
-     @pub        [$($pub:tt)*]
-     @max        [$_max:expr]
-     @debug_name [$debug_name:expr]
-                 MAX = $max:expr,
-                 $($tokens:tt)*) => (
+    (@derives      [$($derives:ident,)*]
+     @type         [$type:ident]
+     @pub          [$($pub:tt)*]
+     @max          [$_max:expr]
+     @debug_format [$debug_format:expr]
+                   MAX = $max:expr,
+                   $($tokens:tt)*) => (
         newtype_index!(
-            @derives    [$($derives),*]
-            @type       [$type]
-            @pub        [$($pub)*]
-            @max        [$max]
-            @debug_name [$debug_name]
-                        $($tokens)*);
+            @derives      [$($derives,)*]
+            @type         [$type]
+            @pub          [$($pub)*]
+            @max          [$max]
+            @debug_format [$debug_format]
+                          $($tokens)*);
     );
 
-    // Replace existing default for debug_name
-    (@derives    [$($derives:ident),*]
-     @type       [$type:ident]
-     @pub        [$($pub:tt)*]
-     @max        [$max:expr]
-     @debug_name [$_debug_name:expr]
-                 DEBUG_NAME = $debug_name:expr,
-                 $($tokens:tt)*) => (
+    // Replace existing default for debug_format
+    (@derives      [$($derives:ident,)*]
+     @type         [$type:ident]
+     @pub          [$($pub:tt)*]
+     @max          [$max:expr]
+     @debug_format [$_debug_format:expr]
+                   DEBUG_FORMAT = $debug_format:expr,
+                   $($tokens:tt)*) => (
         newtype_index!(
-            @derives    [$($derives),*]
-            @type       [$type]
-            @pub        [$($pub)*]
-            @max        [$max]
-            @debug_name [$debug_name]
-                        $($tokens)*);
+            @derives      [$($derives,)*]
+            @type         [$type]
+            @pub          [$($pub)*]
+            @max          [$max]
+            @debug_format [$debug_format]
+                          $($tokens)*);
     );
 
     // Assign a user-defined constant
-    (@derives    [$($derives:ident),*]
-     @type       [$type:ident]
-     @pub        [$($pub:tt)*]
-     @max        [$max:expr]
-     @debug_name [$debug_name:expr]
-                 $(#[doc = $doc:expr])*
-                 const $name:ident = $constant:expr,
-                 $($tokens:tt)*) => (
+    (@derives      [$($derives:ident,)*]
+     @type         [$type:ident]
+     @pub          [$($pub:tt)*]
+     @max          [$max:expr]
+     @debug_format [$debug_format:expr]
+                   $(#[doc = $doc:expr])*
+                   const $name:ident = $constant:expr,
+                   $($tokens:tt)*) => (
         $(#[doc = $doc])*
         pub const $name: $type = $type($constant);
         newtype_index!(
-            @derives    [$($derives),*]
-            @type       [$type]
-            @pub        [$($pub)*]
-            @max        [$max]
-            @debug_name [$debug_name]
-                        $($tokens)*);
+            @derives      [$($derives,)*]
+            @type         [$type]
+            @pub          [$($pub)*]
+            @max          [$max]
+            @debug_format [$debug_format]
+                          $($tokens)*);
     );
 }
 
index b206538324b4a22dc35a0100e560f0efd195c433..77496c7b8f218b922e9991ea3cc540cf8034c311 100644 (file)
@@ -312,7 +312,7 @@ struct CFG<'tcx> {
     basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
 }
 
-newtype_index!(ScopeId { derive[RustcEncodable, RustcDecodable] });
+newtype_index!(ScopeId);
 
 ///////////////////////////////////////////////////////////////////////////
 /// The `BlockAnd` "monad" packages up the new basic block along with a