Skip to content

Commit 35545b3

Browse files
committed
Improve newtype_index macro to handle description and constants consistently
1 parent ade0b01 commit 35545b3

File tree

2 files changed

+80
-28
lines changed

2 files changed

+80
-28
lines changed

src/librustc/mir/mod.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,11 @@ pub enum BorrowKind {
402402
///////////////////////////////////////////////////////////////////////////
403403
// Variables and temps
404404

405-
newtype_index!(Local, "_");
406-
407-
pub const RETURN_POINTER: Local = Local(0);
405+
newtype_index!(Local,
406+
const {
407+
DESCRIPTION = "_",
408+
RETURN_POINTER = 0,
409+
});
408410

409411
/// Classifies locals into categories. See `Mir::local_kind`.
410412
#[derive(PartialEq, Eq, Debug)]
@@ -538,7 +540,7 @@ pub struct UpvarDecl {
538540
///////////////////////////////////////////////////////////////////////////
539541
// BasicBlock
540542

541-
newtype_index!(BasicBlock, "bb");
543+
newtype_index!(BasicBlock, const { DESCRIPTION = "bb" });
542544

543545
///////////////////////////////////////////////////////////////////////////
544546
// BasicBlockData and Terminator
@@ -1118,7 +1120,7 @@ pub type LvalueProjection<'tcx> = Projection<'tcx, Lvalue<'tcx>, Local, Ty<'tcx>
11181120
/// and the index is a local.
11191121
pub type LvalueElem<'tcx> = ProjectionElem<'tcx, Local, Ty<'tcx>>;
11201122

1121-
newtype_index!(Field, "field");
1123+
newtype_index!(Field, const { DESCRIPTION = "field" });
11221124

11231125
impl<'tcx> Lvalue<'tcx> {
11241126
pub fn field(self, f: Field, ty: Ty<'tcx>) -> Lvalue<'tcx> {
@@ -1183,8 +1185,11 @@ impl<'tcx> Debug for Lvalue<'tcx> {
11831185
///////////////////////////////////////////////////////////////////////////
11841186
// Scopes
11851187

1186-
newtype_index!(VisibilityScope, "scope");
1187-
pub const ARGUMENT_VISIBILITY_SCOPE : VisibilityScope = VisibilityScope(0);
1188+
newtype_index!(VisibilityScope,
1189+
const {
1190+
DESCRIPTION = "scope",
1191+
ARGUMENT_VISIBILITY_SCOPE = 0,
1192+
});
11881193

11891194
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
11901195
pub struct VisibilityScopeData {
@@ -1509,7 +1514,7 @@ pub struct Constant<'tcx> {
15091514
pub literal: Literal<'tcx>,
15101515
}
15111516

1512-
newtype_index!(Promoted, "promoted");
1517+
newtype_index!(Promoted, const { DESCRIPTION = "promoted" });
15131518

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

src/librustc_data_structures/indexed_vec.rs

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,39 +40,86 @@ impl Idx for u32 {
4040

4141
#[macro_export]
4242
macro_rules! newtype_index {
43-
($name:ident) => (
44-
newtype_index!($name, unsafe { ::std::intrinsics::type_name::<$name>() });
45-
);
43+
// ---- private rules ----
4644

47-
($name:ident, $debug_name:expr) => (
45+
// Base case, user-defined constants (if any) have already been defined
46+
(@type[$type:ident] @max[$max:expr] @descr[$descr:expr]) => (
4847
#[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-
}
48+
RustcEncodable, RustcDecodable)]
49+
pub struct $type(u32);
5950

60-
impl Idx for $name {
51+
impl Idx for $type {
6152
fn new(value: usize) -> Self {
62-
assert!(value < (::std::u32::MAX) as usize);
63-
$name(value as u32)
53+
assert!(value < ($max) as usize);
54+
$type(value as u32)
6455
}
6556
fn index(self) -> usize {
6657
self.0 as usize
6758
}
6859
}
6960

70-
impl ::std::fmt::Debug for $name {
61+
impl ::std::fmt::Debug for $type {
7162
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
72-
write!(fmt, "{}{}", $debug_name, self.0)
63+
write!(fmt, "{}{}", $descr, self.0)
7364
}
7465
}
75-
)
66+
);
67+
68+
// Replace existing default for max (as final param)
69+
(@type[$type:ident] @max[$_max:expr] @descr[$descr:expr] MAX = $max:expr) => (
70+
newtype_index!(@type[$type] @max[$max] @descr[$descr]);
71+
);
72+
73+
// Replace existing default for max
74+
(@type[$type:ident] @max[$_max:expr] @descr[$descr:expr] MAX = $max:expr, $($idents:ident = $constants:expr),*) => (
75+
newtype_index!(@type[$type] @max[$max] @descr[$descr]);
76+
);
77+
78+
// Replace existing default for description (as final param)
79+
(@type[$type:ident] @max[$max:expr] @descr[$_descr:expr] DESCRIPTION = $descr:expr) => (
80+
newtype_index!(@type[$type] @max[$max] @descr[$descr]);
81+
);
82+
83+
// Replace existing default for description
84+
(@type[$type:ident] @max[$max:expr] @descr[$_descr:expr] DESCRIPTION = $descr:expr, $($idents:ident = $constants:expr),*) => (
85+
newtype_index!(@type[$type] @max[$max] @descr[$descr] $($idents = $constants),*);
86+
);
87+
88+
// Assign a user-defined constant (as final param)
89+
(@type[$type:ident] @max[$max:expr] @descr[$descr:expr] $name:ident = $constant:expr) => (
90+
pub const $name: $type = $type($constant);
91+
newtype_index!(@type[$type] @max[$max] @descr[$descr]);
92+
);
93+
94+
// Assign a user-defined constant
95+
(@type[$type:ident] @max[$max:expr] @descr[$descr:expr] $name:ident = $constant:expr, $($idents:ident = $constants:expr),*) => (
96+
pub const $name: $type = $type($constant);
97+
newtype_index!(@type[$type] @max[$max] @descr[$descr] $($idents = $constants),*);
98+
);
99+
100+
// ---- public rules ----
101+
102+
// Use default constants
103+
($name:ident) => (
104+
newtype_index!(
105+
@type[$name]
106+
@max[::std::u32::MAX]
107+
@descr[unsafe {::std::intrinsics::type_name::<$name>() }]);
108+
);
109+
110+
// Define any constants
111+
($name:ident, const { $($idents:ident = $constants:expr,)+ }) => (
112+
newtype_index!(
113+
@type[$name]
114+
@max[::std::u32::MAX]
115+
@descr[unsafe {::std::intrinsics::type_name::<$name>() }]
116+
$($idents = $constants),+);
117+
);
118+
119+
// Rewrite missing trailing comma in const to version with trailing comma
120+
($name:ident, const { $($idents:ident = $constants:expr),+ }) => (
121+
newtype_index!($name, const { $($idents = $constants,)+ });
122+
);
76123
}
77124

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

0 commit comments

Comments
 (0)