newtype_index: Support simpler serializable override, custom derive, and fix mir_opt tests

This commit is contained in:
Paul Daniel Faria 2017-10-31 23:14:13 -04:00
parent b46e42fe2a
commit bf1198eb1f
6 changed files with 214 additions and 143 deletions

View file

@ -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)]

View file

@ -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,

View 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]

View 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 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)]

View 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 @@ 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)
}
}
);
// 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)*) => (
// 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!(
@derives [$($derives),+]
@type [$type]
@pub [$($pub)*]
@max [$max]
@debug_name [$debug_name]
$($tokens)*);
@handle_debug
@derives [$($derives,)*]
@type [$type]
@debug_format [$debug_format]);
);
// The case where no derives are added
(@type [$type:ident]
@pub [$($pub:tt)*]
@max [$max:expr]
@debug_name [$debug_name:expr]
$($tokens:tt)*) => (
// 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!(
@derives []
@type [$type]
@pub [$($pub)*]
@max [$max]
@debug_name [$debug_name]
$($tokens)*);
@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. 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 []
@type [$type]
@pub [$($pub)*]
@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)*]
@max [$max:expr]
@debug_format [$debug_format:expr]
$($tokens:tt)*) => (
newtype_index!(
@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)*);
);
}

View 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