rustc_metadata/rmeta/
encoder.rs

1use std::borrow::Borrow;
2use std::collections::hash_map::Entry;
3use std::fs::File;
4use std::io::{Read, Seek, Write};
5use std::path::{Path, PathBuf};
6use std::sync::Arc;
7
8use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
9use rustc_data_structures::memmap::{Mmap, MmapMut};
10use rustc_data_structures::sync::{join, par_for_each_in};
11use rustc_data_structures::temp_dir::MaybeTempDir;
12use rustc_data_structures::thousands::usize_with_underscores;
13use rustc_feature::Features;
14use rustc_hir as hir;
15use rustc_hir::attrs::{AttributeKind, EncodeCrossCrate};
16use rustc_hir::def_id::{CRATE_DEF_ID, CRATE_DEF_INDEX, LOCAL_CRATE, LocalDefId, LocalDefIdSet};
17use rustc_hir::definitions::DefPathData;
18use rustc_hir::find_attr;
19use rustc_hir_pretty::id_to_string;
20use rustc_middle::dep_graph::WorkProductId;
21use rustc_middle::middle::dependency_format::Linkage;
22use rustc_middle::mir::interpret;
23use rustc_middle::query::Providers;
24use rustc_middle::traits::specialization_graph;
25use rustc_middle::ty::AssocContainer;
26use rustc_middle::ty::codec::TyEncoder;
27use rustc_middle::ty::fast_reject::{self, TreatParams};
28use rustc_middle::{bug, span_bug};
29use rustc_serialize::{Decodable, Decoder, Encodable, Encoder, opaque};
30use rustc_session::config::{CrateType, OptLevel, TargetModifier};
31use rustc_span::hygiene::HygieneEncodeContext;
32use rustc_span::{
33    ByteSymbol, ExternalSource, FileName, SourceFile, SpanData, SpanEncoder, StableSourceFileId,
34    Symbol, SyntaxContext, sym,
35};
36use tracing::{debug, instrument, trace};
37
38use crate::errors::{FailCreateFileEncoder, FailWriteFile};
39use crate::rmeta::*;
40
41pub(super) struct EncodeContext<'a, 'tcx> {
42    opaque: opaque::FileEncoder,
43    tcx: TyCtxt<'tcx>,
44    feat: &'tcx rustc_feature::Features,
45    tables: TableBuilders,
46
47    lazy_state: LazyState,
48    span_shorthands: FxHashMap<Span, usize>,
49    type_shorthands: FxHashMap<Ty<'tcx>, usize>,
50    predicate_shorthands: FxHashMap<ty::PredicateKind<'tcx>, usize>,
51
52    interpret_allocs: FxIndexSet<interpret::AllocId>,
53
54    // This is used to speed up Span encoding.
55    // The `usize` is an index into the `MonotonicVec`
56    // that stores the `SourceFile`
57    source_file_cache: (Arc<SourceFile>, usize),
58    // The indices (into the `SourceMap`'s `MonotonicVec`)
59    // of all of the `SourceFiles` that we need to serialize.
60    // When we serialize a `Span`, we insert the index of its
61    // `SourceFile` into the `FxIndexSet`.
62    // The order inside the `FxIndexSet` is used as on-disk
63    // order of `SourceFiles`, and encoded inside `Span`s.
64    required_source_files: Option<FxIndexSet<usize>>,
65    is_proc_macro: bool,
66    hygiene_ctxt: &'a HygieneEncodeContext,
67    // Used for both `Symbol`s and `ByteSymbol`s.
68    symbol_index_table: FxHashMap<u32, usize>,
69}
70
71/// If the current crate is a proc-macro, returns early with `LazyArray::default()`.
72/// This is useful for skipping the encoding of things that aren't needed
73/// for proc-macro crates.
74macro_rules! empty_proc_macro {
75    ($self:ident) => {
76        if $self.is_proc_macro {
77            return LazyArray::default();
78        }
79    };
80}
81
82macro_rules! encoder_methods {
83    ($($name:ident($ty:ty);)*) => {
84        $(fn $name(&mut self, value: $ty) {
85            self.opaque.$name(value)
86        })*
87    }
88}
89
90impl<'a, 'tcx> Encoder for EncodeContext<'a, 'tcx> {
91    encoder_methods! {
92        emit_usize(usize);
93        emit_u128(u128);
94        emit_u64(u64);
95        emit_u32(u32);
96        emit_u16(u16);
97        emit_u8(u8);
98
99        emit_isize(isize);
100        emit_i128(i128);
101        emit_i64(i64);
102        emit_i32(i32);
103        emit_i16(i16);
104
105        emit_raw_bytes(&[u8]);
106    }
107}
108
109impl<'a, 'tcx, T> Encodable<EncodeContext<'a, 'tcx>> for LazyValue<T> {
110    fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) {
111        e.emit_lazy_distance(self.position);
112    }
113}
114
115impl<'a, 'tcx, T> Encodable<EncodeContext<'a, 'tcx>> for LazyArray<T> {
116    fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) {
117        e.emit_usize(self.num_elems);
118        if self.num_elems > 0 {
119            e.emit_lazy_distance(self.position)
120        }
121    }
122}
123
124impl<'a, 'tcx, I, T> Encodable<EncodeContext<'a, 'tcx>> for LazyTable<I, T> {
125    fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) {
126        e.emit_usize(self.width);
127        e.emit_usize(self.len);
128        e.emit_lazy_distance(self.position);
129    }
130}
131
132impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for ExpnIndex {
133    fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) {
134        s.emit_u32(self.as_u32());
135    }
136}
137
138impl<'a, 'tcx> SpanEncoder for EncodeContext<'a, 'tcx> {
139    fn encode_crate_num(&mut self, crate_num: CrateNum) {
140        if crate_num != LOCAL_CRATE && self.is_proc_macro {
141            panic!("Attempted to encode non-local CrateNum {crate_num:?} for proc-macro crate");
142        }
143        self.emit_u32(crate_num.as_u32());
144    }
145
146    fn encode_def_index(&mut self, def_index: DefIndex) {
147        self.emit_u32(def_index.as_u32());
148    }
149
150    fn encode_def_id(&mut self, def_id: DefId) {
151        def_id.krate.encode(self);
152        def_id.index.encode(self);
153    }
154
155    fn encode_syntax_context(&mut self, syntax_context: SyntaxContext) {
156        rustc_span::hygiene::raw_encode_syntax_context(syntax_context, self.hygiene_ctxt, self);
157    }
158
159    fn encode_expn_id(&mut self, expn_id: ExpnId) {
160        if expn_id.krate == LOCAL_CRATE {
161            // We will only write details for local expansions. Non-local expansions will fetch
162            // data from the corresponding crate's metadata.
163            // FIXME(#43047) FIXME(#74731) We may eventually want to avoid relying on external
164            // metadata from proc-macro crates.
165            self.hygiene_ctxt.schedule_expn_data_for_encoding(expn_id);
166        }
167        expn_id.krate.encode(self);
168        expn_id.local_id.encode(self);
169    }
170
171    fn encode_span(&mut self, span: Span) {
172        match self.span_shorthands.entry(span) {
173            Entry::Occupied(o) => {
174                // If an offset is smaller than the absolute position, we encode with the offset.
175                // This saves space since smaller numbers encode in less bits.
176                let last_location = *o.get();
177                // This cannot underflow. Metadata is written with increasing position(), so any
178                // previously saved offset must be smaller than the current position.
179                let offset = self.opaque.position() - last_location;
180                if offset < last_location {
181                    let needed = bytes_needed(offset);
182                    SpanTag::indirect(true, needed as u8).encode(self);
183                    self.opaque.write_with(|dest| {
184                        *dest = offset.to_le_bytes();
185                        needed
186                    });
187                } else {
188                    let needed = bytes_needed(last_location);
189                    SpanTag::indirect(false, needed as u8).encode(self);
190                    self.opaque.write_with(|dest| {
191                        *dest = last_location.to_le_bytes();
192                        needed
193                    });
194                }
195            }
196            Entry::Vacant(v) => {
197                let position = self.opaque.position();
198                v.insert(position);
199                // Data is encoded with a SpanTag prefix (see below).
200                span.data().encode(self);
201            }
202        }
203    }
204
205    fn encode_symbol(&mut self, sym: Symbol) {
206        self.encode_symbol_or_byte_symbol(sym.as_u32(), |this| this.emit_str(sym.as_str()));
207    }
208
209    fn encode_byte_symbol(&mut self, byte_sym: ByteSymbol) {
210        self.encode_symbol_or_byte_symbol(byte_sym.as_u32(), |this| {
211            this.emit_byte_str(byte_sym.as_byte_str())
212        });
213    }
214}
215
216fn bytes_needed(n: usize) -> usize {
217    (usize::BITS - n.leading_zeros()).div_ceil(u8::BITS) as usize
218}
219
220impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for SpanData {
221    fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) {
222        // Don't serialize any `SyntaxContext`s from a proc-macro crate,
223        // since we don't load proc-macro dependencies during serialization.
224        // This means that any hygiene information from macros used *within*
225        // a proc-macro crate (e.g. invoking a macro that expands to a proc-macro
226        // definition) will be lost.
227        //
228        // This can show up in two ways:
229        //
230        // 1. Any hygiene information associated with identifier of
231        // a proc macro (e.g. `#[proc_macro] pub fn $name`) will be lost.
232        // Since proc-macros can only be invoked from a different crate,
233        // real code should never need to care about this.
234        //
235        // 2. Using `Span::def_site` or `Span::mixed_site` will not
236        // include any hygiene information associated with the definition
237        // site. This means that a proc-macro cannot emit a `$crate`
238        // identifier which resolves to one of its dependencies,
239        // which also should never come up in practice.
240        //
241        // Additionally, this affects `Span::parent`, and any other
242        // span inspection APIs that would otherwise allow traversing
243        // the `SyntaxContexts` associated with a span.
244        //
245        // None of these user-visible effects should result in any
246        // cross-crate inconsistencies (getting one behavior in the same
247        // crate, and a different behavior in another crate) due to the
248        // limited surface that proc-macros can expose.
249        //
250        // IMPORTANT: If this is ever changed, be sure to update
251        // `rustc_span::hygiene::raw_encode_expn_id` to handle
252        // encoding `ExpnData` for proc-macro crates.
253        let ctxt = if s.is_proc_macro { SyntaxContext::root() } else { self.ctxt };
254
255        if self.is_dummy() {
256            let tag = SpanTag::new(SpanKind::Partial, ctxt, 0);
257            tag.encode(s);
258            if tag.context().is_none() {
259                ctxt.encode(s);
260            }
261            return;
262        }
263
264        // The Span infrastructure should make sure that this invariant holds:
265        debug_assert!(self.lo <= self.hi);
266
267        if !s.source_file_cache.0.contains(self.lo) {
268            let source_map = s.tcx.sess.source_map();
269            let source_file_index = source_map.lookup_source_file_idx(self.lo);
270            s.source_file_cache =
271                (Arc::clone(&source_map.files()[source_file_index]), source_file_index);
272        }
273        let (ref source_file, source_file_index) = s.source_file_cache;
274        debug_assert!(source_file.contains(self.lo));
275
276        if !source_file.contains(self.hi) {
277            // Unfortunately, macro expansion still sometimes generates Spans
278            // that malformed in this way.
279            let tag = SpanTag::new(SpanKind::Partial, ctxt, 0);
280            tag.encode(s);
281            if tag.context().is_none() {
282                ctxt.encode(s);
283            }
284            return;
285        }
286
287        // There are two possible cases here:
288        // 1. This span comes from a 'foreign' crate - e.g. some crate upstream of the
289        // crate we are writing metadata for. When the metadata for *this* crate gets
290        // deserialized, the deserializer will need to know which crate it originally came
291        // from. We use `TAG_VALID_SPAN_FOREIGN` to indicate that a `CrateNum` should
292        // be deserialized after the rest of the span data, which tells the deserializer
293        // which crate contains the source map information.
294        // 2. This span comes from our own crate. No special handling is needed - we just
295        // write `TAG_VALID_SPAN_LOCAL` to let the deserializer know that it should use
296        // our own source map information.
297        //
298        // If we're a proc-macro crate, we always treat this as a local `Span`.
299        // In `encode_source_map`, we serialize foreign `SourceFile`s into our metadata
300        // if we're a proc-macro crate.
301        // This allows us to avoid loading the dependencies of proc-macro crates: all of
302        // the information we need to decode `Span`s is stored in the proc-macro crate.
303        let (kind, metadata_index) = if source_file.is_imported() && !s.is_proc_macro {
304            // To simplify deserialization, we 'rebase' this span onto the crate it originally came
305            // from (the crate that 'owns' the file it references. These rebased 'lo' and 'hi'
306            // values are relative to the source map information for the 'foreign' crate whose
307            // CrateNum we write into the metadata. This allows `imported_source_files` to binary
308            // search through the 'foreign' crate's source map information, using the
309            // deserialized 'lo' and 'hi' values directly.
310            //
311            // All of this logic ensures that the final result of deserialization is a 'normal'
312            // Span that can be used without any additional trouble.
313            let metadata_index = {
314                // Introduce a new scope so that we drop the 'read()' temporary
315                match &*source_file.external_src.read() {
316                    ExternalSource::Foreign { metadata_index, .. } => *metadata_index,
317                    src => panic!("Unexpected external source {src:?}"),
318                }
319            };
320
321            (SpanKind::Foreign, metadata_index)
322        } else {
323            // Record the fact that we need to encode the data for this `SourceFile`
324            let source_files =
325                s.required_source_files.as_mut().expect("Already encoded SourceMap!");
326            let (metadata_index, _) = source_files.insert_full(source_file_index);
327            let metadata_index: u32 =
328                metadata_index.try_into().expect("cannot export more than U32_MAX files");
329
330            (SpanKind::Local, metadata_index)
331        };
332
333        // Encode the start position relative to the file start, so we profit more from the
334        // variable-length integer encoding.
335        let lo = self.lo - source_file.start_pos;
336
337        // Encode length which is usually less than span.hi and profits more
338        // from the variable-length integer encoding that we use.
339        let len = self.hi - self.lo;
340
341        let tag = SpanTag::new(kind, ctxt, len.0 as usize);
342        tag.encode(s);
343        if tag.context().is_none() {
344            ctxt.encode(s);
345        }
346        lo.encode(s);
347        if tag.length().is_none() {
348            len.encode(s);
349        }
350
351        // Encode the index of the `SourceFile` for the span, in order to make decoding faster.
352        metadata_index.encode(s);
353
354        if kind == SpanKind::Foreign {
355            // This needs to be two lines to avoid holding the `s.source_file_cache`
356            // while calling `cnum.encode(s)`
357            let cnum = s.source_file_cache.0.cnum;
358            cnum.encode(s);
359        }
360    }
361}
362
363impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for [u8] {
364    fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) {
365        Encoder::emit_usize(e, self.len());
366        e.emit_raw_bytes(self);
367    }
368}
369
370impl<'a, 'tcx> TyEncoder<'tcx> for EncodeContext<'a, 'tcx> {
371    const CLEAR_CROSS_CRATE: bool = true;
372
373    fn position(&self) -> usize {
374        self.opaque.position()
375    }
376
377    fn type_shorthands(&mut self) -> &mut FxHashMap<Ty<'tcx>, usize> {
378        &mut self.type_shorthands
379    }
380
381    fn predicate_shorthands(&mut self) -> &mut FxHashMap<ty::PredicateKind<'tcx>, usize> {
382        &mut self.predicate_shorthands
383    }
384
385    fn encode_alloc_id(&mut self, alloc_id: &rustc_middle::mir::interpret::AllocId) {
386        let (index, _) = self.interpret_allocs.insert_full(*alloc_id);
387
388        index.encode(self);
389    }
390}
391
392// Shorthand for `$self.$tables.$table.set_some($def_id.index, $self.lazy($value))`, which would
393// normally need extra variables to avoid errors about multiple mutable borrows.
394macro_rules! record {
395    ($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => {{
396        {
397            let value = $value;
398            let lazy = $self.lazy(value);
399            $self.$tables.$table.set_some($def_id.index, lazy);
400        }
401    }};
402}
403
404// Shorthand for `$self.$tables.$table.set_some($def_id.index, $self.lazy_array($value))`, which would
405// normally need extra variables to avoid errors about multiple mutable borrows.
406macro_rules! record_array {
407    ($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => {{
408        {
409            let value = $value;
410            let lazy = $self.lazy_array(value);
411            $self.$tables.$table.set_some($def_id.index, lazy);
412        }
413    }};
414}
415
416macro_rules! record_defaulted_array {
417    ($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => {{
418        {
419            let value = $value;
420            let lazy = $self.lazy_array(value);
421            $self.$tables.$table.set($def_id.index, lazy);
422        }
423    }};
424}
425
426impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
427    fn emit_lazy_distance(&mut self, position: NonZero<usize>) {
428        let pos = position.get();
429        let distance = match self.lazy_state {
430            LazyState::NoNode => bug!("emit_lazy_distance: outside of a metadata node"),
431            LazyState::NodeStart(start) => {
432                let start = start.get();
433                assert!(pos <= start);
434                start - pos
435            }
436            LazyState::Previous(last_pos) => {
437                assert!(
438                    last_pos <= position,
439                    "make sure that the calls to `lazy*` \
440                     are in the same order as the metadata fields",
441                );
442                position.get() - last_pos.get()
443            }
444        };
445        self.lazy_state = LazyState::Previous(NonZero::new(pos).unwrap());
446        self.emit_usize(distance);
447    }
448
449    fn lazy<T: ParameterizedOverTcx, B: Borrow<T::Value<'tcx>>>(&mut self, value: B) -> LazyValue<T>
450    where
451        T::Value<'tcx>: Encodable<EncodeContext<'a, 'tcx>>,
452    {
453        let pos = NonZero::new(self.position()).unwrap();
454
455        assert_eq!(self.lazy_state, LazyState::NoNode);
456        self.lazy_state = LazyState::NodeStart(pos);
457        value.borrow().encode(self);
458        self.lazy_state = LazyState::NoNode;
459
460        assert!(pos.get() <= self.position());
461
462        LazyValue::from_position(pos)
463    }
464
465    fn lazy_array<T: ParameterizedOverTcx, I: IntoIterator<Item = B>, B: Borrow<T::Value<'tcx>>>(
466        &mut self,
467        values: I,
468    ) -> LazyArray<T>
469    where
470        T::Value<'tcx>: Encodable<EncodeContext<'a, 'tcx>>,
471    {
472        let pos = NonZero::new(self.position()).unwrap();
473
474        assert_eq!(self.lazy_state, LazyState::NoNode);
475        self.lazy_state = LazyState::NodeStart(pos);
476        let len = values.into_iter().map(|value| value.borrow().encode(self)).count();
477        self.lazy_state = LazyState::NoNode;
478
479        assert!(pos.get() <= self.position());
480
481        LazyArray::from_position_and_num_elems(pos, len)
482    }
483
484    fn encode_symbol_or_byte_symbol(
485        &mut self,
486        index: u32,
487        emit_str_or_byte_str: impl Fn(&mut Self),
488    ) {
489        // if symbol/byte symbol is predefined, emit tag and symbol index
490        if Symbol::is_predefined(index) {
491            self.opaque.emit_u8(SYMBOL_PREDEFINED);
492            self.opaque.emit_u32(index);
493        } else {
494            // otherwise write it as string or as offset to it
495            match self.symbol_index_table.entry(index) {
496                Entry::Vacant(o) => {
497                    self.opaque.emit_u8(SYMBOL_STR);
498                    let pos = self.opaque.position();
499                    o.insert(pos);
500                    emit_str_or_byte_str(self);
501                }
502                Entry::Occupied(o) => {
503                    let x = *o.get();
504                    self.emit_u8(SYMBOL_OFFSET);
505                    self.emit_usize(x);
506                }
507            }
508        }
509    }
510
511    fn encode_def_path_table(&mut self) {
512        let table = self.tcx.def_path_table();
513        if self.is_proc_macro {
514            for def_index in std::iter::once(CRATE_DEF_INDEX)
515                .chain(self.tcx.resolutions(()).proc_macros.iter().map(|p| p.local_def_index))
516            {
517                let def_key = self.lazy(table.def_key(def_index));
518                let def_path_hash = table.def_path_hash(def_index);
519                self.tables.def_keys.set_some(def_index, def_key);
520                self.tables.def_path_hashes.set(def_index, def_path_hash.local_hash().as_u64());
521            }
522        } else {
523            for (def_index, def_key, def_path_hash) in table.enumerated_keys_and_path_hashes() {
524                let def_key = self.lazy(def_key);
525                self.tables.def_keys.set_some(def_index, def_key);
526                self.tables.def_path_hashes.set(def_index, def_path_hash.local_hash().as_u64());
527            }
528        }
529    }
530
531    fn encode_def_path_hash_map(&mut self) -> LazyValue<DefPathHashMapRef<'static>> {
532        self.lazy(DefPathHashMapRef::BorrowedFromTcx(self.tcx.def_path_hash_to_def_index_map()))
533    }
534
535    fn encode_source_map(&mut self) -> LazyTable<u32, Option<LazyValue<rustc_span::SourceFile>>> {
536        let source_map = self.tcx.sess.source_map();
537        let all_source_files = source_map.files();
538
539        // By replacing the `Option` with `None`, we ensure that we can't
540        // accidentally serialize any more `Span`s after the source map encoding
541        // is done.
542        let required_source_files = self.required_source_files.take().unwrap();
543
544        let working_directory = &self.tcx.sess.opts.working_dir;
545
546        let mut adapted = TableBuilder::default();
547
548        let local_crate_stable_id = self.tcx.stable_crate_id(LOCAL_CRATE);
549
550        // Only serialize `SourceFile`s that were used during the encoding of a `Span`.
551        //
552        // The order in which we encode source files is important here: the on-disk format for
553        // `Span` contains the index of the corresponding `SourceFile`.
554        for (on_disk_index, &source_file_index) in required_source_files.iter().enumerate() {
555            let source_file = &all_source_files[source_file_index];
556            // Don't serialize imported `SourceFile`s, unless we're in a proc-macro crate.
557            assert!(!source_file.is_imported() || self.is_proc_macro);
558
559            // At export time we expand all source file paths to absolute paths because
560            // downstream compilation sessions can have a different compiler working
561            // directory, so relative paths from this or any other upstream crate
562            // won't be valid anymore.
563            //
564            // At this point we also erase the actual on-disk path and only keep
565            // the remapped version -- as is necessary for reproducible builds.
566            let mut adapted_source_file = (**source_file).clone();
567
568            match source_file.name {
569                FileName::Real(ref original_file_name) => {
570                    let adapted_file_name = source_map
571                        .path_mapping()
572                        .to_embeddable_absolute_path(original_file_name.clone(), working_directory);
573
574                    adapted_source_file.name = FileName::Real(adapted_file_name);
575                }
576                _ => {
577                    // expanded code, not from a file
578                }
579            };
580
581            // We're serializing this `SourceFile` into our crate metadata,
582            // so mark it as coming from this crate.
583            // This also ensures that we don't try to deserialize the
584            // `CrateNum` for a proc-macro dependency - since proc macro
585            // dependencies aren't loaded when we deserialize a proc-macro,
586            // trying to remap the `CrateNum` would fail.
587            if self.is_proc_macro {
588                adapted_source_file.cnum = LOCAL_CRATE;
589            }
590
591            // Update the `StableSourceFileId` to make sure it incorporates the
592            // id of the current crate. This way it will be unique within the
593            // crate graph during downstream compilation sessions.
594            adapted_source_file.stable_id = StableSourceFileId::from_filename_for_export(
595                &adapted_source_file.name,
596                local_crate_stable_id,
597            );
598
599            let on_disk_index: u32 =
600                on_disk_index.try_into().expect("cannot export more than U32_MAX files");
601            adapted.set_some(on_disk_index, self.lazy(adapted_source_file));
602        }
603
604        adapted.encode(&mut self.opaque)
605    }
606
607    fn encode_crate_root(&mut self) -> LazyValue<CrateRoot> {
608        let tcx = self.tcx;
609        let mut stats: Vec<(&'static str, usize)> = Vec::with_capacity(32);
610
611        macro_rules! stat {
612            ($label:literal, $f:expr) => {{
613                let orig_pos = self.position();
614                let res = $f();
615                stats.push(($label, self.position() - orig_pos));
616                res
617            }};
618        }
619
620        // We have already encoded some things. Get their combined size from the current position.
621        stats.push(("preamble", self.position()));
622
623        let (crate_deps, dylib_dependency_formats) =
624            stat!("dep", || (self.encode_crate_deps(), self.encode_dylib_dependency_formats()));
625
626        let lib_features = stat!("lib-features", || self.encode_lib_features());
627
628        let stability_implications =
629            stat!("stability-implications", || self.encode_stability_implications());
630
631        let (lang_items, lang_items_missing) = stat!("lang-items", || {
632            (self.encode_lang_items(), self.encode_lang_items_missing())
633        });
634
635        let stripped_cfg_items = stat!("stripped-cfg-items", || self.encode_stripped_cfg_items());
636
637        let diagnostic_items = stat!("diagnostic-items", || self.encode_diagnostic_items());
638
639        let native_libraries = stat!("native-libs", || self.encode_native_libraries());
640
641        let foreign_modules = stat!("foreign-modules", || self.encode_foreign_modules());
642
643        _ = stat!("def-path-table", || self.encode_def_path_table());
644
645        // Encode the def IDs of traits, for rustdoc and diagnostics.
646        let traits = stat!("traits", || self.encode_traits());
647
648        // Encode the def IDs of impls, for coherence checking.
649        let impls = stat!("impls", || self.encode_impls());
650
651        let incoherent_impls = stat!("incoherent-impls", || self.encode_incoherent_impls());
652
653        _ = stat!("mir", || self.encode_mir());
654
655        _ = stat!("def-ids", || self.encode_def_ids());
656
657        let interpret_alloc_index = stat!("interpret-alloc-index", || {
658            let mut interpret_alloc_index = Vec::new();
659            let mut n = 0;
660            trace!("beginning to encode alloc ids");
661            loop {
662                let new_n = self.interpret_allocs.len();
663                // if we have found new ids, serialize those, too
664                if n == new_n {
665                    // otherwise, abort
666                    break;
667                }
668                trace!("encoding {} further alloc ids", new_n - n);
669                for idx in n..new_n {
670                    let id = self.interpret_allocs[idx];
671                    let pos = self.position() as u64;
672                    interpret_alloc_index.push(pos);
673                    interpret::specialized_encode_alloc_id(self, tcx, id);
674                }
675                n = new_n;
676            }
677            self.lazy_array(interpret_alloc_index)
678        });
679
680        // Encode the proc macro data. This affects `tables`, so we need to do this before we
681        // encode the tables. This overwrites def_keys, so it must happen after
682        // encode_def_path_table.
683        let proc_macro_data = stat!("proc-macro-data", || self.encode_proc_macros());
684
685        let tables = stat!("tables", || self.tables.encode(&mut self.opaque));
686
687        let debugger_visualizers =
688            stat!("debugger-visualizers", || self.encode_debugger_visualizers());
689
690        let exportable_items = stat!("exportable-items", || self.encode_exportable_items());
691
692        let stable_order_of_exportable_impls =
693            stat!("exportable-items", || self.encode_stable_order_of_exportable_impls());
694
695        // Encode exported symbols info. This is prefetched in `encode_metadata`.
696        let (exported_non_generic_symbols, exported_generic_symbols) =
697            stat!("exported-symbols", || {
698                (
699                    self.encode_exported_symbols(tcx.exported_non_generic_symbols(LOCAL_CRATE)),
700                    self.encode_exported_symbols(tcx.exported_generic_symbols(LOCAL_CRATE)),
701                )
702            });
703
704        // Encode the hygiene data.
705        // IMPORTANT: this *must* be the last thing that we encode (other than `SourceMap`). The
706        // process of encoding other items (e.g. `optimized_mir`) may cause us to load data from
707        // the incremental cache. If this causes us to deserialize a `Span`, then we may load
708        // additional `SyntaxContext`s into the global `HygieneData`. Therefore, we need to encode
709        // the hygiene data last to ensure that we encode any `SyntaxContext`s that might be used.
710        let (syntax_contexts, expn_data, expn_hashes) = stat!("hygiene", || self.encode_hygiene());
711
712        let def_path_hash_map = stat!("def-path-hash-map", || self.encode_def_path_hash_map());
713
714        // Encode source_map. This needs to be done last, because encoding `Span`s tells us which
715        // `SourceFiles` we actually need to encode.
716        let source_map = stat!("source-map", || self.encode_source_map());
717        let target_modifiers = stat!("target-modifiers", || self.encode_target_modifiers());
718
719        let root = stat!("final", || {
720            let attrs = tcx.hir_krate_attrs();
721            self.lazy(CrateRoot {
722                header: CrateHeader {
723                    name: tcx.crate_name(LOCAL_CRATE),
724                    triple: tcx.sess.opts.target_triple.clone(),
725                    hash: tcx.crate_hash(LOCAL_CRATE),
726                    is_proc_macro_crate: proc_macro_data.is_some(),
727                    is_stub: false,
728                },
729                extra_filename: tcx.sess.opts.cg.extra_filename.clone(),
730                stable_crate_id: tcx.def_path_hash(LOCAL_CRATE.as_def_id()).stable_crate_id(),
731                required_panic_strategy: tcx.required_panic_strategy(LOCAL_CRATE),
732                panic_in_drop_strategy: tcx.sess.opts.unstable_opts.panic_in_drop,
733                edition: tcx.sess.edition(),
734                has_global_allocator: tcx.has_global_allocator(LOCAL_CRATE),
735                has_alloc_error_handler: tcx.has_alloc_error_handler(LOCAL_CRATE),
736                has_panic_handler: tcx.has_panic_handler(LOCAL_CRATE),
737                has_default_lib_allocator: ast::attr::contains_name(
738                    attrs,
739                    sym::default_lib_allocator,
740                ),
741                proc_macro_data,
742                debugger_visualizers,
743                compiler_builtins: ast::attr::contains_name(attrs, sym::compiler_builtins),
744                needs_allocator: ast::attr::contains_name(attrs, sym::needs_allocator),
745                needs_panic_runtime: ast::attr::contains_name(attrs, sym::needs_panic_runtime),
746                no_builtins: ast::attr::contains_name(attrs, sym::no_builtins),
747                panic_runtime: ast::attr::contains_name(attrs, sym::panic_runtime),
748                profiler_runtime: ast::attr::contains_name(attrs, sym::profiler_runtime),
749                symbol_mangling_version: tcx.sess.opts.get_symbol_mangling_version(),
750
751                crate_deps,
752                dylib_dependency_formats,
753                lib_features,
754                stability_implications,
755                lang_items,
756                diagnostic_items,
757                lang_items_missing,
758                stripped_cfg_items,
759                native_libraries,
760                foreign_modules,
761                source_map,
762                target_modifiers,
763                traits,
764                impls,
765                incoherent_impls,
766                exportable_items,
767                stable_order_of_exportable_impls,
768                exported_non_generic_symbols,
769                exported_generic_symbols,
770                interpret_alloc_index,
771                tables,
772                syntax_contexts,
773                expn_data,
774                expn_hashes,
775                def_path_hash_map,
776                specialization_enabled_in: tcx.specialization_enabled_in(LOCAL_CRATE),
777            })
778        });
779
780        let total_bytes = self.position();
781
782        let computed_total_bytes: usize = stats.iter().map(|(_, size)| size).sum();
783        assert_eq!(total_bytes, computed_total_bytes);
784
785        if tcx.sess.opts.unstable_opts.meta_stats {
786            use std::fmt::Write;
787
788            self.opaque.flush();
789
790            // Rewind and re-read all the metadata to count the zero bytes we wrote.
791            let pos_before_rewind = self.opaque.file().stream_position().unwrap();
792            let mut zero_bytes = 0;
793            self.opaque.file().rewind().unwrap();
794            let file = std::io::BufReader::new(self.opaque.file());
795            for e in file.bytes() {
796                if e.unwrap() == 0 {
797                    zero_bytes += 1;
798                }
799            }
800            assert_eq!(self.opaque.file().stream_position().unwrap(), pos_before_rewind);
801
802            stats.sort_by_key(|&(_, usize)| usize);
803            stats.reverse(); // bigger items first
804
805            let prefix = "meta-stats";
806            let perc = |bytes| (bytes * 100) as f64 / total_bytes as f64;
807
808            let section_w = 23;
809            let size_w = 10;
810            let banner_w = 64;
811
812            // We write all the text into a string and print it with a single
813            // `eprint!`. This is an attempt to minimize interleaved text if multiple
814            // rustc processes are printing macro-stats at the same time (e.g. with
815            // `RUSTFLAGS='-Zmeta-stats' cargo build`). It still doesn't guarantee
816            // non-interleaving, though.
817            let mut s = String::new();
818            _ = writeln!(s, "{prefix} {}", "=".repeat(banner_w));
819            _ = writeln!(s, "{prefix} METADATA STATS: {}", tcx.crate_name(LOCAL_CRATE));
820            _ = writeln!(s, "{prefix} {:<section_w$}{:>size_w$}", "Section", "Size");
821            _ = writeln!(s, "{prefix} {}", "-".repeat(banner_w));
822            for (label, size) in stats {
823                _ = writeln!(
824                    s,
825                    "{prefix} {:<section_w$}{:>size_w$} ({:4.1}%)",
826                    label,
827                    usize_with_underscores(size),
828                    perc(size)
829                );
830            }
831            _ = writeln!(s, "{prefix} {}", "-".repeat(banner_w));
832            _ = writeln!(
833                s,
834                "{prefix} {:<section_w$}{:>size_w$} (of which {:.1}% are zero bytes)",
835                "Total",
836                usize_with_underscores(total_bytes),
837                perc(zero_bytes)
838            );
839            _ = writeln!(s, "{prefix} {}", "=".repeat(banner_w));
840            eprint!("{s}");
841        }
842
843        root
844    }
845}
846
847struct AnalyzeAttrState<'a> {
848    is_exported: bool,
849    is_doc_hidden: bool,
850    features: &'a Features,
851}
852
853/// Returns whether an attribute needs to be recorded in metadata, that is, if it's usable and
854/// useful in downstream crates. Local-only attributes are an obvious example, but some
855/// rustdoc-specific attributes can equally be of use while documenting the current crate only.
856///
857/// Removing these superfluous attributes speeds up compilation by making the metadata smaller.
858///
859/// Note: the `is_exported` parameter is used to cache whether the given `DefId` has a public
860/// visibility: this is a piece of data that can be computed once per defid, and not once per
861/// attribute. Some attributes would only be usable downstream if they are public.
862#[inline]
863fn analyze_attr(attr: &hir::Attribute, state: &mut AnalyzeAttrState<'_>) -> bool {
864    let mut should_encode = false;
865    if let hir::Attribute::Parsed(p) = attr
866        && p.encode_cross_crate() == EncodeCrossCrate::No
867    {
868        // Attributes not marked encode-cross-crate don't need to be encoded for downstream crates.
869    } else if let Some(name) = attr.name()
870        && !rustc_feature::encode_cross_crate(name)
871    {
872        // Attributes not marked encode-cross-crate don't need to be encoded for downstream crates.
873    } else if attr.doc_str().is_some() {
874        // We keep all doc comments reachable to rustdoc because they might be "imported" into
875        // downstream crates if they use `#[doc(inline)]` to copy an item's documentation into
876        // their own.
877        if state.is_exported {
878            should_encode = true;
879        }
880    } else if attr.has_name(sym::doc) {
881        // If this is a `doc` attribute that doesn't have anything except maybe `inline` (as in
882        // `#[doc(inline)]`), then we can remove it. It won't be inlinable in downstream crates.
883        if let Some(item_list) = attr.meta_item_list() {
884            for item in item_list {
885                if !item.has_name(sym::inline) {
886                    should_encode = true;
887                    if item.has_name(sym::hidden) {
888                        state.is_doc_hidden = true;
889                        break;
890                    }
891                }
892            }
893        }
894    } else if let &[sym::diagnostic, seg] = &*attr.path() {
895        should_encode = rustc_feature::is_stable_diagnostic_attribute(seg, state.features);
896    } else {
897        should_encode = true;
898    }
899    should_encode
900}
901
902fn should_encode_span(def_kind: DefKind) -> bool {
903    match def_kind {
904        DefKind::Mod
905        | DefKind::Struct
906        | DefKind::Union
907        | DefKind::Enum
908        | DefKind::Variant
909        | DefKind::Trait
910        | DefKind::TyAlias
911        | DefKind::ForeignTy
912        | DefKind::TraitAlias
913        | DefKind::AssocTy
914        | DefKind::TyParam
915        | DefKind::ConstParam
916        | DefKind::LifetimeParam
917        | DefKind::Fn
918        | DefKind::Const
919        | DefKind::Static { .. }
920        | DefKind::Ctor(..)
921        | DefKind::AssocFn
922        | DefKind::AssocConst
923        | DefKind::Macro(_)
924        | DefKind::ExternCrate
925        | DefKind::Use
926        | DefKind::AnonConst
927        | DefKind::InlineConst
928        | DefKind::OpaqueTy
929        | DefKind::Field
930        | DefKind::Impl { .. }
931        | DefKind::Closure
932        | DefKind::SyntheticCoroutineBody => true,
933        DefKind::ForeignMod | DefKind::GlobalAsm => false,
934    }
935}
936
937fn should_encode_attrs(def_kind: DefKind) -> bool {
938    match def_kind {
939        DefKind::Mod
940        | DefKind::Struct
941        | DefKind::Union
942        | DefKind::Enum
943        | DefKind::Variant
944        | DefKind::Trait
945        | DefKind::TyAlias
946        | DefKind::ForeignTy
947        | DefKind::TraitAlias
948        | DefKind::AssocTy
949        | DefKind::Fn
950        | DefKind::Const
951        | DefKind::Static { nested: false, .. }
952        | DefKind::AssocFn
953        | DefKind::AssocConst
954        | DefKind::Macro(_)
955        | DefKind::Field
956        | DefKind::Impl { .. } => true,
957        // Tools may want to be able to detect their tool lints on
958        // closures from upstream crates, too. This is used by
959        // https://github.com/model-checking/kani and is not a performance
960        // or maintenance issue for us.
961        DefKind::Closure => true,
962        DefKind::SyntheticCoroutineBody => false,
963        DefKind::TyParam
964        | DefKind::ConstParam
965        | DefKind::Ctor(..)
966        | DefKind::ExternCrate
967        | DefKind::Use
968        | DefKind::ForeignMod
969        | DefKind::AnonConst
970        | DefKind::InlineConst
971        | DefKind::OpaqueTy
972        | DefKind::LifetimeParam
973        | DefKind::Static { nested: true, .. }
974        | DefKind::GlobalAsm => false,
975    }
976}
977
978fn should_encode_expn_that_defined(def_kind: DefKind) -> bool {
979    match def_kind {
980        DefKind::Mod
981        | DefKind::Struct
982        | DefKind::Union
983        | DefKind::Enum
984        | DefKind::Variant
985        | DefKind::Trait
986        | DefKind::Impl { .. } => true,
987        DefKind::TyAlias
988        | DefKind::ForeignTy
989        | DefKind::TraitAlias
990        | DefKind::AssocTy
991        | DefKind::TyParam
992        | DefKind::Fn
993        | DefKind::Const
994        | DefKind::ConstParam
995        | DefKind::Static { .. }
996        | DefKind::Ctor(..)
997        | DefKind::AssocFn
998        | DefKind::AssocConst
999        | DefKind::Macro(_)
1000        | DefKind::ExternCrate
1001        | DefKind::Use
1002        | DefKind::ForeignMod
1003        | DefKind::AnonConst
1004        | DefKind::InlineConst
1005        | DefKind::OpaqueTy
1006        | DefKind::Field
1007        | DefKind::LifetimeParam
1008        | DefKind::GlobalAsm
1009        | DefKind::Closure
1010        | DefKind::SyntheticCoroutineBody => false,
1011    }
1012}
1013
1014fn should_encode_visibility(def_kind: DefKind) -> bool {
1015    match def_kind {
1016        DefKind::Mod
1017        | DefKind::Struct
1018        | DefKind::Union
1019        | DefKind::Enum
1020        | DefKind::Variant
1021        | DefKind::Trait
1022        | DefKind::TyAlias
1023        | DefKind::ForeignTy
1024        | DefKind::TraitAlias
1025        | DefKind::AssocTy
1026        | DefKind::Fn
1027        | DefKind::Const
1028        | DefKind::Static { nested: false, .. }
1029        | DefKind::Ctor(..)
1030        | DefKind::AssocFn
1031        | DefKind::AssocConst
1032        | DefKind::Macro(..)
1033        | DefKind::Field => true,
1034        DefKind::Use
1035        | DefKind::ForeignMod
1036        | DefKind::TyParam
1037        | DefKind::ConstParam
1038        | DefKind::LifetimeParam
1039        | DefKind::AnonConst
1040        | DefKind::InlineConst
1041        | DefKind::Static { nested: true, .. }
1042        | DefKind::OpaqueTy
1043        | DefKind::GlobalAsm
1044        | DefKind::Impl { .. }
1045        | DefKind::Closure
1046        | DefKind::ExternCrate
1047        | DefKind::SyntheticCoroutineBody => false,
1048    }
1049}
1050
1051fn should_encode_stability(def_kind: DefKind) -> bool {
1052    match def_kind {
1053        DefKind::Mod
1054        | DefKind::Ctor(..)
1055        | DefKind::Variant
1056        | DefKind::Field
1057        | DefKind::Struct
1058        | DefKind::AssocTy
1059        | DefKind::AssocFn
1060        | DefKind::AssocConst
1061        | DefKind::TyParam
1062        | DefKind::ConstParam
1063        | DefKind::Static { .. }
1064        | DefKind::Const
1065        | DefKind::Fn
1066        | DefKind::ForeignMod
1067        | DefKind::TyAlias
1068        | DefKind::OpaqueTy
1069        | DefKind::Enum
1070        | DefKind::Union
1071        | DefKind::Impl { .. }
1072        | DefKind::Trait
1073        | DefKind::TraitAlias
1074        | DefKind::Macro(..)
1075        | DefKind::ForeignTy => true,
1076        DefKind::Use
1077        | DefKind::LifetimeParam
1078        | DefKind::AnonConst
1079        | DefKind::InlineConst
1080        | DefKind::GlobalAsm
1081        | DefKind::Closure
1082        | DefKind::ExternCrate
1083        | DefKind::SyntheticCoroutineBody => false,
1084    }
1085}
1086
1087/// Whether we should encode MIR. Return a pair, resp. for CTFE and for LLVM.
1088///
1089/// Computing, optimizing and encoding the MIR is a relatively expensive operation.
1090/// We want to avoid this work when not required. Therefore:
1091/// - we only compute `mir_for_ctfe` on items with const-eval semantics;
1092/// - we skip `optimized_mir` for check runs.
1093/// - we only encode `optimized_mir` that could be generated in other crates, that is, a code that
1094///   is either generic or has inline hint, and is reachable from the other crates (contained
1095///   in reachable set).
1096///
1097/// Note: Reachable set describes definitions that might be generated or referenced from other
1098/// crates and it can be used to limit optimized MIR that needs to be encoded. On the other hand,
1099/// the reachable set doesn't have much to say about which definitions might be evaluated at compile
1100/// time in other crates, so it cannot be used to omit CTFE MIR. For example, `f` below is
1101/// unreachable and yet it can be evaluated in other crates:
1102///
1103/// ```
1104/// const fn f() -> usize { 0 }
1105/// pub struct S { pub a: [usize; f()] }
1106/// ```
1107fn should_encode_mir(
1108    tcx: TyCtxt<'_>,
1109    reachable_set: &LocalDefIdSet,
1110    def_id: LocalDefId,
1111) -> (bool, bool) {
1112    match tcx.def_kind(def_id) {
1113        // Constructors
1114        DefKind::Ctor(_, _) => {
1115            let mir_opt_base = tcx.sess.opts.output_types.should_codegen()
1116                || tcx.sess.opts.unstable_opts.always_encode_mir;
1117            (true, mir_opt_base)
1118        }
1119        // Constants
1120        DefKind::AnonConst | DefKind::InlineConst | DefKind::AssocConst | DefKind::Const => {
1121            (true, false)
1122        }
1123        // Coroutines require optimized MIR to compute layout.
1124        DefKind::Closure if tcx.is_coroutine(def_id.to_def_id()) => (false, true),
1125        DefKind::SyntheticCoroutineBody => (false, true),
1126        // Full-fledged functions + closures
1127        DefKind::AssocFn | DefKind::Fn | DefKind::Closure => {
1128            let generics = tcx.generics_of(def_id);
1129            let opt = tcx.sess.opts.unstable_opts.always_encode_mir
1130                || (tcx.sess.opts.output_types.should_codegen()
1131                    && reachable_set.contains(&def_id)
1132                    && (generics.requires_monomorphization(tcx)
1133                        || tcx.cross_crate_inlinable(def_id)));
1134            // The function has a `const` modifier or is in a `const trait`.
1135            let is_const_fn = tcx.is_const_fn(def_id.to_def_id())
1136                || tcx.is_const_default_method(def_id.to_def_id());
1137            (is_const_fn, opt)
1138        }
1139        // The others don't have MIR.
1140        _ => (false, false),
1141    }
1142}
1143
1144fn should_encode_variances<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, def_kind: DefKind) -> bool {
1145    match def_kind {
1146        DefKind::Struct
1147        | DefKind::Union
1148        | DefKind::Enum
1149        | DefKind::OpaqueTy
1150        | DefKind::Fn
1151        | DefKind::Ctor(..)
1152        | DefKind::AssocFn => true,
1153        DefKind::AssocTy => {
1154            // Only encode variances for RPITITs (for traits)
1155            matches!(tcx.opt_rpitit_info(def_id), Some(ty::ImplTraitInTraitData::Trait { .. }))
1156        }
1157        DefKind::Mod
1158        | DefKind::Variant
1159        | DefKind::Field
1160        | DefKind::AssocConst
1161        | DefKind::TyParam
1162        | DefKind::ConstParam
1163        | DefKind::Static { .. }
1164        | DefKind::Const
1165        | DefKind::ForeignMod
1166        | DefKind::Impl { .. }
1167        | DefKind::Trait
1168        | DefKind::TraitAlias
1169        | DefKind::Macro(..)
1170        | DefKind::ForeignTy
1171        | DefKind::Use
1172        | DefKind::LifetimeParam
1173        | DefKind::AnonConst
1174        | DefKind::InlineConst
1175        | DefKind::GlobalAsm
1176        | DefKind::Closure
1177        | DefKind::ExternCrate
1178        | DefKind::SyntheticCoroutineBody => false,
1179        DefKind::TyAlias => tcx.type_alias_is_lazy(def_id),
1180    }
1181}
1182
1183fn should_encode_generics(def_kind: DefKind) -> bool {
1184    match def_kind {
1185        DefKind::Struct
1186        | DefKind::Union
1187        | DefKind::Enum
1188        | DefKind::Variant
1189        | DefKind::Trait
1190        | DefKind::TyAlias
1191        | DefKind::ForeignTy
1192        | DefKind::TraitAlias
1193        | DefKind::AssocTy
1194        | DefKind::Fn
1195        | DefKind::Const
1196        | DefKind::Static { .. }
1197        | DefKind::Ctor(..)
1198        | DefKind::AssocFn
1199        | DefKind::AssocConst
1200        | DefKind::AnonConst
1201        | DefKind::InlineConst
1202        | DefKind::OpaqueTy
1203        | DefKind::Impl { .. }
1204        | DefKind::Field
1205        | DefKind::TyParam
1206        | DefKind::Closure
1207        | DefKind::SyntheticCoroutineBody => true,
1208        DefKind::Mod
1209        | DefKind::ForeignMod
1210        | DefKind::ConstParam
1211        | DefKind::Macro(..)
1212        | DefKind::Use
1213        | DefKind::LifetimeParam
1214        | DefKind::GlobalAsm
1215        | DefKind::ExternCrate => false,
1216    }
1217}
1218
1219fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) -> bool {
1220    match def_kind {
1221        DefKind::Struct
1222        | DefKind::Union
1223        | DefKind::Enum
1224        | DefKind::Variant
1225        | DefKind::Ctor(..)
1226        | DefKind::Field
1227        | DefKind::Fn
1228        | DefKind::Const
1229        | DefKind::Static { nested: false, .. }
1230        | DefKind::TyAlias
1231        | DefKind::ForeignTy
1232        | DefKind::Impl { .. }
1233        | DefKind::AssocFn
1234        | DefKind::AssocConst
1235        | DefKind::Closure
1236        | DefKind::ConstParam
1237        | DefKind::AnonConst
1238        | DefKind::InlineConst
1239        | DefKind::SyntheticCoroutineBody => true,
1240
1241        DefKind::OpaqueTy => {
1242            let origin = tcx.local_opaque_ty_origin(def_id);
1243            if let hir::OpaqueTyOrigin::FnReturn { parent, .. }
1244            | hir::OpaqueTyOrigin::AsyncFn { parent, .. } = origin
1245                && let hir::Node::TraitItem(trait_item) = tcx.hir_node_by_def_id(parent)
1246                && let (_, hir::TraitFn::Required(..)) = trait_item.expect_fn()
1247            {
1248                false
1249            } else {
1250                true
1251            }
1252        }
1253
1254        DefKind::AssocTy => {
1255            let assoc_item = tcx.associated_item(def_id);
1256            match assoc_item.container {
1257                ty::AssocContainer::InherentImpl | ty::AssocContainer::TraitImpl(_) => true,
1258                ty::AssocContainer::Trait => assoc_item.defaultness(tcx).has_value(),
1259            }
1260        }
1261        DefKind::TyParam => {
1262            let hir::Node::GenericParam(param) = tcx.hir_node_by_def_id(def_id) else { bug!() };
1263            let hir::GenericParamKind::Type { default, .. } = param.kind else { bug!() };
1264            default.is_some()
1265        }
1266
1267        DefKind::Trait
1268        | DefKind::TraitAlias
1269        | DefKind::Mod
1270        | DefKind::ForeignMod
1271        | DefKind::Macro(..)
1272        | DefKind::Static { nested: true, .. }
1273        | DefKind::Use
1274        | DefKind::LifetimeParam
1275        | DefKind::GlobalAsm
1276        | DefKind::ExternCrate => false,
1277    }
1278}
1279
1280fn should_encode_fn_sig(def_kind: DefKind) -> bool {
1281    match def_kind {
1282        DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fn) => true,
1283
1284        DefKind::Struct
1285        | DefKind::Union
1286        | DefKind::Enum
1287        | DefKind::Variant
1288        | DefKind::Field
1289        | DefKind::Const
1290        | DefKind::Static { .. }
1291        | DefKind::Ctor(..)
1292        | DefKind::TyAlias
1293        | DefKind::OpaqueTy
1294        | DefKind::ForeignTy
1295        | DefKind::Impl { .. }
1296        | DefKind::AssocConst
1297        | DefKind::Closure
1298        | DefKind::ConstParam
1299        | DefKind::AnonConst
1300        | DefKind::InlineConst
1301        | DefKind::AssocTy
1302        | DefKind::TyParam
1303        | DefKind::Trait
1304        | DefKind::TraitAlias
1305        | DefKind::Mod
1306        | DefKind::ForeignMod
1307        | DefKind::Macro(..)
1308        | DefKind::Use
1309        | DefKind::LifetimeParam
1310        | DefKind::GlobalAsm
1311        | DefKind::ExternCrate
1312        | DefKind::SyntheticCoroutineBody => false,
1313    }
1314}
1315
1316fn should_encode_constness(def_kind: DefKind) -> bool {
1317    match def_kind {
1318        DefKind::Fn
1319        | DefKind::AssocFn
1320        | DefKind::Closure
1321        | DefKind::Ctor(_, CtorKind::Fn)
1322        | DefKind::Impl { of_trait: false } => true,
1323
1324        DefKind::Struct
1325        | DefKind::Union
1326        | DefKind::Enum
1327        | DefKind::Field
1328        | DefKind::Const
1329        | DefKind::AssocConst
1330        | DefKind::AnonConst
1331        | DefKind::Static { .. }
1332        | DefKind::TyAlias
1333        | DefKind::OpaqueTy
1334        | DefKind::Impl { .. }
1335        | DefKind::ForeignTy
1336        | DefKind::ConstParam
1337        | DefKind::InlineConst
1338        | DefKind::AssocTy
1339        | DefKind::TyParam
1340        | DefKind::Trait
1341        | DefKind::TraitAlias
1342        | DefKind::Mod
1343        | DefKind::ForeignMod
1344        | DefKind::Macro(..)
1345        | DefKind::Use
1346        | DefKind::LifetimeParam
1347        | DefKind::GlobalAsm
1348        | DefKind::ExternCrate
1349        | DefKind::Ctor(_, CtorKind::Const)
1350        | DefKind::Variant
1351        | DefKind::SyntheticCoroutineBody => false,
1352    }
1353}
1354
1355fn should_encode_const(def_kind: DefKind) -> bool {
1356    match def_kind {
1357        // FIXME(mgca): should we remove Const and AssocConst here?
1358        DefKind::Const | DefKind::AssocConst | DefKind::AnonConst | DefKind::InlineConst => true,
1359
1360        DefKind::Struct
1361        | DefKind::Union
1362        | DefKind::Enum
1363        | DefKind::Variant
1364        | DefKind::Ctor(..)
1365        | DefKind::Field
1366        | DefKind::Fn
1367        | DefKind::Static { .. }
1368        | DefKind::TyAlias
1369        | DefKind::OpaqueTy
1370        | DefKind::ForeignTy
1371        | DefKind::Impl { .. }
1372        | DefKind::AssocFn
1373        | DefKind::Closure
1374        | DefKind::ConstParam
1375        | DefKind::AssocTy
1376        | DefKind::TyParam
1377        | DefKind::Trait
1378        | DefKind::TraitAlias
1379        | DefKind::Mod
1380        | DefKind::ForeignMod
1381        | DefKind::Macro(..)
1382        | DefKind::Use
1383        | DefKind::LifetimeParam
1384        | DefKind::GlobalAsm
1385        | DefKind::ExternCrate
1386        | DefKind::SyntheticCoroutineBody => false,
1387    }
1388}
1389
1390fn should_encode_const_of_item<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, def_kind: DefKind) -> bool {
1391    matches!(def_kind, DefKind::Const | DefKind::AssocConst)
1392        && find_attr!(tcx.get_all_attrs(def_id), AttributeKind::TypeConst(_))
1393        // AssocConst ==> assoc item has value
1394        && (!matches!(def_kind, DefKind::AssocConst) || assoc_item_has_value(tcx, def_id))
1395}
1396
1397fn assoc_item_has_value<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
1398    let assoc_item = tcx.associated_item(def_id);
1399    match assoc_item.container {
1400        ty::AssocContainer::InherentImpl | ty::AssocContainer::TraitImpl(_) => true,
1401        ty::AssocContainer::Trait => assoc_item.defaultness(tcx).has_value(),
1402    }
1403}
1404
1405impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
1406    fn encode_attrs(&mut self, def_id: LocalDefId) {
1407        let tcx = self.tcx;
1408        let mut state = AnalyzeAttrState {
1409            is_exported: tcx.effective_visibilities(()).is_exported(def_id),
1410            is_doc_hidden: false,
1411            features: &tcx.features(),
1412        };
1413        let attr_iter = tcx
1414            .hir_attrs(tcx.local_def_id_to_hir_id(def_id))
1415            .iter()
1416            .filter(|attr| analyze_attr(*attr, &mut state));
1417
1418        record_array!(self.tables.attributes[def_id.to_def_id()] <- attr_iter);
1419
1420        let mut attr_flags = AttrFlags::empty();
1421        if state.is_doc_hidden {
1422            attr_flags |= AttrFlags::IS_DOC_HIDDEN;
1423        }
1424        self.tables.attr_flags.set(def_id.local_def_index, attr_flags);
1425    }
1426
1427    fn encode_def_ids(&mut self) {
1428        self.encode_info_for_mod(CRATE_DEF_ID);
1429
1430        // Proc-macro crates only export proc-macro items, which are looked
1431        // up using `proc_macro_data`
1432        if self.is_proc_macro {
1433            return;
1434        }
1435
1436        let tcx = self.tcx;
1437
1438        for local_id in tcx.iter_local_def_id() {
1439            let def_id = local_id.to_def_id();
1440            let def_kind = tcx.def_kind(local_id);
1441            self.tables.def_kind.set_some(def_id.index, def_kind);
1442
1443            // The `DefCollector` will sometimes create unnecessary `DefId`s
1444            // for trivial const arguments which are directly lowered to
1445            // `ConstArgKind::Path`. We never actually access this `DefId`
1446            // anywhere so we don't need to encode it for other crates.
1447            if def_kind == DefKind::AnonConst
1448                && match tcx.hir_node_by_def_id(local_id) {
1449                    hir::Node::ConstArg(hir::ConstArg { kind, .. }) => match kind {
1450                        // Skip encoding defs for these as they should not have had a `DefId` created
1451                        hir::ConstArgKind::Error(..)
1452                        | hir::ConstArgKind::Path(..)
1453                        | hir::ConstArgKind::Infer(..) => true,
1454                        hir::ConstArgKind::Anon(..) => false,
1455                    },
1456                    _ => false,
1457                }
1458            {
1459                continue;
1460            }
1461
1462            if def_kind == DefKind::Field
1463                && let hir::Node::Field(field) = tcx.hir_node_by_def_id(local_id)
1464                && let Some(anon) = field.default
1465            {
1466                record!(self.tables.default_fields[def_id] <- anon.def_id.to_def_id());
1467            }
1468
1469            if should_encode_span(def_kind) {
1470                let def_span = tcx.def_span(local_id);
1471                record!(self.tables.def_span[def_id] <- def_span);
1472            }
1473            if should_encode_attrs(def_kind) {
1474                self.encode_attrs(local_id);
1475            }
1476            if should_encode_expn_that_defined(def_kind) {
1477                record!(self.tables.expn_that_defined[def_id] <- self.tcx.expn_that_defined(def_id));
1478            }
1479            if should_encode_span(def_kind)
1480                && let Some(ident_span) = tcx.def_ident_span(def_id)
1481            {
1482                record!(self.tables.def_ident_span[def_id] <- ident_span);
1483            }
1484            if def_kind.has_codegen_attrs() {
1485                record!(self.tables.codegen_fn_attrs[def_id] <- self.tcx.codegen_fn_attrs(def_id));
1486            }
1487            if should_encode_visibility(def_kind) {
1488                let vis =
1489                    self.tcx.local_visibility(local_id).map_id(|def_id| def_id.local_def_index);
1490                record!(self.tables.visibility[def_id] <- vis);
1491            }
1492            if should_encode_stability(def_kind) {
1493                self.encode_stability(def_id);
1494                self.encode_const_stability(def_id);
1495                self.encode_default_body_stability(def_id);
1496                self.encode_deprecation(def_id);
1497            }
1498            if should_encode_variances(tcx, def_id, def_kind) {
1499                let v = self.tcx.variances_of(def_id);
1500                record_array!(self.tables.variances_of[def_id] <- v);
1501            }
1502            if should_encode_fn_sig(def_kind) {
1503                record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
1504            }
1505            if should_encode_generics(def_kind) {
1506                let g = tcx.generics_of(def_id);
1507                record!(self.tables.generics_of[def_id] <- g);
1508                record!(self.tables.explicit_predicates_of[def_id] <- self.tcx.explicit_predicates_of(def_id));
1509                let inferred_outlives = self.tcx.inferred_outlives_of(def_id);
1510                record_defaulted_array!(self.tables.inferred_outlives_of[def_id] <- inferred_outlives);
1511
1512                for param in &g.own_params {
1513                    if let ty::GenericParamDefKind::Const { has_default: true, .. } = param.kind {
1514                        let default = self.tcx.const_param_default(param.def_id);
1515                        record!(self.tables.const_param_default[param.def_id] <- default);
1516                    }
1517                }
1518            }
1519            if tcx.is_conditionally_const(def_id) {
1520                record!(self.tables.const_conditions[def_id] <- self.tcx.const_conditions(def_id));
1521            }
1522            if should_encode_type(tcx, local_id, def_kind) {
1523                record!(self.tables.type_of[def_id] <- self.tcx.type_of(def_id));
1524            }
1525            if should_encode_constness(def_kind) {
1526                let constness = self.tcx.constness(def_id);
1527                match constness {
1528                    hir::Constness::Const => self.tables.constness.set(def_id.index, constness),
1529                    hir::Constness::NotConst => {}
1530                }
1531            }
1532            if let DefKind::Fn | DefKind::AssocFn = def_kind {
1533                let asyncness = tcx.asyncness(def_id);
1534                match asyncness {
1535                    ty::Asyncness::Yes => self.tables.asyncness.set(def_id.index, asyncness),
1536                    ty::Asyncness::No => {}
1537                }
1538                record_array!(self.tables.fn_arg_idents[def_id] <- tcx.fn_arg_idents(def_id));
1539            }
1540            if let Some(name) = tcx.intrinsic(def_id) {
1541                record!(self.tables.intrinsic[def_id] <- name);
1542            }
1543            if let DefKind::TyParam = def_kind {
1544                let default = self.tcx.object_lifetime_default(def_id);
1545                record!(self.tables.object_lifetime_default[def_id] <- default);
1546            }
1547            if let DefKind::Trait = def_kind {
1548                record!(self.tables.trait_def[def_id] <- self.tcx.trait_def(def_id));
1549                record_defaulted_array!(self.tables.explicit_super_predicates_of[def_id] <-
1550                    self.tcx.explicit_super_predicates_of(def_id).skip_binder());
1551                record_defaulted_array!(self.tables.explicit_implied_predicates_of[def_id] <-
1552                    self.tcx.explicit_implied_predicates_of(def_id).skip_binder());
1553                let module_children = self.tcx.module_children_local(local_id);
1554                record_array!(self.tables.module_children_non_reexports[def_id] <-
1555                    module_children.iter().map(|child| child.res.def_id().index));
1556                if self.tcx.is_const_trait(def_id) {
1557                    record_defaulted_array!(self.tables.explicit_implied_const_bounds[def_id]
1558                        <- self.tcx.explicit_implied_const_bounds(def_id).skip_binder());
1559                }
1560            }
1561            if let DefKind::TraitAlias = def_kind {
1562                record!(self.tables.trait_def[def_id] <- self.tcx.trait_def(def_id));
1563                record_defaulted_array!(self.tables.explicit_super_predicates_of[def_id] <-
1564                    self.tcx.explicit_super_predicates_of(def_id).skip_binder());
1565                record_defaulted_array!(self.tables.explicit_implied_predicates_of[def_id] <-
1566                    self.tcx.explicit_implied_predicates_of(def_id).skip_binder());
1567            }
1568            if let DefKind::Trait | DefKind::Impl { .. } = def_kind {
1569                let associated_item_def_ids = self.tcx.associated_item_def_ids(def_id);
1570                record_array!(self.tables.associated_item_or_field_def_ids[def_id] <-
1571                    associated_item_def_ids.iter().map(|&def_id| {
1572                        assert!(def_id.is_local());
1573                        def_id.index
1574                    })
1575                );
1576                for &def_id in associated_item_def_ids {
1577                    self.encode_info_for_assoc_item(def_id);
1578                }
1579            }
1580            if let DefKind::Closure | DefKind::SyntheticCoroutineBody = def_kind
1581                && let Some(coroutine_kind) = self.tcx.coroutine_kind(def_id)
1582            {
1583                self.tables.coroutine_kind.set(def_id.index, Some(coroutine_kind))
1584            }
1585            if def_kind == DefKind::Closure
1586                && tcx.type_of(def_id).skip_binder().is_coroutine_closure()
1587            {
1588                let coroutine_for_closure = self.tcx.coroutine_for_closure(def_id);
1589                self.tables
1590                    .coroutine_for_closure
1591                    .set_some(def_id.index, coroutine_for_closure.into());
1592
1593                // If this async closure has a by-move body, record it too.
1594                if tcx.needs_coroutine_by_move_body_def_id(coroutine_for_closure) {
1595                    self.tables.coroutine_by_move_body_def_id.set_some(
1596                        coroutine_for_closure.index,
1597                        self.tcx.coroutine_by_move_body_def_id(coroutine_for_closure).into(),
1598                    );
1599                }
1600            }
1601            if let DefKind::Static { .. } = def_kind {
1602                if !self.tcx.is_foreign_item(def_id) {
1603                    let data = self.tcx.eval_static_initializer(def_id).unwrap();
1604                    record!(self.tables.eval_static_initializer[def_id] <- data);
1605                }
1606            }
1607            if let DefKind::Enum | DefKind::Struct | DefKind::Union = def_kind {
1608                self.encode_info_for_adt(local_id);
1609            }
1610            if let DefKind::Mod = def_kind {
1611                self.encode_info_for_mod(local_id);
1612            }
1613            if let DefKind::Macro(_) = def_kind {
1614                self.encode_info_for_macro(local_id);
1615            }
1616            if let DefKind::TyAlias = def_kind {
1617                self.tables
1618                    .type_alias_is_lazy
1619                    .set(def_id.index, self.tcx.type_alias_is_lazy(def_id));
1620            }
1621            if let DefKind::OpaqueTy = def_kind {
1622                self.encode_explicit_item_bounds(def_id);
1623                self.encode_explicit_item_self_bounds(def_id);
1624                record!(self.tables.opaque_ty_origin[def_id] <- self.tcx.opaque_ty_origin(def_id));
1625                self.encode_precise_capturing_args(def_id);
1626                if tcx.is_conditionally_const(def_id) {
1627                    record_defaulted_array!(self.tables.explicit_implied_const_bounds[def_id]
1628                        <- tcx.explicit_implied_const_bounds(def_id).skip_binder());
1629                }
1630            }
1631            if let DefKind::AnonConst = def_kind {
1632                record!(self.tables.anon_const_kind[def_id] <- self.tcx.anon_const_kind(def_id));
1633            }
1634            if should_encode_const_of_item(self.tcx, def_id, def_kind) {
1635                record!(self.tables.const_of_item[def_id] <- self.tcx.const_of_item(def_id));
1636            }
1637            if tcx.impl_method_has_trait_impl_trait_tys(def_id)
1638                && let Ok(table) = self.tcx.collect_return_position_impl_trait_in_trait_tys(def_id)
1639            {
1640                record!(self.tables.trait_impl_trait_tys[def_id] <- table);
1641            }
1642            if let DefKind::Impl { .. } | DefKind::Trait = def_kind {
1643                let table = tcx.associated_types_for_impl_traits_in_trait_or_impl(def_id);
1644                record!(self.tables.associated_types_for_impl_traits_in_trait_or_impl[def_id] <- table);
1645            }
1646        }
1647
1648        for (def_id, impls) in &tcx.crate_inherent_impls(()).0.inherent_impls {
1649            record_defaulted_array!(self.tables.inherent_impls[def_id.to_def_id()] <- impls.iter().map(|def_id| {
1650                assert!(def_id.is_local());
1651                def_id.index
1652            }));
1653        }
1654
1655        for (def_id, res_map) in &tcx.resolutions(()).doc_link_resolutions {
1656            record!(self.tables.doc_link_resolutions[def_id.to_def_id()] <- res_map);
1657        }
1658
1659        for (def_id, traits) in &tcx.resolutions(()).doc_link_traits_in_scope {
1660            record_array!(self.tables.doc_link_traits_in_scope[def_id.to_def_id()] <- traits);
1661        }
1662    }
1663
1664    #[instrument(level = "trace", skip(self))]
1665    fn encode_info_for_adt(&mut self, local_def_id: LocalDefId) {
1666        let def_id = local_def_id.to_def_id();
1667        let tcx = self.tcx;
1668        let adt_def = tcx.adt_def(def_id);
1669        record!(self.tables.repr_options[def_id] <- adt_def.repr());
1670
1671        let params_in_repr = self.tcx.params_in_repr(def_id);
1672        record!(self.tables.params_in_repr[def_id] <- params_in_repr);
1673
1674        if adt_def.is_enum() {
1675            let module_children = tcx.module_children_local(local_def_id);
1676            record_array!(self.tables.module_children_non_reexports[def_id] <-
1677                module_children.iter().map(|child| child.res.def_id().index));
1678        } else {
1679            // For non-enum, there is only one variant, and its def_id is the adt's.
1680            debug_assert_eq!(adt_def.variants().len(), 1);
1681            debug_assert_eq!(adt_def.non_enum_variant().def_id, def_id);
1682            // Therefore, the loop over variants will encode its fields as the adt's children.
1683        }
1684
1685        for (idx, variant) in adt_def.variants().iter_enumerated() {
1686            let data = VariantData {
1687                discr: variant.discr,
1688                idx,
1689                ctor: variant.ctor.map(|(kind, def_id)| (kind, def_id.index)),
1690                is_non_exhaustive: variant.is_field_list_non_exhaustive(),
1691            };
1692            record!(self.tables.variant_data[variant.def_id] <- data);
1693
1694            record_array!(self.tables.associated_item_or_field_def_ids[variant.def_id] <- variant.fields.iter().map(|f| {
1695                assert!(f.did.is_local());
1696                f.did.index
1697            }));
1698
1699            for field in &variant.fields {
1700                self.tables.safety.set_some(field.did.index, field.safety);
1701            }
1702
1703            if let Some((CtorKind::Fn, ctor_def_id)) = variant.ctor {
1704                let fn_sig = tcx.fn_sig(ctor_def_id);
1705                // FIXME only encode signature for ctor_def_id
1706                record!(self.tables.fn_sig[variant.def_id] <- fn_sig);
1707            }
1708        }
1709
1710        if let Some(destructor) = tcx.adt_destructor(local_def_id) {
1711            record!(self.tables.adt_destructor[def_id] <- destructor);
1712        }
1713
1714        if let Some(destructor) = tcx.adt_async_destructor(local_def_id) {
1715            record!(self.tables.adt_async_destructor[def_id] <- destructor);
1716        }
1717    }
1718
1719    #[instrument(level = "debug", skip(self))]
1720    fn encode_info_for_mod(&mut self, local_def_id: LocalDefId) {
1721        let tcx = self.tcx;
1722        let def_id = local_def_id.to_def_id();
1723
1724        // If we are encoding a proc-macro crates, `encode_info_for_mod` will
1725        // only ever get called for the crate root. We still want to encode
1726        // the crate root for consistency with other crates (some of the resolver
1727        // code uses it). However, we skip encoding anything relating to child
1728        // items - we encode information about proc-macros later on.
1729        if self.is_proc_macro {
1730            // Encode this here because we don't do it in encode_def_ids.
1731            record!(self.tables.expn_that_defined[def_id] <- tcx.expn_that_defined(local_def_id));
1732        } else {
1733            let module_children = tcx.module_children_local(local_def_id);
1734
1735            record_array!(self.tables.module_children_non_reexports[def_id] <-
1736                module_children.iter().filter(|child| child.reexport_chain.is_empty())
1737                    .map(|child| child.res.def_id().index));
1738
1739            record_defaulted_array!(self.tables.module_children_reexports[def_id] <-
1740                module_children.iter().filter(|child| !child.reexport_chain.is_empty()));
1741        }
1742    }
1743
1744    fn encode_explicit_item_bounds(&mut self, def_id: DefId) {
1745        debug!("EncodeContext::encode_explicit_item_bounds({:?})", def_id);
1746        let bounds = self.tcx.explicit_item_bounds(def_id).skip_binder();
1747        record_defaulted_array!(self.tables.explicit_item_bounds[def_id] <- bounds);
1748    }
1749
1750    fn encode_explicit_item_self_bounds(&mut self, def_id: DefId) {
1751        debug!("EncodeContext::encode_explicit_item_self_bounds({:?})", def_id);
1752        let bounds = self.tcx.explicit_item_self_bounds(def_id).skip_binder();
1753        record_defaulted_array!(self.tables.explicit_item_self_bounds[def_id] <- bounds);
1754    }
1755
1756    #[instrument(level = "debug", skip(self))]
1757    fn encode_info_for_assoc_item(&mut self, def_id: DefId) {
1758        let tcx = self.tcx;
1759        let item = tcx.associated_item(def_id);
1760
1761        if matches!(item.container, AssocContainer::Trait | AssocContainer::TraitImpl(_)) {
1762            self.tables.defaultness.set_some(def_id.index, item.defaultness(tcx));
1763        }
1764
1765        record!(self.tables.assoc_container[def_id] <- item.container);
1766
1767        if let AssocContainer::Trait = item.container
1768            && item.is_type()
1769        {
1770            self.encode_explicit_item_bounds(def_id);
1771            self.encode_explicit_item_self_bounds(def_id);
1772            if tcx.is_conditionally_const(def_id) {
1773                record_defaulted_array!(self.tables.explicit_implied_const_bounds[def_id]
1774                    <- self.tcx.explicit_implied_const_bounds(def_id).skip_binder());
1775            }
1776        }
1777        if let ty::AssocKind::Type { data: ty::AssocTypeData::Rpitit(rpitit_info) } = item.kind {
1778            record!(self.tables.opt_rpitit_info[def_id] <- rpitit_info);
1779            if matches!(rpitit_info, ty::ImplTraitInTraitData::Trait { .. }) {
1780                record_array!(
1781                    self.tables.assumed_wf_types_for_rpitit[def_id]
1782                        <- self.tcx.assumed_wf_types_for_rpitit(def_id)
1783                );
1784                self.encode_precise_capturing_args(def_id);
1785            }
1786        }
1787    }
1788
1789    fn encode_precise_capturing_args(&mut self, def_id: DefId) {
1790        let Some(precise_capturing_args) = self.tcx.rendered_precise_capturing_args(def_id) else {
1791            return;
1792        };
1793
1794        record_array!(self.tables.rendered_precise_capturing_args[def_id] <- precise_capturing_args);
1795    }
1796
1797    fn encode_mir(&mut self) {
1798        if self.is_proc_macro {
1799            return;
1800        }
1801
1802        let tcx = self.tcx;
1803        let reachable_set = tcx.reachable_set(());
1804
1805        let keys_and_jobs = tcx.mir_keys(()).iter().filter_map(|&def_id| {
1806            let (encode_const, encode_opt) = should_encode_mir(tcx, reachable_set, def_id);
1807            if encode_const || encode_opt { Some((def_id, encode_const, encode_opt)) } else { None }
1808        });
1809        for (def_id, encode_const, encode_opt) in keys_and_jobs {
1810            debug_assert!(encode_const || encode_opt);
1811
1812            debug!("EntryBuilder::encode_mir({:?})", def_id);
1813            if encode_opt {
1814                record!(self.tables.optimized_mir[def_id.to_def_id()] <- tcx.optimized_mir(def_id));
1815                self.tables
1816                    .cross_crate_inlinable
1817                    .set(def_id.to_def_id().index, self.tcx.cross_crate_inlinable(def_id));
1818                record!(self.tables.closure_saved_names_of_captured_variables[def_id.to_def_id()]
1819                    <- tcx.closure_saved_names_of_captured_variables(def_id));
1820
1821                if self.tcx.is_coroutine(def_id.to_def_id())
1822                    && let Some(witnesses) = tcx.mir_coroutine_witnesses(def_id)
1823                {
1824                    record!(self.tables.mir_coroutine_witnesses[def_id.to_def_id()] <- witnesses);
1825                }
1826            }
1827            let mut is_trivial = false;
1828            if encode_const {
1829                if let Some((val, ty)) = tcx.trivial_const(def_id) {
1830                    is_trivial = true;
1831                    record!(self.tables.trivial_const[def_id.to_def_id()] <- (val, ty));
1832                } else {
1833                    is_trivial = false;
1834                    record!(self.tables.mir_for_ctfe[def_id.to_def_id()] <- tcx.mir_for_ctfe(def_id));
1835                }
1836
1837                // FIXME(generic_const_exprs): this feels wrong to have in `encode_mir`
1838                let abstract_const = tcx.thir_abstract_const(def_id);
1839                if let Ok(Some(abstract_const)) = abstract_const {
1840                    record!(self.tables.thir_abstract_const[def_id.to_def_id()] <- abstract_const);
1841                }
1842
1843                if should_encode_const(tcx.def_kind(def_id)) {
1844                    let qualifs = tcx.mir_const_qualif(def_id);
1845                    record!(self.tables.mir_const_qualif[def_id.to_def_id()] <- qualifs);
1846                    let body = tcx.hir_maybe_body_owned_by(def_id);
1847                    if let Some(body) = body {
1848                        let const_data = rendered_const(self.tcx, &body, def_id);
1849                        record!(self.tables.rendered_const[def_id.to_def_id()] <- const_data);
1850                    }
1851                }
1852            }
1853            if !is_trivial {
1854                record!(self.tables.promoted_mir[def_id.to_def_id()] <- tcx.promoted_mir(def_id));
1855            }
1856
1857            if self.tcx.is_coroutine(def_id.to_def_id())
1858                && let Some(witnesses) = tcx.mir_coroutine_witnesses(def_id)
1859            {
1860                record!(self.tables.mir_coroutine_witnesses[def_id.to_def_id()] <- witnesses);
1861            }
1862        }
1863
1864        // Encode all the deduced parameter attributes for everything that has MIR, even for items
1865        // that can't be inlined. But don't if we aren't optimizing in non-incremental mode, to
1866        // save the query traffic.
1867        if tcx.sess.opts.output_types.should_codegen()
1868            && tcx.sess.opts.optimize != OptLevel::No
1869            && tcx.sess.opts.incremental.is_none()
1870        {
1871            for &local_def_id in tcx.mir_keys(()) {
1872                if let DefKind::AssocFn | DefKind::Fn = tcx.def_kind(local_def_id) {
1873                    record_array!(self.tables.deduced_param_attrs[local_def_id.to_def_id()] <-
1874                        self.tcx.deduced_param_attrs(local_def_id.to_def_id()));
1875                }
1876            }
1877        }
1878    }
1879
1880    #[instrument(level = "debug", skip(self))]
1881    fn encode_stability(&mut self, def_id: DefId) {
1882        // The query lookup can take a measurable amount of time in crates with many items. Check if
1883        // the stability attributes are even enabled before using their queries.
1884        if self.feat.staged_api() || self.tcx.sess.opts.unstable_opts.force_unstable_if_unmarked {
1885            if let Some(stab) = self.tcx.lookup_stability(def_id) {
1886                record!(self.tables.lookup_stability[def_id] <- stab)
1887            }
1888        }
1889    }
1890
1891    #[instrument(level = "debug", skip(self))]
1892    fn encode_const_stability(&mut self, def_id: DefId) {
1893        // The query lookup can take a measurable amount of time in crates with many items. Check if
1894        // the stability attributes are even enabled before using their queries.
1895        if self.feat.staged_api() || self.tcx.sess.opts.unstable_opts.force_unstable_if_unmarked {
1896            if let Some(stab) = self.tcx.lookup_const_stability(def_id) {
1897                record!(self.tables.lookup_const_stability[def_id] <- stab)
1898            }
1899        }
1900    }
1901
1902    #[instrument(level = "debug", skip(self))]
1903    fn encode_default_body_stability(&mut self, def_id: DefId) {
1904        // The query lookup can take a measurable amount of time in crates with many items. Check if
1905        // the stability attributes are even enabled before using their queries.
1906        if self.feat.staged_api() || self.tcx.sess.opts.unstable_opts.force_unstable_if_unmarked {
1907            if let Some(stab) = self.tcx.lookup_default_body_stability(def_id) {
1908                record!(self.tables.lookup_default_body_stability[def_id] <- stab)
1909            }
1910        }
1911    }
1912
1913    #[instrument(level = "debug", skip(self))]
1914    fn encode_deprecation(&mut self, def_id: DefId) {
1915        if let Some(depr) = self.tcx.lookup_deprecation(def_id) {
1916            record!(self.tables.lookup_deprecation_entry[def_id] <- depr);
1917        }
1918    }
1919
1920    #[instrument(level = "debug", skip(self))]
1921    fn encode_info_for_macro(&mut self, def_id: LocalDefId) {
1922        let tcx = self.tcx;
1923
1924        let (_, macro_def, _) = tcx.hir_expect_item(def_id).expect_macro();
1925        self.tables.is_macro_rules.set(def_id.local_def_index, macro_def.macro_rules);
1926        record!(self.tables.macro_definition[def_id.to_def_id()] <- &*macro_def.body);
1927    }
1928
1929    fn encode_native_libraries(&mut self) -> LazyArray<NativeLib> {
1930        empty_proc_macro!(self);
1931        let used_libraries = self.tcx.native_libraries(LOCAL_CRATE);
1932        self.lazy_array(used_libraries.iter())
1933    }
1934
1935    fn encode_foreign_modules(&mut self) -> LazyArray<ForeignModule> {
1936        empty_proc_macro!(self);
1937        let foreign_modules = self.tcx.foreign_modules(LOCAL_CRATE);
1938        self.lazy_array(foreign_modules.iter().map(|(_, m)| m).cloned())
1939    }
1940
1941    fn encode_hygiene(&mut self) -> (SyntaxContextTable, ExpnDataTable, ExpnHashTable) {
1942        let mut syntax_contexts: TableBuilder<_, _> = Default::default();
1943        let mut expn_data_table: TableBuilder<_, _> = Default::default();
1944        let mut expn_hash_table: TableBuilder<_, _> = Default::default();
1945
1946        self.hygiene_ctxt.encode(
1947            &mut (&mut *self, &mut syntax_contexts, &mut expn_data_table, &mut expn_hash_table),
1948            |(this, syntax_contexts, _, _), index, ctxt_data| {
1949                syntax_contexts.set_some(index, this.lazy(ctxt_data));
1950            },
1951            |(this, _, expn_data_table, expn_hash_table), index, expn_data, hash| {
1952                if let Some(index) = index.as_local() {
1953                    expn_data_table.set_some(index.as_raw(), this.lazy(expn_data));
1954                    expn_hash_table.set_some(index.as_raw(), this.lazy(hash));
1955                }
1956            },
1957        );
1958
1959        (
1960            syntax_contexts.encode(&mut self.opaque),
1961            expn_data_table.encode(&mut self.opaque),
1962            expn_hash_table.encode(&mut self.opaque),
1963        )
1964    }
1965
1966    fn encode_proc_macros(&mut self) -> Option<ProcMacroData> {
1967        let is_proc_macro = self.tcx.crate_types().contains(&CrateType::ProcMacro);
1968        if is_proc_macro {
1969            let tcx = self.tcx;
1970            let proc_macro_decls_static = tcx.proc_macro_decls_static(()).unwrap().local_def_index;
1971            let stability = tcx.lookup_stability(CRATE_DEF_ID);
1972            let macros =
1973                self.lazy_array(tcx.resolutions(()).proc_macros.iter().map(|p| p.local_def_index));
1974            for (i, span) in self.tcx.sess.psess.proc_macro_quoted_spans() {
1975                let span = self.lazy(span);
1976                self.tables.proc_macro_quoted_spans.set_some(i, span);
1977            }
1978
1979            self.tables.def_kind.set_some(LOCAL_CRATE.as_def_id().index, DefKind::Mod);
1980            record!(self.tables.def_span[LOCAL_CRATE.as_def_id()] <- tcx.def_span(LOCAL_CRATE.as_def_id()));
1981            self.encode_attrs(LOCAL_CRATE.as_def_id().expect_local());
1982            let vis = tcx.local_visibility(CRATE_DEF_ID).map_id(|def_id| def_id.local_def_index);
1983            record!(self.tables.visibility[LOCAL_CRATE.as_def_id()] <- vis);
1984            if let Some(stability) = stability {
1985                record!(self.tables.lookup_stability[LOCAL_CRATE.as_def_id()] <- stability);
1986            }
1987            self.encode_deprecation(LOCAL_CRATE.as_def_id());
1988            if let Some(res_map) = tcx.resolutions(()).doc_link_resolutions.get(&CRATE_DEF_ID) {
1989                record!(self.tables.doc_link_resolutions[LOCAL_CRATE.as_def_id()] <- res_map);
1990            }
1991            if let Some(traits) = tcx.resolutions(()).doc_link_traits_in_scope.get(&CRATE_DEF_ID) {
1992                record_array!(self.tables.doc_link_traits_in_scope[LOCAL_CRATE.as_def_id()] <- traits);
1993            }
1994
1995            // Normally, this information is encoded when we walk the items
1996            // defined in this crate. However, we skip doing that for proc-macro crates,
1997            // so we manually encode just the information that we need
1998            for &proc_macro in &tcx.resolutions(()).proc_macros {
1999                let id = proc_macro;
2000                let proc_macro = tcx.local_def_id_to_hir_id(proc_macro);
2001                let mut name = tcx.hir_name(proc_macro);
2002                let span = tcx.hir_span(proc_macro);
2003                // Proc-macros may have attributes like `#[allow_internal_unstable]`,
2004                // so downstream crates need access to them.
2005                let attrs = tcx.hir_attrs(proc_macro);
2006                let macro_kind = if find_attr!(attrs, AttributeKind::ProcMacro(..)) {
2007                    MacroKind::Bang
2008                } else if find_attr!(attrs, AttributeKind::ProcMacroAttribute(..)) {
2009                    MacroKind::Attr
2010                } else if let Some(trait_name) = find_attr!(attrs, AttributeKind::ProcMacroDerive { trait_name, ..} => trait_name)
2011                {
2012                    name = *trait_name;
2013                    MacroKind::Derive
2014                } else {
2015                    bug!("Unknown proc-macro type for item {:?}", id);
2016                };
2017
2018                let mut def_key = self.tcx.hir_def_key(id);
2019                def_key.disambiguated_data.data = DefPathData::MacroNs(name);
2020
2021                let def_id = id.to_def_id();
2022                self.tables.def_kind.set_some(def_id.index, DefKind::Macro(macro_kind.into()));
2023                self.tables.proc_macro.set_some(def_id.index, macro_kind);
2024                self.encode_attrs(id);
2025                record!(self.tables.def_keys[def_id] <- def_key);
2026                record!(self.tables.def_ident_span[def_id] <- span);
2027                record!(self.tables.def_span[def_id] <- span);
2028                record!(self.tables.visibility[def_id] <- ty::Visibility::Public);
2029                if let Some(stability) = stability {
2030                    record!(self.tables.lookup_stability[def_id] <- stability);
2031                }
2032            }
2033
2034            Some(ProcMacroData { proc_macro_decls_static, stability, macros })
2035        } else {
2036            None
2037        }
2038    }
2039
2040    fn encode_debugger_visualizers(&mut self) -> LazyArray<DebuggerVisualizerFile> {
2041        empty_proc_macro!(self);
2042        self.lazy_array(
2043            self.tcx
2044                .debugger_visualizers(LOCAL_CRATE)
2045                .iter()
2046                // Erase the path since it may contain privacy sensitive data
2047                // that we don't want to end up in crate metadata.
2048                // The path is only needed for the local crate because of
2049                // `--emit dep-info`.
2050                .map(DebuggerVisualizerFile::path_erased),
2051        )
2052    }
2053
2054    fn encode_crate_deps(&mut self) -> LazyArray<CrateDep> {
2055        empty_proc_macro!(self);
2056
2057        let deps = self
2058            .tcx
2059            .crates(())
2060            .iter()
2061            .map(|&cnum| {
2062                let dep = CrateDep {
2063                    name: self.tcx.crate_name(cnum),
2064                    hash: self.tcx.crate_hash(cnum),
2065                    host_hash: self.tcx.crate_host_hash(cnum),
2066                    kind: self.tcx.dep_kind(cnum),
2067                    extra_filename: self.tcx.extra_filename(cnum).clone(),
2068                    is_private: self.tcx.is_private_dep(cnum),
2069                };
2070                (cnum, dep)
2071            })
2072            .collect::<Vec<_>>();
2073
2074        {
2075            // Sanity-check the crate numbers
2076            let mut expected_cnum = 1;
2077            for &(n, _) in &deps {
2078                assert_eq!(n, CrateNum::new(expected_cnum));
2079                expected_cnum += 1;
2080            }
2081        }
2082
2083        // We're just going to write a list of crate 'name-hash-version's, with
2084        // the assumption that they are numbered 1 to n.
2085        // FIXME (#2166): This is not nearly enough to support correct versioning
2086        // but is enough to get transitive crate dependencies working.
2087        self.lazy_array(deps.iter().map(|(_, dep)| dep))
2088    }
2089
2090    fn encode_target_modifiers(&mut self) -> LazyArray<TargetModifier> {
2091        empty_proc_macro!(self);
2092        let tcx = self.tcx;
2093        self.lazy_array(tcx.sess.opts.gather_target_modifiers())
2094    }
2095
2096    fn encode_lib_features(&mut self) -> LazyArray<(Symbol, FeatureStability)> {
2097        empty_proc_macro!(self);
2098        let tcx = self.tcx;
2099        let lib_features = tcx.lib_features(LOCAL_CRATE);
2100        self.lazy_array(lib_features.to_sorted_vec())
2101    }
2102
2103    fn encode_stability_implications(&mut self) -> LazyArray<(Symbol, Symbol)> {
2104        empty_proc_macro!(self);
2105        let tcx = self.tcx;
2106        let implications = tcx.stability_implications(LOCAL_CRATE);
2107        let sorted = implications.to_sorted_stable_ord();
2108        self.lazy_array(sorted.into_iter().map(|(k, v)| (*k, *v)))
2109    }
2110
2111    fn encode_diagnostic_items(&mut self) -> LazyArray<(Symbol, DefIndex)> {
2112        empty_proc_macro!(self);
2113        let tcx = self.tcx;
2114        let diagnostic_items = &tcx.diagnostic_items(LOCAL_CRATE).name_to_id;
2115        self.lazy_array(diagnostic_items.iter().map(|(&name, def_id)| (name, def_id.index)))
2116    }
2117
2118    fn encode_lang_items(&mut self) -> LazyArray<(DefIndex, LangItem)> {
2119        empty_proc_macro!(self);
2120        let lang_items = self.tcx.lang_items().iter();
2121        self.lazy_array(lang_items.filter_map(|(lang_item, def_id)| {
2122            def_id.as_local().map(|id| (id.local_def_index, lang_item))
2123        }))
2124    }
2125
2126    fn encode_lang_items_missing(&mut self) -> LazyArray<LangItem> {
2127        empty_proc_macro!(self);
2128        let tcx = self.tcx;
2129        self.lazy_array(&tcx.lang_items().missing)
2130    }
2131
2132    fn encode_stripped_cfg_items(&mut self) -> LazyArray<StrippedCfgItem<DefIndex>> {
2133        self.lazy_array(
2134            self.tcx
2135                .stripped_cfg_items(LOCAL_CRATE)
2136                .into_iter()
2137                .map(|item| item.clone().map_mod_id(|def_id| def_id.index)),
2138        )
2139    }
2140
2141    fn encode_traits(&mut self) -> LazyArray<DefIndex> {
2142        empty_proc_macro!(self);
2143        self.lazy_array(self.tcx.traits(LOCAL_CRATE).iter().map(|def_id| def_id.index))
2144    }
2145
2146    /// Encodes an index, mapping each trait to its (local) implementations.
2147    #[instrument(level = "debug", skip(self))]
2148    fn encode_impls(&mut self) -> LazyArray<TraitImpls> {
2149        empty_proc_macro!(self);
2150        let tcx = self.tcx;
2151        let mut trait_impls: FxIndexMap<DefId, Vec<(DefIndex, Option<SimplifiedType>)>> =
2152            FxIndexMap::default();
2153
2154        for id in tcx.hir_free_items() {
2155            let DefKind::Impl { of_trait } = tcx.def_kind(id.owner_id) else {
2156                continue;
2157            };
2158            let def_id = id.owner_id.to_def_id();
2159
2160            if of_trait {
2161                let header = tcx.impl_trait_header(def_id);
2162                record!(self.tables.impl_trait_header[def_id] <- header);
2163
2164                self.tables.defaultness.set_some(def_id.index, tcx.defaultness(def_id));
2165
2166                let trait_ref = header.trait_ref.instantiate_identity();
2167                let simplified_self_ty = fast_reject::simplify_type(
2168                    self.tcx,
2169                    trait_ref.self_ty(),
2170                    TreatParams::InstantiateWithInfer,
2171                );
2172                trait_impls
2173                    .entry(trait_ref.def_id)
2174                    .or_default()
2175                    .push((id.owner_id.def_id.local_def_index, simplified_self_ty));
2176
2177                let trait_def = tcx.trait_def(trait_ref.def_id);
2178                if let Ok(mut an) = trait_def.ancestors(tcx, def_id)
2179                    && let Some(specialization_graph::Node::Impl(parent)) = an.nth(1)
2180                {
2181                    self.tables.impl_parent.set_some(def_id.index, parent.into());
2182                }
2183
2184                // if this is an impl of `CoerceUnsized`, create its
2185                // "unsized info", else just store None
2186                if tcx.is_lang_item(trait_ref.def_id, LangItem::CoerceUnsized) {
2187                    let coerce_unsized_info = tcx.coerce_unsized_info(def_id).unwrap();
2188                    record!(self.tables.coerce_unsized_info[def_id] <- coerce_unsized_info);
2189                }
2190            }
2191        }
2192
2193        let trait_impls: Vec<_> = trait_impls
2194            .into_iter()
2195            .map(|(trait_def_id, impls)| TraitImpls {
2196                trait_id: (trait_def_id.krate.as_u32(), trait_def_id.index),
2197                impls: self.lazy_array(&impls),
2198            })
2199            .collect();
2200
2201        self.lazy_array(&trait_impls)
2202    }
2203
2204    #[instrument(level = "debug", skip(self))]
2205    fn encode_incoherent_impls(&mut self) -> LazyArray<IncoherentImpls> {
2206        empty_proc_macro!(self);
2207        let tcx = self.tcx;
2208
2209        let all_impls: Vec<_> = tcx
2210            .crate_inherent_impls(())
2211            .0
2212            .incoherent_impls
2213            .iter()
2214            .map(|(&simp, impls)| IncoherentImpls {
2215                self_ty: simp,
2216                impls: self.lazy_array(impls.iter().map(|def_id| def_id.local_def_index)),
2217            })
2218            .collect();
2219
2220        self.lazy_array(&all_impls)
2221    }
2222
2223    fn encode_exportable_items(&mut self) -> LazyArray<DefIndex> {
2224        empty_proc_macro!(self);
2225        self.lazy_array(self.tcx.exportable_items(LOCAL_CRATE).iter().map(|def_id| def_id.index))
2226    }
2227
2228    fn encode_stable_order_of_exportable_impls(&mut self) -> LazyArray<(DefIndex, usize)> {
2229        empty_proc_macro!(self);
2230        let stable_order_of_exportable_impls =
2231            self.tcx.stable_order_of_exportable_impls(LOCAL_CRATE);
2232        self.lazy_array(
2233            stable_order_of_exportable_impls.iter().map(|(def_id, idx)| (def_id.index, *idx)),
2234        )
2235    }
2236
2237    // Encodes all symbols exported from this crate into the metadata.
2238    //
2239    // This pass is seeded off the reachability list calculated in the
2240    // middle::reachable module but filters out items that either don't have a
2241    // symbol associated with them (they weren't translated) or if they're an FFI
2242    // definition (as that's not defined in this crate).
2243    fn encode_exported_symbols(
2244        &mut self,
2245        exported_symbols: &[(ExportedSymbol<'tcx>, SymbolExportInfo)],
2246    ) -> LazyArray<(ExportedSymbol<'static>, SymbolExportInfo)> {
2247        empty_proc_macro!(self);
2248
2249        self.lazy_array(exported_symbols.iter().cloned())
2250    }
2251
2252    fn encode_dylib_dependency_formats(&mut self) -> LazyArray<Option<LinkagePreference>> {
2253        empty_proc_macro!(self);
2254        let formats = self.tcx.dependency_formats(());
2255        if let Some(arr) = formats.get(&CrateType::Dylib) {
2256            return self.lazy_array(arr.iter().skip(1 /* skip LOCAL_CRATE */).map(
2257                |slot| match *slot {
2258                    Linkage::NotLinked | Linkage::IncludedFromDylib => None,
2259
2260                    Linkage::Dynamic => Some(LinkagePreference::RequireDynamic),
2261                    Linkage::Static => Some(LinkagePreference::RequireStatic),
2262                },
2263            ));
2264        }
2265        LazyArray::default()
2266    }
2267}
2268
2269/// Used to prefetch queries which will be needed later by metadata encoding.
2270/// Only a subset of the queries are actually prefetched to keep this code smaller.
2271fn prefetch_mir(tcx: TyCtxt<'_>) {
2272    if !tcx.sess.opts.output_types.should_codegen() {
2273        // We won't emit MIR, so don't prefetch it.
2274        return;
2275    }
2276
2277    let reachable_set = tcx.reachable_set(());
2278    par_for_each_in(tcx.mir_keys(()), |&&def_id| {
2279        if tcx.is_trivial_const(def_id) {
2280            return;
2281        }
2282        let (encode_const, encode_opt) = should_encode_mir(tcx, reachable_set, def_id);
2283
2284        if encode_const {
2285            tcx.ensure_done().mir_for_ctfe(def_id);
2286        }
2287        if encode_opt {
2288            tcx.ensure_done().optimized_mir(def_id);
2289        }
2290        if encode_opt || encode_const {
2291            tcx.ensure_done().promoted_mir(def_id);
2292        }
2293    })
2294}
2295
2296// NOTE(eddyb) The following comment was preserved for posterity, even
2297// though it's no longer relevant as EBML (which uses nested & tagged
2298// "documents") was replaced with a scheme that can't go out of bounds.
2299//
2300// And here we run into yet another obscure archive bug: in which metadata
2301// loaded from archives may have trailing garbage bytes. Awhile back one of
2302// our tests was failing sporadically on the macOS 64-bit builders (both nopt
2303// and opt) by having ebml generate an out-of-bounds panic when looking at
2304// metadata.
2305//
2306// Upon investigation it turned out that the metadata file inside of an rlib
2307// (and ar archive) was being corrupted. Some compilations would generate a
2308// metadata file which would end in a few extra bytes, while other
2309// compilations would not have these extra bytes appended to the end. These
2310// extra bytes were interpreted by ebml as an extra tag, so they ended up
2311// being interpreted causing the out-of-bounds.
2312//
2313// The root cause of why these extra bytes were appearing was never
2314// discovered, and in the meantime the solution we're employing is to insert
2315// the length of the metadata to the start of the metadata. Later on this
2316// will allow us to slice the metadata to the precise length that we just
2317// generated regardless of trailing bytes that end up in it.
2318
2319pub struct EncodedMetadata {
2320    // The declaration order matters because `full_metadata` should be dropped
2321    // before `_temp_dir`.
2322    full_metadata: Option<Mmap>,
2323    // This is an optional stub metadata containing only the crate header.
2324    // The header should be very small, so we load it directly into memory.
2325    stub_metadata: Option<Vec<u8>>,
2326    // The path containing the metadata, to record as work product.
2327    path: Option<Box<Path>>,
2328    // We need to carry MaybeTempDir to avoid deleting the temporary
2329    // directory while accessing the Mmap.
2330    _temp_dir: Option<MaybeTempDir>,
2331}
2332
2333impl EncodedMetadata {
2334    #[inline]
2335    pub fn from_path(
2336        path: PathBuf,
2337        stub_path: Option<PathBuf>,
2338        temp_dir: Option<MaybeTempDir>,
2339    ) -> std::io::Result<Self> {
2340        let file = std::fs::File::open(&path)?;
2341        let file_metadata = file.metadata()?;
2342        if file_metadata.len() == 0 {
2343            return Ok(Self {
2344                full_metadata: None,
2345                stub_metadata: None,
2346                path: None,
2347                _temp_dir: None,
2348            });
2349        }
2350        let full_mmap = unsafe { Some(Mmap::map(file)?) };
2351
2352        let stub =
2353            if let Some(stub_path) = stub_path { Some(std::fs::read(stub_path)?) } else { None };
2354
2355        Ok(Self {
2356            full_metadata: full_mmap,
2357            stub_metadata: stub,
2358            path: Some(path.into()),
2359            _temp_dir: temp_dir,
2360        })
2361    }
2362
2363    #[inline]
2364    pub fn full(&self) -> &[u8] {
2365        &self.full_metadata.as_deref().unwrap_or_default()
2366    }
2367
2368    #[inline]
2369    pub fn stub_or_full(&self) -> &[u8] {
2370        self.stub_metadata.as_deref().unwrap_or(self.full())
2371    }
2372
2373    #[inline]
2374    pub fn path(&self) -> Option<&Path> {
2375        self.path.as_deref()
2376    }
2377}
2378
2379impl<S: Encoder> Encodable<S> for EncodedMetadata {
2380    fn encode(&self, s: &mut S) {
2381        self.stub_metadata.encode(s);
2382
2383        let slice = self.full();
2384        slice.encode(s)
2385    }
2386}
2387
2388impl<D: Decoder> Decodable<D> for EncodedMetadata {
2389    fn decode(d: &mut D) -> Self {
2390        let stub = <Option<Vec<u8>>>::decode(d);
2391
2392        let len = d.read_usize();
2393        let full_metadata = if len > 0 {
2394            let mut mmap = MmapMut::map_anon(len).unwrap();
2395            mmap.copy_from_slice(d.read_raw_bytes(len));
2396            Some(mmap.make_read_only().unwrap())
2397        } else {
2398            None
2399        };
2400
2401        Self { full_metadata, stub_metadata: stub, path: None, _temp_dir: None }
2402    }
2403}
2404
2405#[instrument(level = "trace", skip(tcx))]
2406pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path, ref_path: Option<&Path>) {
2407    // Since encoding metadata is not in a query, and nothing is cached,
2408    // there's no need to do dep-graph tracking for any of it.
2409    tcx.dep_graph.assert_ignored();
2410
2411    // Generate the metadata stub manually, as that is a small file compared to full metadata.
2412    if let Some(ref_path) = ref_path {
2413        let _prof_timer = tcx.prof.verbose_generic_activity("generate_crate_metadata_stub");
2414
2415        with_encode_metadata_header(tcx, ref_path, |ecx| {
2416            let header: LazyValue<CrateHeader> = ecx.lazy(CrateHeader {
2417                name: tcx.crate_name(LOCAL_CRATE),
2418                triple: tcx.sess.opts.target_triple.clone(),
2419                hash: tcx.crate_hash(LOCAL_CRATE),
2420                is_proc_macro_crate: false,
2421                is_stub: true,
2422            });
2423            header.position.get()
2424        })
2425    }
2426
2427    let _prof_timer = tcx.prof.verbose_generic_activity("generate_crate_metadata");
2428
2429    let dep_node = tcx.metadata_dep_node();
2430
2431    // If the metadata dep-node is green, try to reuse the saved work product.
2432    if tcx.dep_graph.is_fully_enabled()
2433        && let work_product_id = WorkProductId::from_cgu_name("metadata")
2434        && let Some(work_product) = tcx.dep_graph.previous_work_product(&work_product_id)
2435        && tcx.try_mark_green(&dep_node)
2436    {
2437        let saved_path = &work_product.saved_files["rmeta"];
2438        let incr_comp_session_dir = tcx.sess.incr_comp_session_dir_opt().unwrap();
2439        let source_file = rustc_incremental::in_incr_comp_dir(&incr_comp_session_dir, saved_path);
2440        debug!("copying preexisting metadata from {source_file:?} to {path:?}");
2441        match rustc_fs_util::link_or_copy(&source_file, path) {
2442            Ok(_) => {}
2443            Err(err) => tcx.dcx().emit_fatal(FailCreateFileEncoder { err }),
2444        };
2445        return;
2446    };
2447
2448    if tcx.sess.threads() != 1 {
2449        // Prefetch some queries used by metadata encoding.
2450        // This is not necessary for correctness, but is only done for performance reasons.
2451        // It can be removed if it turns out to cause trouble or be detrimental to performance.
2452        join(
2453            || prefetch_mir(tcx),
2454            || {
2455                let _ = tcx.exported_non_generic_symbols(LOCAL_CRATE);
2456                let _ = tcx.exported_generic_symbols(LOCAL_CRATE);
2457            },
2458        );
2459    }
2460
2461    // Perform metadata encoding inside a task, so the dep-graph can check if any encoded
2462    // information changes, and maybe reuse the work product.
2463    tcx.dep_graph.with_task(
2464        dep_node,
2465        tcx,
2466        path,
2467        |tcx, path| {
2468            with_encode_metadata_header(tcx, path, |ecx| {
2469                // Encode all the entries and extra information in the crate,
2470                // culminating in the `CrateRoot` which points to all of it.
2471                let root = ecx.encode_crate_root();
2472
2473                // Flush buffer to ensure backing file has the correct size.
2474                ecx.opaque.flush();
2475                // Record metadata size for self-profiling
2476                tcx.prof.artifact_size(
2477                    "crate_metadata",
2478                    "crate_metadata",
2479                    ecx.opaque.file().metadata().unwrap().len(),
2480                );
2481
2482                root.position.get()
2483            })
2484        },
2485        None,
2486    );
2487}
2488
2489fn with_encode_metadata_header(
2490    tcx: TyCtxt<'_>,
2491    path: &Path,
2492    f: impl FnOnce(&mut EncodeContext<'_, '_>) -> usize,
2493) {
2494    let mut encoder = opaque::FileEncoder::new(path)
2495        .unwrap_or_else(|err| tcx.dcx().emit_fatal(FailCreateFileEncoder { err }));
2496    encoder.emit_raw_bytes(METADATA_HEADER);
2497
2498    // Will be filled with the root position after encoding everything.
2499    encoder.emit_raw_bytes(&0u64.to_le_bytes());
2500
2501    let source_map_files = tcx.sess.source_map().files();
2502    let source_file_cache = (Arc::clone(&source_map_files[0]), 0);
2503    let required_source_files = Some(FxIndexSet::default());
2504    drop(source_map_files);
2505
2506    let hygiene_ctxt = HygieneEncodeContext::default();
2507
2508    let mut ecx = EncodeContext {
2509        opaque: encoder,
2510        tcx,
2511        feat: tcx.features(),
2512        tables: Default::default(),
2513        lazy_state: LazyState::NoNode,
2514        span_shorthands: Default::default(),
2515        type_shorthands: Default::default(),
2516        predicate_shorthands: Default::default(),
2517        source_file_cache,
2518        interpret_allocs: Default::default(),
2519        required_source_files,
2520        is_proc_macro: tcx.crate_types().contains(&CrateType::ProcMacro),
2521        hygiene_ctxt: &hygiene_ctxt,
2522        symbol_index_table: Default::default(),
2523    };
2524
2525    // Encode the rustc version string in a predictable location.
2526    rustc_version(tcx.sess.cfg_version).encode(&mut ecx);
2527
2528    let root_position = f(&mut ecx);
2529
2530    // Make sure we report any errors from writing to the file.
2531    // If we forget this, compilation can succeed with an incomplete rmeta file,
2532    // causing an ICE when the rmeta file is read by another compilation.
2533    if let Err((path, err)) = ecx.opaque.finish() {
2534        tcx.dcx().emit_fatal(FailWriteFile { path: &path, err });
2535    }
2536
2537    let file = ecx.opaque.file();
2538    if let Err(err) = encode_root_position(file, root_position) {
2539        tcx.dcx().emit_fatal(FailWriteFile { path: ecx.opaque.path(), err });
2540    }
2541}
2542
2543fn encode_root_position(mut file: &File, pos: usize) -> Result<(), std::io::Error> {
2544    // We will return to this position after writing the root position.
2545    let pos_before_seek = file.stream_position().unwrap();
2546
2547    // Encode the root position.
2548    let header = METADATA_HEADER.len();
2549    file.seek(std::io::SeekFrom::Start(header as u64))?;
2550    file.write_all(&pos.to_le_bytes())?;
2551
2552    // Return to the position where we are before writing the root position.
2553    file.seek(std::io::SeekFrom::Start(pos_before_seek))?;
2554    Ok(())
2555}
2556
2557pub(crate) fn provide(providers: &mut Providers) {
2558    *providers = Providers {
2559        doc_link_resolutions: |tcx, def_id| {
2560            tcx.resolutions(())
2561                .doc_link_resolutions
2562                .get(&def_id)
2563                .unwrap_or_else(|| span_bug!(tcx.def_span(def_id), "no resolutions for a doc link"))
2564        },
2565        doc_link_traits_in_scope: |tcx, def_id| {
2566            tcx.resolutions(()).doc_link_traits_in_scope.get(&def_id).unwrap_or_else(|| {
2567                span_bug!(tcx.def_span(def_id), "no traits in scope for a doc link")
2568            })
2569        },
2570
2571        ..*providers
2572    }
2573}
2574
2575/// Build a textual representation of an unevaluated constant expression.
2576///
2577/// If the const expression is too complex, an underscore `_` is returned.
2578/// For const arguments, it's `{ _ }` to be precise.
2579/// This means that the output is not necessarily valid Rust code.
2580///
2581/// Currently, only
2582///
2583/// * literals (optionally with a leading `-`)
2584/// * unit `()`
2585/// * blocks (`{ … }`) around simple expressions and
2586/// * paths without arguments
2587///
2588/// are considered simple enough. Simple blocks are included since they are
2589/// necessary to disambiguate unit from the unit type.
2590/// This list might get extended in the future.
2591///
2592/// Without this censoring, in a lot of cases the output would get too large
2593/// and verbose. Consider `match` expressions, blocks and deeply nested ADTs.
2594/// Further, private and `doc(hidden)` fields of structs would get leaked
2595/// since HIR datatypes like the `body` parameter do not contain enough
2596/// semantic information for this function to be able to hide them –
2597/// at least not without significant performance overhead.
2598///
2599/// Whenever possible, prefer to evaluate the constant first and try to
2600/// use a different method for pretty-printing. Ideally this function
2601/// should only ever be used as a fallback.
2602pub fn rendered_const<'tcx>(tcx: TyCtxt<'tcx>, body: &hir::Body<'_>, def_id: LocalDefId) -> String {
2603    let value = body.value;
2604
2605    #[derive(PartialEq, Eq)]
2606    enum Classification {
2607        Literal,
2608        Simple,
2609        Complex,
2610    }
2611
2612    use Classification::*;
2613
2614    fn classify(expr: &hir::Expr<'_>) -> Classification {
2615        match &expr.kind {
2616            hir::ExprKind::Unary(hir::UnOp::Neg, expr) => {
2617                if matches!(expr.kind, hir::ExprKind::Lit(_)) { Literal } else { Complex }
2618            }
2619            hir::ExprKind::Lit(_) => Literal,
2620            hir::ExprKind::Tup([]) => Simple,
2621            hir::ExprKind::Block(hir::Block { stmts: [], expr: Some(expr), .. }, _) => {
2622                if classify(expr) == Complex { Complex } else { Simple }
2623            }
2624            // Paths with a self-type or arguments are too “complex” following our measure since
2625            // they may leak private fields of structs (with feature `adt_const_params`).
2626            // Consider: `<Self as Trait<{ Struct { private: () } }>>::CONSTANT`.
2627            // Paths without arguments are definitely harmless though.
2628            hir::ExprKind::Path(hir::QPath::Resolved(_, hir::Path { segments, .. })) => {
2629                if segments.iter().all(|segment| segment.args.is_none()) { Simple } else { Complex }
2630            }
2631            // FIXME: Claiming that those kinds of QPaths are simple is probably not true if the Ty
2632            //        contains const arguments. Is there a *concise* way to check for this?
2633            hir::ExprKind::Path(hir::QPath::TypeRelative(..)) => Simple,
2634            _ => Complex,
2635        }
2636    }
2637
2638    match classify(value) {
2639        // For non-macro literals, we avoid invoking the pretty-printer and use the source snippet
2640        // instead to preserve certain stylistic choices the user likely made for the sake of
2641        // legibility, like:
2642        //
2643        // * hexadecimal notation
2644        // * underscores
2645        // * character escapes
2646        //
2647        // FIXME: This passes through `-/*spacer*/0` verbatim.
2648        Literal
2649            if !value.span.from_expansion()
2650                && let Ok(snippet) = tcx.sess.source_map().span_to_snippet(value.span) =>
2651        {
2652            snippet
2653        }
2654
2655        // Otherwise we prefer pretty-printing to get rid of extraneous whitespace, comments and
2656        // other formatting artifacts.
2657        Literal | Simple => id_to_string(&tcx, body.id().hir_id),
2658
2659        // FIXME: Omit the curly braces if the enclosing expression is an array literal
2660        //        with a repeated element (an `ExprKind::Repeat`) as in such case it
2661        //        would not actually need any disambiguation.
2662        Complex => {
2663            if tcx.def_kind(def_id) == DefKind::AnonConst {
2664                "{ _ }".to_owned()
2665            } else {
2666                "_".to_owned()
2667            }
2668        }
2669    }
2670}