From bf1198eb1fea71f94d4f0c3c3e968eaf183dfa65 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Tue, 31 Oct 2017 23:14:13 -0400 Subject: [PATCH] newtype_index: Support simpler serializable override, custom derive, and fix mir_opt tests --- src/librustc/dep_graph/serialized.rs | 2 +- src/librustc/hir/def_id.rs | 3 + src/librustc/middle/region.rs | 7 +- src/librustc/mir/mod.rs | 24 +- src/librustc_data_structures/indexed_vec.rs | 317 +++++++++++++------- src/librustc_mir/build/mod.rs | 2 +- 6 files changed, 213 insertions(+), 142 deletions(-) diff --git a/src/librustc/dep_graph/serialized.rs b/src/librustc/dep_graph/serialized.rs index 120af4821e3..c96040ab9b6 100644 --- a/src/librustc/dep_graph/serialized.rs +++ b/src/librustc/dep_graph/serialized.rs @@ -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)] diff --git a/src/librustc/hir/def_id.rs b/src/librustc/hir/def_id.rs index a893fb82b48..5d488074552 100644 --- a/src/librustc/hir/def_id.rs +++ b/src/librustc/hir/def_id.rs @@ -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, diff --git a/src/librustc/middle/region.rs b/src/librustc/middle/region.rs index e428ff185ac..d43774bf3b0 100644 --- a/src/librustc/middle/region.rs +++ b/src/librustc/middle/region.rs @@ -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 for Scope { #[inline] diff --git a/src/librustc/mir/mod.rs b/src/librustc/mir/mod.rs index 02cb6585eb2..e79c846d449 100644 --- a/src/librustc/mir/mod.rs +++ b/src/librustc/mir/mod.rs @@ -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)] diff --git a/src/librustc_data_structures/indexed_vec.rs b/src/librustc_data_structures/indexed_vec.rs index f0b11dbc1f0..85045342782 100644 --- a/src/librustc_data_structures/indexed_vec.rs +++ b/src/librustc_data_structures/indexed_vec.rs @@ -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)*); ); } diff --git a/src/librustc_mir/build/mod.rs b/src/librustc_mir/build/mod.rs index b206538324b..77496c7b8f2 100644 --- a/src/librustc_mir/build/mod.rs +++ b/src/librustc_mir/build/mod.rs @@ -312,7 +312,7 @@ struct CFG<'tcx> { basic_blocks: IndexVec>, } -newtype_index!(ScopeId { derive[RustcEncodable, RustcDecodable] }); +newtype_index!(ScopeId); /////////////////////////////////////////////////////////////////////////// /// The `BlockAnd` "monad" packages up the new basic block along with a -- 2.44.0