From 4e496de26d483447d309673a47c5e2013e46bf2b Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Sun, 29 Oct 2017 00:13:49 -0400 Subject: [PATCH 1/4] Add derive and doc comment capabilities to newtype_index macro --- src/librustc/dep_graph/serialized.rs | 2 +- src/librustc/hir/def_id.rs | 37 ++---- src/librustc/middle/region.rs | 3 +- src/librustc/mir/mod.rs | 25 +++- src/librustc_data_structures/indexed_vec.rs | 136 +++++++++++++++----- src/librustc_mir/build/mod.rs | 2 +- 6 files changed, 143 insertions(+), 62 deletions(-) diff --git a/src/librustc/dep_graph/serialized.rs b/src/librustc/dep_graph/serialized.rs index c96040ab9b6e3..120af4821e36f 100644 --- a/src/librustc/dep_graph/serialized.rs +++ b/src/librustc/dep_graph/serialized.rs @@ -14,7 +14,7 @@ use dep_graph::DepNode; use ich::Fingerprint; use rustc_data_structures::indexed_vec::{IndexVec, Idx}; -newtype_index!(SerializedDepNodeIndex); +newtype_index!(SerializedDepNodeIndex { derive[RustcEncodable, RustcDecodable] }); /// 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 69d23504cdae0..6c401482c9772 100644 --- a/src/librustc/hir/def_id.rs +++ b/src/librustc/hir/def_id.rs @@ -16,30 +16,19 @@ use serialize::{self, Encoder, Decoder}; use std::fmt; use std::u32; -#[derive(Clone, Copy, Eq, Ord, PartialOrd, PartialEq, Hash, Debug)] -pub struct CrateNum(u32); - -impl Idx for CrateNum { - fn new(value: usize) -> Self { - assert!(value < (u32::MAX) as usize); - CrateNum(value as u32) - } - - fn index(self) -> usize { - self.0 as usize - } -} - -/// Item definitions in the currently-compiled crate would have the CrateNum -/// LOCAL_CRATE in their DefId. -pub const LOCAL_CRATE: CrateNum = CrateNum(0); - -/// Virtual crate for builtin macros -// FIXME(jseyfried): this is also used for custom derives until proc-macro crates get `CrateNum`s. -pub const BUILTIN_MACROS_CRATE: CrateNum = CrateNum(u32::MAX); - -/// A CrateNum value that indicates that something is wrong. -pub const INVALID_CRATE: CrateNum = CrateNum(u32::MAX - 1); +newtype_index!(CrateNum nopub + { + /// Item definitions in the currently-compiled crate would have the CrateNum + /// LOCAL_CRATE in their DefId. + const LOCAL_CRATE = 0, + + /// Virtual crate for builtin macros + // FIXME(jseyfried): this is also used for custom derives until proc-macro crates get `CrateNum`s. + const BUILTIN_MACROS_CRATE = u32::MAX, + + /// A CrateNum value that indicates that something is wrong. + const INVALID_CRATE = u32::MAX - 1, + }); impl CrateNum { pub fn new(x: usize) -> CrateNum { diff --git a/src/librustc/middle/region.rs b/src/librustc/middle/region.rs index e725592ff99e6..e428ff185ac89 100644 --- a/src/librustc/middle/region.rs +++ b/src/librustc/middle/region.rs @@ -158,7 +158,8 @@ pub struct BlockRemainder { newtype_index!(FirstStatementIndex { - DEBUG_FORMAT = "{}", + derive[RustcEncodable, RustcDecodable] + DEBUG_NAME = "", MAX = SCOPE_DATA_REMAINDER_MAX, }); diff --git a/src/librustc/mir/mod.rs b/src/librustc/mir/mod.rs index c4a33bb07cdf2..02cb6585eb2aa 100644 --- a/src/librustc/mir/mod.rs +++ b/src/librustc/mir/mod.rs @@ -417,7 +417,8 @@ pub enum BorrowKind { newtype_index!(Local { - DEBUG_FORMAT = "_{}", + derive[RustcEncodable, RustcDecodable] + DEBUG_NAME = "_", const RETURN_POINTER = 0, }); @@ -553,7 +554,11 @@ pub struct UpvarDecl { /////////////////////////////////////////////////////////////////////////// // BasicBlock -newtype_index!(BasicBlock { DEBUG_FORMAT = "bb{}" }); +newtype_index!(BasicBlock + { + derive[RustcEncodable, RustcDecodable] + DEBUG_NAME = "bb" + }); /////////////////////////////////////////////////////////////////////////// // BasicBlockData and Terminator @@ -1135,7 +1140,11 @@ pub type LvalueProjection<'tcx> = Projection<'tcx, Lvalue<'tcx>, Local, Ty<'tcx> /// and the index is a local. pub type LvalueElem<'tcx> = ProjectionElem<'tcx, Local, Ty<'tcx>>; -newtype_index!(Field { DEBUG_FORMAT = "field[{}]" }); +newtype_index!(Field + { + derive[RustcEncodable, RustcDecodable] + DEBUG_NAME = "field" + }); impl<'tcx> Lvalue<'tcx> { pub fn field(self, f: Field, ty: Ty<'tcx>) -> Lvalue<'tcx> { @@ -1202,7 +1211,8 @@ impl<'tcx> Debug for Lvalue<'tcx> { newtype_index!(VisibilityScope { - DEBUG_FORMAT = "scope[{}]", + derive[RustcEncodable, RustcDecodable] + DEBUG_NAME = "scope", const ARGUMENT_VISIBILITY_SCOPE = 0, }); @@ -1529,7 +1539,12 @@ pub struct Constant<'tcx> { pub literal: Literal<'tcx>, } -newtype_index!(Promoted { DEBUG_FORMAT = "promoted[{}]" }); +newtype_index!(Promoted + { + derive[RustcEncodable, RustcDecodable] + DEBUG_NAME = "promoted" + }); + #[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)] pub enum Literal<'tcx> { diff --git a/src/librustc_data_structures/indexed_vec.rs b/src/librustc_data_structures/indexed_vec.rs index 0660cd96a4a34..06fd10bf6a913 100644 --- a/src/librustc_data_structures/indexed_vec.rs +++ b/src/librustc_data_structures/indexed_vec.rs @@ -45,33 +45,57 @@ macro_rules! newtype_index { // Use default constants ($name:ident) => ( newtype_index!( - @type[$name] - @max[::std::u32::MAX] - @debug_format["{}"]); + // 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>() }]); + ); + + ($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>() }]); ); // Define any constants ($name:ident { $($tokens:tt)+ }) => ( newtype_index!( - @type[$name] - @max[::std::u32::MAX] - @debug_format["{}"] - $($tokens)+); + // 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)+); + ); + + // 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)+); ); // ---- private rules ---- // Base case, user-defined constants (if any) have already been defined - (@type[$type:ident] @max[$max:expr] @debug_format[$debug_format:expr]) => ( - #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, - RustcEncodable, RustcDecodable)] - pub struct $type(pub u32); + (@derives[$($derives:ident),*] @type[$type:ident] @pub[$($pub:tt)*] @max[$max:expr] @debug_name[$debug_name:expr]) => ( + #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, $($derives),*)] + pub struct $type($($pub)* u32); impl Idx for $type { fn new(value: usize) -> Self { assert!(value < ($max) as usize); $type(value as u32) } + fn index(self) -> usize { self.0 as usize } @@ -79,43 +103,95 @@ macro_rules! newtype_index { impl ::std::fmt::Debug for $type { fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { - write!(fmt, $debug_format, self.0) + write!(fmt, "{}{}", $debug_name, self.0) } } ); + // 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)*) => ( + newtype_index!( + @derives [$($derives),+] + @type [$type] + @pub [$($pub)*] + @max [$max] + @debug_name [$debug_name] + $($tokens)*); + ); + + // The case where no derives are added + (@type[$type:ident] @pub[$($pub:tt)*] @max[$max:expr] @debug_name[$debug_name:expr] $($tokens:tt)*) => ( + newtype_index!( + @derives [] + @type [$type] + @pub [$($pub)*] + @max [$max] + @debug_name [$debug_name] + $($tokens)*); + ); + // Rewrite final without comma to one that includes comma - (@type[$type:ident] @max[$max:expr] @debug_format[$debug_format:expr] + (@derives[$($derives:ident),*] @type[$type:ident] @pub[$($pub:tt)*] @max[$max:expr] @debug_name[$debug_name:expr] $name:ident = $constant:expr) => ( - newtype_index!(@type[$type] @max[$max] @debug_format[$debug_format] $name = $constant,); + newtype_index!( + @derives [$($derives),*] + @type [$type] + @pub [$($pub)*] + @max [$max] + @debug_name [$debug_name] + $name = $constant,); ); // Rewrite final const without comma to one that includes comma - (@type[$type:ident] @max[$_max:expr] @debug_format[$debug_format:expr] - const $name:ident = $constant:expr) => ( - newtype_index!(@type[$type] - @max[$max] - @debug_format[$debug_format] - const $name = $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) => ( + newtype_index!( + @derives [$($derives),*] + @type [$type] + @pub [$($pub)*] + @max [$max] + @debug_name [$debug_name] + $(#[doc = $doc])* const $name = $constant,); ); // Replace existing default for max - (@type[$type:ident] @max[$_max:expr] @debug_format[$debug_format:expr] + (@derives[$($derives:ident),*] @type[$type:ident] @pub[$($pub:tt)*] @max[$_max:expr] @debug_name[$debug_name:expr] MAX = $max:expr, $($tokens:tt)*) => ( - newtype_index!(@type[$type] @max[$max] @debug_format[$debug_format] $($tokens)*); + newtype_index!( + @derives [$($derives),*] + @type [$type] + @pub [$($pub)*] + @max [$max] + @debug_name [$debug_name] + $($tokens)*); ); - // Replace existing default for debug_format - (@type[$type:ident] @max[$max:expr] @debug_format[$_debug_format:expr] - DEBUG_FORMAT = $debug_format:expr, $($tokens:tt)*) => ( - newtype_index!(@type[$type] @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)*) => ( + newtype_index!( + @derives [$($derives),*] + @type [$type] + @pub [$($pub)*] + @max [$max] + @debug_name [$debug_name] + $($tokens)*); ); - // Assign a user-defined constant (as final param) - (@type[$type:ident] @max[$max:expr] @debug_format[$debug_format:expr] - const $name:ident = $constant:expr, $($tokens:tt)*) => ( + // 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)*) => ( + $(#[doc = $doc])* pub const $name: $type = $type($constant); - newtype_index!(@type[$type] @max[$max] @debug_format[$debug_format] $($tokens)*); + newtype_index!( + @derives [$($derives),*] + @type [$type] + @pub [$($pub)*] + @max [$max] + @debug_name [$debug_name] + $($tokens)*); ); } diff --git a/src/librustc_mir/build/mod.rs b/src/librustc_mir/build/mod.rs index 77496c7b8f218..b206538324b4a 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); +newtype_index!(ScopeId { derive[RustcEncodable, RustcDecodable] }); /////////////////////////////////////////////////////////////////////////// /// The `BlockAnd` "monad" packages up the new basic block along with a From b46e42fe2ab75f26ce0f34a849a611c6b8cddf13 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Sun, 29 Oct 2017 11:09:54 -0400 Subject: [PATCH 2/4] Clean up macro argument matches so they satisfy tidy checks --- src/librustc/hir/def_id.rs | 3 +- src/librustc_data_structures/indexed_vec.rs | 65 ++++++++++++++++----- 2 files changed, 53 insertions(+), 15 deletions(-) diff --git a/src/librustc/hir/def_id.rs b/src/librustc/hir/def_id.rs index 6c401482c9772..a893fb82b4892 100644 --- a/src/librustc/hir/def_id.rs +++ b/src/librustc/hir/def_id.rs @@ -23,7 +23,8 @@ newtype_index!(CrateNum nopub const LOCAL_CRATE = 0, /// Virtual crate for builtin macros - // FIXME(jseyfried): this is also used for custom derives until proc-macro crates get `CrateNum`s. + // FIXME(jseyfried): this is also used for custom derives until proc-macro crates get + // `CrateNum`s. const BUILTIN_MACROS_CRATE = u32::MAX, /// A CrateNum value that indicates that something is wrong. diff --git a/src/librustc_data_structures/indexed_vec.rs b/src/librustc_data_structures/indexed_vec.rs index 06fd10bf6a913..f0b11dbc1f00b 100644 --- a/src/librustc_data_structures/indexed_vec.rs +++ b/src/librustc_data_structures/indexed_vec.rs @@ -86,7 +86,11 @@ macro_rules! newtype_index { // ---- 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_name [$debug_name:expr]) => ( #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, $($derives),*)] pub struct $type($($pub)* u32); @@ -110,8 +114,12 @@ macro_rules! newtype_index { // 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)*) => ( + (@type [$type:ident] + @pub [$($pub:tt)*] + @max [$max:expr] + @debug_name [$debug_name:expr] + derive [$($derives:ident),+] + $($tokens:tt)*) => ( newtype_index!( @derives [$($derives),+] @type [$type] @@ -122,7 +130,11 @@ macro_rules! newtype_index { ); // The case where no derives are added - (@type[$type:ident] @pub[$($pub:tt)*] @max[$max:expr] @debug_name[$debug_name:expr] $($tokens:tt)*) => ( + (@type [$type:ident] + @pub [$($pub:tt)*] + @max [$max:expr] + @debug_name [$debug_name:expr] + $($tokens:tt)*) => ( newtype_index!( @derives [] @type [$type] @@ -133,8 +145,12 @@ macro_rules! newtype_index { ); // 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_name [$debug_name:expr] + $name:ident = $constant:expr) => ( newtype_index!( @derives [$($derives),*] @type [$type] @@ -145,8 +161,13 @@ macro_rules! newtype_index { ); // 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_name [$debug_name:expr] + $(#[doc = $doc:expr])* + const $name:ident = $constant:expr) => ( newtype_index!( @derives [$($derives),*] @type [$type] @@ -157,8 +178,13 @@ macro_rules! newtype_index { ); // 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_name [$debug_name:expr] + MAX = $max:expr, + $($tokens:tt)*) => ( newtype_index!( @derives [$($derives),*] @type [$type] @@ -169,8 +195,13 @@ macro_rules! newtype_index { ); // 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)*) => ( + (@derives [$($derives:ident),*] + @type [$type:ident] + @pub [$($pub:tt)*] + @max [$max:expr] + @debug_name [$_debug_name:expr] + DEBUG_NAME = $debug_name:expr, + $($tokens:tt)*) => ( newtype_index!( @derives [$($derives),*] @type [$type] @@ -181,8 +212,14 @@ macro_rules! newtype_index { ); // 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_name [$debug_name:expr] + $(#[doc = $doc:expr])* + const $name:ident = $constant:expr, + $($tokens:tt)*) => ( $(#[doc = $doc])* pub const $name: $type = $type($constant); newtype_index!( From bf1198eb1fea71f94d4f0c3c3e968eaf183dfa65 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Tue, 31 Oct 2017 23:14:13 -0400 Subject: [PATCH 3/4] 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 120af4821e36f..c96040ab9b6e3 100644 --- a/src/librustc/dep_graph/serialized.rs +++ b/src/librustc/dep_graph/serialized.rs @@ -14,7 +14,7 @@ use dep_graph::DepNode; 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 a893fb82b4892..5d488074552ed 100644 --- a/src/librustc/hir/def_id.rs +++ b/src/librustc/hir/def_id.rs @@ -18,6 +18,9 @@ use std::u32; 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 e428ff185ac89..d43774bf3b084 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 02cb6585eb2aa..e79c846d44991 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 type LvalueProjection<'tcx> = Projection<'tcx, Lvalue<'tcx>, Local, Ty<'tcx> /// 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 @@ impl<'tcx> Debug for Lvalue<'tcx> { 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 f0b11dbc1f00b..8504534278257 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 @@ macro_rules! newtype_index { } } + 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 b206538324b4a..77496c7b8f218 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 From 97692afda9e493db65c833b931a6971fb58352b2 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Wed, 1 Nov 2017 21:25:37 -0400 Subject: [PATCH 4/4] newindex_type macro: make index private by default and allow pub through config --- src/librustc/hir/def_id.rs | 2 +- src/librustc/middle/region.rs | 6 +- src/librustc_data_structures/indexed_vec.rs | 101 ++++++++++---------- 3 files changed, 59 insertions(+), 50 deletions(-) diff --git a/src/librustc/hir/def_id.rs b/src/librustc/hir/def_id.rs index 5d488074552ed..cc30fdfaca6a3 100644 --- a/src/librustc/hir/def_id.rs +++ b/src/librustc/hir/def_id.rs @@ -16,7 +16,7 @@ use serialize::{self, Encoder, Decoder}; use std::fmt; use std::u32; -newtype_index!(CrateNum nopub +newtype_index!(CrateNum { derive[Debug] ENCODABLE = custom diff --git a/src/librustc/middle/region.rs b/src/librustc/middle/region.rs index d43774bf3b084..89707839144cc 100644 --- a/src/librustc/middle/region.rs +++ b/src/librustc/middle/region.rs @@ -156,7 +156,11 @@ pub struct BlockRemainder { pub first_statement_index: FirstStatementIndex, } -newtype_index!(FirstStatementIndex { MAX = SCOPE_DATA_REMAINDER_MAX }); +newtype_index!(FirstStatementIndex + { + pub idx + MAX = SCOPE_DATA_REMAINDER_MAX + }); impl From for Scope { #[inline] diff --git a/src/librustc_data_structures/indexed_vec.rs b/src/librustc_data_structures/indexed_vec.rs index 8504534278257..dc169c67a7632 100644 --- a/src/librustc_data_structures/indexed_vec.rs +++ b/src/librustc_data_structures/indexed_vec.rs @@ -47,16 +47,6 @@ macro_rules! newtype_index { 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_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_format ["{}"]); ); @@ -66,29 +56,17 @@ macro_rules! newtype_index { 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_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_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)*] + @type [$type:ident] @max [$max:expr] @debug_format [$debug_format:expr]) => ( #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, $($derives),*)] @@ -148,16 +126,43 @@ macro_rules! newtype_index { @debug_format [$debug_format]); ); - // Append comma to end of derives list if it's missing + // Handle the case where someone wants to make the internal field public + (@type [$type:ident] + @max [$max:expr] + @debug_format [$debug_format:expr] + pub idx + $($tokens:tt)*) => ( + newtype_index!( + @pub [pub] + @type [$type] + @max [$max] + @debug_format [$debug_format] + $($tokens)*); + ); + + // The default case is that the internal field is private (@type [$type:ident] - @pub [$($pub:tt)*] @max [$max:expr] @debug_format [$debug_format:expr] - derive [$($derives:ident),*] $($tokens:tt)*) => ( newtype_index!( + @pub [] @type [$type] + @max [$max] + @debug_format [$debug_format] + $($tokens)*); + ); + + // Append comma to end of derives list if it's missing + (@pub [$($pub:tt)*] + @type [$type:ident] + @max [$max:expr] + @debug_format [$debug_format:expr] + derive [$($derives:ident),*] + $($tokens:tt)*) => ( + newtype_index!( @pub [$($pub)*] + @type [$type] @max [$max] @debug_format [$debug_format] derive [$($derives,)*] @@ -166,8 +171,8 @@ macro_rules! newtype_index { // 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)*] + (@pub [$($pub:tt)*] + @type [$type:ident] @max [$max:expr] @debug_format [$debug_format:expr] derive [$($derives:ident,)+] @@ -175,8 +180,8 @@ macro_rules! newtype_index { $($tokens:tt)*) => ( newtype_index!( @derives [$($derives,)+] - @type [$type] @pub [$($pub)*] + @type [$type] @max [$max] @debug_format [$debug_format] $($tokens)*); @@ -184,16 +189,16 @@ macro_rules! newtype_index { // 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 isn't custom, add serialization traits by default. - (@type [$type:ident] - @pub [$($pub:tt)*] + (@pub [$($pub:tt)*] + @type [$type:ident] @max [$max:expr] @debug_format [$debug_format:expr] derive [$($derives:ident,)+] $($tokens:tt)*) => ( newtype_index!( @derives [$($derives,)+ RustcDecodable, RustcEncodable,] - @type [$type] @pub [$($pub)*] + @type [$type] @max [$max] @debug_format [$debug_format] $($tokens)*); @@ -201,31 +206,31 @@ macro_rules! newtype_index { // The case where no derives are added, but encodable is overriden. Don't // derive serialization traits - (@type [$type:ident] - @pub [$($pub:tt)*] + (@pub [$($pub:tt)*] + @type [$type:ident] @max [$max:expr] @debug_format [$debug_format:expr] ENCODABLE = custom $($tokens:tt)*) => ( newtype_index!( @derives [] - @type [$type] @pub [$($pub)*] + @type [$type] @max [$max] @debug_format [$debug_format] $($tokens)*); ); // The case where no derives are added, add serialization derives by default - (@type [$type:ident] - @pub [$($pub:tt)*] + (@pub [$($pub:tt)*] + @type [$type:ident] @max [$max:expr] @debug_format [$debug_format:expr] $($tokens:tt)*) => ( newtype_index!( @derives [RustcDecodable, RustcEncodable,] - @type [$type] @pub [$($pub)*] + @type [$type] @max [$max] @debug_format [$debug_format] $($tokens)*); @@ -233,15 +238,15 @@ macro_rules! newtype_index { // Rewrite final without comma to one that includes comma (@derives [$($derives:ident,)*] - @type [$type:ident] @pub [$($pub:tt)*] + @type [$type:ident] @max [$max:expr] @debug_format [$debug_format:expr] $name:ident = $constant:expr) => ( newtype_index!( @derives [$($derives,)*] - @type [$type] @pub [$($pub)*] + @type [$type] @max [$max] @debug_format [$debug_format] $name = $constant,); @@ -249,16 +254,16 @@ macro_rules! newtype_index { // Rewrite final const without comma to one that includes comma (@derives [$($derives:ident,)*] - @type [$type:ident] @pub [$($pub:tt)*] + @type [$type:ident] @max [$_max:expr] @debug_format [$debug_format:expr] $(#[doc = $doc:expr])* const $name:ident = $constant:expr) => ( newtype_index!( @derives [$($derives,)*] - @type [$type] @pub [$($pub)*] + @type [$type] @max [$max] @debug_format [$debug_format] $(#[doc = $doc])* const $name = $constant,); @@ -266,16 +271,16 @@ macro_rules! newtype_index { // Replace existing default for max (@derives [$($derives:ident,)*] - @type [$type:ident] @pub [$($pub:tt)*] + @type [$type:ident] @max [$_max:expr] @debug_format [$debug_format:expr] MAX = $max:expr, $($tokens:tt)*) => ( newtype_index!( @derives [$($derives,)*] - @type [$type] @pub [$($pub)*] + @type [$type] @max [$max] @debug_format [$debug_format] $($tokens)*); @@ -283,16 +288,16 @@ macro_rules! newtype_index { // Replace existing default for debug_format (@derives [$($derives:ident,)*] - @type [$type:ident] @pub [$($pub:tt)*] + @type [$type:ident] @max [$max:expr] @debug_format [$_debug_format:expr] DEBUG_FORMAT = $debug_format:expr, $($tokens:tt)*) => ( newtype_index!( @derives [$($derives,)*] - @type [$type] @pub [$($pub)*] + @type [$type] @max [$max] @debug_format [$debug_format] $($tokens)*); @@ -300,8 +305,8 @@ macro_rules! newtype_index { // Assign a user-defined constant (@derives [$($derives:ident,)*] - @type [$type:ident] @pub [$($pub:tt)*] + @type [$type:ident] @max [$max:expr] @debug_format [$debug_format:expr] $(#[doc = $doc:expr])* @@ -311,8 +316,8 @@ macro_rules! newtype_index { pub const $name: $type = $type($constant); newtype_index!( @derives [$($derives,)*] - @type [$type] @pub [$($pub)*] + @type [$type] @max [$max] @debug_format [$debug_format] $($tokens)*);