From 35545b36cf1f4fbc20f0a38c367d5f94a6f9a1cf Mon Sep 17 00:00:00 2001 From: Paul Faria Date: Sun, 8 Oct 2017 15:20:12 -0400 Subject: [PATCH 1/3] Improve newtype_index macro to handle description and constants consistently --- src/librustc/mir/mod.rs | 21 +++-- src/librustc_data_structures/indexed_vec.rs | 87 ++++++++++++++++----- 2 files changed, 80 insertions(+), 28 deletions(-) diff --git a/src/librustc/mir/mod.rs b/src/librustc/mir/mod.rs index b909269e1538e..a68a6acab3f33 100644 --- a/src/librustc/mir/mod.rs +++ b/src/librustc/mir/mod.rs @@ -402,9 +402,11 @@ pub enum BorrowKind { /////////////////////////////////////////////////////////////////////////// // Variables and temps -newtype_index!(Local, "_"); - -pub const RETURN_POINTER: Local = Local(0); +newtype_index!(Local, + const { + DESCRIPTION = "_", + RETURN_POINTER = 0, + }); /// Classifies locals into categories. See `Mir::local_kind`. #[derive(PartialEq, Eq, Debug)] @@ -538,7 +540,7 @@ pub struct UpvarDecl { /////////////////////////////////////////////////////////////////////////// // BasicBlock -newtype_index!(BasicBlock, "bb"); +newtype_index!(BasicBlock, const { DESCRIPTION = "bb" }); /////////////////////////////////////////////////////////////////////////// // BasicBlockData and Terminator @@ -1118,7 +1120,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, "field"); +newtype_index!(Field, const { DESCRIPTION = "field" }); impl<'tcx> Lvalue<'tcx> { pub fn field(self, f: Field, ty: Ty<'tcx>) -> Lvalue<'tcx> { @@ -1183,8 +1185,11 @@ impl<'tcx> Debug for Lvalue<'tcx> { /////////////////////////////////////////////////////////////////////////// // Scopes -newtype_index!(VisibilityScope, "scope"); -pub const ARGUMENT_VISIBILITY_SCOPE : VisibilityScope = VisibilityScope(0); +newtype_index!(VisibilityScope, + const { + DESCRIPTION = "scope", + ARGUMENT_VISIBILITY_SCOPE = 0, + }); #[derive(Clone, Debug, RustcEncodable, RustcDecodable)] pub struct VisibilityScopeData { @@ -1509,7 +1514,7 @@ pub struct Constant<'tcx> { pub literal: Literal<'tcx>, } -newtype_index!(Promoted, "promoted"); +newtype_index!(Promoted, const { DESCRIPTION = "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 4b7f55eba06be..0973bfd2429ba 100644 --- a/src/librustc_data_structures/indexed_vec.rs +++ b/src/librustc_data_structures/indexed_vec.rs @@ -40,39 +40,86 @@ impl Idx for u32 { #[macro_export] macro_rules! newtype_index { - ($name:ident) => ( - newtype_index!($name, unsafe { ::std::intrinsics::type_name::<$name>() }); - ); + // ---- private rules ---- - ($name:ident, $debug_name:expr) => ( + // Base case, user-defined constants (if any) have already been defined + (@type[$type:ident] @max[$max:expr] @descr[$descr:expr]) => ( #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, - RustcEncodable, RustcDecodable)] - pub struct $name(u32); - - impl $name { - // HACK use for constants - #[allow(unused)] - const fn const_new(x: u32) -> Self { - $name(x) - } - } + RustcEncodable, RustcDecodable)] + pub struct $type(u32); - impl Idx for $name { + impl Idx for $type { fn new(value: usize) -> Self { - assert!(value < (::std::u32::MAX) as usize); - $name(value as u32) + assert!(value < ($max) as usize); + $type(value as u32) } fn index(self) -> usize { self.0 as usize } } - impl ::std::fmt::Debug for $name { + 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, "{}{}", $descr, self.0) } } - ) + ); + + // Replace existing default for max (as final param) + (@type[$type:ident] @max[$_max:expr] @descr[$descr:expr] MAX = $max:expr) => ( + newtype_index!(@type[$type] @max[$max] @descr[$descr]); + ); + + // Replace existing default for max + (@type[$type:ident] @max[$_max:expr] @descr[$descr:expr] MAX = $max:expr, $($idents:ident = $constants:expr),*) => ( + newtype_index!(@type[$type] @max[$max] @descr[$descr]); + ); + + // Replace existing default for description (as final param) + (@type[$type:ident] @max[$max:expr] @descr[$_descr:expr] DESCRIPTION = $descr:expr) => ( + newtype_index!(@type[$type] @max[$max] @descr[$descr]); + ); + + // Replace existing default for description + (@type[$type:ident] @max[$max:expr] @descr[$_descr:expr] DESCRIPTION = $descr:expr, $($idents:ident = $constants:expr),*) => ( + newtype_index!(@type[$type] @max[$max] @descr[$descr] $($idents = $constants),*); + ); + + // Assign a user-defined constant (as final param) + (@type[$type:ident] @max[$max:expr] @descr[$descr:expr] $name:ident = $constant:expr) => ( + pub const $name: $type = $type($constant); + newtype_index!(@type[$type] @max[$max] @descr[$descr]); + ); + + // Assign a user-defined constant + (@type[$type:ident] @max[$max:expr] @descr[$descr:expr] $name:ident = $constant:expr, $($idents:ident = $constants:expr),*) => ( + pub const $name: $type = $type($constant); + newtype_index!(@type[$type] @max[$max] @descr[$descr] $($idents = $constants),*); + ); + + // ---- public rules ---- + + // Use default constants + ($name:ident) => ( + newtype_index!( + @type[$name] + @max[::std::u32::MAX] + @descr[unsafe {::std::intrinsics::type_name::<$name>() }]); + ); + + // Define any constants + ($name:ident, const { $($idents:ident = $constants:expr,)+ }) => ( + newtype_index!( + @type[$name] + @max[::std::u32::MAX] + @descr[unsafe {::std::intrinsics::type_name::<$name>() }] + $($idents = $constants),+); + ); + + // Rewrite missing trailing comma in const to version with trailing comma + ($name:ident, const { $($idents:ident = $constants:expr),+ }) => ( + newtype_index!($name, const { $($idents = $constants,)+ }); + ); } #[derive(Clone, PartialEq, Eq)] From 9423bee6d306195f8905cb4d594c047144e6d701 Mon Sep 17 00:00:00 2001 From: Paul Faria Date: Wed, 11 Oct 2017 20:01:55 -0400 Subject: [PATCH 2/3] Move const qualifier from brackets to constant values and remove comma from after identifier --- src/librustc/mir/mod.rs | 22 +++--- src/librustc_data_structures/indexed_vec.rs | 81 +++++++++------------ 2 files changed, 46 insertions(+), 57 deletions(-) diff --git a/src/librustc/mir/mod.rs b/src/librustc/mir/mod.rs index a68a6acab3f33..01e3957a80812 100644 --- a/src/librustc/mir/mod.rs +++ b/src/librustc/mir/mod.rs @@ -402,10 +402,10 @@ pub enum BorrowKind { /////////////////////////////////////////////////////////////////////////// // Variables and temps -newtype_index!(Local, - const { - DESCRIPTION = "_", - RETURN_POINTER = 0, +newtype_index!(Local + { + DEBUG_NAME = "_", + const RETURN_POINTER = 0, }); /// Classifies locals into categories. See `Mir::local_kind`. @@ -540,7 +540,7 @@ pub struct UpvarDecl { /////////////////////////////////////////////////////////////////////////// // BasicBlock -newtype_index!(BasicBlock, const { DESCRIPTION = "bb" }); +newtype_index!(BasicBlock { DEBUG_NAME = "bb" }); /////////////////////////////////////////////////////////////////////////// // BasicBlockData and Terminator @@ -1120,7 +1120,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, const { DESCRIPTION = "field" }); +newtype_index!(Field { DEBUG_NAME = "field" }); impl<'tcx> Lvalue<'tcx> { pub fn field(self, f: Field, ty: Ty<'tcx>) -> Lvalue<'tcx> { @@ -1185,10 +1185,10 @@ impl<'tcx> Debug for Lvalue<'tcx> { /////////////////////////////////////////////////////////////////////////// // Scopes -newtype_index!(VisibilityScope, - const { - DESCRIPTION = "scope", - ARGUMENT_VISIBILITY_SCOPE = 0, +newtype_index!(VisibilityScope + { + DEBUG_NAME = "scope", + const ARGUMENT_VISIBILITY_SCOPE = 0, }); #[derive(Clone, Debug, RustcEncodable, RustcDecodable)] @@ -1514,7 +1514,7 @@ pub struct Constant<'tcx> { pub literal: Literal<'tcx>, } -newtype_index!(Promoted, const { DESCRIPTION = "promoted" }); +newtype_index!(Promoted { 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 0973bfd2429ba..c10f7372a8d56 100644 --- a/src/librustc_data_structures/indexed_vec.rs +++ b/src/librustc_data_structures/indexed_vec.rs @@ -40,10 +40,29 @@ impl Idx for u32 { #[macro_export] macro_rules! newtype_index { + // ---- public rules ---- + + // Use default constants + ($name:ident) => ( + newtype_index!( + @type[$name] + @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_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] @descr[$descr:expr]) => ( + (@type[$type:ident] @max[$max:expr] @debug_name[$debug_name:expr]) => ( #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, RustcEncodable, RustcDecodable)] pub struct $type(u32); @@ -60,65 +79,35 @@ macro_rules! newtype_index { impl ::std::fmt::Debug for $type { fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { - write!(fmt, "{}{}", $descr, self.0) + write!(fmt, "{}{}", $debug_name, self.0) } } ); - // Replace existing default for max (as final param) - (@type[$type:ident] @max[$_max:expr] @descr[$descr:expr] MAX = $max:expr) => ( - newtype_index!(@type[$type] @max[$max] @descr[$descr]); + // Rewrite final without comma to one that includes comma + (@type[$type:ident] @max[$max:expr] @debug_name[$debug_name:expr] $name:ident = $constant:expr) => ( + newtype_index!(@type[$type] @max[$max] @debug_name[$debug_name] $name = $constant,); ); - // Replace existing default for max - (@type[$type:ident] @max[$_max:expr] @descr[$descr:expr] MAX = $max:expr, $($idents:ident = $constants:expr),*) => ( - newtype_index!(@type[$type] @max[$max] @descr[$descr]); + // Rewrite final const without comma to one that includes comma + (@type[$type:ident] @max[$_max:expr] @debug_name[$debug_name:expr] const $name:ident = $constant:expr) => ( + newtype_index!(@type[$type] @max[$max] @debug_name[$debug_name] const $name = $constant,); ); - // Replace existing default for description (as final param) - (@type[$type:ident] @max[$max:expr] @descr[$_descr:expr] DESCRIPTION = $descr:expr) => ( - newtype_index!(@type[$type] @max[$max] @descr[$descr]); + // Replace existing default for max + (@type[$type:ident] @max[$_max:expr] @debug_name[$debug_name:expr] MAX = $max:expr, $($tokens:tt)*) => ( + newtype_index!(@type[$type] @max[$max] @debug_name[$debug_name] $(tokens)*); ); - // Replace existing default for description - (@type[$type:ident] @max[$max:expr] @descr[$_descr:expr] DESCRIPTION = $descr:expr, $($idents:ident = $constants:expr),*) => ( - newtype_index!(@type[$type] @max[$max] @descr[$descr] $($idents = $constants),*); + // Replace existing default for debug_name + (@type[$type:ident] @max[$max:expr] @debug_name[$_debug_name:expr] DEBUG_NAME = $debug_name:expr, $($tokens:tt)*) => ( + newtype_index!(@type[$type] @max[$max] @debug_name[$debug_name] $($tokens)*); ); // Assign a user-defined constant (as final param) - (@type[$type:ident] @max[$max:expr] @descr[$descr:expr] $name:ident = $constant:expr) => ( + (@type[$type:ident] @max[$max:expr] @debug_name[$debug_name:expr] const $name:ident = $constant:expr, $($tokens:tt)*) => ( pub const $name: $type = $type($constant); - newtype_index!(@type[$type] @max[$max] @descr[$descr]); - ); - - // Assign a user-defined constant - (@type[$type:ident] @max[$max:expr] @descr[$descr:expr] $name:ident = $constant:expr, $($idents:ident = $constants:expr),*) => ( - pub const $name: $type = $type($constant); - newtype_index!(@type[$type] @max[$max] @descr[$descr] $($idents = $constants),*); - ); - - // ---- public rules ---- - - // Use default constants - ($name:ident) => ( - newtype_index!( - @type[$name] - @max[::std::u32::MAX] - @descr[unsafe {::std::intrinsics::type_name::<$name>() }]); - ); - - // Define any constants - ($name:ident, const { $($idents:ident = $constants:expr,)+ }) => ( - newtype_index!( - @type[$name] - @max[::std::u32::MAX] - @descr[unsafe {::std::intrinsics::type_name::<$name>() }] - $($idents = $constants),+); - ); - - // Rewrite missing trailing comma in const to version with trailing comma - ($name:ident, const { $($idents:ident = $constants:expr),+ }) => ( - newtype_index!($name, const { $($idents = $constants,)+ }); + newtype_index!(@type[$type] @max[$max] @debug_name[$debug_name] $($tokens)*); ); } From 97fe353ce65c67401ef3a9ce2b90a216d982daba Mon Sep 17 00:00:00 2001 From: Paul Faria Date: Wed, 11 Oct 2017 20:06:24 -0400 Subject: [PATCH 3/3] Split lines longer than 100 columns --- src/librustc_data_structures/indexed_vec.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/librustc_data_structures/indexed_vec.rs b/src/librustc_data_structures/indexed_vec.rs index c10f7372a8d56..6c5a37aa1e575 100644 --- a/src/librustc_data_structures/indexed_vec.rs +++ b/src/librustc_data_structures/indexed_vec.rs @@ -85,27 +85,32 @@ macro_rules! newtype_index { ); // Rewrite final without comma to one that includes comma - (@type[$type:ident] @max[$max:expr] @debug_name[$debug_name:expr] $name:ident = $constant:expr) => ( + (@type[$type:ident] @max[$max:expr] @debug_name[$debug_name:expr] + $name:ident = $constant:expr) => ( newtype_index!(@type[$type] @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_name[$debug_name:expr] const $name:ident = $constant:expr) => ( + (@type[$type:ident] @max[$_max:expr] @debug_name[$debug_name:expr] + const $name:ident = $constant:expr) => ( newtype_index!(@type[$type] @max[$max] @debug_name[$debug_name] const $name = $constant,); ); // Replace existing default for max - (@type[$type:ident] @max[$_max:expr] @debug_name[$debug_name:expr] MAX = $max:expr, $($tokens:tt)*) => ( + (@type[$type:ident] @max[$_max:expr] @debug_name[$debug_name:expr] + MAX = $max:expr, $($tokens:tt)*) => ( newtype_index!(@type[$type] @max[$max] @debug_name[$debug_name] $(tokens)*); ); // Replace existing default for debug_name - (@type[$type:ident] @max[$max:expr] @debug_name[$_debug_name:expr] DEBUG_NAME = $debug_name:expr, $($tokens:tt)*) => ( + (@type[$type:ident] @max[$max:expr] @debug_name[$_debug_name:expr] + DEBUG_NAME = $debug_name:expr, $($tokens:tt)*) => ( newtype_index!(@type[$type] @max[$max] @debug_name[$debug_name] $($tokens)*); ); // Assign a user-defined constant (as final param) - (@type[$type:ident] @max[$max:expr] @debug_name[$debug_name:expr] const $name:ident = $constant:expr, $($tokens:tt)*) => ( + (@type[$type:ident] @max[$max:expr] @debug_name[$debug_name:expr] + const $name:ident = $constant:expr, $($tokens:tt)*) => ( pub const $name: $type = $type($constant); newtype_index!(@type[$type] @max[$max] @debug_name[$debug_name] $($tokens)*); );