]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_query_impl/src/on_disk_cache.rs
Rollup merge of #92843 - camelid:str-concat-sugg, r=davidtwco
[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::thir;
13 use rustc_middle::ty::codec::{RefDecodable, TyDecoder, TyEncoder};
14 use rustc_middle::ty::{self, Ty, TyCtxt};
15 use rustc_query_system::dep_graph::DepContext;
16 use rustc_query_system::query::{QueryContext, QuerySideEffects};
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, Pos, SourceFile, Span};
28 use std::mem;
29
30 const TAG_FILE_FOOTER: u128 = 0xC0FFEE_C0FFEE_C0FFEE_C0FFEE_C0FFEE;
31
32 // A normal span encoded with both location information and a `SyntaxContext`
33 const TAG_FULL_SPAN: u8 = 0;
34 // A partial span with no location information, encoded only with a `SyntaxContext`
35 const TAG_PARTIAL_SPAN: u8 = 1;
36 const TAG_RELATIVE_SPAN: u8 = 2;
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 side effects that have been emitted during a query.
45 pub struct OnDiskCache<'sess> {
46     // The complete cache data in serialized form.
47     serialized_data: RwLock<Option<Mmap>>,
48
49     // Collects all `QuerySideEffects` created during the current compilation
50     // session.
51     current_side_effects: Lock<FxHashMap<DepNodeIndex, QuerySideEffects>>,
52
53     source_map: &'sess SourceMap,
54     file_index_to_stable_id: FxHashMap<SourceFileIndex, EncodedSourceFileId>,
55
56     // Caches that are populated lazily during decoding.
57     file_index_to_file: Lock<FxHashMap<SourceFileIndex, Lrc<SourceFile>>>,
58
59     // A map from dep-node to the position of the cached query result in
60     // `serialized_data`.
61     query_result_index: FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
62
63     // A map from dep-node to the position of any associated `QuerySideEffects` in
64     // `serialized_data`.
65     prev_side_effects_index: FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
66
67     alloc_decoding_state: AllocDecodingState,
68
69     // A map from syntax context ids to the position of their associated
70     // `SyntaxContextData`. We use a `u32` instead of a `SyntaxContext`
71     // to represent the fact that we are storing *encoded* ids. When we decode
72     // a `SyntaxContext`, a new id will be allocated from the global `HygieneData`,
73     // which will almost certainly be different than the serialized id.
74     syntax_contexts: FxHashMap<u32, AbsoluteBytePos>,
75     // A map from the `DefPathHash` of an `ExpnId` to the position
76     // of their associated `ExpnData`. Ideally, we would store a `DefId`,
77     // but we need to decode this before we've constructed a `TyCtxt` (which
78     // makes it difficult to decode a `DefId`).
79
80     // Note that these `DefPathHashes` correspond to both local and foreign
81     // `ExpnData` (e.g `ExpnData.krate` may not be `LOCAL_CRATE`). Alternatively,
82     // we could look up the `ExpnData` from the metadata of foreign crates,
83     // but it seemed easier to have `OnDiskCache` be independent of the `CStore`.
84     expn_data: UnhashMap<ExpnHash, AbsoluteBytePos>,
85     // Additional information used when decoding hygiene data.
86     hygiene_context: HygieneDecodeContext,
87     // Maps `ExpnHash`es to their raw value from the *previous*
88     // compilation session. This is used as an initial 'guess' when
89     // we try to map an `ExpnHash` to its value in the current
90     // compilation session.
91     foreign_expn_data: UnhashMap<ExpnHash, u32>,
92 }
93
94 // This type is used only for serialization and deserialization.
95 #[derive(Encodable, Decodable)]
96 struct Footer {
97     file_index_to_stable_id: FxHashMap<SourceFileIndex, EncodedSourceFileId>,
98     query_result_index: EncodedDepNodeIndex,
99     side_effects_index: EncodedDepNodeIndex,
100     // The location of all allocations.
101     interpret_alloc_index: Vec<u32>,
102     // See `OnDiskCache.syntax_contexts`
103     syntax_contexts: FxHashMap<u32, AbsoluteBytePos>,
104     // See `OnDiskCache.expn_data`
105     expn_data: UnhashMap<ExpnHash, AbsoluteBytePos>,
106     foreign_expn_data: UnhashMap<ExpnHash, u32>,
107 }
108
109 pub type EncodedDepNodeIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
110
111 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Encodable, Decodable)]
112 struct SourceFileIndex(u32);
113
114 #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Encodable, Decodable)]
115 pub struct AbsoluteBytePos(u32);
116
117 impl AbsoluteBytePos {
118     fn new(pos: usize) -> AbsoluteBytePos {
119         debug_assert!(pos <= u32::MAX as usize);
120         AbsoluteBytePos(pos as u32)
121     }
122
123     fn to_usize(self) -> usize {
124         self.0 as usize
125     }
126 }
127
128 /// An `EncodedSourceFileId` is the same as a `StableSourceFileId` except that
129 /// the source crate is represented as a [StableCrateId] instead of as a
130 /// `CrateNum`. This way `EncodedSourceFileId` can be encoded and decoded
131 /// without any additional context, i.e. with a simple `opaque::Decoder` (which
132 /// is the only thing available when decoding the cache's [Footer].
133 #[derive(Encodable, Decodable, Clone, Debug)]
134 struct EncodedSourceFileId {
135     file_name_hash: u64,
136     stable_crate_id: StableCrateId,
137 }
138
139 impl EncodedSourceFileId {
140     fn translate(&self, tcx: TyCtxt<'_>) -> StableSourceFileId {
141         let cnum = tcx.stable_crate_id_to_crate_num(self.stable_crate_id);
142         StableSourceFileId { file_name_hash: self.file_name_hash, cnum }
143     }
144
145     fn new(tcx: TyCtxt<'_>, file: &SourceFile) -> EncodedSourceFileId {
146         let source_file_id = StableSourceFileId::new(file);
147         EncodedSourceFileId {
148             file_name_hash: source_file_id.file_name_hash,
149             stable_crate_id: tcx.stable_crate_id(source_file_id.cnum),
150         }
151     }
152 }
153
154 impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
155     /// Creates a new `OnDiskCache` instance from the serialized data in `data`.
156     fn new(sess: &'sess Session, data: Mmap, start_pos: usize) -> Self {
157         debug_assert!(sess.opts.incremental.is_some());
158
159         // Wrap in a scope so we can borrow `data`.
160         let footer: Footer = {
161             let mut decoder = opaque::Decoder::new(&data, start_pos);
162
163             // Decode the *position* of the footer, which can be found in the
164             // last 8 bytes of the file.
165             decoder.set_position(data.len() - IntEncodedWithFixedSize::ENCODED_SIZE);
166             let footer_pos = IntEncodedWithFixedSize::decode(&mut decoder)
167                 .expect("error while trying to decode footer position")
168                 .0 as usize;
169
170             // Decode the file footer, which contains all the lookup tables, etc.
171             decoder.set_position(footer_pos);
172
173             decode_tagged(&mut decoder, TAG_FILE_FOOTER)
174                 .expect("error while trying to decode footer position")
175         };
176
177         Self {
178             serialized_data: RwLock::new(Some(data)),
179             file_index_to_stable_id: footer.file_index_to_stable_id,
180             file_index_to_file: Default::default(),
181             source_map: sess.source_map(),
182             current_side_effects: Default::default(),
183             query_result_index: footer.query_result_index.into_iter().collect(),
184             prev_side_effects_index: footer.side_effects_index.into_iter().collect(),
185             alloc_decoding_state: AllocDecodingState::new(footer.interpret_alloc_index),
186             syntax_contexts: footer.syntax_contexts,
187             expn_data: footer.expn_data,
188             foreign_expn_data: footer.foreign_expn_data,
189             hygiene_context: Default::default(),
190         }
191     }
192
193     fn new_empty(source_map: &'sess SourceMap) -> Self {
194         Self {
195             serialized_data: RwLock::new(None),
196             file_index_to_stable_id: Default::default(),
197             file_index_to_file: Default::default(),
198             source_map,
199             current_side_effects: Default::default(),
200             query_result_index: Default::default(),
201             prev_side_effects_index: Default::default(),
202             alloc_decoding_state: AllocDecodingState::new(Vec::new()),
203             syntax_contexts: FxHashMap::default(),
204             expn_data: UnhashMap::default(),
205             foreign_expn_data: UnhashMap::default(),
206             hygiene_context: Default::default(),
207         }
208     }
209
210     /// Execute all cache promotions and release the serialized backing Mmap.
211     ///
212     /// Cache promotions require invoking queries, which needs to read the serialized data.
213     /// In order to serialize the new on-disk cache, the former on-disk cache file needs to be
214     /// deleted, hence we won't be able to refer to its memmapped data.
215     fn drop_serialized_data(&self, tcx: TyCtxt<'_>) {
216         // Load everything into memory so we can write it out to the on-disk
217         // cache. The vast majority of cacheable query results should already
218         // be in memory, so this should be a cheap operation.
219         // Do this *before* we clone 'latest_foreign_def_path_hashes', since
220         // loading existing queries may cause us to create new DepNodes, which
221         // may in turn end up invoking `store_foreign_def_id_hash`
222         tcx.dep_graph.exec_cache_promotions(tcx);
223
224         *self.serialized_data.write() = None;
225     }
226
227     fn serialize<'tcx>(&self, tcx: TyCtxt<'tcx>, encoder: &mut FileEncoder) -> FileEncodeResult {
228         // Serializing the `DepGraph` should not modify it.
229         tcx.dep_graph.with_ignore(|| {
230             // Allocate `SourceFileIndex`es.
231             let (file_to_file_index, file_index_to_stable_id) = {
232                 let files = tcx.sess.source_map().files();
233                 let mut file_to_file_index =
234                     FxHashMap::with_capacity_and_hasher(files.len(), Default::default());
235                 let mut file_index_to_stable_id =
236                     FxHashMap::with_capacity_and_hasher(files.len(), Default::default());
237
238                 for (index, file) in files.iter().enumerate() {
239                     let index = SourceFileIndex(index as u32);
240                     let file_ptr: *const SourceFile = &**file as *const _;
241                     file_to_file_index.insert(file_ptr, index);
242                     let source_file_id = EncodedSourceFileId::new(tcx, &file);
243                     file_index_to_stable_id.insert(index, source_file_id);
244                 }
245
246                 (file_to_file_index, file_index_to_stable_id)
247             };
248
249             let hygiene_encode_context = HygieneEncodeContext::default();
250
251             let mut encoder = CacheEncoder {
252                 tcx,
253                 encoder,
254                 type_shorthands: Default::default(),
255                 predicate_shorthands: Default::default(),
256                 interpret_allocs: Default::default(),
257                 source_map: CachingSourceMapView::new(tcx.sess.source_map()),
258                 file_to_file_index,
259                 hygiene_context: &hygiene_encode_context,
260             };
261
262             // Encode query results.
263             let mut query_result_index = EncodedDepNodeIndex::new();
264
265             tcx.sess.time("encode_query_results", || -> FileEncodeResult {
266                 let enc = &mut encoder;
267                 let qri = &mut query_result_index;
268                 QueryCtxt::from_tcx(tcx).encode_query_results(enc, qri)
269             })?;
270
271             // Encode side effects.
272             let side_effects_index: EncodedDepNodeIndex = self
273                 .current_side_effects
274                 .borrow()
275                 .iter()
276                 .map(
277                     |(dep_node_index, side_effects)| -> Result<_, <FileEncoder as Encoder>::Error> {
278                         let pos = AbsoluteBytePos::new(encoder.position());
279                         let dep_node_index = SerializedDepNodeIndex::new(dep_node_index.index());
280                         encoder.encode_tagged(dep_node_index, side_effects)?;
281
282                         Ok((dep_node_index, pos))
283                     },
284                 )
285                 .collect::<Result<_, _>>()?;
286
287             let interpret_alloc_index = {
288                 let mut interpret_alloc_index = Vec::new();
289                 let mut n = 0;
290                 loop {
291                     let new_n = encoder.interpret_allocs.len();
292                     // If we have found new IDs, serialize those too.
293                     if n == new_n {
294                         // Otherwise, abort.
295                         break;
296                     }
297                     interpret_alloc_index.reserve(new_n - n);
298                     for idx in n..new_n {
299                         let id = encoder.interpret_allocs[idx];
300                         let pos = encoder.position() as u32;
301                         interpret_alloc_index.push(pos);
302                         interpret::specialized_encode_alloc_id(&mut encoder, tcx, id)?;
303                     }
304                     n = new_n;
305                 }
306                 interpret_alloc_index
307             };
308
309             let mut syntax_contexts = FxHashMap::default();
310             let mut expn_data = UnhashMap::default();
311             let mut foreign_expn_data = UnhashMap::default();
312
313             // Encode all hygiene data (`SyntaxContextData` and `ExpnData`) from the current
314             // session.
315
316             hygiene_encode_context.encode(
317                 &mut encoder,
318                 |encoder, index, ctxt_data| -> FileEncodeResult {
319                     let pos = AbsoluteBytePos::new(encoder.position());
320                     encoder.encode_tagged(TAG_SYNTAX_CONTEXT, ctxt_data)?;
321                     syntax_contexts.insert(index, pos);
322                     Ok(())
323                 },
324                 |encoder, expn_id, data, hash| -> FileEncodeResult {
325                     if expn_id.krate == LOCAL_CRATE {
326                         let pos = AbsoluteBytePos::new(encoder.position());
327                         encoder.encode_tagged(TAG_EXPN_DATA, data)?;
328                         expn_data.insert(hash, pos);
329                     } else {
330                         foreign_expn_data.insert(hash, expn_id.local_id.as_u32());
331                     }
332                     Ok(())
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(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             Ok(())
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, "side_effects");
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, "query result")
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         debug_tag: &'static str,
427     ) -> Option<T>
428     where
429         T: for<'a> Decodable<CacheDecoder<'a, 'tcx>>,
430     {
431         let pos = index.get(&dep_node_index).cloned()?;
432
433         self.with_decoder(tcx, pos, |decoder| match decode_tagged(decoder, dep_node_index) {
434             Ok(v) => Some(v),
435             Err(e) => bug!("could not decode cached {}: {}", debug_tag, e),
436         })
437     }
438
439     fn with_decoder<'a, 'tcx, T, F: for<'s> FnOnce(&mut CacheDecoder<'s, 'tcx>) -> T>(
440         &'sess self,
441         tcx: TyCtxt<'tcx>,
442         pos: AbsoluteBytePos,
443         f: F,
444     ) -> T
445     where
446         T: Decodable<CacheDecoder<'a, 'tcx>>,
447     {
448         let serialized_data = self.serialized_data.read();
449         let mut decoder = CacheDecoder {
450             tcx,
451             opaque: opaque::Decoder::new(serialized_data.as_deref().unwrap_or(&[]), pos.to_usize()),
452             source_map: self.source_map,
453             file_index_to_file: &self.file_index_to_file,
454             file_index_to_stable_id: &self.file_index_to_stable_id,
455             alloc_decoding_session: self.alloc_decoding_state.new_decoding_session(),
456             syntax_contexts: &self.syntax_contexts,
457             expn_data: &self.expn_data,
458             foreign_expn_data: &self.foreign_expn_data,
459             hygiene_context: &self.hygiene_context,
460         };
461         f(&mut decoder)
462     }
463 }
464
465 //- DECODING -------------------------------------------------------------------
466
467 /// A decoder that can read from the incremental compilation cache. It is similar to the one
468 /// we use for crate metadata decoding in that it can rebase spans and eventually
469 /// will also handle things that contain `Ty` instances.
470 pub struct CacheDecoder<'a, 'tcx> {
471     tcx: TyCtxt<'tcx>,
472     opaque: opaque::Decoder<'a>,
473     source_map: &'a SourceMap,
474     file_index_to_file: &'a Lock<FxHashMap<SourceFileIndex, Lrc<SourceFile>>>,
475     file_index_to_stable_id: &'a FxHashMap<SourceFileIndex, EncodedSourceFileId>,
476     alloc_decoding_session: AllocDecodingSession<'a>,
477     syntax_contexts: &'a FxHashMap<u32, AbsoluteBytePos>,
478     expn_data: &'a UnhashMap<ExpnHash, AbsoluteBytePos>,
479     foreign_expn_data: &'a UnhashMap<ExpnHash, u32>,
480     hygiene_context: &'a HygieneDecodeContext,
481 }
482
483 impl<'a, 'tcx> CacheDecoder<'a, 'tcx> {
484     fn file_index_to_file(&self, index: SourceFileIndex) -> Lrc<SourceFile> {
485         let CacheDecoder {
486             tcx,
487             ref file_index_to_file,
488             ref file_index_to_stable_id,
489             ref source_map,
490             ..
491         } = *self;
492
493         file_index_to_file
494             .borrow_mut()
495             .entry(index)
496             .or_insert_with(|| {
497                 let stable_id = file_index_to_stable_id[&index].translate(tcx);
498
499                 // If this `SourceFile` is from a foreign crate, then make sure
500                 // that we've imported all of the source files from that crate.
501                 // This has usually already been done during macro invocation.
502                 // However, when encoding query results like `TypeckResults`,
503                 // we might encode an `AdtDef` for a foreign type (because it
504                 // was referenced in the body of the function). There is no guarantee
505                 // that we will load the source files from that crate during macro
506                 // expansion, so we use `import_source_files` to ensure that the foreign
507                 // source files are actually imported before we call `source_file_by_stable_id`.
508                 if stable_id.cnum != LOCAL_CRATE {
509                     self.tcx.cstore_untracked().import_source_files(self.tcx.sess, stable_id.cnum);
510                 }
511
512                 source_map
513                     .source_file_by_stable_id(stable_id)
514                     .expect("failed to lookup `SourceFile` in new context")
515             })
516             .clone()
517     }
518 }
519
520 trait DecoderWithPosition: Decoder {
521     fn position(&self) -> usize;
522 }
523
524 impl<'a> DecoderWithPosition for opaque::Decoder<'a> {
525     fn position(&self) -> usize {
526         self.position()
527     }
528 }
529
530 impl<'a, 'tcx> DecoderWithPosition for CacheDecoder<'a, 'tcx> {
531     fn position(&self) -> usize {
532         self.opaque.position()
533     }
534 }
535
536 // Decodes something that was encoded with `encode_tagged()` and verify that the
537 // tag matches and the correct amount of bytes was read.
538 fn decode_tagged<D, T, V>(decoder: &mut D, expected_tag: T) -> Result<V, D::Error>
539 where
540     T: Decodable<D> + Eq + std::fmt::Debug,
541     V: Decodable<D>,
542     D: DecoderWithPosition,
543 {
544     let start_pos = decoder.position();
545
546     let actual_tag = T::decode(decoder)?;
547     assert_eq!(actual_tag, expected_tag);
548     let value = V::decode(decoder)?;
549     let end_pos = decoder.position();
550
551     let expected_len: u64 = Decodable::decode(decoder)?;
552     assert_eq!((end_pos - start_pos) as u64, expected_len);
553
554     Ok(value)
555 }
556
557 impl<'a, 'tcx> TyDecoder<'tcx> for CacheDecoder<'a, 'tcx> {
558     const CLEAR_CROSS_CRATE: bool = false;
559
560     #[inline]
561     fn tcx(&self) -> TyCtxt<'tcx> {
562         self.tcx
563     }
564
565     #[inline]
566     fn position(&self) -> usize {
567         self.opaque.position()
568     }
569
570     #[inline]
571     fn peek_byte(&self) -> u8 {
572         self.opaque.data[self.opaque.position()]
573     }
574
575     fn cached_ty_for_shorthand<F>(
576         &mut self,
577         shorthand: usize,
578         or_insert_with: F,
579     ) -> Result<Ty<'tcx>, Self::Error>
580     where
581         F: FnOnce(&mut Self) -> Result<Ty<'tcx>, Self::Error>,
582     {
583         let tcx = self.tcx();
584
585         let cache_key = ty::CReaderCacheKey { cnum: None, pos: shorthand };
586
587         if let Some(&ty) = tcx.ty_rcache.borrow().get(&cache_key) {
588             return Ok(ty);
589         }
590
591         let ty = or_insert_with(self)?;
592         // This may overwrite the entry, but it should overwrite with the same value.
593         tcx.ty_rcache.borrow_mut().insert_same(cache_key, ty);
594         Ok(ty)
595     }
596
597     fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
598     where
599         F: FnOnce(&mut Self) -> R,
600     {
601         debug_assert!(pos < self.opaque.data.len());
602
603         let new_opaque = opaque::Decoder::new(self.opaque.data, pos);
604         let old_opaque = mem::replace(&mut self.opaque, new_opaque);
605         let r = f(self);
606         self.opaque = old_opaque;
607         r
608     }
609
610     fn decode_alloc_id(&mut self) -> Result<interpret::AllocId, Self::Error> {
611         let alloc_decoding_session = self.alloc_decoding_session;
612         alloc_decoding_session.decode_alloc_id(self)
613     }
614 }
615
616 rustc_middle::implement_ty_decoder!(CacheDecoder<'a, 'tcx>);
617
618 // This ensures that the `Decodable<opaque::Decoder>::decode` specialization for `Vec<u8>` is used
619 // when a `CacheDecoder` is passed to `Decodable::decode`. Unfortunately, we have to manually opt
620 // into specializations this way, given how `CacheDecoder` and the decoding traits currently work.
621 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Vec<u8> {
622     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
623         Decodable::decode(&mut d.opaque)
624     }
625 }
626
627 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for SyntaxContext {
628     fn decode(decoder: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
629         let syntax_contexts = decoder.syntax_contexts;
630         rustc_span::hygiene::decode_syntax_context(decoder, decoder.hygiene_context, |this, id| {
631             // This closure is invoked if we haven't already decoded the data for the `SyntaxContext` we are deserializing.
632             // We look up the position of the associated `SyntaxData` and decode it.
633             let pos = syntax_contexts.get(&id).unwrap();
634             this.with_position(pos.to_usize(), |decoder| {
635                 let data: SyntaxContextData = decode_tagged(decoder, TAG_SYNTAX_CONTEXT)?;
636                 Ok(data)
637             })
638         })
639     }
640 }
641
642 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for ExpnId {
643     fn decode(decoder: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
644         let hash = ExpnHash::decode(decoder)?;
645         if hash.is_root() {
646             return Ok(ExpnId::root());
647         }
648
649         if let Some(expn_id) = ExpnId::from_hash(hash) {
650             return Ok(expn_id);
651         }
652
653         let krate = decoder.tcx.stable_crate_id_to_crate_num(hash.stable_crate_id());
654
655         let expn_id = if krate == LOCAL_CRATE {
656             // We look up the position of the associated `ExpnData` and decode it.
657             let pos = decoder
658                 .expn_data
659                 .get(&hash)
660                 .unwrap_or_else(|| panic!("Bad hash {:?} (map {:?})", hash, decoder.expn_data));
661
662             let data: ExpnData = decoder
663                 .with_position(pos.to_usize(), |decoder| decode_tagged(decoder, TAG_EXPN_DATA))?;
664             let expn_id = rustc_span::hygiene::register_local_expn_id(data, hash);
665
666             #[cfg(debug_assertions)]
667             {
668                 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
669                 let mut hcx = decoder.tcx.create_stable_hashing_context();
670                 let mut hasher = StableHasher::new();
671                 hcx.while_hashing_spans(true, |hcx| {
672                     expn_id.expn_data().hash_stable(hcx, &mut hasher)
673                 });
674                 let local_hash: u64 = hasher.finish();
675                 debug_assert_eq!(hash.local_hash(), local_hash);
676             }
677
678             expn_id
679         } else {
680             let index_guess = decoder.foreign_expn_data[&hash];
681             decoder.tcx.cstore_untracked().expn_hash_to_expn_id(
682                 decoder.tcx.sess,
683                 krate,
684                 index_guess,
685                 hash,
686             )
687         };
688
689         debug_assert_eq!(expn_id.krate, krate);
690         Ok(expn_id)
691     }
692 }
693
694 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Span {
695     fn decode(decoder: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
696         let ctxt = SyntaxContext::decode(decoder)?;
697         let parent = Option::<LocalDefId>::decode(decoder)?;
698         let tag: u8 = Decodable::decode(decoder)?;
699
700         if tag == TAG_PARTIAL_SPAN {
701             return Ok(Span::new(BytePos(0), BytePos(0), ctxt, parent));
702         } else if tag == TAG_RELATIVE_SPAN {
703             let dlo = u32::decode(decoder)?;
704             let dto = u32::decode(decoder)?;
705
706             let enclosing =
707                 decoder.tcx.definitions_untracked().def_span(parent.unwrap()).data_untracked();
708             let span = Span::new(
709                 enclosing.lo + BytePos::from_u32(dlo),
710                 enclosing.lo + BytePos::from_u32(dto),
711                 ctxt,
712                 parent,
713             );
714
715             return Ok(span);
716         } else {
717             debug_assert_eq!(tag, TAG_FULL_SPAN);
718         }
719
720         let file_lo_index = SourceFileIndex::decode(decoder)?;
721         let line_lo = usize::decode(decoder)?;
722         let col_lo = BytePos::decode(decoder)?;
723         let len = BytePos::decode(decoder)?;
724
725         let file_lo = decoder.file_index_to_file(file_lo_index);
726         let lo = file_lo.lines[line_lo - 1] + col_lo;
727         let hi = lo + len;
728
729         Ok(Span::new(lo, hi, ctxt, parent))
730     }
731 }
732
733 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for CrateNum {
734     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
735         let stable_id = StableCrateId::decode(d)?;
736         let cnum = d.tcx.stable_crate_id_to_crate_num(stable_id);
737         Ok(cnum)
738     }
739 }
740
741 // This impl makes sure that we get a runtime error when we try decode a
742 // `DefIndex` that is not contained in a `DefId`. Such a case would be problematic
743 // because we would not know how to transform the `DefIndex` to the current
744 // context.
745 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for DefIndex {
746     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<DefIndex, String> {
747         Err(d.error("trying to decode `DefIndex` outside the context of a `DefId`"))
748     }
749 }
750
751 // Both the `CrateNum` and the `DefIndex` of a `DefId` can change in between two
752 // compilation sessions. We use the `DefPathHash`, which is stable across
753 // sessions, to map the old `DefId` to the new one.
754 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for DefId {
755     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
756         // Load the `DefPathHash` which is was we encoded the `DefId` as.
757         let def_path_hash = DefPathHash::decode(d)?;
758
759         // Using the `DefPathHash`, we can lookup the new `DefId`.
760         // Subtle: We only encode a `DefId` as part of a query result.
761         // If we get to this point, then all of the query inputs were green,
762         // which means that the definition with this hash is guaranteed to
763         // still exist in the current compilation session.
764         Ok(d.tcx().def_path_hash_to_def_id(def_path_hash, &mut || {
765             panic!("Failed to convert DefPathHash {:?}", def_path_hash)
766         }))
767     }
768 }
769
770 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx FxHashSet<LocalDefId> {
771     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
772         RefDecodable::decode(d)
773     }
774 }
775
776 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>>
777     for &'tcx IndexVec<mir::Promoted, mir::Body<'tcx>>
778 {
779     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
780         RefDecodable::decode(d)
781     }
782 }
783
784 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [thir::abstract_const::Node<'tcx>] {
785     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
786         RefDecodable::decode(d)
787     }
788 }
789
790 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [(ty::Predicate<'tcx>, Span)] {
791     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
792         RefDecodable::decode(d)
793     }
794 }
795
796 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [rustc_ast::InlineAsmTemplatePiece] {
797     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
798         RefDecodable::decode(d)
799     }
800 }
801
802 impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [Span] {
803     fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
804         RefDecodable::decode(d)
805     }
806 }
807
808 //- ENCODING -------------------------------------------------------------------
809
810 pub trait OpaqueEncoder: Encoder {
811     fn position(&self) -> usize;
812 }
813
814 impl OpaqueEncoder for FileEncoder {
815     #[inline]
816     fn position(&self) -> usize {
817         FileEncoder::position(self)
818     }
819 }
820
821 /// An encoder that can write to the incremental compilation cache.
822 pub struct CacheEncoder<'a, 'tcx, E: OpaqueEncoder> {
823     tcx: TyCtxt<'tcx>,
824     encoder: &'a mut E,
825     type_shorthands: FxHashMap<Ty<'tcx>, usize>,
826     predicate_shorthands: FxHashMap<ty::PredicateKind<'tcx>, usize>,
827     interpret_allocs: FxIndexSet<interpret::AllocId>,
828     source_map: CachingSourceMapView<'tcx>,
829     file_to_file_index: FxHashMap<*const SourceFile, SourceFileIndex>,
830     hygiene_context: &'a HygieneEncodeContext,
831 }
832
833 impl<'a, 'tcx, E> CacheEncoder<'a, 'tcx, E>
834 where
835     E: 'a + OpaqueEncoder,
836 {
837     fn source_file_index(&mut self, source_file: Lrc<SourceFile>) -> SourceFileIndex {
838         self.file_to_file_index[&(&*source_file as *const SourceFile)]
839     }
840
841     /// Encode something with additional information that allows to do some
842     /// sanity checks when decoding the data again. This method will first
843     /// encode the specified tag, then the given value, then the number of
844     /// bytes taken up by tag and value. On decoding, we can then verify that
845     /// we get the expected tag and read the expected number of bytes.
846     fn encode_tagged<T: Encodable<Self>, V: Encodable<Self>>(
847         &mut self,
848         tag: T,
849         value: &V,
850     ) -> Result<(), E::Error> {
851         let start_pos = self.position();
852
853         tag.encode(self)?;
854         value.encode(self)?;
855
856         let end_pos = self.position();
857         ((end_pos - start_pos) as u64).encode(self)
858     }
859 }
860
861 impl<'a, 'tcx, E> Encodable<CacheEncoder<'a, 'tcx, E>> for SyntaxContext
862 where
863     E: 'a + OpaqueEncoder,
864 {
865     fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) -> Result<(), E::Error> {
866         rustc_span::hygiene::raw_encode_syntax_context(*self, s.hygiene_context, s)
867     }
868 }
869
870 impl<'a, 'tcx, E> Encodable<CacheEncoder<'a, 'tcx, E>> for ExpnId
871 where
872     E: 'a + OpaqueEncoder,
873 {
874     fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) -> Result<(), E::Error> {
875         s.hygiene_context.schedule_expn_data_for_encoding(*self);
876         self.expn_hash().encode(s)
877     }
878 }
879
880 impl<'a, 'tcx, E> Encodable<CacheEncoder<'a, 'tcx, E>> for Span
881 where
882     E: 'a + OpaqueEncoder,
883 {
884     fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) -> Result<(), E::Error> {
885         let span_data = self.data_untracked();
886         span_data.ctxt.encode(s)?;
887         span_data.parent.encode(s)?;
888
889         if span_data.is_dummy() {
890             return TAG_PARTIAL_SPAN.encode(s);
891         }
892
893         if let Some(parent) = span_data.parent {
894             let enclosing = s.tcx.definitions_untracked().def_span(parent).data_untracked();
895             if enclosing.contains(span_data) {
896                 TAG_RELATIVE_SPAN.encode(s)?;
897                 (span_data.lo - enclosing.lo).to_u32().encode(s)?;
898                 (span_data.hi - enclosing.lo).to_u32().encode(s)?;
899                 return Ok(());
900             }
901         }
902
903         let pos = s.source_map.byte_pos_to_line_and_col(span_data.lo);
904         let partial_span = match &pos {
905             Some((file_lo, _, _)) => !file_lo.contains(span_data.hi),
906             None => true,
907         };
908
909         if partial_span {
910             return TAG_PARTIAL_SPAN.encode(s);
911         }
912
913         let (file_lo, line_lo, col_lo) = pos.unwrap();
914
915         let len = span_data.hi - span_data.lo;
916
917         let source_file_index = s.source_file_index(file_lo);
918
919         TAG_FULL_SPAN.encode(s)?;
920         source_file_index.encode(s)?;
921         line_lo.encode(s)?;
922         col_lo.encode(s)?;
923         len.encode(s)
924     }
925 }
926
927 impl<'a, 'tcx, E> TyEncoder<'tcx> for CacheEncoder<'a, 'tcx, E>
928 where
929     E: 'a + OpaqueEncoder,
930 {
931     const CLEAR_CROSS_CRATE: bool = false;
932
933     fn position(&self) -> usize {
934         self.encoder.position()
935     }
936     fn type_shorthands(&mut self) -> &mut FxHashMap<Ty<'tcx>, usize> {
937         &mut self.type_shorthands
938     }
939     fn predicate_shorthands(&mut self) -> &mut FxHashMap<ty::PredicateKind<'tcx>, usize> {
940         &mut self.predicate_shorthands
941     }
942     fn encode_alloc_id(&mut self, alloc_id: &interpret::AllocId) -> Result<(), Self::Error> {
943         let (index, _) = self.interpret_allocs.insert_full(*alloc_id);
944
945         index.encode(self)
946     }
947 }
948
949 impl<'a, 'tcx, E> Encodable<CacheEncoder<'a, 'tcx, E>> for CrateNum
950 where
951     E: 'a + OpaqueEncoder,
952 {
953     fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) -> Result<(), E::Error> {
954         s.tcx.stable_crate_id(*self).encode(s)
955     }
956 }
957
958 impl<'a, 'tcx, E> Encodable<CacheEncoder<'a, 'tcx, E>> for DefId
959 where
960     E: 'a + OpaqueEncoder,
961 {
962     fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) -> Result<(), E::Error> {
963         s.tcx.def_path_hash(*self).encode(s)
964     }
965 }
966
967 impl<'a, 'tcx, E> Encodable<CacheEncoder<'a, 'tcx, E>> for DefIndex
968 where
969     E: 'a + OpaqueEncoder,
970 {
971     fn encode(&self, _: &mut CacheEncoder<'a, 'tcx, E>) -> Result<(), E::Error> {
972         bug!("encoding `DefIndex` without context");
973     }
974 }
975
976 macro_rules! encoder_methods {
977     ($($name:ident($ty:ty);)*) => {
978         #[inline]
979         $(fn $name(&mut self, value: $ty) -> Result<(), Self::Error> {
980             self.encoder.$name(value)
981         })*
982     }
983 }
984
985 impl<'a, 'tcx, E> Encoder for CacheEncoder<'a, 'tcx, E>
986 where
987     E: 'a + OpaqueEncoder,
988 {
989     type Error = E::Error;
990
991     #[inline]
992     fn emit_unit(&mut self) -> Result<(), Self::Error> {
993         Ok(())
994     }
995
996     encoder_methods! {
997         emit_usize(usize);
998         emit_u128(u128);
999         emit_u64(u64);
1000         emit_u32(u32);
1001         emit_u16(u16);
1002         emit_u8(u8);
1003
1004         emit_isize(isize);
1005         emit_i128(i128);
1006         emit_i64(i64);
1007         emit_i32(i32);
1008         emit_i16(i16);
1009         emit_i8(i8);
1010
1011         emit_bool(bool);
1012         emit_f64(f64);
1013         emit_f32(f32);
1014         emit_char(char);
1015         emit_str(&str);
1016         emit_raw_bytes(&[u8]);
1017     }
1018 }
1019
1020 // This ensures that the `Encodable<opaque::FileEncoder>::encode` specialization for byte slices
1021 // is used when a `CacheEncoder` having an `opaque::FileEncoder` is passed to `Encodable::encode`.
1022 // Unfortunately, we have to manually opt into specializations this way, given how `CacheEncoder`
1023 // and the encoding traits currently work.
1024 impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx, FileEncoder>> for [u8] {
1025     fn encode(&self, e: &mut CacheEncoder<'a, 'tcx, FileEncoder>) -> FileEncodeResult {
1026         self.encode(e.encoder)
1027     }
1028 }
1029
1030 pub fn encode_query_results<'a, 'tcx, CTX, Q>(
1031     tcx: CTX,
1032     encoder: &mut CacheEncoder<'a, 'tcx, FileEncoder>,
1033     query_result_index: &mut EncodedDepNodeIndex,
1034 ) -> FileEncodeResult
1035 where
1036     CTX: QueryContext + 'tcx,
1037     Q: super::QueryDescription<CTX>,
1038     Q::Value: Encodable<CacheEncoder<'a, 'tcx, FileEncoder>>,
1039 {
1040     let _timer = tcx
1041         .dep_context()
1042         .profiler()
1043         .extra_verbose_generic_activity("encode_query_results_for", std::any::type_name::<Q>());
1044
1045     assert!(Q::query_state(tcx).all_inactive());
1046     let cache = Q::query_cache(tcx);
1047     let mut res = Ok(());
1048     cache.iter_results(&mut |key, value, dep_node| {
1049         if res.is_err() {
1050             return;
1051         }
1052         if Q::cache_on_disk(*tcx.dep_context(), &key) {
1053             let dep_node = SerializedDepNodeIndex::new(dep_node.index());
1054
1055             // Record position of the cache entry.
1056             query_result_index.push((dep_node, AbsoluteBytePos::new(encoder.encoder.position())));
1057
1058             // Encode the type check tables with the `SerializedDepNodeIndex`
1059             // as tag.
1060             match encoder.encode_tagged(dep_node, value) {
1061                 Ok(()) => {}
1062                 Err(e) => {
1063                     res = Err(e);
1064                 }
1065             }
1066         }
1067     });
1068
1069     res
1070 }