Make DefIndex use newtype_index macro

This commit is contained in:
Santiago Pastorino 2017-11-04 17:48:05 -03:00
parent f9bc8e7c4f
commit d19faead2d
8 changed files with 16 additions and 26 deletions

1
src/Cargo.lock generated
View file

@ -1834,6 +1834,7 @@ dependencies = [
"arena 0.0.0", "arena 0.0.0",
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc 0.0.0", "rustc 0.0.0",
"rustc_data_structures 0.0.0",
"rustc_errors 0.0.0", "rustc_errors 0.0.0",
"syntax 0.0.0", "syntax 0.0.0",
"syntax_pos 0.0.0", "syntax_pos 0.0.0",

View file

@ -86,20 +86,17 @@ fn default_decode<D: Decoder>(d: &mut D) -> Result<CrateNum, D::Error> {
/// ///
/// Since the DefIndex is mostly treated as an opaque ID, you probably /// Since the DefIndex is mostly treated as an opaque ID, you probably
/// don't have to care about these ranges. /// don't have to care about these ranges.
#[derive(Clone, Eq, Ord, PartialOrd, PartialEq, RustcEncodable, newtype_index!(DefIndex
RustcDecodable, Hash, Copy)] {
pub struct DefIndex(u32); DEBUG_FORMAT = custom,
impl Idx for DefIndex { /// The start of the "high" range of DefIndexes.
fn new(value: usize) -> Self { const DEF_INDEX_HI_START = 1 << 31,
assert!(value < (u32::MAX) as usize);
DefIndex(value as u32)
}
fn index(self) -> usize { /// The crate root is always assigned index 0 by the AST Map code,
self.0 as usize /// thanks to `NodeCollector::new`.
} const CRATE_DEF_INDEX = 0,
} });
impl fmt::Debug for DefIndex { impl fmt::Debug for DefIndex {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -111,12 +108,6 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
} }
impl DefIndex { impl DefIndex {
#[inline]
pub fn new(x: usize) -> DefIndex {
assert!(x < (u32::MAX as usize));
DefIndex(x as u32)
}
#[inline] #[inline]
pub fn from_u32(x: u32) -> DefIndex { pub fn from_u32(x: u32) -> DefIndex {
DefIndex(x) DefIndex(x)
@ -155,13 +146,6 @@ pub fn from_array_index(i: usize, address_space: DefIndexAddressSpace) -> DefInd
} }
} }
/// The start of the "high" range of DefIndexes.
const DEF_INDEX_HI_START: DefIndex = DefIndex(1 << 31);
/// The crate root is always assigned index 0 by the AST Map code,
/// thanks to `NodeCollector::new`.
pub const CRATE_DEF_INDEX: DefIndex = DefIndex(0);
#[derive(Copy, Clone, Eq, PartialEq, Hash)] #[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub enum DefIndexAddressSpace { pub enum DefIndexAddressSpace {
Low = 0, Low = 0,

View file

@ -19,7 +19,7 @@
CRATE_DEF_INDEX}; CRATE_DEF_INDEX};
use ich::Fingerprint; use ich::Fingerprint;
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::indexed_vec::IndexVec; use rustc_data_structures::indexed_vec::{IndexVec, Idx};
use rustc_data_structures::stable_hasher::StableHasher; use rustc_data_structures::stable_hasher::StableHasher;
use serialize::{Encodable, Decodable, Encoder, Decoder}; use serialize::{Encodable, Decodable, Encoder, Decoder};
use session::CrateDisambiguator; use session::CrateDisambiguator;

View file

@ -19,6 +19,7 @@
use graphviz as dot; use graphviz as dot;
use hir::def_id::DefIndex; use hir::def_id::DefIndex;
use rustc_data_structures::indexed_vec::Idx;
use ty; use ty;
use middle::free_region::RegionRelations; use middle::free_region::RegionRelations;
use middle::region; use middle::region;

View file

@ -41,6 +41,7 @@
use std::u32; use std::u32;
use rustc_serialize::{Decodable, Decoder, SpecializedDecoder, opaque}; use rustc_serialize::{Decodable, Decoder, SpecializedDecoder, opaque};
use rustc_data_structures::indexed_vec::Idx;
use syntax::attr; use syntax::attr;
use syntax::ast::{self, Ident}; use syntax::ast::{self, Ident};
use syntax::codemap; use syntax::codemap;

View file

@ -16,3 +16,4 @@ rustc = { path = "../librustc" }
arena = { path = "../libarena" } arena = { path = "../libarena" }
rustc_errors = { path = "../librustc_errors" } rustc_errors = { path = "../librustc_errors" }
syntax_pos = { path = "../libsyntax_pos" } syntax_pos = { path = "../libsyntax_pos" }
rustc_data_structures = { path = "../librustc_data_structures" }

View file

@ -24,6 +24,7 @@
extern crate arena; extern crate arena;
#[macro_use] #[macro_use]
extern crate rustc; extern crate rustc;
extern crate rustc_data_structures;
use self::Namespace::*; use self::Namespace::*;
use self::TypeParameters::*; use self::TypeParameters::*;

View file

@ -13,6 +13,7 @@
use Namespace::{self, MacroNS}; use Namespace::{self, MacroNS};
use build_reduced_graph::BuildReducedGraphVisitor; use build_reduced_graph::BuildReducedGraphVisitor;
use resolve_imports::ImportResolver; use resolve_imports::ImportResolver;
use rustc_data_structures::indexed_vec::Idx;
use rustc::hir::def_id::{DefId, BUILTIN_MACROS_CRATE, CRATE_DEF_INDEX, DefIndex}; use rustc::hir::def_id::{DefId, BUILTIN_MACROS_CRATE, CRATE_DEF_INDEX, DefIndex};
use rustc::hir::def::{Def, Export}; use rustc::hir::def::{Def, Export};
use rustc::hir::map::{self, DefCollector}; use rustc::hir::map::{self, DefCollector};