]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/ty/query/on_disk_cache.rs
Encode ExpnId using ExpnHash for incr. comp.
[rust.git] / compiler / rustc_middle / src / ty / query / on_disk_cache.rs
1 use crate::dep_graph::{DepNode, DepNodeIndex, SerializedDepNodeIndex};
2 use crate::mir::interpret::{AllocDecodingSession, AllocDecodingState};
3 use crate::mir::{self, interpret};
4 use crate::ty::codec::{RefDecodable, TyDecoder, TyEncoder};
5 use crate::ty::context::TyCtxt;
6 use crate::ty::{self, Ty};
7 use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
8 use rustc_data_structures::sync::{HashMapExt, Lock, Lrc, OnceCell};
9 use rustc_data_structures::thin_vec::ThinVec;
10 use rustc_data_structures::unhash::UnhashMap;
11 use rustc_errors::Diagnostic;
12 use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, StableCrateId, LOCAL_CRATE};
13 use rustc_hir::definitions::DefPathHash;
14 use rustc_index::vec::{Idx, IndexVec};
15 use rustc_query_system::dep_graph::DepContext;
16 use rustc_query_system::query::QueryContext;
17 use rustc_serialize::{
18     opaque::{self, FileEncodeResult, FileEncoder, IntEncodedWithFixedSize},
19     Decodable, Decoder, Encodable, Encoder,
20 };
21 use rustc_session::Session;
22 use rustc_span::hygiene::{
23     ExpnId, HygieneDecodeContext, HygieneEncodeContext, SyntaxContext, SyntaxContextData,
24 };
25 use rustc_span::source_map::{SourceMap, StableSourceFileId};
26 use rustc_span::CachingSourceMapView;
27 use rustc_span::{BytePos, ExpnData, ExpnHash, SourceFile, Span, DUMMY_SP};
28 use std::collections::hash_map::Entry;
29 use std::mem;
30
31 const TAG_FILE_FOOTER: u128 = 0xC0FFEE_C0FFEE_C0FFEE_C0FFEE_C0FFEE;
32
33 // A normal span encoded with both location information and a `SyntaxContext`
34 const TAG_FULL_SPAN: u8 = 0;
35 // A partial span with no location information, encoded only with a `SyntaxContext`
36 const TAG_PARTIAL_SPAN: u8 = 1;
37
38 const TAG_SYNTAX_CONTEXT: u8 = 0;
39 const TAG_EXPN_DATA: u8 = 1;
40
41 /// Provides an interface to incremental compilation data cached from the
42 /// previous compilation session. This data will eventually include the results
43 /// of a few selected queries (like `typeck` and `mir_optimized`) and
44 /// any diagnostics that have been emitted during a query.
45 pub struct OnDiskCache<'sess> {
46     // The complete cache data in serialized form.
47     serialized_data: Vec<u8>,
48
49     // Collects all `Diagnostic`s emitted during the current compilation
50     // session.
51     current_diagnostics: Lock<FxHashMap<DepNodeIndex, Vec<Diagnostic>>>,
52
53     cnum_map: OnceCell<UnhashMap<StableCrateId, CrateNum>>,
54
55     source_map: &'sess SourceMap,
56     file_index_to_stable_id: FxHashMap<SourceFileIndex, EncodedSourceFileId>,
57
58     // Caches that are populated lazily during decoding.
59     file_index_to_file: Lock<FxHashMap<SourceFileIndex, Lrc<SourceFile>>>,
60
61     // A map from dep-node to the position of the cached query result in
62     // `serialized_data`.
63     query_result_index: FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
64
65     // A map from dep-node to the position of any associated diagnostics in
66     // `serialized_data`.
67     prev_diagnostics_index: FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
68
69     alloc_decoding_state: AllocDecodingState,
70
71     // A map from syntax context ids to the position of their associated
72     // `SyntaxContextData`. We use a `u32` instead of a `SyntaxContext`
73     // to represent the fact that we are storing *encoded* ids. When we decode
74     // a `SyntaxContext`, a new id will be allocated from the global `HygieneData`,
75     // which will almost certainly be different than the serialized id.
76     syntax_contexts: FxHashMap<u32, AbsoluteBytePos>,
77     // A map from the `DefPathHash` of an `ExpnId` to the position
78     // of their associated `ExpnData`. Ideally, we would store a `DefId`,
79     // but we need to decode this before we've constructed a `TyCtxt` (which
80     // makes it difficult to decode a `DefId`).
81
82     // Note that these `DefPathHashes` correspond to both local and foreign
83     // `ExpnData` (e.g `ExpnData.krate` may not be `LOCAL_CRATE`). Alternatively,
84     // we could look up the `ExpnData` from the metadata of foreign crates,
85     // but it seemed easier to have `OnDiskCache` be independent of the `CStore`.
86     expn_data: UnhashMap<ExpnHash, AbsoluteBytePos>,
87     // Additional information used when decoding hygiene data.
88     hygiene_context: HygieneDecodeContext,
89     // Maps `DefPathHash`es to their `RawDefId`s from the *previous*
90     // compilation session. This is used as an initial 'guess' when
91     // we try to map a `DefPathHash` to its `DefId` in the current compilation
92     // session.
93     foreign_def_path_hashes: UnhashMap<DefPathHash, RawDefId>,
94     // Likewise for ExpnId.
95     foreign_expn_data: UnhashMap<ExpnHash, u32>,
96
97     // The *next* compilation sessison's `foreign_def_path_hashes` - at
98     // the end of our current compilation session, this will get written
99     // out to the `foreign_def_path_hashes` field of the `Footer`, which
100     // will become `foreign_def_path_hashes` of the next compilation session.
101     // This stores any `DefPathHash` that we may need to map to a `DefId`
102     // during the next compilation session.
103     latest_foreign_def_path_hashes: Lock<UnhashMap<DefPathHash, RawDefId>>,
104
105     // Caches all lookups of `DefPathHashes`, both for local and foreign
106     // definitions. A definition from the previous compilation session
107     // may no longer exist in the current compilation session, so
108     // we use `Option<DefId>` so that we can cache a lookup failure.
109     def_path_hash_to_def_id_cache: Lock<UnhashMap<DefPathHash, Option<DefId>>>,
110 }
111
112 // This type is used only for serialization and deserialization.
113 #[derive(Encodable, Decodable)]
114 struct Footer {
115     file_index_to_stable_id: FxHashMap<SourceFileIndex, EncodedSourceFileId>,
116     query_result_index: EncodedQueryResultIndex,
117     diagnostics_index: EncodedQueryResultIndex,
118     // The location of all allocations.
119     interpret_alloc_index: Vec<u32>,
120     // See `OnDiskCache.syntax_contexts`
121     syntax_contexts: FxHashMap<u32, AbsoluteBytePos>,
122     // See `OnDiskCache.expn_data`
123     expn_data: UnhashMap<ExpnHash, AbsoluteBytePos>,
124     foreign_def_path_hashes: UnhashMap<DefPathHash, RawDefId>,
125     foreign_expn_data: UnhashMap<ExpnHash, u32>,
126 }
127
128 pub type EncodedQueryResultIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
129 type EncodedDiagnosticsIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
130 type EncodedDiagnostics = Vec<Diagnostic>;
131
132 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Encodable, Decodable)]
133 struct SourceFileIndex(u32);
134
135 #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Encodable, Decodable)]
136 pub struct AbsoluteBytePos(u32);
137
138 impl AbsoluteBytePos {
139     fn new(pos: usize) -> AbsoluteBytePos {
140         debug_assert!(pos <= u32::MAX as usize);
141         AbsoluteBytePos(pos as u32)
142     }
143
144     fn to_usize(self) -> usize {
145         self.0 as usize
146     }
147 }
148
149 /// Represents a potentially invalid `DefId`. This is used during incremental
150 /// compilation to represent a `DefId` from the *previous* compilation session,
151 /// which may no longer be valid. This is used to help map a `DefPathHash`
152 /// to a `DefId` in the current compilation session.
153 #[derive(Encodable, Decodable, Copy, Clone, Debug)]
154 crate struct RawDefId {
155     // We deliberately do not use `CrateNum` and `DefIndex`
156     // here, since a crate/index from the previous compilation
157     // session may no longer exist.
158     pub krate: u32,
159     pub index: u32,
160 }
161
162 /// An `EncodedSourceFileId` is the same as a `StableSourceFileId` except that
163 /// the source crate is represented as a [StableCrateId] instead of as a
164 /// `CrateNum`. This way `EncodedSourceFileId` can be encoded and decoded
165 /// without any additional context, i.e. with a simple `opaque::Decoder` (which
166 /// is the only thing available when decoding the cache's [Footer].
167 #[derive(Encodable, Decodable, Clone, Debug)]
168 struct EncodedSourceFileId {
169     file_name_hash: u64,
170     stable_crate_id: StableCrateId,
171 }
172
173 impl EncodedSourceFileId {
174     fn translate(&self, cnum_map: &UnhashMap<StableCrateId, CrateNum>) -> StableSourceFileId {
175         let cnum = cnum_map[&self.stable_crate_id];
176         StableSourceFileId { file_name_hash: self.file_name_hash, cnum }
177     }
178
179     fn new(tcx: TyCtxt<'_>, file: &SourceFile) -> EncodedSourceFileId {
180         let source_file_id = StableSourceFileId::new(file);
181         EncodedSourceFileId {
182             file_name_hash: source_file_id.file_name_hash,
183             stable_crate_id: tcx.stable_crate_id(source_file_id.cnum),
184         }
185     }
186 }
187
188 impl<'sess> OnDiskCache<'sess> {
189     /// Creates a new `OnDiskCache` instance from the serialized data in `data`.
190     pub fn new(sess: &'sess Session, data: Vec<u8>, start_pos: usize) -> Self {
191         debug_assert!(sess.opts.incremental.is_some());
192
193         // Wrap in a scope so we can borrow `data`.
194         let footer: Footer = {
195             let mut decoder = opaque::Decoder::new(&data[..], start_pos);
196
197             // Decode the *position* of the footer, which can be found in the
198             // last 8 bytes of the file.
199             decoder.set_position(data.len() - IntEncodedWithFixedSize::ENCODED_SIZE);
200             let footer_pos = IntEncodedWithFixedSize::decode(&mut decoder)
201                 .expect("error while trying to decode footer position")
202                 .0 as usize;
203
204             // Decode the file footer, which contains all the lookup tables, etc.
205             decoder.set_position(footer_pos);
206
207             decode_tagged(&mut decoder, TAG_FILE_FOOTER)
208                 .expect("error while trying to decode footer position")
209         };
210
211         Self {
212             serialized_data: data,
213             file_index_to_stable_id: footer.file_index_to_stable_id,
214             file_index_to_file: Default::default(),
215             cnum_map: OnceCell::new(),
216             source_map: sess.source_map(),
217             current_diagnostics: Default::default(),
218             query_result_index: footer.query_result_index.into_iter().collect(),
219             prev_diagnostics_index: footer.diagnostics_index.into_iter().collect(),
220             alloc_decoding_state: AllocDecodingState::new(footer.interpret_alloc_index),
221             syntax_contexts: footer.syntax_contexts,
222             expn_data: footer.expn_data,
223             foreign_expn_data: footer.foreign_expn_data,
224             hygiene_context: Default::default(),
225             foreign_def_path_hashes: footer.foreign_def_path_hashes,
226             latest_foreign_def_path_hashes: Default::default(),
227             def_path_hash_to_def_id_cache: Default::default(),
228         }
229     }
230
231     pub fn new_empty(source_map: &'sess SourceMap) -> Self {
232         Self {
233             serialized_data: Vec::new(),
234             file_index_to_stable_id: Default::default(),
235             file_index_to_file: Default::default(),
236             cnum_map: OnceCell::new(),
237             source_map,
238             current_diagnostics: Default::default(),
239             query_result_index: Default::default(),
240             prev_diagnostics_index: Default::default(),
241             alloc_decoding_state: AllocDecodingState::new(Vec::new()),
242             syntax_contexts: FxHashMap::default(),
243             expn_data: UnhashMap::default(),
244             foreign_expn_data: UnhashMap::default(),
245             hygiene_context: Default::default(),
246             foreign_def_path_hashes: Default::default(),
247             latest_foreign_def_path_hashes: Default::default(),
248             def_path_hash_to_def_id_cache: Default::default(),
249         }
250     }
251
252     pub fn serialize<'tcx>(
253         &self,
254         tcx: TyCtxt<'tcx>,
255         encoder: &mut FileEncoder,
256     ) -> FileEncodeResult {
257         // Serializing the `DepGraph` should not modify it.
258         tcx.dep_graph.with_ignore(|| {
259             // Allocate `SourceFileIndex`es.
260             let (file_to_file_index, file_index_to_stable_id) = {
261                 let files = tcx.sess.source_map().files();
262                 let mut file_to_file_index =
263                     FxHashMap::with_capacity_and_hasher(files.len(), Default::default());
264                 let mut file_index_to_stable_id =
265                     FxHashMap::with_capacity_and_hasher(files.len(), Default::default());
266
267                 for (index, file) in files.iter().enumerate() {
268                     let index = SourceFileIndex(index as u32);
269                     let file_ptr: *const SourceFile = &**file as *const _;
270                     file_to_file_index.insert(file_ptr, index);
271                     let source_file_id = EncodedSourceFileId::new(tcx, &file);
272                     file_index_to_stable_id.insert(index, source_file_id);
273                 }
274
275                 (file_to_file_index, file_index_to_stable_id)
276             };
277
278             // Register any dep nodes that we reused from the previous session,
279             // but didn't `DepNode::construct` in this session. This ensures
280             // that their `DefPathHash` to `RawDefId` mappings are registered
281             // in 'latest_foreign_def_path_hashes' if necessary, since that
282             // normally happens in `DepNode::construct`.
283             tcx.dep_graph.register_reused_dep_nodes(tcx);
284
285             // Load everything into memory so we can write it out to the on-disk
286             // cache. The vast majority of cacheable query results should already
287             // be in memory, so this should be a cheap operation.
288             // Do this *before* we clone 'latest_foreign_def_path_hashes', since
289             // loading existing queries may cause us to create new DepNodes, which
290             // may in turn end up invoking `store_foreign_def_id_hash`
291             tcx.queries.exec_cache_promotions(tcx);
292
293             let latest_foreign_def_path_hashes = self.latest_foreign_def_path_hashes.lock().clone();
294             let hygiene_encode_context = HygieneEncodeContext::default();
295
296             let mut encoder = CacheEncoder {
297                 tcx,
298                 encoder,
299                 type_shorthands: Default::default(),
300                 predicate_shorthands: Default::default(),
301                 interpret_allocs: Default::default(),
302                 source_map: CachingSourceMapView::new(tcx.sess.source_map()),
303                 file_to_file_index,
304                 hygiene_context: &hygiene_encode_context,
305                 latest_foreign_def_path_hashes,
306             };
307
308             // Encode query results.
309             let mut query_result_index = EncodedQueryResultIndex::new();
310
311             tcx.sess.time("encode_query_results", || -> FileEncodeResult {
312                 let enc = &mut encoder;
313                 let qri = &mut query_result_index;
314                 tcx.queries.encode_query_results(tcx, enc, qri)
315             })?;
316
317             // Encode diagnostics.
318             let diagnostics_index: EncodedDiagnosticsIndex = self
319                 .current_diagnostics
320                 .borrow()
321                 .iter()
322                 .map(
323                     |(dep_node_index, diagnostics)| -> Result<_, <FileEncoder as Encoder>::Error> {
324                         let pos = AbsoluteBytePos::new(encoder.position());
325                         // Let's make sure we get the expected type here.
326                         let diagnostics: &EncodedDiagnostics = diagnostics;
327                         let dep_node_index = SerializedDepNodeIndex::new(dep_node_index.index());
328                         encoder.encode_tagged(dep_node_index, diagnostics)?;
329
330                         Ok((dep_node_index, pos))
331                     },
332                 )
333                 .collect::<Result<_, _>>()?;
334
335             let interpret_alloc_index = {
336                 let mut interpret_alloc_index = Vec::new();
337                 let mut n = 0;
338                 loop {
339                     let new_n = encoder.interpret_allocs.len();
340                     // If we have found new IDs, serialize those too.
341                     if n == new_n {
342                         // Otherwise, abort.
343                         break;
344                     }
345                     interpret_alloc_index.reserve(new_n - n);
346                     for idx in n..new_n {
347                         let id = encoder.interpret_allocs[idx];
348                         let pos = encoder.position() as u32;
349                         interpret_alloc_index.push(pos);
350                         interpret::specialized_encode_alloc_id(&mut encoder, tcx, id)?;
351                     }
352                     n = new_n;
353                 }
354                 interpret_alloc_index
355             };
356
357             let mut syntax_contexts = FxHashMap::default();
358             let mut expn_data = UnhashMap::default();
359             let mut foreign_expn_data = UnhashMap::default();
360
361             // Encode all hygiene data (`SyntaxContextData` and `ExpnData`) from the current
362             // session.
363
364             hygiene_encode_context.encode(
365                 &mut encoder,
366                 |encoder, index, ctxt_data| -> FileEncodeResult {
367                     let pos = AbsoluteBytePos::new(encoder.position());
368                     encoder.encode_tagged(TAG_SYNTAX_CONTEXT, ctxt_data)?;
369                     syntax_contexts.insert(index, pos);
370                     Ok(())
371                 },
372                 |encoder, expn_id, data, hash| -> FileEncodeResult {
373                     if expn_id.krate == LOCAL_CRATE {
374                         let pos = AbsoluteBytePos::new(encoder.position());
375                         encoder.encode_tagged(TAG_EXPN_DATA, &data)?;
376                         expn_data.insert(hash, pos);
377                     } else {
378                         foreign_expn_data.insert(hash, expn_id.local_id.as_u32());
379                     }
380                     Ok(())
381                 },
382             )?;
383
384             let foreign_def_path_hashes =
385                 std::mem::take(&mut encoder.latest_foreign_def_path_hashes);
386
387             // `Encode the file footer.
388             let footer_pos = encoder.position() as u64;
389             encoder.encode_tagged(
390                 TAG_FILE_FOOTER,
391                 &Footer {
392                     file_index_to_stable_id,
393                     query_result_index,
394                     diagnostics_index,
395                     interpret_alloc_index,
396                     syntax_contexts,
397                     expn_data,
398                     foreign_expn_data,
399                     foreign_def_path_hashes,
400                 },
401             )?;
402
403             // Encode the position of the footer as the last 8 bytes of the
404             // file so we know where to look for it.
405             IntEncodedWithFixedSize(footer_pos).encode(encoder.encoder)?;
406
407             // DO NOT WRITE ANYTHING TO THE ENCODER AFTER THIS POINT! The address
408             // of the footer must be the last thing in the data stream.
409
410             Ok(())
411         })
412     }
413
414     /// Loads a diagnostic emitted during the previous compilation session.
415     pub fn load_diagnostics(
416         &self,
417         tcx: TyCtxt<'_>,
418         dep_node_index: SerializedDepNodeIndex,
419     ) -> Vec<Diagnostic> {
420         let diagnostics: Option<EncodedDiagnostics> =
421             self.load_indexed(tcx, dep_node_index, &self.prev_diagnostics_index, "diagnostics");
422
423         diagnostics.unwrap_or_default()
424     }
425
426     /// Stores a diagnostic emitted during the current compilation session.
427     /// Anything stored like this will be available via `load_diagnostics` in
428     /// the next compilation session.
429     #[inline(never)]
430     #[cold]
431     pub fn store_diagnostics(
432         &self,
433         dep_node_index: DepNodeIndex,
434         diagnostics: ThinVec<Diagnostic>,
435     ) {
436         let mut current_diagnostics = self.current_diagnostics.borrow_mut();
437         let prev = current_diagnostics.insert(dep_node_index, diagnostics.into());
438         debug_assert!(prev.is_none());
439     }
440
441     fn get_raw_def_id(&self, hash: &DefPathHash) -> Option<RawDefId> {
442         self.foreign_def_path_hashes.get(hash).copied()
443     }
444
445     fn try_remap_cnum(&self, tcx: TyCtxt<'_>, stable_crate_id: StableCrateId) -> Option<CrateNum> {
446         let cnum_map = self.cnum_map.get_or_init(|| Self::compute_cnum_map(tcx));
447         debug!("try_remap_cnum({:?}): cnum_map={:?}", stable_crate_id, cnum_map);
448
449         cnum_map.get(&stable_crate_id).copied()
450     }
451
452     pub(crate) fn store_foreign_def_id_hash(&self, def_id: DefId, hash: DefPathHash) {
453         // We may overwrite an existing entry, but it will have the same value,
454         // so it's fine
455         self.latest_foreign_def_path_hashes
456             .lock()
457             .insert(hash, RawDefId { krate: def_id.krate.as_u32(), index: def_id.index.as_u32() });
458     }
459
460     /// If the given `dep_node`'s hash still exists in the current compilation,
461     /// and its current `DefId` is foreign, calls `store_foreign_def_id` with it.
462     ///
463     /// Normally, `store_foreign_def_id_hash` can be called directly by
464     /// the dependency graph when we construct a `DepNode`. However,
465     /// when we re-use a deserialized `DepNode` from the previous compilation
466     /// session, we only have the `DefPathHash` available. This method is used
467     /// to that any `DepNode` that we re-use has a `DefPathHash` -> `RawId` written
468     /// out for usage in the next compilation session.
469     pub fn register_reused_dep_node(&self, tcx: TyCtxt<'tcx>, dep_node: &DepNode) {
470         // For reused dep nodes, we only need to store the mapping if the node
471         // is one whose query key we can reconstruct from the hash. We use the
472         // mapping to aid that reconstruction in the next session. While we also
473         // use it to decode `DefId`s we encoded in the cache as `DefPathHashes`,
474         // they're already registered during `DefId` encoding.
475         if dep_node.kind.can_reconstruct_query_key() {
476             let hash = DefPathHash(dep_node.hash.into());
477
478             // We can't simply copy the `RawDefId` from `foreign_def_path_hashes` to
479             // `latest_foreign_def_path_hashes`, since the `RawDefId` might have
480             // changed in the current compilation session (e.g. we've added/removed crates,
481             // or added/removed definitions before/after the target definition).
482             if let Some(def_id) = self.def_path_hash_to_def_id(tcx, hash) {
483                 if !def_id.is_local() {
484                     self.store_foreign_def_id_hash(def_id, hash);
485                 }
486             }
487         }
488     }
489
490     /// Returns the cached query result if there is something in the cache for
491     /// the given `SerializedDepNodeIndex`; otherwise returns `None`.
492     pub fn try_load_query_result<'tcx, T>(
493         &self,
494         tcx: TyCtxt<'tcx>,
495         dep_node_index: SerializedDepNodeIndex,
496     ) -> Option<T>
497     where
498         T: for<'a> Decodable<CacheDecoder<'a, 'tcx>>,
499     {
500         self.load_indexed(tcx, dep_node_index, &self.query_result_index, "query result")
501     }
502
503     /// Stores a diagnostic emitted during computation of an anonymous query.
504     /// Since many anonymous queries can share the same `DepNode`, we aggregate
505     /// them -- as opposed to regular queries where we assume that there is a
506     /// 1:1 relationship between query-key and `DepNode`.
507     #[inline(never)]
508     #[cold]
509     pub fn store_diagnostics_for_anon_node(
510         &self,
511         dep_node_index: DepNodeIndex,
512         diagnostics: ThinVec<Diagnostic>,
513     ) {
514         let mut current_diagnostics = self.current_diagnostics.borrow_mut();
515
516         let x = current_diagnostics.entry(dep_node_index).or_default();
517
518         x.extend(Into::<Vec<_>>::into(diagnostics));
519     }
520
521     fn load_indexed<'tcx, T>(
522         &self,
523         tcx: TyCtxt<'tcx>,
524         dep_node_index: SerializedDepNodeIndex,
525         index: &FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
526         debug_tag: &'static str,
527     ) -> Option<T>
528     where
529         T: for<'a> Decodable<CacheDecoder<'a, 'tcx>>,
530     {
531         let pos = index.get(&dep_node_index).cloned()?;
532
533         self.with_decoder(tcx, pos, |decoder| match decode_tagged(decoder, dep_node_index) {
534             Ok(v) => Some(v),
535             Err(e) => bug!("could not decode cached {}: {}", debug_tag, e),
536         })
537     }
538
539     fn with_decoder<'a, 'tcx, T, F: FnOnce(&mut CacheDecoder<'sess, 'tcx>) -> T>(
540         &'sess self,
541         tcx: TyCtxt<'tcx>,
542         pos: AbsoluteBytePos,
543         f: F,
544     ) -> T
545     where
546         T: Decodable<CacheDecoder<'a, 'tcx>>,
547     {
548         let cnum_map = self.cnum_map.get_or_init(|| Self::compute_cnum_map(tcx));
549
550         let mut decoder = CacheDecoder {
551             tcx,
552             opaque: opaque::Decoder::new(&self.serialized_data[..], pos.to_usize()),
553             source_map: self.source_map,
554             cnum_map,
555             file_index_to_file: &self.file_index_to_file,
556             file_index_to_stable_id: &self.file_index_to_stable_id,
557             alloc_decoding_session: self.alloc_decoding_state.new_decoding_session(),
558             syntax_contexts: &self.syntax_contexts,
559             expn_data: &self.expn_data,
560             foreign_expn_data: &self.foreign_expn_data,
561             hygiene_context: &self.hygiene_context,
562         };
563         f(&mut decoder)
564     }
565
566     // This function builds mapping from previous-session-`CrateNum` to
567     // current-session-`CrateNum`. There might be `CrateNum`s from the previous
568     // `Session` that don't occur in the current one. For these, the mapping
569     // maps to None.
570     fn compute_cnum_map(tcx: TyCtxt<'_>) -> UnhashMap<StableCrateId, CrateNum> {
571         tcx.dep_graph.with_ignore(|| {
572             tcx.crates(())
573                 .iter()
574                 .chain(std::iter::once(&LOCAL_CRATE))
575                 .map(|&cnum| {
576                     let hash = tcx.def_path_hash(cnum.as_def_id()).stable_crate_id();
577                     (hash, cnum)
578                 })
579                 .collect()
580         })
581     }
582
583     /// Converts a `DefPathHash` to its corresponding `DefId` in the current compilation
584     /// session, if it still exists. This is used during incremental compilation to
585     /// turn a deserialized `DefPathHash` into its current `DefId`.
586     pub(crate) fn def_path_hash_to_def_id(
587         &self,
588         tcx: TyCtxt<'tcx>,
589         hash: DefPathHash,
590     ) -> Option<DefId> {
591         let mut cache = self.def_path_hash_to_def_id_cache.lock();
592         match cache.entry(hash) {
593             Entry::Occupied(e) => *e.get(),
594             Entry::Vacant(e) => {
595                 debug!("def_path_hash_to_def_id({:?})", hash);
596                 // Check if the `DefPathHash` corresponds to a definition in the current
597                 // crate
598                 if let Some(def_id) =
599                     tcx.untracked_resolutions.definitions.local_def_path_hash_to_def_id(hash)
600                 {
601                     let def_id = def_id.to_def_id();
602                     e.insert(Some(def_id));
603                     return Some(def_id);
604                 }
605                 // This `raw_def_id` represents the `DefId` of this `DefPathHash` in
606                 // the *previous* compliation session. The `DefPathHash` includes the
607                 // owning crate, so if the corresponding definition still exists in the
608                 // current compilation session, the crate is guaranteed to be the same
609                 // (otherwise, we would compute a different `DefPathHash`).
610                 let raw_def_id = self.get_raw_def_id(&hash)?;
611                 debug!("def_path_hash_to_def_id({:?}): raw_def_id = {:?}", hash, raw_def_id);
612                 // If the owning crate no longer exists, the corresponding definition definitely
613                 // no longer exists.
614                 let krate = self.try_remap_cnum(tcx, hash.stable_crate_id())?;
615                 debug!("def_path_hash_to_def_id({:?}): krate = {:?}", hash, krate);
616                 // If our `DefPathHash` corresponded to a definition in the local crate,
617                 // we should have either found it in `local_def_path_hash_to_def_id`, or
618                 // never attempted to load it in the first place. Any query result or `DepNode`
619                 // that references a local `DefId` should depend on some HIR-related `DepNode`.
620                 // If a local definition is removed/modified such that its old `DefPathHash`
621                 // no longer has a corresponding definition, that HIR-related `DepNode` should
622                 // end up red. This should prevent us from ever calling
623                 // `tcx.def_path_hash_to_def_id`, since we'll end up recomputing any
624                 // queries involved.
625                 debug_assert_ne!(krate, LOCAL_CRATE);
626                 // Try to find a definition in the current session, using the previous `DefIndex`
627                 // as an initial guess.
628                 let opt_def_id = tcx.untracked_resolutions.cstore.def_path_hash_to_def_id(
629                     krate,
630                     raw_def_id.index,
631                     hash,
632                 );
633                 debug!("def_path_to_def_id({:?}): opt_def_id = {:?}", hash, opt_def_id);
634                 e.insert(opt_def_id);
635                 opt_def_id
636             }
637         }
638     }
639 }
640
641 //- DECODING -------------------------------------------------------------------
642
643 /// A decoder that can read from the incremental compilation cache. It is similar to the one
644 /// we use for crate metadata decoding in that it can rebase spans and eventually
645 /// will also handle things that contain `Ty` instances.
646 pub struct CacheDecoder<'a, 'tcx> {
647     tcx: TyCtxt<'tcx>,
648     opaque: opaque::Decoder<'a>,
649     source_map: &'a SourceMap,
650     cnum_map: &'a UnhashMap<StableCrateId, CrateNum>,
651     file_index_to_file: &'a Lock<FxHashMap<SourceFileIndex, Lrc<SourceFile>>>,
652     file_index_to_stable_id: &'a FxHashMap<SourceFileIndex, EncodedSourceFileId>,
653     alloc_decoding_session: AllocDecodingSession<'a>,
654     syntax_contexts: &'a FxHashMap<u32, AbsoluteBytePos>,
655     expn_data: &'a UnhashMap<ExpnHash, AbsoluteBytePos>,
656     foreign_expn_data: &'a UnhashMap<ExpnHash, u32>,
657     hygiene_context: &'a HygieneDecodeContext,
658 }
659
660 impl<'a, 'tcx> CacheDecoder<'a, 'tcx> {
661     fn file_index_to_file(&self, index: SourceFileIndex) -> Lrc<SourceFile> {
662         let CacheDecoder {
663             ref file_index_to_file,
664             ref file_index_to_stable_id,
665             ref source_map,
666             ref cnum_map,
667             ..
668         } = *self;
669
670         file_index_to_file
671             .borrow_mut()
672             .entry(index)
673             .or_insert_with(|| {
674                 let stable_id = file_index_to_stable_id[&index].translate(cnum_map);
675                 source_map
676                     .source_file_by_stable_id(stable_id)
677                     .expect("failed to lookup `SourceFile` in new context")
678             })
679             .clone()
680     }
681 }
682
683 trait DecoderWithPosition: Decoder {
684     fn position(&self) -> usize;
685 }
686
687 impl<'a> DecoderWithPosition for opaque::Decoder<'a> {
688     fn position(&self) -> usize {
689         self.position()
690     }
691 }
692
693 impl<'a, 'tcx> DecoderWithPosition for CacheDecoder<'a, 'tcx> {
694     fn position(&self) -> usize {
695         self.opaque.position()
696     }
697 }
698
699 // Decodes something that was encoded with `encode_tagged()` and verify that the
700 // tag matches and the correct amount of bytes was read.
701 fn decode_tagged<D, T, V>(decoder: &mut D, expected_tag: T) -> Result<V, D::Error>
702 where
703     T: Decodable<D> + Eq + std::fmt::Debug,
704     V: Decodable<D>,
705     D: DecoderWithPosition,
706 {
707     let start_pos = decoder.position();
708
709     let actual_tag = T::decode(decoder)?;
710     assert_eq!(actual_tag, expected_tag);
711     let value = V::decode(decoder)?;
712     let end_pos = decoder.position();
713
714     let expected_len: u64 = Decodable::decode(decoder)?;
715     assert_eq!((end_pos - start_pos) as u64, expected_len);
716
717     Ok(value)
718 }
719
720 impl<'a, 'tcx> TyDecoder<'tcx> for CacheDecoder<'a, 'tcx> {
721     const CLEAR_CROSS_CRATE: bool = false;
722
723     #[inline]
724     fn tcx(&self) -> TyCtxt<'tcx> {
725         self.tcx
726     }
727
728     #[inline]
729     fn position(&self) -> usize {
730         self.opaque.position()
731     }
732
733     #[inline]
734     fn peek_byte(&self) -> u8 {
735         self.opaque.data[self.opaque.position()]
736     }
737
738     fn cached_ty_for_shorthand<F>(
739         &mut self,
740         shorthand: usize,
741         or_insert_with: F,
742     ) -> Result<Ty<'tcx>, Self::Error>
743     where
744         F: FnOnce(&mut Self) -> Result<Ty<'tcx>, Self::Error>,
745     {
746         let tcx = self.tcx();
747
748         let cache_key = ty::CReaderCacheKey { cnum: None, pos: shorthand };
749
750         if let Some(&ty) = tcx.ty_rcache.borrow().get(&cache_key) {
751             return Ok(ty);
752         }
753
754         let ty = or_insert_with(self)?;
755         // This may overwrite the entry, but it should overwrite with the same value.
756         tcx.ty_rcache.borrow_mut().insert_same(cache_key, ty);
757         Ok(ty)
758     }
759
760     fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
761     where
762         F: FnOnce(&mut Self) -> R,
763     {
764         debug_assert!(pos < self.opaque.data.len());
765
766         let new_opaque = opaque::Decoder::new(self.opaque.data, pos);
767         let old_opaque = mem::replace(&mut self.opaque, new_opaque);
768         let r = f(self);
769         self.opaque = old_opaque;
770         r
771     }
772
773     fn decode_alloc_id(&mut self) -> Result<interpret::AllocId, Self::Error> {
774         let alloc_decoding_session = self.alloc_decoding_session;
775         alloc_decoding_session.decode_alloc_id(self)
776     }
777 }
778
779 crate::implement_ty_decoder!(CacheDecoder<'a, 'tcx>);
780
781 // This ensures that the `Decodable<opaque::Decoder>::decode` specialization for `Vec<u8>` is used
782 // when a `CacheDecoder` is passed to `Decodable::decode`. Unfortunately, we have to manually opt
783 // into specializations this way, given how `CacheDecoder` and the decoding traits currently work.
784 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Vec<u8> {
785     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
786         Decodable::decode(&mut d.opaque)
787     }
788 }
789
790 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for SyntaxContext {
791     fn decode(decoder: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
792         let syntax_contexts = decoder.syntax_contexts;
793         rustc_span::hygiene::decode_syntax_context(decoder, decoder.hygiene_context, |this, id| {
794             // This closure is invoked if we haven't already decoded the data for the `SyntaxContext` we are deserializing.
795             // We look up the position of the associated `SyntaxData` and decode it.
796             let pos = syntax_contexts.get(&id).unwrap();
797             this.with_position(pos.to_usize(), |decoder| {
798                 let data: SyntaxContextData = decode_tagged(decoder, TAG_SYNTAX_CONTEXT)?;
799                 Ok(data)
800             })
801         })
802     }
803 }
804
805 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for ExpnId {
806     fn decode(decoder: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
807         let hash = ExpnHash::decode(decoder)?;
808         if hash.is_root() {
809             return Ok(ExpnId::root());
810         }
811
812         if let Some(expn_id) = ExpnId::from_hash(hash) {
813             return Ok(expn_id);
814         }
815
816         let krate = decoder.cnum_map[&hash.stable_crate_id()];
817
818         let expn_id = if krate == LOCAL_CRATE {
819             // We look up the position of the associated `ExpnData` and decode it.
820             let pos = decoder
821                 .expn_data
822                 .get(&hash)
823                 .unwrap_or_else(|| panic!("Bad hash {:?} (map {:?})", hash, decoder.expn_data));
824
825             let data: ExpnData = decoder
826                 .with_position(pos.to_usize(), |decoder| decode_tagged(decoder, TAG_EXPN_DATA))?;
827             rustc_span::hygiene::register_local_expn_id(data, hash)
828         } else {
829             let index_guess = decoder.foreign_expn_data[&hash];
830             decoder.tcx.untracked_resolutions.cstore.expn_hash_to_expn_id(krate, index_guess, hash)
831         };
832
833         #[cfg(debug_assertions)]
834         {
835             use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
836             let mut hcx = decoder.tcx.create_stable_hashing_context();
837             let mut hasher = StableHasher::new();
838             expn_id.expn_data().hash_stable(&mut hcx, &mut hasher);
839             let local_hash: u64 = hasher.finish();
840             debug_assert_eq!(hash.local_hash(), local_hash);
841         }
842
843         Ok(expn_id)
844     }
845 }
846
847 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Span {
848     fn decode(decoder: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
849         let tag: u8 = Decodable::decode(decoder)?;
850
851         if tag == TAG_PARTIAL_SPAN {
852             let ctxt = SyntaxContext::decode(decoder)?;
853             return Ok(DUMMY_SP.with_ctxt(ctxt));
854         } else {
855             debug_assert_eq!(tag, TAG_FULL_SPAN);
856         }
857
858         let file_lo_index = SourceFileIndex::decode(decoder)?;
859         let line_lo = usize::decode(decoder)?;
860         let col_lo = BytePos::decode(decoder)?;
861         let len = BytePos::decode(decoder)?;
862         let ctxt = SyntaxContext::decode(decoder)?;
863
864         let file_lo = decoder.file_index_to_file(file_lo_index);
865         let lo = file_lo.lines[line_lo - 1] + col_lo;
866         let hi = lo + len;
867
868         Ok(Span::new(lo, hi, ctxt))
869     }
870 }
871
872 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for CrateNum {
873     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
874         let stable_id = StableCrateId::decode(d)?;
875         let cnum = d.cnum_map[&stable_id];
876         Ok(cnum)
877     }
878 }
879
880 // This impl makes sure that we get a runtime error when we try decode a
881 // `DefIndex` that is not contained in a `DefId`. Such a case would be problematic
882 // because we would not know how to transform the `DefIndex` to the current
883 // context.
884 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for DefIndex {
885     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<DefIndex, String> {
886         Err(d.error("trying to decode `DefIndex` outside the context of a `DefId`"))
887     }
888 }
889
890 // Both the `CrateNum` and the `DefIndex` of a `DefId` can change in between two
891 // compilation sessions. We use the `DefPathHash`, which is stable across
892 // sessions, to map the old `DefId` to the new one.
893 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for DefId {
894     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
895         // Load the `DefPathHash` which is was we encoded the `DefId` as.
896         let def_path_hash = DefPathHash::decode(d)?;
897
898         // Using the `DefPathHash`, we can lookup the new `DefId`.
899         // Subtle: We only encode a `DefId` as part of a query result.
900         // If we get to this point, then all of the query inputs were green,
901         // which means that the definition with this hash is guaranteed to
902         // still exist in the current compilation session.
903         Ok(d.tcx()
904             .on_disk_cache
905             .as_ref()
906             .unwrap()
907             .def_path_hash_to_def_id(d.tcx(), def_path_hash)
908             .unwrap())
909     }
910 }
911
912 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx FxHashSet<LocalDefId> {
913     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
914         RefDecodable::decode(d)
915     }
916 }
917
918 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>>
919     for &'tcx IndexVec<mir::Promoted, mir::Body<'tcx>>
920 {
921     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
922         RefDecodable::decode(d)
923     }
924 }
925
926 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [mir::abstract_const::Node<'tcx>] {
927     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
928         RefDecodable::decode(d)
929     }
930 }
931
932 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [(ty::Predicate<'tcx>, Span)] {
933     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
934         RefDecodable::decode(d)
935     }
936 }
937
938 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [rustc_ast::InlineAsmTemplatePiece] {
939     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
940         RefDecodable::decode(d)
941     }
942 }
943
944 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [Span] {
945     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
946         RefDecodable::decode(d)
947     }
948 }
949
950 //- ENCODING -------------------------------------------------------------------
951
952 pub trait OpaqueEncoder: Encoder {
953     fn position(&self) -> usize;
954 }
955
956 impl OpaqueEncoder for FileEncoder {
957     #[inline]
958     fn position(&self) -> usize {
959         FileEncoder::position(self)
960     }
961 }
962
963 /// An encoder that can write to the incremental compilation cache.
964 pub struct CacheEncoder<'a, 'tcx, E: OpaqueEncoder> {
965     tcx: TyCtxt<'tcx>,
966     encoder: &'a mut E,
967     type_shorthands: FxHashMap<Ty<'tcx>, usize>,
968     predicate_shorthands: FxHashMap<ty::PredicateKind<'tcx>, usize>,
969     interpret_allocs: FxIndexSet<interpret::AllocId>,
970     source_map: CachingSourceMapView<'tcx>,
971     file_to_file_index: FxHashMap<*const SourceFile, SourceFileIndex>,
972     hygiene_context: &'a HygieneEncodeContext,
973     latest_foreign_def_path_hashes: UnhashMap<DefPathHash, RawDefId>,
974 }
975
976 impl<'a, 'tcx, E> CacheEncoder<'a, 'tcx, E>
977 where
978     E: 'a + OpaqueEncoder,
979 {
980     fn source_file_index(&mut self, source_file: Lrc<SourceFile>) -> SourceFileIndex {
981         self.file_to_file_index[&(&*source_file as *const SourceFile)]
982     }
983
984     /// Encode something with additional information that allows to do some
985     /// sanity checks when decoding the data again. This method will first
986     /// encode the specified tag, then the given value, then the number of
987     /// bytes taken up by tag and value. On decoding, we can then verify that
988     /// we get the expected tag and read the expected number of bytes.
989     fn encode_tagged<T: Encodable<Self>, V: Encodable<Self>>(
990         &mut self,
991         tag: T,
992         value: &V,
993     ) -> Result<(), E::Error> {
994         let start_pos = self.position();
995
996         tag.encode(self)?;
997         value.encode(self)?;
998
999         let end_pos = self.position();
1000         ((end_pos - start_pos) as u64).encode(self)
1001     }
1002 }
1003
1004 impl<'a, 'tcx, E> Encodable<CacheEncoder<'a, 'tcx, E>> for SyntaxContext
1005 where
1006     E: 'a + OpaqueEncoder,
1007 {
1008     fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) -> Result<(), E::Error> {
1009         rustc_span::hygiene::raw_encode_syntax_context(*self, s.hygiene_context, s)
1010     }
1011 }
1012
1013 impl<'a, 'tcx, E> Encodable<CacheEncoder<'a, 'tcx, E>> for ExpnId
1014 where
1015     E: 'a + OpaqueEncoder,
1016 {
1017     fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) -> Result<(), E::Error> {
1018         s.hygiene_context.schedule_expn_data_for_encoding(*self);
1019         self.expn_hash().encode(s)
1020     }
1021 }
1022
1023 impl<'a, 'tcx, E> Encodable<CacheEncoder<'a, 'tcx, E>> for Span
1024 where
1025     E: 'a + OpaqueEncoder,
1026 {
1027     fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) -> Result<(), E::Error> {
1028         let span_data = self.data();
1029         if self.is_dummy() {
1030             TAG_PARTIAL_SPAN.encode(s)?;
1031             return span_data.ctxt.encode(s);
1032         }
1033
1034         let pos = s.source_map.byte_pos_to_line_and_col(span_data.lo);
1035         let partial_span = match &pos {
1036             Some((file_lo, _, _)) => !file_lo.contains(span_data.hi),
1037             None => true,
1038         };
1039
1040         if partial_span {
1041             TAG_PARTIAL_SPAN.encode(s)?;
1042             return span_data.ctxt.encode(s);
1043         }
1044
1045         let (file_lo, line_lo, col_lo) = pos.unwrap();
1046
1047         let len = span_data.hi - span_data.lo;
1048
1049         let source_file_index = s.source_file_index(file_lo);
1050
1051         TAG_FULL_SPAN.encode(s)?;
1052         source_file_index.encode(s)?;
1053         line_lo.encode(s)?;
1054         col_lo.encode(s)?;
1055         len.encode(s)?;
1056         span_data.ctxt.encode(s)
1057     }
1058 }
1059
1060 impl<'a, 'tcx, E> TyEncoder<'tcx> for CacheEncoder<'a, 'tcx, E>
1061 where
1062     E: 'a + OpaqueEncoder,
1063 {
1064     const CLEAR_CROSS_CRATE: bool = false;
1065
1066     fn position(&self) -> usize {
1067         self.encoder.position()
1068     }
1069     fn type_shorthands(&mut self) -> &mut FxHashMap<Ty<'tcx>, usize> {
1070         &mut self.type_shorthands
1071     }
1072     fn predicate_shorthands(&mut self) -> &mut FxHashMap<ty::PredicateKind<'tcx>, usize> {
1073         &mut self.predicate_shorthands
1074     }
1075     fn encode_alloc_id(&mut self, alloc_id: &interpret::AllocId) -> Result<(), Self::Error> {
1076         let (index, _) = self.interpret_allocs.insert_full(*alloc_id);
1077
1078         index.encode(self)
1079     }
1080 }
1081
1082 impl<'a, 'tcx, E> Encodable<CacheEncoder<'a, 'tcx, E>> for CrateNum
1083 where
1084     E: 'a + OpaqueEncoder,
1085 {
1086     fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) -> Result<(), E::Error> {
1087         s.tcx.stable_crate_id(*self).encode(s)
1088     }
1089 }
1090
1091 impl<'a, 'tcx, E> Encodable<CacheEncoder<'a, 'tcx, E>> for DefId
1092 where
1093     E: 'a + OpaqueEncoder,
1094 {
1095     fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) -> Result<(), E::Error> {
1096         let def_path_hash = s.tcx.def_path_hash(*self);
1097         // Store additional information when we encode a foreign `DefId`,
1098         // so that we can map its `DefPathHash` back to a `DefId` in the next
1099         // compilation session.
1100         if !self.is_local() {
1101             s.latest_foreign_def_path_hashes.insert(
1102                 def_path_hash,
1103                 RawDefId { krate: self.krate.as_u32(), index: self.index.as_u32() },
1104             );
1105         }
1106         def_path_hash.encode(s)
1107     }
1108 }
1109
1110 impl<'a, 'tcx, E> Encodable<CacheEncoder<'a, 'tcx, E>> for DefIndex
1111 where
1112     E: 'a + OpaqueEncoder,
1113 {
1114     fn encode(&self, _: &mut CacheEncoder<'a, 'tcx, E>) -> Result<(), E::Error> {
1115         bug!("encoding `DefIndex` without context");
1116     }
1117 }
1118
1119 macro_rules! encoder_methods {
1120     ($($name:ident($ty:ty);)*) => {
1121         #[inline]
1122         $(fn $name(&mut self, value: $ty) -> Result<(), Self::Error> {
1123             self.encoder.$name(value)
1124         })*
1125     }
1126 }
1127
1128 impl<'a, 'tcx, E> Encoder for CacheEncoder<'a, 'tcx, E>
1129 where
1130     E: 'a + OpaqueEncoder,
1131 {
1132     type Error = E::Error;
1133
1134     #[inline]
1135     fn emit_unit(&mut self) -> Result<(), Self::Error> {
1136         Ok(())
1137     }
1138
1139     encoder_methods! {
1140         emit_usize(usize);
1141         emit_u128(u128);
1142         emit_u64(u64);
1143         emit_u32(u32);
1144         emit_u16(u16);
1145         emit_u8(u8);
1146
1147         emit_isize(isize);
1148         emit_i128(i128);
1149         emit_i64(i64);
1150         emit_i32(i32);
1151         emit_i16(i16);
1152         emit_i8(i8);
1153
1154         emit_bool(bool);
1155         emit_f64(f64);
1156         emit_f32(f32);
1157         emit_char(char);
1158         emit_str(&str);
1159         emit_raw_bytes(&[u8]);
1160     }
1161 }
1162
1163 // This ensures that the `Encodable<opaque::FileEncoder>::encode` specialization for byte slices
1164 // is used when a `CacheEncoder` having an `opaque::FileEncoder` is passed to `Encodable::encode`.
1165 // Unfortunately, we have to manually opt into specializations this way, given how `CacheEncoder`
1166 // and the encoding traits currently work.
1167 impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx, FileEncoder>> for [u8] {
1168     fn encode(&self, e: &mut CacheEncoder<'a, 'tcx, FileEncoder>) -> FileEncodeResult {
1169         self.encode(e.encoder)
1170     }
1171 }
1172
1173 pub fn encode_query_results<'a, 'tcx, CTX, Q>(
1174     tcx: CTX,
1175     encoder: &mut CacheEncoder<'a, 'tcx, FileEncoder>,
1176     query_result_index: &mut EncodedQueryResultIndex,
1177 ) -> FileEncodeResult
1178 where
1179     CTX: QueryContext + 'tcx,
1180     Q: super::QueryDescription<CTX> + super::QueryAccessors<CTX>,
1181     Q::Value: Encodable<CacheEncoder<'a, 'tcx, FileEncoder>>,
1182 {
1183     let _timer = tcx
1184         .dep_context()
1185         .profiler()
1186         .extra_verbose_generic_activity("encode_query_results_for", std::any::type_name::<Q>());
1187
1188     assert!(Q::query_state(tcx).all_inactive());
1189     let cache = Q::query_cache(tcx);
1190     let mut res = Ok(());
1191     cache.iter_results(&mut |key, value, dep_node| {
1192         if res.is_err() {
1193             return;
1194         }
1195         if Q::cache_on_disk(tcx, &key, Some(value)) {
1196             let dep_node = SerializedDepNodeIndex::new(dep_node.index());
1197
1198             // Record position of the cache entry.
1199             query_result_index.push((dep_node, AbsoluteBytePos::new(encoder.encoder.position())));
1200
1201             // Encode the type check tables with the `SerializedDepNodeIndex`
1202             // as tag.
1203             match encoder.encode_tagged(dep_node, value) {
1204                 Ok(()) => {}
1205                 Err(e) => {
1206                     res = Err(e);
1207                 }
1208             }
1209         }
1210     });
1211
1212     res
1213 }