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::{self, interpret};
28use crate::mono::MonoItem;
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<'static>) -> 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                caching_source_map_view: 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    fn cached_ty_for_shorthand<F>(&mut self, shorthand: usize, or_insert_with: F) -> Ty<'tcx>
493    where
494        F: FnOnce(&mut Self) -> Ty<'tcx>,
495    {
496        let tcx = self.tcx;
497
498        let cache_key = ty::CReaderCacheKey { cnum: None, pos: shorthand };
499
500        if let Some(&ty) = tcx.ty_rcache.borrow().get(&cache_key) {
501            return ty;
502        }
503
504        let ty = or_insert_with(self);
505        // This may overwrite the entry, but it should overwrite with the same value.
506        tcx.ty_rcache.borrow_mut().insert_same(cache_key, ty);
507        ty
508    }
509
510    fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
511    where
512        F: FnOnce(&mut Self) -> R,
513    {
514        if true {
    if !(pos < self.opaque.len()) {
        ::core::panicking::panic("assertion failed: pos < self.opaque.len()")
    };
};debug_assert!(pos < self.opaque.len());
515
516        let new_opaque = self.opaque.split_at(pos);
517        let old_opaque = mem::replace(&mut self.opaque, new_opaque);
518        let r = f(self);
519        self.opaque = old_opaque;
520        r
521    }
522
523    fn decode_alloc_id(&mut self) -> interpret::AllocId {
524        let alloc_decoding_session = self.alloc_decoding_session;
525        alloc_decoding_session.decode_alloc_id(self)
526    }
527}
528
529impl<'a, 'tcx> rustc_type_ir::InternerDecoder for CacheDecoder<'a, 'tcx> {
530    type Interner = TyCtxt<'tcx>;
531
532    #[inline]
533    fn interner(&self) -> Self::Interner {
534        self.tcx
535    }
536}
537
538mod __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>);
539
540// This ensures that the `Decodable<opaque::Decoder>::decode` specialization for `Vec<u8>` is used
541// when a `CacheDecoder` is passed to `Decodable::decode`. Unfortunately, we have to manually opt
542// into specializations this way, given how `CacheDecoder` and the decoding traits currently work.
543impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Vec<u8> {
544    fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
545        Decodable::decode(&mut d.opaque)
546    }
547}
548
549impl<'a, 'tcx> SpanDecoder for CacheDecoder<'a, 'tcx> {
550    fn decode_syntax_context(&mut self) -> SyntaxContext {
551        let syntax_contexts = self.syntax_contexts;
552        rustc_span::hygiene::decode_syntax_context(self, self.hygiene_context, |this, id| {
553            // This closure is invoked if we haven't already decoded the data for the `SyntaxContext` we are deserializing.
554            // We look up the position of the associated `SyntaxData` and decode it.
555            let pos = syntax_contexts.get(&id).unwrap();
556            this.with_position(pos.to_usize(), |decoder| {
557                let data: SyntaxContextKey = decode_tagged(decoder, TAG_SYNTAX_CONTEXT);
558                data
559            })
560        })
561    }
562
563    fn decode_expn_id(&mut self) -> ExpnId {
564        let hash = ExpnHash::decode(self);
565        if hash.is_root() {
566            return ExpnId::root();
567        }
568
569        if let Some(expn_id) = ExpnId::from_hash(hash) {
570            return expn_id;
571        }
572
573        let krate = self.tcx.stable_crate_id_to_crate_num(hash.stable_crate_id());
574
575        let expn_id = if krate == LOCAL_CRATE {
576            // We look up the position of the associated `ExpnData` and decode it.
577            let pos = self
578                .expn_data
579                .get(&hash)
580                .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));
581
582            let data: ExpnData =
583                self.with_position(pos.to_usize(), |decoder| decode_tagged(decoder, TAG_EXPN_DATA));
584            let expn_id = rustc_span::hygiene::register_local_expn_id(data, hash);
585
586            #[cfg(debug_assertions)]
587            {
588                use rustc_data_structures::stable_hash::{StableHash, StableHasher};
589                let local_hash = self.tcx.with_stable_hashing_context(|mut hcx| {
590                    let mut hasher = StableHasher::new();
591                    expn_id.expn_data().stable_hash(&mut hcx, &mut hasher);
592                    hasher.finish()
593                });
594                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);
595            }
596
597            expn_id
598        } else {
599            let index_guess = self.foreign_expn_data[&hash];
600            self.tcx.expn_hash_to_expn_id(krate, index_guess, hash)
601        };
602
603        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);
604        expn_id
605    }
606
607    fn decode_span(&mut self) -> Span {
608        let ctxt = SyntaxContext::decode(self);
609        let parent = Option::<LocalDefId>::decode(self);
610        let tag: u8 = Decodable::decode(self);
611
612        let (lo, hi) = match tag {
613            TAG_PARTIAL_SPAN => (BytePos(0), BytePos(0)),
614            TAG_RELATIVE_SPAN => {
615                let dlo = u32::decode(self);
616                let dto = u32::decode(self);
617
618                let enclosing = self.tcx.source_span_untracked(parent.unwrap()).data_untracked();
619                (
620                    BytePos(enclosing.lo.0.wrapping_add(dlo)),
621                    BytePos(enclosing.lo.0.wrapping_add(dto)),
622                )
623            }
624            TAG_FULL_SPAN => {
625                let file_lo_index = SourceFileIndex::decode(self);
626                let line_lo = usize::decode(self);
627                let col_lo = RelativeBytePos::decode(self);
628                let len = BytePos::decode(self);
629
630                let file_lo = self.file_index_to_file(file_lo_index);
631                let lo = file_lo.lines()[line_lo - 1] + col_lo;
632                let lo = file_lo.absolute_position(lo);
633                let hi = lo + len;
634                (lo, hi)
635            }
636            _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
637        };
638
639        Span::new(lo, hi, ctxt, parent)
640    }
641
642    fn decode_crate_num(&mut self) -> CrateNum {
643        let stable_id = StableCrateId::decode(self);
644        let cnum = self.tcx.stable_crate_id_to_crate_num(stable_id);
645        cnum
646    }
647
648    // Both the `CrateNum` and the `DefIndex` of a `DefId` can change in between two
649    // compilation sessions. We use the `DefPathHash`, which is stable across
650    // sessions, to map the old `DefId` to the new one.
651    fn decode_def_id(&mut self) -> DefId {
652        // Load the `DefPathHash` which is was we encoded the `DefId` as.
653        let def_path_hash = DefPathHash::decode(self);
654
655        // Using the `DefPathHash`, we can lookup the new `DefId`.
656        // Subtle: We only encode a `DefId` as part of a query result.
657        // If we get to this point, then all of the query inputs were green,
658        // which means that the definition with this hash is guaranteed to
659        // still exist in the current compilation session.
660        match self.tcx.def_path_hash_to_def_id(def_path_hash) {
661            Some(r) => r,
662            None => {
    ::core::panicking::panic_fmt(format_args!("Failed to convert DefPathHash {0:?}",
            def_path_hash));
}panic!("Failed to convert DefPathHash {def_path_hash:?}"),
663        }
664    }
665
666    fn decode_attr_id(&mut self) -> rustc_span::AttrId {
667        {
    ::core::panicking::panic_fmt(format_args!("cannot decode `AttrId` with `CacheDecoder`"));
};panic!("cannot decode `AttrId` with `CacheDecoder`");
668    }
669}
670
671impl<'a, 'tcx> BlobDecoder for CacheDecoder<'a, 'tcx> {
672    fn decode_symbol(&mut self) -> Symbol {
673        self.decode_symbol_or_byte_symbol(
674            Symbol::new,
675            |this| Symbol::intern(this.read_str()),
676            |opaque| Symbol::intern(opaque.read_str()),
677        )
678    }
679
680    fn decode_byte_symbol(&mut self) -> ByteSymbol {
681        self.decode_symbol_or_byte_symbol(
682            ByteSymbol::new,
683            |this| ByteSymbol::intern(this.read_byte_str()),
684            |opaque| ByteSymbol::intern(opaque.read_byte_str()),
685        )
686    }
687
688    // This impl makes sure that we get a runtime error when we try decode a
689    // `DefIndex` that is not contained in a `DefId`. Such a case would be problematic
690    // because we would not know how to transform the `DefIndex` to the current
691    // context.
692    fn decode_def_index(&mut self) -> DefIndex {
693        {
    ::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`")
694    }
695}
696
697impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx UnordSet<LocalDefId> {
698    #[inline]
699    fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
700        RefDecodable::decode(d)
701    }
702}
703
704impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>>
705    for &'tcx UnordMap<DefId, ty::EarlyBinder<'tcx, Ty<'tcx>>>
706{
707    #[inline]
708    fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
709        RefDecodable::decode(d)
710    }
711}
712
713impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>>
714    for &'tcx IndexVec<mir::Promoted, mir::Body<'tcx>>
715{
716    #[inline]
717    fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
718        RefDecodable::decode(d)
719    }
720}
721
722impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [(ty::Clause<'tcx>, Span)] {
723    #[inline]
724    fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
725        RefDecodable::decode(d)
726    }
727}
728
729impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [rustc_ast::InlineAsmTemplatePiece] {
730    #[inline]
731    fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
732        RefDecodable::decode(d)
733    }
734}
735
736impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [Spanned<MonoItem<'tcx>>] {
737    #[inline]
738    fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
739        RefDecodable::decode(d)
740    }
741}
742
743impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>>
744    for &'tcx crate::traits::specialization_graph::Graph
745{
746    #[inline]
747    fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
748        RefDecodable::decode(d)
749    }
750}
751
752impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx rustc_ast::tokenstream::TokenStream {
753    #[inline]
754    fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
755        RefDecodable::decode(d)
756    }
757}
758
759macro_rules! impl_ref_decoder {
760    (<$tcx:tt> $($ty:ty,)*) => {
761        $(impl<'a, $tcx> Decodable<CacheDecoder<'a, $tcx>> for &$tcx [$ty] {
762            #[inline]
763            fn decode(d: &mut CacheDecoder<'a, $tcx>) -> Self {
764                RefDecodable::decode(d)
765            }
766        })*
767    };
768}
769
770impl<'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>
771    Span,
772    rustc_hir::Attribute,
773    rustc_span::Ident,
774    ty::Variance,
775    rustc_span::def_id::DefId,
776    rustc_span::def_id::LocalDefId,
777    (rustc_middle::middle::exported_symbols::ExportedSymbol<'tcx>, rustc_middle::middle::exported_symbols::SymbolExportInfo),
778    rustc_middle::middle::deduced_param_attrs::DeducedParamAttrs,
779}
780
781//- ENCODING -------------------------------------------------------------------
782
783/// An encoder that can write to the incremental compilation cache.
784pub struct CacheEncoder<'a, 'tcx> {
785    tcx: TyCtxt<'tcx>,
786    encoder: FileEncoder<'static>,
787    type_shorthands: FxHashMap<Ty<'tcx>, usize>,
788    predicate_shorthands: FxHashMap<ty::PredicateKind<'tcx>, usize>,
789    interpret_allocs: FxIndexSet<interpret::AllocId>,
790    caching_source_map_view: CachingSourceMapView<'tcx>,
791    file_to_file_index: FxHashMap<*const SourceFile, SourceFileIndex>,
792    hygiene_context: &'a HygieneEncodeContext,
793    // Used for both `Symbol`s and `ByteSymbol`s.
794    symbol_index_table: FxHashMap<u32, usize>,
795
796    query_values_index: Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>,
797    side_effects_index: Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>,
798}
799
800impl<'a, 'tcx> fmt::Debug for CacheEncoder<'a, 'tcx> {
801    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
802        // Add more details here if/when necessary.
803        f.write_str("CacheEncoder")
804    }
805}
806
807impl<'a, 'tcx> CacheEncoder<'a, 'tcx> {
808    #[inline]
809    fn source_file_index(&mut self, source_file: Arc<SourceFile>) -> SourceFileIndex {
810        self.file_to_file_index[&(&raw const *source_file)]
811    }
812
813    /// Encode something with additional information that allows to do some
814    /// sanity checks when decoding the data again. This method will first
815    /// encode the specified tag, then the given value, then the number of
816    /// bytes taken up by tag and value. On decoding, we can then verify that
817    /// we get the expected tag and read the expected number of bytes.
818    fn encode_tagged<T: Encodable<Self>, V: Encodable<Self>>(&mut self, tag: T, value: &V) {
819        let start_pos = self.position();
820
821        tag.encode(self);
822        value.encode(self);
823
824        let end_pos = self.position();
825        ((end_pos - start_pos) as u64).encode(self);
826    }
827
828    pub fn encode_query_value<V: Encodable<Self>>(&mut self, index: DepNodeIndex, value: &V) {
829        let index = SerializedDepNodeIndex::from_curr_for_serialization(index);
830
831        self.query_values_index.push((index, AbsoluteBytePos::new(self.position())));
832        self.encode_tagged(index, value);
833    }
834
835    fn encode_side_effect(&mut self, index: DepNodeIndex, side_effect: &QuerySideEffect) {
836        let index = SerializedDepNodeIndex::from_curr_for_serialization(index);
837
838        self.side_effects_index.push((index, AbsoluteBytePos::new(self.position())));
839        self.encode_tagged(index, side_effect);
840    }
841
842    // copy&paste impl from rustc_metadata
843    fn encode_symbol_or_byte_symbol(
844        &mut self,
845        index: u32,
846        emit_str_or_byte_str: impl Fn(&mut Self),
847    ) {
848        // if symbol/byte symbol is predefined, emit tag and symbol index
849        if Symbol::is_predefined(index) {
850            self.encoder.emit_u8(SYMBOL_PREDEFINED);
851            self.encoder.emit_u32(index);
852        } else {
853            // otherwise write it as string or as offset to it
854            match self.symbol_index_table.entry(index) {
855                Entry::Vacant(o) => {
856                    self.encoder.emit_u8(SYMBOL_STR);
857                    let pos = self.encoder.position();
858                    o.insert(pos);
859                    emit_str_or_byte_str(self);
860                }
861                Entry::Occupied(o) => {
862                    let x = *o.get();
863                    self.emit_u8(SYMBOL_OFFSET);
864                    self.emit_usize(x);
865                }
866            }
867        }
868    }
869
870    #[inline]
871    fn finish(mut self) -> FileEncodeResult {
872        self.encoder.finish()
873    }
874}
875
876impl<'a, 'tcx> SpanEncoder for CacheEncoder<'a, 'tcx> {
877    fn encode_syntax_context(&mut self, syntax_context: SyntaxContext) {
878        rustc_span::hygiene::raw_encode_syntax_context(syntax_context, self.hygiene_context, self);
879    }
880
881    fn encode_expn_id(&mut self, expn_id: ExpnId) {
882        self.hygiene_context.schedule_expn_data_for_encoding(expn_id);
883        expn_id.expn_hash().encode(self);
884    }
885
886    fn encode_span(&mut self, span: Span) {
887        let span_data = span.data_untracked();
888        span_data.ctxt.encode(self);
889        span_data.parent.encode(self);
890
891        if span_data.is_dummy() {
892            return TAG_PARTIAL_SPAN.encode(self);
893        }
894
895        let parent =
896            span_data.parent.map(|parent| self.tcx.source_span_untracked(parent).data_untracked());
897        if let Some(parent) = parent
898            && parent.contains(span_data)
899        {
900            TAG_RELATIVE_SPAN.encode(self);
901            (span_data.lo.0.wrapping_sub(parent.lo.0)).encode(self);
902            (span_data.hi.0.wrapping_sub(parent.lo.0)).encode(self);
903            return;
904        }
905
906        let Some((file_lo, line_lo, col_lo)) =
907            self.caching_source_map_view.byte_pos_to_line_and_col(span_data.lo)
908        else {
909            return TAG_PARTIAL_SPAN.encode(self);
910        };
911
912        if let Some(parent) = parent
913            && file_lo.contains(parent.lo)
914        {
915            TAG_RELATIVE_SPAN.encode(self);
916            (span_data.lo.0.wrapping_sub(parent.lo.0)).encode(self);
917            (span_data.hi.0.wrapping_sub(parent.lo.0)).encode(self);
918            return;
919        }
920
921        let len = span_data.hi - span_data.lo;
922        let source_file_index = self.source_file_index(file_lo);
923
924        TAG_FULL_SPAN.encode(self);
925        source_file_index.encode(self);
926        line_lo.encode(self);
927        col_lo.encode(self);
928        len.encode(self);
929    }
930
931    fn encode_symbol(&mut self, sym: Symbol) {
932        self.encode_symbol_or_byte_symbol(sym.as_u32(), |this| this.emit_str(sym.as_str()));
933    }
934
935    fn encode_byte_symbol(&mut self, byte_sym: ByteSymbol) {
936        self.encode_symbol_or_byte_symbol(byte_sym.as_u32(), |this| {
937            this.emit_byte_str(byte_sym.as_byte_str())
938        });
939    }
940
941    fn encode_crate_num(&mut self, crate_num: CrateNum) {
942        self.tcx.stable_crate_id(crate_num).encode(self);
943    }
944
945    fn encode_def_id(&mut self, def_id: DefId) {
946        self.tcx.def_path_hash(def_id).encode(self);
947    }
948
949    fn encode_def_index(&mut self, _def_index: DefIndex) {
950        crate::util::bug::bug_fmt(format_args!("encoding `DefIndex` without context"));bug!("encoding `DefIndex` without context");
951    }
952}
953
954impl<'a, 'tcx> TyEncoder<'tcx> for CacheEncoder<'a, 'tcx> {
955    const CLEAR_CROSS_CRATE: bool = false;
956
957    #[inline]
958    fn position(&self) -> usize {
959        self.encoder.position()
960    }
961    #[inline]
962    fn type_shorthands(&mut self) -> &mut FxHashMap<Ty<'tcx>, usize> {
963        &mut self.type_shorthands
964    }
965    #[inline]
966    fn predicate_shorthands(&mut self) -> &mut FxHashMap<ty::PredicateKind<'tcx>, usize> {
967        &mut self.predicate_shorthands
968    }
969    #[inline]
970    fn encode_alloc_id(&mut self, alloc_id: &interpret::AllocId) {
971        let (index, _) = self.interpret_allocs.insert_full(*alloc_id);
972
973        index.encode(self);
974    }
975}
976
977macro_rules! encoder_methods {
978    ($($name:ident($ty:ty);)*) => {
979        #[inline]
980        $(fn $name(&mut self, value: $ty) {
981            self.encoder.$name(value)
982        })*
983    }
984}
985
986impl<'a, 'tcx> Encoder for CacheEncoder<'a, 'tcx> {
987    fn emit_raw_bytes(&mut self, value: &[u8]) {
    self.encoder.emit_raw_bytes(value)
}encoder_methods! {
988        emit_usize(usize);
989        emit_u128(u128);
990        emit_u64(u64);
991        emit_u32(u32);
992        emit_u16(u16);
993        emit_u8(u8);
994
995        emit_isize(isize);
996        emit_i128(i128);
997        emit_i64(i64);
998        emit_i32(i32);
999        emit_i16(i16);
1000
1001        emit_raw_bytes(&[u8]);
1002    }
1003}
1004
1005// This ensures that the `Encodable<opaque::FileEncoder>::encode` specialization for byte slices
1006// is used when a `CacheEncoder` having an `opaque::FileEncoder` is passed to `Encodable::encode`.
1007// Unfortunately, we have to manually opt into specializations this way, given how `CacheEncoder`
1008// and the encoding traits currently work.
1009impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for [u8] {
1010    fn encode(&self, e: &mut CacheEncoder<'a, 'tcx>) {
1011        self.encode(&mut e.encoder);
1012    }
1013}