]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_metadata/src/rmeta/decoder.rs
Add fn_arg_names table.
[rust.git] / compiler / rustc_metadata / src / rmeta / decoder.rs
1 // Decoding metadata from a single crate's metadata
2
3 use crate::creader::CrateMetadataRef;
4 use crate::rmeta::table::{FixedSizeEncoding, Table};
5 use crate::rmeta::*;
6
7 use rustc_ast as ast;
8 use rustc_attr as attr;
9 use rustc_data_structures::captures::Captures;
10 use rustc_data_structures::fx::FxHashMap;
11 use rustc_data_structures::svh::Svh;
12 use rustc_data_structures::sync::{Lock, LockGuard, Lrc, OnceCell};
13 use rustc_data_structures::unhash::UnhashMap;
14 use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind};
15 use rustc_expand::proc_macro::{AttrProcMacro, BangProcMacro, ProcMacroDerive};
16 use rustc_hir as hir;
17 use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
18 use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE};
19 use rustc_hir::definitions::{DefKey, DefPath, DefPathData, DefPathHash};
20 use rustc_hir::diagnostic_items::DiagnosticItems;
21 use rustc_hir::lang_items;
22 use rustc_index::vec::{Idx, IndexVec};
23 use rustc_middle::arena::ArenaAllocatable;
24 use rustc_middle::metadata::ModChild;
25 use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
26 use rustc_middle::middle::stability::DeprecationEntry;
27 use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState};
28 use rustc_middle::thir;
29 use rustc_middle::ty::codec::TyDecoder;
30 use rustc_middle::ty::fast_reject::SimplifiedType;
31 use rustc_middle::ty::{self, Ty, TyCtxt, Visibility};
32 use rustc_serialize::{opaque, Decodable, Decoder};
33 use rustc_session::cstore::{
34     CrateSource, ExternCrate, ForeignModule, LinkagePreference, NativeLib,
35 };
36 use rustc_session::Session;
37 use rustc_span::hygiene::{ExpnIndex, MacroKind};
38 use rustc_span::source_map::{respan, Spanned};
39 use rustc_span::symbol::{sym, Ident, Symbol};
40 use rustc_span::{self, BytePos, ExpnId, Pos, Span, SyntaxContext, DUMMY_SP};
41
42 use proc_macro::bridge::client::ProcMacro;
43 use std::io;
44 use std::mem;
45 use std::num::NonZeroUsize;
46 use std::path::Path;
47 use tracing::debug;
48
49 pub(super) use cstore_impl::provide;
50 pub use cstore_impl::provide_extern;
51 use rustc_span::hygiene::HygieneDecodeContext;
52
53 mod cstore_impl;
54
55 /// A reference to the raw binary version of crate metadata.
56 /// A `MetadataBlob` internally is just a reference counted pointer to
57 /// the actual data, so cloning it is cheap.
58 #[derive(Clone)]
59 crate struct MetadataBlob(Lrc<MetadataRef>);
60
61 // This is needed so we can create an OwningRef into the blob.
62 // The data behind a `MetadataBlob` has a stable address because it is
63 // contained within an Rc/Arc.
64 unsafe impl rustc_data_structures::owning_ref::StableAddress for MetadataBlob {}
65
66 // This is needed so we can create an OwningRef into the blob.
67 impl std::ops::Deref for MetadataBlob {
68     type Target = [u8];
69
70     #[inline]
71     fn deref(&self) -> &[u8] {
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.
80 crate type CrateNumMap = IndexVec<CrateNum, CrateNum>;
81
82 crate struct CrateMetadata {
83     /// The primary crate data - binary metadata blob.
84     blob: MetadataBlob,
85
86     // --- Some data pre-decoded from the metadata blob, usually for performance ---
87     /// Properties of the whole crate.
88     /// NOTE(eddyb) we pass `'static` to a `'tcx` parameter because this
89     /// lifetime is only used behind `Lazy`, and therefore acts like a
90     /// universal (`for<'tcx>`), that is paired up with whichever `TyCtxt`
91     /// is being used to decode those values.
92     root: CrateRoot<'static>,
93     /// Trait impl data.
94     /// FIXME: Used only from queries and can use query cache,
95     /// so pre-decoding can probably be avoided.
96     trait_impls: FxHashMap<(u32, DefIndex), Lazy<[(DefIndex, Option<SimplifiedType>)]>>,
97     /// Proc macro descriptions for this crate, if it's a proc macro crate.
98     raw_proc_macros: Option<&'static [ProcMacro]>,
99     /// Source maps for code from the crate.
100     source_map_import_info: OnceCell<Vec<ImportedSourceFile>>,
101     /// For every definition in this crate, maps its `DefPathHash` to its `DefIndex`.
102     def_path_hash_map: DefPathHashMapRef<'static>,
103     /// Likewise for ExpnHash.
104     expn_hash_map: OnceCell<UnhashMap<ExpnHash, ExpnIndex>>,
105     /// Used for decoding interpret::AllocIds in a cached & thread-safe manner.
106     alloc_decoding_state: AllocDecodingState,
107     /// Caches decoded `DefKey`s.
108     def_key_cache: Lock<FxHashMap<DefIndex, DefKey>>,
109     /// Caches decoded `DefPathHash`es.
110     def_path_hash_cache: Lock<FxHashMap<DefIndex, DefPathHash>>,
111
112     // --- Other significant crate properties ---
113     /// ID of this crate, from the current compilation session's point of view.
114     cnum: CrateNum,
115     /// Maps crate IDs as they are were seen from this crate's compilation sessions into
116     /// IDs as they are seen from the current compilation session.
117     cnum_map: CrateNumMap,
118     /// Same ID set as `cnum_map` plus maybe some injected crates like panic runtime.
119     dependencies: Lock<Vec<CrateNum>>,
120     /// How to link (or not link) this crate to the currently compiled crate.
121     dep_kind: Lock<CrateDepKind>,
122     /// Filesystem location of this crate.
123     source: Lrc<CrateSource>,
124     /// Whether or not this crate should be consider a private dependency
125     /// for purposes of the 'exported_private_dependencies' lint
126     private_dep: bool,
127     /// The hash for the host proc macro. Used to support `-Z dual-proc-macro`.
128     host_hash: Option<Svh>,
129
130     /// Additional data used for decoding `HygieneData` (e.g. `SyntaxContext`
131     /// and `ExpnId`).
132     /// Note that we store a `HygieneDecodeContext` for each `CrateMetadat`. This is
133     /// because `SyntaxContext` ids are not globally unique, so we need
134     /// to track which ids we've decoded on a per-crate basis.
135     hygiene_context: HygieneDecodeContext,
136
137     // --- Data used only for improving diagnostics ---
138     /// Information about the `extern crate` item or path that caused this crate to be loaded.
139     /// If this is `None`, then the crate was injected (e.g., by the allocator).
140     extern_crate: Lock<Option<ExternCrate>>,
141 }
142
143 /// Holds information about a rustc_span::SourceFile imported from another crate.
144 /// See `imported_source_files()` for more information.
145 struct ImportedSourceFile {
146     /// This SourceFile's byte-offset within the source_map of its original crate
147     original_start_pos: rustc_span::BytePos,
148     /// The end of this SourceFile within the source_map of its original crate
149     original_end_pos: rustc_span::BytePos,
150     /// The imported SourceFile's representation within the local source_map
151     translated_source_file: Lrc<rustc_span::SourceFile>,
152 }
153
154 pub(super) struct DecodeContext<'a, 'tcx> {
155     opaque: opaque::Decoder<'a>,
156     cdata: Option<CrateMetadataRef<'a>>,
157     blob: &'a MetadataBlob,
158     sess: Option<&'tcx Session>,
159     tcx: Option<TyCtxt<'tcx>>,
160
161     // Cache the last used source_file for translating spans as an optimization.
162     last_source_file_index: usize,
163
164     lazy_state: LazyState,
165
166     // Used for decoding interpret::AllocIds in a cached & thread-safe manner.
167     alloc_decoding_session: Option<AllocDecodingSession<'a>>,
168 }
169
170 /// Abstract over the various ways one can create metadata decoders.
171 pub(super) trait Metadata<'a, 'tcx>: Copy {
172     fn blob(self) -> &'a MetadataBlob;
173
174     fn cdata(self) -> Option<CrateMetadataRef<'a>> {
175         None
176     }
177     fn sess(self) -> Option<&'tcx Session> {
178         None
179     }
180     fn tcx(self) -> Option<TyCtxt<'tcx>> {
181         None
182     }
183
184     fn decoder(self, pos: usize) -> DecodeContext<'a, 'tcx> {
185         let tcx = self.tcx();
186         DecodeContext {
187             opaque: opaque::Decoder::new(self.blob(), pos),
188             cdata: self.cdata(),
189             blob: self.blob(),
190             sess: self.sess().or(tcx.map(|tcx| tcx.sess)),
191             tcx,
192             last_source_file_index: 0,
193             lazy_state: LazyState::NoNode,
194             alloc_decoding_session: self
195                 .cdata()
196                 .map(|cdata| cdata.cdata.alloc_decoding_state.new_decoding_session()),
197         }
198     }
199 }
200
201 impl<'a, 'tcx> Metadata<'a, 'tcx> for &'a MetadataBlob {
202     #[inline]
203     fn blob(self) -> &'a MetadataBlob {
204         self
205     }
206 }
207
208 impl<'a, 'tcx> Metadata<'a, 'tcx> for (&'a MetadataBlob, &'tcx Session) {
209     #[inline]
210     fn blob(self) -> &'a MetadataBlob {
211         self.0
212     }
213
214     #[inline]
215     fn sess(self) -> Option<&'tcx Session> {
216         let (_, sess) = self;
217         Some(sess)
218     }
219 }
220
221 impl<'a, 'tcx> Metadata<'a, 'tcx> for CrateMetadataRef<'a> {
222     #[inline]
223     fn blob(self) -> &'a MetadataBlob {
224         &self.cdata.blob
225     }
226     #[inline]
227     fn cdata(self) -> Option<CrateMetadataRef<'a>> {
228         Some(self)
229     }
230 }
231
232 impl<'a, 'tcx> Metadata<'a, 'tcx> for (CrateMetadataRef<'a>, &'tcx Session) {
233     #[inline]
234     fn blob(self) -> &'a MetadataBlob {
235         &self.0.cdata.blob
236     }
237     #[inline]
238     fn cdata(self) -> Option<CrateMetadataRef<'a>> {
239         Some(self.0)
240     }
241     #[inline]
242     fn sess(self) -> Option<&'tcx Session> {
243         Some(self.1)
244     }
245 }
246
247 impl<'a, 'tcx> Metadata<'a, 'tcx> for (CrateMetadataRef<'a>, TyCtxt<'tcx>) {
248     #[inline]
249     fn blob(self) -> &'a MetadataBlob {
250         &self.0.cdata.blob
251     }
252     #[inline]
253     fn cdata(self) -> Option<CrateMetadataRef<'a>> {
254         Some(self.0)
255     }
256     #[inline]
257     fn tcx(self) -> Option<TyCtxt<'tcx>> {
258         Some(self.1)
259     }
260 }
261
262 impl<'a, 'tcx, T: Decodable<DecodeContext<'a, 'tcx>>> Lazy<T> {
263     fn decode<M: Metadata<'a, 'tcx>>(self, metadata: M) -> T {
264         let mut dcx = metadata.decoder(self.position.get());
265         dcx.lazy_state = LazyState::NodeStart(self.position);
266         T::decode(&mut dcx)
267     }
268 }
269
270 impl<'a: 'x, 'tcx: 'x, 'x, T: Decodable<DecodeContext<'a, 'tcx>>> Lazy<[T]> {
271     fn decode<M: Metadata<'a, 'tcx>>(
272         self,
273         metadata: M,
274     ) -> impl ExactSizeIterator<Item = T> + Captures<'a> + Captures<'tcx> + 'x {
275         let mut dcx = metadata.decoder(self.position.get());
276         dcx.lazy_state = LazyState::NodeStart(self.position);
277         (0..self.meta).map(move |_| T::decode(&mut dcx))
278     }
279 }
280
281 trait LazyQueryDecodable<'a, 'tcx, T> {
282     fn decode_query(
283         self,
284         cdata: CrateMetadataRef<'a>,
285         tcx: TyCtxt<'tcx>,
286         err: impl FnOnce() -> !,
287     ) -> T;
288 }
289
290 impl<'a, 'tcx, T> LazyQueryDecodable<'a, 'tcx, T> for Option<Lazy<T>>
291 where
292     T: Decodable<DecodeContext<'a, 'tcx>>,
293 {
294     fn decode_query(
295         self,
296         cdata: CrateMetadataRef<'a>,
297         tcx: TyCtxt<'tcx>,
298         err: impl FnOnce() -> !,
299     ) -> T {
300         if let Some(l) = self { l.decode((cdata, tcx)) } else { err() }
301     }
302 }
303
304 impl<'a, 'tcx, T> LazyQueryDecodable<'a, 'tcx, &'tcx T> for Option<Lazy<T>>
305 where
306     T: Decodable<DecodeContext<'a, 'tcx>>,
307     T: ArenaAllocatable<'tcx>,
308 {
309     fn decode_query(
310         self,
311         cdata: CrateMetadataRef<'a>,
312         tcx: TyCtxt<'tcx>,
313         err: impl FnOnce() -> !,
314     ) -> &'tcx T {
315         if let Some(l) = self { tcx.arena.alloc(l.decode((cdata, tcx))) } else { err() }
316     }
317 }
318
319 impl<'a, 'tcx, T> LazyQueryDecodable<'a, 'tcx, Option<T>> for Option<Lazy<T>>
320 where
321     T: Decodable<DecodeContext<'a, 'tcx>>,
322 {
323     fn decode_query(
324         self,
325         cdata: CrateMetadataRef<'a>,
326         tcx: TyCtxt<'tcx>,
327         _err: impl FnOnce() -> !,
328     ) -> Option<T> {
329         self.map(|l| l.decode((cdata, tcx)))
330     }
331 }
332
333 impl<'a, 'tcx, T, E> LazyQueryDecodable<'a, 'tcx, Result<Option<T>, E>> for Option<Lazy<T>>
334 where
335     T: Decodable<DecodeContext<'a, 'tcx>>,
336 {
337     fn decode_query(
338         self,
339         cdata: CrateMetadataRef<'a>,
340         tcx: TyCtxt<'tcx>,
341         _err: impl FnOnce() -> !,
342     ) -> Result<Option<T>, E> {
343         Ok(self.map(|l| l.decode((cdata, tcx))))
344     }
345 }
346
347 impl<'a, 'tcx, T> LazyQueryDecodable<'a, 'tcx, &'tcx [T]> for Option<Lazy<[T], usize>>
348 where
349     T: Decodable<DecodeContext<'a, 'tcx>> + Copy,
350 {
351     fn decode_query(
352         self,
353         cdata: CrateMetadataRef<'a>,
354         tcx: TyCtxt<'tcx>,
355         _err: impl FnOnce() -> !,
356     ) -> &'tcx [T] {
357         if let Some(l) = self { tcx.arena.alloc_from_iter(l.decode((cdata, tcx))) } else { &[] }
358     }
359 }
360
361 impl<'a, 'tcx> LazyQueryDecodable<'a, 'tcx, Option<DeprecationEntry>>
362     for Option<Lazy<attr::Deprecation>>
363 {
364     fn decode_query(
365         self,
366         cdata: CrateMetadataRef<'a>,
367         tcx: TyCtxt<'tcx>,
368         _err: impl FnOnce() -> !,
369     ) -> Option<DeprecationEntry> {
370         self.map(|l| l.decode((cdata, tcx))).map(DeprecationEntry::external)
371     }
372 }
373
374 impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
375     #[inline]
376     fn tcx(&self) -> TyCtxt<'tcx> {
377         debug_assert!(self.tcx.is_some(), "missing TyCtxt in DecodeContext");
378         self.tcx.unwrap()
379     }
380
381     #[inline]
382     pub fn blob(&self) -> &'a MetadataBlob {
383         self.blob
384     }
385
386     #[inline]
387     pub fn cdata(&self) -> CrateMetadataRef<'a> {
388         debug_assert!(self.cdata.is_some(), "missing CrateMetadata in DecodeContext");
389         self.cdata.unwrap()
390     }
391
392     fn map_encoded_cnum_to_current(&self, cnum: CrateNum) -> CrateNum {
393         if cnum == LOCAL_CRATE { self.cdata().cnum } else { self.cdata().cnum_map[cnum] }
394     }
395
396     fn read_lazy_with_meta<T: ?Sized + LazyMeta>(&mut self, meta: T::Meta) -> Lazy<T> {
397         let distance = self.read_usize();
398         let position = match self.lazy_state {
399             LazyState::NoNode => bug!("read_lazy_with_meta: outside of a metadata node"),
400             LazyState::NodeStart(start) => {
401                 let start = start.get();
402                 assert!(distance <= start);
403                 start - distance
404             }
405             LazyState::Previous(last_pos) => last_pos.get() + distance,
406         };
407         self.lazy_state = LazyState::Previous(NonZeroUsize::new(position).unwrap());
408         Lazy::from_position_and_meta(NonZeroUsize::new(position).unwrap(), meta)
409     }
410
411     #[inline]
412     pub fn read_raw_bytes(&mut self, len: usize) -> &'a [u8] {
413         self.opaque.read_raw_bytes(len)
414     }
415 }
416
417 impl<'a, 'tcx> TyDecoder<'tcx> for DecodeContext<'a, 'tcx> {
418     const CLEAR_CROSS_CRATE: bool = true;
419
420     #[inline]
421     fn tcx(&self) -> TyCtxt<'tcx> {
422         self.tcx.expect("missing TyCtxt in DecodeContext")
423     }
424
425     #[inline]
426     fn peek_byte(&self) -> u8 {
427         self.opaque.data[self.opaque.position()]
428     }
429
430     #[inline]
431     fn position(&self) -> usize {
432         self.opaque.position()
433     }
434
435     fn cached_ty_for_shorthand<F>(&mut self, shorthand: usize, or_insert_with: F) -> Ty<'tcx>
436     where
437         F: FnOnce(&mut Self) -> Ty<'tcx>,
438     {
439         let tcx = self.tcx();
440
441         let key = ty::CReaderCacheKey { cnum: Some(self.cdata().cnum), pos: shorthand };
442
443         if let Some(&ty) = tcx.ty_rcache.borrow().get(&key) {
444             return ty;
445         }
446
447         let ty = or_insert_with(self);
448         tcx.ty_rcache.borrow_mut().insert(key, ty);
449         ty
450     }
451
452     fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
453     where
454         F: FnOnce(&mut Self) -> R,
455     {
456         let new_opaque = opaque::Decoder::new(self.opaque.data, pos);
457         let old_opaque = mem::replace(&mut self.opaque, new_opaque);
458         let old_state = mem::replace(&mut self.lazy_state, LazyState::NoNode);
459         let r = f(self);
460         self.opaque = old_opaque;
461         self.lazy_state = old_state;
462         r
463     }
464
465     fn decode_alloc_id(&mut self) -> rustc_middle::mir::interpret::AllocId {
466         if let Some(alloc_decoding_session) = self.alloc_decoding_session {
467             alloc_decoding_session.decode_alloc_id(self)
468         } else {
469             bug!("Attempting to decode interpret::AllocId without CrateMetadata")
470         }
471     }
472 }
473
474 impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for CrateNum {
475     fn decode(d: &mut DecodeContext<'a, 'tcx>) -> CrateNum {
476         let cnum = CrateNum::from_u32(d.read_u32());
477         d.map_encoded_cnum_to_current(cnum)
478     }
479 }
480
481 impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for DefIndex {
482     fn decode(d: &mut DecodeContext<'a, 'tcx>) -> DefIndex {
483         DefIndex::from_u32(d.read_u32())
484     }
485 }
486
487 impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for ExpnIndex {
488     fn decode(d: &mut DecodeContext<'a, 'tcx>) -> ExpnIndex {
489         ExpnIndex::from_u32(d.read_u32())
490     }
491 }
492
493 impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for SyntaxContext {
494     fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> SyntaxContext {
495         let cdata = decoder.cdata();
496         let sess = decoder.sess.unwrap();
497         let cname = cdata.root.name;
498         rustc_span::hygiene::decode_syntax_context(decoder, &cdata.hygiene_context, |_, id| {
499             debug!("SpecializedDecoder<SyntaxContext>: decoding {}", id);
500             cdata
501                 .root
502                 .syntax_contexts
503                 .get(cdata, id)
504                 .unwrap_or_else(|| panic!("Missing SyntaxContext {:?} for crate {:?}", id, cname))
505                 .decode((cdata, sess))
506         })
507     }
508 }
509
510 impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for ExpnId {
511     fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> ExpnId {
512         let local_cdata = decoder.cdata();
513         let sess = decoder.sess.unwrap();
514
515         let cnum = CrateNum::decode(decoder);
516         let index = u32::decode(decoder);
517
518         let expn_id = rustc_span::hygiene::decode_expn_id(cnum, index, |expn_id| {
519             let ExpnId { krate: cnum, local_id: index } = expn_id;
520             // Lookup local `ExpnData`s in our own crate data. Foreign `ExpnData`s
521             // are stored in the owning crate, to avoid duplication.
522             debug_assert_ne!(cnum, LOCAL_CRATE);
523             let crate_data = if cnum == local_cdata.cnum {
524                 local_cdata
525             } else {
526                 local_cdata.cstore.get_crate_data(cnum)
527             };
528             let expn_data = crate_data
529                 .root
530                 .expn_data
531                 .get(crate_data, index)
532                 .unwrap()
533                 .decode((crate_data, sess));
534             let expn_hash = crate_data
535                 .root
536                 .expn_hashes
537                 .get(crate_data, index)
538                 .unwrap()
539                 .decode((crate_data, sess));
540             (expn_data, expn_hash)
541         });
542         expn_id
543     }
544 }
545
546 impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for Span {
547     fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> Span {
548         let ctxt = SyntaxContext::decode(decoder);
549         let tag = u8::decode(decoder);
550
551         if tag == TAG_PARTIAL_SPAN {
552             return DUMMY_SP.with_ctxt(ctxt);
553         }
554
555         debug_assert!(tag == TAG_VALID_SPAN_LOCAL || tag == TAG_VALID_SPAN_FOREIGN);
556
557         let lo = BytePos::decode(decoder);
558         let len = BytePos::decode(decoder);
559         let hi = lo + len;
560
561         let Some(sess) = decoder.sess else {
562             bug!("Cannot decode Span without Session.")
563         };
564
565         // There are two possibilities here:
566         // 1. This is a 'local span', which is located inside a `SourceFile`
567         // that came from this crate. In this case, we use the source map data
568         // encoded in this crate. This branch should be taken nearly all of the time.
569         // 2. This is a 'foreign span', which is located inside a `SourceFile`
570         // that came from a *different* crate (some crate upstream of the one
571         // whose metadata we're looking at). For example, consider this dependency graph:
572         //
573         // A -> B -> C
574         //
575         // Suppose that we're currently compiling crate A, and start deserializing
576         // metadata from crate B. When we deserialize a Span from crate B's metadata,
577         // there are two posibilites:
578         //
579         // 1. The span references a file from crate B. This makes it a 'local' span,
580         // which means that we can use crate B's serialized source map information.
581         // 2. The span references a file from crate C. This makes it a 'foreign' span,
582         // which means we need to use Crate *C* (not crate B) to determine the source
583         // map information. We only record source map information for a file in the
584         // crate that 'owns' it, so deserializing a Span may require us to look at
585         // a transitive dependency.
586         //
587         // When we encode a foreign span, we adjust its 'lo' and 'high' values
588         // to be based on the *foreign* crate (e.g. crate C), not the crate
589         // we are writing metadata for (e.g. crate B). This allows us to
590         // treat the 'local' and 'foreign' cases almost identically during deserialization:
591         // we can call `imported_source_files` for the proper crate, and binary search
592         // through the returned slice using our span.
593         let imported_source_files = if tag == TAG_VALID_SPAN_LOCAL {
594             decoder.cdata().imported_source_files(sess)
595         } else {
596             // When we encode a proc-macro crate, all `Span`s should be encoded
597             // with `TAG_VALID_SPAN_LOCAL`
598             if decoder.cdata().root.is_proc_macro_crate() {
599                 // Decode `CrateNum` as u32 - using `CrateNum::decode` will ICE
600                 // since we don't have `cnum_map` populated.
601                 let cnum = u32::decode(decoder);
602                 panic!(
603                     "Decoding of crate {:?} tried to access proc-macro dep {:?}",
604                     decoder.cdata().root.name,
605                     cnum
606                 );
607             }
608             // tag is TAG_VALID_SPAN_FOREIGN, checked by `debug_assert` above
609             let cnum = CrateNum::decode(decoder);
610             debug!(
611                 "SpecializedDecoder<Span>::specialized_decode: loading source files from cnum {:?}",
612                 cnum
613             );
614
615             // Decoding 'foreign' spans should be rare enough that it's
616             // not worth it to maintain a per-CrateNum cache for `last_source_file_index`.
617             // We just set it to 0, to ensure that we don't try to access something out
618             // of bounds for our initial 'guess'
619             decoder.last_source_file_index = 0;
620
621             let foreign_data = decoder.cdata().cstore.get_crate_data(cnum);
622             foreign_data.imported_source_files(sess)
623         };
624
625         let source_file = {
626             // Optimize for the case that most spans within a translated item
627             // originate from the same source_file.
628             let last_source_file = &imported_source_files[decoder.last_source_file_index];
629
630             if lo >= last_source_file.original_start_pos && lo <= last_source_file.original_end_pos
631             {
632                 last_source_file
633             } else {
634                 let index = imported_source_files
635                     .binary_search_by_key(&lo, |source_file| source_file.original_start_pos)
636                     .unwrap_or_else(|index| index - 1);
637
638                 // Don't try to cache the index for foreign spans,
639                 // as this would require a map from CrateNums to indices
640                 if tag == TAG_VALID_SPAN_LOCAL {
641                     decoder.last_source_file_index = index;
642                 }
643                 &imported_source_files[index]
644             }
645         };
646
647         // Make sure our binary search above is correct.
648         debug_assert!(
649             lo >= source_file.original_start_pos && lo <= source_file.original_end_pos,
650             "Bad binary search: lo={:?} source_file.original_start_pos={:?} source_file.original_end_pos={:?}",
651             lo,
652             source_file.original_start_pos,
653             source_file.original_end_pos
654         );
655
656         // Make sure we correctly filtered out invalid spans during encoding
657         debug_assert!(
658             hi >= source_file.original_start_pos && hi <= source_file.original_end_pos,
659             "Bad binary search: hi={:?} source_file.original_start_pos={:?} source_file.original_end_pos={:?}",
660             hi,
661             source_file.original_start_pos,
662             source_file.original_end_pos
663         );
664
665         let lo =
666             (lo + source_file.translated_source_file.start_pos) - source_file.original_start_pos;
667         let hi =
668             (hi + source_file.translated_source_file.start_pos) - source_file.original_start_pos;
669
670         // Do not try to decode parent for foreign spans.
671         Span::new(lo, hi, ctxt, None)
672     }
673 }
674
675 impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for &'tcx [thir::abstract_const::Node<'tcx>] {
676     fn decode(d: &mut DecodeContext<'a, 'tcx>) -> Self {
677         ty::codec::RefDecodable::decode(d)
678     }
679 }
680
681 impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for &'tcx [(ty::Predicate<'tcx>, Span)] {
682     fn decode(d: &mut DecodeContext<'a, 'tcx>) -> Self {
683         ty::codec::RefDecodable::decode(d)
684     }
685 }
686
687 impl<'a, 'tcx, T: Decodable<DecodeContext<'a, 'tcx>>> Decodable<DecodeContext<'a, 'tcx>>
688     for Lazy<T>
689 {
690     fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> Self {
691         decoder.read_lazy_with_meta(())
692     }
693 }
694
695 impl<'a, 'tcx, T: Decodable<DecodeContext<'a, 'tcx>>> Decodable<DecodeContext<'a, 'tcx>>
696     for Lazy<[T]>
697 {
698     fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> Self {
699         let len = decoder.read_usize();
700         if len == 0 { Lazy::empty() } else { decoder.read_lazy_with_meta(len) }
701     }
702 }
703
704 impl<'a, 'tcx, I: Idx, T: Decodable<DecodeContext<'a, 'tcx>>> Decodable<DecodeContext<'a, 'tcx>>
705     for Lazy<Table<I, T>>
706 where
707     Option<T>: FixedSizeEncoding,
708 {
709     fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> Self {
710         let len = decoder.read_usize();
711         decoder.read_lazy_with_meta(len)
712     }
713 }
714
715 implement_ty_decoder!(DecodeContext<'a, 'tcx>);
716
717 impl<'tcx> MetadataBlob {
718     crate fn new(metadata_ref: MetadataRef) -> MetadataBlob {
719         MetadataBlob(Lrc::new(metadata_ref))
720     }
721
722     crate fn is_compatible(&self) -> bool {
723         self.blob().starts_with(METADATA_HEADER)
724     }
725
726     crate fn get_rustc_version(&self) -> String {
727         Lazy::<String>::from_position(NonZeroUsize::new(METADATA_HEADER.len() + 4).unwrap())
728             .decode(self)
729     }
730
731     crate fn get_root(&self) -> CrateRoot<'tcx> {
732         let slice = &self.blob()[..];
733         let offset = METADATA_HEADER.len();
734         let pos = (((slice[offset + 0] as u32) << 24)
735             | ((slice[offset + 1] as u32) << 16)
736             | ((slice[offset + 2] as u32) << 8)
737             | ((slice[offset + 3] as u32) << 0)) as usize;
738         Lazy::<CrateRoot<'tcx>>::from_position(NonZeroUsize::new(pos).unwrap()).decode(self)
739     }
740
741     crate fn list_crate_metadata(&self, out: &mut dyn io::Write) -> io::Result<()> {
742         let root = self.get_root();
743         writeln!(out, "Crate info:")?;
744         writeln!(out, "name {}{}", root.name, root.extra_filename)?;
745         writeln!(out, "hash {} stable_crate_id {:?}", root.hash, root.stable_crate_id)?;
746         writeln!(out, "proc_macro {:?}", root.proc_macro_data.is_some())?;
747         writeln!(out, "=External Dependencies=")?;
748         for (i, dep) in root.crate_deps.decode(self).enumerate() {
749             writeln!(
750                 out,
751                 "{} {}{} hash {} host_hash {:?} kind {:?}",
752                 i + 1,
753                 dep.name,
754                 dep.extra_filename,
755                 dep.hash,
756                 dep.host_hash,
757                 dep.kind
758             )?;
759         }
760         write!(out, "\n")?;
761         Ok(())
762     }
763 }
764
765 impl CrateRoot<'_> {
766     crate fn is_proc_macro_crate(&self) -> bool {
767         self.proc_macro_data.is_some()
768     }
769
770     crate fn name(&self) -> Symbol {
771         self.name
772     }
773
774     crate fn hash(&self) -> Svh {
775         self.hash
776     }
777
778     crate fn stable_crate_id(&self) -> StableCrateId {
779         self.stable_crate_id
780     }
781
782     crate fn triple(&self) -> &TargetTriple {
783         &self.triple
784     }
785
786     crate fn decode_crate_deps<'a>(
787         &self,
788         metadata: &'a MetadataBlob,
789     ) -> impl ExactSizeIterator<Item = CrateDep> + Captures<'a> {
790         self.crate_deps.decode(metadata)
791     }
792 }
793
794 impl<'a, 'tcx> CrateMetadataRef<'a> {
795     fn raw_proc_macro(self, id: DefIndex) -> &'a ProcMacro {
796         // DefIndex's in root.proc_macro_data have a one-to-one correspondence
797         // with items in 'raw_proc_macros'.
798         let pos = self
799             .root
800             .proc_macro_data
801             .as_ref()
802             .unwrap()
803             .macros
804             .decode(self)
805             .position(|i| i == id)
806             .unwrap();
807         &self.raw_proc_macros.unwrap()[pos]
808     }
809
810     fn opt_item_name(self, item_index: DefIndex) -> Option<Symbol> {
811         self.def_key(item_index).disambiguated_data.data.get_opt_name()
812     }
813
814     fn item_name(self, item_index: DefIndex) -> Symbol {
815         self.opt_item_name(item_index).expect("no encoded ident for item")
816     }
817
818     fn opt_item_ident(self, item_index: DefIndex, sess: &Session) -> Option<Ident> {
819         let name = self.opt_item_name(item_index)?;
820         let span = match self.root.tables.def_ident_span.get(self, item_index) {
821             Some(lazy_span) => lazy_span.decode((self, sess)),
822             None => {
823                 // FIXME: this weird case of a name with no span is specific to `extern crate`
824                 // items, which are supposed to be treated like `use` items and only be encoded
825                 // to metadata as `Export`s, return `None` because that's what all the callers
826                 // expect in this case.
827                 assert_eq!(self.def_kind(item_index), DefKind::ExternCrate);
828                 return None;
829             }
830         };
831         Some(Ident::new(name, span))
832     }
833
834     fn item_ident(self, item_index: DefIndex, sess: &Session) -> Ident {
835         self.opt_item_ident(item_index, sess).expect("no encoded ident for item")
836     }
837
838     fn maybe_kind(self, item_id: DefIndex) -> Option<EntryKind> {
839         self.root.tables.kind.get(self, item_id).map(|k| k.decode(self))
840     }
841
842     fn kind(self, item_id: DefIndex) -> EntryKind {
843         self.maybe_kind(item_id).unwrap_or_else(|| {
844             bug!(
845                 "CrateMetadata::kind({:?}): id not found, in crate {:?} with number {}",
846                 item_id,
847                 self.root.name,
848                 self.cnum,
849             )
850         })
851     }
852
853     fn def_kind(self, item_id: DefIndex) -> DefKind {
854         self.root.tables.opt_def_kind.get(self, item_id).map(|k| k.decode(self)).unwrap_or_else(
855             || {
856                 bug!(
857                     "CrateMetadata::def_kind({:?}): id not found, in crate {:?} with number {}",
858                     item_id,
859                     self.root.name,
860                     self.cnum,
861                 )
862             },
863         )
864     }
865
866     fn get_span(self, index: DefIndex, sess: &Session) -> Span {
867         self.root
868             .tables
869             .def_span
870             .get(self, index)
871             .unwrap_or_else(|| panic!("Missing span for {:?}", index))
872             .decode((self, sess))
873     }
874
875     fn load_proc_macro(self, id: DefIndex, sess: &Session) -> SyntaxExtension {
876         let (name, kind, helper_attrs) = match *self.raw_proc_macro(id) {
877             ProcMacro::CustomDerive { trait_name, attributes, client } => {
878                 let helper_attrs =
879                     attributes.iter().cloned().map(Symbol::intern).collect::<Vec<_>>();
880                 (
881                     trait_name,
882                     SyntaxExtensionKind::Derive(Box::new(ProcMacroDerive { client })),
883                     helper_attrs,
884                 )
885             }
886             ProcMacro::Attr { name, client } => {
887                 (name, SyntaxExtensionKind::Attr(Box::new(AttrProcMacro { client })), Vec::new())
888             }
889             ProcMacro::Bang { name, client } => {
890                 (name, SyntaxExtensionKind::Bang(Box::new(BangProcMacro { client })), Vec::new())
891             }
892         };
893
894         let attrs: Vec<_> = self.get_item_attrs(id, sess).collect();
895         SyntaxExtension::new(
896             sess,
897             kind,
898             self.get_span(id, sess),
899             helper_attrs,
900             self.root.edition,
901             Symbol::intern(name),
902             &attrs,
903         )
904     }
905
906     fn get_trait_def(self, item_id: DefIndex, sess: &Session) -> ty::TraitDef {
907         match self.kind(item_id) {
908             EntryKind::Trait(data) => {
909                 let data = data.decode((self, sess));
910                 ty::TraitDef::new(
911                     self.local_def_id(item_id),
912                     data.unsafety,
913                     data.paren_sugar,
914                     data.has_auto_impl,
915                     data.is_marker,
916                     data.skip_array_during_method_dispatch,
917                     data.specialization_kind,
918                     self.def_path_hash(item_id),
919                     data.must_implement_one_of,
920                 )
921             }
922             EntryKind::TraitAlias => ty::TraitDef::new(
923                 self.local_def_id(item_id),
924                 hir::Unsafety::Normal,
925                 false,
926                 false,
927                 false,
928                 false,
929                 ty::trait_def::TraitSpecializationKind::None,
930                 self.def_path_hash(item_id),
931                 None,
932             ),
933             _ => bug!("def-index does not refer to trait or trait alias"),
934         }
935     }
936
937     fn get_variant(self, kind: &EntryKind, index: DefIndex, parent_did: DefId) -> ty::VariantDef {
938         let data = match kind {
939             EntryKind::Variant(data) | EntryKind::Struct(data, _) | EntryKind::Union(data, _) => {
940                 data.decode(self)
941             }
942             _ => bug!(),
943         };
944
945         let adt_kind = match kind {
946             EntryKind::Variant(_) => ty::AdtKind::Enum,
947             EntryKind::Struct(..) => ty::AdtKind::Struct,
948             EntryKind::Union(..) => ty::AdtKind::Union,
949             _ => bug!(),
950         };
951
952         let variant_did =
953             if adt_kind == ty::AdtKind::Enum { Some(self.local_def_id(index)) } else { None };
954         let ctor_did = data.ctor.map(|index| self.local_def_id(index));
955
956         ty::VariantDef::new(
957             self.item_name(index),
958             variant_did,
959             ctor_did,
960             data.discr,
961             self.root
962                 .tables
963                 .children
964                 .get(self, index)
965                 .unwrap_or_else(Lazy::empty)
966                 .decode(self)
967                 .map(|index| ty::FieldDef {
968                     did: self.local_def_id(index),
969                     name: self.item_name(index),
970                     vis: self.get_visibility(index),
971                 })
972                 .collect(),
973             data.ctor_kind,
974             adt_kind,
975             parent_did,
976             false,
977             data.is_non_exhaustive,
978         )
979     }
980
981     fn get_adt_def(self, item_id: DefIndex, tcx: TyCtxt<'tcx>) -> &'tcx ty::AdtDef {
982         let kind = self.kind(item_id);
983         let did = self.local_def_id(item_id);
984
985         let (adt_kind, repr) = match kind {
986             EntryKind::Enum(repr) => (ty::AdtKind::Enum, repr),
987             EntryKind::Struct(_, repr) => (ty::AdtKind::Struct, repr),
988             EntryKind::Union(_, repr) => (ty::AdtKind::Union, repr),
989             _ => bug!("get_adt_def called on a non-ADT {:?}", did),
990         };
991
992         let variants = if let ty::AdtKind::Enum = adt_kind {
993             self.root
994                 .tables
995                 .children
996                 .get(self, item_id)
997                 .unwrap_or_else(Lazy::empty)
998                 .decode(self)
999                 .map(|index| self.get_variant(&self.kind(index), index, did))
1000                 .collect()
1001         } else {
1002             std::iter::once(self.get_variant(&kind, item_id, did)).collect()
1003         };
1004
1005         tcx.alloc_adt_def(did, adt_kind, variants, repr)
1006     }
1007
1008     fn get_generics(self, item_id: DefIndex, sess: &Session) -> ty::Generics {
1009         self.root.tables.generics_of.get(self, item_id).unwrap().decode((self, sess))
1010     }
1011
1012     fn get_visibility(self, id: DefIndex) -> ty::Visibility {
1013         self.root.tables.visibility.get(self, id).unwrap().decode(self)
1014     }
1015
1016     fn get_trait_item_def_id(self, id: DefIndex) -> Option<DefId> {
1017         self.root.tables.trait_item_def_id.get(self, id).map(|d| d.decode(self))
1018     }
1019
1020     fn get_expn_that_defined(self, id: DefIndex, sess: &Session) -> ExpnId {
1021         self.root.tables.expn_that_defined.get(self, id).unwrap().decode((self, sess))
1022     }
1023
1024     /// Iterates over all the stability attributes in the given crate.
1025     fn get_lib_features(self, tcx: TyCtxt<'tcx>) -> &'tcx [(Symbol, Option<Symbol>)] {
1026         tcx.arena.alloc_from_iter(self.root.lib_features.decode(self))
1027     }
1028
1029     /// Iterates over the language items in the given crate.
1030     fn get_lang_items(self) -> impl Iterator<Item = (DefId, usize)> + 'a {
1031         self.root
1032             .lang_items
1033             .decode(self)
1034             .map(move |(def_index, index)| (self.local_def_id(def_index), index))
1035     }
1036
1037     /// Iterates over the diagnostic items in the given crate.
1038     fn get_diagnostic_items(self) -> DiagnosticItems {
1039         let mut id_to_name = FxHashMap::default();
1040         let name_to_id = self
1041             .root
1042             .diagnostic_items
1043             .decode(self)
1044             .map(|(name, def_index)| {
1045                 let id = self.local_def_id(def_index);
1046                 id_to_name.insert(id, name);
1047                 (name, id)
1048             })
1049             .collect();
1050         DiagnosticItems { id_to_name, name_to_id }
1051     }
1052
1053     /// Iterates over all named children of the given module,
1054     /// including both proper items and reexports.
1055     /// Module here is understood in name resolution sense - it can be a `mod` item,
1056     /// or a crate root, or an enum, or a trait.
1057     fn for_each_module_child(
1058         self,
1059         id: DefIndex,
1060         mut callback: impl FnMut(ModChild),
1061         sess: &Session,
1062     ) {
1063         if let Some(data) = &self.root.proc_macro_data {
1064             // If we are loading as a proc macro, we want to return
1065             // the view of this crate as a proc macro crate.
1066             if id == CRATE_DEF_INDEX {
1067                 for def_index in data.macros.decode(self) {
1068                     let raw_macro = self.raw_proc_macro(def_index);
1069                     let res = Res::Def(
1070                         DefKind::Macro(macro_kind(raw_macro)),
1071                         self.local_def_id(def_index),
1072                     );
1073                     let ident = self.item_ident(def_index, sess);
1074                     callback(ModChild {
1075                         ident,
1076                         res,
1077                         vis: ty::Visibility::Public,
1078                         span: ident.span,
1079                     });
1080                 }
1081             }
1082             return;
1083         }
1084
1085         // Iterate over all children.
1086         if let Some(children) = self.root.tables.children.get(self, id) {
1087             for child_index in children.decode((self, sess)) {
1088                 if let Some(ident) = self.opt_item_ident(child_index, sess) {
1089                     let kind = self.def_kind(child_index);
1090                     if matches!(kind, DefKind::Macro(..)) {
1091                         // FIXME: Macros are currently encoded twice, once as items and once as
1092                         // reexports. We ignore the items here and only use the reexports.
1093                         continue;
1094                     }
1095                     let def_id = self.local_def_id(child_index);
1096                     let res = Res::Def(kind, def_id);
1097                     let vis = self.get_visibility(child_index);
1098                     let span = self.get_span(child_index, sess);
1099
1100                     callback(ModChild { ident, res, vis, span });
1101
1102                     // For non-re-export structs and variants add their constructors to children.
1103                     // Re-export lists automatically contain constructors when necessary.
1104                     match kind {
1105                         DefKind::Struct => {
1106                             if let Some((ctor_def_id, ctor_kind)) =
1107                                 self.get_ctor_def_id_and_kind(child_index)
1108                             {
1109                                 let ctor_res =
1110                                     Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id);
1111                                 let vis = self.get_visibility(ctor_def_id.index);
1112                                 callback(ModChild { ident, res: ctor_res, vis, span });
1113                             }
1114                         }
1115                         DefKind::Variant => {
1116                             // Braced variants, unlike structs, generate unusable names in
1117                             // value namespace, they are reserved for possible future use.
1118                             // It's ok to use the variant's id as a ctor id since an
1119                             // error will be reported on any use of such resolution anyway.
1120                             let (ctor_def_id, ctor_kind) = self
1121                                 .get_ctor_def_id_and_kind(child_index)
1122                                 .unwrap_or((def_id, CtorKind::Fictive));
1123                             let ctor_res =
1124                                 Res::Def(DefKind::Ctor(CtorOf::Variant, ctor_kind), ctor_def_id);
1125                             let mut vis = self.get_visibility(ctor_def_id.index);
1126                             if ctor_def_id == def_id && vis.is_public() {
1127                                 // For non-exhaustive variants lower the constructor visibility to
1128                                 // within the crate. We only need this for fictive constructors,
1129                                 // for other constructors correct visibilities
1130                                 // were already encoded in metadata.
1131                                 let mut attrs = self.get_item_attrs(def_id.index, sess);
1132                                 if attrs.any(|item| item.has_name(sym::non_exhaustive)) {
1133                                     let crate_def_id = self.local_def_id(CRATE_DEF_INDEX);
1134                                     vis = ty::Visibility::Restricted(crate_def_id);
1135                                 }
1136                             }
1137                             callback(ModChild { ident, res: ctor_res, vis, span });
1138                         }
1139                         _ => {}
1140                     }
1141                 }
1142             }
1143         }
1144
1145         match self.kind(id) {
1146             EntryKind::Mod(exports) => {
1147                 for exp in exports.decode((self, sess)) {
1148                     callback(exp);
1149                 }
1150             }
1151             EntryKind::Enum(..) | EntryKind::Trait(..) => {}
1152             _ => bug!("`for_each_module_child` is called on a non-module: {:?}", self.def_kind(id)),
1153         }
1154     }
1155
1156     fn is_ctfe_mir_available(self, id: DefIndex) -> bool {
1157         self.root.tables.mir_for_ctfe.get(self, id).is_some()
1158     }
1159
1160     fn is_item_mir_available(self, id: DefIndex) -> bool {
1161         self.root.tables.optimized_mir.get(self, id).is_some()
1162     }
1163
1164     fn module_expansion(self, id: DefIndex, sess: &Session) -> ExpnId {
1165         match self.kind(id) {
1166             EntryKind::Mod(_) | EntryKind::Enum(_) | EntryKind::Trait(_) => {
1167                 self.get_expn_that_defined(id, sess)
1168             }
1169             _ => panic!("Expected module, found {:?}", self.local_def_id(id)),
1170         }
1171     }
1172
1173     fn get_fn_has_self_parameter(self, id: DefIndex) -> bool {
1174         match self.kind(id) {
1175             EntryKind::AssocFn(data) => data.decode(self).has_self,
1176             _ => false,
1177         }
1178     }
1179
1180     fn get_associated_item_def_ids(self, tcx: TyCtxt<'tcx>, id: DefIndex) -> &'tcx [DefId] {
1181         if let Some(children) = self.root.tables.children.get(self, id) {
1182             tcx.arena.alloc_from_iter(
1183                 children.decode((self, tcx.sess)).map(|child_index| self.local_def_id(child_index)),
1184             )
1185         } else {
1186             &[]
1187         }
1188     }
1189
1190     fn get_associated_item(self, id: DefIndex) -> ty::AssocItem {
1191         let def_key = self.def_key(id);
1192         let parent = self.local_def_id(def_key.parent.unwrap());
1193         let name = self.item_name(id);
1194
1195         let (kind, container, has_self) = match self.kind(id) {
1196             EntryKind::AssocConst(container) => (ty::AssocKind::Const, container, false),
1197             EntryKind::AssocFn(data) => {
1198                 let data = data.decode(self);
1199                 (ty::AssocKind::Fn, data.container, data.has_self)
1200             }
1201             EntryKind::AssocType(container) => (ty::AssocKind::Type, container, false),
1202             _ => bug!("cannot get associated-item of `{:?}`", def_key),
1203         };
1204
1205         ty::AssocItem {
1206             name,
1207             kind,
1208             vis: self.get_visibility(id),
1209             defaultness: container.defaultness(),
1210             def_id: self.local_def_id(id),
1211             trait_item_def_id: self.get_trait_item_def_id(id),
1212             container: container.with_def_id(parent),
1213             fn_has_self_parameter: has_self,
1214         }
1215     }
1216
1217     fn get_ctor_def_id_and_kind(self, node_id: DefIndex) -> Option<(DefId, CtorKind)> {
1218         match self.kind(node_id) {
1219             EntryKind::Struct(data, _) | EntryKind::Variant(data) => {
1220                 let vdata = data.decode(self);
1221                 vdata.ctor.map(|index| (self.local_def_id(index), vdata.ctor_kind))
1222             }
1223             _ => None,
1224         }
1225     }
1226
1227     fn get_item_attrs(
1228         self,
1229         id: DefIndex,
1230         sess: &'a Session,
1231     ) -> impl Iterator<Item = ast::Attribute> + 'a {
1232         self.root
1233             .tables
1234             .attributes
1235             .get(self, id)
1236             .unwrap_or_else(|| {
1237                 // Structure and variant constructors don't have any attributes encoded for them,
1238                 // but we assume that someone passing a constructor ID actually wants to look at
1239                 // the attributes on the corresponding struct or variant.
1240                 let def_key = self.def_key(id);
1241                 assert_eq!(def_key.disambiguated_data.data, DefPathData::Ctor);
1242                 let parent_id = def_key.parent.expect("no parent for a constructor");
1243                 self.root
1244                     .tables
1245                     .attributes
1246                     .get(self, parent_id)
1247                     .expect("no encoded attributes for a structure or variant")
1248             })
1249             .decode((self, sess))
1250     }
1251
1252     fn get_struct_field_names(
1253         self,
1254         id: DefIndex,
1255         sess: &'a Session,
1256     ) -> impl Iterator<Item = Spanned<Symbol>> + 'a {
1257         self.root
1258             .tables
1259             .children
1260             .get(self, id)
1261             .unwrap_or_else(Lazy::empty)
1262             .decode(self)
1263             .map(move |index| respan(self.get_span(index, sess), self.item_name(index)))
1264     }
1265
1266     fn get_struct_field_visibilities(self, id: DefIndex) -> impl Iterator<Item = Visibility> + 'a {
1267         self.root
1268             .tables
1269             .children
1270             .get(self, id)
1271             .unwrap_or_else(Lazy::empty)
1272             .decode(self)
1273             .map(move |field_index| self.get_visibility(field_index))
1274     }
1275
1276     fn get_inherent_implementations_for_type(
1277         self,
1278         tcx: TyCtxt<'tcx>,
1279         id: DefIndex,
1280     ) -> &'tcx [DefId] {
1281         tcx.arena.alloc_from_iter(
1282             self.root
1283                 .tables
1284                 .inherent_impls
1285                 .get(self, id)
1286                 .unwrap_or_else(Lazy::empty)
1287                 .decode(self)
1288                 .map(|index| self.local_def_id(index)),
1289         )
1290     }
1291
1292     /// Decodes all inherent impls in the crate (for rustdoc).
1293     fn get_inherent_impls(self) -> impl Iterator<Item = (DefId, DefId)> + 'a {
1294         (0..self.root.tables.inherent_impls.size()).flat_map(move |i| {
1295             let ty_index = DefIndex::from_usize(i);
1296             let ty_def_id = self.local_def_id(ty_index);
1297             self.root
1298                 .tables
1299                 .inherent_impls
1300                 .get(self, ty_index)
1301                 .unwrap_or_else(Lazy::empty)
1302                 .decode(self)
1303                 .map(move |impl_index| (ty_def_id, self.local_def_id(impl_index)))
1304         })
1305     }
1306
1307     /// Decodes all traits in the crate (for rustdoc and rustc diagnostics).
1308     fn get_traits(self) -> impl Iterator<Item = DefId> + 'a {
1309         self.root.traits.decode(self).map(move |index| self.local_def_id(index))
1310     }
1311
1312     /// Decodes all trait impls in the crate (for rustdoc).
1313     fn get_trait_impls(self) -> impl Iterator<Item = (DefId, DefId, Option<SimplifiedType>)> + 'a {
1314         self.cdata.trait_impls.iter().flat_map(move |((trait_cnum_raw, trait_index), impls)| {
1315             let trait_def_id = DefId {
1316                 krate: self.cnum_map[CrateNum::from_u32(*trait_cnum_raw)],
1317                 index: *trait_index,
1318             };
1319             impls.decode(self).map(move |(impl_index, simplified_self_ty)| {
1320                 (trait_def_id, self.local_def_id(impl_index), simplified_self_ty)
1321             })
1322         })
1323     }
1324
1325     fn get_implementations_of_trait(
1326         self,
1327         tcx: TyCtxt<'tcx>,
1328         trait_def_id: DefId,
1329     ) -> &'tcx [(DefId, Option<SimplifiedType>)] {
1330         if self.trait_impls.is_empty() {
1331             return &[];
1332         }
1333
1334         // Do a reverse lookup beforehand to avoid touching the crate_num
1335         // hash map in the loop below.
1336         let key = match self.reverse_translate_def_id(trait_def_id) {
1337             Some(def_id) => (def_id.krate.as_u32(), def_id.index),
1338             None => return &[],
1339         };
1340
1341         if let Some(impls) = self.trait_impls.get(&key) {
1342             tcx.arena.alloc_from_iter(
1343                 impls
1344                     .decode(self)
1345                     .map(|(idx, simplified_self_ty)| (self.local_def_id(idx), simplified_self_ty)),
1346             )
1347         } else {
1348             &[]
1349         }
1350     }
1351
1352     fn get_trait_of_item(self, id: DefIndex) -> Option<DefId> {
1353         let def_key = self.def_key(id);
1354         match def_key.disambiguated_data.data {
1355             DefPathData::TypeNs(..) | DefPathData::ValueNs(..) => (),
1356             // Not an associated item
1357             _ => return None,
1358         }
1359         def_key.parent.and_then(|parent_index| match self.kind(parent_index) {
1360             EntryKind::Trait(_) | EntryKind::TraitAlias => Some(self.local_def_id(parent_index)),
1361             _ => None,
1362         })
1363     }
1364
1365     fn get_native_libraries(self, sess: &'a Session) -> impl Iterator<Item = NativeLib> + 'a {
1366         self.root.native_libraries.decode((self, sess))
1367     }
1368
1369     fn get_proc_macro_quoted_span(self, index: usize, sess: &Session) -> Span {
1370         self.root
1371             .tables
1372             .proc_macro_quoted_spans
1373             .get(self, index)
1374             .unwrap_or_else(|| panic!("Missing proc macro quoted span: {:?}", index))
1375             .decode((self, sess))
1376     }
1377
1378     fn get_foreign_modules(self, sess: &'a Session) -> impl Iterator<Item = ForeignModule> + '_ {
1379         self.root.foreign_modules.decode((self, sess))
1380     }
1381
1382     fn get_dylib_dependency_formats(
1383         self,
1384         tcx: TyCtxt<'tcx>,
1385     ) -> &'tcx [(CrateNum, LinkagePreference)] {
1386         tcx.arena.alloc_from_iter(
1387             self.root.dylib_dependency_formats.decode(self).enumerate().flat_map(|(i, link)| {
1388                 let cnum = CrateNum::new(i + 1);
1389                 link.map(|link| (self.cnum_map[cnum], link))
1390             }),
1391         )
1392     }
1393
1394     fn get_missing_lang_items(self, tcx: TyCtxt<'tcx>) -> &'tcx [lang_items::LangItem] {
1395         tcx.arena.alloc_from_iter(self.root.lang_items_missing.decode(self))
1396     }
1397
1398     fn exported_symbols(
1399         self,
1400         tcx: TyCtxt<'tcx>,
1401     ) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportLevel)] {
1402         tcx.arena.alloc_from_iter(self.root.exported_symbols.decode((self, tcx)))
1403     }
1404
1405     fn get_macro(self, id: DefIndex, sess: &Session) -> MacroDef {
1406         match self.kind(id) {
1407             EntryKind::MacroDef(macro_def) => macro_def.decode((self, sess)),
1408             _ => bug!(),
1409         }
1410     }
1411
1412     // This replicates some of the logic of the crate-local `is_const_fn_raw` query, because we
1413     // don't serialize constness for tuple variant and tuple struct constructors.
1414     fn is_const_fn_raw(self, id: DefIndex) -> bool {
1415         let constness = match self.kind(id) {
1416             EntryKind::AssocFn(data) => data.decode(self).fn_data.constness,
1417             EntryKind::Fn(data) => data.decode(self).constness,
1418             EntryKind::ForeignFn(data) => data.decode(self).constness,
1419             EntryKind::Variant(..) | EntryKind::Struct(..) => hir::Constness::Const,
1420             _ => hir::Constness::NotConst,
1421         };
1422         constness == hir::Constness::Const
1423     }
1424
1425     fn is_foreign_item(self, id: DefIndex) -> bool {
1426         match self.kind(id) {
1427             EntryKind::ForeignImmStatic | EntryKind::ForeignMutStatic | EntryKind::ForeignFn(_) => {
1428                 true
1429             }
1430             _ => false,
1431         }
1432     }
1433
1434     fn static_mutability(self, id: DefIndex) -> Option<hir::Mutability> {
1435         match self.kind(id) {
1436             EntryKind::ImmStatic | EntryKind::ForeignImmStatic => Some(hir::Mutability::Not),
1437             EntryKind::MutStatic | EntryKind::ForeignMutStatic => Some(hir::Mutability::Mut),
1438             _ => None,
1439         }
1440     }
1441
1442     fn generator_kind(self, id: DefIndex) -> Option<hir::GeneratorKind> {
1443         match self.kind(id) {
1444             EntryKind::Generator(data) => Some(data),
1445             _ => None,
1446         }
1447     }
1448
1449     #[inline]
1450     fn def_key(self, index: DefIndex) -> DefKey {
1451         *self
1452             .def_key_cache
1453             .lock()
1454             .entry(index)
1455             .or_insert_with(|| self.root.tables.def_keys.get(self, index).unwrap().decode(self))
1456     }
1457
1458     // Returns the path leading to the thing with this `id`.
1459     fn def_path(self, id: DefIndex) -> DefPath {
1460         debug!("def_path(cnum={:?}, id={:?})", self.cnum, id);
1461         DefPath::make(self.cnum, id, |parent| self.def_key(parent))
1462     }
1463
1464     fn def_path_hash_unlocked(
1465         self,
1466         index: DefIndex,
1467         def_path_hashes: &mut FxHashMap<DefIndex, DefPathHash>,
1468     ) -> DefPathHash {
1469         *def_path_hashes.entry(index).or_insert_with(|| {
1470             self.root.tables.def_path_hashes.get(self, index).unwrap().decode(self)
1471         })
1472     }
1473
1474     #[inline]
1475     fn def_path_hash(self, index: DefIndex) -> DefPathHash {
1476         let mut def_path_hashes = self.def_path_hash_cache.lock();
1477         self.def_path_hash_unlocked(index, &mut def_path_hashes)
1478     }
1479
1480     #[inline]
1481     fn def_path_hash_to_def_index(self, hash: DefPathHash) -> DefIndex {
1482         self.def_path_hash_map.def_path_hash_to_def_index(&hash)
1483     }
1484
1485     fn expn_hash_to_expn_id(self, sess: &Session, index_guess: u32, hash: ExpnHash) -> ExpnId {
1486         debug_assert_eq!(ExpnId::from_hash(hash), None);
1487         let index_guess = ExpnIndex::from_u32(index_guess);
1488         let old_hash = self.root.expn_hashes.get(self, index_guess).map(|lazy| lazy.decode(self));
1489
1490         let index = if old_hash == Some(hash) {
1491             // Fast path: the expn and its index is unchanged from the
1492             // previous compilation session. There is no need to decode anything
1493             // else.
1494             index_guess
1495         } else {
1496             // Slow path: We need to find out the new `DefIndex` of the provided
1497             // `DefPathHash`, if its still exists. This requires decoding every `DefPathHash`
1498             // stored in this crate.
1499             let map = self.cdata.expn_hash_map.get_or_init(|| {
1500                 let end_id = self.root.expn_hashes.size() as u32;
1501                 let mut map =
1502                     UnhashMap::with_capacity_and_hasher(end_id as usize, Default::default());
1503                 for i in 0..end_id {
1504                     let i = ExpnIndex::from_u32(i);
1505                     if let Some(hash) = self.root.expn_hashes.get(self, i) {
1506                         map.insert(hash.decode(self), i);
1507                     }
1508                 }
1509                 map
1510             });
1511             map[&hash]
1512         };
1513
1514         let data = self.root.expn_data.get(self, index).unwrap().decode((self, sess));
1515         rustc_span::hygiene::register_expn_id(self.cnum, index, data, hash)
1516     }
1517
1518     /// Imports the source_map from an external crate into the source_map of the crate
1519     /// currently being compiled (the "local crate").
1520     ///
1521     /// The import algorithm works analogous to how AST items are inlined from an
1522     /// external crate's metadata:
1523     /// For every SourceFile in the external source_map an 'inline' copy is created in the
1524     /// local source_map. The correspondence relation between external and local
1525     /// SourceFiles is recorded in the `ImportedSourceFile` objects returned from this
1526     /// function. When an item from an external crate is later inlined into this
1527     /// crate, this correspondence information is used to translate the span
1528     /// information of the inlined item so that it refers the correct positions in
1529     /// the local source_map (see `<decoder::DecodeContext as SpecializedDecoder<Span>>`).
1530     ///
1531     /// The import algorithm in the function below will reuse SourceFiles already
1532     /// existing in the local source_map. For example, even if the SourceFile of some
1533     /// source file of libstd gets imported many times, there will only ever be
1534     /// one SourceFile object for the corresponding file in the local source_map.
1535     ///
1536     /// Note that imported SourceFiles do not actually contain the source code of the
1537     /// file they represent, just information about length, line breaks, and
1538     /// multibyte characters. This information is enough to generate valid debuginfo
1539     /// for items inlined from other crates.
1540     ///
1541     /// Proc macro crates don't currently export spans, so this function does not have
1542     /// to work for them.
1543     fn imported_source_files(self, sess: &Session) -> &'a [ImportedSourceFile] {
1544         // Translate the virtual `/rustc/$hash` prefix back to a real directory
1545         // that should hold actual sources, where possible.
1546         //
1547         // NOTE: if you update this, you might need to also update bootstrap's code for generating
1548         // the `rust-src` component in `Src::run` in `src/bootstrap/dist.rs`.
1549         let virtual_rust_source_base_dir = option_env!("CFG_VIRTUAL_RUST_SOURCE_BASE_DIR")
1550             .map(Path::new)
1551             .filter(|_| {
1552                 // Only spend time on further checks if we have what to translate *to*.
1553                 sess.opts.real_rust_source_base_dir.is_some()
1554             })
1555             .filter(|virtual_dir| {
1556                 // Don't translate away `/rustc/$hash` if we're still remapping to it,
1557                 // since that means we're still building `std`/`rustc` that need it,
1558                 // and we don't want the real path to leak into codegen/debuginfo.
1559                 !sess.opts.remap_path_prefix.iter().any(|(_from, to)| to == virtual_dir)
1560             });
1561         let try_to_translate_virtual_to_real = |name: &mut rustc_span::FileName| {
1562             debug!(
1563                 "try_to_translate_virtual_to_real(name={:?}): \
1564                  virtual_rust_source_base_dir={:?}, real_rust_source_base_dir={:?}",
1565                 name, virtual_rust_source_base_dir, sess.opts.real_rust_source_base_dir,
1566             );
1567
1568             if let Some(virtual_dir) = virtual_rust_source_base_dir {
1569                 if let Some(real_dir) = &sess.opts.real_rust_source_base_dir {
1570                     if let rustc_span::FileName::Real(old_name) = name {
1571                         if let rustc_span::RealFileName::Remapped { local_path: _, virtual_name } =
1572                             old_name
1573                         {
1574                             if let Ok(rest) = virtual_name.strip_prefix(virtual_dir) {
1575                                 let virtual_name = virtual_name.clone();
1576
1577                                 // The std library crates are in
1578                                 // `$sysroot/lib/rustlib/src/rust/library`, whereas other crates
1579                                 // may be in `$sysroot/lib/rustlib/src/rust/` directly. So we
1580                                 // detect crates from the std libs and handle them specially.
1581                                 const STD_LIBS: &[&str] = &[
1582                                     "core",
1583                                     "alloc",
1584                                     "std",
1585                                     "test",
1586                                     "term",
1587                                     "unwind",
1588                                     "proc_macro",
1589                                     "panic_abort",
1590                                     "panic_unwind",
1591                                     "profiler_builtins",
1592                                     "rtstartup",
1593                                     "rustc-std-workspace-core",
1594                                     "rustc-std-workspace-alloc",
1595                                     "rustc-std-workspace-std",
1596                                     "backtrace",
1597                                 ];
1598                                 let is_std_lib = STD_LIBS.iter().any(|l| rest.starts_with(l));
1599
1600                                 let new_path = if is_std_lib {
1601                                     real_dir.join("library").join(rest)
1602                                 } else {
1603                                     real_dir.join(rest)
1604                                 };
1605
1606                                 debug!(
1607                                     "try_to_translate_virtual_to_real: `{}` -> `{}`",
1608                                     virtual_name.display(),
1609                                     new_path.display(),
1610                                 );
1611                                 let new_name = rustc_span::RealFileName::Remapped {
1612                                     local_path: Some(new_path),
1613                                     virtual_name,
1614                                 };
1615                                 *old_name = new_name;
1616                             }
1617                         }
1618                     }
1619                 }
1620             }
1621         };
1622
1623         self.cdata.source_map_import_info.get_or_init(|| {
1624             let external_source_map = self.root.source_map.decode(self);
1625
1626             external_source_map
1627                 .map(|source_file_to_import| {
1628                     // We can't reuse an existing SourceFile, so allocate a new one
1629                     // containing the information we need.
1630                     let rustc_span::SourceFile {
1631                         mut name,
1632                         src_hash,
1633                         start_pos,
1634                         end_pos,
1635                         mut lines,
1636                         mut multibyte_chars,
1637                         mut non_narrow_chars,
1638                         mut normalized_pos,
1639                         name_hash,
1640                         ..
1641                     } = source_file_to_import;
1642
1643                     // If this file is under $sysroot/lib/rustlib/src/ but has not been remapped
1644                     // during rust bootstrapping by `remap-debuginfo = true`, and the user
1645                     // wish to simulate that behaviour by -Z simulate-remapped-rust-src-base,
1646                     // then we change `name` to a similar state as if the rust was bootstrapped
1647                     // with `remap-debuginfo = true`.
1648                     // This is useful for testing so that tests about the effects of
1649                     // `try_to_translate_virtual_to_real` don't have to worry about how the
1650                     // compiler is bootstrapped.
1651                     if let Some(virtual_dir) =
1652                         &sess.opts.debugging_opts.simulate_remapped_rust_src_base
1653                     {
1654                         if let Some(real_dir) = &sess.opts.real_rust_source_base_dir {
1655                             if let rustc_span::FileName::Real(ref mut old_name) = name {
1656                                 if let rustc_span::RealFileName::LocalPath(local) = old_name {
1657                                     if let Ok(rest) = local.strip_prefix(real_dir) {
1658                                         *old_name = rustc_span::RealFileName::Remapped {
1659                                             local_path: None,
1660                                             virtual_name: virtual_dir.join(rest),
1661                                         };
1662                                     }
1663                                 }
1664                             }
1665                         }
1666                     }
1667
1668                     // If this file's path has been remapped to `/rustc/$hash`,
1669                     // we might be able to reverse that (also see comments above,
1670                     // on `try_to_translate_virtual_to_real`).
1671                     try_to_translate_virtual_to_real(&mut name);
1672
1673                     let source_length = (end_pos - start_pos).to_usize();
1674
1675                     // Translate line-start positions and multibyte character
1676                     // position into frame of reference local to file.
1677                     // `SourceMap::new_imported_source_file()` will then translate those
1678                     // coordinates to their new global frame of reference when the
1679                     // offset of the SourceFile is known.
1680                     for pos in &mut lines {
1681                         *pos = *pos - start_pos;
1682                     }
1683                     for mbc in &mut multibyte_chars {
1684                         mbc.pos = mbc.pos - start_pos;
1685                     }
1686                     for swc in &mut non_narrow_chars {
1687                         *swc = *swc - start_pos;
1688                     }
1689                     for np in &mut normalized_pos {
1690                         np.pos = np.pos - start_pos;
1691                     }
1692
1693                     let local_version = sess.source_map().new_imported_source_file(
1694                         name,
1695                         src_hash,
1696                         name_hash,
1697                         source_length,
1698                         self.cnum,
1699                         lines,
1700                         multibyte_chars,
1701                         non_narrow_chars,
1702                         normalized_pos,
1703                         start_pos,
1704                         end_pos,
1705                     );
1706                     debug!(
1707                         "CrateMetaData::imported_source_files alloc \
1708                          source_file {:?} original (start_pos {:?} end_pos {:?}) \
1709                          translated (start_pos {:?} end_pos {:?})",
1710                         local_version.name,
1711                         start_pos,
1712                         end_pos,
1713                         local_version.start_pos,
1714                         local_version.end_pos
1715                     );
1716
1717                     ImportedSourceFile {
1718                         original_start_pos: start_pos,
1719                         original_end_pos: end_pos,
1720                         translated_source_file: local_version,
1721                     }
1722                 })
1723                 .collect()
1724         })
1725     }
1726 }
1727
1728 impl CrateMetadata {
1729     crate fn new(
1730         sess: &Session,
1731         blob: MetadataBlob,
1732         root: CrateRoot<'static>,
1733         raw_proc_macros: Option<&'static [ProcMacro]>,
1734         cnum: CrateNum,
1735         cnum_map: CrateNumMap,
1736         dep_kind: CrateDepKind,
1737         source: CrateSource,
1738         private_dep: bool,
1739         host_hash: Option<Svh>,
1740     ) -> CrateMetadata {
1741         let trait_impls = root
1742             .impls
1743             .decode((&blob, sess))
1744             .map(|trait_impls| (trait_impls.trait_id, trait_impls.impls))
1745             .collect();
1746         let alloc_decoding_state =
1747             AllocDecodingState::new(root.interpret_alloc_index.decode(&blob).collect());
1748         let dependencies = Lock::new(cnum_map.iter().cloned().collect());
1749
1750         // Pre-decode the DefPathHash->DefIndex table. This is a cheap operation
1751         // that does not copy any data. It just does some data verification.
1752         let def_path_hash_map = root.def_path_hash_map.decode(&blob);
1753
1754         CrateMetadata {
1755             blob,
1756             root,
1757             trait_impls,
1758             raw_proc_macros,
1759             source_map_import_info: OnceCell::new(),
1760             def_path_hash_map,
1761             expn_hash_map: Default::default(),
1762             alloc_decoding_state,
1763             cnum,
1764             cnum_map,
1765             dependencies,
1766             dep_kind: Lock::new(dep_kind),
1767             source: Lrc::new(source),
1768             private_dep,
1769             host_hash,
1770             extern_crate: Lock::new(None),
1771             hygiene_context: Default::default(),
1772             def_key_cache: Default::default(),
1773             def_path_hash_cache: Default::default(),
1774         }
1775     }
1776
1777     crate fn dependencies(&self) -> LockGuard<'_, Vec<CrateNum>> {
1778         self.dependencies.borrow()
1779     }
1780
1781     crate fn add_dependency(&self, cnum: CrateNum) {
1782         self.dependencies.borrow_mut().push(cnum);
1783     }
1784
1785     crate fn update_extern_crate(&self, new_extern_crate: ExternCrate) -> bool {
1786         let mut extern_crate = self.extern_crate.borrow_mut();
1787         let update = Some(new_extern_crate.rank()) > extern_crate.as_ref().map(ExternCrate::rank);
1788         if update {
1789             *extern_crate = Some(new_extern_crate);
1790         }
1791         update
1792     }
1793
1794     crate fn source(&self) -> &CrateSource {
1795         &*self.source
1796     }
1797
1798     crate fn dep_kind(&self) -> CrateDepKind {
1799         *self.dep_kind.lock()
1800     }
1801
1802     crate fn update_dep_kind(&self, f: impl FnOnce(CrateDepKind) -> CrateDepKind) {
1803         self.dep_kind.with_lock(|dep_kind| *dep_kind = f(*dep_kind))
1804     }
1805
1806     crate fn panic_strategy(&self) -> PanicStrategy {
1807         self.root.panic_strategy
1808     }
1809
1810     crate fn needs_panic_runtime(&self) -> bool {
1811         self.root.needs_panic_runtime
1812     }
1813
1814     crate fn is_panic_runtime(&self) -> bool {
1815         self.root.panic_runtime
1816     }
1817
1818     crate fn is_profiler_runtime(&self) -> bool {
1819         self.root.profiler_runtime
1820     }
1821
1822     crate fn needs_allocator(&self) -> bool {
1823         self.root.needs_allocator
1824     }
1825
1826     crate fn has_global_allocator(&self) -> bool {
1827         self.root.has_global_allocator
1828     }
1829
1830     crate fn has_default_lib_allocator(&self) -> bool {
1831         self.root.has_default_lib_allocator
1832     }
1833
1834     crate fn is_proc_macro_crate(&self) -> bool {
1835         self.root.is_proc_macro_crate()
1836     }
1837
1838     crate fn name(&self) -> Symbol {
1839         self.root.name
1840     }
1841
1842     crate fn stable_crate_id(&self) -> StableCrateId {
1843         self.root.stable_crate_id
1844     }
1845
1846     crate fn hash(&self) -> Svh {
1847         self.root.hash
1848     }
1849
1850     fn num_def_ids(&self) -> usize {
1851         self.root.tables.def_keys.size()
1852     }
1853
1854     fn local_def_id(&self, index: DefIndex) -> DefId {
1855         DefId { krate: self.cnum, index }
1856     }
1857
1858     // Translate a DefId from the current compilation environment to a DefId
1859     // for an external crate.
1860     fn reverse_translate_def_id(&self, did: DefId) -> Option<DefId> {
1861         for (local, &global) in self.cnum_map.iter_enumerated() {
1862             if global == did.krate {
1863                 return Some(DefId { krate: local, index: did.index });
1864             }
1865         }
1866
1867         None
1868     }
1869 }
1870
1871 // Cannot be implemented on 'ProcMacro', as libproc_macro
1872 // does not depend on librustc_ast
1873 fn macro_kind(raw: &ProcMacro) -> MacroKind {
1874     match raw {
1875         ProcMacro::CustomDerive { .. } => MacroKind::Derive,
1876         ProcMacro::Attr { .. } => MacroKind::Attr,
1877         ProcMacro::Bang { .. } => MacroKind::Bang,
1878     }
1879 }