Skip to content

Commit cb1ff24

Browse files
incr.comp.: Remove default serialization implementations for things in rustc::hir::def_id so that we get an ICE instead of silently doing the wrong thing.
1 parent 723028f commit cb1ff24

File tree

9 files changed

+81
-62
lines changed

9 files changed

+81
-62
lines changed

src/librustc/hir/def_id.rs

Lines changed: 7 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
use ty;
1212

1313
use rustc_data_structures::indexed_vec::Idx;
14-
use serialize::{self, Encoder, Decoder, Decodable, Encodable};
15-
14+
use serialize;
1615
use std::fmt;
1716
use std::u32;
1817

@@ -65,17 +64,8 @@ impl fmt::Display for CrateNum {
6564
}
6665
}
6766

68-
impl serialize::UseSpecializedEncodable for CrateNum {
69-
fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
70-
s.emit_u32(self.0)
71-
}
72-
}
73-
74-
impl serialize::UseSpecializedDecodable for CrateNum {
75-
fn default_decode<D: Decoder>(d: &mut D) -> Result<CrateNum, D::Error> {
76-
d.read_u32().map(CrateNum)
77-
}
78-
}
67+
impl serialize::UseSpecializedEncodable for CrateNum {}
68+
impl serialize::UseSpecializedDecodable for CrateNum {}
7969

8070
/// A DefIndex is an index into the hir-map for a crate, identifying a
8171
/// particular definition. It should really be considered an interned
@@ -151,19 +141,8 @@ impl DefIndex {
151141
}
152142
}
153143

154-
impl serialize::UseSpecializedEncodable for DefIndex {
155-
#[inline]
156-
fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
157-
s.emit_u32(self.0)
158-
}
159-
}
160-
161-
impl serialize::UseSpecializedDecodable for DefIndex {
162-
#[inline]
163-
fn default_decode<D: Decoder>(d: &mut D) -> Result<DefIndex, D::Error> {
164-
d.read_u32().map(DefIndex)
165-
}
166-
}
144+
impl serialize::UseSpecializedEncodable for DefIndex {}
145+
impl serialize::UseSpecializedDecodable for DefIndex {}
167146

168147
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
169148
pub enum DefIndexAddressSpace {
@@ -225,31 +204,8 @@ impl DefId {
225204
}
226205
}
227206

228-
impl serialize::UseSpecializedEncodable for DefId {
229-
#[inline]
230-
fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
231-
let DefId {
232-
krate,
233-
index,
234-
} = *self;
235-
236-
krate.encode(s)?;
237-
index.encode(s)
238-
}
239-
}
240-
241-
impl serialize::UseSpecializedDecodable for DefId {
242-
#[inline]
243-
fn default_decode<D: Decoder>(d: &mut D) -> Result<DefId, D::Error> {
244-
let krate = CrateNum::decode(d)?;
245-
let index = DefIndex::decode(d)?;
246-
247-
Ok(DefId {
248-
krate,
249-
index
250-
})
251-
}
252-
}
207+
impl serialize::UseSpecializedEncodable for DefId {}
208+
impl serialize::UseSpecializedDecodable for DefId {}
253209

254210

255211
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]

src/librustc/middle/cstore.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
2525
use hir;
2626
use hir::def;
27-
use hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE};
27+
use hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
2828
use hir::map as hir_map;
2929
use hir::map::definitions::{Definitions, DefKey, DefPathTable};
3030
use hir::svh::Svh;
@@ -180,7 +180,7 @@ impl EncodedMetadata {
180180
/// upstream crate.
181181
#[derive(Debug, RustcEncodable, RustcDecodable, Copy, Clone)]
182182
pub struct EncodedMetadataHash {
183-
pub def_index: DefIndex,
183+
pub def_index: u32,
184184
pub hash: ich::Fingerprint,
185185
}
186186

src/librustc/session/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,7 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
10421042
"enable incremental compilation (experimental)"),
10431043
incremental_cc: bool = (false, parse_bool, [UNTRACKED],
10441044
"enable cross-crate incremental compilation (even more experimental)"),
1045-
incremental_queries: bool = (false, parse_bool, [UNTRACKED],
1045+
incremental_queries: bool = (true, parse_bool, [UNTRACKED],
10461046
"enable incremental compilation support for queries (experimental)"),
10471047
incremental_info: bool = (false, parse_bool, [UNTRACKED],
10481048
"print high-level information about incremental reuse (or the lack thereof)"),

src/librustc/ty/maps/on_disk_cache.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,14 +559,25 @@ impl<'enc, 'a, 'tcx, E> CacheEncoder<'enc, 'a, 'tcx, E>
559559
impl<'enc, 'a, 'tcx, E> ty_codec::TyEncoder for CacheEncoder<'enc, 'a, 'tcx, E>
560560
where E: 'enc + ty_codec::TyEncoder
561561
{
562+
#[inline]
562563
fn position(&self) -> usize {
563564
self.encoder.position()
564565
}
565566
}
566567

568+
impl<'enc, 'a, 'tcx, E> SpecializedEncoder<CrateNum> for CacheEncoder<'enc, 'a, 'tcx, E>
569+
where E: 'enc + ty_codec::TyEncoder
570+
{
571+
#[inline]
572+
fn specialized_encode(&mut self, cnum: &CrateNum) -> Result<(), Self::Error> {
573+
self.emit_u32(cnum.as_u32())
574+
}
575+
}
576+
567577
impl<'enc, 'a, 'tcx, E> SpecializedEncoder<ty::Ty<'tcx>> for CacheEncoder<'enc, 'a, 'tcx, E>
568578
where E: 'enc + ty_codec::TyEncoder
569579
{
580+
#[inline]
570581
fn specialized_encode(&mut self, ty: &ty::Ty<'tcx>) -> Result<(), Self::Error> {
571582
ty_codec::encode_with_shorthand(self, ty,
572583
|encoder| &mut encoder.type_shorthands)
@@ -577,6 +588,7 @@ impl<'enc, 'a, 'tcx, E> SpecializedEncoder<ty::GenericPredicates<'tcx>>
577588
for CacheEncoder<'enc, 'a, 'tcx, E>
578589
where E: 'enc + ty_codec::TyEncoder
579590
{
591+
#[inline]
580592
fn specialized_encode(&mut self,
581593
predicates: &ty::GenericPredicates<'tcx>)
582594
-> Result<(), Self::Error> {
@@ -588,6 +600,7 @@ impl<'enc, 'a, 'tcx, E> SpecializedEncoder<ty::GenericPredicates<'tcx>>
588600
impl<'enc, 'a, 'tcx, E> SpecializedEncoder<hir::HirId> for CacheEncoder<'enc, 'a, 'tcx, E>
589601
where E: 'enc + ty_codec::TyEncoder
590602
{
603+
#[inline]
591604
fn specialized_encode(&mut self, id: &hir::HirId) -> Result<(), Self::Error> {
592605
let hir::HirId {
593606
owner,
@@ -605,6 +618,7 @@ impl<'enc, 'a, 'tcx, E> SpecializedEncoder<hir::HirId> for CacheEncoder<'enc, 'a
605618
impl<'enc, 'a, 'tcx, E> SpecializedEncoder<DefId> for CacheEncoder<'enc, 'a, 'tcx, E>
606619
where E: 'enc + ty_codec::TyEncoder
607620
{
621+
#[inline]
608622
fn specialized_encode(&mut self, id: &DefId) -> Result<(), Self::Error> {
609623
let def_path_hash = self.tcx.def_path_hash(*id);
610624
def_path_hash.encode(self)
@@ -614,6 +628,7 @@ impl<'enc, 'a, 'tcx, E> SpecializedEncoder<DefId> for CacheEncoder<'enc, 'a, 'tc
614628
impl<'enc, 'a, 'tcx, E> SpecializedEncoder<LocalDefId> for CacheEncoder<'enc, 'a, 'tcx, E>
615629
where E: 'enc + ty_codec::TyEncoder
616630
{
631+
#[inline]
617632
fn specialized_encode(&mut self, id: &LocalDefId) -> Result<(), Self::Error> {
618633
id.to_def_id().encode(self)
619634
}
@@ -632,6 +647,7 @@ impl<'enc, 'a, 'tcx, E> SpecializedEncoder<DefIndex> for CacheEncoder<'enc, 'a,
632647
impl<'enc, 'a, 'tcx, E> SpecializedEncoder<NodeId> for CacheEncoder<'enc, 'a, 'tcx, E>
633648
where E: 'enc + ty_codec::TyEncoder
634649
{
650+
#[inline]
635651
fn specialized_encode(&mut self, node_id: &NodeId) -> Result<(), Self::Error> {
636652
let hir_id = self.tcx.hir.node_to_hir_id(*node_id);
637653
hir_id.encode(self)

src/librustc_incremental/persist/data.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
//! The data that we will serialize and deserialize.
1212
1313
use rustc::dep_graph::{WorkProduct, WorkProductId};
14-
use rustc::hir::def_id::DefIndex;
1514
use rustc::hir::map::DefPathHash;
1615
use rustc::middle::cstore::EncodedMetadataHash;
1716
use rustc_data_structures::fx::FxHashMap;
@@ -58,5 +57,5 @@ pub struct SerializedMetadataHashes {
5857
/// is only populated if -Z query-dep-graph is specified. It will be
5958
/// empty otherwise. Importing crates are perfectly happy with just having
6059
/// the DefIndex.
61-
pub index_map: FxHashMap<DefIndex, DefPathHash>
60+
pub index_map: FxHashMap<u32, DefPathHash>
6261
}

src/librustc_incremental/persist/save.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
use rustc::dep_graph::{DepGraph, DepKind};
12-
use rustc::hir::def_id::DefId;
12+
use rustc::hir::def_id::{DefId, DefIndex};
1313
use rustc::hir::svh::Svh;
1414
use rustc::ich::Fingerprint;
1515
use rustc::middle::cstore::EncodedMetadataHashes;
@@ -270,11 +270,11 @@ fn encode_metadata_hashes(tcx: TyCtxt,
270270

271271
if tcx.sess.opts.debugging_opts.query_dep_graph {
272272
for serialized_hash in &serialized_hashes.entry_hashes {
273-
let def_id = DefId::local(serialized_hash.def_index);
273+
let def_id = DefId::local(DefIndex::from_u32(serialized_hash.def_index));
274274

275275
// Store entry in the index_map
276276
let def_path_hash = tcx.def_path_hash(def_id);
277-
serialized_hashes.index_map.insert(def_id.index, def_path_hash);
277+
serialized_hashes.index_map.insert(def_id.index.as_u32(), def_path_hash);
278278

279279
// Record hash in current_metadata_hashes
280280
current_metadata_hashes.insert(def_id, serialized_hash.hash);

src/librustc_metadata/decoder.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,27 @@ impl<'a, 'tcx, T> SpecializedDecoder<LazySeq<T>> for DecodeContext<'a, 'tcx> {
246246
}
247247
}
248248

249+
250+
impl<'a, 'tcx> SpecializedDecoder<DefId> for DecodeContext<'a, 'tcx> {
251+
#[inline]
252+
fn specialized_decode(&mut self) -> Result<DefId, Self::Error> {
253+
let krate = CrateNum::decode(self)?;
254+
let index = DefIndex::decode(self)?;
255+
256+
Ok(DefId {
257+
krate,
258+
index,
259+
})
260+
}
261+
}
262+
263+
impl<'a, 'tcx> SpecializedDecoder<DefIndex> for DecodeContext<'a, 'tcx> {
264+
#[inline]
265+
fn specialized_decode(&mut self) -> Result<DefIndex, Self::Error> {
266+
Ok(DefIndex::from_u32(self.read_u32()?))
267+
}
268+
}
269+
249270
impl<'a, 'tcx> SpecializedDecoder<Span> for DecodeContext<'a, 'tcx> {
250271
fn specialized_decode(&mut self) -> Result<Span, Self::Error> {
251272
let lo = BytePos::decode(self)?;

src/librustc_metadata/encoder.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,33 @@ impl<'a, 'tcx, T> SpecializedEncoder<LazySeq<T>> for EncodeContext<'a, 'tcx> {
116116
}
117117
}
118118

119+
impl<'a, 'tcx> SpecializedEncoder<CrateNum> for EncodeContext<'a, 'tcx> {
120+
#[inline]
121+
fn specialized_encode(&mut self, cnum: &CrateNum) -> Result<(), Self::Error> {
122+
self.emit_u32(cnum.as_u32())
123+
}
124+
}
125+
126+
impl<'a, 'tcx> SpecializedEncoder<DefId> for EncodeContext<'a, 'tcx> {
127+
#[inline]
128+
fn specialized_encode(&mut self, def_id: &DefId) -> Result<(), Self::Error> {
129+
let DefId {
130+
krate,
131+
index,
132+
} = *def_id;
133+
134+
krate.encode(self)?;
135+
index.encode(self)
136+
}
137+
}
138+
139+
impl<'a, 'tcx> SpecializedEncoder<DefIndex> for EncodeContext<'a, 'tcx> {
140+
#[inline]
141+
fn specialized_encode(&mut self, def_index: &DefIndex) -> Result<(), Self::Error> {
142+
self.emit_u32(def_index.as_u32())
143+
}
144+
}
145+
119146
impl<'a, 'tcx> SpecializedEncoder<Ty<'tcx>> for EncodeContext<'a, 'tcx> {
120147
fn specialized_encode(&mut self, ty: &Ty<'tcx>) -> Result<(), Self::Error> {
121148
ty_codec::encode_with_shorthand(self, ty, |ecx| &mut ecx.type_shorthands)
@@ -213,7 +240,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
213240

214241
if let Some(fingerprint) = fingerprint {
215242
this.metadata_hashes.hashes.push(EncodedMetadataHash {
216-
def_index,
243+
def_index: def_index.as_u32(),
217244
hash: fingerprint,
218245
})
219246
}
@@ -395,7 +422,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
395422
let total_bytes = self.position();
396423

397424
self.metadata_hashes.hashes.push(EncodedMetadataHash {
398-
def_index: global_metadata_def_index(GlobalMetaDataKind::Krate),
425+
def_index: global_metadata_def_index(GlobalMetaDataKind::Krate).as_u32(),
399426
hash: Fingerprint::from_smaller_hash(link_meta.crate_hash.as_u64())
400427
});
401428

src/librustc_metadata/index_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl<'a, 'b, 'tcx> IndexBuilder<'a, 'b, 'tcx> {
136136
let (fingerprint, ecx) = entry_builder.finish();
137137
if let Some(hash) = fingerprint {
138138
ecx.metadata_hashes.hashes.push(EncodedMetadataHash {
139-
def_index: id.index,
139+
def_index: id.index.as_u32(),
140140
hash,
141141
});
142142
}

0 commit comments

Comments
 (0)