Skip to content

Commit

Permalink
Rollup merge of #53315 - nikomatsakis:newtype-index, r=Mark-Simulacrum
Browse files Browse the repository at this point in the history
use `NonZeroU32` in `newtype_index!`macro, change syntax

Various improvements to the `newtype_index!` macro:

- Use `NonZeroU32` so that `Option<T>` is cheap
- More ergonomic helper method, no need to import `Idx` trait all the time
- Improve syntax to use `struct` keyword so that ripgrep works to find type def'n

Fixes #50337

I'm curious to see if this passes tests =)
  • Loading branch information
kennytm committed Sep 8, 2018
2 parents b1ef2b8 + ab43c1e commit 51c3879
Show file tree
Hide file tree
Showing 44 changed files with 299 additions and 200 deletions.
12 changes: 8 additions & 4 deletions src/librustc/dep_graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ pub struct DepGraph {
fingerprints: Lrc<Lock<IndexVec<DepNodeIndex, Fingerprint>>>
}

newtype_index!(DepNodeIndex);
newtype_index! {
pub struct DepNodeIndex { .. }
}

impl DepNodeIndex {
const INVALID: DepNodeIndex = DepNodeIndex(::std::u32::MAX);
const INVALID: DepNodeIndex = DepNodeIndex::MAX;
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -1125,14 +1127,16 @@ impl DepNodeColorMap {
match self.values[index] {
COMPRESSED_NONE => None,
COMPRESSED_RED => Some(DepNodeColor::Red),
value => Some(DepNodeColor::Green(DepNodeIndex(value - COMPRESSED_FIRST_GREEN)))
value => Some(DepNodeColor::Green(DepNodeIndex::from_u32(
value - COMPRESSED_FIRST_GREEN
)))
}
}

fn insert(&mut self, index: SerializedDepNodeIndex, color: DepNodeColor) {
self.values[index] = match color {
DepNodeColor::Red => COMPRESSED_RED,
DepNodeColor::Green(index) => index.0 + COMPRESSED_FIRST_GREEN,
DepNodeColor::Green(index) => index.as_u32() + COMPRESSED_FIRST_GREEN,
}
}
}
4 changes: 3 additions & 1 deletion src/librustc/dep_graph/serialized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ use dep_graph::DepNode;
use ich::Fingerprint;
use rustc_data_structures::indexed_vec::{IndexVec, Idx};

newtype_index!(SerializedDepNodeIndex);
newtype_index! {
pub struct SerializedDepNodeIndex { .. }
}

/// Data for use when recompiling the **current crate**.
#[derive(Debug, RustcEncodable, RustcDecodable)]
Expand Down
30 changes: 9 additions & 21 deletions src/librustc/hir/def_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use serialize;
use std::fmt;
use std::u32;

newtype_index!(CrateNum
{
newtype_index! {
pub struct CrateNum {
ENCODABLE = custom
DEBUG_FORMAT = "crate{}",

Expand All @@ -27,40 +27,28 @@ newtype_index!(CrateNum
/// Virtual crate for builtin macros
// FIXME(jseyfried): this is also used for custom derives until proc-macro crates get
// `CrateNum`s.
const BUILTIN_MACROS_CRATE = u32::MAX,
const BUILTIN_MACROS_CRATE = CrateNum::MAX_AS_U32,

/// A CrateNum value that indicates that something is wrong.
const INVALID_CRATE = u32::MAX - 1,
const INVALID_CRATE = CrateNum::MAX_AS_U32 - 1,

/// A special CrateNum that we use for the tcx.rcache when decoding from
/// the incr. comp. cache.
const RESERVED_FOR_INCR_COMP_CACHE = u32::MAX - 2,
});
const RESERVED_FOR_INCR_COMP_CACHE = CrateNum::MAX_AS_U32 - 2,
}
}

impl CrateNum {
pub fn new(x: usize) -> CrateNum {
assert!(x < (u32::MAX as usize));
CrateNum(x as u32)
}

pub fn from_u32(x: u32) -> CrateNum {
CrateNum(x)
}

pub fn as_usize(&self) -> usize {
self.0 as usize
}

pub fn as_u32(&self) -> u32 {
self.0
CrateNum::from_usize(x)
}

pub fn as_def_id(&self) -> DefId { DefId { krate: *self, index: CRATE_DEF_INDEX } }
}

impl fmt::Display for CrateNum {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
fmt::Display::fmt(&self.as_u32(), f)
}
}

Expand Down
5 changes: 0 additions & 5 deletions src/librustc/ich/impls_mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ impl<'a> HashStable<StableHashingContext<'a>> for mir::Local {
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a>,
hasher: &mut StableHasher<W>) {
use rustc_data_structures::indexed_vec::Idx;
self.index().hash_stable(hcx, hasher);
}
}
Expand All @@ -112,7 +111,6 @@ impl<'a> HashStable<StableHashingContext<'a>> for mir::BasicBlock {
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a>,
hasher: &mut StableHasher<W>) {
use rustc_data_structures::indexed_vec::Idx;
self.index().hash_stable(hcx, hasher);
}
}
Expand All @@ -122,7 +120,6 @@ impl<'a> HashStable<StableHashingContext<'a>> for mir::Field {
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a>,
hasher: &mut StableHasher<W>) {
use rustc_data_structures::indexed_vec::Idx;
self.index().hash_stable(hcx, hasher);
}
}
Expand All @@ -133,7 +130,6 @@ for mir::SourceScope {
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a>,
hasher: &mut StableHasher<W>) {
use rustc_data_structures::indexed_vec::Idx;
self.index().hash_stable(hcx, hasher);
}
}
Expand All @@ -143,7 +139,6 @@ impl<'a> HashStable<StableHashingContext<'a>> for mir::Promoted {
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a>,
hasher: &mut StableHasher<W>) {
use rustc_data_structures::indexed_vec::Idx;
self.index().hash_stable(hcx, hasher);
}
}
Expand Down
3 changes: 0 additions & 3 deletions src/librustc/ich/impls_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ impl<'a> HashStable<StableHashingContext<'a>> for ty::RegionVid {
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a>,
hasher: &mut StableHasher<W>) {
use rustc_data_structures::indexed_vec::Idx;
self.index().hash_stable(hcx, hasher);
}
}
Expand All @@ -153,7 +152,6 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for ty::CanonicalVar {
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'gcx>,
hasher: &mut StableHasher<W>) {
use rustc_data_structures::indexed_vec::Idx;
self.index().hash_stable(hcx, hasher);
}
}
Expand Down Expand Up @@ -774,7 +772,6 @@ impl_stable_hash_for!(enum ty::cast::CastKind {
FnPtrAddrCast
});

impl_stable_hash_for!(tuple_struct ::middle::region::FirstStatementIndex { idx });
impl_stable_hash_for!(struct ::middle::region::Scope { id, code });

impl<'a> ToStableHashKey<StableHashingContext<'a>> for region::Scope {
Expand Down
2 changes: 0 additions & 2 deletions src/librustc/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ use syntax::ast::DUMMY_NODE_ID;
use syntax_pos::{Pos, Span};
use errors::{Applicability, DiagnosticBuilder, DiagnosticStyledString};

use rustc_data_structures::indexed_vec::Idx;

mod note;

mod need_type_info;
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/infer/region_constraints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use self::CombineMapType::*;
use super::{MiscVariable, RegionVariableOrigin, SubregionOrigin};
use super::unify_key;

use rustc_data_structures::indexed_vec::{IndexVec, Idx};
use rustc_data_structures::indexed_vec::IndexVec;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::unify as ut;
use ty::{self, Ty, TyCtxt};
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/infer/unify_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ impl UnifyValue for RegionVidKey {

impl UnifyKey for ty::RegionVid {
type Value = RegionVidKey;
fn index(&self) -> u32 { self.0 }
fn from_index(i: u32) -> ty::RegionVid { ty::RegionVid(i) }
fn index(&self) -> u32 { u32::from(*self) }
fn from_index(i: u32) -> ty::RegionVid { ty::RegionVid::from(i) }
fn tag() -> &'static str { "RegionVid" }
}

Expand Down
10 changes: 6 additions & 4 deletions src/librustc/middle/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,13 @@ pub struct BlockRemainder {
pub first_statement_index: FirstStatementIndex,
}

newtype_index!(FirstStatementIndex
{
pub idx
newtype_index! {
pub struct FirstStatementIndex {
MAX = SCOPE_DATA_REMAINDER_MAX
});
}
}

impl_stable_hash_for!(struct ::middle::region::FirstStatementIndex { private });

impl From<ScopeData> for Scope {
#[inline]
Expand Down
38 changes: 25 additions & 13 deletions src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,6 @@ pub struct Mir<'tcx> {
cache: cache::Cache,
}

/// where execution begins
pub const START_BLOCK: BasicBlock = BasicBlock(0);

impl<'tcx> Mir<'tcx> {
pub fn new(
basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
Expand Down Expand Up @@ -239,7 +236,7 @@ impl<'tcx> Mir<'tcx> {

#[inline]
pub fn local_kind(&self, local: Local) -> LocalKind {
let index = local.0 as usize;
let index = local.as_usize();
if index == 0 {
debug_assert!(
self.local_decls[local].mutability == Mutability::Mut,
Expand Down Expand Up @@ -523,11 +520,12 @@ impl BorrowKind {
///////////////////////////////////////////////////////////////////////////
// Variables and temps

newtype_index!(Local
{
newtype_index! {
pub struct Local {
DEBUG_FORMAT = "_{}",
const RETURN_PLACE = 0,
});
}
}

/// Classifies locals into categories. See `Mir::local_kind`.
#[derive(PartialEq, Eq, Debug)]
Expand Down Expand Up @@ -852,7 +850,12 @@ pub struct UpvarDecl {
///////////////////////////////////////////////////////////////////////////
// BasicBlock

newtype_index!(BasicBlock { DEBUG_FORMAT = "bb{}" });
newtype_index! {
pub struct BasicBlock {
DEBUG_FORMAT = "bb{}",
const START_BLOCK = 0,
}
}

impl BasicBlock {
pub fn start_location(self) -> Location {
Expand Down Expand Up @@ -1822,7 +1825,11 @@ pub type PlaceProjection<'tcx> = Projection<'tcx, Place<'tcx>, Local, Ty<'tcx>>;
/// and the index is a local.
pub type PlaceElem<'tcx> = ProjectionElem<'tcx, Local, Ty<'tcx>>;

newtype_index!(Field { DEBUG_FORMAT = "field[{}]" });
newtype_index! {
pub struct Field {
DEBUG_FORMAT = "field[{}]"
}
}

impl<'tcx> Place<'tcx> {
pub fn field(self, f: Field, ty: Ty<'tcx>) -> Place<'tcx> {
Expand Down Expand Up @@ -1895,11 +1902,12 @@ impl<'tcx> Debug for Place<'tcx> {
///////////////////////////////////////////////////////////////////////////
// Scopes

newtype_index!(SourceScope
{
newtype_index! {
pub struct SourceScope {
DEBUG_FORMAT = "scope[{}]",
const OUTERMOST_SOURCE_SCOPE = 0,
});
}
}

#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
pub struct SourceScopeData {
Expand Down Expand Up @@ -2271,7 +2279,11 @@ pub struct Constant<'tcx> {
pub literal: &'tcx ty::Const<'tcx>,
}

newtype_index!(Promoted { DEBUG_FORMAT = "promoted[{}]" });
newtype_index! {
pub struct Promoted {
DEBUG_FORMAT = "promoted[{}]"
}
}

impl<'tcx> Debug for Constant<'tcx> {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
Expand Down
Loading

0 comments on commit 51c3879

Please sign in to comment.