Skip to content

Commit 82f2c28

Browse files
authored
Rollup merge of rust-lang#45110 - Nashenas88:master, r=arielb1
Improve newtype_index macro to handle description and constants consistently
2 parents 6c43bd3 + 97fe353 commit 82f2c28

File tree

2 files changed

+71
-25
lines changed

2 files changed

+71
-25
lines changed

src/librustc/mir/mod.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,11 @@ pub enum BorrowKind {
415415
///////////////////////////////////////////////////////////////////////////
416416
// Variables and temps
417417

418-
newtype_index!(Local, "_");
419-
420-
pub const RETURN_POINTER: Local = Local(0);
418+
newtype_index!(Local
419+
{
420+
DEBUG_NAME = "_",
421+
const RETURN_POINTER = 0,
422+
});
421423

422424
/// Classifies locals into categories. See `Mir::local_kind`.
423425
#[derive(PartialEq, Eq, Debug)]
@@ -551,7 +553,7 @@ pub struct UpvarDecl {
551553
///////////////////////////////////////////////////////////////////////////
552554
// BasicBlock
553555

554-
newtype_index!(BasicBlock, "bb");
556+
newtype_index!(BasicBlock { DEBUG_NAME = "bb" });
555557

556558
///////////////////////////////////////////////////////////////////////////
557559
// BasicBlockData and Terminator
@@ -1131,7 +1133,7 @@ pub type LvalueProjection<'tcx> = Projection<'tcx, Lvalue<'tcx>, Local, Ty<'tcx>
11311133
/// and the index is a local.
11321134
pub type LvalueElem<'tcx> = ProjectionElem<'tcx, Local, Ty<'tcx>>;
11331135

1134-
newtype_index!(Field, "field");
1136+
newtype_index!(Field { DEBUG_NAME = "field" });
11351137

11361138
impl<'tcx> Lvalue<'tcx> {
11371139
pub fn field(self, f: Field, ty: Ty<'tcx>) -> Lvalue<'tcx> {
@@ -1196,8 +1198,11 @@ impl<'tcx> Debug for Lvalue<'tcx> {
11961198
///////////////////////////////////////////////////////////////////////////
11971199
// Scopes
11981200

1199-
newtype_index!(VisibilityScope, "scope");
1200-
pub const ARGUMENT_VISIBILITY_SCOPE : VisibilityScope = VisibilityScope(0);
1201+
newtype_index!(VisibilityScope
1202+
{
1203+
DEBUG_NAME = "scope",
1204+
const ARGUMENT_VISIBILITY_SCOPE = 0,
1205+
});
12011206

12021207
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
12031208
pub struct VisibilityScopeData {
@@ -1522,7 +1527,7 @@ pub struct Constant<'tcx> {
15221527
pub literal: Literal<'tcx>,
15231528
}
15241529

1525-
newtype_index!(Promoted, "promoted");
1530+
newtype_index!(Promoted { DEBUG_NAME = "promoted" });
15261531

15271532
#[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
15281533
pub enum Literal<'tcx> {

src/librustc_data_structures/indexed_vec.rs

+58-17
Original file line numberDiff line numberDiff line change
@@ -40,39 +40,80 @@ impl Idx for u32 {
4040

4141
#[macro_export]
4242
macro_rules! newtype_index {
43+
// ---- public rules ----
44+
45+
// Use default constants
4346
($name:ident) => (
44-
newtype_index!($name, unsafe { ::std::intrinsics::type_name::<$name>() });
47+
newtype_index!(
48+
@type[$name]
49+
@max[::std::u32::MAX]
50+
@debug_name[unsafe {::std::intrinsics::type_name::<$name>() }]);
51+
);
52+
53+
// Define any constants
54+
($name:ident { $($tokens:tt)+ }) => (
55+
newtype_index!(
56+
@type[$name]
57+
@max[::std::u32::MAX]
58+
@debug_name[unsafe {::std::intrinsics::type_name::<$name>() }]
59+
$($tokens)+);
4560
);
4661

47-
($name:ident, $debug_name:expr) => (
62+
// ---- private rules ----
63+
64+
// Base case, user-defined constants (if any) have already been defined
65+
(@type[$type:ident] @max[$max:expr] @debug_name[$debug_name:expr]) => (
4866
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord,
49-
RustcEncodable, RustcDecodable)]
50-
pub struct $name(u32);
51-
52-
impl $name {
53-
// HACK use for constants
54-
#[allow(unused)]
55-
const fn const_new(x: u32) -> Self {
56-
$name(x)
57-
}
58-
}
67+
RustcEncodable, RustcDecodable)]
68+
pub struct $type(u32);
5969

60-
impl Idx for $name {
70+
impl Idx for $type {
6171
fn new(value: usize) -> Self {
62-
assert!(value < (::std::u32::MAX) as usize);
63-
$name(value as u32)
72+
assert!(value < ($max) as usize);
73+
$type(value as u32)
6474
}
6575
fn index(self) -> usize {
6676
self.0 as usize
6777
}
6878
}
6979

70-
impl ::std::fmt::Debug for $name {
80+
impl ::std::fmt::Debug for $type {
7181
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
7282
write!(fmt, "{}{}", $debug_name, self.0)
7383
}
7484
}
75-
)
85+
);
86+
87+
// Rewrite final without comma to one that includes comma
88+
(@type[$type:ident] @max[$max:expr] @debug_name[$debug_name:expr]
89+
$name:ident = $constant:expr) => (
90+
newtype_index!(@type[$type] @max[$max] @debug_name[$debug_name] $name = $constant,);
91+
);
92+
93+
// Rewrite final const without comma to one that includes comma
94+
(@type[$type:ident] @max[$_max:expr] @debug_name[$debug_name:expr]
95+
const $name:ident = $constant:expr) => (
96+
newtype_index!(@type[$type] @max[$max] @debug_name[$debug_name] const $name = $constant,);
97+
);
98+
99+
// Replace existing default for max
100+
(@type[$type:ident] @max[$_max:expr] @debug_name[$debug_name:expr]
101+
MAX = $max:expr, $($tokens:tt)*) => (
102+
newtype_index!(@type[$type] @max[$max] @debug_name[$debug_name] $(tokens)*);
103+
);
104+
105+
// Replace existing default for debug_name
106+
(@type[$type:ident] @max[$max:expr] @debug_name[$_debug_name:expr]
107+
DEBUG_NAME = $debug_name:expr, $($tokens:tt)*) => (
108+
newtype_index!(@type[$type] @max[$max] @debug_name[$debug_name] $($tokens)*);
109+
);
110+
111+
// Assign a user-defined constant (as final param)
112+
(@type[$type:ident] @max[$max:expr] @debug_name[$debug_name:expr]
113+
const $name:ident = $constant:expr, $($tokens:tt)*) => (
114+
pub const $name: $type = $type($constant);
115+
newtype_index!(@type[$type] @max[$max] @debug_name[$debug_name] $($tokens)*);
116+
);
76117
}
77118

78119
#[derive(Clone, PartialEq, Eq)]

0 commit comments

Comments
 (0)