Split CrateNum
into an enum instead of having magic constants
This commit is contained in:
parent
d1b5231aa7
commit
1ec86708ca
5 changed files with 58 additions and 26 deletions
|
@ -18,23 +18,51 @@ use std::u32;
|
|||
newtype_index! {
|
||||
pub struct CrateNum {
|
||||
ENCODABLE = custom
|
||||
DEBUG_FORMAT = "crate{}",
|
||||
}
|
||||
}
|
||||
|
||||
/// Item definitions in the currently-compiled crate would have the CrateNum
|
||||
/// LOCAL_CRATE in their DefId.
|
||||
const LOCAL_CRATE = 0,
|
||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub enum CrateNum {
|
||||
/// Virtual crate for builtin macros
|
||||
// FIXME(jseyfried): this is also used for custom derives until proc-macro crates get
|
||||
// `CrateNum`s.
|
||||
BuiltinMacros,
|
||||
/// A CrateNum value that indicates that something is wrong.
|
||||
Invalid,
|
||||
/// A special CrateNum that we use for the tcx.rcache when decoding from
|
||||
/// the incr. comp. cache.
|
||||
ReservedForIncrCompCache,
|
||||
Index(CrateId),
|
||||
}
|
||||
|
||||
/// Virtual crate for builtin macros
|
||||
// FIXME(jseyfried): this is also used for custom derives until proc-macro crates get
|
||||
// `CrateNum`s.
|
||||
const BUILTIN_MACROS_CRATE = CrateNum::MAX_AS_U32,
|
||||
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.0),
|
||||
CrateNum::Invalid => write!(fmt, "invalid crate"),
|
||||
CrateNum::BuiltinMacros => write!(fmt, "bultin macros crate"),
|
||||
CrateNum::ReservedForIncrCompCache => write!(fmt, "crate for decoding incr comp cache"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A CrateNum value that indicates that something is wrong.
|
||||
const INVALID_CRATE = CrateNum::MAX_AS_U32 - 1,
|
||||
/// Item definitions in the currently-compiled crate would have the CrateNum
|
||||
/// LOCAL_CRATE in their DefId.
|
||||
pub const LOCAL_CRATE: CrateNum = CrateNum::Index(CrateId(0));
|
||||
|
||||
/// A special CrateNum that we use for the tcx.rcache when decoding from
|
||||
/// the incr. comp. cache.
|
||||
const RESERVED_FOR_INCR_COMP_CACHE = CrateNum::MAX_AS_U32 - 2,
|
||||
|
||||
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),
|
||||
_ => bug!("Tried to get crate index of {:?}", self),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -48,7 +76,12 @@ impl CrateNum {
|
|||
|
||||
impl fmt::Display for CrateNum {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt::Display::fmt(&self.as_u32(), f)
|
||||
match self {
|
||||
CrateNum::Index(id) => fmt::Display::fmt(&id.0, f),
|
||||
CrateNum::Invalid => write!(f, "invalid crate"),
|
||||
CrateNum::BuiltinMacros => write!(f, "bultin macros crate"),
|
||||
CrateNum::ReservedForIncrCompCache => write!(f, "crate for decoding incr comp cache"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -829,9 +829,9 @@ impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for TypeckTables<'gcx> {
|
|||
impl<'tcx> CommonTypes<'tcx> {
|
||||
fn new(interners: &CtxtInterners<'tcx>) -> CommonTypes<'tcx> {
|
||||
// Ensure our type representation does not grow
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
#[cfg(all(not(stage0), target_pointer_width = "64"))]
|
||||
assert!(mem::size_of::<ty::TyKind>() <= 24);
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
#[cfg(all(not(stage0), target_pointer_width = "64"))]
|
||||
assert!(mem::size_of::<ty::TyS>() <= 32);
|
||||
|
||||
let mk = |sty| CtxtInterners::intern_ty(interners, interners, sty);
|
||||
|
|
|
@ -11,8 +11,7 @@
|
|||
use dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
|
||||
use errors::Diagnostic;
|
||||
use hir;
|
||||
use hir::def_id::{CrateNum, DefIndex, DefId, LocalDefId,
|
||||
RESERVED_FOR_INCR_COMP_CACHE, LOCAL_CRATE};
|
||||
use hir::def_id::{CrateNum, DefIndex, DefId, LocalDefId, LOCAL_CRATE};
|
||||
use hir::map::definitions::DefPathHash;
|
||||
use ich::{CachingSourceMapView, Fingerprint};
|
||||
use mir::{self, interpret};
|
||||
|
@ -566,7 +565,7 @@ impl<'a, 'tcx: 'a, 'x> ty_codec::TyDecoder<'a, 'tcx> for CacheDecoder<'a, 'tcx,
|
|||
let tcx = self.tcx();
|
||||
|
||||
let cache_key = ty::CReaderCacheKey {
|
||||
cnum: RESERVED_FOR_INCR_COMP_CACHE,
|
||||
cnum: CrateNum::ReservedForIncrCompCache,
|
||||
pos: shorthand,
|
||||
};
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ use Namespace::{self, TypeNS, ValueNS, MacroNS};
|
|||
use {resolve_error, resolve_struct_error, ResolutionError};
|
||||
|
||||
use rustc::hir::def::*;
|
||||
use rustc::hir::def_id::{BUILTIN_MACROS_CRATE, CRATE_DEF_INDEX, LOCAL_CRATE, DefId};
|
||||
use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, LOCAL_CRATE, DefId};
|
||||
use rustc::ty;
|
||||
use rustc::middle::cstore::CrateStore;
|
||||
use rustc_metadata::cstore::LoadedMacro;
|
||||
|
@ -768,7 +768,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
|
|||
let def_id = self.macro_defs[&expansion];
|
||||
if let Some(id) = self.definitions.as_local_node_id(def_id) {
|
||||
self.local_macro_def_scopes[&id]
|
||||
} else if def_id.krate == BUILTIN_MACROS_CRATE {
|
||||
} else if def_id.krate == CrateNum::BuiltinMacros {
|
||||
self.injected_crate.unwrap_or(self.graph_root)
|
||||
} else {
|
||||
let module_def_id = ty::DefIdTree::parent(&*self, def_id).unwrap();
|
||||
|
|
|
@ -14,8 +14,8 @@ use ModuleOrUniformRoot;
|
|||
use Namespace::{self, TypeNS, MacroNS};
|
||||
use build_reduced_graph::{BuildReducedGraphVisitor, IsMacroExport};
|
||||
use resolve_imports::ImportResolver;
|
||||
use rustc::hir::def_id::{DefId, BUILTIN_MACROS_CRATE, CRATE_DEF_INDEX, DefIndex,
|
||||
DefIndexAddressSpace};
|
||||
use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX, DefIndex,
|
||||
CrateNum, DefIndexAddressSpace};
|
||||
use rustc::hir::def::{Def, NonMacroAttrKind};
|
||||
use rustc::hir::map::{self, DefCollector};
|
||||
use rustc::{ty, lint};
|
||||
|
@ -202,7 +202,7 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
|
|||
|
||||
fn add_builtin(&mut self, ident: ast::Ident, ext: Lrc<SyntaxExtension>) {
|
||||
let def_id = DefId {
|
||||
krate: BUILTIN_MACROS_CRATE,
|
||||
krate: CrateNum::BuiltinMacros,
|
||||
index: DefIndex::from_array_index(self.macro_map.len(),
|
||||
DefIndexAddressSpace::Low),
|
||||
};
|
||||
|
@ -335,7 +335,7 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
|
|||
self.definitions.add_parent_module_of_macro_def(invoc.expansion_data.mark,
|
||||
normal_module_def_id);
|
||||
invoc.expansion_data.mark.set_default_transparency(ext.default_transparency());
|
||||
invoc.expansion_data.mark.set_is_builtin(def_id.krate == BUILTIN_MACROS_CRATE);
|
||||
invoc.expansion_data.mark.set_is_builtin(def_id.krate == CrateNum::BuiltinMacros);
|
||||
}
|
||||
|
||||
Ok(Some(ext))
|
||||
|
@ -1087,7 +1087,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
|
|||
};
|
||||
|
||||
// Plugin-based syntax extensions are exempt from this check
|
||||
if krate == BUILTIN_MACROS_CRATE { return; }
|
||||
if krate == CrateNum::BuiltinMacros { return; }
|
||||
|
||||
let ext = binding.get_macro(self);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue