Skip to content

Commit 4e496de

Browse files
committed
Add derive and doc comment capabilities to newtype_index macro
1 parent 2be4cc0 commit 4e496de

File tree

6 files changed

+143
-62
lines changed

6 files changed

+143
-62
lines changed

src/librustc/dep_graph/serialized.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use dep_graph::DepNode;
1414
use ich::Fingerprint;
1515
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
1616

17-
newtype_index!(SerializedDepNodeIndex);
17+
newtype_index!(SerializedDepNodeIndex { derive[RustcEncodable, RustcDecodable] });
1818

1919
/// Data for use when recompiling the **current crate**.
2020
#[derive(Debug, RustcEncodable, RustcDecodable)]

src/librustc/hir/def_id.rs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,19 @@ use serialize::{self, Encoder, Decoder};
1616
use std::fmt;
1717
use std::u32;
1818

19-
#[derive(Clone, Copy, Eq, Ord, PartialOrd, PartialEq, Hash, Debug)]
20-
pub struct CrateNum(u32);
21-
22-
impl Idx for CrateNum {
23-
fn new(value: usize) -> Self {
24-
assert!(value < (u32::MAX) as usize);
25-
CrateNum(value as u32)
26-
}
27-
28-
fn index(self) -> usize {
29-
self.0 as usize
30-
}
31-
}
32-
33-
/// Item definitions in the currently-compiled crate would have the CrateNum
34-
/// LOCAL_CRATE in their DefId.
35-
pub const LOCAL_CRATE: CrateNum = CrateNum(0);
36-
37-
/// Virtual crate for builtin macros
38-
// FIXME(jseyfried): this is also used for custom derives until proc-macro crates get `CrateNum`s.
39-
pub const BUILTIN_MACROS_CRATE: CrateNum = CrateNum(u32::MAX);
40-
41-
/// A CrateNum value that indicates that something is wrong.
42-
pub const INVALID_CRATE: CrateNum = CrateNum(u32::MAX - 1);
19+
newtype_index!(CrateNum nopub
20+
{
21+
/// Item definitions in the currently-compiled crate would have the CrateNum
22+
/// LOCAL_CRATE in their DefId.
23+
const LOCAL_CRATE = 0,
24+
25+
/// Virtual crate for builtin macros
26+
// FIXME(jseyfried): this is also used for custom derives until proc-macro crates get `CrateNum`s.
27+
const BUILTIN_MACROS_CRATE = u32::MAX,
28+
29+
/// A CrateNum value that indicates that something is wrong.
30+
const INVALID_CRATE = u32::MAX - 1,
31+
});
4332

4433
impl CrateNum {
4534
pub fn new(x: usize) -> CrateNum {

src/librustc/middle/region.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ pub struct BlockRemainder {
158158

159159
newtype_index!(FirstStatementIndex
160160
{
161-
DEBUG_FORMAT = "{}",
161+
derive[RustcEncodable, RustcDecodable]
162+
DEBUG_NAME = "",
162163
MAX = SCOPE_DATA_REMAINDER_MAX,
163164
});
164165

src/librustc/mir/mod.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,8 @@ pub enum BorrowKind {
417417

418418
newtype_index!(Local
419419
{
420-
DEBUG_FORMAT = "_{}",
420+
derive[RustcEncodable, RustcDecodable]
421+
DEBUG_NAME = "_",
421422
const RETURN_POINTER = 0,
422423
});
423424

@@ -553,7 +554,11 @@ pub struct UpvarDecl {
553554
///////////////////////////////////////////////////////////////////////////
554555
// BasicBlock
555556

556-
newtype_index!(BasicBlock { DEBUG_FORMAT = "bb{}" });
557+
newtype_index!(BasicBlock
558+
{
559+
derive[RustcEncodable, RustcDecodable]
560+
DEBUG_NAME = "bb"
561+
});
557562

558563
///////////////////////////////////////////////////////////////////////////
559564
// BasicBlockData and Terminator
@@ -1135,7 +1140,11 @@ pub type LvalueProjection<'tcx> = Projection<'tcx, Lvalue<'tcx>, Local, Ty<'tcx>
11351140
/// and the index is a local.
11361141
pub type LvalueElem<'tcx> = ProjectionElem<'tcx, Local, Ty<'tcx>>;
11371142

1138-
newtype_index!(Field { DEBUG_FORMAT = "field[{}]" });
1143+
newtype_index!(Field
1144+
{
1145+
derive[RustcEncodable, RustcDecodable]
1146+
DEBUG_NAME = "field"
1147+
});
11391148

11401149
impl<'tcx> Lvalue<'tcx> {
11411150
pub fn field(self, f: Field, ty: Ty<'tcx>) -> Lvalue<'tcx> {
@@ -1202,7 +1211,8 @@ impl<'tcx> Debug for Lvalue<'tcx> {
12021211

12031212
newtype_index!(VisibilityScope
12041213
{
1205-
DEBUG_FORMAT = "scope[{}]",
1214+
derive[RustcEncodable, RustcDecodable]
1215+
DEBUG_NAME = "scope",
12061216
const ARGUMENT_VISIBILITY_SCOPE = 0,
12071217
});
12081218

@@ -1529,7 +1539,12 @@ pub struct Constant<'tcx> {
15291539
pub literal: Literal<'tcx>,
15301540
}
15311541

1532-
newtype_index!(Promoted { DEBUG_FORMAT = "promoted[{}]" });
1542+
newtype_index!(Promoted
1543+
{
1544+
derive[RustcEncodable, RustcDecodable]
1545+
DEBUG_NAME = "promoted"
1546+
});
1547+
15331548

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

src/librustc_data_structures/indexed_vec.rs

Lines changed: 106 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,77 +45,153 @@ macro_rules! newtype_index {
4545
// Use default constants
4646
($name:ident) => (
4747
newtype_index!(
48-
@type[$name]
49-
@max[::std::u32::MAX]
50-
@debug_format["{}"]);
48+
// Leave out derives marker so we can use its absence to ensure it comes first
49+
@type [$name]
50+
@pub [pub]
51+
@max [::std::u32::MAX]
52+
@debug_name [unsafe {::std::intrinsics::type_name::<$name>() }]);
53+
);
54+
55+
($name:ident nopub) => (
56+
newtype_index!(
57+
// Leave out derives marker so we can use its absence to ensure it comes first
58+
@type [$name]
59+
@pub []
60+
@max [::std::u32::MAX]
61+
@debug_name [unsafe {::std::intrinsics::type_name::<$name>() }]);
5162
);
5263

5364
// Define any constants
5465
($name:ident { $($tokens:tt)+ }) => (
5566
newtype_index!(
56-
@type[$name]
57-
@max[::std::u32::MAX]
58-
@debug_format["{}"]
59-
$($tokens)+);
67+
// Leave out derives marker so we can use its absence to ensure it comes first
68+
@type [$name]
69+
@pub [pub]
70+
@max [::std::u32::MAX]
71+
@debug_name [unsafe {::std::intrinsics::type_name::<$name>() }]
72+
$($tokens)+);
73+
);
74+
75+
// Define any constants
76+
($name:ident nopub { $($tokens:tt)+ }) => (
77+
newtype_index!(
78+
// Leave out derives marker so we can use its absence to ensure it comes first
79+
@type [$name]
80+
@pub []
81+
@max [::std::u32::MAX]
82+
@debug_name [unsafe {::std::intrinsics::type_name::<$name>() }]
83+
$($tokens)+);
6084
);
6185

6286
// ---- private rules ----
6387

6488
// Base case, user-defined constants (if any) have already been defined
65-
(@type[$type:ident] @max[$max:expr] @debug_format[$debug_format:expr]) => (
66-
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord,
67-
RustcEncodable, RustcDecodable)]
68-
pub struct $type(pub u32);
89+
(@derives[$($derives:ident),*] @type[$type:ident] @pub[$($pub:tt)*] @max[$max:expr] @debug_name[$debug_name:expr]) => (
90+
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, $($derives),*)]
91+
pub struct $type($($pub)* u32);
6992

7093
impl Idx for $type {
7194
fn new(value: usize) -> Self {
7295
assert!(value < ($max) as usize);
7396
$type(value as u32)
7497
}
98+
7599
fn index(self) -> usize {
76100
self.0 as usize
77101
}
78102
}
79103

80104
impl ::std::fmt::Debug for $type {
81105
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
82-
write!(fmt, $debug_format, self.0)
106+
write!(fmt, "{}{}", $debug_name, self.0)
83107
}
84108
}
85109
);
86110

111+
// By not including the @derives marker in this list nor in the default args, we can force it
112+
// to come first if it exists
113+
(@type[$type:ident] @pub[$($pub:tt)*] @max[$max:expr] @debug_name[$debug_name:expr]
114+
derive [$($derives:ident),+] $($tokens:tt)*) => (
115+
newtype_index!(
116+
@derives [$($derives),+]
117+
@type [$type]
118+
@pub [$($pub)*]
119+
@max [$max]
120+
@debug_name [$debug_name]
121+
$($tokens)*);
122+
);
123+
124+
// The case where no derives are added
125+
(@type[$type:ident] @pub[$($pub:tt)*] @max[$max:expr] @debug_name[$debug_name:expr] $($tokens:tt)*) => (
126+
newtype_index!(
127+
@derives []
128+
@type [$type]
129+
@pub [$($pub)*]
130+
@max [$max]
131+
@debug_name [$debug_name]
132+
$($tokens)*);
133+
);
134+
87135
// Rewrite final without comma to one that includes comma
88-
(@type[$type:ident] @max[$max:expr] @debug_format[$debug_format:expr]
136+
(@derives[$($derives:ident),*] @type[$type:ident] @pub[$($pub:tt)*] @max[$max:expr] @debug_name[$debug_name:expr]
89137
$name:ident = $constant:expr) => (
90-
newtype_index!(@type[$type] @max[$max] @debug_format[$debug_format] $name = $constant,);
138+
newtype_index!(
139+
@derives [$($derives),*]
140+
@type [$type]
141+
@pub [$($pub)*]
142+
@max [$max]
143+
@debug_name [$debug_name]
144+
$name = $constant,);
91145
);
92146

93147
// Rewrite final const without comma to one that includes comma
94-
(@type[$type:ident] @max[$_max:expr] @debug_format[$debug_format:expr]
95-
const $name:ident = $constant:expr) => (
96-
newtype_index!(@type[$type]
97-
@max[$max]
98-
@debug_format[$debug_format]
99-
const $name = $constant,);
148+
(@derives[$($derives:ident),*] @type[$type:ident] @pub[$($pub:tt)*] @max[$_max:expr] @debug_name[$debug_name:expr]
149+
$(#[doc = $doc:expr])* const $name:ident = $constant:expr) => (
150+
newtype_index!(
151+
@derives [$($derives),*]
152+
@type [$type]
153+
@pub [$($pub)*]
154+
@max [$max]
155+
@debug_name [$debug_name]
156+
$(#[doc = $doc])* const $name = $constant,);
100157
);
101158

102159
// Replace existing default for max
103-
(@type[$type:ident] @max[$_max:expr] @debug_format[$debug_format:expr]
160+
(@derives[$($derives:ident),*] @type[$type:ident] @pub[$($pub:tt)*] @max[$_max:expr] @debug_name[$debug_name:expr]
104161
MAX = $max:expr, $($tokens:tt)*) => (
105-
newtype_index!(@type[$type] @max[$max] @debug_format[$debug_format] $($tokens)*);
162+
newtype_index!(
163+
@derives [$($derives),*]
164+
@type [$type]
165+
@pub [$($pub)*]
166+
@max [$max]
167+
@debug_name [$debug_name]
168+
$($tokens)*);
106169
);
107170

108-
// Replace existing default for debug_format
109-
(@type[$type:ident] @max[$max:expr] @debug_format[$_debug_format:expr]
110-
DEBUG_FORMAT = $debug_format:expr, $($tokens:tt)*) => (
111-
newtype_index!(@type[$type] @max[$max] @debug_format[$debug_format] $($tokens)*);
171+
// Replace existing default for debug_name
172+
(@derives[$($derives:ident),*] @type[$type:ident] @pub[$($pub:tt)*] @max[$max:expr] @debug_name[$_debug_name:expr]
173+
DEBUG_NAME = $debug_name:expr, $($tokens:tt)*) => (
174+
newtype_index!(
175+
@derives [$($derives),*]
176+
@type [$type]
177+
@pub [$($pub)*]
178+
@max [$max]
179+
@debug_name [$debug_name]
180+
$($tokens)*);
112181
);
113182

114-
// Assign a user-defined constant (as final param)
115-
(@type[$type:ident] @max[$max:expr] @debug_format[$debug_format:expr]
116-
const $name:ident = $constant:expr, $($tokens:tt)*) => (
183+
// Assign a user-defined constant
184+
(@derives[$($derives:ident),*] @type[$type:ident] @pub[$($pub:tt)*] @max[$max:expr] @debug_name[$debug_name:expr]
185+
$(#[doc = $doc:expr])* const $name:ident = $constant:expr, $($tokens:tt)*) => (
186+
$(#[doc = $doc])*
117187
pub const $name: $type = $type($constant);
118-
newtype_index!(@type[$type] @max[$max] @debug_format[$debug_format] $($tokens)*);
188+
newtype_index!(
189+
@derives [$($derives),*]
190+
@type [$type]
191+
@pub [$($pub)*]
192+
@max [$max]
193+
@debug_name [$debug_name]
194+
$($tokens)*);
119195
);
120196
}
121197

src/librustc_mir/build/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ struct CFG<'tcx> {
312312
basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
313313
}
314314

315-
newtype_index!(ScopeId);
315+
newtype_index!(ScopeId { derive[RustcEncodable, RustcDecodable] });
316316

317317
///////////////////////////////////////////////////////////////////////////
318318
/// The `BlockAnd` "monad" packages up the new basic block along with a

0 commit comments

Comments
 (0)