From cb1ff24425c37835b5d746a22d68047ff2cb807b Mon Sep 17 00:00:00 2001 From: Michael Woerister Date: Thu, 16 Nov 2017 17:13:39 +0100 Subject: [PATCH] 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. --- src/librustc/hir/def_id.rs | 58 +++--------------------- src/librustc/middle/cstore.rs | 4 +- src/librustc/session/config.rs | 2 +- src/librustc/ty/maps/on_disk_cache.rs | 16 +++++++ src/librustc_incremental/persist/data.rs | 3 +- src/librustc_incremental/persist/save.rs | 6 +-- src/librustc_metadata/decoder.rs | 21 +++++++++ src/librustc_metadata/encoder.rs | 31 ++++++++++++- src/librustc_metadata/index_builder.rs | 2 +- 9 files changed, 81 insertions(+), 62 deletions(-) diff --git a/src/librustc/hir/def_id.rs b/src/librustc/hir/def_id.rs index f34022993de2..05d021ac6d26 100644 --- a/src/librustc/hir/def_id.rs +++ b/src/librustc/hir/def_id.rs @@ -11,8 +11,7 @@ use ty; use rustc_data_structures::indexed_vec::Idx; -use serialize::{self, Encoder, Decoder, Decodable, Encodable}; - +use serialize; use std::fmt; use std::u32; @@ -65,17 +64,8 @@ impl fmt::Display for CrateNum { } } -impl serialize::UseSpecializedEncodable for CrateNum { - fn default_encode(&self, s: &mut S) -> Result<(), S::Error> { - s.emit_u32(self.0) - } -} - -impl serialize::UseSpecializedDecodable for CrateNum { - fn default_decode(d: &mut D) -> Result { - d.read_u32().map(CrateNum) - } -} +impl serialize::UseSpecializedEncodable for CrateNum {} +impl serialize::UseSpecializedDecodable for CrateNum {} /// A DefIndex is an index into the hir-map for a crate, identifying a /// particular definition. It should really be considered an interned @@ -151,19 +141,8 @@ impl DefIndex { } } -impl serialize::UseSpecializedEncodable for DefIndex { - #[inline] - fn default_encode(&self, s: &mut S) -> Result<(), S::Error> { - s.emit_u32(self.0) - } -} - -impl serialize::UseSpecializedDecodable for DefIndex { - #[inline] - fn default_decode(d: &mut D) -> Result { - d.read_u32().map(DefIndex) - } -} +impl serialize::UseSpecializedEncodable for DefIndex {} +impl serialize::UseSpecializedDecodable for DefIndex {} #[derive(Copy, Clone, Eq, PartialEq, Hash)] pub enum DefIndexAddressSpace { @@ -225,31 +204,8 @@ impl DefId { } } -impl serialize::UseSpecializedEncodable for DefId { - #[inline] - fn default_encode(&self, s: &mut S) -> Result<(), S::Error> { - let DefId { - krate, - index, - } = *self; - - krate.encode(s)?; - index.encode(s) - } -} - -impl serialize::UseSpecializedDecodable for DefId { - #[inline] - fn default_decode(d: &mut D) -> Result { - let krate = CrateNum::decode(d)?; - let index = DefIndex::decode(d)?; - - Ok(DefId { - krate, - index - }) - } -} +impl serialize::UseSpecializedEncodable for DefId {} +impl serialize::UseSpecializedDecodable for DefId {} #[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] diff --git a/src/librustc/middle/cstore.rs b/src/librustc/middle/cstore.rs index 628538b41c5d..5d7141949e38 100644 --- a/src/librustc/middle/cstore.rs +++ b/src/librustc/middle/cstore.rs @@ -24,7 +24,7 @@ use hir; use hir::def; -use hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE}; +use hir::def_id::{CrateNum, DefId, LOCAL_CRATE}; use hir::map as hir_map; use hir::map::definitions::{Definitions, DefKey, DefPathTable}; use hir::svh::Svh; @@ -180,7 +180,7 @@ impl EncodedMetadata { /// upstream crate. #[derive(Debug, RustcEncodable, RustcDecodable, Copy, Clone)] pub struct EncodedMetadataHash { - pub def_index: DefIndex, + pub def_index: u32, pub hash: ich::Fingerprint, } diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index 2857d50fc87b..b7abcc03132c 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -1042,7 +1042,7 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options, "enable incremental compilation (experimental)"), incremental_cc: bool = (false, parse_bool, [UNTRACKED], "enable cross-crate incremental compilation (even more experimental)"), - incremental_queries: bool = (false, parse_bool, [UNTRACKED], + incremental_queries: bool = (true, parse_bool, [UNTRACKED], "enable incremental compilation support for queries (experimental)"), incremental_info: bool = (false, parse_bool, [UNTRACKED], "print high-level information about incremental reuse (or the lack thereof)"), diff --git a/src/librustc/ty/maps/on_disk_cache.rs b/src/librustc/ty/maps/on_disk_cache.rs index 8c234e632e9f..53ca9b3851d5 100644 --- a/src/librustc/ty/maps/on_disk_cache.rs +++ b/src/librustc/ty/maps/on_disk_cache.rs @@ -559,14 +559,25 @@ impl<'enc, 'a, 'tcx, E> CacheEncoder<'enc, 'a, 'tcx, E> impl<'enc, 'a, 'tcx, E> ty_codec::TyEncoder for CacheEncoder<'enc, 'a, 'tcx, E> where E: 'enc + ty_codec::TyEncoder { + #[inline] fn position(&self) -> usize { self.encoder.position() } } +impl<'enc, 'a, 'tcx, E> SpecializedEncoder for CacheEncoder<'enc, 'a, 'tcx, E> + where E: 'enc + ty_codec::TyEncoder +{ + #[inline] + fn specialized_encode(&mut self, cnum: &CrateNum) -> Result<(), Self::Error> { + self.emit_u32(cnum.as_u32()) + } +} + impl<'enc, 'a, 'tcx, E> SpecializedEncoder> for CacheEncoder<'enc, 'a, 'tcx, E> where E: 'enc + ty_codec::TyEncoder { + #[inline] fn specialized_encode(&mut self, ty: &ty::Ty<'tcx>) -> Result<(), Self::Error> { ty_codec::encode_with_shorthand(self, ty, |encoder| &mut encoder.type_shorthands) @@ -577,6 +588,7 @@ impl<'enc, 'a, 'tcx, E> SpecializedEncoder> for CacheEncoder<'enc, 'a, 'tcx, E> where E: 'enc + ty_codec::TyEncoder { + #[inline] fn specialized_encode(&mut self, predicates: &ty::GenericPredicates<'tcx>) -> Result<(), Self::Error> { @@ -588,6 +600,7 @@ impl<'enc, 'a, 'tcx, E> SpecializedEncoder> impl<'enc, 'a, 'tcx, E> SpecializedEncoder for CacheEncoder<'enc, 'a, 'tcx, E> where E: 'enc + ty_codec::TyEncoder { + #[inline] fn specialized_encode(&mut self, id: &hir::HirId) -> Result<(), Self::Error> { let hir::HirId { owner, @@ -605,6 +618,7 @@ impl<'enc, 'a, 'tcx, E> SpecializedEncoder for CacheEncoder<'enc, 'a impl<'enc, 'a, 'tcx, E> SpecializedEncoder for CacheEncoder<'enc, 'a, 'tcx, E> where E: 'enc + ty_codec::TyEncoder { + #[inline] fn specialized_encode(&mut self, id: &DefId) -> Result<(), Self::Error> { let def_path_hash = self.tcx.def_path_hash(*id); def_path_hash.encode(self) @@ -614,6 +628,7 @@ impl<'enc, 'a, 'tcx, E> SpecializedEncoder for CacheEncoder<'enc, 'a, 'tc impl<'enc, 'a, 'tcx, E> SpecializedEncoder for CacheEncoder<'enc, 'a, 'tcx, E> where E: 'enc + ty_codec::TyEncoder { + #[inline] fn specialized_encode(&mut self, id: &LocalDefId) -> Result<(), Self::Error> { id.to_def_id().encode(self) } @@ -632,6 +647,7 @@ impl<'enc, 'a, 'tcx, E> SpecializedEncoder for CacheEncoder<'enc, 'a, impl<'enc, 'a, 'tcx, E> SpecializedEncoder for CacheEncoder<'enc, 'a, 'tcx, E> where E: 'enc + ty_codec::TyEncoder { + #[inline] fn specialized_encode(&mut self, node_id: &NodeId) -> Result<(), Self::Error> { let hir_id = self.tcx.hir.node_to_hir_id(*node_id); hir_id.encode(self) diff --git a/src/librustc_incremental/persist/data.rs b/src/librustc_incremental/persist/data.rs index fc417851b889..08f9dba2ba16 100644 --- a/src/librustc_incremental/persist/data.rs +++ b/src/librustc_incremental/persist/data.rs @@ -11,7 +11,6 @@ //! The data that we will serialize and deserialize. use rustc::dep_graph::{WorkProduct, WorkProductId}; -use rustc::hir::def_id::DefIndex; use rustc::hir::map::DefPathHash; use rustc::middle::cstore::EncodedMetadataHash; use rustc_data_structures::fx::FxHashMap; @@ -58,5 +57,5 @@ pub struct SerializedMetadataHashes { /// is only populated if -Z query-dep-graph is specified. It will be /// empty otherwise. Importing crates are perfectly happy with just having /// the DefIndex. - pub index_map: FxHashMap + pub index_map: FxHashMap } diff --git a/src/librustc_incremental/persist/save.rs b/src/librustc_incremental/persist/save.rs index 63038f1b93a3..b6dabf99be7d 100644 --- a/src/librustc_incremental/persist/save.rs +++ b/src/librustc_incremental/persist/save.rs @@ -9,7 +9,7 @@ // except according to those terms. use rustc::dep_graph::{DepGraph, DepKind}; -use rustc::hir::def_id::DefId; +use rustc::hir::def_id::{DefId, DefIndex}; use rustc::hir::svh::Svh; use rustc::ich::Fingerprint; use rustc::middle::cstore::EncodedMetadataHashes; @@ -270,11 +270,11 @@ fn encode_metadata_hashes(tcx: TyCtxt, if tcx.sess.opts.debugging_opts.query_dep_graph { for serialized_hash in &serialized_hashes.entry_hashes { - let def_id = DefId::local(serialized_hash.def_index); + let def_id = DefId::local(DefIndex::from_u32(serialized_hash.def_index)); // Store entry in the index_map let def_path_hash = tcx.def_path_hash(def_id); - serialized_hashes.index_map.insert(def_id.index, def_path_hash); + serialized_hashes.index_map.insert(def_id.index.as_u32(), def_path_hash); // Record hash in current_metadata_hashes current_metadata_hashes.insert(def_id, serialized_hash.hash); diff --git a/src/librustc_metadata/decoder.rs b/src/librustc_metadata/decoder.rs index ddc8b6abfacc..0dd1b9e500c0 100644 --- a/src/librustc_metadata/decoder.rs +++ b/src/librustc_metadata/decoder.rs @@ -246,6 +246,27 @@ impl<'a, 'tcx, T> SpecializedDecoder> for DecodeContext<'a, 'tcx> { } } + +impl<'a, 'tcx> SpecializedDecoder for DecodeContext<'a, 'tcx> { + #[inline] + fn specialized_decode(&mut self) -> Result { + let krate = CrateNum::decode(self)?; + let index = DefIndex::decode(self)?; + + Ok(DefId { + krate, + index, + }) + } +} + +impl<'a, 'tcx> SpecializedDecoder for DecodeContext<'a, 'tcx> { + #[inline] + fn specialized_decode(&mut self) -> Result { + Ok(DefIndex::from_u32(self.read_u32()?)) + } +} + impl<'a, 'tcx> SpecializedDecoder for DecodeContext<'a, 'tcx> { fn specialized_decode(&mut self) -> Result { let lo = BytePos::decode(self)?; diff --git a/src/librustc_metadata/encoder.rs b/src/librustc_metadata/encoder.rs index d5eee14bf506..19d0de733465 100644 --- a/src/librustc_metadata/encoder.rs +++ b/src/librustc_metadata/encoder.rs @@ -116,6 +116,33 @@ impl<'a, 'tcx, T> SpecializedEncoder> for EncodeContext<'a, 'tcx> { } } +impl<'a, 'tcx> SpecializedEncoder for EncodeContext<'a, 'tcx> { + #[inline] + fn specialized_encode(&mut self, cnum: &CrateNum) -> Result<(), Self::Error> { + self.emit_u32(cnum.as_u32()) + } +} + +impl<'a, 'tcx> SpecializedEncoder for EncodeContext<'a, 'tcx> { + #[inline] + fn specialized_encode(&mut self, def_id: &DefId) -> Result<(), Self::Error> { + let DefId { + krate, + index, + } = *def_id; + + krate.encode(self)?; + index.encode(self) + } +} + +impl<'a, 'tcx> SpecializedEncoder for EncodeContext<'a, 'tcx> { + #[inline] + fn specialized_encode(&mut self, def_index: &DefIndex) -> Result<(), Self::Error> { + self.emit_u32(def_index.as_u32()) + } +} + impl<'a, 'tcx> SpecializedEncoder> for EncodeContext<'a, 'tcx> { fn specialized_encode(&mut self, ty: &Ty<'tcx>) -> Result<(), Self::Error> { ty_codec::encode_with_shorthand(self, ty, |ecx| &mut ecx.type_shorthands) @@ -213,7 +240,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { if let Some(fingerprint) = fingerprint { this.metadata_hashes.hashes.push(EncodedMetadataHash { - def_index, + def_index: def_index.as_u32(), hash: fingerprint, }) } @@ -395,7 +422,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let total_bytes = self.position(); self.metadata_hashes.hashes.push(EncodedMetadataHash { - def_index: global_metadata_def_index(GlobalMetaDataKind::Krate), + def_index: global_metadata_def_index(GlobalMetaDataKind::Krate).as_u32(), hash: Fingerprint::from_smaller_hash(link_meta.crate_hash.as_u64()) }); diff --git a/src/librustc_metadata/index_builder.rs b/src/librustc_metadata/index_builder.rs index 1d2b6cc33d46..46706bba96d6 100644 --- a/src/librustc_metadata/index_builder.rs +++ b/src/librustc_metadata/index_builder.rs @@ -136,7 +136,7 @@ impl<'a, 'b, 'tcx> IndexBuilder<'a, 'b, 'tcx> { let (fingerprint, ecx) = entry_builder.finish(); if let Some(hash) = fingerprint { ecx.metadata_hashes.hashes.push(EncodedMetadataHash { - def_index: id.index, + def_index: id.index.as_u32(), hash, }); }