diff --git a/src/librustc/dep_graph/serialized.rs b/src/librustc/dep_graph/serialized.rs index 640877d3d55c3..45ef52dbf39c2 100644 --- a/src/librustc/dep_graph/serialized.rs +++ b/src/librustc/dep_graph/serialized.rs @@ -2,7 +2,7 @@ use crate::dep_graph::DepNode; use crate::ich::Fingerprint; -use rustc_index::vec::{Idx, IndexVec}; +use rustc_index::vec::IndexVec; rustc_index::newtype_index! { pub struct SerializedDepNodeIndex { .. } diff --git a/src/librustc/middle/region.rs b/src/librustc/middle/region.rs index b2a5efca92df3..9e9c8bd846473 100644 --- a/src/librustc/middle/region.rs +++ b/src/librustc/middle/region.rs @@ -14,7 +14,6 @@ use rustc_hir::Node; use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; -use rustc_index::vec::Idx; use rustc_macros::HashStable; use rustc_span::{Span, DUMMY_SP}; diff --git a/src/librustc_hir/hir_id.rs b/src/librustc_hir/hir_id.rs index 462946411718b..6d2ec44576353 100644 --- a/src/librustc_hir/hir_id.rs +++ b/src/librustc_hir/hir_id.rs @@ -56,7 +56,6 @@ impl fmt::Display for HirId { rustc_data_structures::define_id_collections!(HirIdMap, HirIdSet, HirId); rustc_data_structures::define_id_collections!(ItemLocalMap, ItemLocalSet, ItemLocalId); -use rustc_index::vec::Idx; rustc_index::newtype_index! { /// An `ItemLocalId` uniquely identifies something within a given "item-like"; /// that is, within a `hir::Item`, `hir::TraitItem`, or `hir::ImplItem`. There is no diff --git a/src/librustc_index/vec.rs b/src/librustc_index/vec.rs index 1f6a330cdc235..d14bafb44fd5b 100644 --- a/src/librustc_index/vec.rs +++ b/src/librustc_index/vec.rs @@ -120,13 +120,13 @@ macro_rules! newtype_index { impl $type { $v const MAX_AS_U32: u32 = $max; - $v const MAX: $type = $type::from_u32_const($max); + $v const MAX: Self = Self::from_u32_const($max); #[inline] $v fn from_usize(value: usize) -> Self { assert!(value <= ($max as usize)); unsafe { - $type::from_u32_unchecked(value as u32) + Self::from_u32_unchecked(value as u32) } } @@ -134,7 +134,7 @@ macro_rules! newtype_index { $v fn from_u32(value: u32) -> Self { assert!(value <= $max); unsafe { - $type::from_u32_unchecked(value) + Self::from_u32_unchecked(value) } } @@ -152,13 +152,13 @@ macro_rules! newtype_index { ]; unsafe { - $type { private: value } + Self { private: value } } } #[inline] $v const unsafe fn from_u32_unchecked(value: u32) -> Self { - $type { private: value } + Self { private: value } } /// Extracts the value of this index as an integer. @@ -184,14 +184,14 @@ macro_rules! newtype_index { type Output = Self; fn add(self, other: usize) -> Self { - Self::new(self.index() + other) + Self::from_usize(self.index() + other) } } - impl Idx for $type { + impl $crate::vec::Idx for $type { #[inline] fn new(value: usize) -> Self { - Self::from(value) + Self::from_usize(value) } #[inline] @@ -204,39 +204,39 @@ macro_rules! newtype_index { #[inline] fn steps_between(start: &Self, end: &Self) -> Option { ::steps_between( - &Idx::index(*start), - &Idx::index(*end), + &Self::index(*start), + &Self::index(*end), ) } #[inline] fn replace_one(&mut self) -> Self { - ::std::mem::replace(self, Self::new(1)) + ::std::mem::replace(self, Self::from_u32(1)) } #[inline] fn replace_zero(&mut self) -> Self { - ::std::mem::replace(self, Self::new(0)) + ::std::mem::replace(self, Self::from_u32(0)) } #[inline] fn add_one(&self) -> Self { - Self::new(Idx::index(*self) + 1) + Self::from_usize(Self::index(*self) + 1) } #[inline] fn sub_one(&self) -> Self { - Self::new(Idx::index(*self) - 1) + Self::from_usize(Self::index(*self) - 1) } #[inline] fn add_usize(&self, u: usize) -> Option { - Idx::index(*self).checked_add(u).map(Self::new) + Self::index(*self).checked_add(u).map(Self::from_usize) } #[inline] fn sub_usize(&self, u: usize) -> Option { - Idx::index(*self).checked_sub(u).map(Self::new) + Self::index(*self).checked_sub(u).map(Self::from_usize) } } @@ -257,14 +257,14 @@ macro_rules! newtype_index { impl From for $type { #[inline] fn from(value: usize) -> Self { - $type::from_usize(value) + Self::from_usize(value) } } impl From for $type { #[inline] fn from(value: u32) -> Self { - $type::from_u32(value) + Self::from_u32(value) } } @@ -409,7 +409,7 @@ macro_rules! newtype_index { (@decodable $type:ident) => ( impl ::rustc_serialize::Decodable for $type { fn decode(d: &mut D) -> Result { - d.read_u32().map(Self::from) + d.read_u32().map(Self::from_u32) } } ); @@ -500,7 +500,7 @@ macro_rules! newtype_index { const $name:ident = $constant:expr, $($tokens:tt)*) => ( $(#[doc = $doc])* - pub const $name: $type = $type::from_u32_const($constant); + $v const $name: $type = $type::from_u32_const($constant); $crate::newtype_index!( @derives [$($derives,)*] @attrs [$(#[$attrs])*] @@ -839,3 +839,6 @@ impl FnMut<(usize,)> for IntoIdx { I::new(n) } } + +#[cfg(test)] +mod tests; diff --git a/src/librustc_index/vec/tests.rs b/src/librustc_index/vec/tests.rs new file mode 100644 index 0000000000000..15c43c72c7b37 --- /dev/null +++ b/src/librustc_index/vec/tests.rs @@ -0,0 +1,51 @@ +#![allow(dead_code)] +newtype_index!(struct MyIdx { MAX = 0xFFFF_FFFA }); + +#[test] +fn index_size_is_optimized() { + use std::mem::size_of; + + assert_eq!(size_of::(), 4); + // Uses 0xFFFF_FFFB + assert_eq!(size_of::>(), 4); + // Uses 0xFFFF_FFFC + assert_eq!(size_of::>>(), 4); + // Uses 0xFFFF_FFFD + assert_eq!(size_of::>>>(), 4); + // Uses 0xFFFF_FFFE + assert_eq!(size_of::>>>>(), 4); + // Uses 0xFFFF_FFFF + assert_eq!(size_of::>>>>>(), 4); + // Uses a tag + assert_eq!(size_of::>>>>>>(), 8); +} + +#[test] +fn range_iterator_iterates_forwards() { + let range = MyIdx::from_u32(1)..MyIdx::from_u32(4); + assert_eq!( + range.collect::>(), + [MyIdx::from_u32(1), MyIdx::from_u32(2), MyIdx::from_u32(3)] + ); +} + +#[test] +fn range_iterator_iterates_backwards() { + let range = MyIdx::from_u32(1)..MyIdx::from_u32(4); + assert_eq!( + range.rev().collect::>(), + [MyIdx::from_u32(3), MyIdx::from_u32(2), MyIdx::from_u32(1)] + ); +} + +#[test] +fn range_count_is_correct() { + let range = MyIdx::from_u32(1)..MyIdx::from_u32(4); + assert_eq!(range.count(), 3); +} + +#[test] +fn range_size_hint_is_correct() { + let range = MyIdx::from_u32(1)..MyIdx::from_u32(4); + assert_eq!(range.size_hint(), (3, Some(3))); +} diff --git a/src/librustc_mir/borrow_check/constraints/mod.rs b/src/librustc_mir/borrow_check/constraints/mod.rs index 48defecd28cb0..ef70b127ac5bd 100644 --- a/src/librustc_mir/borrow_check/constraints/mod.rs +++ b/src/librustc_mir/borrow_check/constraints/mod.rs @@ -1,7 +1,7 @@ use rustc::mir::ConstraintCategory; use rustc::ty::RegionVid; use rustc_data_structures::graph::scc::Sccs; -use rustc_index::vec::{Idx, IndexVec}; +use rustc_index::vec::IndexVec; use std::fmt; use std::ops::Index; diff --git a/src/librustc_mir/borrow_check/member_constraints.rs b/src/librustc_mir/borrow_check/member_constraints.rs index 53fa5b1269849..c95919685bbc7 100644 --- a/src/librustc_mir/borrow_check/member_constraints.rs +++ b/src/librustc_mir/borrow_check/member_constraints.rs @@ -2,7 +2,7 @@ use crate::rustc::ty::{self, Ty}; use rustc::infer::region_constraints::MemberConstraint; use rustc_data_structures::fx::FxHashMap; use rustc_hir::def_id::DefId; -use rustc_index::vec::{Idx, IndexVec}; +use rustc_index::vec::IndexVec; use rustc_span::Span; use std::hash::Hash; use std::ops::Index; diff --git a/src/librustc_mir/borrow_check/type_check/liveness/local_use_map.rs b/src/librustc_mir/borrow_check/type_check/liveness/local_use_map.rs index 7bf355ddbf612..8155aa0ee000a 100644 --- a/src/librustc_mir/borrow_check/type_check/liveness/local_use_map.rs +++ b/src/librustc_mir/borrow_check/type_check/liveness/local_use_map.rs @@ -1,7 +1,7 @@ use rustc::mir::visit::{PlaceContext, Visitor}; use rustc::mir::{Local, Location, ReadOnlyBodyAndCache}; use rustc_data_structures::vec_linked_list as vll; -use rustc_index::vec::{Idx, IndexVec}; +use rustc_index::vec::IndexVec; use crate::util::liveness::{categorize, DefUse}; diff --git a/src/librustc_mir/dataflow/impls/borrows.rs b/src/librustc_mir/dataflow/impls/borrows.rs index 30eccebd3315f..8d51cb2391234 100644 --- a/src/librustc_mir/dataflow/impls/borrows.rs +++ b/src/librustc_mir/dataflow/impls/borrows.rs @@ -4,7 +4,7 @@ use rustc::ty::{self, TyCtxt}; use rustc_data_structures::fx::FxHashMap; use rustc_index::bit_set::BitSet; -use rustc_index::vec::{Idx, IndexVec}; +use rustc_index::vec::IndexVec; use crate::borrow_check::{ places_conflict, BorrowData, BorrowSet, PlaceConflictBias, PlaceExt, RegionInferenceContext, diff --git a/src/librustc_mir/dataflow/move_paths/mod.rs b/src/librustc_mir/dataflow/move_paths/mod.rs index 6450762d351b1..e5cddaf6c66f2 100644 --- a/src/librustc_mir/dataflow/move_paths/mod.rs +++ b/src/librustc_mir/dataflow/move_paths/mod.rs @@ -2,7 +2,7 @@ use core::slice::Iter; use rustc::mir::*; use rustc::ty::{ParamEnv, Ty, TyCtxt}; use rustc_data_structures::fx::FxHashMap; -use rustc_index::vec::{Enumerated, Idx, IndexVec}; +use rustc_index::vec::{Enumerated, IndexVec}; use rustc_span::Span; use smallvec::SmallVec; diff --git a/src/librustc_session/node_id.rs b/src/librustc_session/node_id.rs index 5e51c5a92ccc4..9fefe908e578e 100644 --- a/src/librustc_session/node_id.rs +++ b/src/librustc_session/node_id.rs @@ -1,4 +1,3 @@ -use rustc_index::vec::Idx; use rustc_serialize::{Decoder, Encoder}; use rustc_span::ExpnId; use std::fmt; diff --git a/src/librustc_span/symbol.rs b/src/librustc_span/symbol.rs index fea72744d31e4..40abc8b2179b8 100644 --- a/src/librustc_span/symbol.rs +++ b/src/librustc_span/symbol.rs @@ -5,7 +5,6 @@ use arena::DroplessArena; use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey}; -use rustc_index::vec::Idx; use rustc_macros::{symbols, HashStable_Generic}; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use rustc_serialize::{UseSpecializedDecodable, UseSpecializedEncodable}; diff --git a/src/test/ui-fulldeps/newtype_index.rs b/src/test/ui-fulldeps/newtype_index.rs deleted file mode 100644 index fe68b394e5a5c..0000000000000 --- a/src/test/ui-fulldeps/newtype_index.rs +++ /dev/null @@ -1,22 +0,0 @@ -// run-pass - -#![feature(rustc_private)] - -extern crate rustc_index; -extern crate serialize as rustc_serialize; - -use rustc_index::{newtype_index, vec::Idx}; - -newtype_index!(struct MyIdx { MAX = 0xFFFF_FFFA }); - -use std::mem::size_of; - -fn main() { - assert_eq!(size_of::(), 4); - assert_eq!(size_of::>(), 4); - assert_eq!(size_of::>>(), 4); - assert_eq!(size_of::>>>(), 4); - assert_eq!(size_of::>>>>(), 4); - assert_eq!(size_of::>>>>>(), 4); - assert_eq!(size_of::>>>>>>(), 8); -}