Skip to content

Commit 9462c8b

Browse files
committed
Improve hygiene of newtype_index
Also add unit tests
1 parent ebbb2bf commit 9462c8b

File tree

13 files changed

+80
-52
lines changed

13 files changed

+80
-52
lines changed

src/librustc/dep_graph/serialized.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::dep_graph::DepNode;
44
use crate::ich::Fingerprint;
5-
use rustc_index::vec::{Idx, IndexVec};
5+
use rustc_index::vec::IndexVec;
66

77
rustc_index::newtype_index! {
88
pub struct SerializedDepNodeIndex { .. }

src/librustc/middle/region.rs

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use rustc_hir::Node;
1414

1515
use rustc_data_structures::fx::FxHashMap;
1616
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
17-
use rustc_index::vec::Idx;
1817
use rustc_macros::HashStable;
1918
use rustc_span::{Span, DUMMY_SP};
2019

src/librustc_hir/hir_id.rs

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ impl fmt::Display for HirId {
5656
rustc_data_structures::define_id_collections!(HirIdMap, HirIdSet, HirId);
5757
rustc_data_structures::define_id_collections!(ItemLocalMap, ItemLocalSet, ItemLocalId);
5858

59-
use rustc_index::vec::Idx;
6059
rustc_index::newtype_index! {
6160
/// An `ItemLocalId` uniquely identifies something within a given "item-like";
6261
/// that is, within a `hir::Item`, `hir::TraitItem`, or `hir::ImplItem`. There is no

src/librustc_index/vec.rs

+23-20
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,21 @@ macro_rules! newtype_index {
120120
impl $type {
121121
$v const MAX_AS_U32: u32 = $max;
122122

123-
$v const MAX: $type = $type::from_u32_const($max);
123+
$v const MAX: Self = Self::from_u32_const($max);
124124

125125
#[inline]
126126
$v fn from_usize(value: usize) -> Self {
127127
assert!(value <= ($max as usize));
128128
unsafe {
129-
$type::from_u32_unchecked(value as u32)
129+
Self::from_u32_unchecked(value as u32)
130130
}
131131
}
132132

133133
#[inline]
134134
$v fn from_u32(value: u32) -> Self {
135135
assert!(value <= $max);
136136
unsafe {
137-
$type::from_u32_unchecked(value)
137+
Self::from_u32_unchecked(value)
138138
}
139139
}
140140

@@ -152,13 +152,13 @@ macro_rules! newtype_index {
152152
];
153153

154154
unsafe {
155-
$type { private: value }
155+
Self { private: value }
156156
}
157157
}
158158

159159
#[inline]
160160
$v const unsafe fn from_u32_unchecked(value: u32) -> Self {
161-
$type { private: value }
161+
Self { private: value }
162162
}
163163

164164
/// Extracts the value of this index as an integer.
@@ -184,14 +184,14 @@ macro_rules! newtype_index {
184184
type Output = Self;
185185

186186
fn add(self, other: usize) -> Self {
187-
Self::new(self.index() + other)
187+
Self::from_usize(self.index() + other)
188188
}
189189
}
190190

191-
impl Idx for $type {
191+
impl $crate::vec::Idx for $type {
192192
#[inline]
193193
fn new(value: usize) -> Self {
194-
Self::from(value)
194+
Self::from_usize(value)
195195
}
196196

197197
#[inline]
@@ -204,39 +204,39 @@ macro_rules! newtype_index {
204204
#[inline]
205205
fn steps_between(start: &Self, end: &Self) -> Option<usize> {
206206
<usize as ::std::iter::Step>::steps_between(
207-
&Idx::index(*start),
208-
&Idx::index(*end),
207+
&Self::index(*start),
208+
&Self::index(*end),
209209
)
210210
}
211211

212212
#[inline]
213213
fn replace_one(&mut self) -> Self {
214-
::std::mem::replace(self, Self::new(1))
214+
::std::mem::replace(self, Self::from_u32(1))
215215
}
216216

217217
#[inline]
218218
fn replace_zero(&mut self) -> Self {
219-
::std::mem::replace(self, Self::new(0))
219+
::std::mem::replace(self, Self::from_u32(0))
220220
}
221221

222222
#[inline]
223223
fn add_one(&self) -> Self {
224-
Self::new(Idx::index(*self) + 1)
224+
Self::from_usize(Self::index(*self) + 1)
225225
}
226226

227227
#[inline]
228228
fn sub_one(&self) -> Self {
229-
Self::new(Idx::index(*self) - 1)
229+
Self::from_usize(Self::index(*self) - 1)
230230
}
231231

232232
#[inline]
233233
fn add_usize(&self, u: usize) -> Option<Self> {
234-
Idx::index(*self).checked_add(u).map(Self::new)
234+
Self::index(*self).checked_add(u).map(Self::from_usize)
235235
}
236236

237237
#[inline]
238238
fn sub_usize(&self, u: usize) -> Option<Self> {
239-
Idx::index(*self).checked_sub(u).map(Self::new)
239+
Self::index(*self).checked_sub(u).map(Self::from_usize)
240240
}
241241
}
242242

@@ -257,14 +257,14 @@ macro_rules! newtype_index {
257257
impl From<usize> for $type {
258258
#[inline]
259259
fn from(value: usize) -> Self {
260-
$type::from_usize(value)
260+
Self::from_usize(value)
261261
}
262262
}
263263

264264
impl From<u32> for $type {
265265
#[inline]
266266
fn from(value: u32) -> Self {
267-
$type::from_u32(value)
267+
Self::from_u32(value)
268268
}
269269
}
270270

@@ -409,7 +409,7 @@ macro_rules! newtype_index {
409409
(@decodable $type:ident) => (
410410
impl ::rustc_serialize::Decodable for $type {
411411
fn decode<D: ::rustc_serialize::Decoder>(d: &mut D) -> Result<Self, D::Error> {
412-
d.read_u32().map(Self::from)
412+
d.read_u32().map(Self::from_u32)
413413
}
414414
}
415415
);
@@ -500,7 +500,7 @@ macro_rules! newtype_index {
500500
const $name:ident = $constant:expr,
501501
$($tokens:tt)*) => (
502502
$(#[doc = $doc])*
503-
pub const $name: $type = $type::from_u32_const($constant);
503+
$v const $name: $type = $type::from_u32_const($constant);
504504
$crate::newtype_index!(
505505
@derives [$($derives,)*]
506506
@attrs [$(#[$attrs])*]
@@ -839,3 +839,6 @@ impl<I: Idx> FnMut<(usize,)> for IntoIdx<I> {
839839
I::new(n)
840840
}
841841
}
842+
843+
#[cfg(test)]
844+
mod tests;

src/librustc_index/vec/tests.rs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#![allow(dead_code)]
2+
newtype_index!(struct MyIdx { MAX = 0xFFFF_FFFA });
3+
4+
#[test]
5+
fn index_size_is_optimized() {
6+
use std::mem::size_of;
7+
8+
assert_eq!(size_of::<MyIdx>(), 4);
9+
// Uses 0xFFFF_FFFB
10+
assert_eq!(size_of::<Option<MyIdx>>(), 4);
11+
// Uses 0xFFFF_FFFC
12+
assert_eq!(size_of::<Option<Option<MyIdx>>>(), 4);
13+
// Uses 0xFFFF_FFFD
14+
assert_eq!(size_of::<Option<Option<Option<MyIdx>>>>(), 4);
15+
// Uses 0xFFFF_FFFE
16+
assert_eq!(size_of::<Option<Option<Option<Option<MyIdx>>>>>(), 4);
17+
// Uses 0xFFFF_FFFF
18+
assert_eq!(size_of::<Option<Option<Option<Option<Option<MyIdx>>>>>>(), 4);
19+
// Uses a tag
20+
assert_eq!(size_of::<Option<Option<Option<Option<Option<Option<MyIdx>>>>>>>(), 8);
21+
}
22+
23+
#[test]
24+
fn range_iterator_iterates_forwards() {
25+
let range = MyIdx::from_u32(1)..MyIdx::from_u32(4);
26+
assert_eq!(
27+
range.collect::<Vec<_>>(),
28+
[MyIdx::from_u32(1), MyIdx::from_u32(2), MyIdx::from_u32(3)]
29+
);
30+
}
31+
32+
#[test]
33+
fn range_iterator_iterates_backwards() {
34+
let range = MyIdx::from_u32(1)..MyIdx::from_u32(4);
35+
assert_eq!(
36+
range.rev().collect::<Vec<_>>(),
37+
[MyIdx::from_u32(3), MyIdx::from_u32(2), MyIdx::from_u32(1)]
38+
);
39+
}
40+
41+
#[test]
42+
fn range_count_is_correct() {
43+
let range = MyIdx::from_u32(1)..MyIdx::from_u32(4);
44+
assert_eq!(range.count(), 3);
45+
}
46+
47+
#[test]
48+
fn range_size_hint_is_correct() {
49+
let range = MyIdx::from_u32(1)..MyIdx::from_u32(4);
50+
assert_eq!(range.size_hint(), (3, Some(3)));
51+
}

src/librustc_mir/borrow_check/constraints/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc::mir::ConstraintCategory;
22
use rustc::ty::RegionVid;
33
use rustc_data_structures::graph::scc::Sccs;
4-
use rustc_index::vec::{Idx, IndexVec};
4+
use rustc_index::vec::IndexVec;
55
use std::fmt;
66
use std::ops::Index;
77

src/librustc_mir/borrow_check/member_constraints.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::rustc::ty::{self, Ty};
22
use rustc::infer::region_constraints::MemberConstraint;
33
use rustc_data_structures::fx::FxHashMap;
44
use rustc_hir::def_id::DefId;
5-
use rustc_index::vec::{Idx, IndexVec};
5+
use rustc_index::vec::IndexVec;
66
use rustc_span::Span;
77
use std::hash::Hash;
88
use std::ops::Index;

src/librustc_mir/borrow_check/type_check/liveness/local_use_map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc::mir::visit::{PlaceContext, Visitor};
22
use rustc::mir::{Local, Location, ReadOnlyBodyAndCache};
33
use rustc_data_structures::vec_linked_list as vll;
4-
use rustc_index::vec::{Idx, IndexVec};
4+
use rustc_index::vec::IndexVec;
55

66
use crate::util::liveness::{categorize, DefUse};
77

src/librustc_mir/dataflow/impls/borrows.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc::ty::{self, TyCtxt};
44

55
use rustc_data_structures::fx::FxHashMap;
66
use rustc_index::bit_set::BitSet;
7-
use rustc_index::vec::{Idx, IndexVec};
7+
use rustc_index::vec::IndexVec;
88

99
use crate::borrow_check::{
1010
places_conflict, BorrowData, BorrowSet, PlaceConflictBias, PlaceExt, RegionInferenceContext,

src/librustc_mir/dataflow/move_paths/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use core::slice::Iter;
22
use rustc::mir::*;
33
use rustc::ty::{ParamEnv, Ty, TyCtxt};
44
use rustc_data_structures::fx::FxHashMap;
5-
use rustc_index::vec::{Enumerated, Idx, IndexVec};
5+
use rustc_index::vec::{Enumerated, IndexVec};
66
use rustc_span::Span;
77
use smallvec::SmallVec;
88

src/librustc_session/node_id.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use rustc_index::vec::Idx;
21
use rustc_serialize::{Decoder, Encoder};
32
use rustc_span::ExpnId;
43
use std::fmt;

src/librustc_span/symbol.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use arena::DroplessArena;
66
use rustc_data_structures::fx::FxHashMap;
77
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
8-
use rustc_index::vec::Idx;
98
use rustc_macros::{symbols, HashStable_Generic};
109
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
1110
use rustc_serialize::{UseSpecializedDecodable, UseSpecializedEncodable};

src/test/ui-fulldeps/newtype_index.rs

-22
This file was deleted.

0 commit comments

Comments
 (0)