Skip to main content

rustc_metadata/rmeta/
decoder.rs

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