Skip to content

Commit 24ab375

Browse files
committed
remove all references to private from outside the macro
1 parent 6ccf9b8 commit 24ab375

File tree

5 files changed

+63
-37
lines changed

5 files changed

+63
-37
lines changed

src/librustc/dep_graph/graph.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ newtype_index! {
4444
}
4545

4646
impl DepNodeIndex {
47-
const INVALID: DepNodeIndex = DepNodeIndex { private: ::std::u32::MAX };
47+
const INVALID: DepNodeIndex = unsafe {
48+
DepNodeIndex::from_u32_unchecked(::std::u32::MAX)
49+
};
4850
}
4951

5052
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
@@ -1127,14 +1129,16 @@ impl DepNodeColorMap {
11271129
match self.values[index] {
11281130
COMPRESSED_NONE => None,
11291131
COMPRESSED_RED => Some(DepNodeColor::Red),
1130-
value => Some(DepNodeColor::Green(DepNodeIndex { private: value - COMPRESSED_FIRST_GREEN })),
1132+
value => Some(DepNodeColor::Green(DepNodeIndex::from_u32(
1133+
value - COMPRESSED_FIRST_GREEN
1134+
)))
11311135
}
11321136
}
11331137

11341138
fn insert(&mut self, index: SerializedDepNodeIndex, color: DepNodeColor) {
11351139
self.values[index] = match color {
11361140
DepNodeColor::Red => COMPRESSED_RED,
1137-
DepNodeColor::Green(index) => index.private + COMPRESSED_FIRST_GREEN,
1141+
DepNodeColor::Green(index) => index.as_u32() + COMPRESSED_FIRST_GREEN,
11381142
}
11391143
}
11401144
}

src/librustc/hir/def_id.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,15 @@ newtype_index! {
4141
impl CrateNum {
4242
pub fn new(x: usize) -> CrateNum {
4343
assert!(x < (u32::MAX as usize));
44-
CrateNum { private: x as u32 }
45-
}
46-
47-
pub fn from_u32(x: u32) -> CrateNum {
48-
CrateNum { private: x }
49-
}
50-
51-
pub fn as_usize(&self) -> usize {
52-
self.private as usize
53-
}
54-
55-
pub fn as_u32(&self) -> u32 {
56-
u32::from(*self)
44+
CrateNum::from_u32(x as u32)
5745
}
5846

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

6250
impl fmt::Display for CrateNum {
6351
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
64-
fmt::Display::fmt(&self.private, f)
52+
fmt::Display::fmt(&self.as_u32(), f)
6553
}
6654
}
6755

src/librustc/mir/mod.rs

Lines changed: 3 additions & 5 deletions
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 { private: 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.private as usize;
239+
let index = local.as_usize();
243240
if index == 0 {
244241
debug_assert!(
245242
self.local_decls[local].mutability == Mutability::Mut,
@@ -855,7 +852,8 @@ pub struct UpvarDecl {
855852

856853
newtype_index! {
857854
pub struct BasicBlock {
858-
DEBUG_FORMAT = "bb{}"
855+
DEBUG_FORMAT = "bb{}",
856+
const START_BLOCK = 0,
859857
}
860858
}
861859

src/librustc/ty/sty.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,7 +1274,9 @@ impl DebruijnIndex {
12741274
/// you would need to shift the index for `'a` into 1 new binder.
12751275
#[must_use]
12761276
pub const fn shifted_in(self, amount: u32) -> DebruijnIndex {
1277-
DebruijnIndex { private: self.private + amount }
1277+
unsafe {
1278+
DebruijnIndex::from_u32_unchecked(self.as_u32() + amount)
1279+
}
12781280
}
12791281

12801282
/// Update this index in place by shifting it "in" through
@@ -1287,7 +1289,9 @@ impl DebruijnIndex {
12871289
/// `amount` number of new binders.
12881290
#[must_use]
12891291
pub const fn shifted_out(self, amount: u32) -> DebruijnIndex {
1290-
DebruijnIndex { private: self.private - amount }
1292+
unsafe {
1293+
DebruijnIndex::from_u32_unchecked(self.as_u32() - amount)
1294+
}
12911295
}
12921296

12931297
/// Update in place by shifting out from `amount` binders.
@@ -1316,7 +1320,7 @@ impl DebruijnIndex {
13161320
/// bound by one of the binders we are shifting out of, that is an
13171321
/// error (and should fail an assertion failure).
13181322
pub fn shifted_out_to_binder(self, to_binder: DebruijnIndex) -> Self {
1319-
self.shifted_out((to_binder.private - INNERMOST.private) as u32)
1323+
self.shifted_out(to_binder.as_u32() - INNERMOST.as_u32())
13201324
}
13211325
}
13221326

src/librustc_data_structures/indexed_vec.rs

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,55 @@ macro_rules! newtype_index {
102102
}
103103

104104
impl $type {
105+
#[inline]
106+
$v fn from_usize(value: usize) -> Self {
107+
assert!(value < ($max as usize));
108+
unsafe {
109+
$type::from_u32_unchecked(value as u32)
110+
}
111+
}
112+
113+
#[inline]
114+
$v fn from_u32(value: u32) -> Self {
115+
assert!(value < $max);
116+
unsafe {
117+
$type::from_u32_unchecked(value)
118+
}
119+
}
120+
121+
#[inline]
122+
$v const unsafe fn from_u32_unchecked(value: u32) -> Self {
123+
$type { private: value }
124+
}
125+
105126
/// Extract value of this index as an integer.
106127
#[inline]
107128
$v fn index(self) -> usize {
108-
<Self as Idx>::index(self)
129+
self.as_usize()
130+
}
131+
132+
/// Extract value of this index as a usize.
133+
#[inline]
134+
$v const fn as_u32(self) -> u32 {
135+
self.private
136+
}
137+
138+
/// Extract value of this index as a u32.
139+
#[inline]
140+
$v const fn as_usize(self) -> usize {
141+
self.private as usize
109142
}
110143
}
111144

112145
impl Idx for $type {
113146
#[inline]
114147
fn new(value: usize) -> Self {
115-
assert!(value < ($max) as usize);
116-
$type { private: value as u32 }
148+
Self::from(value)
117149
}
118150

119151
#[inline]
120152
fn index(self) -> usize {
121-
self.private as usize
153+
usize::from(self)
122154
}
123155
}
124156

@@ -153,25 +185,25 @@ macro_rules! newtype_index {
153185

154186
impl From<$type> for u32 {
155187
fn from(v: $type) -> u32 {
156-
v.private
188+
v.as_u32()
157189
}
158190
}
159191

160192
impl From<$type> for usize {
161193
fn from(v: $type) -> usize {
162-
v.private as usize
194+
v.as_usize()
163195
}
164196
}
165197

166198
impl From<usize> for $type {
167-
fn from(v: usize) -> Self {
168-
Self::new(v)
199+
fn from(value: usize) -> Self {
200+
$type::from_usize(value)
169201
}
170202
}
171203

172204
impl From<u32> for $type {
173-
fn from(v: u32) -> Self {
174-
Self::new(v as usize)
205+
fn from(value: u32) -> Self {
206+
$type::from_u32(value)
175207
}
176208
}
177209

@@ -195,7 +227,7 @@ macro_rules! newtype_index {
195227
@debug_format [$debug_format:tt]) => (
196228
impl ::std::fmt::Debug for $type {
197229
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
198-
write!(fmt, $debug_format, self.private)
230+
write!(fmt, $debug_format, self.as_u32())
199231
}
200232
}
201233
);
@@ -378,7 +410,7 @@ macro_rules! newtype_index {
378410
const $name:ident = $constant:expr,
379411
$($tokens:tt)*) => (
380412
$(#[doc = $doc])*
381-
pub const $name: $type = $type { private: $constant };
413+
pub const $name: $type = unsafe { $type::from_u32_unchecked($constant) };
382414
newtype_index!(
383415
@derives [$($derives,)*]
384416
@type [$type]

0 commit comments

Comments
 (0)