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