remove all references to private from outside the macro

This commit is contained in:
Niko Matsakis 2018-08-28 12:20:56 -04:00
parent 6ccf9b8134
commit 24ab3758a6
5 changed files with 63 additions and 37 deletions

View File

@ -44,7 +44,9 @@ newtype_index! {
}
impl DepNodeIndex {
const INVALID: DepNodeIndex = DepNodeIndex { private: ::std::u32::MAX };
const INVALID: DepNodeIndex = unsafe {
DepNodeIndex::from_u32_unchecked(::std::u32::MAX)
};
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
@ -1127,14 +1129,16 @@ impl DepNodeColorMap {
match self.values[index] {
COMPRESSED_NONE => None,
COMPRESSED_RED => Some(DepNodeColor::Red),
value => Some(DepNodeColor::Green(DepNodeIndex { private: 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.private + COMPRESSED_FIRST_GREEN,
DepNodeColor::Green(index) => index.as_u32() + COMPRESSED_FIRST_GREEN,
}
}
}

View File

@ -41,19 +41,7 @@ newtype_index! {
impl CrateNum {
pub fn new(x: usize) -> CrateNum {
assert!(x < (u32::MAX as usize));
CrateNum { private: x as u32 }
}
pub fn from_u32(x: u32) -> CrateNum {
CrateNum { private: x }
}
pub fn as_usize(&self) -> usize {
self.private as usize
}
pub fn as_u32(&self) -> u32 {
u32::from(*self)
CrateNum::from_u32(x as u32)
}
pub fn as_def_id(&self) -> DefId { DefId { krate: *self, index: CRATE_DEF_INDEX } }
@ -61,7 +49,7 @@ impl CrateNum {
impl fmt::Display for CrateNum {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.private, f)
fmt::Display::fmt(&self.as_u32(), f)
}
}

View File

@ -131,9 +131,6 @@ pub struct Mir<'tcx> {
cache: cache::Cache,
}
/// where execution begins
pub const START_BLOCK: BasicBlock = BasicBlock { private: 0 };
impl<'tcx> Mir<'tcx> {
pub fn new(
basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
@ -239,7 +236,7 @@ impl<'tcx> Mir<'tcx> {
#[inline]
pub fn local_kind(&self, local: Local) -> LocalKind {
let index = local.private as usize;
let index = local.as_usize();
if index == 0 {
debug_assert!(
self.local_decls[local].mutability == Mutability::Mut,
@ -855,7 +852,8 @@ pub struct UpvarDecl {
newtype_index! {
pub struct BasicBlock {
DEBUG_FORMAT = "bb{}"
DEBUG_FORMAT = "bb{}",
const START_BLOCK = 0,
}
}

View File

@ -1274,7 +1274,9 @@ impl DebruijnIndex {
/// you would need to shift the index for `'a` into 1 new binder.
#[must_use]
pub const fn shifted_in(self, amount: u32) -> DebruijnIndex {
DebruijnIndex { private: self.private + amount }
unsafe {
DebruijnIndex::from_u32_unchecked(self.as_u32() + amount)
}
}
/// Update this index in place by shifting it "in" through
@ -1287,7 +1289,9 @@ impl DebruijnIndex {
/// `amount` number of new binders.
#[must_use]
pub const fn shifted_out(self, amount: u32) -> DebruijnIndex {
DebruijnIndex { private: self.private - amount }
unsafe {
DebruijnIndex::from_u32_unchecked(self.as_u32() - amount)
}
}
/// Update in place by shifting out from `amount` binders.
@ -1316,7 +1320,7 @@ impl DebruijnIndex {
/// bound by one of the binders we are shifting out of, that is an
/// error (and should fail an assertion failure).
pub fn shifted_out_to_binder(self, to_binder: DebruijnIndex) -> Self {
self.shifted_out((to_binder.private - INNERMOST.private) as u32)
self.shifted_out(to_binder.as_u32() - INNERMOST.as_u32())
}
}

View File

@ -102,23 +102,55 @@ macro_rules! newtype_index {
}
impl $type {
#[inline]
$v fn from_usize(value: usize) -> Self {
assert!(value < ($max as usize));
unsafe {
$type::from_u32_unchecked(value as u32)
}
}
#[inline]
$v fn from_u32(value: u32) -> Self {
assert!(value < $max);
unsafe {
$type::from_u32_unchecked(value)
}
}
#[inline]
$v const unsafe fn from_u32_unchecked(value: u32) -> Self {
$type { private: value }
}
/// Extract value of this index as an integer.
#[inline]
$v fn index(self) -> usize {
<Self as Idx>::index(self)
self.as_usize()
}
/// Extract value of this index as a usize.
#[inline]
$v const fn as_u32(self) -> u32 {
self.private
}
/// Extract value of this index as a u32.
#[inline]
$v const fn as_usize(self) -> usize {
self.private as usize
}
}
impl Idx for $type {
#[inline]
fn new(value: usize) -> Self {
assert!(value < ($max) as usize);
$type { private: value as u32 }
Self::from(value)
}
#[inline]
fn index(self) -> usize {
self.private as usize
usize::from(self)
}
}
@ -153,25 +185,25 @@ macro_rules! newtype_index {
impl From<$type> for u32 {
fn from(v: $type) -> u32 {
v.private
v.as_u32()
}
}
impl From<$type> for usize {
fn from(v: $type) -> usize {
v.private as usize
v.as_usize()
}
}
impl From<usize> for $type {
fn from(v: usize) -> Self {
Self::new(v)
fn from(value: usize) -> Self {
$type::from_usize(value)
}
}
impl From<u32> for $type {
fn from(v: u32) -> Self {
Self::new(v as usize)
fn from(value: u32) -> Self {
$type::from_u32(value)
}
}
@ -195,7 +227,7 @@ macro_rules! newtype_index {
@debug_format [$debug_format:tt]) => (
impl ::std::fmt::Debug for $type {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(fmt, $debug_format, self.private)
write!(fmt, $debug_format, self.as_u32())
}
}
);
@ -378,7 +410,7 @@ macro_rules! newtype_index {
const $name:ident = $constant:expr,
$($tokens:tt)*) => (
$(#[doc = $doc])*
pub const $name: $type = $type { private: $constant };
pub const $name: $type = unsafe { $type::from_u32_unchecked($constant) };
newtype_index!(
@derives [$($derives,)*]
@type [$type]