rustc_metadata/rmeta/
decoder.rs

1// Decoding metadata from a single crate's metadata
2
3use std::iter::TrustedLen;
4use std::path::{Path, PathBuf};
5use std::sync::{Arc, OnceLock};
6use std::{io, mem};
7
8pub(super) use cstore_impl::provide;
9use rustc_ast as ast;
10use rustc_data_structures::fingerprint::Fingerprint;
11use rustc_data_structures::fx::FxIndexMap;
12use rustc_data_structures::owned_slice::OwnedSlice;
13use rustc_data_structures::sync::Lock;
14use rustc_data_structures::unhash::UnhashMap;
15use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind};
16use rustc_expand::proc_macro::{AttrProcMacro, BangProcMacro, DeriveProcMacro};
17use rustc_hir::Safety;
18use rustc_hir::def::Res;
19use rustc_hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE};
20use rustc_hir::definitions::{DefPath, DefPathData};
21use rustc_hir::diagnostic_items::DiagnosticItems;
22use rustc_index::Idx;
23use rustc_middle::middle::lib_features::LibFeatures;
24use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState};
25use rustc_middle::ty::Visibility;
26use rustc_middle::ty::codec::TyDecoder;
27use rustc_middle::{bug, implement_ty_decoder};
28use rustc_proc_macro::bridge::client::ProcMacro;
29use rustc_serialize::opaque::MemDecoder;
30use rustc_serialize::{Decodable, Decoder};
31use rustc_session::Session;
32use rustc_session::config::TargetModifier;
33use rustc_session::cstore::{CrateSource, ExternCrate};
34use rustc_span::hygiene::HygieneDecodeContext;
35use rustc_span::{
36    BytePos, ByteSymbol, DUMMY_SP, Pos, SpanData, SpanDecoder, Symbol, SyntaxContext, kw,
37};
38use tracing::debug;
39
40use crate::creader::CStore;
41use crate::rmeta::table::IsDefault;
42use crate::rmeta::*;
43
44mod cstore_impl;
45
46/// A reference to the raw binary version of crate metadata.
47/// This struct applies [`MemDecoder`]'s validation when constructed
48/// so that later constructions are guaranteed to succeed.
49pub(crate) struct MetadataBlob(OwnedSlice);
50
51impl std::ops::Deref for MetadataBlob {
52    type Target = [u8];
53
54    #[inline]
55    fn deref(&self) -> &[u8] {
56        &self.0[..]
57    }
58}
59
60impl MetadataBlob {
61    /// Runs the [`MemDecoder`] validation and if it passes, constructs a new [`MetadataBlob`].
62    pub(crate) fn new(slice: OwnedSlice) -> Result<Self, ()> {
63        if MemDecoder::new(&slice, 0).is_ok() { Ok(Self(slice)) } else { Err(()) }
64    }
65
66    /// Since this has passed the validation of [`MetadataBlob::new`], this returns bytes which are
67    /// known to pass the [`MemDecoder`] validation.
68    pub(crate) fn bytes(&self) -> &OwnedSlice {
69        &self.0
70    }
71}
72
73/// A map from external crate numbers (as decoded from some crate file) to
74/// local crate numbers (as generated during this session). Each external
75/// crate may refer to types in other external crates, and each has their
76/// own crate numbers.
77pub(crate) type CrateNumMap = IndexVec<CrateNum, CrateNum>;
78
79/// Target modifiers - abi or exploit mitigations flags
80pub(crate) type TargetModifiers = Vec<TargetModifier>;
81
82pub(crate) struct CrateMetadata {
83    /// The primary crate data - binary metadata blob.
84    blob: MetadataBlob,
85
86    // --- Some data pre-decoded from the metadata blob, usually for performance ---
87    /// Data about the top-level items in a crate, as well as various crate-level metadata.
88    root: CrateRoot,
89    /// Trait impl data.
90    /// FIXME: Used only from queries and can use query cache,
91    /// so pre-decoding can probably be avoided.
92    trait_impls: FxIndexMap<(u32, DefIndex), LazyArray<(DefIndex, Option<SimplifiedType>)>>,
93    /// Inherent impls which do not follow the normal coherence rules.
94    ///
95    /// These can be introduced using either `#![rustc_coherence_is_core]`
96    /// or `#[rustc_allow_incoherent_impl]`.
97    incoherent_impls: FxIndexMap<SimplifiedType, LazyArray<DefIndex>>,
98    /// Proc macro descriptions for this crate, if it's a proc macro crate.
99    raw_proc_macros: Option<&'static [ProcMacro]>,
100    /// Source maps for code from the crate.
101    source_map_import_info: Lock<Vec<Option<ImportedSourceFile>>>,
102    /// For every definition in this crate, maps its `DefPathHash` to its `DefIndex`.
103    def_path_hash_map: DefPathHashMapRef<'static>,
104    /// Likewise for ExpnHash.
105    expn_hash_map: OnceLock<UnhashMap<ExpnHash, ExpnIndex>>,
106    /// Used for decoding interpret::AllocIds in a cached & thread-safe manner.
107    alloc_decoding_state: AllocDecodingState,
108    /// Caches decoded `DefKey`s.
109    def_key_cache: Lock<FxHashMap<DefIndex, DefKey>>,
110
111    // --- Other significant crate properties ---
112    /// ID of this crate, from the current compilation session's point of view.
113    cnum: CrateNum,
114    /// Maps crate IDs as they are were seen from this crate's compilation sessions into
115    /// IDs as they are seen from the current compilation session.
116    cnum_map: CrateNumMap,
117    /// Same ID set as `cnum_map` plus maybe some injected crates like panic runtime.
118    dependencies: Vec<CrateNum>,
119    /// How to link (or not link) this crate to the currently compiled crate.
120    dep_kind: CrateDepKind,
121    /// Filesystem location of this crate.
122    source: Arc<CrateSource>,
123    /// Whether or not this crate should be consider a private dependency.
124    /// Used by the 'exported_private_dependencies' lint, and for determining
125    /// whether to emit suggestions that reference this crate.
126    private_dep: bool,
127    /// The hash for the host proc macro. Used to support `-Z dual-proc-macro`.
128    host_hash: Option<Svh>,
129    /// The crate was used non-speculatively.
130    used: bool,
131
132    /// Additional data used for decoding `HygieneData` (e.g. `SyntaxContext`
133    /// and `ExpnId`).
134    /// Note that we store a `HygieneDecodeContext` for each `CrateMetadata`. This is
135    /// because `SyntaxContext` ids are not globally unique, so we need
136    /// to track which ids we've decoded on a per-crate basis.
137    hygiene_context: HygieneDecodeContext,
138
139    // --- Data used only for improving diagnostics ---
140    /// Information about the `extern crate` item or path that caused this crate to be loaded.
141    /// If this is `None`, then the crate was injected (e.g., by the allocator).
142    extern_crate: Option<ExternCrate>,
143}
144
145/// Holds information about a rustc_span::SourceFile imported from another crate.
146/// See `imported_source_file()` for more information.
147#[derive(Clone)]
148struct ImportedSourceFile {
149    /// This SourceFile's byte-offset within the source_map of its original crate
150    original_start_pos: rustc_span::BytePos,
151    /// The end of this SourceFile within the source_map of its original crate
152    original_end_pos: rustc_span::BytePos,
153    /// The imported SourceFile's representation within the local source_map
154    translated_source_file: Arc<rustc_span::SourceFile>,
155}
156
157pub(super) struct DecodeContext<'a, 'tcx> {
158    opaque: MemDecoder<'a>,
159    cdata: Option<CrateMetadataRef<'a>>,
160    blob: &'a MetadataBlob,
161    sess: Option<&'tcx Session>,
162    tcx: Option<TyCtxt<'tcx>>,
163
164    lazy_state: LazyState,
165
166    // Used for decoding interpret::AllocIds in a cached & thread-safe manner.
167    alloc_decoding_session: Option<AllocDecodingSession<'a>>,
168}
169
170/// Abstract over the various ways one can create metadata decoders.
171pub(super) trait Metadata<'a, 'tcx>: Copy {
172    fn blob(self) -> &'a MetadataBlob;
173
174    fn cdata(self) -> Option<CrateMetadataRef<'a>> {
175        None
176    }
177    fn sess(self) -> Option<&'tcx Session> {
178        None
179    }
180    fn tcx(self) -> Option<TyCtxt<'tcx>> {
181        None
182    }
183
184    fn decoder(self, pos: usize) -> DecodeContext<'a, 'tcx> {
185        let tcx = self.tcx();
186        DecodeContext {
187            // FIXME: This unwrap should never panic because we check that it won't when creating
188            // `MetadataBlob`. Ideally we'd just have a `MetadataDecoder` and hand out subslices of
189            // it as we do elsewhere in the compiler using `MetadataDecoder::split_at`. But we own
190            // the data for the decoder so holding onto the `MemDecoder` too would make us a
191            // self-referential struct which is downright goofy because `MetadataBlob` is already
192            // self-referential. Probably `MemDecoder` should contain an `OwnedSlice`, but that
193            // demands a significant refactoring due to our crate graph.
194            opaque: MemDecoder::new(self.blob(), pos).unwrap(),
195            cdata: self.cdata(),
196            blob: self.blob(),
197            sess: self.sess().or(tcx.map(|tcx| tcx.sess)),
198            tcx,
199            lazy_state: LazyState::NoNode,
200            alloc_decoding_session: self
201                .cdata()
202                .map(|cdata| cdata.cdata.alloc_decoding_state.new_decoding_session()),
203        }
204    }
205}
206
207impl<'a, 'tcx> Metadata<'a, 'tcx> for &'a MetadataBlob {
208    #[inline]
209    fn blob(self) -> &'a MetadataBlob {
210        self
211    }
212}
213
214impl<'a, 'tcx> Metadata<'a, 'tcx> for CrateMetadataRef<'a> {
215    #[inline]
216    fn blob(self) -> &'a MetadataBlob {
217        &self.cdata.blob
218    }
219    #[inline]
220    fn cdata(self) -> Option<CrateMetadataRef<'a>> {
221        Some(self)
222    }
223}
224
225impl<'a, 'tcx> Metadata<'a, 'tcx> for (CrateMetadataRef<'a>, &'tcx Session) {
226    #[inline]
227    fn blob(self) -> &'a MetadataBlob {
228        &self.0.cdata.blob
229    }
230    #[inline]
231    fn cdata(self) -> Option<CrateMetadataRef<'a>> {
232        Some(self.0)
233    }
234    #[inline]
235    fn sess(self) -> Option<&'tcx Session> {
236        Some(self.1)
237    }
238}
239
240impl<'a, 'tcx> Metadata<'a, 'tcx> for (CrateMetadataRef<'a>, TyCtxt<'tcx>) {
241    #[inline]
242    fn blob(self) -> &'a MetadataBlob {
243        &self.0.cdata.blob
244    }
245    #[inline]
246    fn cdata(self) -> Option<CrateMetadataRef<'a>> {
247        Some(self.0)
248    }
249    #[inline]
250    fn tcx(self) -> Option<TyCtxt<'tcx>> {
251        Some(self.1)
252    }
253}
254
255impl<T: ParameterizedOverTcx> LazyValue<T> {
256    #[inline]
257    fn decode<'a, 'tcx, M: Metadata<'a, 'tcx>>(self, metadata: M) -> T::Value<'tcx>
258    where
259        T::Value<'tcx>: Decodable<DecodeContext<'a, 'tcx>>,
260    {
261        let mut dcx = metadata.decoder(self.position.get());
262        dcx.lazy_state = LazyState::NodeStart(self.position);
263        T::Value::decode(&mut dcx)
264    }
265}
266
267struct DecodeIterator<'a, 'tcx, T> {
268    elem_counter: std::ops::Range<usize>,
269    dcx: DecodeContext<'a, 'tcx>,
270    _phantom: PhantomData<fn() -> T>,
271}
272
273impl<'a, 'tcx, T: Decodable<DecodeContext<'a, 'tcx>>> Iterator for DecodeIterator<'a, 'tcx, T> {
274    type Item = T;
275
276    #[inline(always)]
277    fn next(&mut self) -> Option<Self::Item> {
278        self.elem_counter.next().map(|_| T::decode(&mut self.dcx))
279    }
280
281    #[inline(always)]
282    fn size_hint(&self) -> (usize, Option<usize>) {
283        self.elem_counter.size_hint()
284    }
285}
286
287impl<'a, 'tcx, T: Decodable<DecodeContext<'a, 'tcx>>> ExactSizeIterator
288    for DecodeIterator<'a, 'tcx, T>
289{
290    fn len(&self) -> usize {
291        self.elem_counter.len()
292    }
293}
294
295unsafe impl<'a, 'tcx, T: Decodable<DecodeContext<'a, 'tcx>>> TrustedLen
296    for DecodeIterator<'a, 'tcx, T>
297{
298}
299
300impl<T: ParameterizedOverTcx> LazyArray<T> {
301    #[inline]
302    fn decode<'a, 'tcx, M: Metadata<'a, 'tcx>>(
303        self,
304        metadata: M,
305    ) -> DecodeIterator<'a, 'tcx, T::Value<'tcx>>
306    where
307        T::Value<'tcx>: Decodable<DecodeContext<'a, 'tcx>>,
308    {
309        let mut dcx = metadata.decoder(self.position.get());
310        dcx.lazy_state = LazyState::NodeStart(self.position);
311        DecodeIterator { elem_counter: (0..self.num_elems), dcx, _phantom: PhantomData }
312    }
313}
314
315impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
316    #[inline]
317    fn tcx(&self) -> TyCtxt<'tcx> {
318        let Some(tcx) = self.tcx else {
319            bug!(
320                "No TyCtxt found for decoding. \
321                You need to explicitly pass `(crate_metadata_ref, tcx)` to `decode` instead of just `crate_metadata_ref`."
322            );
323        };
324        tcx
325    }
326
327    #[inline]
328    pub(crate) fn blob(&self) -> &'a MetadataBlob {
329        self.blob
330    }
331
332    #[inline]
333    fn cdata(&self) -> CrateMetadataRef<'a> {
334        debug_assert!(self.cdata.is_some(), "missing CrateMetadata in DecodeContext");
335        self.cdata.unwrap()
336    }
337
338    #[inline]
339    fn map_encoded_cnum_to_current(&self, cnum: CrateNum) -> CrateNum {
340        self.cdata().map_encoded_cnum_to_current(cnum)
341    }
342
343    #[inline]
344    fn read_lazy_offset_then<T>(&mut self, f: impl Fn(NonZero<usize>) -> T) -> T {
345        let distance = self.read_usize();
346        let position = match self.lazy_state {
347            LazyState::NoNode => bug!("read_lazy_with_meta: outside of a metadata node"),
348            LazyState::NodeStart(start) => {
349                let start = start.get();
350                assert!(distance <= start);
351                start - distance
352            }
353            LazyState::Previous(last_pos) => last_pos.get() + distance,
354        };
355        let position = NonZero::new(position).unwrap();
356        self.lazy_state = LazyState::Previous(position);
357        f(position)
358    }
359
360    fn read_lazy<T>(&mut self) -> LazyValue<T> {
361        self.read_lazy_offset_then(|pos| LazyValue::from_position(pos))
362    }
363
364    fn read_lazy_array<T>(&mut self, len: usize) -> LazyArray<T> {
365        self.read_lazy_offset_then(|pos| LazyArray::from_position_and_num_elems(pos, len))
366    }
367
368    fn read_lazy_table<I, T>(&mut self, width: usize, len: usize) -> LazyTable<I, T> {
369        self.read_lazy_offset_then(|pos| LazyTable::from_position_and_encoded_size(pos, width, len))
370    }
371
372    #[inline]
373    fn read_raw_bytes(&mut self, len: usize) -> &[u8] {
374        self.opaque.read_raw_bytes(len)
375    }
376
377    fn decode_symbol_or_byte_symbol<S>(
378        &mut self,
379        new_from_index: impl Fn(u32) -> S,
380        read_and_intern_str_or_byte_str_this: impl Fn(&mut Self) -> S,
381        read_and_intern_str_or_byte_str_opaque: impl Fn(&mut MemDecoder<'a>) -> S,
382    ) -> S {
383        let tag = self.read_u8();
384
385        match tag {
386            SYMBOL_STR => read_and_intern_str_or_byte_str_this(self),
387            SYMBOL_OFFSET => {
388                // read str offset
389                let pos = self.read_usize();
390
391                // move to str offset and read
392                self.opaque.with_position(pos, |d| read_and_intern_str_or_byte_str_opaque(d))
393            }
394            SYMBOL_PREDEFINED => new_from_index(self.read_u32()),
395            _ => unreachable!(),
396        }
397    }
398}
399
400impl<'a, 'tcx> TyDecoder<'tcx> for DecodeContext<'a, 'tcx> {
401    const CLEAR_CROSS_CRATE: bool = true;
402
403    #[inline]
404    fn interner(&self) -> TyCtxt<'tcx> {
405        self.tcx()
406    }
407
408    fn cached_ty_for_shorthand<F>(&mut self, shorthand: usize, or_insert_with: F) -> Ty<'tcx>
409    where
410        F: FnOnce(&mut Self) -> Ty<'tcx>,
411    {
412        let tcx = self.tcx();
413
414        let key = ty::CReaderCacheKey { cnum: Some(self.cdata().cnum), pos: shorthand };
415
416        if let Some(&ty) = tcx.ty_rcache.borrow().get(&key) {
417            return ty;
418        }
419
420        let ty = or_insert_with(self);
421        tcx.ty_rcache.borrow_mut().insert(key, ty);
422        ty
423    }
424
425    fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
426    where
427        F: FnOnce(&mut Self) -> R,
428    {
429        let new_opaque = self.opaque.split_at(pos);
430        let old_opaque = mem::replace(&mut self.opaque, new_opaque);
431        let old_state = mem::replace(&mut self.lazy_state, LazyState::NoNode);
432        let r = f(self);
433        self.opaque = old_opaque;
434        self.lazy_state = old_state;
435        r
436    }
437
438    fn decode_alloc_id(&mut self) -> rustc_middle::mir::interpret::AllocId {
439        if let Some(alloc_decoding_session) = self.alloc_decoding_session {
440            alloc_decoding_session.decode_alloc_id(self)
441        } else {
442            bug!("Attempting to decode interpret::AllocId without CrateMetadata")
443        }
444    }
445}
446
447impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for ExpnIndex {
448    #[inline]
449    fn decode(d: &mut DecodeContext<'a, 'tcx>) -> ExpnIndex {
450        ExpnIndex::from_u32(d.read_u32())
451    }
452}
453
454impl<'a, 'tcx> SpanDecoder for DecodeContext<'a, 'tcx> {
455    fn decode_attr_id(&mut self) -> rustc_span::AttrId {
456        let sess = self.sess.expect("can't decode AttrId without Session");
457        sess.psess.attr_id_generator.mk_attr_id()
458    }
459
460    fn decode_crate_num(&mut self) -> CrateNum {
461        let cnum = CrateNum::from_u32(self.read_u32());
462        self.map_encoded_cnum_to_current(cnum)
463    }
464
465    fn decode_def_index(&mut self) -> DefIndex {
466        DefIndex::from_u32(self.read_u32())
467    }
468
469    fn decode_def_id(&mut self) -> DefId {
470        DefId { krate: Decodable::decode(self), index: Decodable::decode(self) }
471    }
472
473    fn decode_syntax_context(&mut self) -> SyntaxContext {
474        let cdata = self.cdata();
475
476        let Some(sess) = self.sess else {
477            bug!(
478                "Cannot decode SyntaxContext without Session.\
479                You need to explicitly pass `(crate_metadata_ref, tcx)` to `decode` instead of just `crate_metadata_ref`."
480            );
481        };
482
483        let cname = cdata.root.name();
484        rustc_span::hygiene::decode_syntax_context(self, &cdata.hygiene_context, |_, id| {
485            debug!("SpecializedDecoder<SyntaxContext>: decoding {}", id);
486            cdata
487                .root
488                .syntax_contexts
489                .get(cdata, id)
490                .unwrap_or_else(|| panic!("Missing SyntaxContext {id:?} for crate {cname:?}"))
491                .decode((cdata, sess))
492        })
493    }
494
495    fn decode_expn_id(&mut self) -> ExpnId {
496        let local_cdata = self.cdata();
497
498        let Some(sess) = self.sess else {
499            bug!(
500                "Cannot decode ExpnId without Session. \
501                You need to explicitly pass `(crate_metadata_ref, tcx)` to `decode` instead of just `crate_metadata_ref`."
502            );
503        };
504
505        let cnum = CrateNum::decode(self);
506        let index = u32::decode(self);
507
508        let expn_id = rustc_span::hygiene::decode_expn_id(cnum, index, |expn_id| {
509            let ExpnId { krate: cnum, local_id: index } = expn_id;
510            // Lookup local `ExpnData`s in our own crate data. Foreign `ExpnData`s
511            // are stored in the owning crate, to avoid duplication.
512            debug_assert_ne!(cnum, LOCAL_CRATE);
513            let crate_data = if cnum == local_cdata.cnum {
514                local_cdata
515            } else {
516                local_cdata.cstore.get_crate_data(cnum)
517            };
518            let expn_data = crate_data
519                .root
520                .expn_data
521                .get(crate_data, index)
522                .unwrap()
523                .decode((crate_data, sess));
524            let expn_hash = crate_data
525                .root
526                .expn_hashes
527                .get(crate_data, index)
528                .unwrap()
529                .decode((crate_data, sess));
530            (expn_data, expn_hash)
531        });
532        expn_id
533    }
534
535    fn decode_span(&mut self) -> Span {
536        let start = self.position();
537        let tag = SpanTag(self.peek_byte());
538        let data = if tag.kind() == SpanKind::Indirect {
539            // Skip past the tag we just peek'd.
540            self.read_u8();
541            // indirect tag lengths are safe to access, since they're (0, 8)
542            let bytes_needed = tag.length().unwrap().0 as usize;
543            let mut total = [0u8; usize::BITS as usize / 8];
544            total[..bytes_needed].copy_from_slice(self.read_raw_bytes(bytes_needed));
545            let offset_or_position = usize::from_le_bytes(total);
546            let position = if tag.is_relative_offset() {
547                start - offset_or_position
548            } else {
549                offset_or_position
550            };
551            self.with_position(position, SpanData::decode)
552        } else {
553            SpanData::decode(self)
554        };
555        data.span()
556    }
557
558    fn decode_symbol(&mut self) -> Symbol {
559        self.decode_symbol_or_byte_symbol(
560            Symbol::new,
561            |this| Symbol::intern(this.read_str()),
562            |opaque| Symbol::intern(opaque.read_str()),
563        )
564    }
565
566    fn decode_byte_symbol(&mut self) -> ByteSymbol {
567        self.decode_symbol_or_byte_symbol(
568            ByteSymbol::new,
569            |this| ByteSymbol::intern(this.read_byte_str()),
570            |opaque| ByteSymbol::intern(opaque.read_byte_str()),
571        )
572    }
573}
574
575impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for SpanData {
576    fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> SpanData {
577        let tag = SpanTag::decode(decoder);
578        let ctxt = tag.context().unwrap_or_else(|| SyntaxContext::decode(decoder));
579
580        if tag.kind() == SpanKind::Partial {
581            return DUMMY_SP.with_ctxt(ctxt).data();
582        }
583
584        debug_assert!(tag.kind() == SpanKind::Local || tag.kind() == SpanKind::Foreign);
585
586        let lo = BytePos::decode(decoder);
587        let len = tag.length().unwrap_or_else(|| BytePos::decode(decoder));
588        let hi = lo + len;
589
590        let Some(sess) = decoder.sess else {
591            bug!(
592                "Cannot decode Span without Session. \
593                You need to explicitly pass `(crate_metadata_ref, tcx)` to `decode` instead of just `crate_metadata_ref`."
594            )
595        };
596
597        // Index of the file in the corresponding crate's list of encoded files.
598        let metadata_index = u32::decode(decoder);
599
600        // There are two possibilities here:
601        // 1. This is a 'local span', which is located inside a `SourceFile`
602        // that came from this crate. In this case, we use the source map data
603        // encoded in this crate. This branch should be taken nearly all of the time.
604        // 2. This is a 'foreign span', which is located inside a `SourceFile`
605        // that came from a *different* crate (some crate upstream of the one
606        // whose metadata we're looking at). For example, consider this dependency graph:
607        //
608        // A -> B -> C
609        //
610        // Suppose that we're currently compiling crate A, and start deserializing
611        // metadata from crate B. When we deserialize a Span from crate B's metadata,
612        // there are two possibilities:
613        //
614        // 1. The span references a file from crate B. This makes it a 'local' span,
615        // which means that we can use crate B's serialized source map information.
616        // 2. The span references a file from crate C. This makes it a 'foreign' span,
617        // which means we need to use Crate *C* (not crate B) to determine the source
618        // map information. We only record source map information for a file in the
619        // crate that 'owns' it, so deserializing a Span may require us to look at
620        // a transitive dependency.
621        //
622        // When we encode a foreign span, we adjust its 'lo' and 'high' values
623        // to be based on the *foreign* crate (e.g. crate C), not the crate
624        // we are writing metadata for (e.g. crate B). This allows us to
625        // treat the 'local' and 'foreign' cases almost identically during deserialization:
626        // we can call `imported_source_file` for the proper crate, and binary search
627        // through the returned slice using our span.
628        let source_file = if tag.kind() == SpanKind::Local {
629            decoder.cdata().imported_source_file(metadata_index, sess)
630        } else {
631            // When we encode a proc-macro crate, all `Span`s should be encoded
632            // with `TAG_VALID_SPAN_LOCAL`
633            if decoder.cdata().root.is_proc_macro_crate() {
634                // Decode `CrateNum` as u32 - using `CrateNum::decode` will ICE
635                // since we don't have `cnum_map` populated.
636                let cnum = u32::decode(decoder);
637                panic!(
638                    "Decoding of crate {:?} tried to access proc-macro dep {:?}",
639                    decoder.cdata().root.header.name,
640                    cnum
641                );
642            }
643            // tag is TAG_VALID_SPAN_FOREIGN, checked by `debug_assert` above
644            let cnum = CrateNum::decode(decoder);
645            debug!(
646                "SpecializedDecoder<Span>::specialized_decode: loading source files from cnum {:?}",
647                cnum
648            );
649
650            let foreign_data = decoder.cdata().cstore.get_crate_data(cnum);
651            foreign_data.imported_source_file(metadata_index, sess)
652        };
653
654        // Make sure our span is well-formed.
655        debug_assert!(
656            lo + source_file.original_start_pos <= source_file.original_end_pos,
657            "Malformed encoded span: lo={:?} source_file.original_start_pos={:?} source_file.original_end_pos={:?}",
658            lo,
659            source_file.original_start_pos,
660            source_file.original_end_pos
661        );
662
663        // Make sure we correctly filtered out invalid spans during encoding.
664        debug_assert!(
665            hi + source_file.original_start_pos <= source_file.original_end_pos,
666            "Malformed encoded span: hi={:?} source_file.original_start_pos={:?} source_file.original_end_pos={:?}",
667            hi,
668            source_file.original_start_pos,
669            source_file.original_end_pos
670        );
671
672        let lo = lo + source_file.translated_source_file.start_pos;
673        let hi = hi + source_file.translated_source_file.start_pos;
674
675        // Do not try to decode parent for foreign spans (it wasn't encoded in the first place).
676        SpanData { lo, hi, ctxt, parent: None }
677    }
678}
679
680impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for &'tcx [(ty::Clause<'tcx>, Span)] {
681    fn decode(d: &mut DecodeContext<'a, 'tcx>) -> Self {
682        ty::codec::RefDecodable::decode(d)
683    }
684}
685
686impl<'a, 'tcx, T> Decodable<DecodeContext<'a, 'tcx>> for LazyValue<T> {
687    fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> Self {
688        decoder.read_lazy()
689    }
690}
691
692impl<'a, 'tcx, T> Decodable<DecodeContext<'a, 'tcx>> for LazyArray<T> {
693    #[inline]
694    fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> Self {
695        let len = decoder.read_usize();
696        if len == 0 { LazyArray::default() } else { decoder.read_lazy_array(len) }
697    }
698}
699
700impl<'a, 'tcx, I: Idx, T> Decodable<DecodeContext<'a, 'tcx>> for LazyTable<I, T> {
701    fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> Self {
702        let width = decoder.read_usize();
703        let len = decoder.read_usize();
704        decoder.read_lazy_table(width, len)
705    }
706}
707
708implement_ty_decoder!(DecodeContext<'a, 'tcx>);
709
710impl MetadataBlob {
711    pub(crate) fn check_compatibility(
712        &self,
713        cfg_version: &'static str,
714    ) -> Result<(), Option<String>> {
715        if !self.blob().starts_with(METADATA_HEADER) {
716            if self.blob().starts_with(b"rust") {
717                return Err(Some("<unknown rustc version>".to_owned()));
718            }
719            return Err(None);
720        }
721
722        let found_version =
723            LazyValue::<String>::from_position(NonZero::new(METADATA_HEADER.len() + 8).unwrap())
724                .decode(self);
725        if rustc_version(cfg_version) != found_version {
726            return Err(Some(found_version));
727        }
728
729        Ok(())
730    }
731
732    fn root_pos(&self) -> NonZero<usize> {
733        let offset = METADATA_HEADER.len();
734        let pos_bytes = self.blob()[offset..][..8].try_into().unwrap();
735        let pos = u64::from_le_bytes(pos_bytes);
736        NonZero::new(pos as usize).unwrap()
737    }
738
739    pub(crate) fn get_header(&self) -> CrateHeader {
740        let pos = self.root_pos();
741        LazyValue::<CrateHeader>::from_position(pos).decode(self)
742    }
743
744    pub(crate) fn get_root(&self) -> CrateRoot {
745        let pos = self.root_pos();
746        LazyValue::<CrateRoot>::from_position(pos).decode(self)
747    }
748
749    pub(crate) fn list_crate_metadata(
750        &self,
751        out: &mut dyn io::Write,
752        ls_kinds: &[String],
753    ) -> io::Result<()> {
754        let root = self.get_root();
755
756        let all_ls_kinds = vec![
757            "root".to_owned(),
758            "lang_items".to_owned(),
759            "features".to_owned(),
760            "items".to_owned(),
761        ];
762        let ls_kinds = if ls_kinds.contains(&"all".to_owned()) { &all_ls_kinds } else { ls_kinds };
763
764        for kind in ls_kinds {
765            match &**kind {
766                "root" => {
767                    writeln!(out, "Crate info:")?;
768                    writeln!(out, "name {}{}", root.name(), root.extra_filename)?;
769                    writeln!(
770                        out,
771                        "hash {} stable_crate_id {:?}",
772                        root.hash(),
773                        root.stable_crate_id
774                    )?;
775                    writeln!(out, "proc_macro {:?}", root.proc_macro_data.is_some())?;
776                    writeln!(out, "triple {}", root.header.triple.tuple())?;
777                    writeln!(out, "edition {}", root.edition)?;
778                    writeln!(out, "symbol_mangling_version {:?}", root.symbol_mangling_version)?;
779                    writeln!(
780                        out,
781                        "required_panic_strategy {:?} panic_in_drop_strategy {:?}",
782                        root.required_panic_strategy, root.panic_in_drop_strategy
783                    )?;
784                    writeln!(
785                        out,
786                        "has_global_allocator {} has_alloc_error_handler {} has_panic_handler {} has_default_lib_allocator {}",
787                        root.has_global_allocator,
788                        root.has_alloc_error_handler,
789                        root.has_panic_handler,
790                        root.has_default_lib_allocator
791                    )?;
792                    writeln!(
793                        out,
794                        "compiler_builtins {} needs_allocator {} needs_panic_runtime {} no_builtins {} panic_runtime {} profiler_runtime {}",
795                        root.compiler_builtins,
796                        root.needs_allocator,
797                        root.needs_panic_runtime,
798                        root.no_builtins,
799                        root.panic_runtime,
800                        root.profiler_runtime
801                    )?;
802
803                    writeln!(out, "=External Dependencies=")?;
804                    let dylib_dependency_formats =
805                        root.dylib_dependency_formats.decode(self).collect::<Vec<_>>();
806                    for (i, dep) in root.crate_deps.decode(self).enumerate() {
807                        let CrateDep { name, extra_filename, hash, host_hash, kind, is_private } =
808                            dep;
809                        let number = i + 1;
810
811                        writeln!(
812                            out,
813                            "{number} {name}{extra_filename} hash {hash} host_hash {host_hash:?} kind {kind:?} {privacy}{linkage}",
814                            privacy = if is_private { "private" } else { "public" },
815                            linkage = if dylib_dependency_formats.is_empty() {
816                                String::new()
817                            } else {
818                                format!(" linkage {:?}", dylib_dependency_formats[i])
819                            }
820                        )?;
821                    }
822                    write!(out, "\n")?;
823                }
824
825                "lang_items" => {
826                    writeln!(out, "=Lang items=")?;
827                    for (id, lang_item) in root.lang_items.decode(self) {
828                        writeln!(
829                            out,
830                            "{} = crate{}",
831                            lang_item.name(),
832                            DefPath::make(LOCAL_CRATE, id, |parent| root
833                                .tables
834                                .def_keys
835                                .get(self, parent)
836                                .unwrap()
837                                .decode(self))
838                            .to_string_no_crate_verbose()
839                        )?;
840                    }
841                    for lang_item in root.lang_items_missing.decode(self) {
842                        writeln!(out, "{} = <missing>", lang_item.name())?;
843                    }
844                    write!(out, "\n")?;
845                }
846
847                "features" => {
848                    writeln!(out, "=Lib features=")?;
849                    for (feature, since) in root.lib_features.decode(self) {
850                        writeln!(
851                            out,
852                            "{}{}",
853                            feature,
854                            if let FeatureStability::AcceptedSince(since) = since {
855                                format!(" since {since}")
856                            } else {
857                                String::new()
858                            }
859                        )?;
860                    }
861                    write!(out, "\n")?;
862                }
863
864                "items" => {
865                    writeln!(out, "=Items=")?;
866
867                    fn print_item(
868                        blob: &MetadataBlob,
869                        out: &mut dyn io::Write,
870                        item: DefIndex,
871                        indent: usize,
872                    ) -> io::Result<()> {
873                        let root = blob.get_root();
874
875                        let def_kind = root.tables.def_kind.get(blob, item).unwrap();
876                        let def_key = root.tables.def_keys.get(blob, item).unwrap().decode(blob);
877                        #[allow(rustc::symbol_intern_string_literal)]
878                        let def_name = if item == CRATE_DEF_INDEX {
879                            kw::Crate
880                        } else {
881                            def_key
882                                .disambiguated_data
883                                .data
884                                .get_opt_name()
885                                .unwrap_or_else(|| Symbol::intern("???"))
886                        };
887                        let visibility =
888                            root.tables.visibility.get(blob, item).unwrap().decode(blob).map_id(
889                                |index| {
890                                    format!(
891                                        "crate{}",
892                                        DefPath::make(LOCAL_CRATE, index, |parent| root
893                                            .tables
894                                            .def_keys
895                                            .get(blob, parent)
896                                            .unwrap()
897                                            .decode(blob))
898                                        .to_string_no_crate_verbose()
899                                    )
900                                },
901                            );
902                        write!(
903                            out,
904                            "{nil: <indent$}{:?} {:?} {} {{",
905                            visibility,
906                            def_kind,
907                            def_name,
908                            nil = "",
909                        )?;
910
911                        if let Some(children) =
912                            root.tables.module_children_non_reexports.get(blob, item)
913                        {
914                            write!(out, "\n")?;
915                            for child in children.decode(blob) {
916                                print_item(blob, out, child, indent + 4)?;
917                            }
918                            writeln!(out, "{nil: <indent$}}}", nil = "")?;
919                        } else {
920                            writeln!(out, "}}")?;
921                        }
922
923                        Ok(())
924                    }
925
926                    print_item(self, out, CRATE_DEF_INDEX, 0)?;
927
928                    write!(out, "\n")?;
929                }
930
931                _ => {
932                    writeln!(
933                        out,
934                        "unknown -Zls kind. allowed values are: all, root, lang_items, features, items"
935                    )?;
936                }
937            }
938        }
939
940        Ok(())
941    }
942}
943
944impl CrateRoot {
945    pub(crate) fn is_proc_macro_crate(&self) -> bool {
946        self.proc_macro_data.is_some()
947    }
948
949    pub(crate) fn name(&self) -> Symbol {
950        self.header.name
951    }
952
953    pub(crate) fn hash(&self) -> Svh {
954        self.header.hash
955    }
956
957    pub(crate) fn stable_crate_id(&self) -> StableCrateId {
958        self.stable_crate_id
959    }
960
961    pub(crate) fn decode_crate_deps<'a>(
962        &self,
963        metadata: &'a MetadataBlob,
964    ) -> impl ExactSizeIterator<Item = CrateDep> {
965        self.crate_deps.decode(metadata)
966    }
967
968    pub(crate) fn decode_target_modifiers<'a>(
969        &self,
970        metadata: &'a MetadataBlob,
971    ) -> impl ExactSizeIterator<Item = TargetModifier> {
972        self.target_modifiers.decode(metadata)
973    }
974}
975
976impl<'a> CrateMetadataRef<'a> {
977    fn missing(self, descr: &str, id: DefIndex) -> ! {
978        bug!("missing `{descr}` for {:?}", self.local_def_id(id))
979    }
980
981    fn raw_proc_macro(self, id: DefIndex) -> &'a ProcMacro {
982        // DefIndex's in root.proc_macro_data have a one-to-one correspondence
983        // with items in 'raw_proc_macros'.
984        let pos = self
985            .root
986            .proc_macro_data
987            .as_ref()
988            .unwrap()
989            .macros
990            .decode(self)
991            .position(|i| i == id)
992            .unwrap();
993        &self.raw_proc_macros.unwrap()[pos]
994    }
995
996    fn opt_item_name(self, item_index: DefIndex) -> Option<Symbol> {
997        let def_key = self.def_key(item_index);
998        def_key.disambiguated_data.data.get_opt_name().or_else(|| {
999            if def_key.disambiguated_data.data == DefPathData::Ctor {
1000                let parent_index = def_key.parent.expect("no parent for a constructor");
1001                self.def_key(parent_index).disambiguated_data.data.get_opt_name()
1002            } else {
1003                None
1004            }
1005        })
1006    }
1007
1008    fn item_name(self, item_index: DefIndex) -> Symbol {
1009        self.opt_item_name(item_index).expect("no encoded ident for item")
1010    }
1011
1012    fn opt_item_ident(self, item_index: DefIndex, sess: &Session) -> Option<Ident> {
1013        let name = self.opt_item_name(item_index)?;
1014        let span = self
1015            .root
1016            .tables
1017            .def_ident_span
1018            .get(self, item_index)
1019            .unwrap_or_else(|| self.missing("def_ident_span", item_index))
1020            .decode((self, sess));
1021        Some(Ident::new(name, span))
1022    }
1023
1024    fn item_ident(self, item_index: DefIndex, sess: &Session) -> Ident {
1025        self.opt_item_ident(item_index, sess).expect("no encoded ident for item")
1026    }
1027
1028    #[inline]
1029    pub(super) fn map_encoded_cnum_to_current(self, cnum: CrateNum) -> CrateNum {
1030        if cnum == LOCAL_CRATE { self.cnum } else { self.cnum_map[cnum] }
1031    }
1032
1033    fn def_kind(self, item_id: DefIndex) -> DefKind {
1034        self.root
1035            .tables
1036            .def_kind
1037            .get(self, item_id)
1038            .unwrap_or_else(|| self.missing("def_kind", item_id))
1039    }
1040
1041    fn get_span(self, index: DefIndex, sess: &Session) -> Span {
1042        self.root
1043            .tables
1044            .def_span
1045            .get(self, index)
1046            .unwrap_or_else(|| self.missing("def_span", index))
1047            .decode((self, sess))
1048    }
1049
1050    fn load_proc_macro<'tcx>(self, id: DefIndex, tcx: TyCtxt<'tcx>) -> SyntaxExtension {
1051        let (name, kind, helper_attrs) = match *self.raw_proc_macro(id) {
1052            ProcMacro::CustomDerive { trait_name, attributes, client } => {
1053                let helper_attrs =
1054                    attributes.iter().cloned().map(Symbol::intern).collect::<Vec<_>>();
1055                (
1056                    trait_name,
1057                    SyntaxExtensionKind::Derive(Arc::new(DeriveProcMacro { client })),
1058                    helper_attrs,
1059                )
1060            }
1061            ProcMacro::Attr { name, client } => {
1062                (name, SyntaxExtensionKind::Attr(Arc::new(AttrProcMacro { client })), Vec::new())
1063            }
1064            ProcMacro::Bang { name, client } => {
1065                (name, SyntaxExtensionKind::Bang(Arc::new(BangProcMacro { client })), Vec::new())
1066            }
1067        };
1068
1069        let sess = tcx.sess;
1070        let attrs: Vec<_> = self.get_item_attrs(id, sess).collect();
1071        SyntaxExtension::new(
1072            sess,
1073            kind,
1074            self.get_span(id, sess),
1075            helper_attrs,
1076            self.root.edition,
1077            Symbol::intern(name),
1078            &attrs,
1079            false,
1080        )
1081    }
1082
1083    fn get_variant(
1084        self,
1085        kind: DefKind,
1086        index: DefIndex,
1087        parent_did: DefId,
1088    ) -> (VariantIdx, ty::VariantDef) {
1089        let adt_kind = match kind {
1090            DefKind::Variant => ty::AdtKind::Enum,
1091            DefKind::Struct => ty::AdtKind::Struct,
1092            DefKind::Union => ty::AdtKind::Union,
1093            _ => bug!(),
1094        };
1095
1096        let data = self.root.tables.variant_data.get(self, index).unwrap().decode(self);
1097
1098        let variant_did =
1099            if adt_kind == ty::AdtKind::Enum { Some(self.local_def_id(index)) } else { None };
1100        let ctor = data.ctor.map(|(kind, index)| (kind, self.local_def_id(index)));
1101
1102        (
1103            data.idx,
1104            ty::VariantDef::new(
1105                self.item_name(index),
1106                variant_did,
1107                ctor,
1108                data.discr,
1109                self.get_associated_item_or_field_def_ids(index)
1110                    .map(|did| ty::FieldDef {
1111                        did,
1112                        name: self.item_name(did.index),
1113                        vis: self.get_visibility(did.index),
1114                        safety: self.get_safety(did.index),
1115                        value: self.get_default_field(did.index),
1116                    })
1117                    .collect(),
1118                parent_did,
1119                None,
1120                data.is_non_exhaustive,
1121            ),
1122        )
1123    }
1124
1125    fn get_adt_def<'tcx>(self, item_id: DefIndex, tcx: TyCtxt<'tcx>) -> ty::AdtDef<'tcx> {
1126        let kind = self.def_kind(item_id);
1127        let did = self.local_def_id(item_id);
1128
1129        let adt_kind = match kind {
1130            DefKind::Enum => ty::AdtKind::Enum,
1131            DefKind::Struct => ty::AdtKind::Struct,
1132            DefKind::Union => ty::AdtKind::Union,
1133            _ => bug!("get_adt_def called on a non-ADT {:?}", did),
1134        };
1135        let repr = self.root.tables.repr_options.get(self, item_id).unwrap().decode(self);
1136
1137        let mut variants: Vec<_> = if let ty::AdtKind::Enum = adt_kind {
1138            self.root
1139                .tables
1140                .module_children_non_reexports
1141                .get(self, item_id)
1142                .expect("variants are not encoded for an enum")
1143                .decode(self)
1144                .filter_map(|index| {
1145                    let kind = self.def_kind(index);
1146                    match kind {
1147                        DefKind::Ctor(..) => None,
1148                        _ => Some(self.get_variant(kind, index, did)),
1149                    }
1150                })
1151                .collect()
1152        } else {
1153            std::iter::once(self.get_variant(kind, item_id, did)).collect()
1154        };
1155
1156        variants.sort_by_key(|(idx, _)| *idx);
1157
1158        tcx.mk_adt_def(
1159            did,
1160            adt_kind,
1161            variants.into_iter().map(|(_, variant)| variant).collect(),
1162            repr,
1163        )
1164    }
1165
1166    fn get_visibility(self, id: DefIndex) -> Visibility<DefId> {
1167        self.root
1168            .tables
1169            .visibility
1170            .get(self, id)
1171            .unwrap_or_else(|| self.missing("visibility", id))
1172            .decode(self)
1173            .map_id(|index| self.local_def_id(index))
1174    }
1175
1176    fn get_safety(self, id: DefIndex) -> Safety {
1177        self.root.tables.safety.get(self, id)
1178    }
1179
1180    fn get_default_field(self, id: DefIndex) -> Option<DefId> {
1181        self.root.tables.default_fields.get(self, id).map(|d| d.decode(self))
1182    }
1183
1184    fn get_expn_that_defined(self, id: DefIndex, sess: &Session) -> ExpnId {
1185        self.root
1186            .tables
1187            .expn_that_defined
1188            .get(self, id)
1189            .unwrap_or_else(|| self.missing("expn_that_defined", id))
1190            .decode((self, sess))
1191    }
1192
1193    fn get_debugger_visualizers(self) -> Vec<DebuggerVisualizerFile> {
1194        self.root.debugger_visualizers.decode(self).collect::<Vec<_>>()
1195    }
1196
1197    /// Iterates over all the stability attributes in the given crate.
1198    fn get_lib_features(self) -> LibFeatures {
1199        LibFeatures {
1200            stability: self
1201                .root
1202                .lib_features
1203                .decode(self)
1204                .map(|(sym, stab)| (sym, (stab, DUMMY_SP)))
1205                .collect(),
1206        }
1207    }
1208
1209    /// Iterates over the stability implications in the given crate (when a `#[unstable]` attribute
1210    /// has an `implied_by` meta item, then the mapping from the implied feature to the actual
1211    /// feature is a stability implication).
1212    fn get_stability_implications<'tcx>(self, tcx: TyCtxt<'tcx>) -> &'tcx [(Symbol, Symbol)] {
1213        tcx.arena.alloc_from_iter(self.root.stability_implications.decode(self))
1214    }
1215
1216    /// Iterates over the lang items in the given crate.
1217    fn get_lang_items<'tcx>(self, tcx: TyCtxt<'tcx>) -> &'tcx [(DefId, LangItem)] {
1218        tcx.arena.alloc_from_iter(
1219            self.root
1220                .lang_items
1221                .decode(self)
1222                .map(move |(def_index, index)| (self.local_def_id(def_index), index)),
1223        )
1224    }
1225
1226    fn get_stripped_cfg_items<'tcx>(
1227        self,
1228        cnum: CrateNum,
1229        tcx: TyCtxt<'tcx>,
1230    ) -> &'tcx [StrippedCfgItem] {
1231        let item_names = self
1232            .root
1233            .stripped_cfg_items
1234            .decode((self, tcx))
1235            .map(|item| item.map_mod_id(|index| DefId { krate: cnum, index }));
1236        tcx.arena.alloc_from_iter(item_names)
1237    }
1238
1239    /// Iterates over the diagnostic items in the given crate.
1240    fn get_diagnostic_items(self) -> DiagnosticItems {
1241        let mut id_to_name = DefIdMap::default();
1242        let name_to_id = self
1243            .root
1244            .diagnostic_items
1245            .decode(self)
1246            .map(|(name, def_index)| {
1247                let id = self.local_def_id(def_index);
1248                id_to_name.insert(id, name);
1249                (name, id)
1250            })
1251            .collect();
1252        DiagnosticItems { id_to_name, name_to_id }
1253    }
1254
1255    fn get_mod_child(self, id: DefIndex, sess: &Session) -> ModChild {
1256        let ident = self.item_ident(id, sess);
1257        let res = Res::Def(self.def_kind(id), self.local_def_id(id));
1258        let vis = self.get_visibility(id);
1259
1260        ModChild { ident, res, vis, reexport_chain: Default::default() }
1261    }
1262
1263    /// Iterates over all named children of the given module,
1264    /// including both proper items and reexports.
1265    /// Module here is understood in name resolution sense - it can be a `mod` item,
1266    /// or a crate root, or an enum, or a trait.
1267    fn get_module_children(
1268        self,
1269        id: DefIndex,
1270        sess: &'a Session,
1271    ) -> impl Iterator<Item = ModChild> {
1272        gen move {
1273            if let Some(data) = &self.root.proc_macro_data {
1274                // If we are loading as a proc macro, we want to return
1275                // the view of this crate as a proc macro crate.
1276                if id == CRATE_DEF_INDEX {
1277                    for child_index in data.macros.decode(self) {
1278                        yield self.get_mod_child(child_index, sess);
1279                    }
1280                }
1281            } else {
1282                // Iterate over all children.
1283                let non_reexports = self.root.tables.module_children_non_reexports.get(self, id);
1284                for child_index in non_reexports.unwrap().decode(self) {
1285                    yield self.get_mod_child(child_index, sess);
1286                }
1287
1288                let reexports = self.root.tables.module_children_reexports.get(self, id);
1289                if !reexports.is_default() {
1290                    for reexport in reexports.decode((self, sess)) {
1291                        yield reexport;
1292                    }
1293                }
1294            }
1295        }
1296    }
1297
1298    fn is_ctfe_mir_available(self, id: DefIndex) -> bool {
1299        self.root.tables.mir_for_ctfe.get(self, id).is_some()
1300    }
1301
1302    fn is_item_mir_available(self, id: DefIndex) -> bool {
1303        self.root.tables.optimized_mir.get(self, id).is_some()
1304    }
1305
1306    fn get_fn_has_self_parameter(self, id: DefIndex, sess: &'a Session) -> bool {
1307        self.root
1308            .tables
1309            .fn_arg_idents
1310            .get(self, id)
1311            .expect("argument names not encoded for a function")
1312            .decode((self, sess))
1313            .nth(0)
1314            .is_some_and(|ident| matches!(ident, Some(Ident { name: kw::SelfLower, .. })))
1315    }
1316
1317    fn get_associated_item_or_field_def_ids(self, id: DefIndex) -> impl Iterator<Item = DefId> {
1318        self.root
1319            .tables
1320            .associated_item_or_field_def_ids
1321            .get(self, id)
1322            .unwrap_or_else(|| self.missing("associated_item_or_field_def_ids", id))
1323            .decode(self)
1324            .map(move |child_index| self.local_def_id(child_index))
1325    }
1326
1327    fn get_associated_item(self, id: DefIndex, sess: &'a Session) -> ty::AssocItem {
1328        let kind = match self.def_kind(id) {
1329            DefKind::AssocConst => ty::AssocKind::Const { name: self.item_name(id) },
1330            DefKind::AssocFn => ty::AssocKind::Fn {
1331                name: self.item_name(id),
1332                has_self: self.get_fn_has_self_parameter(id, sess),
1333            },
1334            DefKind::AssocTy => {
1335                let data = if let Some(rpitit_info) = self.root.tables.opt_rpitit_info.get(self, id)
1336                {
1337                    ty::AssocTypeData::Rpitit(rpitit_info.decode(self))
1338                } else {
1339                    ty::AssocTypeData::Normal(self.item_name(id))
1340                };
1341                ty::AssocKind::Type { data }
1342            }
1343            _ => bug!("cannot get associated-item of `{:?}`", self.def_key(id)),
1344        };
1345        let container = self.root.tables.assoc_container.get(self, id).unwrap().decode(self);
1346
1347        ty::AssocItem { kind, def_id: self.local_def_id(id), container }
1348    }
1349
1350    fn get_ctor(self, node_id: DefIndex) -> Option<(CtorKind, DefId)> {
1351        match self.def_kind(node_id) {
1352            DefKind::Struct | DefKind::Variant => {
1353                let vdata = self.root.tables.variant_data.get(self, node_id).unwrap().decode(self);
1354                vdata.ctor.map(|(kind, index)| (kind, self.local_def_id(index)))
1355            }
1356            _ => None,
1357        }
1358    }
1359
1360    fn get_item_attrs(
1361        self,
1362        id: DefIndex,
1363        sess: &'a Session,
1364    ) -> impl Iterator<Item = hir::Attribute> {
1365        self.root
1366            .tables
1367            .attributes
1368            .get(self, id)
1369            .unwrap_or_else(|| {
1370                // Structure and variant constructors don't have any attributes encoded for them,
1371                // but we assume that someone passing a constructor ID actually wants to look at
1372                // the attributes on the corresponding struct or variant.
1373                let def_key = self.def_key(id);
1374                assert_eq!(def_key.disambiguated_data.data, DefPathData::Ctor);
1375                let parent_id = def_key.parent.expect("no parent for a constructor");
1376                self.root
1377                    .tables
1378                    .attributes
1379                    .get(self, parent_id)
1380                    .expect("no encoded attributes for a structure or variant")
1381            })
1382            .decode((self, sess))
1383    }
1384
1385    fn get_inherent_implementations_for_type<'tcx>(
1386        self,
1387        tcx: TyCtxt<'tcx>,
1388        id: DefIndex,
1389    ) -> &'tcx [DefId] {
1390        tcx.arena.alloc_from_iter(
1391            self.root
1392                .tables
1393                .inherent_impls
1394                .get(self, id)
1395                .decode(self)
1396                .map(|index| self.local_def_id(index)),
1397        )
1398    }
1399
1400    /// Decodes all traits in the crate (for rustdoc and rustc diagnostics).
1401    fn get_traits(self) -> impl Iterator<Item = DefId> {
1402        self.root.traits.decode(self).map(move |index| self.local_def_id(index))
1403    }
1404
1405    /// Decodes all trait impls in the crate (for rustdoc).
1406    fn get_trait_impls(self) -> impl Iterator<Item = DefId> {
1407        self.cdata.trait_impls.values().flat_map(move |impls| {
1408            impls.decode(self).map(move |(impl_index, _)| self.local_def_id(impl_index))
1409        })
1410    }
1411
1412    fn get_incoherent_impls<'tcx>(self, tcx: TyCtxt<'tcx>, simp: SimplifiedType) -> &'tcx [DefId] {
1413        if let Some(impls) = self.cdata.incoherent_impls.get(&simp) {
1414            tcx.arena.alloc_from_iter(impls.decode(self).map(|idx| self.local_def_id(idx)))
1415        } else {
1416            &[]
1417        }
1418    }
1419
1420    fn get_implementations_of_trait<'tcx>(
1421        self,
1422        tcx: TyCtxt<'tcx>,
1423        trait_def_id: DefId,
1424    ) -> &'tcx [(DefId, Option<SimplifiedType>)] {
1425        if self.trait_impls.is_empty() {
1426            return &[];
1427        }
1428
1429        // Do a reverse lookup beforehand to avoid touching the crate_num
1430        // hash map in the loop below.
1431        let key = match self.reverse_translate_def_id(trait_def_id) {
1432            Some(def_id) => (def_id.krate.as_u32(), def_id.index),
1433            None => return &[],
1434        };
1435
1436        if let Some(impls) = self.trait_impls.get(&key) {
1437            tcx.arena.alloc_from_iter(
1438                impls
1439                    .decode(self)
1440                    .map(|(idx, simplified_self_ty)| (self.local_def_id(idx), simplified_self_ty)),
1441            )
1442        } else {
1443            &[]
1444        }
1445    }
1446
1447    fn get_native_libraries(self, sess: &'a Session) -> impl Iterator<Item = NativeLib> {
1448        self.root.native_libraries.decode((self, sess))
1449    }
1450
1451    fn get_proc_macro_quoted_span(self, index: usize, sess: &Session) -> Span {
1452        self.root
1453            .tables
1454            .proc_macro_quoted_spans
1455            .get(self, index)
1456            .unwrap_or_else(|| panic!("Missing proc macro quoted span: {index:?}"))
1457            .decode((self, sess))
1458    }
1459
1460    fn get_foreign_modules(self, sess: &'a Session) -> impl Iterator<Item = ForeignModule> {
1461        self.root.foreign_modules.decode((self, sess))
1462    }
1463
1464    fn get_dylib_dependency_formats<'tcx>(
1465        self,
1466        tcx: TyCtxt<'tcx>,
1467    ) -> &'tcx [(CrateNum, LinkagePreference)] {
1468        tcx.arena.alloc_from_iter(
1469            self.root.dylib_dependency_formats.decode(self).enumerate().flat_map(|(i, link)| {
1470                let cnum = CrateNum::new(i + 1); // We skipped LOCAL_CRATE when encoding
1471                link.map(|link| (self.cnum_map[cnum], link))
1472            }),
1473        )
1474    }
1475
1476    fn get_missing_lang_items<'tcx>(self, tcx: TyCtxt<'tcx>) -> &'tcx [LangItem] {
1477        tcx.arena.alloc_from_iter(self.root.lang_items_missing.decode(self))
1478    }
1479
1480    fn get_exportable_items(self) -> impl Iterator<Item = DefId> {
1481        self.root.exportable_items.decode(self).map(move |index| self.local_def_id(index))
1482    }
1483
1484    fn get_stable_order_of_exportable_impls(self) -> impl Iterator<Item = (DefId, usize)> {
1485        self.root
1486            .stable_order_of_exportable_impls
1487            .decode(self)
1488            .map(move |v| (self.local_def_id(v.0), v.1))
1489    }
1490
1491    fn exported_non_generic_symbols<'tcx>(
1492        self,
1493        tcx: TyCtxt<'tcx>,
1494    ) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportInfo)] {
1495        tcx.arena.alloc_from_iter(self.root.exported_non_generic_symbols.decode((self, tcx)))
1496    }
1497
1498    fn exported_generic_symbols<'tcx>(
1499        self,
1500        tcx: TyCtxt<'tcx>,
1501    ) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportInfo)] {
1502        tcx.arena.alloc_from_iter(self.root.exported_generic_symbols.decode((self, tcx)))
1503    }
1504
1505    fn get_macro(self, id: DefIndex, sess: &Session) -> ast::MacroDef {
1506        match self.def_kind(id) {
1507            DefKind::Macro(_) => {
1508                let macro_rules = self.root.tables.is_macro_rules.get(self, id);
1509                let body =
1510                    self.root.tables.macro_definition.get(self, id).unwrap().decode((self, sess));
1511                ast::MacroDef { macro_rules, body: Box::new(body) }
1512            }
1513            _ => bug!(),
1514        }
1515    }
1516
1517    #[inline]
1518    fn def_key(self, index: DefIndex) -> DefKey {
1519        *self
1520            .def_key_cache
1521            .lock()
1522            .entry(index)
1523            .or_insert_with(|| self.root.tables.def_keys.get(self, index).unwrap().decode(self))
1524    }
1525
1526    // Returns the path leading to the thing with this `id`.
1527    fn def_path(self, id: DefIndex) -> DefPath {
1528        debug!("def_path(cnum={:?}, id={:?})", self.cnum, id);
1529        DefPath::make(self.cnum, id, |parent| self.def_key(parent))
1530    }
1531
1532    #[inline]
1533    fn def_path_hash(self, index: DefIndex) -> DefPathHash {
1534        // This is a hack to workaround the fact that we can't easily encode/decode a Hash64
1535        // into the FixedSizeEncoding, as Hash64 lacks a Default impl. A future refactor to
1536        // relax the Default restriction will likely fix this.
1537        let fingerprint = Fingerprint::new(
1538            self.root.stable_crate_id.as_u64(),
1539            self.root.tables.def_path_hashes.get(self, index),
1540        );
1541        DefPathHash::new(self.root.stable_crate_id, fingerprint.split().1)
1542    }
1543
1544    #[inline]
1545    fn def_path_hash_to_def_index(self, hash: DefPathHash) -> Option<DefIndex> {
1546        self.def_path_hash_map.def_path_hash_to_def_index(&hash)
1547    }
1548
1549    fn expn_hash_to_expn_id(self, sess: &Session, index_guess: u32, hash: ExpnHash) -> ExpnId {
1550        let index_guess = ExpnIndex::from_u32(index_guess);
1551        let old_hash = self.root.expn_hashes.get(self, index_guess).map(|lazy| lazy.decode(self));
1552
1553        let index = if old_hash == Some(hash) {
1554            // Fast path: the expn and its index is unchanged from the
1555            // previous compilation session. There is no need to decode anything
1556            // else.
1557            index_guess
1558        } else {
1559            // Slow path: We need to find out the new `DefIndex` of the provided
1560            // `DefPathHash`, if its still exists. This requires decoding every `DefPathHash`
1561            // stored in this crate.
1562            let map = self.cdata.expn_hash_map.get_or_init(|| {
1563                let end_id = self.root.expn_hashes.size() as u32;
1564                let mut map =
1565                    UnhashMap::with_capacity_and_hasher(end_id as usize, Default::default());
1566                for i in 0..end_id {
1567                    let i = ExpnIndex::from_u32(i);
1568                    if let Some(hash) = self.root.expn_hashes.get(self, i) {
1569                        map.insert(hash.decode(self), i);
1570                    }
1571                }
1572                map
1573            });
1574            map[&hash]
1575        };
1576
1577        let data = self.root.expn_data.get(self, index).unwrap().decode((self, sess));
1578        rustc_span::hygiene::register_expn_id(self.cnum, index, data, hash)
1579    }
1580
1581    /// Imports the source_map from an external crate into the source_map of the crate
1582    /// currently being compiled (the "local crate").
1583    ///
1584    /// The import algorithm works analogous to how AST items are inlined from an
1585    /// external crate's metadata:
1586    /// For every SourceFile in the external source_map an 'inline' copy is created in the
1587    /// local source_map. The correspondence relation between external and local
1588    /// SourceFiles is recorded in the `ImportedSourceFile` objects returned from this
1589    /// function. When an item from an external crate is later inlined into this
1590    /// crate, this correspondence information is used to translate the span
1591    /// information of the inlined item so that it refers the correct positions in
1592    /// the local source_map (see `<decoder::DecodeContext as SpecializedDecoder<Span>>`).
1593    ///
1594    /// The import algorithm in the function below will reuse SourceFiles already
1595    /// existing in the local source_map. For example, even if the SourceFile of some
1596    /// source file of libstd gets imported many times, there will only ever be
1597    /// one SourceFile object for the corresponding file in the local source_map.
1598    ///
1599    /// Note that imported SourceFiles do not actually contain the source code of the
1600    /// file they represent, just information about length, line breaks, and
1601    /// multibyte characters. This information is enough to generate valid debuginfo
1602    /// for items inlined from other crates.
1603    ///
1604    /// Proc macro crates don't currently export spans, so this function does not have
1605    /// to work for them.
1606    fn imported_source_file(self, source_file_index: u32, sess: &Session) -> ImportedSourceFile {
1607        fn filter<'a>(
1608            sess: &Session,
1609            real_source_base_dir: &Option<PathBuf>,
1610            path: Option<&'a Path>,
1611        ) -> Option<&'a Path> {
1612            path.filter(|_| {
1613                // Only spend time on further checks if we have what to translate *to*.
1614                real_source_base_dir.is_some()
1615                // Some tests need the translation to be always skipped.
1616                && sess.opts.unstable_opts.translate_remapped_path_to_local_path
1617            })
1618            .filter(|virtual_dir| {
1619                // Don't translate away `/rustc/$hash` if we're still remapping to it,
1620                // since that means we're still building `std`/`rustc` that need it,
1621                // and we don't want the real path to leak into codegen/debuginfo.
1622                !sess.opts.remap_path_prefix.iter().any(|(_from, to)| to == virtual_dir)
1623            })
1624        }
1625
1626        let try_to_translate_virtual_to_real =
1627            |virtual_source_base_dir: Option<&str>,
1628             real_source_base_dir: &Option<PathBuf>,
1629             name: &mut rustc_span::FileName| {
1630                let virtual_source_base_dir = [
1631                    filter(sess, real_source_base_dir, virtual_source_base_dir.map(Path::new)),
1632                    filter(
1633                        sess,
1634                        real_source_base_dir,
1635                        sess.opts.unstable_opts.simulate_remapped_rust_src_base.as_deref(),
1636                    ),
1637                ];
1638
1639                debug!(
1640                    "try_to_translate_virtual_to_real(name={:?}): \
1641                     virtual_source_base_dir={:?}, real_source_base_dir={:?}",
1642                    name, virtual_source_base_dir, real_source_base_dir,
1643                );
1644
1645                for virtual_dir in virtual_source_base_dir.iter().flatten() {
1646                    if let Some(real_dir) = &real_source_base_dir
1647                        && let rustc_span::FileName::Real(old_name) = name
1648                        && let rustc_span::RealFileName::Remapped { local_path: _, virtual_name } =
1649                            old_name
1650                        && let Ok(rest) = virtual_name.strip_prefix(virtual_dir)
1651                    {
1652                        let new_path = real_dir.join(rest);
1653
1654                        debug!(
1655                            "try_to_translate_virtual_to_real: `{}` -> `{}`",
1656                            virtual_name.display(),
1657                            new_path.display(),
1658                        );
1659
1660                        // Check if the translated real path is affected by any user-requested
1661                        // remaps via --remap-path-prefix. Apply them if so.
1662                        // Note that this is a special case for imported rust-src paths specified by
1663                        // https://rust-lang.github.io/rfcs/3127-trim-paths.html#handling-sysroot-paths.
1664                        // Other imported paths are not currently remapped (see #66251).
1665                        let (user_remapped, applied) =
1666                            sess.source_map().path_mapping().map_prefix(&new_path);
1667                        let new_name = if applied {
1668                            rustc_span::RealFileName::Remapped {
1669                                local_path: Some(new_path.clone()),
1670                                virtual_name: user_remapped.to_path_buf(),
1671                            }
1672                        } else {
1673                            rustc_span::RealFileName::LocalPath(new_path)
1674                        };
1675                        *old_name = new_name;
1676                    }
1677                }
1678            };
1679
1680        let try_to_translate_real_to_virtual =
1681            |virtual_source_base_dir: Option<&str>,
1682             real_source_base_dir: &Option<PathBuf>,
1683             subdir: &str,
1684             name: &mut rustc_span::FileName| {
1685                if let Some(virtual_dir) = &sess.opts.unstable_opts.simulate_remapped_rust_src_base
1686                    && let Some(real_dir) = real_source_base_dir
1687                    && let rustc_span::FileName::Real(old_name) = name
1688                {
1689                    let relative_path = match old_name {
1690                        rustc_span::RealFileName::LocalPath(local) => {
1691                            local.strip_prefix(real_dir).ok()
1692                        }
1693                        rustc_span::RealFileName::Remapped { virtual_name, .. } => {
1694                            virtual_source_base_dir
1695                                .and_then(|virtual_dir| virtual_name.strip_prefix(virtual_dir).ok())
1696                        }
1697                    };
1698                    debug!(
1699                        ?relative_path,
1700                        ?virtual_dir,
1701                        ?subdir,
1702                        "simulate_remapped_rust_src_base"
1703                    );
1704                    if let Some(rest) = relative_path.and_then(|p| p.strip_prefix(subdir).ok()) {
1705                        *old_name = rustc_span::RealFileName::Remapped {
1706                            local_path: None,
1707                            virtual_name: virtual_dir.join(subdir).join(rest),
1708                        };
1709                    }
1710                }
1711            };
1712
1713        let mut import_info = self.cdata.source_map_import_info.lock();
1714        for _ in import_info.len()..=(source_file_index as usize) {
1715            import_info.push(None);
1716        }
1717        import_info[source_file_index as usize]
1718            .get_or_insert_with(|| {
1719                let source_file_to_import = self
1720                    .root
1721                    .source_map
1722                    .get(self, source_file_index)
1723                    .expect("missing source file")
1724                    .decode(self);
1725
1726                // We can't reuse an existing SourceFile, so allocate a new one
1727                // containing the information we need.
1728                let original_end_pos = source_file_to_import.end_position();
1729                let rustc_span::SourceFile {
1730                    mut name,
1731                    src_hash,
1732                    checksum_hash,
1733                    start_pos: original_start_pos,
1734                    normalized_source_len,
1735                    unnormalized_source_len,
1736                    lines,
1737                    multibyte_chars,
1738                    normalized_pos,
1739                    stable_id,
1740                    ..
1741                } = source_file_to_import;
1742
1743                // If this file is under $sysroot/lib/rustlib/src/
1744                // and the user wish to simulate remapping with -Z simulate-remapped-rust-src-base,
1745                // then we change `name` to a similar state as if the rust was bootstrapped
1746                // with `remap-debuginfo = true`.
1747                // This is useful for testing so that tests about the effects of
1748                // `try_to_translate_virtual_to_real` don't have to worry about how the
1749                // compiler is bootstrapped.
1750                try_to_translate_real_to_virtual(
1751                    option_env!("CFG_VIRTUAL_RUST_SOURCE_BASE_DIR"),
1752                    &sess.opts.real_rust_source_base_dir,
1753                    "library",
1754                    &mut name,
1755                );
1756
1757                // If this file is under $sysroot/lib/rustlib/rustc-src/
1758                // and the user wish to simulate remapping with -Z simulate-remapped-rust-src-base,
1759                // then we change `name` to a similar state as if the rust was bootstrapped
1760                // with `remap-debuginfo = true`.
1761                try_to_translate_real_to_virtual(
1762                    option_env!("CFG_VIRTUAL_RUSTC_DEV_SOURCE_BASE_DIR"),
1763                    &sess.opts.real_rustc_dev_source_base_dir,
1764                    "compiler",
1765                    &mut name,
1766                );
1767
1768                // If this file's path has been remapped to `/rustc/$hash`,
1769                // we might be able to reverse that.
1770                //
1771                // NOTE: if you update this, you might need to also update bootstrap's code for generating
1772                // the `rust-src` component in `Src::run` in `src/bootstrap/dist.rs`.
1773                try_to_translate_virtual_to_real(
1774                    option_env!("CFG_VIRTUAL_RUST_SOURCE_BASE_DIR"),
1775                    &sess.opts.real_rust_source_base_dir,
1776                    &mut name,
1777                );
1778
1779                // If this file's path has been remapped to `/rustc-dev/$hash`,
1780                // we might be able to reverse that.
1781                //
1782                // NOTE: if you update this, you might need to also update bootstrap's code for generating
1783                // the `rustc-dev` component in `Src::run` in `src/bootstrap/dist.rs`.
1784                try_to_translate_virtual_to_real(
1785                    option_env!("CFG_VIRTUAL_RUSTC_DEV_SOURCE_BASE_DIR"),
1786                    &sess.opts.real_rustc_dev_source_base_dir,
1787                    &mut name,
1788                );
1789
1790                let local_version = sess.source_map().new_imported_source_file(
1791                    name,
1792                    src_hash,
1793                    checksum_hash,
1794                    stable_id,
1795                    normalized_source_len.to_u32(),
1796                    unnormalized_source_len,
1797                    self.cnum,
1798                    lines,
1799                    multibyte_chars,
1800                    normalized_pos,
1801                    source_file_index,
1802                );
1803                debug!(
1804                    "CrateMetaData::imported_source_files alloc \
1805                         source_file {:?} original (start_pos {:?} source_len {:?}) \
1806                         translated (start_pos {:?} source_len {:?})",
1807                    local_version.name,
1808                    original_start_pos,
1809                    normalized_source_len,
1810                    local_version.start_pos,
1811                    local_version.normalized_source_len
1812                );
1813
1814                ImportedSourceFile {
1815                    original_start_pos,
1816                    original_end_pos,
1817                    translated_source_file: local_version,
1818                }
1819            })
1820            .clone()
1821    }
1822
1823    fn get_attr_flags(self, index: DefIndex) -> AttrFlags {
1824        self.root.tables.attr_flags.get(self, index)
1825    }
1826
1827    fn get_intrinsic(self, index: DefIndex) -> Option<ty::IntrinsicDef> {
1828        self.root.tables.intrinsic.get(self, index).map(|d| d.decode(self))
1829    }
1830
1831    fn get_doc_link_resolutions(self, index: DefIndex) -> DocLinkResMap {
1832        self.root
1833            .tables
1834            .doc_link_resolutions
1835            .get(self, index)
1836            .expect("no resolutions for a doc link")
1837            .decode(self)
1838    }
1839
1840    fn get_doc_link_traits_in_scope(self, index: DefIndex) -> impl Iterator<Item = DefId> {
1841        self.root
1842            .tables
1843            .doc_link_traits_in_scope
1844            .get(self, index)
1845            .expect("no traits in scope for a doc link")
1846            .decode(self)
1847    }
1848}
1849
1850impl CrateMetadata {
1851    pub(crate) fn new(
1852        cstore: &CStore,
1853        blob: MetadataBlob,
1854        root: CrateRoot,
1855        raw_proc_macros: Option<&'static [ProcMacro]>,
1856        cnum: CrateNum,
1857        cnum_map: CrateNumMap,
1858        dep_kind: CrateDepKind,
1859        source: CrateSource,
1860        private_dep: bool,
1861        host_hash: Option<Svh>,
1862    ) -> CrateMetadata {
1863        let trait_impls = root
1864            .impls
1865            .decode(&blob)
1866            .map(|trait_impls| (trait_impls.trait_id, trait_impls.impls))
1867            .collect();
1868        let alloc_decoding_state =
1869            AllocDecodingState::new(root.interpret_alloc_index.decode(&blob).collect());
1870        let dependencies = cnum_map.iter().copied().collect();
1871
1872        // Pre-decode the DefPathHash->DefIndex table. This is a cheap operation
1873        // that does not copy any data. It just does some data verification.
1874        let def_path_hash_map = root.def_path_hash_map.decode(&blob);
1875
1876        let mut cdata = CrateMetadata {
1877            blob,
1878            root,
1879            trait_impls,
1880            incoherent_impls: Default::default(),
1881            raw_proc_macros,
1882            source_map_import_info: Lock::new(Vec::new()),
1883            def_path_hash_map,
1884            expn_hash_map: Default::default(),
1885            alloc_decoding_state,
1886            cnum,
1887            cnum_map,
1888            dependencies,
1889            dep_kind,
1890            source: Arc::new(source),
1891            private_dep,
1892            host_hash,
1893            used: false,
1894            extern_crate: None,
1895            hygiene_context: Default::default(),
1896            def_key_cache: Default::default(),
1897        };
1898
1899        // Need `CrateMetadataRef` to decode `DefId`s in simplified types.
1900        cdata.incoherent_impls = cdata
1901            .root
1902            .incoherent_impls
1903            .decode(CrateMetadataRef { cdata: &cdata, cstore })
1904            .map(|incoherent_impls| (incoherent_impls.self_ty, incoherent_impls.impls))
1905            .collect();
1906
1907        cdata
1908    }
1909
1910    pub(crate) fn dependencies(&self) -> impl Iterator<Item = CrateNum> {
1911        self.dependencies.iter().copied()
1912    }
1913
1914    pub(crate) fn target_modifiers(&self) -> TargetModifiers {
1915        self.root.decode_target_modifiers(&self.blob).collect()
1916    }
1917
1918    /// Keep `new_extern_crate` if it looks better in diagnostics
1919    pub(crate) fn update_extern_crate_diagnostics(
1920        &mut self,
1921        new_extern_crate: ExternCrate,
1922    ) -> bool {
1923        let update =
1924            self.extern_crate.as_ref().is_none_or(|old| old.rank() < new_extern_crate.rank());
1925        if update {
1926            self.extern_crate = Some(new_extern_crate);
1927        }
1928        update
1929    }
1930
1931    pub(crate) fn source(&self) -> &CrateSource {
1932        &*self.source
1933    }
1934
1935    pub(crate) fn dep_kind(&self) -> CrateDepKind {
1936        self.dep_kind
1937    }
1938
1939    pub(crate) fn set_dep_kind(&mut self, dep_kind: CrateDepKind) {
1940        self.dep_kind = dep_kind;
1941    }
1942
1943    pub(crate) fn update_and_private_dep(&mut self, private_dep: bool) {
1944        self.private_dep &= private_dep;
1945    }
1946
1947    pub(crate) fn used(&self) -> bool {
1948        self.used
1949    }
1950
1951    pub(crate) fn required_panic_strategy(&self) -> Option<PanicStrategy> {
1952        self.root.required_panic_strategy
1953    }
1954
1955    pub(crate) fn needs_panic_runtime(&self) -> bool {
1956        self.root.needs_panic_runtime
1957    }
1958
1959    pub(crate) fn is_private_dep(&self) -> bool {
1960        self.private_dep
1961    }
1962
1963    pub(crate) fn is_panic_runtime(&self) -> bool {
1964        self.root.panic_runtime
1965    }
1966
1967    pub(crate) fn is_profiler_runtime(&self) -> bool {
1968        self.root.profiler_runtime
1969    }
1970
1971    pub(crate) fn is_compiler_builtins(&self) -> bool {
1972        self.root.compiler_builtins
1973    }
1974
1975    pub(crate) fn needs_allocator(&self) -> bool {
1976        self.root.needs_allocator
1977    }
1978
1979    pub(crate) fn has_global_allocator(&self) -> bool {
1980        self.root.has_global_allocator
1981    }
1982
1983    pub(crate) fn has_alloc_error_handler(&self) -> bool {
1984        self.root.has_alloc_error_handler
1985    }
1986
1987    pub(crate) fn has_default_lib_allocator(&self) -> bool {
1988        self.root.has_default_lib_allocator
1989    }
1990
1991    pub(crate) fn is_proc_macro_crate(&self) -> bool {
1992        self.root.is_proc_macro_crate()
1993    }
1994
1995    pub(crate) fn proc_macros_for_crate(
1996        &self,
1997        krate: CrateNum,
1998        cstore: &CStore,
1999    ) -> impl Iterator<Item = DefId> {
2000        gen move {
2001            for def_id in self.root.proc_macro_data.as_ref().into_iter().flat_map(move |data| {
2002                data.macros
2003                    .decode(CrateMetadataRef { cdata: self, cstore })
2004                    .map(move |index| DefId { index, krate })
2005            }) {
2006                yield def_id;
2007            }
2008        }
2009    }
2010
2011    pub(crate) fn name(&self) -> Symbol {
2012        self.root.header.name
2013    }
2014
2015    pub(crate) fn hash(&self) -> Svh {
2016        self.root.header.hash
2017    }
2018
2019    pub(crate) fn has_async_drops(&self) -> bool {
2020        self.root.tables.adt_async_destructor.len > 0
2021    }
2022
2023    fn num_def_ids(&self) -> usize {
2024        self.root.tables.def_keys.size()
2025    }
2026
2027    fn local_def_id(&self, index: DefIndex) -> DefId {
2028        DefId { krate: self.cnum, index }
2029    }
2030
2031    // Translate a DefId from the current compilation environment to a DefId
2032    // for an external crate.
2033    fn reverse_translate_def_id(&self, did: DefId) -> Option<DefId> {
2034        for (local, &global) in self.cnum_map.iter_enumerated() {
2035            if global == did.krate {
2036                return Some(DefId { krate: local, index: did.index });
2037            }
2038        }
2039
2040        None
2041    }
2042}