Skip to main content

rustc_middle/query/
on_disk_cache.rs

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