Auto merge of #85829 - bjorn3:simplify_crate_num, r=jackh726

Remove CrateNum::ReservedForIncrCompCache

It's only use is easily replaceable with `Option<CrateNum>`.
This commit is contained in:
bors 2021-06-01 20:09:03 +00:00
commit 625d5a693e
7 changed files with 12 additions and 71 deletions

View file

@ -9,65 +9,21 @@ use std::borrow::Borrow;
use std::fmt;
rustc_index::newtype_index! {
pub struct CrateId {
pub struct CrateNum {
ENCODABLE = custom
DEBUG_FORMAT = "crate{}"
}
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum CrateNum {
/// A special `CrateNum` that we use for the `tcx.rcache` when decoding from
/// the incr. comp. cache.
ReservedForIncrCompCache,
Index(CrateId),
}
/// Item definitions in the currently-compiled crate would have the `CrateNum`
/// `LOCAL_CRATE` in their `DefId`.
pub const LOCAL_CRATE: CrateNum = CrateNum::Index(CrateId::from_u32(0));
impl Idx for CrateNum {
#[inline]
fn new(value: usize) -> Self {
CrateNum::Index(Idx::new(value))
}
#[inline]
fn index(self) -> usize {
match self {
CrateNum::Index(idx) => Idx::index(idx),
_ => panic!("Tried to get crate index of {:?}", self),
}
}
}
pub const LOCAL_CRATE: CrateNum = CrateNum::from_u32(0);
impl CrateNum {
pub fn new(x: usize) -> CrateNum {
CrateNum::from_usize(x)
}
pub fn from_usize(x: usize) -> CrateNum {
CrateNum::Index(CrateId::from_usize(x))
}
pub fn from_u32(x: u32) -> CrateNum {
CrateNum::Index(CrateId::from_u32(x))
}
pub fn as_usize(self) -> usize {
match self {
CrateNum::Index(id) => id.as_usize(),
_ => panic!("tried to get index of non-standard crate {:?}", self),
}
}
pub fn as_u32(self) -> u32 {
match self {
CrateNum::Index(id) => id.as_u32(),
_ => panic!("tried to get index of non-standard crate {:?}", self),
}
}
pub fn as_def_id(&self) -> DefId {
DefId { krate: *self, index: CRATE_DEF_INDEX }
}
@ -75,10 +31,7 @@ impl CrateNum {
impl fmt::Display for CrateNum {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CrateNum::Index(id) => fmt::Display::fmt(&id.private, f),
CrateNum::ReservedForIncrCompCache => write!(f, "crate for decoding incr comp cache"),
}
fmt::Display::fmt(&self.private, f)
}
}
@ -96,15 +49,6 @@ impl<D: Decoder> Decodable<D> for CrateNum {
}
}
impl ::std::fmt::Debug for CrateNum {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
match self {
CrateNum::Index(id) => write!(fmt, "crate{}", id.private),
CrateNum::ReservedForIncrCompCache => write!(fmt, "crate for decoding incr comp cache"),
}
}
}
/// A `DefPathHash` is a fixed-size representation of a `DefPath` that is
/// stable across crate and compilation session boundaries. It consists of two
/// separate 64-bit hashes. The first uniquely identifies the crate this