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