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