Skip to content

Commit 44990e5

Browse files
committed
Auto merge of #45770 - spastorino:newtype_index, r=nikomatsakis
Make last structs indexes definitions use newtype_index macro This PR makes the last two index structs not using newtype_index macro to use it and also fixes this #45763 issue.
2 parents 3b82e4c + d19faea commit 44990e5

File tree

10 files changed

+35
-60
lines changed

10 files changed

+35
-60
lines changed

src/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/librustc/dep_graph/graph.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,10 @@ pub struct DepGraph {
4545
}
4646

4747

48-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
49-
pub struct DepNodeIndex {
50-
index: u32,
51-
}
52-
53-
impl Idx for DepNodeIndex {
54-
fn new(idx: usize) -> Self {
55-
debug_assert!((idx & 0xFFFF_FFFF) == idx);
56-
DepNodeIndex { index: idx as u32 }
57-
}
58-
fn index(self) -> usize {
59-
self.index as usize
60-
}
61-
}
48+
newtype_index!(DepNodeIndex);
6249

6350
impl DepNodeIndex {
64-
const INVALID: DepNodeIndex = DepNodeIndex {
65-
index: ::std::u32::MAX,
66-
};
51+
const INVALID: DepNodeIndex = DepNodeIndex(::std::u32::MAX);
6752
}
6853

6954
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]

src/librustc/hir/def_id.rs

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,17 @@ impl serialize::UseSpecializedDecodable for CrateNum {
8686
///
8787
/// Since the DefIndex is mostly treated as an opaque ID, you probably
8888
/// don't have to care about these ranges.
89-
#[derive(Clone, Eq, Ord, PartialOrd, PartialEq, RustcEncodable,
90-
RustcDecodable, Hash, Copy)]
91-
pub struct DefIndex(u32);
89+
newtype_index!(DefIndex
90+
{
91+
DEBUG_FORMAT = custom,
9292

93-
impl Idx for DefIndex {
94-
fn new(value: usize) -> Self {
95-
assert!(value < (u32::MAX) as usize);
96-
DefIndex(value as u32)
97-
}
93+
/// The start of the "high" range of DefIndexes.
94+
const DEF_INDEX_HI_START = 1 << 31,
9895

99-
fn index(self) -> usize {
100-
self.0 as usize
101-
}
102-
}
96+
/// The crate root is always assigned index 0 by the AST Map code,
97+
/// thanks to `NodeCollector::new`.
98+
const CRATE_DEF_INDEX = 0,
99+
});
103100

104101
impl fmt::Debug for DefIndex {
105102
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -111,12 +108,6 @@ impl fmt::Debug for DefIndex {
111108
}
112109

113110
impl DefIndex {
114-
#[inline]
115-
pub fn new(x: usize) -> DefIndex {
116-
assert!(x < (u32::MAX as usize));
117-
DefIndex(x as u32)
118-
}
119-
120111
#[inline]
121112
pub fn from_u32(x: u32) -> DefIndex {
122113
DefIndex(x)
@@ -155,13 +146,6 @@ impl DefIndex {
155146
}
156147
}
157148

158-
/// The start of the "high" range of DefIndexes.
159-
const DEF_INDEX_HI_START: DefIndex = DefIndex(1 << 31);
160-
161-
/// The crate root is always assigned index 0 by the AST Map code,
162-
/// thanks to `NodeCollector::new`.
163-
pub const CRATE_DEF_INDEX: DefIndex = DefIndex(0);
164-
165149
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
166150
pub enum DefIndexAddressSpace {
167151
Low = 0,

src/librustc/hir/map/definitions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE, DefIndexAddressSpace,
1919
CRATE_DEF_INDEX};
2020
use ich::Fingerprint;
2121
use rustc_data_structures::fx::FxHashMap;
22-
use rustc_data_structures::indexed_vec::IndexVec;
22+
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
2323
use rustc_data_structures::stable_hasher::StableHasher;
2424
use serialize::{Encodable, Decodable, Encoder, Decoder};
2525
use session::CrateDisambiguator;

src/librustc/infer/region_inference/graphviz.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use graphviz as dot;
2020

2121
use hir::def_id::DefIndex;
22+
use rustc_data_structures::indexed_vec::Idx;
2223
use ty;
2324
use middle::free_region::RegionRelations;
2425
use middle::region;

src/librustc_data_structures/indexed_vec.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ macro_rules! newtype_index {
6868
@pub [$($pub:tt)*]
6969
@type [$type:ident]
7070
@max [$max:expr]
71-
@debug_format [$debug_format:expr]) => (
71+
@debug_format [$debug_format:tt]) => (
7272
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, $($derives),*)]
7373
pub struct $type($($pub)* u32);
7474

@@ -100,7 +100,7 @@ macro_rules! newtype_index {
100100
(@handle_debug
101101
@derives []
102102
@type [$type:ident]
103-
@debug_format [$debug_format:expr]) => (
103+
@debug_format [$debug_format:tt]) => (
104104
impl ::std::fmt::Debug for $type {
105105
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
106106
write!(fmt, $debug_format, self.0)
@@ -112,13 +112,13 @@ macro_rules! newtype_index {
112112
(@handle_debug
113113
@derives [Debug, $($derives:ident,)*]
114114
@type [$type:ident]
115-
@debug_format [$debug_format:expr]) => ();
115+
@debug_format [$debug_format:tt]) => ();
116116

117117
// It's not Debug, so just pop it off the front of the derives stack and check the rest.
118118
(@handle_debug
119119
@derives [$_derive:ident, $($derives:ident,)*]
120120
@type [$type:ident]
121-
@debug_format [$debug_format:expr]) => (
121+
@debug_format [$debug_format:tt]) => (
122122
newtype_index!(
123123
@handle_debug
124124
@derives [$($derives,)*]
@@ -129,7 +129,7 @@ macro_rules! newtype_index {
129129
// Handle the case where someone wants to make the internal field public
130130
(@type [$type:ident]
131131
@max [$max:expr]
132-
@debug_format [$debug_format:expr]
132+
@debug_format [$debug_format:tt]
133133
pub idx
134134
$($tokens:tt)*) => (
135135
newtype_index!(
@@ -143,7 +143,7 @@ macro_rules! newtype_index {
143143
// The default case is that the internal field is private
144144
(@type [$type:ident]
145145
@max [$max:expr]
146-
@debug_format [$debug_format:expr]
146+
@debug_format [$debug_format:tt]
147147
$($tokens:tt)*) => (
148148
newtype_index!(
149149
@pub []
@@ -157,7 +157,7 @@ macro_rules! newtype_index {
157157
(@pub [$($pub:tt)*]
158158
@type [$type:ident]
159159
@max [$max:expr]
160-
@debug_format [$debug_format:expr]
160+
@debug_format [$debug_format:tt]
161161
derive [$($derives:ident),*]
162162
$($tokens:tt)*) => (
163163
newtype_index!(
@@ -174,7 +174,7 @@ macro_rules! newtype_index {
174174
(@pub [$($pub:tt)*]
175175
@type [$type:ident]
176176
@max [$max:expr]
177-
@debug_format [$debug_format:expr]
177+
@debug_format [$debug_format:tt]
178178
derive [$($derives:ident,)+]
179179
ENCODABLE = custom
180180
$($tokens:tt)*) => (
@@ -192,7 +192,7 @@ macro_rules! newtype_index {
192192
(@pub [$($pub:tt)*]
193193
@type [$type:ident]
194194
@max [$max:expr]
195-
@debug_format [$debug_format:expr]
195+
@debug_format [$debug_format:tt]
196196
derive [$($derives:ident,)+]
197197
$($tokens:tt)*) => (
198198
newtype_index!(
@@ -209,7 +209,7 @@ macro_rules! newtype_index {
209209
(@pub [$($pub:tt)*]
210210
@type [$type:ident]
211211
@max [$max:expr]
212-
@debug_format [$debug_format:expr]
212+
@debug_format [$debug_format:tt]
213213
ENCODABLE = custom
214214
$($tokens:tt)*) => (
215215
newtype_index!(
@@ -225,7 +225,7 @@ macro_rules! newtype_index {
225225
(@pub [$($pub:tt)*]
226226
@type [$type:ident]
227227
@max [$max:expr]
228-
@debug_format [$debug_format:expr]
228+
@debug_format [$debug_format:tt]
229229
$($tokens:tt)*) => (
230230
newtype_index!(
231231
@derives [RustcDecodable, RustcEncodable,]
@@ -241,7 +241,7 @@ macro_rules! newtype_index {
241241
@pub [$($pub:tt)*]
242242
@type [$type:ident]
243243
@max [$max:expr]
244-
@debug_format [$debug_format:expr]
244+
@debug_format [$debug_format:tt]
245245
$name:ident = $constant:expr) => (
246246
newtype_index!(
247247
@derives [$($derives,)*]
@@ -257,7 +257,7 @@ macro_rules! newtype_index {
257257
@pub [$($pub:tt)*]
258258
@type [$type:ident]
259259
@max [$_max:expr]
260-
@debug_format [$debug_format:expr]
260+
@debug_format [$debug_format:tt]
261261
$(#[doc = $doc:expr])*
262262
const $name:ident = $constant:expr) => (
263263
newtype_index!(
@@ -274,7 +274,7 @@ macro_rules! newtype_index {
274274
@pub [$($pub:tt)*]
275275
@type [$type:ident]
276276
@max [$_max:expr]
277-
@debug_format [$debug_format:expr]
277+
@debug_format [$debug_format:tt]
278278
MAX = $max:expr,
279279
$($tokens:tt)*) => (
280280
newtype_index!(
@@ -291,8 +291,8 @@ macro_rules! newtype_index {
291291
@pub [$($pub:tt)*]
292292
@type [$type:ident]
293293
@max [$max:expr]
294-
@debug_format [$_debug_format:expr]
295-
DEBUG_FORMAT = $debug_format:expr,
294+
@debug_format [$_debug_format:tt]
295+
DEBUG_FORMAT = $debug_format:tt,
296296
$($tokens:tt)*) => (
297297
newtype_index!(
298298
@derives [$($derives,)*]
@@ -308,7 +308,7 @@ macro_rules! newtype_index {
308308
@pub [$($pub:tt)*]
309309
@type [$type:ident]
310310
@max [$max:expr]
311-
@debug_format [$debug_format:expr]
311+
@debug_format [$debug_format:tt]
312312
$(#[doc = $doc:expr])*
313313
const $name:ident = $constant:expr,
314314
$($tokens:tt)*) => (

src/librustc_metadata/decoder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ use std::str;
4141
use std::u32;
4242

4343
use rustc_serialize::{Decodable, Decoder, SpecializedDecoder, opaque};
44+
use rustc_data_structures::indexed_vec::Idx;
4445
use syntax::attr;
4546
use syntax::ast::{self, Ident};
4647
use syntax::codemap;

src/librustc_resolve/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ rustc = { path = "../librustc" }
1616
arena = { path = "../libarena" }
1717
rustc_errors = { path = "../librustc_errors" }
1818
syntax_pos = { path = "../libsyntax_pos" }
19+
rustc_data_structures = { path = "../librustc_data_structures" }

src/librustc_resolve/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ extern crate rustc_errors as errors;
2424
extern crate arena;
2525
#[macro_use]
2626
extern crate rustc;
27+
extern crate rustc_data_structures;
2728

2829
use self::Namespace::*;
2930
use self::TypeParameters::*;

src/librustc_resolve/macros.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use {Module, ModuleKind, NameBinding, NameBindingKind, PathResult};
1313
use Namespace::{self, MacroNS};
1414
use build_reduced_graph::BuildReducedGraphVisitor;
1515
use resolve_imports::ImportResolver;
16+
use rustc_data_structures::indexed_vec::Idx;
1617
use rustc::hir::def_id::{DefId, BUILTIN_MACROS_CRATE, CRATE_DEF_INDEX, DefIndex};
1718
use rustc::hir::def::{Def, Export};
1819
use rustc::hir::map::{self, DefCollector};

0 commit comments

Comments
 (0)