1 use crate::dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
2 use crate::mir::interpret::{AllocDecodingSession, AllocDecodingState};
3 use crate::mir::{self, interpret};
4 use crate::ty::codec::{OpaqueEncoder, RefDecodable, TyDecoder, TyEncoder};
5 use crate::ty::context::TyCtxt;
6 use crate::ty::{self, Ty};
7 use rustc_data_structures::fingerprint::{Fingerprint, FingerprintDecoder, FingerprintEncoder};
8 use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
9 use rustc_data_structures::sync::{HashMapExt, Lock, Lrc, OnceCell};
10 use rustc_data_structures::thin_vec::ThinVec;
11 use rustc_errors::Diagnostic;
12 use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, LOCAL_CRATE};
13 use rustc_hir::definitions::DefPathHash;
14 use rustc_index::vec::{Idx, IndexVec};
15 use rustc_serialize::{opaque, Decodable, Decoder, Encodable, Encoder};
16 use rustc_session::{CrateDisambiguator, Session};
17 use rustc_span::hygiene::{
18 ExpnDataDecodeMode, ExpnDataEncodeMode, ExpnId, HygieneDecodeContext, HygieneEncodeContext,
19 SyntaxContext, SyntaxContextData,
21 use rustc_span::source_map::{SourceMap, StableSourceFileId};
22 use rustc_span::CachingSourceMapView;
23 use rustc_span::{BytePos, ExpnData, SourceFile, Span, DUMMY_SP};
26 const TAG_FILE_FOOTER: u128 = 0xC0FFEE_C0FFEE_C0FFEE_C0FFEE_C0FFEE;
28 const TAG_VALID_SPAN: u8 = 0;
29 const TAG_INVALID_SPAN: u8 = 1;
31 const TAG_SYNTAX_CONTEXT: u8 = 0;
32 const TAG_EXPN_DATA: u8 = 1;
34 /// Provides an interface to incremental compilation data cached from the
35 /// previous compilation session. This data will eventually include the results
36 /// of a few selected queries (like `typeck` and `mir_optimized`) and
37 /// any diagnostics that have been emitted during a query.
38 pub struct OnDiskCache<'sess> {
39 // The complete cache data in serialized form.
40 serialized_data: Vec<u8>,
42 // Collects all `Diagnostic`s emitted during the current compilation
44 current_diagnostics: Lock<FxHashMap<DepNodeIndex, Vec<Diagnostic>>>,
46 prev_cnums: Vec<(u32, String, CrateDisambiguator)>,
47 cnum_map: OnceCell<IndexVec<CrateNum, Option<CrateNum>>>,
49 source_map: &'sess SourceMap,
50 file_index_to_stable_id: FxHashMap<SourceFileIndex, StableSourceFileId>,
52 // Caches that are populated lazily during decoding.
53 file_index_to_file: Lock<FxHashMap<SourceFileIndex, Lrc<SourceFile>>>,
55 // A map from dep-node to the position of the cached query result in
57 query_result_index: FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
59 // A map from dep-node to the position of any associated diagnostics in
61 prev_diagnostics_index: FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
63 alloc_decoding_state: AllocDecodingState,
65 // A map from syntax context ids to the position of their associated
66 // `SyntaxContextData`. We use a `u32` instead of a `SyntaxContext`
67 // to represent the fact that we are storing *encoded* ids. When we decode
68 // a `SyntaxContext`, a new id will be allocated from the global `HygieneData`,
69 // which will almost certainly be different than the serialized id.
70 syntax_contexts: FxHashMap<u32, AbsoluteBytePos>,
71 // A map from the `DefPathHash` of an `ExpnId` to the position
72 // of their associated `ExpnData`. Ideally, we would store a `DefId`,
73 // but we need to decode this before we've constructed a `TyCtxt` (which
74 // makes it difficult to decode a `DefId`).
76 // Note that these `DefPathHashes` correspond to both local and foreign
77 // `ExpnData` (e.g `ExpnData.krate` may not be `LOCAL_CRATE`). Alternatively,
78 // we could look up the `ExpnData` from the metadata of foreign crates,
79 // but it seemed easier to have `OnDiskCache` be independent of the `CStore`.
80 expn_data: FxHashMap<u32, AbsoluteBytePos>,
81 // Additional information used when decoding hygiene data.
82 hygiene_context: HygieneDecodeContext,
85 // This type is used only for serialization and deserialization.
86 #[derive(Encodable, Decodable)]
88 file_index_to_stable_id: FxHashMap<SourceFileIndex, StableSourceFileId>,
89 prev_cnums: Vec<(u32, String, CrateDisambiguator)>,
90 query_result_index: EncodedQueryResultIndex,
91 diagnostics_index: EncodedQueryResultIndex,
92 // The location of all allocations.
93 interpret_alloc_index: Vec<u32>,
94 // See `OnDiskCache.syntax_contexts`
95 syntax_contexts: FxHashMap<u32, AbsoluteBytePos>,
96 // See `OnDiskCache.expn_data`
97 expn_data: FxHashMap<u32, AbsoluteBytePos>,
100 type EncodedQueryResultIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
101 type EncodedDiagnosticsIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
102 type EncodedDiagnostics = Vec<Diagnostic>;
104 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Encodable, Decodable)]
105 struct SourceFileIndex(u32);
107 #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Encodable, Decodable)]
108 struct AbsoluteBytePos(u32);
110 impl AbsoluteBytePos {
111 fn new(pos: usize) -> AbsoluteBytePos {
112 debug_assert!(pos <= u32::MAX as usize);
113 AbsoluteBytePos(pos as u32)
116 fn to_usize(self) -> usize {
121 impl<'sess> OnDiskCache<'sess> {
122 /// Creates a new `OnDiskCache` instance from the serialized data in `data`.
123 pub fn new(sess: &'sess Session, data: Vec<u8>, start_pos: usize) -> Self {
124 debug_assert!(sess.opts.incremental.is_some());
126 // Wrap in a scope so we can borrow `data`.
127 let footer: Footer = {
128 let mut decoder = opaque::Decoder::new(&data[..], start_pos);
130 // Decode the *position* of the footer, which can be found in the
131 // last 8 bytes of the file.
132 decoder.set_position(data.len() - IntEncodedWithFixedSize::ENCODED_SIZE);
133 let footer_pos = IntEncodedWithFixedSize::decode(&mut decoder)
134 .expect("error while trying to decode footer position")
137 // Decode the file footer, which contains all the lookup tables, etc.
138 decoder.set_position(footer_pos);
140 decode_tagged(&mut decoder, TAG_FILE_FOOTER)
141 .expect("error while trying to decode footer position")
145 serialized_data: data,
146 file_index_to_stable_id: footer.file_index_to_stable_id,
147 file_index_to_file: Default::default(),
148 prev_cnums: footer.prev_cnums,
149 cnum_map: OnceCell::new(),
150 source_map: sess.source_map(),
151 current_diagnostics: Default::default(),
152 query_result_index: footer.query_result_index.into_iter().collect(),
153 prev_diagnostics_index: footer.diagnostics_index.into_iter().collect(),
154 alloc_decoding_state: AllocDecodingState::new(footer.interpret_alloc_index),
155 syntax_contexts: footer.syntax_contexts,
156 expn_data: footer.expn_data,
157 hygiene_context: Default::default(),
161 pub fn new_empty(source_map: &'sess SourceMap) -> Self {
163 serialized_data: Vec::new(),
164 file_index_to_stable_id: Default::default(),
165 file_index_to_file: Default::default(),
167 cnum_map: OnceCell::new(),
169 current_diagnostics: Default::default(),
170 query_result_index: Default::default(),
171 prev_diagnostics_index: Default::default(),
172 alloc_decoding_state: AllocDecodingState::new(Vec::new()),
173 syntax_contexts: FxHashMap::default(),
174 expn_data: FxHashMap::default(),
175 hygiene_context: Default::default(),
179 pub fn serialize<'tcx, E>(&self, tcx: TyCtxt<'tcx>, encoder: &mut E) -> Result<(), E::Error>
183 // Serializing the `DepGraph` should not modify it.
184 tcx.dep_graph.with_ignore(|| {
185 // Allocate `SourceFileIndex`es.
186 let (file_to_file_index, file_index_to_stable_id) = {
187 let files = tcx.sess.source_map().files();
188 let mut file_to_file_index =
189 FxHashMap::with_capacity_and_hasher(files.len(), Default::default());
190 let mut file_index_to_stable_id =
191 FxHashMap::with_capacity_and_hasher(files.len(), Default::default());
193 for (index, file) in files.iter().enumerate() {
194 let index = SourceFileIndex(index as u32);
195 let file_ptr: *const SourceFile = &**file as *const _;
196 file_to_file_index.insert(file_ptr, index);
197 file_index_to_stable_id.insert(index, StableSourceFileId::new(&file));
200 (file_to_file_index, file_index_to_stable_id)
203 let hygiene_encode_context = HygieneEncodeContext::default();
205 let mut encoder = CacheEncoder {
208 type_shorthands: Default::default(),
209 predicate_shorthands: Default::default(),
210 interpret_allocs: Default::default(),
211 source_map: CachingSourceMapView::new(tcx.sess.source_map()),
213 hygiene_context: &hygiene_encode_context,
216 // Load everything into memory so we can write it out to the on-disk
217 // cache. The vast majority of cacheable query results should already
218 // be in memory, so this should be a cheap operation.
219 tcx.dep_graph.exec_cache_promotions(tcx);
221 // Encode query results.
222 let mut query_result_index = EncodedQueryResultIndex::new();
224 tcx.sess.time("encode_query_results", || {
225 let enc = &mut encoder;
226 let qri = &mut query_result_index;
228 macro_rules! encode_queries {
229 ($($query:ident,)*) => {
231 encode_query_results::<ty::query::queries::$query<'_>, _>(
240 rustc_cached_queries!(encode_queries!);
245 // Encode diagnostics.
246 let diagnostics_index: EncodedDiagnosticsIndex = self
250 .map(|(dep_node_index, diagnostics)| {
251 let pos = AbsoluteBytePos::new(encoder.position());
252 // Let's make sure we get the expected type here.
253 let diagnostics: &EncodedDiagnostics = diagnostics;
254 let dep_node_index = SerializedDepNodeIndex::new(dep_node_index.index());
255 encoder.encode_tagged(dep_node_index, diagnostics)?;
257 Ok((dep_node_index, pos))
259 .collect::<Result<_, _>>()?;
261 let interpret_alloc_index = {
262 let mut interpret_alloc_index = Vec::new();
265 let new_n = encoder.interpret_allocs.len();
266 // If we have found new IDs, serialize those too.
271 interpret_alloc_index.reserve(new_n - n);
272 for idx in n..new_n {
273 let id = encoder.interpret_allocs[idx];
274 let pos = encoder.position() as u32;
275 interpret_alloc_index.push(pos);
276 interpret::specialized_encode_alloc_id(&mut encoder, tcx, id)?;
280 interpret_alloc_index
283 let sorted_cnums = sorted_cnums_including_local_crate(tcx);
284 let prev_cnums: Vec<_> = sorted_cnums
287 let crate_name = tcx.original_crate_name(cnum).to_string();
288 let crate_disambiguator = tcx.crate_disambiguator(cnum);
289 (cnum.as_u32(), crate_name, crate_disambiguator)
293 let mut syntax_contexts = FxHashMap::default();
294 let mut expn_ids = FxHashMap::default();
296 // Encode all hygiene data (`SyntaxContextData` and `ExpnData`) from the current
299 hygiene_encode_context.encode(
301 |encoder, index, ctxt_data| {
302 let pos = AbsoluteBytePos::new(encoder.position());
303 encoder.encode_tagged(TAG_SYNTAX_CONTEXT, ctxt_data)?;
304 syntax_contexts.insert(index, pos);
307 |encoder, index, expn_data| {
308 let pos = AbsoluteBytePos::new(encoder.position());
309 encoder.encode_tagged(TAG_EXPN_DATA, expn_data)?;
310 expn_ids.insert(index, pos);
315 // `Encode the file footer.
316 let footer_pos = encoder.position() as u64;
317 encoder.encode_tagged(
320 file_index_to_stable_id,
324 interpret_alloc_index,
330 // Encode the position of the footer as the last 8 bytes of the
331 // file so we know where to look for it.
332 IntEncodedWithFixedSize(footer_pos).encode(encoder.encoder.opaque())?;
334 // DO NOT WRITE ANYTHING TO THE ENCODER AFTER THIS POINT! The address
335 // of the footer must be the last thing in the data stream.
339 fn sorted_cnums_including_local_crate(tcx: TyCtxt<'_>) -> Vec<CrateNum> {
340 let mut cnums = vec![LOCAL_CRATE];
341 cnums.extend_from_slice(&tcx.crates()[..]);
342 cnums.sort_unstable();
343 // Just to be sure...
350 /// Loads a diagnostic emitted during the previous compilation session.
351 pub fn load_diagnostics(
354 dep_node_index: SerializedDepNodeIndex,
355 ) -> Vec<Diagnostic> {
356 let diagnostics: Option<EncodedDiagnostics> =
357 self.load_indexed(tcx, dep_node_index, &self.prev_diagnostics_index, "diagnostics");
359 diagnostics.unwrap_or_default()
362 /// Stores a diagnostic emitted during the current compilation session.
363 /// Anything stored like this will be available via `load_diagnostics` in
364 /// the next compilation session.
367 pub fn store_diagnostics(
369 dep_node_index: DepNodeIndex,
370 diagnostics: ThinVec<Diagnostic>,
372 let mut current_diagnostics = self.current_diagnostics.borrow_mut();
373 let prev = current_diagnostics.insert(dep_node_index, diagnostics.into());
374 debug_assert!(prev.is_none());
377 /// Returns the cached query result if there is something in the cache for
378 /// the given `SerializedDepNodeIndex`; otherwise returns `None`.
379 crate fn try_load_query_result<'tcx, T>(
382 dep_node_index: SerializedDepNodeIndex,
385 T: for<'a> Decodable<CacheDecoder<'a, 'tcx>>,
387 self.load_indexed(tcx, dep_node_index, &self.query_result_index, "query result")
390 /// Stores a diagnostic emitted during computation of an anonymous query.
391 /// Since many anonymous queries can share the same `DepNode`, we aggregate
392 /// them -- as opposed to regular queries where we assume that there is a
393 /// 1:1 relationship between query-key and `DepNode`.
396 pub fn store_diagnostics_for_anon_node(
398 dep_node_index: DepNodeIndex,
399 diagnostics: ThinVec<Diagnostic>,
401 let mut current_diagnostics = self.current_diagnostics.borrow_mut();
403 let x = current_diagnostics.entry(dep_node_index).or_insert(Vec::new());
405 x.extend(Into::<Vec<_>>::into(diagnostics));
408 fn load_indexed<'tcx, T>(
411 dep_node_index: SerializedDepNodeIndex,
412 index: &FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
413 debug_tag: &'static str,
416 T: for<'a> Decodable<CacheDecoder<'a, 'tcx>>,
418 let pos = index.get(&dep_node_index).cloned()?;
420 self.with_decoder(tcx, pos, |decoder| match decode_tagged(decoder, dep_node_index) {
422 Err(e) => bug!("could not decode cached {}: {}", debug_tag, e),
426 fn with_decoder<'a, 'tcx, T, F: FnOnce(&mut CacheDecoder<'sess, 'tcx>) -> T>(
429 pos: AbsoluteBytePos,
433 T: Decodable<CacheDecoder<'a, 'tcx>>,
436 self.cnum_map.get_or_init(|| Self::compute_cnum_map(tcx, &self.prev_cnums[..]));
438 let mut decoder = CacheDecoder {
440 opaque: opaque::Decoder::new(&self.serialized_data[..], pos.to_usize()),
441 source_map: self.source_map,
443 file_index_to_file: &self.file_index_to_file,
444 file_index_to_stable_id: &self.file_index_to_stable_id,
445 alloc_decoding_session: self.alloc_decoding_state.new_decoding_session(),
446 syntax_contexts: &self.syntax_contexts,
447 expn_data: &self.expn_data,
448 hygiene_context: &self.hygiene_context,
453 // This function builds mapping from previous-session-`CrateNum` to
454 // current-session-`CrateNum`. There might be `CrateNum`s from the previous
455 // `Session` that don't occur in the current one. For these, the mapping
459 prev_cnums: &[(u32, String, CrateDisambiguator)],
460 ) -> IndexVec<CrateNum, Option<CrateNum>> {
461 tcx.dep_graph.with_ignore(|| {
462 let current_cnums = tcx
463 .all_crate_nums(LOCAL_CRATE)
466 let crate_name = tcx.original_crate_name(cnum).to_string();
467 let crate_disambiguator = tcx.crate_disambiguator(cnum);
468 ((crate_name, crate_disambiguator), cnum)
470 .collect::<FxHashMap<_, _>>();
472 let map_size = prev_cnums.iter().map(|&(cnum, ..)| cnum).max().unwrap_or(0) + 1;
473 let mut map = IndexVec::from_elem_n(None, map_size as usize);
475 for &(prev_cnum, ref crate_name, crate_disambiguator) in prev_cnums {
476 let key = (crate_name.clone(), crate_disambiguator);
477 map[CrateNum::from_u32(prev_cnum)] = current_cnums.get(&key).cloned();
480 map[LOCAL_CRATE] = Some(LOCAL_CRATE);
486 //- DECODING -------------------------------------------------------------------
488 /// A decoder that can read from the incr. comp. cache. It is similar to the one
489 /// we use for crate metadata decoding in that it can rebase spans and eventually
490 /// will also handle things that contain `Ty` instances.
491 crate struct CacheDecoder<'a, 'tcx> {
493 opaque: opaque::Decoder<'a>,
494 source_map: &'a SourceMap,
495 cnum_map: &'a IndexVec<CrateNum, Option<CrateNum>>,
496 file_index_to_file: &'a Lock<FxHashMap<SourceFileIndex, Lrc<SourceFile>>>,
497 file_index_to_stable_id: &'a FxHashMap<SourceFileIndex, StableSourceFileId>,
498 alloc_decoding_session: AllocDecodingSession<'a>,
499 syntax_contexts: &'a FxHashMap<u32, AbsoluteBytePos>,
500 expn_data: &'a FxHashMap<u32, AbsoluteBytePos>,
501 hygiene_context: &'a HygieneDecodeContext,
504 impl<'a, 'tcx> CacheDecoder<'a, 'tcx> {
505 fn file_index_to_file(&self, index: SourceFileIndex) -> Lrc<SourceFile> {
507 ref file_index_to_file,
508 ref file_index_to_stable_id,
517 let stable_id = file_index_to_stable_id[&index];
519 .source_file_by_stable_id(stable_id)
520 .expect("failed to lookup `SourceFile` in new context")
526 trait DecoderWithPosition: Decoder {
527 fn position(&self) -> usize;
530 impl<'a> DecoderWithPosition for opaque::Decoder<'a> {
531 fn position(&self) -> usize {
536 impl<'a, 'tcx> DecoderWithPosition for CacheDecoder<'a, 'tcx> {
537 fn position(&self) -> usize {
538 self.opaque.position()
542 // Decodes something that was encoded with `encode_tagged()` and verify that the
543 // tag matches and the correct amount of bytes was read.
544 fn decode_tagged<D, T, V>(decoder: &mut D, expected_tag: T) -> Result<V, D::Error>
546 T: Decodable<D> + Eq + std::fmt::Debug,
548 D: DecoderWithPosition,
550 let start_pos = decoder.position();
552 let actual_tag = T::decode(decoder)?;
553 assert_eq!(actual_tag, expected_tag);
554 let value = V::decode(decoder)?;
555 let end_pos = decoder.position();
557 let expected_len: u64 = Decodable::decode(decoder)?;
558 assert_eq!((end_pos - start_pos) as u64, expected_len);
563 impl<'a, 'tcx> TyDecoder<'tcx> for CacheDecoder<'a, 'tcx> {
564 const CLEAR_CROSS_CRATE: bool = false;
567 fn tcx(&self) -> TyCtxt<'tcx> {
572 fn position(&self) -> usize {
573 self.opaque.position()
577 fn peek_byte(&self) -> u8 {
578 self.opaque.data[self.opaque.position()]
581 fn cached_ty_for_shorthand<F>(
585 ) -> Result<Ty<'tcx>, Self::Error>
587 F: FnOnce(&mut Self) -> Result<Ty<'tcx>, Self::Error>,
589 let tcx = self.tcx();
592 ty::CReaderCacheKey { cnum: CrateNum::ReservedForIncrCompCache, pos: shorthand };
594 if let Some(&ty) = tcx.ty_rcache.borrow().get(&cache_key) {
598 let ty = or_insert_with(self)?;
599 // This may overwrite the entry, but it should overwrite with the same value.
600 tcx.ty_rcache.borrow_mut().insert_same(cache_key, ty);
604 fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
606 F: FnOnce(&mut Self) -> R,
608 debug_assert!(pos < self.opaque.data.len());
610 let new_opaque = opaque::Decoder::new(self.opaque.data, pos);
611 let old_opaque = mem::replace(&mut self.opaque, new_opaque);
613 self.opaque = old_opaque;
617 fn map_encoded_cnum_to_current(&self, cnum: CrateNum) -> CrateNum {
618 self.cnum_map[cnum].unwrap_or_else(|| bug!("could not find new `CrateNum` for {:?}", cnum))
621 fn decode_alloc_id(&mut self) -> Result<interpret::AllocId, Self::Error> {
622 let alloc_decoding_session = self.alloc_decoding_session;
623 alloc_decoding_session.decode_alloc_id(self)
627 crate::implement_ty_decoder!(CacheDecoder<'a, 'tcx>);
629 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for SyntaxContext {
630 fn decode(decoder: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
631 let syntax_contexts = decoder.syntax_contexts;
632 rustc_span::hygiene::decode_syntax_context(decoder, decoder.hygiene_context, |this, id| {
633 // This closure is invoked if we haven't already decoded the data for the `SyntaxContext` we are deserializing.
634 // We look up the position of the associated `SyntaxData` and decode it.
635 let pos = syntax_contexts.get(&id).unwrap();
636 this.with_position(pos.to_usize(), |decoder| {
637 let data: SyntaxContextData = decode_tagged(decoder, TAG_SYNTAX_CONTEXT)?;
644 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for ExpnId {
645 fn decode(decoder: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
646 let expn_data = decoder.expn_data;
647 rustc_span::hygiene::decode_expn_id(
649 ExpnDataDecodeMode::incr_comp(decoder.hygiene_context),
651 // This closure is invoked if we haven't already decoded the data for the `ExpnId` we are deserializing.
652 // We look up the position of the associated `ExpnData` and decode it.
655 .unwrap_or_else(|| panic!("Bad index {:?} (map {:?})", index, expn_data));
657 this.with_position(pos.to_usize(), |decoder| {
658 let data: ExpnData = decode_tagged(decoder, TAG_EXPN_DATA)?;
666 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Span {
667 fn decode(decoder: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
668 let tag: u8 = Decodable::decode(decoder)?;
670 if tag == TAG_INVALID_SPAN {
673 debug_assert_eq!(tag, TAG_VALID_SPAN);
676 let file_lo_index = SourceFileIndex::decode(decoder)?;
677 let line_lo = usize::decode(decoder)?;
678 let col_lo = BytePos::decode(decoder)?;
679 let len = BytePos::decode(decoder)?;
680 let ctxt = SyntaxContext::decode(decoder)?;
682 let file_lo = decoder.file_index_to_file(file_lo_index);
683 let lo = file_lo.lines[line_lo - 1] + col_lo;
686 Ok(Span::new(lo, hi, ctxt))
690 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for CrateNum {
691 fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
692 let cnum = CrateNum::from_u32(u32::decode(d)?);
693 Ok(d.map_encoded_cnum_to_current(cnum))
697 // This impl makes sure that we get a runtime error when we try decode a
698 // `DefIndex` that is not contained in a `DefId`. Such a case would be problematic
699 // because we would not know how to transform the `DefIndex` to the current
701 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for DefIndex {
702 fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<DefIndex, String> {
703 Err(d.error("trying to decode `DefIndex` outside the context of a `DefId`"))
707 // Both the `CrateNum` and the `DefIndex` of a `DefId` can change in between two
708 // compilation sessions. We use the `DefPathHash`, which is stable across
709 // sessions, to map the old `DefId` to the new one.
710 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for DefId {
711 fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
712 // Load the `DefPathHash` which is was we encoded the `DefId` as.
713 let def_path_hash = DefPathHash::decode(d)?;
715 // Using the `DefPathHash`, we can lookup the new `DefId`.
716 Ok(d.tcx().def_path_hash_to_def_id.as_ref().unwrap()[&def_path_hash])
720 impl<'a, 'tcx> FingerprintDecoder for CacheDecoder<'a, 'tcx> {
721 fn decode_fingerprint(&mut self) -> Result<Fingerprint, Self::Error> {
722 Fingerprint::decode_opaque(&mut self.opaque)
726 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx FxHashSet<LocalDefId> {
727 fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
728 RefDecodable::decode(d)
732 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>>
733 for &'tcx IndexVec<mir::Promoted, mir::Body<'tcx>>
735 fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
736 RefDecodable::decode(d)
740 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [mir::abstract_const::Node<'tcx>] {
741 fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
742 RefDecodable::decode(d)
746 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [(ty::Predicate<'tcx>, Span)] {
747 fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
748 RefDecodable::decode(d)
752 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [rustc_ast::InlineAsmTemplatePiece] {
753 fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
754 RefDecodable::decode(d)
758 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [Span] {
759 fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
760 RefDecodable::decode(d)
764 //- ENCODING -------------------------------------------------------------------
766 /// An encoder that can write the incr. comp. cache.
767 struct CacheEncoder<'a, 'tcx, E: OpaqueEncoder> {
770 type_shorthands: FxHashMap<Ty<'tcx>, usize>,
771 predicate_shorthands: FxHashMap<ty::Predicate<'tcx>, usize>,
772 interpret_allocs: FxIndexSet<interpret::AllocId>,
773 source_map: CachingSourceMapView<'tcx>,
774 file_to_file_index: FxHashMap<*const SourceFile, SourceFileIndex>,
775 hygiene_context: &'a HygieneEncodeContext,
778 impl<'a, 'tcx, E> CacheEncoder<'a, 'tcx, E>
780 E: 'a + OpaqueEncoder,
782 fn source_file_index(&mut self, source_file: Lrc<SourceFile>) -> SourceFileIndex {
783 self.file_to_file_index[&(&*source_file as *const SourceFile)]
786 /// Encode something with additional information that allows to do some
787 /// sanity checks when decoding the data again. This method will first
788 /// encode the specified tag, then the given value, then the number of
789 /// bytes taken up by tag and value. On decoding, we can then verify that
790 /// we get the expected tag and read the expected number of bytes.
791 fn encode_tagged<T: Encodable<Self>, V: Encodable<Self>>(
795 ) -> Result<(), E::Error> {
796 let start_pos = self.position();
801 let end_pos = self.position();
802 ((end_pos - start_pos) as u64).encode(self)
806 impl<'a, 'tcx> FingerprintEncoder for CacheEncoder<'a, 'tcx, rustc_serialize::opaque::Encoder> {
807 fn encode_fingerprint(&mut self, f: &Fingerprint) -> opaque::EncodeResult {
808 f.encode_opaque(self.encoder)
812 impl<'a, 'tcx, E> Encodable<CacheEncoder<'a, 'tcx, E>> for SyntaxContext
814 E: 'a + OpaqueEncoder,
816 fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) -> Result<(), E::Error> {
817 rustc_span::hygiene::raw_encode_syntax_context(*self, s.hygiene_context, s)
821 impl<'a, 'tcx, E> Encodable<CacheEncoder<'a, 'tcx, E>> for ExpnId
823 E: 'a + OpaqueEncoder,
825 fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) -> Result<(), E::Error> {
826 rustc_span::hygiene::raw_encode_expn_id(
829 ExpnDataEncodeMode::IncrComp,
835 impl<'a, 'tcx, E> Encodable<CacheEncoder<'a, 'tcx, E>> for Span
837 E: 'a + OpaqueEncoder,
839 fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) -> Result<(), E::Error> {
840 if *self == DUMMY_SP {
841 return TAG_INVALID_SPAN.encode(s);
844 let span_data = self.data();
845 let (file_lo, line_lo, col_lo) = match s.source_map.byte_pos_to_line_and_col(span_data.lo) {
847 None => return TAG_INVALID_SPAN.encode(s),
850 if !file_lo.contains(span_data.hi) {
851 return TAG_INVALID_SPAN.encode(s);
854 let len = span_data.hi - span_data.lo;
856 let source_file_index = s.source_file_index(file_lo);
858 TAG_VALID_SPAN.encode(s)?;
859 source_file_index.encode(s)?;
863 span_data.ctxt.encode(s)
867 impl<'a, 'tcx, E> TyEncoder<'tcx> for CacheEncoder<'a, 'tcx, E>
869 E: 'a + OpaqueEncoder,
871 const CLEAR_CROSS_CRATE: bool = false;
873 fn tcx(&self) -> TyCtxt<'tcx> {
876 fn position(&self) -> usize {
877 self.encoder.encoder_position()
879 fn type_shorthands(&mut self) -> &mut FxHashMap<Ty<'tcx>, usize> {
880 &mut self.type_shorthands
882 fn predicate_shorthands(&mut self) -> &mut FxHashMap<ty::Predicate<'tcx>, usize> {
883 &mut self.predicate_shorthands
885 fn encode_alloc_id(&mut self, alloc_id: &interpret::AllocId) -> Result<(), Self::Error> {
886 let (index, _) = self.interpret_allocs.insert_full(*alloc_id);
892 impl<'a, 'tcx, E> Encodable<CacheEncoder<'a, 'tcx, E>> for DefId
894 E: 'a + OpaqueEncoder,
896 fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) -> Result<(), E::Error> {
897 let def_path_hash = s.tcx.def_path_hash(*self);
898 def_path_hash.encode(s)
902 impl<'a, 'tcx, E> Encodable<CacheEncoder<'a, 'tcx, E>> for DefIndex
904 E: 'a + OpaqueEncoder,
906 fn encode(&self, _: &mut CacheEncoder<'a, 'tcx, E>) -> Result<(), E::Error> {
907 bug!("encoding `DefIndex` without context");
911 macro_rules! encoder_methods {
912 ($($name:ident($ty:ty);)*) => {
914 $(fn $name(&mut self, value: $ty) -> Result<(), Self::Error> {
915 self.encoder.$name(value)
920 impl<'a, 'tcx, E> Encoder for CacheEncoder<'a, 'tcx, E>
922 E: 'a + OpaqueEncoder,
924 type Error = E::Error;
927 fn emit_unit(&mut self) -> Result<(), Self::Error> {
954 // An integer that will always encode to 8 bytes.
955 struct IntEncodedWithFixedSize(u64);
957 impl IntEncodedWithFixedSize {
958 pub const ENCODED_SIZE: usize = 8;
961 impl Encodable<opaque::Encoder> for IntEncodedWithFixedSize {
962 fn encode(&self, e: &mut opaque::Encoder) -> Result<(), !> {
963 let start_pos = e.position();
964 for i in 0..IntEncodedWithFixedSize::ENCODED_SIZE {
965 ((self.0 >> (i * 8)) as u8).encode(e)?;
967 let end_pos = e.position();
968 assert_eq!((end_pos - start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
973 impl<'a> Decodable<opaque::Decoder<'a>> for IntEncodedWithFixedSize {
974 fn decode(decoder: &mut opaque::Decoder<'a>) -> Result<IntEncodedWithFixedSize, String> {
975 let mut value: u64 = 0;
976 let start_pos = decoder.position();
978 for i in 0..IntEncodedWithFixedSize::ENCODED_SIZE {
979 let byte: u8 = Decodable::decode(decoder)?;
980 value |= (byte as u64) << (i * 8);
983 let end_pos = decoder.position();
984 assert_eq!((end_pos - start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
986 Ok(IntEncodedWithFixedSize(value))
990 fn encode_query_results<'a, 'tcx, Q, E>(
992 encoder: &mut CacheEncoder<'a, 'tcx, E>,
993 query_result_index: &mut EncodedQueryResultIndex,
994 ) -> Result<(), E::Error>
996 Q: super::QueryDescription<TyCtxt<'tcx>> + super::QueryAccessors<TyCtxt<'tcx>>,
997 Q::Value: Encodable<CacheEncoder<'a, 'tcx, E>>,
998 E: 'a + OpaqueEncoder,
1003 .extra_verbose_generic_activity("encode_query_results_for", std::any::type_name::<Q>());
1005 let state = Q::query_state(tcx);
1006 assert!(state.all_inactive());
1008 state.iter_results(|results| {
1009 for (key, value, dep_node) in results {
1010 if Q::cache_on_disk(tcx, &key, Some(value)) {
1011 let dep_node = SerializedDepNodeIndex::new(dep_node.index());
1013 // Record position of the cache entry.
1015 .push((dep_node, AbsoluteBytePos::new(encoder.encoder.opaque().position())));
1017 // Encode the type check tables with the `SerializedDepNodeIndex`
1019 encoder.encode_tagged(dep_node, value)?;