From 64e109327dfd96cb8902b994b0a1e7f2d32215b0 Mon Sep 17 00:00:00 2001 From: Michael Woerister Date: Tue, 28 Nov 2017 14:19:44 +0100 Subject: [PATCH] incr.comp.: Add a newtype for byte positions within the incr. comp. cache. --- src/librustc/ty/maps/on_disk_cache.rs | 36 +++++++++++++++++++-------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/src/librustc/ty/maps/on_disk_cache.rs b/src/librustc/ty/maps/on_disk_cache.rs index 40103571afad..18304b7aa7d7 100644 --- a/src/librustc/ty/maps/on_disk_cache.rs +++ b/src/librustc/ty/maps/on_disk_cache.rs @@ -73,11 +73,11 @@ pub struct OnDiskCache<'sess> { // These two fields caches that are populated lazily during decoding. file_index_to_file: RefCell>>, - synthetic_expansion_infos: RefCell>, + synthetic_expansion_infos: RefCell>, // A map from dep-node to the position of the cached query result in // `serialized_data`. - query_result_index: FxHashMap, + query_result_index: FxHashMap, } // This type is used only for (de-)serialization. @@ -88,11 +88,25 @@ struct Header { } type EncodedPrevDiagnostics = Vec<(SerializedDepNodeIndex, Vec)>; -type EncodedQueryResultIndex = Vec<(SerializedDepNodeIndex, usize)>; +type EncodedQueryResultIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>; #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)] struct FileMapIndex(u32); +#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, RustcEncodable, RustcDecodable)] +struct AbsoluteBytePos(u32); + +impl AbsoluteBytePos { + fn new(pos: usize) -> AbsoluteBytePos { + debug_assert!(pos <= ::std::u32::MAX as usize); + AbsoluteBytePos(pos as u32) + } + + fn to_usize(self) -> usize { + self.0 as usize + } +} + impl<'sess> OnDiskCache<'sess> { /// Create a new OnDiskCache instance from the serialized data in `data`. pub fn new(sess: &'sess Session, data: Vec, start_pos: usize) -> OnDiskCache<'sess> { @@ -309,7 +323,7 @@ impl<'sess> OnDiskCache<'sess> { let mut decoder = CacheDecoder { tcx: Some(tcx), - opaque: opaque::Decoder::new(&self.serialized_data[..], pos), + opaque: opaque::Decoder::new(&self.serialized_data[..], pos.to_usize()), codemap: self.codemap, cnum_map: cnum_map.as_ref().unwrap(), file_index_to_file: &mut file_index_to_file, @@ -389,7 +403,7 @@ struct CacheDecoder<'a, 'tcx: 'a, 'x> { opaque: opaque::Decoder<'x>, codemap: &'x CodeMap, cnum_map: &'x IndexVec>, - synthetic_expansion_infos: &'x mut FxHashMap, + synthetic_expansion_infos: &'x mut FxHashMap, file_index_to_file: &'x mut FxHashMap>, file_index_to_stable_id: &'x FxHashMap, } @@ -521,18 +535,18 @@ impl<'a, 'tcx, 'x> SpecializedDecoder for CacheDecoder<'a, 'tcx, 'x> { SyntaxContext::empty() } TAG_EXPANSION_INFO_INLINE => { - let pos = self.position(); + let pos = AbsoluteBytePos::new(self.position()); let expn_info: ExpnInfo = Decodable::decode(self)?; let ctxt = SyntaxContext::allocate_directly(expn_info); self.synthetic_expansion_infos.insert(pos, ctxt); ctxt } TAG_EXPANSION_INFO_SHORTHAND => { - let pos = usize::decode(self)?; + let pos = AbsoluteBytePos::decode(self)?; if let Some(ctxt) = self.synthetic_expansion_infos.get(&pos).cloned() { ctxt } else { - let expn_info = self.with_position(pos, |this| { + let expn_info = self.with_position(pos.to_usize(), |this| { ExpnInfo::decode(this) })?; let ctxt = SyntaxContext::allocate_directly(expn_info); @@ -644,7 +658,7 @@ struct CacheEncoder<'enc, 'a, 'tcx, E> encoder: &'enc mut E, type_shorthands: FxHashMap, usize>, predicate_shorthands: FxHashMap, usize>, - expn_info_shorthands: FxHashMap, + expn_info_shorthands: FxHashMap, codemap: CachingCodemapView<'tcx>, file_to_file_index: FxHashMap<*const FileMap, FileMapIndex>, } @@ -725,7 +739,7 @@ impl<'enc, 'a, 'tcx, E> SpecializedEncoder for CacheEncoder<'enc, 'a, 'tcx pos.encode(self) } else { TAG_EXPANSION_INFO_INLINE.encode(self)?; - let pos = self.position(); + let pos = AbsoluteBytePos::new(self.position()); self.expn_info_shorthands.insert(mark, pos); expn_info.encode(self) } @@ -951,7 +965,7 @@ fn encode_query_results<'enc, 'a, 'tcx, Q, E>(tcx: TyCtxt<'a, 'tcx, 'tcx>, let dep_node = SerializedDepNodeIndex::new(entry.index.index()); // Record position of the cache entry - query_result_index.push((dep_node, encoder.position())); + query_result_index.push((dep_node, AbsoluteBytePos::new(encoder.position()))); // Encode the type check tables with the SerializedDepNodeIndex // as tag.