rustc_middle/query/
on_disk_cache.rs

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