]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_metadata/src/rmeta/encoder.rs
New upstream version 1.70.0+dfsg1
[rustc.git] / compiler / rustc_metadata / src / rmeta / encoder.rs
CommitLineData
f2b60f7d 1use crate::errors::{FailCreateFileEncoder, FailSeekFile, FailWriteFile};
c295e0f8 2use crate::rmeta::def_path_hash_map::DefPathHashMapRef;
923072b8 3use crate::rmeta::table::TableBuilder;
dfeec247 4use crate::rmeta::*;
92a42be0 5
2b03887a 6use rustc_ast::Attribute;
5e7ed085 7use rustc_data_structures::fingerprint::Fingerprint;
5869c6ff 8use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
064997fb 9use rustc_data_structures::memmap::{Mmap, MmapMut};
5e7ed085 10use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
5869c6ff 11use rustc_data_structures::sync::{join, par_iter, Lrc, ParallelIterator};
064997fb 12use rustc_data_structures::temp_dir::MaybeTempDir;
ba9703b0 13use rustc_hir as hir;
5e7ed085 14use rustc_hir::def::DefKind;
6a06907d
XL
15use rustc_hir::def_id::{
16 CrateNum, DefId, DefIndex, LocalDefId, CRATE_DEF_ID, CRATE_DEF_INDEX, LOCAL_CRATE,
17};
fc512014 18use rustc_hir::definitions::DefPathData;
5099ac24 19use rustc_hir::intravisit::{self, Visitor};
487cf647 20use rustc_hir::lang_items::LangItem;
5099ac24 21use rustc_middle::hir::nested_filter;
ba9703b0
XL
22use rustc_middle::middle::dependency_format::Linkage;
23use rustc_middle::middle::exported_symbols::{
04454e1e 24 metadata_symbol_name, ExportedSymbol, SymbolExportInfo,
ba9703b0 25};
3dfed10e 26use rustc_middle::mir::interpret;
353b0b11 27use rustc_middle::query::LocalCrate;
ba9703b0 28use rustc_middle::traits::specialization_graph;
3dfed10e 29use rustc_middle::ty::codec::TyEncoder;
5e7ed085 30use rustc_middle::ty::fast_reject::{self, SimplifiedType, TreatParams};
a2a8927a 31use rustc_middle::ty::query::Providers;
ba9703b0 32use rustc_middle::ty::{self, SymbolName, Ty, TyCtxt};
2b03887a 33use rustc_middle::util::common::to_readable_str;
064997fb 34use rustc_serialize::{opaque, Decodable, Decoder, Encodable, Encoder};
2b03887a 35use rustc_session::config::{CrateType, OptLevel};
c295e0f8 36use rustc_session::cstore::{ForeignModule, LinkagePreference, NativeLib};
923072b8
FG
37use rustc_span::hygiene::{ExpnIndex, HygieneEncodeContext, MacroKind};
38use rustc_span::symbol::{sym, Symbol};
04454e1e
FG
39use rustc_span::{
40 self, DebuggerVisualizerFile, ExternalSource, FileName, SourceFile, Span, SyntaxContext,
41};
923072b8 42use std::borrow::Borrow;
f2b60f7d 43use std::collections::hash_map::Entry;
ff7c6d11 44use std::hash::Hash;
064997fb 45use std::io::{Read, Seek, Write};
e74abb32 46use std::num::NonZeroUsize;
064997fb 47use std::path::{Path, PathBuf};
9e0c209e 48
3dfed10e 49pub(super) struct EncodeContext<'a, 'tcx> {
064997fb 50 opaque: opaque::FileEncoder,
e74abb32 51 tcx: TyCtxt<'tcx>,
1b1a35ee 52 feat: &'tcx rustc_feature::Features,
dc9dc135 53
923072b8 54 tables: TableBuilders,
9e0c209e
SL
55
56 lazy_state: LazyState,
476ff2be 57 type_shorthands: FxHashMap<Ty<'tcx>, usize>,
5869c6ff 58 predicate_shorthands: FxHashMap<ty::PredicateKind<'tcx>, usize>,
2c00a5a8 59
3dfed10e 60 interpret_allocs: FxIndexSet<interpret::AllocId>,
0531ce1d 61
2c00a5a8 62 // This is used to speed up Span encoding.
f035d41b
XL
63 // The `usize` is an index into the `MonotonicVec`
64 // that stores the `SourceFile`
65 source_file_cache: (Lrc<SourceFile>, usize),
66 // The indices (into the `SourceMap`'s `MonotonicVec`)
67 // of all of the `SourceFiles` that we need to serialize.
68 // When we serialize a `Span`, we insert the index of its
f2b60f7d
FG
69 // `SourceFile` into the `FxIndexSet`.
70 // The order inside the `FxIndexSet` is used as on-disk
71 // order of `SourceFiles`, and encoded inside `Span`s.
72 required_source_files: Option<FxIndexSet<usize>>,
f035d41b 73 is_proc_macro: bool,
3dfed10e 74 hygiene_ctxt: &'a HygieneEncodeContext,
f2b60f7d 75 symbol_table: FxHashMap<Symbol, usize>,
9e0c209e
SL
76}
77
9ffffee4 78/// If the current crate is a proc-macro, returns early with `LazyArray::default()`.
1b1a35ee
XL
79/// This is useful for skipping the encoding of things that aren't needed
80/// for proc-macro crates.
81macro_rules! empty_proc_macro {
82 ($self:ident) => {
83 if $self.is_proc_macro {
9ffffee4 84 return LazyArray::default();
1b1a35ee
XL
85 }
86 };
87}
88
9e0c209e
SL
89macro_rules! encoder_methods {
90 ($($name:ident($ty:ty);)*) => {
923072b8 91 $(fn $name(&mut self, value: $ty) {
9e0c209e
SL
92 self.opaque.$name(value)
93 })*
b039eaaf
SL
94 }
95}
96
3dfed10e 97impl<'a, 'tcx> Encoder for EncodeContext<'a, 'tcx> {
9e0c209e
SL
98 encoder_methods! {
99 emit_usize(usize);
32a655c1 100 emit_u128(u128);
9e0c209e
SL
101 emit_u64(u64);
102 emit_u32(u32);
103 emit_u16(u16);
104 emit_u8(u8);
105
106 emit_isize(isize);
32a655c1 107 emit_i128(i128);
9e0c209e
SL
108 emit_i64(i64);
109 emit_i32(i32);
110 emit_i16(i16);
111 emit_i8(i8);
112
113 emit_bool(bool);
9e0c209e
SL
114 emit_char(char);
115 emit_str(&str);
cdc7bbd5 116 emit_raw_bytes(&[u8]);
b039eaaf
SL
117 }
118}
119
923072b8
FG
120impl<'a, 'tcx, T> Encodable<EncodeContext<'a, 'tcx>> for LazyValue<T> {
121 fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) {
122 e.emit_lazy_distance(self.position);
9e0c209e 123 }
223e47cc
LB
124}
125
923072b8
FG
126impl<'a, 'tcx, T> Encodable<EncodeContext<'a, 'tcx>> for LazyArray<T> {
127 fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) {
128 e.emit_usize(self.num_elems);
129 if self.num_elems > 0 {
130 e.emit_lazy_distance(self.position)
9e0c209e 131 }
9e0c209e 132 }
223e47cc
LB
133}
134
923072b8
FG
135impl<'a, 'tcx, I, T> Encodable<EncodeContext<'a, 'tcx>> for LazyTable<I, T> {
136 fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) {
137 e.emit_usize(self.encoded_size);
138 e.emit_lazy_distance(self.position);
e74abb32
XL
139 }
140}
141
1b1a35ee 142impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for CrateNum {
923072b8 143 fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) {
1b1a35ee 144 if *self != LOCAL_CRATE && s.is_proc_macro {
9c376795 145 panic!("Attempted to encode non-local CrateNum {self:?} for proc-macro crate");
1b1a35ee 146 }
923072b8 147 s.emit_u32(self.as_u32());
1b1a35ee
XL
148 }
149}
150
3dfed10e 151impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for DefIndex {
923072b8
FG
152 fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) {
153 s.emit_u32(self.as_u32());
abe05a73
XL
154 }
155}
156
136023e0 157impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for ExpnIndex {
923072b8
FG
158 fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) {
159 s.emit_u32(self.as_u32());
136023e0
XL
160 }
161}
162
3dfed10e 163impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for SyntaxContext {
923072b8
FG
164 fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) {
165 rustc_span::hygiene::raw_encode_syntax_context(*self, &s.hygiene_ctxt, s);
abe05a73
XL
166 }
167}
168
3dfed10e 169impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for ExpnId {
923072b8 170 fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) {
136023e0 171 if self.krate == LOCAL_CRATE {
9c376795 172 // We will only write details for local expansions. Non-local expansions will fetch
136023e0
XL
173 // data from the corresponding crate's metadata.
174 // FIXME(#43047) FIXME(#74731) We may eventually want to avoid relying on external
175 // metadata from proc-macro crates.
176 s.hygiene_ctxt.schedule_expn_data_for_encoding(*self);
177 }
923072b8
FG
178 self.krate.encode(s);
179 self.local_id.encode(s);
2c00a5a8
XL
180 }
181}
182
3dfed10e 183impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for Span {
923072b8 184 fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) {
cdc7bbd5
XL
185 let span = self.data();
186
187 // Don't serialize any `SyntaxContext`s from a proc-macro crate,
188 // since we don't load proc-macro dependencies during serialization.
189 // This means that any hygiene information from macros used *within*
190 // a proc-macro crate (e.g. invoking a macro that expands to a proc-macro
191 // definition) will be lost.
192 //
193 // This can show up in two ways:
194 //
195 // 1. Any hygiene information associated with identifier of
196 // a proc macro (e.g. `#[proc_macro] pub fn $name`) will be lost.
197 // Since proc-macros can only be invoked from a different crate,
198 // real code should never need to care about this.
199 //
200 // 2. Using `Span::def_site` or `Span::mixed_site` will not
201 // include any hygiene information associated with the definition
202 // site. This means that a proc-macro cannot emit a `$crate`
203 // identifier which resolves to one of its dependencies,
204 // which also should never come up in practice.
205 //
206 // Additionally, this affects `Span::parent`, and any other
207 // span inspection APIs that would otherwise allow traversing
208 // the `SyntaxContexts` associated with a span.
209 //
210 // None of these user-visible effects should result in any
211 // cross-crate inconsistencies (getting one behavior in the same
212 // crate, and a different behavior in another crate) due to the
213 // limited surface that proc-macros can expose.
214 //
215 // IMPORTANT: If this is ever changed, be sure to update
216 // `rustc_span::hygiene::raw_encode_expn_id` to handle
217 // encoding `ExpnData` for proc-macro crates.
218 if s.is_proc_macro {
923072b8 219 SyntaxContext::root().encode(s);
cdc7bbd5 220 } else {
923072b8 221 span.ctxt.encode(s);
2c00a5a8
XL
222 }
223
cdc7bbd5
XL
224 if self.is_dummy() {
225 return TAG_PARTIAL_SPAN.encode(s);
226 }
2c00a5a8
XL
227
228 // The Span infrastructure should make sure that this invariant holds:
229 debug_assert!(span.lo <= span.hi);
230
3dfed10e
XL
231 if !s.source_file_cache.0.contains(span.lo) {
232 let source_map = s.tcx.sess.source_map();
b7449926 233 let source_file_index = source_map.lookup_source_file_idx(span.lo);
3dfed10e 234 s.source_file_cache =
f035d41b 235 (source_map.files()[source_file_index].clone(), source_file_index);
2c00a5a8 236 }
f2b60f7d
FG
237 let (ref source_file, source_file_index) = s.source_file_cache;
238 debug_assert!(source_file.contains(span.lo));
2c00a5a8 239
f2b60f7d 240 if !source_file.contains(span.hi) {
2c00a5a8
XL
241 // Unfortunately, macro expansion still sometimes generates Spans
242 // that malformed in this way.
cdc7bbd5 243 return TAG_PARTIAL_SPAN.encode(s);
2c00a5a8
XL
244 }
245
ba9703b0
XL
246 // There are two possible cases here:
247 // 1. This span comes from a 'foreign' crate - e.g. some crate upstream of the
248 // crate we are writing metadata for. When the metadata for *this* crate gets
249 // deserialized, the deserializer will need to know which crate it originally came
250 // from. We use `TAG_VALID_SPAN_FOREIGN` to indicate that a `CrateNum` should
251 // be deserialized after the rest of the span data, which tells the deserializer
252 // which crate contains the source map information.
5e7ed085 253 // 2. This span comes from our own crate. No special handling is needed - we just
ba9703b0
XL
254 // write `TAG_VALID_SPAN_LOCAL` to let the deserializer know that it should use
255 // our own source map information.
f035d41b
XL
256 //
257 // If we're a proc-macro crate, we always treat this as a local `Span`.
258 // In `encode_source_map`, we serialize foreign `SourceFile`s into our metadata
259 // if we're a proc-macro crate.
260 // This allows us to avoid loading the dependencies of proc-macro crates: all of
261 // the information we need to decode `Span`s is stored in the proc-macro crate.
f2b60f7d 262 let (tag, metadata_index) = if source_file.is_imported() && !s.is_proc_macro {
2b03887a
FG
263 // To simplify deserialization, we 'rebase' this span onto the crate it originally came
264 // from (the crate that 'owns' the file it references. These rebased 'lo' and 'hi'
265 // values are relative to the source map information for the 'foreign' crate whose
266 // CrateNum we write into the metadata. This allows `imported_source_files` to binary
ba9703b0
XL
267 // search through the 'foreign' crate's source map information, using the
268 // deserialized 'lo' and 'hi' values directly.
269 //
270 // All of this logic ensures that the final result of deserialization is a 'normal'
271 // Span that can be used without any additional trouble.
f2b60f7d 272 let metadata_index = {
ba9703b0 273 // Introduce a new scope so that we drop the 'lock()' temporary
f2b60f7d
FG
274 match &*source_file.external_src.lock() {
275 ExternalSource::Foreign { metadata_index, .. } => *metadata_index,
9c376795 276 src => panic!("Unexpected external source {src:?}"),
ba9703b0
XL
277 }
278 };
e74abb32 279
f2b60f7d 280 (TAG_VALID_SPAN_FOREIGN, metadata_index)
ba9703b0 281 } else {
f2b60f7d
FG
282 // Record the fact that we need to encode the data for this `SourceFile`
283 let source_files =
284 s.required_source_files.as_mut().expect("Already encoded SourceMap!");
285 let (metadata_index, _) = source_files.insert_full(source_file_index);
286 let metadata_index: u32 =
287 metadata_index.try_into().expect("cannot export more than U32_MAX files");
288
289 (TAG_VALID_SPAN_LOCAL, metadata_index)
ba9703b0
XL
290 };
291
f2b60f7d
FG
292 // Encode the start position relative to the file start, so we profit more from the
293 // variable-length integer encoding.
294 let lo = span.lo - source_file.start_pos;
2c00a5a8
XL
295
296 // Encode length which is usually less than span.hi and profits more
297 // from the variable-length integer encoding that we use.
f2b60f7d
FG
298 let len = span.hi - span.lo;
299
300 tag.encode(s);
301 lo.encode(s);
923072b8 302 len.encode(s);
3dfed10e 303
f2b60f7d
FG
304 // Encode the index of the `SourceFile` for the span, in order to make decoding faster.
305 metadata_index.encode(s);
306
ba9703b0 307 if tag == TAG_VALID_SPAN_FOREIGN {
3dfed10e
XL
308 // This needs to be two lines to avoid holding the `s.source_file_cache`
309 // while calling `cnum.encode(s)`
310 let cnum = s.source_file_cache.0.cnum;
923072b8 311 cnum.encode(s);
ba9703b0 312 }
abe05a73
XL
313 }
314}
315
f2b60f7d
FG
316impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for Symbol {
317 fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) {
318 // if symbol preinterned, emit tag and symbol index
319 if self.is_preinterned() {
320 s.opaque.emit_u8(SYMBOL_PREINTERNED);
321 s.opaque.emit_u32(self.as_u32());
322 } else {
323 // otherwise write it as string or as offset to it
324 match s.symbol_table.entry(*self) {
325 Entry::Vacant(o) => {
326 s.opaque.emit_u8(SYMBOL_STR);
327 let pos = s.opaque.position();
328 o.insert(pos);
329 s.emit_str(self.as_str());
330 }
331 Entry::Occupied(o) => {
9c376795 332 let x = *o.get();
f2b60f7d
FG
333 s.emit_u8(SYMBOL_OFFSET);
334 s.emit_usize(x);
335 }
336 }
337 }
338 }
339}
340
923072b8 341impl<'a, 'tcx> TyEncoder for EncodeContext<'a, 'tcx> {
3dfed10e 342 const CLEAR_CROSS_CRATE: bool = true;
d9579d0f 343
923072b8
FG
344 type I = TyCtxt<'tcx>;
345
3dfed10e
XL
346 fn position(&self) -> usize {
347 self.opaque.position()
f035d41b 348 }
f035d41b 349
3dfed10e
XL
350 fn type_shorthands(&mut self) -> &mut FxHashMap<Ty<'tcx>, usize> {
351 &mut self.type_shorthands
0531ce1d 352 }
0531ce1d 353
5869c6ff 354 fn predicate_shorthands(&mut self) -> &mut FxHashMap<ty::PredicateKind<'tcx>, usize> {
3dfed10e 355 &mut self.predicate_shorthands
2c00a5a8 356 }
2c00a5a8 357
923072b8 358 fn encode_alloc_id(&mut self, alloc_id: &rustc_middle::mir::interpret::AllocId) {
3dfed10e
XL
359 let (index, _) = self.interpret_allocs.insert_full(*alloc_id);
360
923072b8 361 index.encode(self);
ff7c6d11
XL
362 }
363}
364
9ffffee4 365// Shorthand for `$self.$tables.$table.set_some($def_id.index, $self.lazy_value($value))`, which would
e74abb32
XL
366// normally need extra variables to avoid errors about multiple mutable borrows.
367macro_rules! record {
60c5eb7d 368 ($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => {{
e74abb32
XL
369 {
370 let value = $value;
371 let lazy = $self.lazy(value);
9ffffee4 372 $self.$tables.$table.set_some($def_id.index, lazy);
e74abb32 373 }
dfeec247 374 }};
e74abb32
XL
375}
376
9ffffee4 377// Shorthand for `$self.$tables.$table.set_some($def_id.index, $self.lazy_value($value))`, which would
923072b8
FG
378// normally need extra variables to avoid errors about multiple mutable borrows.
379macro_rules! record_array {
9ffffee4
FG
380 ($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => {{
381 {
382 let value = $value;
383 let lazy = $self.lazy_array(value);
384 $self.$tables.$table.set_some($def_id.index, lazy);
385 }
386 }};
387}
388
389macro_rules! record_defaulted_array {
923072b8
FG
390 ($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => {{
391 {
392 let value = $value;
393 let lazy = $self.lazy_array(value);
394 $self.$tables.$table.set($def_id.index, lazy);
395 }
396 }};
397}
398
3dfed10e 399impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
923072b8
FG
400 fn emit_lazy_distance(&mut self, position: NonZeroUsize) {
401 let pos = position.get();
9e0c209e 402 let distance = match self.lazy_state {
c30ab7b3 403 LazyState::NoNode => bug!("emit_lazy_distance: outside of a metadata node"),
9e0c209e 404 LazyState::NodeStart(start) => {
e74abb32 405 let start = start.get();
5099ac24
FG
406 assert!(pos <= start);
407 start - pos
9e0c209e 408 }
5099ac24 409 LazyState::Previous(last_pos) => {
0531ce1d 410 assert!(
923072b8 411 last_pos <= position,
0531ce1d 412 "make sure that the calls to `lazy*` \
74b04a01 413 are in the same order as the metadata fields",
0531ce1d 414 );
923072b8 415 position.get() - last_pos.get()
9e0c209e
SL
416 }
417 };
5099ac24 418 self.lazy_state = LazyState::Previous(NonZeroUsize::new(pos).unwrap());
923072b8 419 self.emit_usize(distance);
9e0c209e 420 }
1a4d82fc 421
923072b8
FG
422 fn lazy<T: ParameterizedOverTcx, B: Borrow<T::Value<'tcx>>>(&mut self, value: B) -> LazyValue<T>
423 where
424 T::Value<'tcx>: Encodable<EncodeContext<'a, 'tcx>>,
425 {
426 let pos = NonZeroUsize::new(self.position()).unwrap();
427
428 assert_eq!(self.lazy_state, LazyState::NoNode);
429 self.lazy_state = LazyState::NodeStart(pos);
430 value.borrow().encode(self);
431 self.lazy_state = LazyState::NoNode;
432
433 assert!(pos.get() <= self.position());
434
435 LazyValue::from_position(pos)
436 }
437
438 fn lazy_array<T: ParameterizedOverTcx, I: IntoIterator<Item = B>, B: Borrow<T::Value<'tcx>>>(
3dfed10e 439 &mut self,
923072b8
FG
440 values: I,
441 ) -> LazyArray<T>
442 where
443 T::Value<'tcx>: Encodable<EncodeContext<'a, 'tcx>>,
444 {
e74abb32 445 let pos = NonZeroUsize::new(self.position()).unwrap();
223e47cc 446
e1599b0c
XL
447 assert_eq!(self.lazy_state, LazyState::NoNode);
448 self.lazy_state = LazyState::NodeStart(pos);
923072b8 449 let len = values.into_iter().map(|value| value.borrow().encode(self)).count();
e1599b0c 450 self.lazy_state = LazyState::NoNode;
1a4d82fc 451
5099ac24 452 assert!(pos.get() <= self.position());
1a4d82fc 453
923072b8 454 LazyArray::from_position_and_num_elems(pos, len)
1a4d82fc 455 }
223e47cc 456
dc9dc135 457 fn encode_info_for_items(&mut self) {
353b0b11 458 self.encode_info_for_mod(CRATE_DEF_ID);
1b1a35ee
XL
459
460 // Proc-macro crates only export proc-macro items, which are looked
461 // up using `proc_macro_data`
462 if self.is_proc_macro {
463 return;
464 }
465
064997fb 466 self.tcx.hir().visit_all_item_likes_in_crate(self);
7cac9316
XL
467 }
468
3dfed10e 469 fn encode_def_path_table(&mut self) {
064997fb 470 let table = self.tcx.def_path_table();
1b1a35ee
XL
471 if self.is_proc_macro {
472 for def_index in std::iter::once(CRATE_DEF_INDEX)
94222f64 473 .chain(self.tcx.resolutions(()).proc_macros.iter().map(|p| p.local_def_index))
1b1a35ee
XL
474 {
475 let def_key = self.lazy(table.def_key(def_index));
04454e1e 476 let def_path_hash = table.def_path_hash(def_index);
9ffffee4 477 self.tables.def_keys.set_some(def_index, def_key);
1b1a35ee
XL
478 self.tables.def_path_hashes.set(def_index, def_path_hash);
479 }
480 } else {
481 for (def_index, def_key, def_path_hash) in table.enumerated_keys_and_path_hashes() {
482 let def_key = self.lazy(def_key);
9ffffee4 483 self.tables.def_keys.set_some(def_index, def_key);
04454e1e 484 self.tables.def_path_hashes.set(def_index, *def_path_hash);
1b1a35ee 485 }
3dfed10e 486 }
7cac9316
XL
487 }
488
923072b8 489 fn encode_def_path_hash_map(&mut self) -> LazyValue<DefPathHashMapRef<'static>> {
064997fb 490 self.lazy(DefPathHashMapRef::BorrowedFromTcx(self.tcx.def_path_hash_to_def_index_map()))
c295e0f8
XL
491 }
492
9ffffee4 493 fn encode_source_map(&mut self) -> LazyTable<u32, Option<LazyValue<rustc_span::SourceFile>>> {
b7449926
XL
494 let source_map = self.tcx.sess.source_map();
495 let all_source_files = source_map.files();
7cac9316 496
f035d41b
XL
497 // By replacing the `Option` with `None`, we ensure that we can't
498 // accidentally serialize any more `Span`s after the source map encoding
499 // is done.
500 let required_source_files = self.required_source_files.take().unwrap();
7cac9316 501
923072b8
FG
502 let working_directory = &self.tcx.sess.opts.working_dir;
503
f2b60f7d
FG
504 let mut adapted = TableBuilder::default();
505
506 // Only serialize `SourceFile`s that were used during the encoding of a `Span`.
507 //
508 // The order in which we encode source files is important here: the on-disk format for
509 // `Span` contains the index of the corresponding `SourceFile`.
510 for (on_disk_index, &source_file_index) in required_source_files.iter().enumerate() {
511 let source_file = &all_source_files[source_file_index];
512 // Don't serialize imported `SourceFile`s, unless we're in a proc-macro crate.
513 assert!(!source_file.is_imported() || self.is_proc_macro);
514
515 // At export time we expand all source file paths to absolute paths because
516 // downstream compilation sessions can have a different compiler working
517 // directory, so relative paths from this or any other upstream crate
518 // won't be valid anymore.
519 //
520 // At this point we also erase the actual on-disk path and only keep
521 // the remapped version -- as is necessary for reproducible builds.
522 let mut source_file = match source_file.name {
523 FileName::Real(ref original_file_name) => {
524 let adapted_file_name = source_map
525 .path_mapping()
526 .to_embeddable_absolute_path(original_file_name.clone(), working_directory);
527
528 if adapted_file_name != *original_file_name {
529 let mut adapted: SourceFile = (**source_file).clone();
530 adapted.name = FileName::Real(adapted_file_name);
531 adapted.name_hash = {
532 let mut hasher: StableHasher = StableHasher::new();
533 adapted.name.hash(&mut hasher);
534 hasher.finish::<u128>()
535 };
536 Lrc::new(adapted)
537 } else {
538 // Nothing to adapt
539 source_file.clone()
dfeec247 540 }
7cac9316 541 }
f2b60f7d
FG
542 // expanded code, not from a file
543 _ => source_file.clone(),
544 };
545
546 // We're serializing this `SourceFile` into our crate metadata,
547 // so mark it as coming from this crate.
548 // This also ensures that we don't try to deserialize the
549 // `CrateNum` for a proc-macro dependency - since proc macro
550 // dependencies aren't loaded when we deserialize a proc-macro,
551 // trying to remap the `CrateNum` would fail.
552 if self.is_proc_macro {
553 Lrc::make_mut(&mut source_file).cnum = LOCAL_CRATE;
554 }
555
556 let on_disk_index: u32 =
557 on_disk_index.try_into().expect("cannot export more than U32_MAX files");
9ffffee4 558 adapted.set_some(on_disk_index, self.lazy(source_file));
f2b60f7d 559 }
7cac9316 560
f2b60f7d 561 adapted.encode(&mut self.opaque)
7cac9316
XL
562 }
563
923072b8 564 fn encode_crate_root(&mut self) -> LazyValue<CrateRoot> {
5e7ed085 565 let tcx = self.tcx;
2b03887a
FG
566 let mut stats: Vec<(&'static str, usize)> = Vec::with_capacity(32);
567
568 macro_rules! stat {
569 ($label:literal, $f:expr) => {{
570 let orig_pos = self.position();
571 let res = $f();
572 stats.push(($label, self.position() - orig_pos));
573 res
574 }};
575 }
576
577 // We have already encoded some things. Get their combined size from the current position.
578 stats.push(("preamble", self.position()));
579
580 let (crate_deps, dylib_dependency_formats) =
581 stat!("dep", || (self.encode_crate_deps(), self.encode_dylib_dependency_formats()));
582
583 let lib_features = stat!("lib-features", || self.encode_lib_features());
584
585 let stability_implications =
586 stat!("stability-implications", || self.encode_stability_implications());
587
588 let (lang_items, lang_items_missing) = stat!("lang-items", || {
589 (self.encode_lang_items(), self.encode_lang_items_missing())
590 });
591
592 let diagnostic_items = stat!("diagnostic-items", || self.encode_diagnostic_items());
593
594 let native_libraries = stat!("native-libs", || self.encode_native_libraries());
595
596 let foreign_modules = stat!("foreign-modules", || self.encode_foreign_modules());
597
598 _ = stat!("def-path-table", || self.encode_def_path_table());
7cac9316 599
a2a8927a 600 // Encode the def IDs of traits, for rustdoc and diagnostics.
2b03887a 601 let traits = stat!("traits", || self.encode_traits());
a2a8927a 602
7cac9316 603 // Encode the def IDs of impls, for coherence checking.
2b03887a
FG
604 let impls = stat!("impls", || self.encode_impls());
605
606 let incoherent_impls = stat!("incoherent-impls", || self.encode_incoherent_impls());
607
608 _ = stat!("mir", || self.encode_mir());
609
353b0b11
FG
610 _ = stat!("def-ids", || self.encode_def_ids());
611
612 _ = stat!("items", || self.encode_info_for_items());
2b03887a
FG
613
614 let interpret_alloc_index = stat!("interpret-alloc-index", || {
0531ce1d
XL
615 let mut interpret_alloc_index = Vec::new();
616 let mut n = 0;
617 trace!("beginning to encode alloc ids");
618 loop {
3dfed10e 619 let new_n = self.interpret_allocs.len();
0531ce1d
XL
620 // if we have found new ids, serialize those, too
621 if n == new_n {
622 // otherwise, abort
623 break;
624 }
625 trace!("encoding {} further alloc ids", new_n - n);
626 for idx in n..new_n {
3dfed10e 627 let id = self.interpret_allocs[idx];
0531ce1d
XL
628 let pos = self.position() as u32;
629 interpret_alloc_index.push(pos);
923072b8 630 interpret::specialized_encode_alloc_id(self, tcx, id);
0531ce1d
XL
631 }
632 n = new_n;
633 }
923072b8 634 self.lazy_array(interpret_alloc_index)
2b03887a 635 });
0531ce1d 636
2b03887a
FG
637 // Encode the proc macro data. This affects `tables`, so we need to do this before we
638 // encode the tables. This overwrites def_keys, so it must happen after
639 // encode_def_path_table.
640 let proc_macro_data = stat!("proc-macro-data", || self.encode_proc_macros());
e1599b0c 641
2b03887a 642 let tables = stat!("tables", || self.tables.encode(&mut self.opaque));
1b1a35ee 643
2b03887a
FG
644 let debugger_visualizers =
645 stat!("debugger-visualizers", || self.encode_debugger_visualizers());
04454e1e 646
ba9703b0 647 // Encode exported symbols info. This is prefetched in `encode_metadata` so we encode
3dfed10e 648 // this as late as possible to give the prefetching as much time as possible to complete.
2b03887a
FG
649 let exported_symbols = stat!("exported-symbols", || {
650 self.encode_exported_symbols(&tcx.exported_symbols(LOCAL_CRATE))
651 });
652
653 // Encode the hygiene data.
654 // IMPORTANT: this *must* be the last thing that we encode (other than `SourceMap`). The
655 // process of encoding other items (e.g. `optimized_mir`) may cause us to load data from
656 // the incremental cache. If this causes us to deserialize a `Span`, then we may load
657 // additional `SyntaxContext`s into the global `HygieneData`. Therefore, we need to encode
658 // the hygiene data last to ensure that we encode any `SyntaxContext`s that might be used.
659 let (syntax_contexts, expn_data, expn_hashes) = stat!("hygiene", || self.encode_hygiene());
660
661 let def_path_hash_map = stat!("def-path-hash-map", || self.encode_def_path_hash_map());
662
663 // Encode source_map. This needs to be done last, because encoding `Span`s tells us which
664 // `SourceFiles` we actually need to encode.
665 let source_map = stat!("source-map", || self.encode_source_map());
666
667 let root = stat!("final", || {
668 let attrs = tcx.hir().krate_attrs();
669 self.lazy(CrateRoot {
670 name: tcx.crate_name(LOCAL_CRATE),
671 extra_filename: tcx.sess.opts.cg.extra_filename.clone(),
672 triple: tcx.sess.opts.target_triple.clone(),
673 hash: tcx.crate_hash(LOCAL_CRATE),
674 stable_crate_id: tcx.def_path_hash(LOCAL_CRATE.as_def_id()).stable_crate_id(),
675 required_panic_strategy: tcx.required_panic_strategy(LOCAL_CRATE),
676 panic_in_drop_strategy: tcx.sess.opts.unstable_opts.panic_in_drop,
677 edition: tcx.sess.edition(),
678 has_global_allocator: tcx.has_global_allocator(LOCAL_CRATE),
487cf647 679 has_alloc_error_handler: tcx.has_alloc_error_handler(LOCAL_CRATE),
2b03887a 680 has_panic_handler: tcx.has_panic_handler(LOCAL_CRATE),
353b0b11 681 has_default_lib_allocator: attr::contains_name(&attrs, sym::default_lib_allocator),
2b03887a
FG
682 proc_macro_data,
683 debugger_visualizers,
353b0b11
FG
684 compiler_builtins: attr::contains_name(&attrs, sym::compiler_builtins),
685 needs_allocator: attr::contains_name(&attrs, sym::needs_allocator),
686 needs_panic_runtime: attr::contains_name(&attrs, sym::needs_panic_runtime),
687 no_builtins: attr::contains_name(&attrs, sym::no_builtins),
688 panic_runtime: attr::contains_name(&attrs, sym::panic_runtime),
689 profiler_runtime: attr::contains_name(&attrs, sym::profiler_runtime),
2b03887a
FG
690 symbol_mangling_version: tcx.sess.opts.get_symbol_mangling_version(),
691
692 crate_deps,
693 dylib_dependency_formats,
694 lib_features,
695 stability_implications,
696 lang_items,
697 diagnostic_items,
698 lang_items_missing,
699 native_libraries,
700 foreign_modules,
701 source_map,
702 traits,
703 impls,
704 incoherent_impls,
705 exported_symbols,
706 interpret_alloc_index,
707 tables,
708 syntax_contexts,
709 expn_data,
710 expn_hashes,
711 def_path_hash_map,
712 })
7cac9316
XL
713 });
714
715 let total_bytes = self.position();
716
2b03887a 717 let computed_total_bytes: usize = stats.iter().map(|(_, size)| size).sum();
923072b8
FG
718 assert_eq!(total_bytes, computed_total_bytes);
719
9c376795 720 if tcx.sess.opts.unstable_opts.meta_stats {
064997fb
FG
721 self.opaque.flush();
722
723 // Rewind and re-read all the metadata to count the zero bytes we wrote.
724 let pos_before_rewind = self.opaque.file().stream_position().unwrap();
7cac9316 725 let mut zero_bytes = 0;
064997fb
FG
726 self.opaque.file().rewind().unwrap();
727 let file = std::io::BufReader::new(self.opaque.file());
728 for e in file.bytes() {
729 if e.unwrap() == 0 {
7cac9316
XL
730 zero_bytes += 1;
731 }
732 }
064997fb 733 assert_eq!(self.opaque.file().stream_position().unwrap(), pos_before_rewind);
7cac9316 734
2b03887a
FG
735 stats.sort_by_key(|&(_, usize)| usize);
736
737 let prefix = "meta-stats";
923072b8 738 let perc = |bytes| (bytes * 100) as f64 / total_bytes as f64;
923072b8 739
9c376795 740 eprintln!("{prefix} METADATA STATS");
2b03887a 741 eprintln!("{} {:<23}{:>10}", prefix, "Section", "Size");
9c376795 742 eprintln!("{prefix} ----------------------------------------------------------------");
2b03887a
FG
743 for (label, size) in stats {
744 eprintln!(
745 "{} {:<23}{:>10} ({:4.1}%)",
746 prefix,
747 label,
748 to_readable_str(size),
749 perc(size)
750 );
751 }
9c376795 752 eprintln!("{prefix} ----------------------------------------------------------------");
923072b8 753 eprintln!(
2b03887a
FG
754 "{} {:<23}{:>10} (of which {:.1}% are zero bytes)",
755 prefix,
756 "Total",
757 to_readable_str(total_bytes),
923072b8
FG
758 perc(zero_bytes)
759 );
9c376795 760 eprintln!("{prefix}");
7cac9316
XL
761 }
762
763 root
764 }
cc61c64b 765}
1a4d82fc 766
9ffffee4
FG
767struct AnalyzeAttrState {
768 is_exported: bool,
769 is_doc_hidden: bool,
770}
771
2b03887a
FG
772/// Returns whether an attribute needs to be recorded in metadata, that is, if it's usable and
773/// useful in downstream crates. Local-only attributes are an obvious example, but some
774/// rustdoc-specific attributes can equally be of use while documenting the current crate only.
775///
776/// Removing these superfluous attributes speeds up compilation by making the metadata smaller.
777///
9ffffee4 778/// Note: the `is_exported` parameter is used to cache whether the given `DefId` has a public
2b03887a
FG
779/// visibility: this is a piece of data that can be computed once per defid, and not once per
780/// attribute. Some attributes would only be usable downstream if they are public.
781#[inline]
9ffffee4
FG
782fn analyze_attr(attr: &Attribute, state: &mut AnalyzeAttrState) -> bool {
783 let mut should_encode = false;
2b03887a
FG
784 if rustc_feature::is_builtin_only_local(attr.name_or_empty()) {
785 // Attributes marked local-only don't need to be encoded for downstream crates.
2b03887a 786 } else if attr.doc_str().is_some() {
9ffffee4
FG
787 // We keep all doc comments reachable to rustdoc because they might be "imported" into
788 // downstream crates if they use `#[doc(inline)]` to copy an item's documentation into
789 // their own.
790 if state.is_exported {
791 should_encode = true;
792 }
2b03887a 793 } else if attr.has_name(sym::doc) {
9ffffee4
FG
794 // If this is a `doc` attribute that doesn't have anything except maybe `inline` (as in
795 // `#[doc(inline)]`), then we can remove it. It won't be inlinable in downstream crates.
796 if let Some(item_list) = attr.meta_item_list() {
797 for item in item_list {
798 if !item.has_name(sym::inline) {
799 should_encode = true;
800 if item.has_name(sym::hidden) {
801 state.is_doc_hidden = true;
802 break;
803 }
804 }
805 }
806 }
2b03887a 807 } else {
9ffffee4 808 should_encode = true;
2b03887a 809 }
9ffffee4 810 should_encode
2b03887a
FG
811}
812
353b0b11 813fn should_encode_span(def_kind: DefKind) -> bool {
5869c6ff
XL
814 match def_kind {
815 DefKind::Mod
816 | DefKind::Struct
817 | DefKind::Union
818 | DefKind::Enum
819 | DefKind::Variant
820 | DefKind::Trait
821 | DefKind::TyAlias
822 | DefKind::ForeignTy
823 | DefKind::TraitAlias
824 | DefKind::AssocTy
353b0b11
FG
825 | DefKind::TyParam
826 | DefKind::ConstParam
827 | DefKind::LifetimeParam
5869c6ff
XL
828 | DefKind::Fn
829 | DefKind::Const
353b0b11 830 | DefKind::Static(_)
5869c6ff
XL
831 | DefKind::Ctor(..)
832 | DefKind::AssocFn
833 | DefKind::AssocConst
353b0b11
FG
834 | DefKind::Macro(_)
835 | DefKind::AnonConst
836 | DefKind::InlineConst
837 | DefKind::OpaqueTy
838 | DefKind::Field
839 | DefKind::Impl { .. }
840 | DefKind::Closure
841 | DefKind::Generator => true,
842 DefKind::ExternCrate
5869c6ff
XL
843 | DefKind::Use
844 | DefKind::ForeignMod
353b0b11
FG
845 | DefKind::ImplTraitPlaceholder
846 | DefKind::GlobalAsm => false,
847 }
848}
849
850fn should_encode_attrs(def_kind: DefKind) -> bool {
851 match def_kind {
852 DefKind::Mod
853 | DefKind::Struct
854 | DefKind::Union
855 | DefKind::Enum
856 | DefKind::Variant
857 | DefKind::Trait
858 | DefKind::TyAlias
859 | DefKind::ForeignTy
860 | DefKind::TraitAlias
861 | DefKind::AssocTy
862 | DefKind::Fn
863 | DefKind::Const
864 | DefKind::Static(_)
865 | DefKind::AssocFn
866 | DefKind::AssocConst
867 | DefKind::Macro(_)
868 | DefKind::Field
869 | DefKind::Impl { .. } => true,
870 DefKind::TyParam
871 | DefKind::ConstParam
872 | DefKind::Ctor(..)
873 | DefKind::ExternCrate
874 | DefKind::Use
875 | DefKind::ForeignMod
876 | DefKind::AnonConst
877 | DefKind::InlineConst
5869c6ff 878 | DefKind::OpaqueTy
f2b60f7d 879 | DefKind::ImplTraitPlaceholder
353b0b11
FG
880 | DefKind::LifetimeParam
881 | DefKind::GlobalAsm
882 | DefKind::Closure
883 | DefKind::Generator => false,
884 }
885}
886
887fn should_encode_expn_that_defined(def_kind: DefKind) -> bool {
888 match def_kind {
889 DefKind::Mod
890 | DefKind::Struct
891 | DefKind::Union
892 | DefKind::Enum
893 | DefKind::Variant
894 | DefKind::Trait
895 | DefKind::Impl { .. } => true,
896 DefKind::TyAlias
897 | DefKind::ForeignTy
898 | DefKind::TraitAlias
899 | DefKind::AssocTy
900 | DefKind::TyParam
901 | DefKind::Fn
902 | DefKind::Const
903 | DefKind::ConstParam
904 | DefKind::Static(_)
905 | DefKind::Ctor(..)
906 | DefKind::AssocFn
907 | DefKind::AssocConst
908 | DefKind::Macro(_)
909 | DefKind::ExternCrate
910 | DefKind::Use
911 | DefKind::ForeignMod
912 | DefKind::AnonConst
913 | DefKind::InlineConst
914 | DefKind::OpaqueTy
915 | DefKind::ImplTraitPlaceholder
916 | DefKind::Field
917 | DefKind::LifetimeParam
918 | DefKind::GlobalAsm
919 | DefKind::Closure
920 | DefKind::Generator => false,
921 }
922}
923
924fn should_encode_visibility(def_kind: DefKind) -> bool {
925 match def_kind {
926 DefKind::Mod
927 | DefKind::Struct
928 | DefKind::Union
929 | DefKind::Enum
930 | DefKind::Variant
931 | DefKind::Trait
932 | DefKind::TyAlias
933 | DefKind::ForeignTy
934 | DefKind::TraitAlias
935 | DefKind::AssocTy
936 | DefKind::Fn
937 | DefKind::Const
938 | DefKind::Static(..)
939 | DefKind::Ctor(..)
940 | DefKind::AssocFn
941 | DefKind::AssocConst
942 | DefKind::Macro(..)
5869c6ff 943 | DefKind::Field => true,
353b0b11
FG
944 DefKind::Use
945 | DefKind::ForeignMod
946 | DefKind::TyParam
5869c6ff
XL
947 | DefKind::ConstParam
948 | DefKind::LifetimeParam
949 | DefKind::AnonConst
3c0e092e 950 | DefKind::InlineConst
353b0b11
FG
951 | DefKind::OpaqueTy
952 | DefKind::ImplTraitPlaceholder
5869c6ff 953 | DefKind::GlobalAsm
353b0b11 954 | DefKind::Impl { .. }
5869c6ff
XL
955 | DefKind::Closure
956 | DefKind::Generator
957 | DefKind::ExternCrate => false,
958 }
959}
960
961fn should_encode_stability(def_kind: DefKind) -> bool {
962 match def_kind {
963 DefKind::Mod
964 | DefKind::Ctor(..)
965 | DefKind::Variant
966 | DefKind::Field
967 | DefKind::Struct
968 | DefKind::AssocTy
969 | DefKind::AssocFn
970 | DefKind::AssocConst
971 | DefKind::TyParam
972 | DefKind::ConstParam
5e7ed085 973 | DefKind::Static(..)
5869c6ff
XL
974 | DefKind::Const
975 | DefKind::Fn
976 | DefKind::ForeignMod
977 | DefKind::TyAlias
978 | DefKind::OpaqueTy
f2b60f7d 979 | DefKind::ImplTraitPlaceholder
5869c6ff
XL
980 | DefKind::Enum
981 | DefKind::Union
9ffffee4 982 | DefKind::Impl { .. }
5869c6ff
XL
983 | DefKind::Trait
984 | DefKind::TraitAlias
985 | DefKind::Macro(..)
986 | DefKind::ForeignTy => true,
987 DefKind::Use
988 | DefKind::LifetimeParam
989 | DefKind::AnonConst
3c0e092e 990 | DefKind::InlineConst
5869c6ff
XL
991 | DefKind::GlobalAsm
992 | DefKind::Closure
993 | DefKind::Generator
994 | DefKind::ExternCrate => false,
995 }
996}
997
998/// Whether we should encode MIR.
999///
1000/// Computing, optimizing and encoding the MIR is a relatively expensive operation.
1001/// We want to avoid this work when not required. Therefore:
1002/// - we only compute `mir_for_ctfe` on items with const-eval semantics;
1003/// - we skip `optimized_mir` for check runs.
1004///
1005/// Return a pair, resp. for CTFE and for LLVM.
1006fn should_encode_mir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> (bool, bool) {
1007 match tcx.def_kind(def_id) {
1008 // Constructors
1009 DefKind::Ctor(_, _) => {
1010 let mir_opt_base = tcx.sess.opts.output_types.should_codegen()
064997fb 1011 || tcx.sess.opts.unstable_opts.always_encode_mir;
5869c6ff
XL
1012 (true, mir_opt_base)
1013 }
1014 // Constants
3c0e092e
XL
1015 DefKind::AnonConst
1016 | DefKind::InlineConst
1017 | DefKind::AssocConst
5e7ed085 1018 | DefKind::Static(..)
3c0e092e 1019 | DefKind::Const => (true, false),
9c376795
FG
1020 // Full-fledged functions + closures
1021 DefKind::AssocFn | DefKind::Fn | DefKind::Closure => {
5869c6ff
XL
1022 let generics = tcx.generics_of(def_id);
1023 let needs_inline = (generics.requires_monomorphization(tcx)
1024 || tcx.codegen_fn_attrs(def_id).requests_inline())
1025 && tcx.sess.opts.output_types.should_codegen();
923072b8 1026 // The function has a `const` modifier or is in a `#[const_trait]`.
a2a8927a 1027 let is_const_fn = tcx.is_const_fn_raw(def_id.to_def_id())
923072b8 1028 || tcx.is_const_default_method(def_id.to_def_id());
064997fb 1029 let always_encode_mir = tcx.sess.opts.unstable_opts.always_encode_mir;
5869c6ff
XL
1030 (is_const_fn, needs_inline || always_encode_mir)
1031 }
5869c6ff
XL
1032 // Generators require optimized MIR to compute layout.
1033 DefKind::Generator => (false, true),
1034 // The others don't have MIR.
1035 _ => (false, false),
1036 }
1037}
1038
6a06907d
XL
1039fn should_encode_variances(def_kind: DefKind) -> bool {
1040 match def_kind {
1041 DefKind::Struct
1042 | DefKind::Union
1043 | DefKind::Enum
1044 | DefKind::Variant
487cf647
FG
1045 | DefKind::OpaqueTy
1046 | DefKind::ImplTraitPlaceholder
6a06907d
XL
1047 | DefKind::Fn
1048 | DefKind::Ctor(..)
1049 | DefKind::AssocFn => true,
1050 DefKind::Mod
1051 | DefKind::Field
1052 | DefKind::AssocTy
1053 | DefKind::AssocConst
1054 | DefKind::TyParam
1055 | DefKind::ConstParam
5e7ed085 1056 | DefKind::Static(..)
6a06907d
XL
1057 | DefKind::Const
1058 | DefKind::ForeignMod
1059 | DefKind::TyAlias
9ffffee4 1060 | DefKind::Impl { .. }
6a06907d
XL
1061 | DefKind::Trait
1062 | DefKind::TraitAlias
1063 | DefKind::Macro(..)
1064 | DefKind::ForeignTy
1065 | DefKind::Use
1066 | DefKind::LifetimeParam
1067 | DefKind::AnonConst
3c0e092e 1068 | DefKind::InlineConst
6a06907d
XL
1069 | DefKind::GlobalAsm
1070 | DefKind::Closure
1071 | DefKind::Generator
1072 | DefKind::ExternCrate => false,
1073 }
1074}
1075
1076fn should_encode_generics(def_kind: DefKind) -> bool {
1077 match def_kind {
1078 DefKind::Struct
1079 | DefKind::Union
1080 | DefKind::Enum
1081 | DefKind::Variant
1082 | DefKind::Trait
1083 | DefKind::TyAlias
1084 | DefKind::ForeignTy
1085 | DefKind::TraitAlias
1086 | DefKind::AssocTy
1087 | DefKind::Fn
1088 | DefKind::Const
5e7ed085 1089 | DefKind::Static(..)
6a06907d
XL
1090 | DefKind::Ctor(..)
1091 | DefKind::AssocFn
1092 | DefKind::AssocConst
1093 | DefKind::AnonConst
3c0e092e 1094 | DefKind::InlineConst
6a06907d 1095 | DefKind::OpaqueTy
f2b60f7d 1096 | DefKind::ImplTraitPlaceholder
9ffffee4 1097 | DefKind::Impl { .. }
94222f64
XL
1098 | DefKind::Field
1099 | DefKind::TyParam
6a06907d
XL
1100 | DefKind::Closure
1101 | DefKind::Generator => true,
1102 DefKind::Mod
6a06907d 1103 | DefKind::ForeignMod
6a06907d
XL
1104 | DefKind::ConstParam
1105 | DefKind::Macro(..)
1106 | DefKind::Use
1107 | DefKind::LifetimeParam
1108 | DefKind::GlobalAsm
1109 | DefKind::ExternCrate => false,
1110 }
1111}
1112
f2b60f7d
FG
1113fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) -> bool {
1114 match def_kind {
1115 DefKind::Struct
1116 | DefKind::Union
1117 | DefKind::Enum
1118 | DefKind::Variant
1119 | DefKind::Ctor(..)
1120 | DefKind::Field
1121 | DefKind::Fn
1122 | DefKind::Const
1123 | DefKind::Static(..)
1124 | DefKind::TyAlias
f2b60f7d 1125 | DefKind::ForeignTy
9ffffee4 1126 | DefKind::Impl { .. }
f2b60f7d
FG
1127 | DefKind::AssocFn
1128 | DefKind::AssocConst
1129 | DefKind::Closure
1130 | DefKind::Generator
1131 | DefKind::ConstParam
1132 | DefKind::AnonConst
1133 | DefKind::InlineConst => true,
1134
353b0b11
FG
1135 DefKind::OpaqueTy => {
1136 let opaque = tcx.hir().expect_item(def_id).expect_opaque_ty();
1137 if let hir::OpaqueTyOrigin::FnReturn(fn_def_id) | hir::OpaqueTyOrigin::AsyncFn(fn_def_id) = opaque.origin
1138 && let hir::Node::TraitItem(trait_item) = tcx.hir().get_by_def_id(fn_def_id)
1139 && let (_, hir::TraitFn::Required(..)) = trait_item.expect_fn()
1140 {
1141 false
1142 } else {
1143 true
1144 }
1145 }
1146
f2b60f7d 1147 DefKind::ImplTraitPlaceholder => {
353b0b11 1148 let parent_def_id = tcx.impl_trait_in_trait_parent_fn(def_id.to_def_id());
f2b60f7d
FG
1149 let assoc_item = tcx.associated_item(parent_def_id);
1150 match assoc_item.container {
1151 // Always encode an RPIT in an impl fn, since it always has a body
1152 ty::AssocItemContainer::ImplContainer => true,
1153 ty::AssocItemContainer::TraitContainer => {
1154 // Encode an RPIT for a trait only if the trait has a default body
1155 assoc_item.defaultness(tcx).has_value()
1156 }
1157 }
1158 }
1159
1160 DefKind::AssocTy => {
1161 let assoc_item = tcx.associated_item(def_id);
1162 match assoc_item.container {
1163 ty::AssocItemContainer::ImplContainer => true,
353b0b11
FG
1164 // FIXME(-Zlower-impl-trait-in-trait-to-assoc-ty) always encode RPITITs,
1165 // since we need to be able to "project" from an RPITIT associated item
1166 // to an opaque when installing the default projection predicates in
1167 // default trait methods with RPITITs.
1168 ty::AssocItemContainer::TraitContainer => {
1169 assoc_item.defaultness(tcx).has_value() || assoc_item.opt_rpitit_info.is_some()
1170 }
f2b60f7d
FG
1171 }
1172 }
1173 DefKind::TyParam => {
1174 let hir::Node::GenericParam(param) = tcx.hir().get_by_def_id(def_id) else { bug!() };
1175 let hir::GenericParamKind::Type { default, .. } = param.kind else { bug!() };
1176 default.is_some()
1177 }
1178
1179 DefKind::Trait
1180 | DefKind::TraitAlias
1181 | DefKind::Mod
1182 | DefKind::ForeignMod
1183 | DefKind::Macro(..)
1184 | DefKind::Use
1185 | DefKind::LifetimeParam
1186 | DefKind::GlobalAsm
1187 | DefKind::ExternCrate => false,
1188 }
1189}
1190
1191fn should_encode_const(def_kind: DefKind) -> bool {
1192 match def_kind {
1193 DefKind::Const | DefKind::AssocConst | DefKind::AnonConst => true,
1194
1195 DefKind::Struct
1196 | DefKind::Union
1197 | DefKind::Enum
1198 | DefKind::Variant
1199 | DefKind::Ctor(..)
1200 | DefKind::Field
1201 | DefKind::Fn
1202 | DefKind::Static(..)
1203 | DefKind::TyAlias
1204 | DefKind::OpaqueTy
1205 | DefKind::ImplTraitPlaceholder
1206 | DefKind::ForeignTy
9ffffee4 1207 | DefKind::Impl { .. }
f2b60f7d
FG
1208 | DefKind::AssocFn
1209 | DefKind::Closure
1210 | DefKind::Generator
1211 | DefKind::ConstParam
1212 | DefKind::InlineConst
1213 | DefKind::AssocTy
1214 | DefKind::TyParam
1215 | DefKind::Trait
1216 | DefKind::TraitAlias
1217 | DefKind::Mod
1218 | DefKind::ForeignMod
1219 | DefKind::Macro(..)
1220 | DefKind::Use
1221 | DefKind::LifetimeParam
1222 | DefKind::GlobalAsm
1223 | DefKind::ExternCrate => false,
1224 }
1225}
1226
9ffffee4
FG
1227// We only encode impl trait in trait when using `lower-impl-trait-in-trait-to-assoc-ty` unstable
1228// option.
1229fn should_encode_fn_impl_trait_in_trait<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
353b0b11 1230 if tcx.lower_impl_trait_in_trait_to_assoc_ty()
9ffffee4
FG
1231 && let Some(assoc_item) = tcx.opt_associated_item(def_id)
1232 && assoc_item.container == ty::AssocItemContainer::TraitContainer
1233 && assoc_item.kind == ty::AssocKind::Fn
1234 {
1235 true
1236 } else {
1237 false
2b03887a 1238 }
2b03887a
FG
1239}
1240
a2a8927a 1241impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
04454e1e 1242 fn encode_attrs(&mut self, def_id: LocalDefId) {
2b03887a 1243 let tcx = self.tcx;
9ffffee4
FG
1244 let mut state = AnalyzeAttrState {
1245 is_exported: tcx.effective_visibilities(()).is_exported(def_id),
1246 is_doc_hidden: false,
1247 };
1248 let attr_iter = tcx
1249 .opt_local_def_id_to_hir_id(def_id)
1250 .map_or(Default::default(), |hir_id| tcx.hir().attrs(hir_id))
04454e1e 1251 .iter()
9ffffee4
FG
1252 .filter(|attr| analyze_attr(attr, &mut state));
1253
1254 record_array!(self.tables.attributes[def_id.to_def_id()] <- attr_iter);
04454e1e 1255
9ffffee4
FG
1256 let mut attr_flags = AttrFlags::empty();
1257 if state.is_doc_hidden {
1258 attr_flags |= AttrFlags::IS_DOC_HIDDEN;
04454e1e 1259 }
9ffffee4 1260 self.tables.attr_flags.set(def_id.local_def_index, attr_flags);
04454e1e
FG
1261 }
1262
5869c6ff
XL
1263 fn encode_def_ids(&mut self) {
1264 if self.is_proc_macro {
1265 return;
1266 }
1267 let tcx = self.tcx;
064997fb 1268 for local_id in tcx.iter_local_def_id() {
5869c6ff
XL
1269 let def_id = local_id.to_def_id();
1270 let def_kind = tcx.opt_def_kind(local_id);
5099ac24 1271 let Some(def_kind) = def_kind else { continue };
9ffffee4 1272 self.tables.opt_def_kind.set_some(def_id.index, def_kind);
353b0b11
FG
1273 if should_encode_span(def_kind) {
1274 let def_span = tcx.def_span(local_id);
1275 record!(self.tables.def_span[def_id] <- def_span);
1276 }
1277 if should_encode_attrs(def_kind) {
1278 self.encode_attrs(local_id);
1279 }
1280 if should_encode_expn_that_defined(def_kind) {
1281 record!(self.tables.expn_that_defined[def_id] <- self.tcx.expn_that_defined(def_id));
1282 }
1283 if should_encode_span(def_kind) && let Some(ident_span) = tcx.def_ident_span(def_id) {
923072b8
FG
1284 record!(self.tables.def_ident_span[def_id] <- ident_span);
1285 }
04454e1e
FG
1286 if def_kind.has_codegen_attrs() {
1287 record!(self.tables.codegen_fn_attrs[def_id] <- self.tcx.codegen_fn_attrs(def_id));
1288 }
5869c6ff 1289 if should_encode_visibility(def_kind) {
f2b60f7d
FG
1290 let vis =
1291 self.tcx.local_visibility(local_id).map_id(|def_id| def_id.local_def_index);
1292 record!(self.tables.visibility[def_id] <- vis);
5869c6ff
XL
1293 }
1294 if should_encode_stability(def_kind) {
1295 self.encode_stability(def_id);
1296 self.encode_const_stability(def_id);
f2b60f7d 1297 self.encode_default_body_stability(def_id);
5869c6ff
XL
1298 self.encode_deprecation(def_id);
1299 }
6a06907d
XL
1300 if should_encode_variances(def_kind) {
1301 let v = self.tcx.variances_of(def_id);
923072b8 1302 record_array!(self.tables.variances_of[def_id] <- v);
6a06907d
XL
1303 }
1304 if should_encode_generics(def_kind) {
1305 let g = tcx.generics_of(def_id);
5e7ed085
FG
1306 record!(self.tables.generics_of[def_id] <- g);
1307 record!(self.tables.explicit_predicates_of[def_id] <- self.tcx.explicit_predicates_of(def_id));
6a06907d 1308 let inferred_outlives = self.tcx.inferred_outlives_of(def_id);
9ffffee4 1309 record_defaulted_array!(self.tables.inferred_outlives_of[def_id] <- inferred_outlives);
6a06907d 1310 }
f2b60f7d
FG
1311 if should_encode_type(tcx, local_id, def_kind) {
1312 record!(self.tables.type_of[def_id] <- self.tcx.type_of(def_id));
1313 }
1314 if let DefKind::TyParam = def_kind {
1315 let default = self.tcx.object_lifetime_default(def_id);
1316 record!(self.tables.object_lifetime_default[def_id] <- default);
1317 }
353b0b11 1318 if let DefKind::Trait = def_kind {
5e7ed085 1319 record!(self.tables.super_predicates_of[def_id] <- self.tcx.super_predicates_of(def_id));
6a06907d 1320 }
353b0b11
FG
1321 if let DefKind::TraitAlias = def_kind {
1322 record!(self.tables.super_predicates_of[def_id] <- self.tcx.super_predicates_of(def_id));
1323 record!(self.tables.implied_predicates_of[def_id] <- self.tcx.implied_predicates_of(def_id));
1324 }
2b03887a 1325 if let DefKind::Enum | DefKind::Struct | DefKind::Union = def_kind {
353b0b11 1326 self.encode_info_for_adt(local_id);
2b03887a 1327 }
9ffffee4 1328 if tcx.impl_method_has_trait_impl_trait_tys(def_id)
9c376795 1329 && let Ok(table) = self.tcx.collect_return_position_impl_trait_in_trait_tys(def_id)
2b03887a
FG
1330 {
1331 record!(self.tables.trait_impl_trait_tys[def_id] <- table);
1332 }
9ffffee4 1333 if should_encode_fn_impl_trait_in_trait(tcx, def_id) {
353b0b11
FG
1334 let table = tcx.associated_types_for_impl_traits_in_associated_fn(def_id);
1335 record_defaulted_array!(self.tables.associated_types_for_impl_traits_in_associated_fn[def_id] <- table);
9ffffee4 1336 }
6a06907d 1337 }
9ffffee4 1338
9c376795
FG
1339 let inherent_impls = tcx.with_stable_hashing_context(|hcx| {
1340 tcx.crate_inherent_impls(()).inherent_impls.to_sorted(&hcx, true)
1341 });
9ffffee4
FG
1342 for (def_id, impls) in inherent_impls {
1343 record_defaulted_array!(self.tables.inherent_impls[def_id.to_def_id()] <- impls.iter().map(|def_id| {
6a06907d
XL
1344 assert!(def_id.is_local());
1345 def_id.index
1346 }));
5869c6ff 1347 }
9ffffee4
FG
1348
1349 for (def_id, res_map) in &tcx.resolutions(()).doc_link_resolutions {
1350 record!(self.tables.doc_link_resolutions[def_id.to_def_id()] <- res_map);
1351 }
1352
1353 for (def_id, traits) in &tcx.resolutions(()).doc_link_traits_in_scope {
1354 record_array!(self.tables.doc_link_traits_in_scope[def_id.to_def_id()] <- traits);
1355 }
223e47cc 1356 }
970d7e83 1357
9ffffee4 1358 #[instrument(level = "trace", skip(self))]
353b0b11
FG
1359 fn encode_info_for_adt(&mut self, local_def_id: LocalDefId) {
1360 let def_id = local_def_id.to_def_id();
9e0c209e 1361 let tcx = self.tcx;
9ffffee4
FG
1362 let adt_def = tcx.adt_def(def_id);
1363 record!(self.tables.repr_options[def_id] <- adt_def.repr());
1364
1365 let params_in_repr = self.tcx.params_in_repr(def_id);
1366 record!(self.tables.params_in_repr[def_id] <- params_in_repr);
970d7e83 1367
9ffffee4 1368 if adt_def.is_enum() {
353b0b11
FG
1369 let module_children = tcx.module_children_non_reexports(local_def_id);
1370 record_array!(self.tables.children[def_id] <-
1371 module_children.iter().map(|def_id| def_id.local_def_index));
9ffffee4
FG
1372 } else {
1373 // For non-enum, there is only one variant, and its def_id is the adt's.
1374 debug_assert_eq!(adt_def.variants().len(), 1);
1375 debug_assert_eq!(adt_def.non_enum_variant().def_id, def_id);
1376 // Therefore, the loop over variants will encode its fields as the adt's children.
9e0c209e 1377 }
223e47cc 1378
353b0b11 1379 for (idx, variant) in adt_def.variants().iter_enumerated() {
9ffffee4
FG
1380 let data = VariantData {
1381 discr: variant.discr,
353b0b11 1382 idx,
9ffffee4
FG
1383 ctor: variant.ctor.map(|(kind, def_id)| (kind, def_id.index)),
1384 is_non_exhaustive: variant.is_field_list_non_exhaustive(),
1385 };
1386 record!(self.tables.variant_data[variant.def_id] <- data);
532ac7d7 1387
9ffffee4
FG
1388 self.tables.constness.set_some(variant.def_id.index, hir::Constness::Const);
1389 record_array!(self.tables.children[variant.def_id] <- variant.fields.iter().map(|f| {
1390 assert!(f.did.is_local());
1391 f.did.index
1392 }));
532ac7d7 1393
9ffffee4
FG
1394 if let Some((CtorKind::Fn, ctor_def_id)) = variant.ctor {
1395 self.tables.constness.set_some(ctor_def_id.index, hir::Constness::Const);
1396 let fn_sig = tcx.fn_sig(ctor_def_id);
1397 record!(self.tables.fn_sig[ctor_def_id] <- fn_sig);
1398 // FIXME only encode signature for ctor_def_id
1399 record!(self.tables.fn_sig[variant.def_id] <- fn_sig);
1400 }
532ac7d7
XL
1401 }
1402 }
1403
353b0b11 1404 fn encode_info_for_mod(&mut self, local_def_id: LocalDefId) {
9e0c209e 1405 let tcx = self.tcx;
3dfed10e 1406 let def_id = local_def_id.to_def_id();
dc9dc135 1407 debug!("EncodeContext::encode_info_for_mod({:?})", def_id);
9e0c209e 1408
1b1a35ee
XL
1409 // If we are encoding a proc-macro crates, `encode_info_for_mod` will
1410 // only ever get called for the crate root. We still want to encode
1411 // the crate root for consistency with other crates (some of the resolver
1412 // code uses it). However, we skip encoding anything relating to child
1413 // items - we encode information about proc-macros later on.
1b1a35ee 1414 if self.is_proc_macro {
3c0e092e
XL
1415 // Encode this here because we don't do it in encode_def_ids.
1416 record!(self.tables.expn_that_defined[def_id] <- tcx.expn_that_defined(local_def_id));
1b1a35ee 1417 } else {
353b0b11
FG
1418 let non_reexports = tcx.module_children_non_reexports(local_def_id);
1419 record_array!(self.tables.children[def_id] <-
1420 non_reexports.iter().map(|def_id| def_id.local_def_index));
487cf647 1421
353b0b11
FG
1422 record_defaulted_array!(self.tables.module_children_reexports[def_id] <-
1423 tcx.module_children_reexports(local_def_id));
1b1a35ee 1424 }
9e0c209e 1425 }
223e47cc 1426
29967ef6
XL
1427 fn encode_explicit_item_bounds(&mut self, def_id: DefId) {
1428 debug!("EncodeContext::encode_explicit_item_bounds({:?})", def_id);
1429 let bounds = self.tcx.explicit_item_bounds(def_id);
9ffffee4 1430 record_defaulted_array!(self.tables.explicit_item_bounds[def_id] <- bounds);
29967ef6
XL
1431 }
1432
e74abb32 1433 fn encode_info_for_trait_item(&mut self, def_id: DefId) {
dc9dc135 1434 debug!("EncodeContext::encode_info_for_trait_item({:?})", def_id);
9e0c209e 1435 let tcx = self.tcx;
c34b1796 1436
9c376795 1437 let impl_defaultness = tcx.impl_defaultness(def_id.expect_local());
9ffffee4 1438 self.tables.impl_defaultness.set_some(def_id.index, impl_defaultness);
476ff2be 1439 let trait_item = tcx.associated_item(def_id);
9ffffee4 1440 self.tables.assoc_container.set_some(def_id.index, trait_item.container);
476ff2be 1441
5869c6ff 1442 match trait_item.kind {
f2b60f7d 1443 ty::AssocKind::Const => {}
ba9703b0 1444 ty::AssocKind::Fn => {
9c376795 1445 record_array!(self.tables.fn_arg_names[def_id] <- tcx.fn_arg_names(def_id));
9ffffee4
FG
1446 self.tables.asyncness.set_some(def_id.index, tcx.asyncness(def_id));
1447 self.tables.constness.set_some(def_id.index, hir::Constness::NotConst);
9e0c209e 1448 }
29967ef6
XL
1449 ty::AssocKind::Type => {
1450 self.encode_explicit_item_bounds(def_id);
e74abb32 1451 }
e74abb32 1452 }
ba9703b0
XL
1453 if trait_item.kind == ty::AssocKind::Fn {
1454 record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
1a4d82fc 1455 }
353b0b11
FG
1456 if let Some(rpitit_info) = trait_item.opt_rpitit_info {
1457 let rpitit_info = self.lazy(rpitit_info);
1458 self.tables.opt_rpitit_info.set_some(def_id.index, rpitit_info);
1459 }
83c7162d
XL
1460 }
1461
e74abb32 1462 fn encode_info_for_impl_item(&mut self, def_id: DefId) {
dc9dc135 1463 debug!("EncodeContext::encode_info_for_impl_item({:?})", def_id);
041b39d2
XL
1464 let tcx = self.tcx;
1465
353b0b11
FG
1466 let defaultness = self.tcx.impl_defaultness(def_id.expect_local());
1467 self.tables.impl_defaultness.set_some(def_id.index, defaultness);
476ff2be 1468 let impl_item = self.tcx.associated_item(def_id);
9ffffee4 1469 self.tables.assoc_container.set_some(def_id.index, impl_item.container);
d9579d0f 1470
5869c6ff 1471 match impl_item.kind {
ba9703b0 1472 ty::AssocKind::Fn => {
353b0b11 1473 let ast_item = self.tcx.hir().expect_impl_item(def_id.expect_local());
5e7ed085 1474 let hir::ImplItemKind::Fn(ref sig, body) = ast_item.kind else { bug!() };
9ffffee4 1475 self.tables.asyncness.set_some(def_id.index, sig.header.asyncness);
923072b8 1476 record_array!(self.tables.fn_arg_names[def_id] <- self.tcx.hir().body_param_names(body));
5e7ed085
FG
1477 // Can be inside `impl const Trait`, so using sig.header.constness is not reliable
1478 let constness = if self.tcx.is_const_fn_raw(def_id) {
1479 hir::Constness::Const
9e0c209e 1480 } else {
5e7ed085 1481 hir::Constness::NotConst
9e0c209e 1482 };
9ffffee4 1483 self.tables.constness.set_some(def_id.index, constness);
5869c6ff 1484 }
f2b60f7d 1485 ty::AssocKind::Const | ty::AssocKind::Type => {}
5869c6ff 1486 }
5099ac24 1487 if let Some(trait_item_def_id) = impl_item.trait_item_def_id {
9ffffee4 1488 self.tables.trait_item_def_id.set_some(def_id.index, trait_item_def_id.into());
5099ac24 1489 }
ba9703b0
XL
1490 if impl_item.kind == ty::AssocKind::Fn {
1491 record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
9ffffee4 1492 self.tables.is_intrinsic.set(def_id.index, tcx.is_intrinsic(def_id));
e74abb32 1493 }
353b0b11
FG
1494 if let Some(rpitit_info) = impl_item.opt_rpitit_info {
1495 let rpitit_info = self.lazy(rpitit_info);
1496 self.tables.opt_rpitit_info.set_some(def_id.index, rpitit_info);
1497 }
223e47cc 1498 }
1a4d82fc 1499
5869c6ff
XL
1500 fn encode_mir(&mut self) {
1501 if self.is_proc_macro {
1502 return;
1503 }
3dfed10e 1504
f2b60f7d
FG
1505 let tcx = self.tcx;
1506
1507 let keys_and_jobs = tcx.mir_keys(()).iter().filter_map(|&def_id| {
1508 let (encode_const, encode_opt) = should_encode_mir(tcx, def_id);
1509 if encode_const || encode_opt { Some((def_id, encode_const, encode_opt)) } else { None }
1510 });
1511 for (def_id, encode_const, encode_opt) in keys_and_jobs {
5869c6ff
XL
1512 debug_assert!(encode_const || encode_opt);
1513
1514 debug!("EntryBuilder::encode_mir({:?})", def_id);
1515 if encode_opt {
f2b60f7d 1516 record!(self.tables.optimized_mir[def_id.to_def_id()] <- tcx.optimized_mir(def_id));
9ffffee4
FG
1517
1518 if tcx.sess.opts.unstable_opts.drop_tracking_mir && let DefKind::Generator = self.tcx.def_kind(def_id) {
1519 record!(self.tables.mir_generator_witnesses[def_id.to_def_id()] <- tcx.mir_generator_witnesses(def_id));
1520 }
3dfed10e 1521 }
5869c6ff 1522 if encode_const {
f2b60f7d 1523 record!(self.tables.mir_for_ctfe[def_id.to_def_id()] <- tcx.mir_for_ctfe(def_id));
1b1a35ee 1524
c295e0f8 1525 // FIXME(generic_const_exprs): this feels wrong to have in `encode_mir`
f2b60f7d 1526 let abstract_const = tcx.thir_abstract_const(def_id);
5869c6ff 1527 if let Ok(Some(abstract_const)) = abstract_const {
5e7ed085 1528 record!(self.tables.thir_abstract_const[def_id.to_def_id()] <- abstract_const);
5869c6ff 1529 }
f2b60f7d
FG
1530
1531 if should_encode_const(tcx.def_kind(def_id)) {
1532 let qualifs = tcx.mir_const_qualif(def_id);
1533 record!(self.tables.mir_const_qualif[def_id.to_def_id()] <- qualifs);
1534 let body_id = tcx.hir().maybe_body_owned_by(def_id);
1535 if let Some(body_id) = body_id {
1536 let const_data = self.encode_rendered_const_for_body(body_id);
1537 record!(self.tables.rendered_const[def_id.to_def_id()] <- const_data);
1538 }
1539 }
1b1a35ee 1540 }
f2b60f7d 1541 record!(self.tables.promoted_mir[def_id.to_def_id()] <- tcx.promoted_mir(def_id));
5869c6ff 1542
c295e0f8
XL
1543 let instance =
1544 ty::InstanceDef::Item(ty::WithOptConstParam::unknown(def_id.to_def_id()));
f2b60f7d 1545 let unused = tcx.unused_generic_params(instance);
353b0b11 1546 self.tables.unused_generic_params.set(def_id.local_def_index, unused);
7cac9316 1547 }
2b03887a
FG
1548
1549 // Encode all the deduced parameter attributes for everything that has MIR, even for items
1550 // that can't be inlined. But don't if we aren't optimizing in non-incremental mode, to
1551 // save the query traffic.
1552 if tcx.sess.opts.output_types.should_codegen()
1553 && tcx.sess.opts.optimize != OptLevel::No
1554 && tcx.sess.opts.incremental.is_none()
1555 {
1556 for &local_def_id in tcx.mir_keys(()) {
1557 if let DefKind::AssocFn | DefKind::Fn = tcx.def_kind(local_def_id) {
1558 record_array!(self.tables.deduced_param_attrs[local_def_id.to_def_id()] <-
1559 self.tcx.deduced_param_attrs(local_def_id.to_def_id()));
1560 }
1561 }
1562 }
9cc50fc6 1563 }
223e47cc 1564
e74abb32 1565 fn encode_stability(&mut self, def_id: DefId) {
dc9dc135 1566 debug!("EncodeContext::encode_stability({:?})", def_id);
1b1a35ee
XL
1567
1568 // The query lookup can take a measurable amount of time in crates with many items. Check if
1569 // the stability attributes are even enabled before using their queries.
064997fb 1570 if self.feat.staged_api || self.tcx.sess.opts.unstable_opts.force_unstable_if_unmarked {
1b1a35ee 1571 if let Some(stab) = self.tcx.lookup_stability(def_id) {
5e7ed085 1572 record!(self.tables.lookup_stability[def_id] <- stab)
1b1a35ee 1573 }
e74abb32 1574 }
9e0c209e 1575 }
1a4d82fc 1576
60c5eb7d
XL
1577 fn encode_const_stability(&mut self, def_id: DefId) {
1578 debug!("EncodeContext::encode_const_stability({:?})", def_id);
1b1a35ee
XL
1579
1580 // The query lookup can take a measurable amount of time in crates with many items. Check if
1581 // the stability attributes are even enabled before using their queries.
064997fb 1582 if self.feat.staged_api || self.tcx.sess.opts.unstable_opts.force_unstable_if_unmarked {
1b1a35ee 1583 if let Some(stab) = self.tcx.lookup_const_stability(def_id) {
5e7ed085 1584 record!(self.tables.lookup_const_stability[def_id] <- stab)
1b1a35ee 1585 }
60c5eb7d
XL
1586 }
1587 }
1588
f2b60f7d
FG
1589 fn encode_default_body_stability(&mut self, def_id: DefId) {
1590 debug!("EncodeContext::encode_default_body_stability({:?})", def_id);
1591
1592 // The query lookup can take a measurable amount of time in crates with many items. Check if
1593 // the stability attributes are even enabled before using their queries.
1594 if self.feat.staged_api || self.tcx.sess.opts.unstable_opts.force_unstable_if_unmarked {
1595 if let Some(stab) = self.tcx.lookup_default_body_stability(def_id) {
1596 record!(self.tables.lookup_default_body_stability[def_id] <- stab)
1597 }
1598 }
1599 }
1600
e74abb32 1601 fn encode_deprecation(&mut self, def_id: DefId) {
dc9dc135 1602 debug!("EncodeContext::encode_deprecation({:?})", def_id);
e74abb32 1603 if let Some(depr) = self.tcx.lookup_deprecation(def_id) {
5e7ed085 1604 record!(self.tables.lookup_deprecation_entry[def_id] <- depr);
e74abb32 1605 }
9e0c209e 1606 }
9cc50fc6 1607
5e7ed085 1608 fn encode_rendered_const_for_body(&mut self, body_id: hir::BodyId) -> String {
ba9703b0
XL
1609 let hir = self.tcx.hir();
1610 let body = hir.body(body_id);
5e7ed085 1611 rustc_hir_pretty::to_string(&(&hir as &dyn intravisit::Map<'_>), |s| {
ba9703b0 1612 s.print_expr(&body.value)
5e7ed085 1613 })
83c7162d
XL
1614 }
1615
353b0b11 1616 fn encode_info_for_item(&mut self, item: &'tcx hir::Item<'tcx>) {
9e0c209e 1617 let tcx = self.tcx;
353b0b11 1618 let def_id = item.owner_id.to_def_id();
dc9dc135 1619 debug!("EncodeContext::encode_info_for_item({:?})", def_id);
9e0c209e 1620
353b0b11
FG
1621 let record_associated_item_def_ids = |this: &mut Self, def_ids: &[DefId]| {
1622 record_array!(this.tables.children[def_id] <- def_ids.iter().map(|&def_id| {
1623 assert!(def_id.is_local());
1624 def_id.index
1625 }))
1626 };
1627
f2b60f7d 1628 match item.kind {
60c5eb7d 1629 hir::ItemKind::Fn(ref sig, .., body) => {
9ffffee4 1630 self.tables.asyncness.set_some(def_id.index, sig.header.asyncness);
923072b8 1631 record_array!(self.tables.fn_arg_names[def_id] <- self.tcx.hir().body_param_names(body));
9ffffee4 1632 self.tables.constness.set_some(def_id.index, sig.header.constness);
353b0b11
FG
1633 record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
1634 self.tables.is_intrinsic.set(def_id.index, tcx.is_intrinsic(def_id));
b039eaaf 1635 }
5e7ed085 1636 hir::ItemKind::Macro(ref macro_def, _) => {
9ffffee4 1637 self.tables.is_macro_rules.set(def_id.index, macro_def.macro_rules);
f2b60f7d 1638 record!(self.tables.macro_definition[def_id] <- &*macro_def.body);
94222f64 1639 }
353b0b11
FG
1640 hir::ItemKind::Mod(..) => {
1641 self.encode_info_for_mod(item.owner_id.def_id);
9e0c209e 1642 }
487cf647 1643 hir::ItemKind::OpaqueTy(ref opaque) => {
29967ef6 1644 self.encode_explicit_item_bounds(def_id);
9ffffee4
FG
1645 self.tables
1646 .is_type_alias_impl_trait
1647 .set(def_id.index, matches!(opaque.origin, hir::OpaqueTyOrigin::TyAlias));
9e0c209e 1648 }
136023e0 1649 hir::ItemKind::Impl(hir::Impl { defaultness, constness, .. }) => {
9ffffee4
FG
1650 self.tables.impl_defaultness.set_some(def_id.index, *defaultness);
1651 self.tables.constness.set_some(def_id.index, *constness);
353b0b11
FG
1652 self.tables.impl_polarity.set_some(def_id.index, self.tcx.impl_polarity(def_id));
1653
1654 if let Some(trait_ref) = self.tcx.impl_trait_ref(def_id) {
1655 record!(self.tables.impl_trait_ref[def_id] <- trait_ref);
5e7ed085 1656
9ffffee4 1657 let trait_ref = trait_ref.skip_binder();
e74abb32 1658 let trait_def = self.tcx.trait_def(trait_ref.def_id);
9c376795 1659 if let Ok(mut an) = trait_def.ancestors(self.tcx, def_id) {
5e7ed085 1660 if let Some(specialization_graph::Node::Impl(parent)) = an.nth(1) {
9ffffee4 1661 self.tables.impl_parent.set_some(def_id.index, parent.into());
5e7ed085
FG
1662 }
1663 }
b039eaaf 1664
5e7ed085
FG
1665 // if this is an impl of `CoerceUnsized`, create its
1666 // "unsized info", else just store None
1667 if Some(trait_ref.def_id) == self.tcx.lang_items().coerce_unsized_trait() {
1668 let coerce_unsized_info =
1669 self.tcx.at(item.span).coerce_unsized_info(def_id);
1670 record!(self.tables.coerce_unsized_info[def_id] <- coerce_unsized_info);
5869c6ff 1671 }
5e7ed085 1672 }
5869c6ff 1673
353b0b11
FG
1674 let associated_item_def_ids = self.tcx.associated_item_def_ids(def_id);
1675 record_associated_item_def_ids(self, associated_item_def_ids);
1676 for &trait_item_def_id in associated_item_def_ids {
1677 self.encode_info_for_impl_item(trait_item_def_id);
1678 }
9e0c209e 1679 }
9fa01778 1680 hir::ItemKind::Trait(..) => {
353b0b11
FG
1681 record!(self.tables.trait_def[def_id] <- self.tcx.trait_def(def_id));
1682
1683 let associated_item_def_ids = self.tcx.associated_item_def_ids(def_id);
1684 record_associated_item_def_ids(self, associated_item_def_ids);
1685 for &item_def_id in associated_item_def_ids {
1686 self.encode_info_for_trait_item(item_def_id);
1687 }
5e7ed085
FG
1688 }
1689 hir::ItemKind::TraitAlias(..) => {
353b0b11 1690 record!(self.tables.trait_def[def_id] <- self.tcx.trait_def(def_id));
d9579d0f 1691 }
353b0b11
FG
1692 hir::ItemKind::ExternCrate(_)
1693 | hir::ItemKind::Use(..)
1694 | hir::ItemKind::Static(..)
f2b60f7d 1695 | hir::ItemKind::Const(..)
9ffffee4
FG
1696 | hir::ItemKind::Enum(..)
1697 | hir::ItemKind::Struct(..)
1698 | hir::ItemKind::Union(..)
f2b60f7d
FG
1699 | hir::ItemKind::ForeignMod { .. }
1700 | hir::ItemKind::GlobalAsm(..)
1701 | hir::ItemKind::TyAlias(..) => {}
223e47cc 1702 }
223e47cc 1703 }
223e47cc 1704
487cf647
FG
1705 #[instrument(level = "debug", skip(self))]
1706 fn encode_info_for_closure(&mut self, def_id: LocalDefId) {
e74abb32 1707 // NOTE(eddyb) `tcx.type_of(def_id)` isn't used because it's fully generic,
9ffffee4 1708 // including on the signature, which is inferred in `typeck`.
04454e1e 1709 let typeck_result: &'tcx ty::TypeckResults<'tcx> = self.tcx.typeck(def_id);
487cf647 1710 let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
04454e1e 1711 let ty = typeck_result.node_type(hir_id);
5869c6ff 1712 match ty.kind() {
74b04a01
XL
1713 ty::Generator(..) => {
1714 let data = self.tcx.generator_kind(def_id).unwrap();
04454e1e 1715 let generator_diagnostic_data = typeck_result.get_generator_diagnostic_data();
5e7ed085 1716 record!(self.tables.generator_kind[def_id.to_def_id()] <- data);
04454e1e 1717 record!(self.tables.generator_diagnostic_data[def_id.to_def_id()] <- generator_diagnostic_data);
ff7c6d11
XL
1718 }
1719
f2b60f7d 1720 ty::Closure(_, substs) => {
9c376795 1721 let constness = self.tcx.constness(def_id.to_def_id());
9ffffee4
FG
1722 self.tables.constness.set_some(def_id.to_def_id().index, constness);
1723 record!(self.tables.fn_sig[def_id.to_def_id()] <- ty::EarlyBinder(substs.as_closure().sig()));
5869c6ff 1724 }
223e47cc 1725
e74abb32 1726 _ => bug!("closure that is neither generator nor closure"),
5869c6ff 1727 }
cc61c64b 1728 }
cc61c64b 1729
923072b8 1730 fn encode_native_libraries(&mut self) -> LazyArray<NativeLib> {
1b1a35ee 1731 empty_proc_macro!(self);
ea8adc8c 1732 let used_libraries = self.tcx.native_libraries(LOCAL_CRATE);
923072b8 1733 self.lazy_array(used_libraries.iter())
1a4d82fc 1734 }
1a4d82fc 1735
923072b8 1736 fn encode_foreign_modules(&mut self) -> LazyArray<ForeignModule> {
1b1a35ee 1737 empty_proc_macro!(self);
0531ce1d 1738 let foreign_modules = self.tcx.foreign_modules(LOCAL_CRATE);
923072b8 1739 self.lazy_array(foreign_modules.iter().map(|(_, m)| m).cloned())
e1599b0c
XL
1740 }
1741
136023e0 1742 fn encode_hygiene(&mut self) -> (SyntaxContextTable, ExpnDataTable, ExpnHashTable) {
3dfed10e
XL
1743 let mut syntax_contexts: TableBuilder<_, _> = Default::default();
1744 let mut expn_data_table: TableBuilder<_, _> = Default::default();
136023e0 1745 let mut expn_hash_table: TableBuilder<_, _> = Default::default();
3dfed10e 1746
923072b8 1747 self.hygiene_ctxt.encode(
136023e0
XL
1748 &mut (&mut *self, &mut syntax_contexts, &mut expn_data_table, &mut expn_hash_table),
1749 |(this, syntax_contexts, _, _), index, ctxt_data| {
9ffffee4 1750 syntax_contexts.set_some(index, this.lazy(ctxt_data));
3dfed10e 1751 },
136023e0
XL
1752 |(this, _, expn_data_table, expn_hash_table), index, expn_data, hash| {
1753 if let Some(index) = index.as_local() {
9ffffee4
FG
1754 expn_data_table.set_some(index.as_raw(), this.lazy(expn_data));
1755 expn_hash_table.set_some(index.as_raw(), this.lazy(hash));
136023e0 1756 }
3dfed10e
XL
1757 },
1758 );
1759
136023e0
XL
1760 (
1761 syntax_contexts.encode(&mut self.opaque),
1762 expn_data_table.encode(&mut self.opaque),
1763 expn_hash_table.encode(&mut self.opaque),
1764 )
3dfed10e
XL
1765 }
1766
1b1a35ee 1767 fn encode_proc_macros(&mut self) -> Option<ProcMacroData> {
f9f354fc 1768 let is_proc_macro = self.tcx.sess.crate_types().contains(&CrateType::ProcMacro);
e1599b0c
XL
1769 if is_proc_macro {
1770 let tcx = self.tcx;
1b1a35ee
XL
1771 let hir = tcx.hir();
1772
17df50a5 1773 let proc_macro_decls_static = tcx.proc_macro_decls_static(()).unwrap().local_def_index;
04454e1e 1774 let stability = tcx.lookup_stability(CRATE_DEF_ID);
94222f64 1775 let macros =
923072b8 1776 self.lazy_array(tcx.resolutions(()).proc_macros.iter().map(|p| p.local_def_index));
353b0b11 1777 for (i, span) in self.tcx.sess.parse_sess.proc_macro_quoted_spans() {
17df50a5 1778 let span = self.lazy(span);
9ffffee4 1779 self.tables.proc_macro_quoted_spans.set_some(i, span);
17df50a5 1780 }
1b1a35ee 1781
9ffffee4 1782 self.tables.opt_def_kind.set_some(LOCAL_CRATE.as_def_id().index, DefKind::Mod);
5e7ed085 1783 record!(self.tables.def_span[LOCAL_CRATE.as_def_id()] <- tcx.def_span(LOCAL_CRATE.as_def_id()));
04454e1e 1784 self.encode_attrs(LOCAL_CRATE.as_def_id().expect_local());
f2b60f7d
FG
1785 let vis = tcx.local_visibility(CRATE_DEF_ID).map_id(|def_id| def_id.local_def_index);
1786 record!(self.tables.visibility[LOCAL_CRATE.as_def_id()] <- vis);
5869c6ff 1787 if let Some(stability) = stability {
5e7ed085 1788 record!(self.tables.lookup_stability[LOCAL_CRATE.as_def_id()] <- stability);
5869c6ff
XL
1789 }
1790 self.encode_deprecation(LOCAL_CRATE.as_def_id());
9ffffee4
FG
1791 if let Some(res_map) = tcx.resolutions(()).doc_link_resolutions.get(&CRATE_DEF_ID) {
1792 record!(self.tables.doc_link_resolutions[LOCAL_CRATE.as_def_id()] <- res_map);
1793 }
1794 if let Some(traits) = tcx.resolutions(()).doc_link_traits_in_scope.get(&CRATE_DEF_ID) {
1795 record_array!(self.tables.doc_link_traits_in_scope[LOCAL_CRATE.as_def_id()] <- traits);
1796 }
5869c6ff 1797
1b1a35ee
XL
1798 // Normally, this information is encoded when we walk the items
1799 // defined in this crate. However, we skip doing that for proc-macro crates,
1800 // so we manually encode just the information that we need
94222f64
XL
1801 for &proc_macro in &tcx.resolutions(()).proc_macros {
1802 let id = proc_macro;
1803 let proc_macro = hir.local_def_id_to_hir_id(proc_macro);
1804 let mut name = hir.name(proc_macro);
1805 let span = hir.span(proc_macro);
1b1a35ee
XL
1806 // Proc-macros may have attributes like `#[allow_internal_unstable]`,
1807 // so downstream crates need access to them.
94222f64 1808 let attrs = hir.attrs(proc_macro);
353b0b11 1809 let macro_kind = if attr::contains_name(attrs, sym::proc_macro) {
fc512014 1810 MacroKind::Bang
353b0b11 1811 } else if attr::contains_name(attrs, sym::proc_macro_attribute) {
fc512014 1812 MacroKind::Attr
353b0b11 1813 } else if let Some(attr) = attr::find_by_name(attrs, sym::proc_macro_derive) {
fc512014
XL
1814 // This unwrap chain should have been checked by the proc-macro harness.
1815 name = attr.meta_item_list().unwrap()[0]
1816 .meta_item()
1817 .unwrap()
1818 .ident()
1819 .unwrap()
1820 .name;
1821 MacroKind::Derive
1822 } else {
1823 bug!("Unknown proc-macro type for item {:?}", id);
1824 };
1825
94222f64 1826 let mut def_key = self.tcx.hir().def_key(id);
fc512014
XL
1827 def_key.disambiguated_data.data = DefPathData::MacroNs(name);
1828
94222f64 1829 let def_id = id.to_def_id();
9ffffee4
FG
1830 self.tables.opt_def_kind.set_some(def_id.index, DefKind::Macro(macro_kind));
1831 self.tables.proc_macro.set_some(def_id.index, macro_kind);
04454e1e 1832 self.encode_attrs(id);
fc512014 1833 record!(self.tables.def_keys[def_id] <- def_key);
5e7ed085
FG
1834 record!(self.tables.def_ident_span[def_id] <- span);
1835 record!(self.tables.def_span[def_id] <- span);
fc512014
XL
1836 record!(self.tables.visibility[def_id] <- ty::Visibility::Public);
1837 if let Some(stability) = stability {
5e7ed085 1838 record!(self.tables.lookup_stability[def_id] <- stability);
fc512014 1839 }
1b1a35ee
XL
1840 }
1841
1842 Some(ProcMacroData { proc_macro_decls_static, stability, macros })
e1599b0c
XL
1843 } else {
1844 None
1845 }
0531ce1d
XL
1846 }
1847
923072b8 1848 fn encode_debugger_visualizers(&mut self) -> LazyArray<DebuggerVisualizerFile> {
04454e1e 1849 empty_proc_macro!(self);
923072b8 1850 self.lazy_array(self.tcx.debugger_visualizers(LOCAL_CRATE).iter())
04454e1e
FG
1851 }
1852
923072b8 1853 fn encode_crate_deps(&mut self) -> LazyArray<CrateDep> {
1b1a35ee 1854 empty_proc_macro!(self);
9e0c209e 1855
c295e0f8
XL
1856 let deps = self
1857 .tcx
1858 .crates(())
7cac9316
XL
1859 .iter()
1860 .map(|&cnum| {
1861 let dep = CrateDep {
17df50a5 1862 name: self.tcx.crate_name(cnum),
ea8adc8c 1863 hash: self.tcx.crate_hash(cnum),
e74abb32 1864 host_hash: self.tcx.crate_host_hash(cnum),
ea8adc8c 1865 kind: self.tcx.dep_kind(cnum),
5099ac24 1866 extra_filename: self.tcx.extra_filename(cnum).clone(),
7cac9316
XL
1867 };
1868 (cnum, dep)
1869 })
1870 .collect::<Vec<_>>();
1871
7cac9316 1872 {
9e0c209e
SL
1873 // Sanity-check the crate numbers
1874 let mut expected_cnum = 1;
1875 for &(n, _) in &deps {
1876 assert_eq!(n, CrateNum::new(expected_cnum));
1877 expected_cnum += 1;
1a4d82fc
JJ
1878 }
1879 }
1a4d82fc 1880
9e0c209e
SL
1881 // We're just going to write a list of crate 'name-hash-version's, with
1882 // the assumption that they are numbered 1 to n.
1883 // FIXME (#2166): This is not nearly enough to support correct versioning
1884 // but is enough to get transitive crate dependencies working.
9c376795 1885 self.lazy_array(deps.iter().map(|(_, dep)| dep))
223e47cc 1886 }
1a4d82fc 1887
923072b8 1888 fn encode_lib_features(&mut self) -> LazyArray<(Symbol, Option<Symbol>)> {
1b1a35ee 1889 empty_proc_macro!(self);
b7449926 1890 let tcx = self.tcx;
a2a8927a 1891 let lib_features = tcx.lib_features(());
923072b8 1892 self.lazy_array(lib_features.to_vec())
e1599b0c
XL
1893 }
1894
064997fb
FG
1895 fn encode_stability_implications(&mut self) -> LazyArray<(Symbol, Symbol)> {
1896 empty_proc_macro!(self);
1897 let tcx = self.tcx;
1898 let implications = tcx.stability_implications(LOCAL_CRATE);
1899 self.lazy_array(implications.iter().map(|(k, v)| (*k, *v)))
1900 }
1901
923072b8 1902 fn encode_diagnostic_items(&mut self) -> LazyArray<(Symbol, DefIndex)> {
1b1a35ee 1903 empty_proc_macro!(self);
e1599b0c 1904 let tcx = self.tcx;
c295e0f8 1905 let diagnostic_items = &tcx.diagnostic_items(LOCAL_CRATE).name_to_id;
923072b8 1906 self.lazy_array(diagnostic_items.iter().map(|(&name, def_id)| (name, def_id.index)))
b7449926
XL
1907 }
1908
487cf647 1909 fn encode_lang_items(&mut self) -> LazyArray<(DefIndex, LangItem)> {
1b1a35ee 1910 empty_proc_macro!(self);
487cf647
FG
1911 let lang_items = self.tcx.lang_items().iter();
1912 self.lazy_array(lang_items.filter_map(|(lang_item, def_id)| {
1913 def_id.as_local().map(|id| (id.local_def_index, lang_item))
7cac9316 1914 }))
1a4d82fc 1915 }
476ff2be 1916
487cf647 1917 fn encode_lang_items_missing(&mut self) -> LazyArray<LangItem> {
1b1a35ee 1918 empty_proc_macro!(self);
7cac9316 1919 let tcx = self.tcx;
923072b8 1920 self.lazy_array(&tcx.lang_items().missing)
476ff2be 1921 }
1a4d82fc 1922
923072b8 1923 fn encode_traits(&mut self) -> LazyArray<DefIndex> {
a2a8927a 1924 empty_proc_macro!(self);
923072b8 1925 self.lazy_array(self.tcx.traits_in_crate(LOCAL_CRATE).iter().map(|def_id| def_id.index))
a2a8927a
XL
1926 }
1927
9e0c209e 1928 /// Encodes an index, mapping each trait to its (local) implementations.
923072b8 1929 fn encode_impls(&mut self) -> LazyArray<TraitImpls> {
a2a8927a 1930 debug!("EncodeContext::encode_traits_and_impls()");
1b1a35ee 1931 empty_proc_macro!(self);
7cac9316 1932 let tcx = self.tcx;
04454e1e
FG
1933 let mut fx_hash_map: FxHashMap<DefId, Vec<(DefIndex, Option<SimplifiedType>)>> =
1934 FxHashMap::default();
1a4d82fc 1935
04454e1e 1936 for id in tcx.hir().items() {
9ffffee4 1937 if matches!(tcx.def_kind(id.owner_id), DefKind::Impl { .. }) {
2b03887a 1938 if let Some(trait_ref) = tcx.impl_trait_ref(id.owner_id) {
9c376795
FG
1939 let trait_ref = trait_ref.subst_identity();
1940
04454e1e
FG
1941 let simplified_self_ty = fast_reject::simplify_type(
1942 self.tcx,
1943 trait_ref.self_ty(),
353b0b11 1944 TreatParams::AsCandidateKey,
04454e1e
FG
1945 );
1946
1947 fx_hash_map
1948 .entry(trait_ref.def_id)
1949 .or_default()
2b03887a 1950 .push((id.owner_id.def_id.local_def_index, simplified_self_ty));
04454e1e
FG
1951 }
1952 }
1953 }
1954
1955 let mut all_impls: Vec<_> = fx_hash_map.into_iter().collect();
7cac9316
XL
1956
1957 // Bring everything into deterministic order for hashing
dfeec247 1958 all_impls.sort_by_cached_key(|&(trait_def_id, _)| tcx.def_path_hash(trait_def_id));
7cac9316
XL
1959
1960 let all_impls: Vec<_> = all_impls
c30ab7b3 1961 .into_iter()
7cac9316
XL
1962 .map(|(trait_def_id, mut impls)| {
1963 // Bring everything into deterministic order for hashing
3dfed10e 1964 impls.sort_by_cached_key(|&(index, _)| {
136023e0 1965 tcx.hir().def_path_hash(LocalDefId { local_def_index: index })
7cac9316
XL
1966 });
1967
c30ab7b3
SL
1968 TraitImpls {
1969 trait_id: (trait_def_id.krate.as_u32(), trait_def_id.index),
923072b8 1970 impls: self.lazy_array(&impls),
c30ab7b3
SL
1971 }
1972 })
1973 .collect();
1a4d82fc 1974
923072b8 1975 self.lazy_array(&all_impls)
1a4d82fc 1976 }
970d7e83 1977
923072b8 1978 fn encode_incoherent_impls(&mut self) -> LazyArray<IncoherentImpls> {
5e7ed085
FG
1979 debug!("EncodeContext::encode_traits_and_impls()");
1980 empty_proc_macro!(self);
1981 let tcx = self.tcx;
5e7ed085 1982 let mut all_impls: Vec<_> = tcx.crate_inherent_impls(()).incoherent_impls.iter().collect();
064997fb
FG
1983 tcx.with_stable_hashing_context(|mut ctx| {
1984 all_impls.sort_by_cached_key(|&(&simp, _)| {
1985 let mut hasher = StableHasher::new();
1986 simp.hash_stable(&mut ctx, &mut hasher);
1987 hasher.finish::<Fingerprint>()
1988 })
5e7ed085
FG
1989 });
1990 let all_impls: Vec<_> = all_impls
1991 .into_iter()
1992 .map(|(&simp, impls)| {
1993 let mut impls: Vec<_> =
1994 impls.into_iter().map(|def_id| def_id.local_def_index).collect();
1995 impls.sort_by_cached_key(|&local_def_index| {
1996 tcx.hir().def_path_hash(LocalDefId { local_def_index })
1997 });
1998
923072b8 1999 IncoherentImpls { self_ty: simp, impls: self.lazy_array(impls) }
5e7ed085
FG
2000 })
2001 .collect();
2002
923072b8 2003 self.lazy_array(&all_impls)
5e7ed085
FG
2004 }
2005
476ff2be 2006 // Encodes all symbols exported from this crate into the metadata.
9e0c209e
SL
2007 //
2008 // This pass is seeded off the reachability list calculated in the
2009 // middle::reachable module but filters out items that either don't have a
2010 // symbol associated with them (they weren't translated) or if they're an FFI
2011 // definition (as that's not defined in this crate).
dfeec247
XL
2012 fn encode_exported_symbols(
2013 &mut self,
04454e1e 2014 exported_symbols: &[(ExportedSymbol<'tcx>, SymbolExportInfo)],
923072b8 2015 ) -> LazyArray<(ExportedSymbol<'static>, SymbolExportInfo)> {
1b1a35ee 2016 empty_proc_macro!(self);
0531ce1d
XL
2017 // The metadata symbol name is special. It should not show up in
2018 // downstream crates.
3dfed10e 2019 let metadata_symbol_name = SymbolName::new(self.tcx, &metadata_symbol_name(self.tcx));
0531ce1d 2020
923072b8 2021 self.lazy_array(
dfeec247
XL
2022 exported_symbols
2023 .iter()
9c376795 2024 .filter(|&(exported_symbol, _)| match *exported_symbol {
dfeec247 2025 ExportedSymbol::NoDefId(symbol_name) => symbol_name != metadata_symbol_name,
0531ce1d 2026 _ => true,
dfeec247
XL
2027 })
2028 .cloned(),
2029 )
0531ce1d
XL
2030 }
2031
923072b8 2032 fn encode_dylib_dependency_formats(&mut self) -> LazyArray<Option<LinkagePreference>> {
1b1a35ee 2033 empty_proc_macro!(self);
17df50a5 2034 let formats = self.tcx.dependency_formats(());
e74abb32 2035 for (ty, arr) in formats.iter() {
f9f354fc 2036 if *ty != CrateType::Dylib {
e74abb32 2037 continue;
9e0c209e 2038 }
923072b8 2039 return self.lazy_array(arr.iter().map(|slot| match *slot {
dfeec247 2040 Linkage::NotLinked | Linkage::IncludedFromDylib => None,
e74abb32 2041
dfeec247
XL
2042 Linkage::Dynamic => Some(LinkagePreference::RequireDynamic),
2043 Linkage::Static => Some(LinkagePreference::RequireStatic),
e74abb32 2044 }));
a7813a04 2045 }
9ffffee4 2046 LazyArray::default()
a7813a04 2047 }
1a4d82fc 2048
dfeec247 2049 fn encode_info_for_foreign_item(&mut self, def_id: DefId, nitem: &hir::ForeignItem<'_>) {
7cac9316 2050 let tcx = self.tcx;
9e0c209e 2051
dc9dc135 2052 debug!("EncodeContext::encode_info_for_foreign_item({:?})", def_id);
9e0c209e 2053
5869c6ff 2054 match nitem.kind {
8faf50e0 2055 hir::ForeignItemKind::Fn(_, ref names, _) => {
9ffffee4 2056 self.tables.asyncness.set_some(def_id.index, hir::IsAsync::NotAsync);
923072b8 2057 record_array!(self.tables.fn_arg_names[def_id] <- *names);
5e7ed085
FG
2058 let constness = if self.tcx.is_const_fn_raw(def_id) {
2059 hir::Constness::Const
2060 } else {
2061 hir::Constness::NotConst
7cac9316 2062 };
9ffffee4 2063 self.tables.constness.set_some(def_id.index, constness);
f2b60f7d 2064 record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
5869c6ff 2065 }
f2b60f7d 2066 hir::ForeignItemKind::Static(..) | hir::ForeignItemKind::Type => {}
5869c6ff 2067 }
e74abb32 2068 if let hir::ForeignItemKind::Fn(..) = nitem.kind {
9ffffee4 2069 self.tables.is_intrinsic.set(def_id.index, tcx.is_intrinsic(def_id));
7cac9316
XL
2070 }
2071 }
2072}
9e0c209e 2073
e74abb32 2074// FIXME(eddyb) make metadata encoding walk over all definitions, instead of HIR.
a2a8927a 2075impl<'a, 'tcx> Visitor<'tcx> for EncodeContext<'a, 'tcx> {
5099ac24 2076 type NestedFilter = nested_filter::OnlyBodies;
dfeec247 2077
5099ac24
FG
2078 fn nested_visit_map(&mut self) -> Self::Map {
2079 self.tcx.hir()
7cac9316 2080 }
dfeec247 2081 fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) {
7cac9316 2082 intravisit::walk_expr(self, ex);
dc9dc135 2083 self.encode_info_for_expr(ex);
7cac9316 2084 }
dfeec247 2085 fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
7cac9316 2086 intravisit::walk_item(self, item);
353b0b11 2087 self.encode_info_for_item(item);
7cac9316 2088 }
dfeec247 2089 fn visit_foreign_item(&mut self, ni: &'tcx hir::ForeignItem<'tcx>) {
7cac9316 2090 intravisit::walk_foreign_item(self, ni);
2b03887a 2091 self.encode_info_for_foreign_item(ni.owner_id.to_def_id(), ni);
7cac9316 2092 }
dfeec247 2093 fn visit_generics(&mut self, generics: &'tcx hir::Generics<'tcx>) {
7cac9316 2094 intravisit::walk_generics(self, generics);
dc9dc135 2095 self.encode_info_for_generics(generics);
7cac9316 2096 }
7cac9316 2097}
9e0c209e 2098
a2a8927a 2099impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
dfeec247
XL
2100 fn encode_info_for_generics(&mut self, generics: &hir::Generics<'tcx>) {
2101 for param in generics.params {
b7449926 2102 match param.kind {
f2b60f7d
FG
2103 hir::GenericParamKind::Lifetime { .. } | hir::GenericParamKind::Type { .. } => {}
2104 hir::GenericParamKind::Const { ref default, .. } => {
487cf647 2105 let def_id = param.def_id.to_def_id();
cdc7bbd5 2106 if default.is_some() {
5e7ed085 2107 record!(self.tables.const_param_default[def_id] <- self.tcx.const_param_default(def_id))
cdc7bbd5 2108 }
9fa01778 2109 }
8faf50e0 2110 }
b7449926 2111 }
7cac9316 2112 }
9cc50fc6 2113
dfeec247 2114 fn encode_info_for_expr(&mut self, expr: &hir::Expr<'_>) {
487cf647
FG
2115 if let hir::ExprKind::Closure(closure) = expr.kind {
2116 self.encode_info_for_closure(closure.def_id);
7cac9316
XL
2117 }
2118 }
7cac9316 2119}
9e0c209e 2120
ba9703b0
XL
2121/// Used to prefetch queries which will be needed later by metadata encoding.
2122/// Only a subset of the queries are actually prefetched to keep this code smaller.
5869c6ff
XL
2123fn prefetch_mir(tcx: TyCtxt<'_>) {
2124 if !tcx.sess.opts.output_types.should_codegen() {
2125 // We won't emit MIR, so don't prefetch it.
2126 return;
ba9703b0 2127 }
ba9703b0 2128
17df50a5 2129 par_iter(tcx.mir_keys(())).for_each(|&def_id| {
5869c6ff 2130 let (encode_const, encode_opt) = should_encode_mir(tcx, def_id);
ba9703b0 2131
5869c6ff 2132 if encode_const {
353b0b11 2133 tcx.ensure_with_value().mir_for_ctfe(def_id);
ba9703b0 2134 }
5869c6ff 2135 if encode_opt {
353b0b11 2136 tcx.ensure_with_value().optimized_mir(def_id);
5869c6ff
XL
2137 }
2138 if encode_opt || encode_const {
353b0b11 2139 tcx.ensure_with_value().promoted_mir(def_id);
5869c6ff
XL
2140 }
2141 })
ba9703b0
XL
2142}
2143
9e0c209e
SL
2144// NOTE(eddyb) The following comment was preserved for posterity, even
2145// though it's no longer relevant as EBML (which uses nested & tagged
2146// "documents") was replaced with a scheme that can't go out of bounds.
2147//
2148// And here we run into yet another obscure archive bug: in which metadata
2149// loaded from archives may have trailing garbage bytes. Awhile back one of
cc61c64b 2150// our tests was failing sporadically on the macOS 64-bit builders (both nopt
9e0c209e
SL
2151// and opt) by having ebml generate an out-of-bounds panic when looking at
2152// metadata.
2153//
2154// Upon investigation it turned out that the metadata file inside of an rlib
2155// (and ar archive) was being corrupted. Some compilations would generate a
2156// metadata file which would end in a few extra bytes, while other
2157// compilations would not have these extra bytes appended to the end. These
2158// extra bytes were interpreted by ebml as an extra tag, so they ended up
2159// being interpreted causing the out-of-bounds.
2160//
2161// The root cause of why these extra bytes were appearing was never
2162// discovered, and in the meantime the solution we're employing is to insert
2163// the length of the metadata to the start of the metadata. Later on this
2164// will allow us to slice the metadata to the precise length that we just
2165// generated regardless of trailing bytes that end up in it.
2166
c295e0f8 2167pub struct EncodedMetadata {
064997fb
FG
2168 // The declaration order matters because `mmap` should be dropped before `_temp_dir`.
2169 mmap: Option<Mmap>,
2170 // We need to carry MaybeTempDir to avoid deleting the temporary
2171 // directory while accessing the Mmap.
2172 _temp_dir: Option<MaybeTempDir>,
c295e0f8
XL
2173}
2174
2175impl EncodedMetadata {
2176 #[inline]
064997fb
FG
2177 pub fn from_path(path: PathBuf, temp_dir: Option<MaybeTempDir>) -> std::io::Result<Self> {
2178 let file = std::fs::File::open(&path)?;
2179 let file_metadata = file.metadata()?;
2180 if file_metadata.len() == 0 {
2181 return Ok(Self { mmap: None, _temp_dir: None });
2182 }
2183 let mmap = unsafe { Some(Mmap::map(file)?) };
2184 Ok(Self { mmap, _temp_dir: temp_dir })
c295e0f8
XL
2185 }
2186
2187 #[inline]
2188 pub fn raw_data(&self) -> &[u8] {
487cf647 2189 self.mmap.as_deref().unwrap_or_default()
c295e0f8
XL
2190 }
2191}
2192
064997fb
FG
2193impl<S: Encoder> Encodable<S> for EncodedMetadata {
2194 fn encode(&self, s: &mut S) {
2195 let slice = self.raw_data();
2196 slice.encode(s)
2197 }
2198}
2199
2200impl<D: Decoder> Decodable<D> for EncodedMetadata {
2201 fn decode(d: &mut D) -> Self {
2202 let len = d.read_usize();
2203 let mmap = if len > 0 {
2204 let mut mmap = MmapMut::map_anon(len).unwrap();
2205 for _ in 0..len {
2206 (&mut mmap[..]).write(&[d.read_u8()]).unwrap();
2207 }
2208 mmap.flush().unwrap();
2209 Some(mmap.make_read_only().unwrap())
2210 } else {
2211 None
2212 };
2213
2214 Self { mmap, _temp_dir: None }
2215 }
2216}
2217
2218pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path) {
c295e0f8
XL
2219 let _prof_timer = tcx.prof.verbose_generic_activity("generate_crate_metadata");
2220
ba9703b0
XL
2221 // Since encoding metadata is not in a query, and nothing is cached,
2222 // there's no need to do dep-graph tracking for any of it.
2223 tcx.dep_graph.assert_ignored();
2224
2225 join(
064997fb 2226 || encode_metadata_impl(tcx, path),
ba9703b0
XL
2227 || {
2228 if tcx.sess.threads() == 1 {
2229 return;
2230 }
2231 // Prefetch some queries used by metadata encoding.
2232 // This is not necessary for correctness, but is only done for performance reasons.
2233 // It can be removed if it turns out to cause trouble or be detrimental to performance.
5869c6ff 2234 join(|| prefetch_mir(tcx), || tcx.exported_symbols(LOCAL_CRATE));
ba9703b0 2235 },
064997fb 2236 );
ba9703b0
XL
2237}
2238
064997fb
FG
2239fn encode_metadata_impl(tcx: TyCtxt<'_>, path: &Path) {
2240 let mut encoder = opaque::FileEncoder::new(path)
f2b60f7d 2241 .unwrap_or_else(|err| tcx.sess.emit_fatal(FailCreateFileEncoder { err }));
923072b8 2242 encoder.emit_raw_bytes(METADATA_HEADER);
9e0c209e 2243
7cac9316 2244 // Will be filled with the root position after encoding everything.
923072b8 2245 encoder.emit_raw_bytes(&[0, 0, 0, 0]);
9e0c209e 2246
f035d41b 2247 let source_map_files = tcx.sess.source_map().files();
29967ef6 2248 let source_file_cache = (source_map_files[0].clone(), 0);
f2b60f7d 2249 let required_source_files = Some(FxIndexSet::default());
29967ef6
XL
2250 drop(source_map_files);
2251
3dfed10e 2252 let hygiene_ctxt = HygieneEncodeContext::default();
f035d41b 2253
ba9703b0
XL
2254 let mut ecx = EncodeContext {
2255 opaque: encoder,
2256 tcx,
1b1a35ee 2257 feat: tcx.features(),
ba9703b0
XL
2258 tables: Default::default(),
2259 lazy_state: LazyState::NoNode,
2260 type_shorthands: Default::default(),
2261 predicate_shorthands: Default::default(),
29967ef6 2262 source_file_cache,
ba9703b0 2263 interpret_allocs: Default::default(),
29967ef6 2264 required_source_files,
f035d41b 2265 is_proc_macro: tcx.sess.crate_types().contains(&CrateType::ProcMacro),
3dfed10e 2266 hygiene_ctxt: &hygiene_ctxt,
f2b60f7d 2267 symbol_table: Default::default(),
ba9703b0
XL
2268 };
2269
2270 // Encode the rustc version string in a predictable location.
923072b8 2271 rustc_version().encode(&mut ecx);
ba9703b0
XL
2272
2273 // Encode all the entries and extra information in the crate,
2274 // culminating in the `CrateRoot` which points to all of it.
2275 let root = ecx.encode_crate_root();
2276
064997fb
FG
2277 ecx.opaque.flush();
2278
2279 let mut file = ecx.opaque.file();
2280 // We will return to this position after writing the root position.
2281 let pos_before_seek = file.stream_position().unwrap();
9e0c209e
SL
2282
2283 // Encode the root position.
2284 let header = METADATA_HEADER.len();
064997fb 2285 file.seek(std::io::SeekFrom::Start(header as u64))
f2b60f7d 2286 .unwrap_or_else(|err| tcx.sess.emit_fatal(FailSeekFile { err }));
e74abb32 2287 let pos = root.position.get();
064997fb 2288 file.write_all(&[(pos >> 24) as u8, (pos >> 16) as u8, (pos >> 8) as u8, (pos >> 0) as u8])
f2b60f7d 2289 .unwrap_or_else(|err| tcx.sess.emit_fatal(FailWriteFile { err }));
9e0c209e 2290
064997fb
FG
2291 // Return to the position where we are before writing the root position.
2292 file.seek(std::io::SeekFrom::Start(pos_before_seek)).unwrap();
3c0e092e 2293
064997fb
FG
2294 // Record metadata size for self-profiling
2295 tcx.prof.artifact_size(
2296 "crate_metadata",
2297 "crate_metadata",
2298 file.metadata().unwrap().len() as u64,
2299 );
223e47cc 2300}
a2a8927a
XL
2301
2302pub fn provide(providers: &mut Providers) {
2303 *providers = Providers {
9ffffee4
FG
2304 doc_link_resolutions: |tcx, def_id| {
2305 tcx.resolutions(())
2306 .doc_link_resolutions
353b0b11 2307 .get(&def_id)
9ffffee4
FG
2308 .expect("no resolutions for a doc link")
2309 },
2310 doc_link_traits_in_scope: |tcx, def_id| {
2311 tcx.resolutions(())
2312 .doc_link_traits_in_scope
353b0b11 2313 .get(&def_id)
9ffffee4
FG
2314 .expect("no traits in scope for a doc link")
2315 },
353b0b11 2316 traits_in_crate: |tcx, LocalCrate| {
923072b8
FG
2317 let mut traits = Vec::new();
2318 for id in tcx.hir().items() {
2b03887a
FG
2319 if matches!(tcx.def_kind(id.owner_id), DefKind::Trait | DefKind::TraitAlias) {
2320 traits.push(id.owner_id.to_def_id())
a2a8927a 2321 }
a2a8927a
XL
2322 }
2323
a2a8927a 2324 // Bring everything into deterministic order.
923072b8
FG
2325 traits.sort_by_cached_key(|&def_id| tcx.def_path_hash(def_id));
2326 tcx.arena.alloc_slice(&traits)
a2a8927a 2327 },
353b0b11 2328 trait_impls_in_crate: |tcx, LocalCrate| {
9ffffee4
FG
2329 let mut trait_impls = Vec::new();
2330 for id in tcx.hir().items() {
2331 if matches!(tcx.def_kind(id.owner_id), DefKind::Impl { .. })
2332 && tcx.impl_trait_ref(id.owner_id).is_some()
2333 {
2334 trait_impls.push(id.owner_id.to_def_id())
2335 }
2336 }
2337
2338 // Bring everything into deterministic order.
2339 trait_impls.sort_by_cached_key(|&def_id| tcx.def_path_hash(def_id));
2340 tcx.arena.alloc_slice(&trait_impls)
2341 },
a2a8927a
XL
2342
2343 ..*providers
064997fb 2344 }
a2a8927a 2345}