Skip to content

Commit 51c3879

Browse files
committed
Rollup merge of #53315 - nikomatsakis:newtype-index, r=Mark-Simulacrum
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 =)
2 parents b1ef2b8 + ab43c1e commit 51c3879

File tree

44 files changed

+299
-200
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+299
-200
lines changed

src/librustc/dep_graph/graph.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ pub struct DepGraph {
3939
fingerprints: Lrc<Lock<IndexVec<DepNodeIndex, Fingerprint>>>
4040
}
4141

42-
newtype_index!(DepNodeIndex);
42+
newtype_index! {
43+
pub struct DepNodeIndex { .. }
44+
}
4345

4446
impl DepNodeIndex {
45-
const INVALID: DepNodeIndex = DepNodeIndex(::std::u32::MAX);
47+
const INVALID: DepNodeIndex = DepNodeIndex::MAX;
4648
}
4749

4850
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
@@ -1125,14 +1127,16 @@ impl DepNodeColorMap {
11251127
match self.values[index] {
11261128
COMPRESSED_NONE => None,
11271129
COMPRESSED_RED => Some(DepNodeColor::Red),
1128-
value => Some(DepNodeColor::Green(DepNodeIndex(value - COMPRESSED_FIRST_GREEN)))
1130+
value => Some(DepNodeColor::Green(DepNodeIndex::from_u32(
1131+
value - COMPRESSED_FIRST_GREEN
1132+
)))
11291133
}
11301134
}
11311135

11321136
fn insert(&mut self, index: SerializedDepNodeIndex, color: DepNodeColor) {
11331137
self.values[index] = match color {
11341138
DepNodeColor::Red => COMPRESSED_RED,
1135-
DepNodeColor::Green(index) => index.0 + COMPRESSED_FIRST_GREEN,
1139+
DepNodeColor::Green(index) => index.as_u32() + COMPRESSED_FIRST_GREEN,
11361140
}
11371141
}
11381142
}

src/librustc/dep_graph/serialized.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ use dep_graph::DepNode;
1414
use ich::Fingerprint;
1515
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
1616

17-
newtype_index!(SerializedDepNodeIndex);
17+
newtype_index! {
18+
pub struct SerializedDepNodeIndex { .. }
19+
}
1820

1921
/// Data for use when recompiling the **current crate**.
2022
#[derive(Debug, RustcEncodable, RustcDecodable)]

src/librustc/hir/def_id.rs

+9-21
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use serialize;
1515
use std::fmt;
1616
use std::u32;
1717

18-
newtype_index!(CrateNum
19-
{
18+
newtype_index! {
19+
pub struct CrateNum {
2020
ENCODABLE = custom
2121
DEBUG_FORMAT = "crate{}",
2222

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

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

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

4041
impl CrateNum {
4142
pub fn new(x: usize) -> CrateNum {
42-
assert!(x < (u32::MAX as usize));
43-
CrateNum(x as u32)
44-
}
45-
46-
pub fn from_u32(x: u32) -> CrateNum {
47-
CrateNum(x)
48-
}
49-
50-
pub fn as_usize(&self) -> usize {
51-
self.0 as usize
52-
}
53-
54-
pub fn as_u32(&self) -> u32 {
55-
self.0
43+
CrateNum::from_usize(x)
5644
}
5745

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

6149
impl fmt::Display for CrateNum {
6250
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63-
fmt::Display::fmt(&self.0, f)
51+
fmt::Display::fmt(&self.as_u32(), f)
6452
}
6553
}
6654

src/librustc/ich/impls_mir.rs

-5
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ impl<'a> HashStable<StableHashingContext<'a>> for mir::Local {
102102
fn hash_stable<W: StableHasherResult>(&self,
103103
hcx: &mut StableHashingContext<'a>,
104104
hasher: &mut StableHasher<W>) {
105-
use rustc_data_structures::indexed_vec::Idx;
106105
self.index().hash_stable(hcx, hasher);
107106
}
108107
}
@@ -112,7 +111,6 @@ impl<'a> HashStable<StableHashingContext<'a>> for mir::BasicBlock {
112111
fn hash_stable<W: StableHasherResult>(&self,
113112
hcx: &mut StableHashingContext<'a>,
114113
hasher: &mut StableHasher<W>) {
115-
use rustc_data_structures::indexed_vec::Idx;
116114
self.index().hash_stable(hcx, hasher);
117115
}
118116
}
@@ -122,7 +120,6 @@ impl<'a> HashStable<StableHashingContext<'a>> for mir::Field {
122120
fn hash_stable<W: StableHasherResult>(&self,
123121
hcx: &mut StableHashingContext<'a>,
124122
hasher: &mut StableHasher<W>) {
125-
use rustc_data_structures::indexed_vec::Idx;
126123
self.index().hash_stable(hcx, hasher);
127124
}
128125
}
@@ -133,7 +130,6 @@ for mir::SourceScope {
133130
fn hash_stable<W: StableHasherResult>(&self,
134131
hcx: &mut StableHashingContext<'a>,
135132
hasher: &mut StableHasher<W>) {
136-
use rustc_data_structures::indexed_vec::Idx;
137133
self.index().hash_stable(hcx, hasher);
138134
}
139135
}
@@ -143,7 +139,6 @@ impl<'a> HashStable<StableHashingContext<'a>> for mir::Promoted {
143139
fn hash_stable<W: StableHasherResult>(&self,
144140
hcx: &mut StableHashingContext<'a>,
145141
hasher: &mut StableHasher<W>) {
146-
use rustc_data_structures::indexed_vec::Idx;
147142
self.index().hash_stable(hcx, hasher);
148143
}
149144
}

src/librustc/ich/impls_ty.rs

-3
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ impl<'a> HashStable<StableHashingContext<'a>> for ty::RegionVid {
143143
fn hash_stable<W: StableHasherResult>(&self,
144144
hcx: &mut StableHashingContext<'a>,
145145
hasher: &mut StableHasher<W>) {
146-
use rustc_data_structures::indexed_vec::Idx;
147146
self.index().hash_stable(hcx, hasher);
148147
}
149148
}
@@ -153,7 +152,6 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for ty::CanonicalVar {
153152
fn hash_stable<W: StableHasherResult>(&self,
154153
hcx: &mut StableHashingContext<'gcx>,
155154
hasher: &mut StableHasher<W>) {
156-
use rustc_data_structures::indexed_vec::Idx;
157155
self.index().hash_stable(hcx, hasher);
158156
}
159157
}
@@ -774,7 +772,6 @@ impl_stable_hash_for!(enum ty::cast::CastKind {
774772
FnPtrAddrCast
775773
});
776774

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

780777
impl<'a> ToStableHashKey<StableHashingContext<'a>> for region::Scope {

src/librustc/infer/error_reporting/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ use syntax::ast::DUMMY_NODE_ID;
7373
use syntax_pos::{Pos, Span};
7474
use errors::{Applicability, DiagnosticBuilder, DiagnosticStyledString};
7575

76-
use rustc_data_structures::indexed_vec::Idx;
77-
7876
mod note;
7977

8078
mod need_type_info;

src/librustc/infer/region_constraints/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use self::CombineMapType::*;
1616
use super::{MiscVariable, RegionVariableOrigin, SubregionOrigin};
1717
use super::unify_key;
1818

19-
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
19+
use rustc_data_structures::indexed_vec::IndexVec;
2020
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
2121
use rustc_data_structures::unify as ut;
2222
use ty::{self, Ty, TyCtxt};

src/librustc/infer/unify_key.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ impl UnifyValue for RegionVidKey {
4949

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

src/librustc/middle/region.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,13 @@ pub struct BlockRemainder {
159159
pub first_statement_index: FirstStatementIndex,
160160
}
161161

162-
newtype_index!(FirstStatementIndex
163-
{
164-
pub idx
162+
newtype_index! {
163+
pub struct FirstStatementIndex {
165164
MAX = SCOPE_DATA_REMAINDER_MAX
166-
});
165+
}
166+
}
167+
168+
impl_stable_hash_for!(struct ::middle::region::FirstStatementIndex { private });
167169

168170
impl From<ScopeData> for Scope {
169171
#[inline]

src/librustc/mir/mod.rs

+25-13
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,6 @@ pub struct Mir<'tcx> {
131131
cache: cache::Cache,
132132
}
133133

134-
/// where execution begins
135-
pub const START_BLOCK: BasicBlock = BasicBlock(0);
136-
137134
impl<'tcx> Mir<'tcx> {
138135
pub fn new(
139136
basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
@@ -239,7 +236,7 @@ impl<'tcx> Mir<'tcx> {
239236

240237
#[inline]
241238
pub fn local_kind(&self, local: Local) -> LocalKind {
242-
let index = local.0 as usize;
239+
let index = local.as_usize();
243240
if index == 0 {
244241
debug_assert!(
245242
self.local_decls[local].mutability == Mutability::Mut,
@@ -523,11 +520,12 @@ impl BorrowKind {
523520
///////////////////////////////////////////////////////////////////////////
524521
// Variables and temps
525522

526-
newtype_index!(Local
527-
{
523+
newtype_index! {
524+
pub struct Local {
528525
DEBUG_FORMAT = "_{}",
529526
const RETURN_PLACE = 0,
530-
});
527+
}
528+
}
531529

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

855-
newtype_index!(BasicBlock { DEBUG_FORMAT = "bb{}" });
853+
newtype_index! {
854+
pub struct BasicBlock {
855+
DEBUG_FORMAT = "bb{}",
856+
const START_BLOCK = 0,
857+
}
858+
}
856859

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

1825-
newtype_index!(Field { DEBUG_FORMAT = "field[{}]" });
1828+
newtype_index! {
1829+
pub struct Field {
1830+
DEBUG_FORMAT = "field[{}]"
1831+
}
1832+
}
18261833

18271834
impl<'tcx> Place<'tcx> {
18281835
pub fn field(self, f: Field, ty: Ty<'tcx>) -> Place<'tcx> {
@@ -1895,11 +1902,12 @@ impl<'tcx> Debug for Place<'tcx> {
18951902
///////////////////////////////////////////////////////////////////////////
18961903
// Scopes
18971904

1898-
newtype_index!(SourceScope
1899-
{
1905+
newtype_index! {
1906+
pub struct SourceScope {
19001907
DEBUG_FORMAT = "scope[{}]",
19011908
const OUTERMOST_SOURCE_SCOPE = 0,
1902-
});
1909+
}
1910+
}
19031911

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

2274-
newtype_index!(Promoted { DEBUG_FORMAT = "promoted[{}]" });
2282+
newtype_index! {
2283+
pub struct Promoted {
2284+
DEBUG_FORMAT = "promoted[{}]"
2285+
}
2286+
}
22752287

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

0 commit comments

Comments
 (0)