Skip to content

Instantly share code, notes, and snippets.

Created September 28, 2017 23:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/341c6951962f1ca62da32fb4c99d51c3 to your computer and use it in GitHub Desktop.
Save anonymous/341c6951962f1ca62da32fb4c99d51c3 to your computer and use it in GitHub Desktop.
From 2f35cfa1e8b156a7f52eaf25109e9d59ba12531f Mon Sep 17 00:00:00 2001
From: Santiago Pastorino <spastorino@gmail.com>
Date: Thu, 28 Sep 2017 19:25:00 -0300
Subject: [PATCH] Generate FirstStatementIndex using newtype_index macro
---
src/librustc/dep_graph/graph.rs | 3 ++-
src/librustc/dep_graph/serialized.rs | 3 ++-
src/librustc/ich/impls_ty.rs | 2 +-
src/librustc/middle/region.rs | 23 ++---------------------
src/librustc/mir/mod.rs | 18 +++++++++---------
src/librustc_data_structures/indexed_vec.rs | 17 ++++++++++-------
src/librustc_incremental/persist/data.rs | 3 ++-
src/librustc_mir/build/mod.rs | 2 +-
src/librustc_mir/transform/nll.rs | 3 ++-
9 files changed, 31 insertions(+), 43 deletions(-)
diff --git a/src/librustc/dep_graph/graph.rs b/src/librustc/dep_graph/graph.rs
index 1d52c9cd76..a2b930c5f5 100644
--- a/src/librustc/dep_graph/graph.rs
+++ b/src/librustc/dep_graph/graph.rs
@@ -16,6 +16,7 @@ use session::config::OutputType;
use std::cell::{Ref, RefCell};
use std::hash::Hash;
use std::rc::Rc;
+use std::u32;
use util::common::{ProfileQueriesMsg, profq_msg};
use ich::Fingerprint;
@@ -563,7 +564,7 @@ impl CurrentDepGraph {
}
}
-newtype_index!(DepNodeIndexNew);
+newtype_index!(DepNodeIndexNew, u32::MAX);
impl DepNodeIndexNew {
const INVALID: DepNodeIndexNew = DepNodeIndexNew::const_new(::std::u32::MAX);
diff --git a/src/librustc/dep_graph/serialized.rs b/src/librustc/dep_graph/serialized.rs
index c6cfdf6bd0..014154243e 100644
--- a/src/librustc/dep_graph/serialized.rs
+++ b/src/librustc/dep_graph/serialized.rs
@@ -13,8 +13,9 @@
use dep_graph::DepNode;
use ich::Fingerprint;
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
+use std::u32;
-newtype_index!(SerializedDepNodeIndex);
+newtype_index!(SerializedDepNodeIndex, u32::MAX);
/// Data for use when recompiling the **current crate**.
#[derive(Debug, RustcEncodable, RustcDecodable)]
diff --git a/src/librustc/ich/impls_ty.rs b/src/librustc/ich/impls_ty.rs
index 2bbf807807..88aa6bdc98 100644
--- a/src/librustc/ich/impls_ty.rs
+++ b/src/librustc/ich/impls_ty.rs
@@ -514,7 +514,7 @@ impl_stable_hash_for!(enum ty::cast::CastKind {
FnPtrAddrCast
});
-impl_stable_hash_for!(struct ::middle::region::FirstStatementIndex { idx });
+impl_stable_hash_for!(struct ::middle::region::FirstStatementIndex { index });
impl_stable_hash_for!(struct ::middle::region::Scope { id, code });
impl<'gcx> ToStableHashKey<StableHashingContext<'gcx>> for region::Scope {
diff --git a/src/librustc/middle/region.rs b/src/librustc/middle/region.rs
index cede0c2b9a..e64d2c3001 100644
--- a/src/librustc/middle/region.rs
+++ b/src/librustc/middle/region.rs
@@ -156,26 +156,7 @@ pub struct BlockRemainder {
pub first_statement_index: FirstStatementIndex,
}
-#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, RustcEncodable,
- RustcDecodable, Copy)]
-pub struct FirstStatementIndex { pub idx: u32 }
-
-impl Idx for FirstStatementIndex {
- fn new(idx: usize) -> Self {
- assert!(idx <= SCOPE_DATA_REMAINDER_MAX as usize);
- FirstStatementIndex { idx: idx as u32 }
- }
-
- fn index(self) -> usize {
- self.idx as usize
- }
-}
-
-impl fmt::Debug for FirstStatementIndex {
- fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
- fmt::Debug::fmt(&self.index(), formatter)
- }
-}
+newtype_index!(FirstStatementIndex, SCOPE_DATA_REMAINDER_MAX);
impl From<ScopeData> for Scope {
#[inline]
@@ -208,7 +189,7 @@ impl Scope {
SCOPE_DATA_DESTRUCTION => ScopeData::Destruction(self.id),
idx => ScopeData::Remainder(BlockRemainder {
block: self.id,
- first_statement_index: FirstStatementIndex { idx }
+ first_statement_index: FirstStatementIndex::new(idx as usize)
})
}
}
diff --git a/src/librustc/mir/mod.rs b/src/librustc/mir/mod.rs
index be863a0fb7..d84da9ad9c 100644
--- a/src/librustc/mir/mod.rs
+++ b/src/librustc/mir/mod.rs
@@ -127,7 +127,7 @@ pub struct Mir<'tcx> {
}
/// where execution begins
-pub const START_BLOCK: BasicBlock = BasicBlock(0);
+pub const START_BLOCK: BasicBlock = BasicBlock::const_new(0);
impl<'tcx> Mir<'tcx> {
pub fn new(basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
@@ -193,7 +193,7 @@ impl<'tcx> Mir<'tcx> {
#[inline]
pub fn local_kind(&self, local: Local) -> LocalKind {
- let index = local.0 as usize;
+ let index = local.index as usize;
if index == 0 {
debug_assert!(self.local_decls[local].mutability == Mutability::Mut,
"return pointer should be mutable");
@@ -402,9 +402,9 @@ pub enum BorrowKind {
///////////////////////////////////////////////////////////////////////////
// Variables and temps
-newtype_index!(Local);
+newtype_index!(Local, u32::MAX);
-pub const RETURN_POINTER: Local = Local(0);
+pub const RETURN_POINTER: Local = Local::const_new(0);
/// Classifies locals into categories. See `Mir::local_kind`.
#[derive(PartialEq, Eq, Debug)]
@@ -538,7 +538,7 @@ pub struct UpvarDecl {
///////////////////////////////////////////////////////////////////////////
// BasicBlock
-newtype_index!(BasicBlock);
+newtype_index!(BasicBlock, u32::MAX);
///////////////////////////////////////////////////////////////////////////
// BasicBlockData and Terminator
@@ -1118,7 +1118,7 @@ pub type LvalueProjection<'tcx> = Projection<'tcx, Lvalue<'tcx>, Local, Ty<'tcx>
/// and the index is a local.
pub type LvalueElem<'tcx> = ProjectionElem<'tcx, Local, Ty<'tcx>>;
-newtype_index!(Field);
+newtype_index!(Field, u32::MAX);
impl<'tcx> Lvalue<'tcx> {
pub fn field(self, f: Field, ty: Ty<'tcx>) -> Lvalue<'tcx> {
@@ -1183,8 +1183,8 @@ impl<'tcx> Debug for Lvalue<'tcx> {
///////////////////////////////////////////////////////////////////////////
// Scopes
-newtype_index!(VisibilityScope);
-pub const ARGUMENT_VISIBILITY_SCOPE : VisibilityScope = VisibilityScope(0);
+newtype_index!(VisibilityScope, u32::MAX);
+pub const ARGUMENT_VISIBILITY_SCOPE : VisibilityScope = VisibilityScope::const_new(0);
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
pub struct VisibilityScopeData {
@@ -1509,7 +1509,7 @@ pub struct Constant<'tcx> {
pub literal: Literal<'tcx>,
}
-newtype_index!(Promoted);
+newtype_index!(Promoted, u32::MAX);
#[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
pub enum Literal<'tcx> {
diff --git a/src/librustc_data_structures/indexed_vec.rs b/src/librustc_data_structures/indexed_vec.rs
index dfdd23a369..c671d0716f 100644
--- a/src/librustc_data_structures/indexed_vec.rs
+++ b/src/librustc_data_structures/indexed_vec.rs
@@ -40,33 +40,36 @@ impl Idx for u32 {
#[macro_export]
macro_rules! newtype_index {
- ($name:ident) => (
+ ($name:ident, $max:expr) => (
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord,
RustcEncodable, RustcDecodable)]
- pub struct $name(u32);
+ pub struct $name {
+ pub index: u32,
+ }
impl $name {
// HACK use for constants
#[allow(unused)]
const fn const_new(x: u32) -> Self {
- $name(x)
+ $name { index: x }
}
}
impl Idx for $name {
fn new(value: usize) -> Self {
- assert!(value < (::std::u32::MAX) as usize);
- $name(value as u32)
+ assert!(value < $max as usize);
+ $name { index: value as u32 }
}
+
fn index(self) -> usize {
- self.0 as usize
+ self.index as usize
}
}
impl ::std::fmt::Debug for $name {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
let debug_name = unsafe { ::std::intrinsics::type_name::<$name>() };
- write!(fmt, "{}{}", debug_name, self.0)
+ write!(fmt, "{}{}", debug_name, self.index)
}
}
)
diff --git a/src/librustc_incremental/persist/data.rs b/src/librustc_incremental/persist/data.rs
index aaed716328..3210f08e0a 100644
--- a/src/librustc_incremental/persist/data.rs
+++ b/src/librustc_incremental/persist/data.rs
@@ -17,6 +17,7 @@ use rustc::ich::Fingerprint;
use rustc::middle::cstore::EncodedMetadataHash;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
+use std::u32;
/// Data for use when recompiling the **current crate**.
#[derive(Debug, RustcEncodable, RustcDecodable)]
@@ -65,7 +66,7 @@ impl SerializedDepGraph {
}
}
-newtype_index!(DepNodeIndex);
+newtype_index!(DepNodeIndex, u32::MAX);
#[derive(Debug, RustcEncodable, RustcDecodable)]
pub struct SerializedWorkProduct {
diff --git a/src/librustc_mir/build/mod.rs b/src/librustc_mir/build/mod.rs
index 46a5e5abbd..2e9ed0fd5e 100644
--- a/src/librustc_mir/build/mod.rs
+++ b/src/librustc_mir/build/mod.rs
@@ -311,7 +311,7 @@ struct CFG<'tcx> {
basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
}
-newtype_index!(ScopeId);
+newtype_index!(ScopeId, u32::MAX);
///////////////////////////////////////////////////////////////////////////
/// The `BlockAnd` "monad" packages up the new basic block along with a
diff --git a/src/librustc_mir/transform/nll.rs b/src/librustc_mir/transform/nll.rs
index d4a5354c78..3572be01f4 100644
--- a/src/librustc_mir/transform/nll.rs
+++ b/src/librustc_mir/transform/nll.rs
@@ -19,6 +19,7 @@ use rustc::util::nodemap::FxHashSet;
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
use syntax_pos::DUMMY_SP;
use std::collections::HashMap;
+use std::u32;
#[allow(dead_code)]
struct NLLVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
@@ -155,4 +156,4 @@ struct Region {
points: FxHashSet<Location>,
}
-newtype_index!(RegionIndex);
+newtype_index!(RegionIndex, u32::MAX);
--
2.14.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment