Skip to content

Commit 1ec8670

Browse files
committed
Split CrateNum into an enum instead of having magic constants
1 parent d1b5231 commit 1ec8670

File tree

5 files changed

+58
-26
lines changed

5 files changed

+58
-26
lines changed

Diff for: src/librustc/hir/def_id.rs

+47-14
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,51 @@ use std::u32;
1818
newtype_index! {
1919
pub struct CrateNum {
2020
ENCODABLE = custom
21-
DEBUG_FORMAT = "crate{}",
21+
}
22+
}
23+
24+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
25+
pub enum CrateNum {
26+
/// Virtual crate for builtin macros
27+
// FIXME(jseyfried): this is also used for custom derives until proc-macro crates get
28+
// `CrateNum`s.
29+
BuiltinMacros,
30+
/// A CrateNum value that indicates that something is wrong.
31+
Invalid,
32+
/// A special CrateNum that we use for the tcx.rcache when decoding from
33+
/// the incr. comp. cache.
34+
ReservedForIncrCompCache,
35+
Index(CrateId),
36+
}
2237

23-
/// Item definitions in the currently-compiled crate would have the CrateNum
24-
/// LOCAL_CRATE in their DefId.
25-
const LOCAL_CRATE = 0,
38+
impl ::std::fmt::Debug for CrateNum {
39+
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
40+
match self {
41+
CrateNum::Index(id) => write!(fmt, "crate{}", id.0),
42+
CrateNum::Invalid => write!(fmt, "invalid crate"),
43+
CrateNum::BuiltinMacros => write!(fmt, "bultin macros crate"),
44+
CrateNum::ReservedForIncrCompCache => write!(fmt, "crate for decoding incr comp cache"),
45+
}
46+
}
47+
}
2648

27-
/// Virtual crate for builtin macros
28-
// FIXME(jseyfried): this is also used for custom derives until proc-macro crates get
29-
// `CrateNum`s.
30-
const BUILTIN_MACROS_CRATE = CrateNum::MAX_AS_U32,
49+
/// Item definitions in the currently-compiled crate would have the CrateNum
50+
/// LOCAL_CRATE in their DefId.
51+
pub const LOCAL_CRATE: CrateNum = CrateNum::Index(CrateId(0));
3152

32-
/// A CrateNum value that indicates that something is wrong.
33-
const INVALID_CRATE = CrateNum::MAX_AS_U32 - 1,
3453

35-
/// A special CrateNum that we use for the tcx.rcache when decoding from
36-
/// the incr. comp. cache.
37-
const RESERVED_FOR_INCR_COMP_CACHE = CrateNum::MAX_AS_U32 - 2,
54+
impl Idx for CrateNum {
55+
#[inline]
56+
fn new(value: usize) -> Self {
57+
CrateNum::Index(Idx::new(value))
58+
}
59+
60+
#[inline]
61+
fn index(self) -> usize {
62+
match self {
63+
CrateNum::Index(idx) => Idx::index(idx),
64+
_ => bug!("Tried to get crate index of {:?}", self),
65+
}
3866
}
3967
}
4068

@@ -48,7 +76,12 @@ impl CrateNum {
4876

4977
impl fmt::Display for CrateNum {
5078
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
51-
fmt::Display::fmt(&self.as_u32(), f)
79+
match self {
80+
CrateNum::Index(id) => fmt::Display::fmt(&id.0, f),
81+
CrateNum::Invalid => write!(f, "invalid crate"),
82+
CrateNum::BuiltinMacros => write!(f, "bultin macros crate"),
83+
CrateNum::ReservedForIncrCompCache => write!(f, "crate for decoding incr comp cache"),
84+
}
5285
}
5386
}
5487

Diff for: src/librustc/ty/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -829,9 +829,9 @@ impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for TypeckTables<'gcx> {
829829
impl<'tcx> CommonTypes<'tcx> {
830830
fn new(interners: &CtxtInterners<'tcx>) -> CommonTypes<'tcx> {
831831
// Ensure our type representation does not grow
832-
#[cfg(target_pointer_width = "64")]
832+
#[cfg(all(not(stage0), target_pointer_width = "64"))]
833833
assert!(mem::size_of::<ty::TyKind>() <= 24);
834-
#[cfg(target_pointer_width = "64")]
834+
#[cfg(all(not(stage0), target_pointer_width = "64"))]
835835
assert!(mem::size_of::<ty::TyS>() <= 32);
836836

837837
let mk = |sty| CtxtInterners::intern_ty(interners, interners, sty);

Diff for: src/librustc/ty/query/on_disk_cache.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
use dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
1212
use errors::Diagnostic;
1313
use hir;
14-
use hir::def_id::{CrateNum, DefIndex, DefId, LocalDefId,
15-
RESERVED_FOR_INCR_COMP_CACHE, LOCAL_CRATE};
14+
use hir::def_id::{CrateNum, DefIndex, DefId, LocalDefId, LOCAL_CRATE};
1615
use hir::map::definitions::DefPathHash;
1716
use ich::{CachingSourceMapView, Fingerprint};
1817
use mir::{self, interpret};
@@ -566,7 +565,7 @@ impl<'a, 'tcx: 'a, 'x> ty_codec::TyDecoder<'a, 'tcx> for CacheDecoder<'a, 'tcx,
566565
let tcx = self.tcx();
567566

568567
let cache_key = ty::CReaderCacheKey {
569-
cnum: RESERVED_FOR_INCR_COMP_CACHE,
568+
cnum: CrateNum::ReservedForIncrCompCache,
570569
pos: shorthand,
571570
};
572571

Diff for: src/librustc_resolve/build_reduced_graph.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use Namespace::{self, TypeNS, ValueNS, MacroNS};
2222
use {resolve_error, resolve_struct_error, ResolutionError};
2323

2424
use rustc::hir::def::*;
25-
use rustc::hir::def_id::{BUILTIN_MACROS_CRATE, CRATE_DEF_INDEX, LOCAL_CRATE, DefId};
25+
use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, LOCAL_CRATE, DefId};
2626
use rustc::ty;
2727
use rustc::middle::cstore::CrateStore;
2828
use rustc_metadata::cstore::LoadedMacro;
@@ -768,7 +768,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
768768
let def_id = self.macro_defs[&expansion];
769769
if let Some(id) = self.definitions.as_local_node_id(def_id) {
770770
self.local_macro_def_scopes[&id]
771-
} else if def_id.krate == BUILTIN_MACROS_CRATE {
771+
} else if def_id.krate == CrateNum::BuiltinMacros {
772772
self.injected_crate.unwrap_or(self.graph_root)
773773
} else {
774774
let module_def_id = ty::DefIdTree::parent(&*self, def_id).unwrap();

Diff for: src/librustc_resolve/macros.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use ModuleOrUniformRoot;
1414
use Namespace::{self, TypeNS, MacroNS};
1515
use build_reduced_graph::{BuildReducedGraphVisitor, IsMacroExport};
1616
use resolve_imports::ImportResolver;
17-
use rustc::hir::def_id::{DefId, BUILTIN_MACROS_CRATE, CRATE_DEF_INDEX, DefIndex,
18-
DefIndexAddressSpace};
17+
use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX, DefIndex,
18+
CrateNum, DefIndexAddressSpace};
1919
use rustc::hir::def::{Def, NonMacroAttrKind};
2020
use rustc::hir::map::{self, DefCollector};
2121
use rustc::{ty, lint};
@@ -202,7 +202,7 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
202202

203203
fn add_builtin(&mut self, ident: ast::Ident, ext: Lrc<SyntaxExtension>) {
204204
let def_id = DefId {
205-
krate: BUILTIN_MACROS_CRATE,
205+
krate: CrateNum::BuiltinMacros,
206206
index: DefIndex::from_array_index(self.macro_map.len(),
207207
DefIndexAddressSpace::Low),
208208
};
@@ -335,7 +335,7 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
335335
self.definitions.add_parent_module_of_macro_def(invoc.expansion_data.mark,
336336
normal_module_def_id);
337337
invoc.expansion_data.mark.set_default_transparency(ext.default_transparency());
338-
invoc.expansion_data.mark.set_is_builtin(def_id.krate == BUILTIN_MACROS_CRATE);
338+
invoc.expansion_data.mark.set_is_builtin(def_id.krate == CrateNum::BuiltinMacros);
339339
}
340340

341341
Ok(Some(ext))
@@ -1087,7 +1087,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
10871087
};
10881088

10891089
// Plugin-based syntax extensions are exempt from this check
1090-
if krate == BUILTIN_MACROS_CRATE { return; }
1090+
if krate == CrateNum::BuiltinMacros { return; }
10911091

10921092
let ext = binding.get_macro(self);
10931093

0 commit comments

Comments
 (0)