• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
2 use rustc_data_structures::memmap::Mmap;
3 use rustc_data_structures::stable_hasher::Hash64;
4 use rustc_data_structures::sync::{HashMapExt, Lock, Lrc, RwLock};
5 use rustc_data_structures::unhash::UnhashMap;
6 use rustc_data_structures::unord::UnordSet;
7 use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, StableCrateId, LOCAL_CRATE};
8 use rustc_hir::definitions::DefPathHash;
9 use rustc_index::{Idx, IndexVec};
10 use rustc_middle::dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
11 use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState};
12 use rustc_middle::mir::{self, interpret};
13 use rustc_middle::ty::codec::{RefDecodable, TyDecoder, TyEncoder};
14 use rustc_middle::ty::{self, Ty, TyCtxt};
15 use rustc_query_system::query::QuerySideEffects;
16 use rustc_serialize::{
17     opaque::{FileEncodeResult, FileEncoder, IntEncodedWithFixedSize, MemDecoder},
18     Decodable, Decoder, Encodable, Encoder,
19 };
20 use rustc_session::Session;
21 use rustc_span::hygiene::{
22     ExpnId, HygieneDecodeContext, HygieneEncodeContext, SyntaxContext, SyntaxContextData,
23 };
24 use rustc_span::source_map::{SourceMap, StableSourceFileId};
25 use rustc_span::{BytePos, ExpnData, ExpnHash, Pos, SourceFile, Span};
26 use rustc_span::{CachingSourceMapView, Symbol};
27 use std::collections::hash_map::Entry;
28 use std::io;
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 const TAG_RELATIVE_SPAN: u8 = 2;
38 
39 const TAG_SYNTAX_CONTEXT: u8 = 0;
40 const TAG_EXPN_DATA: u8 = 1;
41 
42 // Tags for encoding Symbol's
43 const SYMBOL_STR: u8 = 0;
44 const SYMBOL_OFFSET: u8 = 1;
45 const SYMBOL_PREINTERNED: u8 = 2;
46 
47 /// Provides an interface to incremental compilation data cached from the
48 /// previous compilation session. This data will eventually include the results
49 /// of a few selected queries (like `typeck` and `mir_optimized`) and
50 /// any side effects that have been emitted during a query.
51 pub struct OnDiskCache<'sess> {
52     // The complete cache data in serialized form.
53     serialized_data: RwLock<Option<Mmap>>,
54 
55     // Collects all `QuerySideEffects` created during the current compilation
56     // session.
57     current_side_effects: Lock<FxHashMap<DepNodeIndex, QuerySideEffects>>,
58 
59     source_map: &'sess SourceMap,
60     file_index_to_stable_id: FxHashMap<SourceFileIndex, EncodedSourceFileId>,
61 
62     // Caches that are populated lazily during decoding.
63     file_index_to_file: Lock<FxHashMap<SourceFileIndex, Lrc<SourceFile>>>,
64 
65     // A map from dep-node to the position of the cached query result in
66     // `serialized_data`.
67     query_result_index: FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
68 
69     // A map from dep-node to the position of any associated `QuerySideEffects` in
70     // `serialized_data`.
71     prev_side_effects_index: FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
72 
73     alloc_decoding_state: AllocDecodingState,
74 
75     // A map from syntax context ids to the position of their associated
76     // `SyntaxContextData`. We use a `u32` instead of a `SyntaxContext`
77     // to represent the fact that we are storing *encoded* ids. When we decode
78     // a `SyntaxContext`, a new id will be allocated from the global `HygieneData`,
79     // which will almost certainly be different than the serialized id.
80     syntax_contexts: FxHashMap<u32, AbsoluteBytePos>,
81     // A map from the `DefPathHash` of an `ExpnId` to the position
82     // of their associated `ExpnData`. Ideally, we would store a `DefId`,
83     // but we need to decode this before we've constructed a `TyCtxt` (which
84     // makes it difficult to decode a `DefId`).
85 
86     // Note that these `DefPathHashes` correspond to both local and foreign
87     // `ExpnData` (e.g `ExpnData.krate` may not be `LOCAL_CRATE`). Alternatively,
88     // we could look up the `ExpnData` from the metadata of foreign crates,
89     // but it seemed easier to have `OnDiskCache` be independent of the `CStore`.
90     expn_data: UnhashMap<ExpnHash, AbsoluteBytePos>,
91     // Additional information used when decoding hygiene data.
92     hygiene_context: HygieneDecodeContext,
93     // Maps `ExpnHash`es to their raw value from the *previous*
94     // compilation session. This is used as an initial 'guess' when
95     // we try to map an `ExpnHash` to its value in the current
96     // compilation session.
97     foreign_expn_data: UnhashMap<ExpnHash, u32>,
98 }
99 
100 // This type is used only for serialization and deserialization.
101 #[derive(Encodable, Decodable)]
102 struct Footer {
103     file_index_to_stable_id: FxHashMap<SourceFileIndex, EncodedSourceFileId>,
104     query_result_index: EncodedDepNodeIndex,
105     side_effects_index: EncodedDepNodeIndex,
106     // The location of all allocations.
107     interpret_alloc_index: Vec<u32>,
108     // See `OnDiskCache.syntax_contexts`
109     syntax_contexts: FxHashMap<u32, AbsoluteBytePos>,
110     // See `OnDiskCache.expn_data`
111     expn_data: UnhashMap<ExpnHash, AbsoluteBytePos>,
112     foreign_expn_data: UnhashMap<ExpnHash, u32>,
113 }
114 
115 pub type EncodedDepNodeIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
116 
117 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Encodable, Decodable)]
118 struct SourceFileIndex(u32);
119 
120 #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Encodable, Decodable)]
121 pub struct AbsoluteBytePos(u64);
122 
123 impl AbsoluteBytePos {
124     #[inline]
new(pos: usize) -> AbsoluteBytePos125     pub fn new(pos: usize) -> AbsoluteBytePos {
126         AbsoluteBytePos(pos.try_into().expect("Incremental cache file size overflowed u64."))
127     }
128 
129     #[inline]
to_usize(self) -> usize130     fn to_usize(self) -> usize {
131         self.0 as usize
132     }
133 }
134 
135 /// An `EncodedSourceFileId` is the same as a `StableSourceFileId` except that
136 /// the source crate is represented as a [StableCrateId] instead of as a
137 /// `CrateNum`. This way `EncodedSourceFileId` can be encoded and decoded
138 /// without any additional context, i.e. with a simple `opaque::Decoder` (which
139 /// is the only thing available when decoding the cache's [Footer].
140 #[derive(Encodable, Decodable, Clone, Debug)]
141 struct EncodedSourceFileId {
142     file_name_hash: Hash64,
143     stable_crate_id: StableCrateId,
144 }
145 
146 impl EncodedSourceFileId {
147     #[inline]
translate(&self, tcx: TyCtxt<'_>) -> StableSourceFileId148     fn translate(&self, tcx: TyCtxt<'_>) -> StableSourceFileId {
149         let cnum = tcx.stable_crate_id_to_crate_num(self.stable_crate_id);
150         StableSourceFileId { file_name_hash: self.file_name_hash, cnum }
151     }
152 
153     #[inline]
new(tcx: TyCtxt<'_>, file: &SourceFile) -> EncodedSourceFileId154     fn new(tcx: TyCtxt<'_>, file: &SourceFile) -> EncodedSourceFileId {
155         let source_file_id = StableSourceFileId::new(file);
156         EncodedSourceFileId {
157             file_name_hash: source_file_id.file_name_hash,
158             stable_crate_id: tcx.stable_crate_id(source_file_id.cnum),
159         }
160     }
161 }
162 
163 impl<'sess> OnDiskCache<'sess> {
164     /// Creates a new `OnDiskCache` instance from the serialized data in `data`.
new(sess: &'sess Session, data: Mmap, start_pos: usize) -> Self165     pub fn new(sess: &'sess Session, data: Mmap, start_pos: usize) -> Self {
166         debug_assert!(sess.opts.incremental.is_some());
167 
168         // Wrap in a scope so we can borrow `data`.
169         let footer: Footer = {
170             let mut decoder = MemDecoder::new(&data, start_pos);
171 
172             // Decode the *position* of the footer, which can be found in the
173             // last 8 bytes of the file.
174             let footer_pos = decoder
175                 .with_position(decoder.len() - IntEncodedWithFixedSize::ENCODED_SIZE, |decoder| {
176                     IntEncodedWithFixedSize::decode(decoder).0 as usize
177                 });
178             // Decode the file footer, which contains all the lookup tables, etc.
179             decoder.with_position(footer_pos, |decoder| decode_tagged(decoder, TAG_FILE_FOOTER))
180         };
181 
182         Self {
183             serialized_data: RwLock::new(Some(data)),
184             file_index_to_stable_id: footer.file_index_to_stable_id,
185             file_index_to_file: Default::default(),
186             source_map: sess.source_map(),
187             current_side_effects: Default::default(),
188             query_result_index: footer.query_result_index.into_iter().collect(),
189             prev_side_effects_index: footer.side_effects_index.into_iter().collect(),
190             alloc_decoding_state: AllocDecodingState::new(footer.interpret_alloc_index),
191             syntax_contexts: footer.syntax_contexts,
192             expn_data: footer.expn_data,
193             foreign_expn_data: footer.foreign_expn_data,
194             hygiene_context: Default::default(),
195         }
196     }
197 
new_empty(source_map: &'sess SourceMap) -> Self198     pub fn new_empty(source_map: &'sess SourceMap) -> Self {
199         Self {
200             serialized_data: RwLock::new(None),
201             file_index_to_stable_id: Default::default(),
202             file_index_to_file: Default::default(),
203             source_map,
204             current_side_effects: Default::default(),
205             query_result_index: Default::default(),
206             prev_side_effects_index: Default::default(),
207             alloc_decoding_state: AllocDecodingState::new(Vec::new()),
208             syntax_contexts: FxHashMap::default(),
209             expn_data: UnhashMap::default(),
210             foreign_expn_data: UnhashMap::default(),
211             hygiene_context: Default::default(),
212         }
213     }
214 
215     /// Execute all cache promotions and release the serialized backing Mmap.
216     ///
217     /// Cache promotions require invoking queries, which needs to read the serialized data.
218     /// In order to serialize the new on-disk cache, the former on-disk cache file needs to be
219     /// deleted, hence we won't be able to refer to its memmapped data.
drop_serialized_data(&self, tcx: TyCtxt<'_>)220     pub fn drop_serialized_data(&self, tcx: TyCtxt<'_>) {
221         // Load everything into memory so we can write it out to the on-disk
222         // cache. The vast majority of cacheable query results should already
223         // be in memory, so this should be a cheap operation.
224         // Do this *before* we clone 'latest_foreign_def_path_hashes', since
225         // loading existing queries may cause us to create new DepNodes, which
226         // may in turn end up invoking `store_foreign_def_id_hash`
227         tcx.dep_graph.exec_cache_promotions(tcx);
228 
229         *self.serialized_data.write() = None;
230     }
231 
serialize(&self, tcx: TyCtxt<'_>, encoder: FileEncoder) -> FileEncodeResult232     pub fn serialize(&self, tcx: TyCtxt<'_>, encoder: FileEncoder) -> FileEncodeResult {
233         // Serializing the `DepGraph` should not modify it.
234         tcx.dep_graph.with_ignore(|| {
235             // Allocate `SourceFileIndex`es.
236             let (file_to_file_index, file_index_to_stable_id) = {
237                 let files = tcx.sess.source_map().files();
238                 let mut file_to_file_index =
239                     FxHashMap::with_capacity_and_hasher(files.len(), Default::default());
240                 let mut file_index_to_stable_id =
241                     FxHashMap::with_capacity_and_hasher(files.len(), Default::default());
242 
243                 for (index, file) in files.iter().enumerate() {
244                     let index = SourceFileIndex(index as u32);
245                     let file_ptr: *const SourceFile = &**file as *const _;
246                     file_to_file_index.insert(file_ptr, index);
247                     let source_file_id = EncodedSourceFileId::new(tcx, &file);
248                     file_index_to_stable_id.insert(index, source_file_id);
249                 }
250 
251                 (file_to_file_index, file_index_to_stable_id)
252             };
253 
254             let hygiene_encode_context = HygieneEncodeContext::default();
255 
256             let mut encoder = CacheEncoder {
257                 tcx,
258                 encoder,
259                 type_shorthands: Default::default(),
260                 predicate_shorthands: Default::default(),
261                 interpret_allocs: Default::default(),
262                 source_map: CachingSourceMapView::new(tcx.sess.source_map()),
263                 file_to_file_index,
264                 hygiene_context: &hygiene_encode_context,
265                 symbol_table: Default::default(),
266             };
267 
268             // Encode query results.
269             let mut query_result_index = EncodedDepNodeIndex::new();
270 
271             tcx.sess.time("encode_query_results", || {
272                 let enc = &mut encoder;
273                 let qri = &mut query_result_index;
274                 (tcx.query_system.fns.encode_query_results)(tcx, enc, qri);
275             });
276 
277             // Encode side effects.
278             let side_effects_index: EncodedDepNodeIndex = self
279                 .current_side_effects
280                 .borrow()
281                 .iter()
282                 .map(|(dep_node_index, side_effects)| {
283                     let pos = AbsoluteBytePos::new(encoder.position());
284                     let dep_node_index = SerializedDepNodeIndex::new(dep_node_index.index());
285                     encoder.encode_tagged(dep_node_index, side_effects);
286 
287                     (dep_node_index, pos)
288                 })
289                 .collect();
290 
291             let interpret_alloc_index = {
292                 let mut interpret_alloc_index = Vec::new();
293                 let mut n = 0;
294                 loop {
295                     let new_n = encoder.interpret_allocs.len();
296                     // If we have found new IDs, serialize those too.
297                     if n == new_n {
298                         // Otherwise, abort.
299                         break;
300                     }
301                     interpret_alloc_index.reserve(new_n - n);
302                     for idx in n..new_n {
303                         let id = encoder.interpret_allocs[idx];
304                         let pos: u32 = encoder.position().try_into().unwrap();
305                         interpret_alloc_index.push(pos);
306                         interpret::specialized_encode_alloc_id(&mut encoder, tcx, id);
307                     }
308                     n = new_n;
309                 }
310                 interpret_alloc_index
311             };
312 
313             let mut syntax_contexts = FxHashMap::default();
314             let mut expn_data = UnhashMap::default();
315             let mut foreign_expn_data = UnhashMap::default();
316 
317             // Encode all hygiene data (`SyntaxContextData` and `ExpnData`) from the current
318             // session.
319 
320             hygiene_encode_context.encode(
321                 &mut encoder,
322                 |encoder, index, ctxt_data| {
323                     let pos = AbsoluteBytePos::new(encoder.position());
324                     encoder.encode_tagged(TAG_SYNTAX_CONTEXT, ctxt_data);
325                     syntax_contexts.insert(index, pos);
326                 },
327                 |encoder, expn_id, data, hash| {
328                     if expn_id.krate == LOCAL_CRATE {
329                         let pos = AbsoluteBytePos::new(encoder.position());
330                         encoder.encode_tagged(TAG_EXPN_DATA, data);
331                         expn_data.insert(hash, pos);
332                     } else {
333                         foreign_expn_data.insert(hash, expn_id.local_id.as_u32());
334                     }
335                 },
336             );
337 
338             // Encode the file footer.
339             let footer_pos = encoder.position() as u64;
340             encoder.encode_tagged(
341                 TAG_FILE_FOOTER,
342                 &Footer {
343                     file_index_to_stable_id,
344                     query_result_index,
345                     side_effects_index,
346                     interpret_alloc_index,
347                     syntax_contexts,
348                     expn_data,
349                     foreign_expn_data,
350                 },
351             );
352 
353             // Encode the position of the footer as the last 8 bytes of the
354             // file so we know where to look for it.
355             IntEncodedWithFixedSize(footer_pos).encode(&mut encoder.encoder);
356 
357             // DO NOT WRITE ANYTHING TO THE ENCODER AFTER THIS POINT! The address
358             // of the footer must be the last thing in the data stream.
359 
360             encoder.finish()
361         })
362     }
363 
364     /// Loads a `QuerySideEffects` created during the previous compilation session.
load_side_effects( &self, tcx: TyCtxt<'_>, dep_node_index: SerializedDepNodeIndex, ) -> QuerySideEffects365     pub fn load_side_effects(
366         &self,
367         tcx: TyCtxt<'_>,
368         dep_node_index: SerializedDepNodeIndex,
369     ) -> QuerySideEffects {
370         let side_effects: Option<QuerySideEffects> =
371             self.load_indexed(tcx, dep_node_index, &self.prev_side_effects_index);
372 
373         side_effects.unwrap_or_default()
374     }
375 
376     /// Stores a `QuerySideEffects` emitted during the current compilation session.
377     /// Anything stored like this will be available via `load_side_effects` in
378     /// the next compilation session.
store_side_effects(&self, dep_node_index: DepNodeIndex, side_effects: QuerySideEffects)379     pub fn store_side_effects(&self, dep_node_index: DepNodeIndex, side_effects: QuerySideEffects) {
380         let mut current_side_effects = self.current_side_effects.borrow_mut();
381         let prev = current_side_effects.insert(dep_node_index, side_effects);
382         debug_assert!(prev.is_none());
383     }
384 
385     /// Return whether the cached query result can be decoded.
386     #[inline]
loadable_from_disk(&self, dep_node_index: SerializedDepNodeIndex) -> bool387     pub fn loadable_from_disk(&self, dep_node_index: SerializedDepNodeIndex) -> bool {
388         self.query_result_index.contains_key(&dep_node_index)
389         // with_decoder is infallible, so we can stop here
390     }
391 
392     /// Returns the cached query result if there is something in the cache for
393     /// the given `SerializedDepNodeIndex`; otherwise returns `None`.
try_load_query_result<'tcx, T>( &self, tcx: TyCtxt<'tcx>, dep_node_index: SerializedDepNodeIndex, ) -> Option<T> where T: for<'a> Decodable<CacheDecoder<'a, 'tcx>>,394     pub fn try_load_query_result<'tcx, T>(
395         &self,
396         tcx: TyCtxt<'tcx>,
397         dep_node_index: SerializedDepNodeIndex,
398     ) -> Option<T>
399     where
400         T: for<'a> Decodable<CacheDecoder<'a, 'tcx>>,
401     {
402         let opt_value = self.load_indexed(tcx, dep_node_index, &self.query_result_index);
403         debug_assert_eq!(opt_value.is_some(), self.loadable_from_disk(dep_node_index));
404         opt_value
405     }
406 
407     /// Stores side effect emitted during computation of an anonymous query.
408     /// Since many anonymous queries can share the same `DepNode`, we aggregate
409     /// them -- as opposed to regular queries where we assume that there is a
410     /// 1:1 relationship between query-key and `DepNode`.
store_side_effects_for_anon_node( &self, dep_node_index: DepNodeIndex, side_effects: QuerySideEffects, )411     pub fn store_side_effects_for_anon_node(
412         &self,
413         dep_node_index: DepNodeIndex,
414         side_effects: QuerySideEffects,
415     ) {
416         let mut current_side_effects = self.current_side_effects.borrow_mut();
417 
418         let x = current_side_effects.entry(dep_node_index).or_default();
419         x.append(side_effects);
420     }
421 
load_indexed<'tcx, T>( &self, tcx: TyCtxt<'tcx>, dep_node_index: SerializedDepNodeIndex, index: &FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>, ) -> Option<T> where T: for<'a> Decodable<CacheDecoder<'a, 'tcx>>,422     fn load_indexed<'tcx, T>(
423         &self,
424         tcx: TyCtxt<'tcx>,
425         dep_node_index: SerializedDepNodeIndex,
426         index: &FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
427     ) -> Option<T>
428     where
429         T: for<'a> Decodable<CacheDecoder<'a, 'tcx>>,
430     {
431         let pos = index.get(&dep_node_index).cloned()?;
432         let value = self.with_decoder(tcx, pos, |decoder| decode_tagged(decoder, dep_node_index));
433         Some(value)
434     }
435 
with_decoder<'a, 'tcx, T, F: for<'s> FnOnce(&mut CacheDecoder<'s, 'tcx>) -> T>( &'sess self, tcx: TyCtxt<'tcx>, pos: AbsoluteBytePos, f: F, ) -> T where T: Decodable<CacheDecoder<'a, 'tcx>>,436     fn with_decoder<'a, 'tcx, T, F: for<'s> FnOnce(&mut CacheDecoder<'s, 'tcx>) -> T>(
437         &'sess self,
438         tcx: TyCtxt<'tcx>,
439         pos: AbsoluteBytePos,
440         f: F,
441     ) -> T
442     where
443         T: Decodable<CacheDecoder<'a, 'tcx>>,
444     {
445         let serialized_data = self.serialized_data.read();
446         let mut decoder = CacheDecoder {
447             tcx,
448             opaque: MemDecoder::new(serialized_data.as_deref().unwrap_or(&[]), pos.to_usize()),
449             source_map: self.source_map,
450             file_index_to_file: &self.file_index_to_file,
451             file_index_to_stable_id: &self.file_index_to_stable_id,
452             alloc_decoding_session: self.alloc_decoding_state.new_decoding_session(),
453             syntax_contexts: &self.syntax_contexts,
454             expn_data: &self.expn_data,
455             foreign_expn_data: &self.foreign_expn_data,
456             hygiene_context: &self.hygiene_context,
457         };
458         f(&mut decoder)
459     }
460 }
461 
462 //- DECODING -------------------------------------------------------------------
463 
464 /// A decoder that can read from the incremental compilation cache. It is similar to the one
465 /// we use for crate metadata decoding in that it can rebase spans and eventually
466 /// will also handle things that contain `Ty` instances.
467 pub struct CacheDecoder<'a, 'tcx> {
468     tcx: TyCtxt<'tcx>,
469     opaque: MemDecoder<'a>,
470     source_map: &'a SourceMap,
471     file_index_to_file: &'a Lock<FxHashMap<SourceFileIndex, Lrc<SourceFile>>>,
472     file_index_to_stable_id: &'a FxHashMap<SourceFileIndex, EncodedSourceFileId>,
473     alloc_decoding_session: AllocDecodingSession<'a>,
474     syntax_contexts: &'a FxHashMap<u32, AbsoluteBytePos>,
475     expn_data: &'a UnhashMap<ExpnHash, AbsoluteBytePos>,
476     foreign_expn_data: &'a UnhashMap<ExpnHash, u32>,
477     hygiene_context: &'a HygieneDecodeContext,
478 }
479 
480 impl<'a, 'tcx> CacheDecoder<'a, 'tcx> {
481     #[inline]
file_index_to_file(&self, index: SourceFileIndex) -> Lrc<SourceFile>482     fn file_index_to_file(&self, index: SourceFileIndex) -> Lrc<SourceFile> {
483         let CacheDecoder {
484             tcx,
485             ref file_index_to_file,
486             ref file_index_to_stable_id,
487             ref source_map,
488             ..
489         } = *self;
490 
491         file_index_to_file
492             .borrow_mut()
493             .entry(index)
494             .or_insert_with(|| {
495                 let stable_id = file_index_to_stable_id[&index].translate(tcx);
496 
497                 // If this `SourceFile` is from a foreign crate, then make sure
498                 // that we've imported all of the source files from that crate.
499                 // This has usually already been done during macro invocation.
500                 // However, when encoding query results like `TypeckResults`,
501                 // we might encode an `AdtDef` for a foreign type (because it
502                 // was referenced in the body of the function). There is no guarantee
503                 // that we will load the source files from that crate during macro
504                 // expansion, so we use `import_source_files` to ensure that the foreign
505                 // source files are actually imported before we call `source_file_by_stable_id`.
506                 if stable_id.cnum != LOCAL_CRATE {
507                     self.tcx.cstore_untracked().import_source_files(self.tcx.sess, stable_id.cnum);
508                 }
509 
510                 source_map
511                     .source_file_by_stable_id(stable_id)
512                     .expect("failed to lookup `SourceFile` in new context")
513             })
514             .clone()
515     }
516 }
517 
518 // Decodes something that was encoded with `encode_tagged()` and verify that the
519 // tag matches and the correct amount of bytes was read.
decode_tagged<D, T, V>(decoder: &mut D, expected_tag: T) -> V where T: Decodable<D> + Eq + std::fmt::Debug, V: Decodable<D>, D: Decoder,520 fn decode_tagged<D, T, V>(decoder: &mut D, expected_tag: T) -> V
521 where
522     T: Decodable<D> + Eq + std::fmt::Debug,
523     V: Decodable<D>,
524     D: Decoder,
525 {
526     let start_pos = decoder.position();
527 
528     let actual_tag = T::decode(decoder);
529     assert_eq!(actual_tag, expected_tag);
530     let value = V::decode(decoder);
531     let end_pos = decoder.position();
532 
533     let expected_len: u64 = Decodable::decode(decoder);
534     assert_eq!((end_pos - start_pos) as u64, expected_len);
535 
536     value
537 }
538 
539 impl<'a, 'tcx> TyDecoder for CacheDecoder<'a, 'tcx> {
540     type I = TyCtxt<'tcx>;
541     const CLEAR_CROSS_CRATE: bool = false;
542 
543     #[inline]
interner(&self) -> TyCtxt<'tcx>544     fn interner(&self) -> TyCtxt<'tcx> {
545         self.tcx
546     }
547 
cached_ty_for_shorthand<F>(&mut self, shorthand: usize, or_insert_with: F) -> Ty<'tcx> where F: FnOnce(&mut Self) -> Ty<'tcx>,548     fn cached_ty_for_shorthand<F>(&mut self, shorthand: usize, or_insert_with: F) -> Ty<'tcx>
549     where
550         F: FnOnce(&mut Self) -> Ty<'tcx>,
551     {
552         let tcx = self.tcx;
553 
554         let cache_key = ty::CReaderCacheKey { cnum: None, pos: shorthand };
555 
556         if let Some(&ty) = tcx.ty_rcache.borrow().get(&cache_key) {
557             return ty;
558         }
559 
560         let ty = or_insert_with(self);
561         // This may overwrite the entry, but it should overwrite with the same value.
562         tcx.ty_rcache.borrow_mut().insert_same(cache_key, ty);
563         ty
564     }
565 
with_position<F, R>(&mut self, pos: usize, f: F) -> R where F: FnOnce(&mut Self) -> R,566     fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
567     where
568         F: FnOnce(&mut Self) -> R,
569     {
570         debug_assert!(pos < self.opaque.len());
571 
572         let new_opaque = MemDecoder::new(self.opaque.data(), pos);
573         let old_opaque = mem::replace(&mut self.opaque, new_opaque);
574         let r = f(self);
575         self.opaque = old_opaque;
576         r
577     }
578 
decode_alloc_id(&mut self) -> interpret::AllocId579     fn decode_alloc_id(&mut self) -> interpret::AllocId {
580         let alloc_decoding_session = self.alloc_decoding_session;
581         alloc_decoding_session.decode_alloc_id(self)
582     }
583 }
584 
585 rustc_middle::implement_ty_decoder!(CacheDecoder<'a, 'tcx>);
586 
587 // This ensures that the `Decodable<opaque::Decoder>::decode` specialization for `Vec<u8>` is used
588 // when a `CacheDecoder` is passed to `Decodable::decode`. Unfortunately, we have to manually opt
589 // into specializations this way, given how `CacheDecoder` and the decoding traits currently work.
590 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Vec<u8> {
decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self591     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
592         Decodable::decode(&mut d.opaque)
593     }
594 }
595 
596 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for SyntaxContext {
decode(decoder: &mut CacheDecoder<'a, 'tcx>) -> Self597     fn decode(decoder: &mut CacheDecoder<'a, 'tcx>) -> Self {
598         let syntax_contexts = decoder.syntax_contexts;
599         rustc_span::hygiene::decode_syntax_context(decoder, decoder.hygiene_context, |this, id| {
600             // This closure is invoked if we haven't already decoded the data for the `SyntaxContext` we are deserializing.
601             // We look up the position of the associated `SyntaxData` and decode it.
602             let pos = syntax_contexts.get(&id).unwrap();
603             this.with_position(pos.to_usize(), |decoder| {
604                 let data: SyntaxContextData = decode_tagged(decoder, TAG_SYNTAX_CONTEXT);
605                 data
606             })
607         })
608     }
609 }
610 
611 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for ExpnId {
decode(decoder: &mut CacheDecoder<'a, 'tcx>) -> Self612     fn decode(decoder: &mut CacheDecoder<'a, 'tcx>) -> Self {
613         let hash = ExpnHash::decode(decoder);
614         if hash.is_root() {
615             return ExpnId::root();
616         }
617 
618         if let Some(expn_id) = ExpnId::from_hash(hash) {
619             return expn_id;
620         }
621 
622         let krate = decoder.tcx.stable_crate_id_to_crate_num(hash.stable_crate_id());
623 
624         let expn_id = if krate == LOCAL_CRATE {
625             // We look up the position of the associated `ExpnData` and decode it.
626             let pos = decoder
627                 .expn_data
628                 .get(&hash)
629                 .unwrap_or_else(|| panic!("Bad hash {:?} (map {:?})", hash, decoder.expn_data));
630 
631             let data: ExpnData = decoder
632                 .with_position(pos.to_usize(), |decoder| decode_tagged(decoder, TAG_EXPN_DATA));
633             let expn_id = rustc_span::hygiene::register_local_expn_id(data, hash);
634 
635             #[cfg(debug_assertions)]
636             {
637                 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
638                 let local_hash = decoder.tcx.with_stable_hashing_context(|mut hcx| {
639                     let mut hasher = StableHasher::new();
640                     expn_id.expn_data().hash_stable(&mut hcx, &mut hasher);
641                     hasher.finish()
642                 });
643                 debug_assert_eq!(hash.local_hash(), local_hash);
644             }
645 
646             expn_id
647         } else {
648             let index_guess = decoder.foreign_expn_data[&hash];
649             decoder.tcx.cstore_untracked().expn_hash_to_expn_id(
650                 decoder.tcx.sess,
651                 krate,
652                 index_guess,
653                 hash,
654             )
655         };
656 
657         debug_assert_eq!(expn_id.krate, krate);
658         expn_id
659     }
660 }
661 
662 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Span {
decode(decoder: &mut CacheDecoder<'a, 'tcx>) -> Self663     fn decode(decoder: &mut CacheDecoder<'a, 'tcx>) -> Self {
664         let ctxt = SyntaxContext::decode(decoder);
665         let parent = Option::<LocalDefId>::decode(decoder);
666         let tag: u8 = Decodable::decode(decoder);
667 
668         if tag == TAG_PARTIAL_SPAN {
669             return Span::new(BytePos(0), BytePos(0), ctxt, parent);
670         } else if tag == TAG_RELATIVE_SPAN {
671             let dlo = u32::decode(decoder);
672             let dto = u32::decode(decoder);
673 
674             let enclosing = decoder.tcx.source_span_untracked(parent.unwrap()).data_untracked();
675             let span = Span::new(
676                 enclosing.lo + BytePos::from_u32(dlo),
677                 enclosing.lo + BytePos::from_u32(dto),
678                 ctxt,
679                 parent,
680             );
681 
682             return span;
683         } else {
684             debug_assert_eq!(tag, TAG_FULL_SPAN);
685         }
686 
687         let file_lo_index = SourceFileIndex::decode(decoder);
688         let line_lo = usize::decode(decoder);
689         let col_lo = BytePos::decode(decoder);
690         let len = BytePos::decode(decoder);
691 
692         let file_lo = decoder.file_index_to_file(file_lo_index);
693         let lo = file_lo.lines(|lines| lines[line_lo - 1] + col_lo);
694         let hi = lo + len;
695 
696         Span::new(lo, hi, ctxt, parent)
697     }
698 }
699 
700 // copy&paste impl from rustc_metadata
701 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Symbol {
702     #[inline]
decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self703     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
704         let tag = d.read_u8();
705 
706         match tag {
707             SYMBOL_STR => {
708                 let s = d.read_str();
709                 Symbol::intern(s)
710             }
711             SYMBOL_OFFSET => {
712                 // read str offset
713                 let pos = d.read_usize();
714 
715                 // move to str offset and read
716                 d.opaque.with_position(pos, |d| {
717                     let s = d.read_str();
718                     Symbol::intern(s)
719                 })
720             }
721             SYMBOL_PREINTERNED => {
722                 let symbol_index = d.read_u32();
723                 Symbol::new_from_decoded(symbol_index)
724             }
725             _ => unreachable!(),
726         }
727     }
728 }
729 
730 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for CrateNum {
731     #[inline]
decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self732     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
733         let stable_id = StableCrateId::decode(d);
734         let cnum = d.tcx.stable_crate_id_to_crate_num(stable_id);
735         cnum
736     }
737 }
738 
739 // This impl makes sure that we get a runtime error when we try decode a
740 // `DefIndex` that is not contained in a `DefId`. Such a case would be problematic
741 // because we would not know how to transform the `DefIndex` to the current
742 // context.
743 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for DefIndex {
decode(_d: &mut CacheDecoder<'a, 'tcx>) -> DefIndex744     fn decode(_d: &mut CacheDecoder<'a, 'tcx>) -> DefIndex {
745         panic!("trying to decode `DefIndex` outside the context of a `DefId`")
746     }
747 }
748 
749 // Both the `CrateNum` and the `DefIndex` of a `DefId` can change in between two
750 // compilation sessions. We use the `DefPathHash`, which is stable across
751 // sessions, to map the old `DefId` to the new one.
752 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for DefId {
753     #[inline]
decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self754     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
755         // Load the `DefPathHash` which is was we encoded the `DefId` as.
756         let def_path_hash = DefPathHash::decode(d);
757 
758         // Using the `DefPathHash`, we can lookup the new `DefId`.
759         // Subtle: We only encode a `DefId` as part of a query result.
760         // If we get to this point, then all of the query inputs were green,
761         // which means that the definition with this hash is guaranteed to
762         // still exist in the current compilation session.
763         d.tcx.def_path_hash_to_def_id(def_path_hash, &mut || {
764             panic!("Failed to convert DefPathHash {def_path_hash:?}")
765         })
766     }
767 }
768 
769 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx UnordSet<LocalDefId> {
770     #[inline]
decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self771     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
772         RefDecodable::decode(d)
773     }
774 }
775 
776 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>>
777     for &'tcx FxHashMap<DefId, ty::EarlyBinder<Ty<'tcx>>>
778 {
779     #[inline]
decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self780     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
781         RefDecodable::decode(d)
782     }
783 }
784 
785 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>>
786     for &'tcx IndexVec<mir::Promoted, mir::Body<'tcx>>
787 {
788     #[inline]
decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self789     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
790         RefDecodable::decode(d)
791     }
792 }
793 
794 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [(ty::Clause<'tcx>, Span)] {
795     #[inline]
decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self796     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
797         RefDecodable::decode(d)
798     }
799 }
800 
801 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [rustc_ast::InlineAsmTemplatePiece] {
802     #[inline]
decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self803     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
804         RefDecodable::decode(d)
805     }
806 }
807 
808 macro_rules! impl_ref_decoder {
809     (<$tcx:tt> $($ty:ty,)*) => {
810         $(impl<'a, $tcx> Decodable<CacheDecoder<'a, $tcx>> for &$tcx [$ty] {
811             #[inline]
812             fn decode(d: &mut CacheDecoder<'a, $tcx>) -> Self {
813                 RefDecodable::decode(d)
814             }
815         })*
816     };
817 }
818 
819 impl_ref_decoder! {<'tcx>
820     Span,
821     rustc_ast::Attribute,
822     rustc_span::symbol::Ident,
823     ty::Variance,
824     rustc_span::def_id::DefId,
825     rustc_span::def_id::LocalDefId,
826     (rustc_middle::middle::exported_symbols::ExportedSymbol<'tcx>, rustc_middle::middle::exported_symbols::SymbolExportInfo),
827     ty::DeducedParamAttrs,
828 }
829 
830 //- ENCODING -------------------------------------------------------------------
831 
832 /// An encoder that can write to the incremental compilation cache.
833 pub struct CacheEncoder<'a, 'tcx> {
834     tcx: TyCtxt<'tcx>,
835     encoder: FileEncoder,
836     type_shorthands: FxHashMap<Ty<'tcx>, usize>,
837     predicate_shorthands: FxHashMap<ty::PredicateKind<'tcx>, usize>,
838     interpret_allocs: FxIndexSet<interpret::AllocId>,
839     source_map: CachingSourceMapView<'tcx>,
840     file_to_file_index: FxHashMap<*const SourceFile, SourceFileIndex>,
841     hygiene_context: &'a HygieneEncodeContext,
842     symbol_table: FxHashMap<Symbol, usize>,
843 }
844 
845 impl<'a, 'tcx> CacheEncoder<'a, 'tcx> {
846     #[inline]
source_file_index(&mut self, source_file: Lrc<SourceFile>) -> SourceFileIndex847     fn source_file_index(&mut self, source_file: Lrc<SourceFile>) -> SourceFileIndex {
848         self.file_to_file_index[&(&*source_file as *const SourceFile)]
849     }
850 
851     /// Encode something with additional information that allows to do some
852     /// sanity checks when decoding the data again. This method will first
853     /// encode the specified tag, then the given value, then the number of
854     /// bytes taken up by tag and value. On decoding, we can then verify that
855     /// we get the expected tag and read the expected number of bytes.
encode_tagged<T: Encodable<Self>, V: Encodable<Self>>(&mut self, tag: T, value: &V)856     pub fn encode_tagged<T: Encodable<Self>, V: Encodable<Self>>(&mut self, tag: T, value: &V) {
857         let start_pos = self.position();
858 
859         tag.encode(self);
860         value.encode(self);
861 
862         let end_pos = self.position();
863         ((end_pos - start_pos) as u64).encode(self);
864     }
865 
866     #[inline]
finish(self) -> Result<usize, io::Error>867     fn finish(self) -> Result<usize, io::Error> {
868         self.encoder.finish()
869     }
870 }
871 
872 impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for SyntaxContext {
encode(&self, s: &mut CacheEncoder<'a, 'tcx>)873     fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) {
874         rustc_span::hygiene::raw_encode_syntax_context(*self, s.hygiene_context, s);
875     }
876 }
877 
878 impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for ExpnId {
encode(&self, s: &mut CacheEncoder<'a, 'tcx>)879     fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) {
880         s.hygiene_context.schedule_expn_data_for_encoding(*self);
881         self.expn_hash().encode(s);
882     }
883 }
884 
885 impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for Span {
encode(&self, s: &mut CacheEncoder<'a, 'tcx>)886     fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) {
887         let span_data = self.data_untracked();
888         span_data.ctxt.encode(s);
889         span_data.parent.encode(s);
890 
891         if span_data.is_dummy() {
892             return TAG_PARTIAL_SPAN.encode(s);
893         }
894 
895         if let Some(parent) = span_data.parent {
896             let enclosing = s.tcx.source_span(parent).data_untracked();
897             if enclosing.contains(span_data) {
898                 TAG_RELATIVE_SPAN.encode(s);
899                 (span_data.lo - enclosing.lo).to_u32().encode(s);
900                 (span_data.hi - enclosing.lo).to_u32().encode(s);
901                 return;
902             }
903         }
904 
905         let pos = s.source_map.byte_pos_to_line_and_col(span_data.lo);
906         let partial_span = match &pos {
907             Some((file_lo, _, _)) => !file_lo.contains(span_data.hi),
908             None => true,
909         };
910 
911         if partial_span {
912             return TAG_PARTIAL_SPAN.encode(s);
913         }
914 
915         let (file_lo, line_lo, col_lo) = pos.unwrap();
916 
917         let len = span_data.hi - span_data.lo;
918 
919         let source_file_index = s.source_file_index(file_lo);
920 
921         TAG_FULL_SPAN.encode(s);
922         source_file_index.encode(s);
923         line_lo.encode(s);
924         col_lo.encode(s);
925         len.encode(s);
926     }
927 }
928 
929 // copy&paste impl from rustc_metadata
930 impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for Symbol {
encode(&self, s: &mut CacheEncoder<'a, 'tcx>)931     fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) {
932         // if symbol preinterned, emit tag and symbol index
933         if self.is_preinterned() {
934             s.encoder.emit_u8(SYMBOL_PREINTERNED);
935             s.encoder.emit_u32(self.as_u32());
936         } else {
937             // otherwise write it as string or as offset to it
938             match s.symbol_table.entry(*self) {
939                 Entry::Vacant(o) => {
940                     s.encoder.emit_u8(SYMBOL_STR);
941                     let pos = s.encoder.position();
942                     o.insert(pos);
943                     s.emit_str(self.as_str());
944                 }
945                 Entry::Occupied(o) => {
946                     let x = *o.get();
947                     s.emit_u8(SYMBOL_OFFSET);
948                     s.emit_usize(x);
949                 }
950             }
951         }
952     }
953 }
954 
955 impl<'a, 'tcx> TyEncoder for CacheEncoder<'a, 'tcx> {
956     type I = TyCtxt<'tcx>;
957     const CLEAR_CROSS_CRATE: bool = false;
958 
959     #[inline]
position(&self) -> usize960     fn position(&self) -> usize {
961         self.encoder.position()
962     }
963     #[inline]
type_shorthands(&mut self) -> &mut FxHashMap<Ty<'tcx>, usize>964     fn type_shorthands(&mut self) -> &mut FxHashMap<Ty<'tcx>, usize> {
965         &mut self.type_shorthands
966     }
967     #[inline]
predicate_shorthands(&mut self) -> &mut FxHashMap<ty::PredicateKind<'tcx>, usize>968     fn predicate_shorthands(&mut self) -> &mut FxHashMap<ty::PredicateKind<'tcx>, usize> {
969         &mut self.predicate_shorthands
970     }
971     #[inline]
encode_alloc_id(&mut self, alloc_id: &interpret::AllocId)972     fn encode_alloc_id(&mut self, alloc_id: &interpret::AllocId) {
973         let (index, _) = self.interpret_allocs.insert_full(*alloc_id);
974 
975         index.encode(self);
976     }
977 }
978 
979 impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for CrateNum {
980     #[inline]
encode(&self, s: &mut CacheEncoder<'a, 'tcx>)981     fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) {
982         s.tcx.stable_crate_id(*self).encode(s);
983     }
984 }
985 
986 impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for DefId {
987     #[inline]
encode(&self, s: &mut CacheEncoder<'a, 'tcx>)988     fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) {
989         s.tcx.def_path_hash(*self).encode(s);
990     }
991 }
992 
993 impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for DefIndex {
encode(&self, _: &mut CacheEncoder<'a, 'tcx>)994     fn encode(&self, _: &mut CacheEncoder<'a, 'tcx>) {
995         bug!("encoding `DefIndex` without context");
996     }
997 }
998 
999 macro_rules! encoder_methods {
1000     ($($name:ident($ty:ty);)*) => {
1001         #[inline]
1002         $(fn $name(&mut self, value: $ty) {
1003             self.encoder.$name(value)
1004         })*
1005     }
1006 }
1007 
1008 impl<'a, 'tcx> Encoder for CacheEncoder<'a, 'tcx> {
1009     encoder_methods! {
1010         emit_usize(usize);
1011         emit_u128(u128);
1012         emit_u64(u64);
1013         emit_u32(u32);
1014         emit_u16(u16);
1015         emit_u8(u8);
1016 
1017         emit_isize(isize);
1018         emit_i128(i128);
1019         emit_i64(i64);
1020         emit_i32(i32);
1021         emit_i16(i16);
1022 
1023         emit_raw_bytes(&[u8]);
1024     }
1025 }
1026 
1027 // This ensures that the `Encodable<opaque::FileEncoder>::encode` specialization for byte slices
1028 // is used when a `CacheEncoder` having an `opaque::FileEncoder` is passed to `Encodable::encode`.
1029 // Unfortunately, we have to manually opt into specializations this way, given how `CacheEncoder`
1030 // and the encoding traits currently work.
1031 impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for [u8] {
encode(&self, e: &mut CacheEncoder<'a, 'tcx>)1032     fn encode(&self, e: &mut CacheEncoder<'a, 'tcx>) {
1033         self.encode(&mut e.encoder);
1034     }
1035 }
1036