1use std::borrow::Borrow;
2use std::collections::hash_map::Entry;
3use std::fs::File;
4use std::io::{Read, Seek, Write};
5use std::path::{Path, PathBuf};
6use std::sync::Arc;
7
8use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
9use rustc_data_structures::memmap::{Mmap, MmapMut};
10use rustc_data_structures::sync::{join, par_for_each_in};
11use rustc_data_structures::temp_dir::MaybeTempDir;
12use rustc_data_structures::thousands::usize_with_underscores;
13use rustc_feature::Features;
14use rustc_hir as hir;
15use rustc_hir::attrs::{AttributeKind, EncodeCrossCrate};
16use rustc_hir::def_id::{CRATE_DEF_ID, CRATE_DEF_INDEX, LOCAL_CRATE, LocalDefId, LocalDefIdSet};
17use rustc_hir::definitions::DefPathData;
18use rustc_hir::find_attr;
19use rustc_hir_pretty::id_to_string;
20use rustc_middle::dep_graph::WorkProductId;
21use rustc_middle::middle::dependency_format::Linkage;
22use rustc_middle::middle::exported_symbols::metadata_symbol_name;
23use rustc_middle::mir::interpret;
24use rustc_middle::query::Providers;
25use rustc_middle::traits::specialization_graph;
26use rustc_middle::ty::codec::TyEncoder;
27use rustc_middle::ty::fast_reject::{self, TreatParams};
28use rustc_middle::ty::{AssocItemContainer, SymbolName};
29use rustc_middle::{bug, span_bug};
30use rustc_serialize::{Decodable, Decoder, Encodable, Encoder, opaque};
31use rustc_session::config::{CrateType, OptLevel, TargetModifier};
32use rustc_span::hygiene::HygieneEncodeContext;
33use rustc_span::{
34 ByteSymbol, ExternalSource, FileName, SourceFile, SpanData, SpanEncoder, StableSourceFileId,
35 Symbol, SyntaxContext, sym,
36};
37use tracing::{debug, instrument, trace};
38
39use crate::errors::{FailCreateFileEncoder, FailWriteFile};
40use crate::rmeta::*;
41
42pub(super) struct EncodeContext<'a, 'tcx> {
43 opaque: opaque::FileEncoder,
44 tcx: TyCtxt<'tcx>,
45 feat: &'tcx rustc_feature::Features,
46 tables: TableBuilders,
47
48 lazy_state: LazyState,
49 span_shorthands: FxHashMap<Span, usize>,
50 type_shorthands: FxHashMap<Ty<'tcx>, usize>,
51 predicate_shorthands: FxHashMap<ty::PredicateKind<'tcx>, usize>,
52
53 interpret_allocs: FxIndexSet<interpret::AllocId>,
54
55 source_file_cache: (Arc<SourceFile>, usize),
59 required_source_files: Option<FxIndexSet<usize>>,
66 is_proc_macro: bool,
67 hygiene_ctxt: &'a HygieneEncodeContext,
68 symbol_index_table: FxHashMap<u32, usize>,
70}
71
72macro_rules! empty_proc_macro {
76 ($self:ident) => {
77 if $self.is_proc_macro {
78 return LazyArray::default();
79 }
80 };
81}
82
83macro_rules! encoder_methods {
84 ($($name:ident($ty:ty);)*) => {
85 $(fn $name(&mut self, value: $ty) {
86 self.opaque.$name(value)
87 })*
88 }
89}
90
91impl<'a, 'tcx> Encoder for EncodeContext<'a, 'tcx> {
92 encoder_methods! {
93 emit_usize(usize);
94 emit_u128(u128);
95 emit_u64(u64);
96 emit_u32(u32);
97 emit_u16(u16);
98 emit_u8(u8);
99
100 emit_isize(isize);
101 emit_i128(i128);
102 emit_i64(i64);
103 emit_i32(i32);
104 emit_i16(i16);
105
106 emit_raw_bytes(&[u8]);
107 }
108}
109
110impl<'a, 'tcx, T> Encodable<EncodeContext<'a, 'tcx>> for LazyValue<T> {
111 fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) {
112 e.emit_lazy_distance(self.position);
113 }
114}
115
116impl<'a, 'tcx, T> Encodable<EncodeContext<'a, 'tcx>> for LazyArray<T> {
117 fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) {
118 e.emit_usize(self.num_elems);
119 if self.num_elems > 0 {
120 e.emit_lazy_distance(self.position)
121 }
122 }
123}
124
125impl<'a, 'tcx, I, T> Encodable<EncodeContext<'a, 'tcx>> for LazyTable<I, T> {
126 fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) {
127 e.emit_usize(self.width);
128 e.emit_usize(self.len);
129 e.emit_lazy_distance(self.position);
130 }
131}
132
133impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for ExpnIndex {
134 fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) {
135 s.emit_u32(self.as_u32());
136 }
137}
138
139impl<'a, 'tcx> SpanEncoder for EncodeContext<'a, 'tcx> {
140 fn encode_crate_num(&mut self, crate_num: CrateNum) {
141 if crate_num != LOCAL_CRATE && self.is_proc_macro {
142 panic!("Attempted to encode non-local CrateNum {crate_num:?} for proc-macro crate");
143 }
144 self.emit_u32(crate_num.as_u32());
145 }
146
147 fn encode_def_index(&mut self, def_index: DefIndex) {
148 self.emit_u32(def_index.as_u32());
149 }
150
151 fn encode_def_id(&mut self, def_id: DefId) {
152 def_id.krate.encode(self);
153 def_id.index.encode(self);
154 }
155
156 fn encode_syntax_context(&mut self, syntax_context: SyntaxContext) {
157 rustc_span::hygiene::raw_encode_syntax_context(syntax_context, self.hygiene_ctxt, self);
158 }
159
160 fn encode_expn_id(&mut self, expn_id: ExpnId) {
161 if expn_id.krate == LOCAL_CRATE {
162 self.hygiene_ctxt.schedule_expn_data_for_encoding(expn_id);
167 }
168 expn_id.krate.encode(self);
169 expn_id.local_id.encode(self);
170 }
171
172 fn encode_span(&mut self, span: Span) {
173 match self.span_shorthands.entry(span) {
174 Entry::Occupied(o) => {
175 let last_location = *o.get();
178 let offset = self.opaque.position() - last_location;
181 if offset < last_location {
182 let needed = bytes_needed(offset);
183 SpanTag::indirect(true, needed as u8).encode(self);
184 self.opaque.write_with(|dest| {
185 *dest = offset.to_le_bytes();
186 needed
187 });
188 } else {
189 let needed = bytes_needed(last_location);
190 SpanTag::indirect(false, needed as u8).encode(self);
191 self.opaque.write_with(|dest| {
192 *dest = last_location.to_le_bytes();
193 needed
194 });
195 }
196 }
197 Entry::Vacant(v) => {
198 let position = self.opaque.position();
199 v.insert(position);
200 span.data().encode(self);
202 }
203 }
204 }
205
206 fn encode_symbol(&mut self, sym: Symbol) {
207 self.encode_symbol_or_byte_symbol(sym.as_u32(), |this| this.emit_str(sym.as_str()));
208 }
209
210 fn encode_byte_symbol(&mut self, byte_sym: ByteSymbol) {
211 self.encode_symbol_or_byte_symbol(byte_sym.as_u32(), |this| {
212 this.emit_byte_str(byte_sym.as_byte_str())
213 });
214 }
215}
216
217fn bytes_needed(n: usize) -> usize {
218 (usize::BITS - n.leading_zeros()).div_ceil(u8::BITS) as usize
219}
220
221impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for SpanData {
222 fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) {
223 let ctxt = if s.is_proc_macro { SyntaxContext::root() } else { self.ctxt };
255
256 if self.is_dummy() {
257 let tag = SpanTag::new(SpanKind::Partial, ctxt, 0);
258 tag.encode(s);
259 if tag.context().is_none() {
260 ctxt.encode(s);
261 }
262 return;
263 }
264
265 debug_assert!(self.lo <= self.hi);
267
268 if !s.source_file_cache.0.contains(self.lo) {
269 let source_map = s.tcx.sess.source_map();
270 let source_file_index = source_map.lookup_source_file_idx(self.lo);
271 s.source_file_cache =
272 (Arc::clone(&source_map.files()[source_file_index]), source_file_index);
273 }
274 let (ref source_file, source_file_index) = s.source_file_cache;
275 debug_assert!(source_file.contains(self.lo));
276
277 if !source_file.contains(self.hi) {
278 let tag = SpanTag::new(SpanKind::Partial, ctxt, 0);
281 tag.encode(s);
282 if tag.context().is_none() {
283 ctxt.encode(s);
284 }
285 return;
286 }
287
288 let (kind, metadata_index) = if source_file.is_imported() && !s.is_proc_macro {
305 let metadata_index = {
315 match &*source_file.external_src.read() {
317 ExternalSource::Foreign { metadata_index, .. } => *metadata_index,
318 src => panic!("Unexpected external source {src:?}"),
319 }
320 };
321
322 (SpanKind::Foreign, metadata_index)
323 } else {
324 let source_files =
326 s.required_source_files.as_mut().expect("Already encoded SourceMap!");
327 let (metadata_index, _) = source_files.insert_full(source_file_index);
328 let metadata_index: u32 =
329 metadata_index.try_into().expect("cannot export more than U32_MAX files");
330
331 (SpanKind::Local, metadata_index)
332 };
333
334 let lo = self.lo - source_file.start_pos;
337
338 let len = self.hi - self.lo;
341
342 let tag = SpanTag::new(kind, ctxt, len.0 as usize);
343 tag.encode(s);
344 if tag.context().is_none() {
345 ctxt.encode(s);
346 }
347 lo.encode(s);
348 if tag.length().is_none() {
349 len.encode(s);
350 }
351
352 metadata_index.encode(s);
354
355 if kind == SpanKind::Foreign {
356 let cnum = s.source_file_cache.0.cnum;
359 cnum.encode(s);
360 }
361 }
362}
363
364impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for [u8] {
365 fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) {
366 Encoder::emit_usize(e, self.len());
367 e.emit_raw_bytes(self);
368 }
369}
370
371impl<'a, 'tcx> TyEncoder<'tcx> for EncodeContext<'a, 'tcx> {
372 const CLEAR_CROSS_CRATE: bool = true;
373
374 fn position(&self) -> usize {
375 self.opaque.position()
376 }
377
378 fn type_shorthands(&mut self) -> &mut FxHashMap<Ty<'tcx>, usize> {
379 &mut self.type_shorthands
380 }
381
382 fn predicate_shorthands(&mut self) -> &mut FxHashMap<ty::PredicateKind<'tcx>, usize> {
383 &mut self.predicate_shorthands
384 }
385
386 fn encode_alloc_id(&mut self, alloc_id: &rustc_middle::mir::interpret::AllocId) {
387 let (index, _) = self.interpret_allocs.insert_full(*alloc_id);
388
389 index.encode(self);
390 }
391}
392
393macro_rules! record {
396 ($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => {{
397 {
398 let value = $value;
399 let lazy = $self.lazy(value);
400 $self.$tables.$table.set_some($def_id.index, lazy);
401 }
402 }};
403}
404
405macro_rules! record_array {
408 ($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => {{
409 {
410 let value = $value;
411 let lazy = $self.lazy_array(value);
412 $self.$tables.$table.set_some($def_id.index, lazy);
413 }
414 }};
415}
416
417macro_rules! record_defaulted_array {
418 ($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => {{
419 {
420 let value = $value;
421 let lazy = $self.lazy_array(value);
422 $self.$tables.$table.set($def_id.index, lazy);
423 }
424 }};
425}
426
427impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
428 fn emit_lazy_distance(&mut self, position: NonZero<usize>) {
429 let pos = position.get();
430 let distance = match self.lazy_state {
431 LazyState::NoNode => bug!("emit_lazy_distance: outside of a metadata node"),
432 LazyState::NodeStart(start) => {
433 let start = start.get();
434 assert!(pos <= start);
435 start - pos
436 }
437 LazyState::Previous(last_pos) => {
438 assert!(
439 last_pos <= position,
440 "make sure that the calls to `lazy*` \
441 are in the same order as the metadata fields",
442 );
443 position.get() - last_pos.get()
444 }
445 };
446 self.lazy_state = LazyState::Previous(NonZero::new(pos).unwrap());
447 self.emit_usize(distance);
448 }
449
450 fn lazy<T: ParameterizedOverTcx, B: Borrow<T::Value<'tcx>>>(&mut self, value: B) -> LazyValue<T>
451 where
452 T::Value<'tcx>: Encodable<EncodeContext<'a, 'tcx>>,
453 {
454 let pos = NonZero::new(self.position()).unwrap();
455
456 assert_eq!(self.lazy_state, LazyState::NoNode);
457 self.lazy_state = LazyState::NodeStart(pos);
458 value.borrow().encode(self);
459 self.lazy_state = LazyState::NoNode;
460
461 assert!(pos.get() <= self.position());
462
463 LazyValue::from_position(pos)
464 }
465
466 fn lazy_array<T: ParameterizedOverTcx, I: IntoIterator<Item = B>, B: Borrow<T::Value<'tcx>>>(
467 &mut self,
468 values: I,
469 ) -> LazyArray<T>
470 where
471 T::Value<'tcx>: Encodable<EncodeContext<'a, 'tcx>>,
472 {
473 let pos = NonZero::new(self.position()).unwrap();
474
475 assert_eq!(self.lazy_state, LazyState::NoNode);
476 self.lazy_state = LazyState::NodeStart(pos);
477 let len = values.into_iter().map(|value| value.borrow().encode(self)).count();
478 self.lazy_state = LazyState::NoNode;
479
480 assert!(pos.get() <= self.position());
481
482 LazyArray::from_position_and_num_elems(pos, len)
483 }
484
485 fn encode_symbol_or_byte_symbol(
486 &mut self,
487 index: u32,
488 emit_str_or_byte_str: impl Fn(&mut Self),
489 ) {
490 if Symbol::is_predefined(index) {
492 self.opaque.emit_u8(SYMBOL_PREDEFINED);
493 self.opaque.emit_u32(index);
494 } else {
495 match self.symbol_index_table.entry(index) {
497 Entry::Vacant(o) => {
498 self.opaque.emit_u8(SYMBOL_STR);
499 let pos = self.opaque.position();
500 o.insert(pos);
501 emit_str_or_byte_str(self);
502 }
503 Entry::Occupied(o) => {
504 let x = *o.get();
505 self.emit_u8(SYMBOL_OFFSET);
506 self.emit_usize(x);
507 }
508 }
509 }
510 }
511
512 fn encode_def_path_table(&mut self) {
513 let table = self.tcx.def_path_table();
514 if self.is_proc_macro {
515 for def_index in std::iter::once(CRATE_DEF_INDEX)
516 .chain(self.tcx.resolutions(()).proc_macros.iter().map(|p| p.local_def_index))
517 {
518 let def_key = self.lazy(table.def_key(def_index));
519 let def_path_hash = table.def_path_hash(def_index);
520 self.tables.def_keys.set_some(def_index, def_key);
521 self.tables.def_path_hashes.set(def_index, def_path_hash.local_hash().as_u64());
522 }
523 } else {
524 for (def_index, def_key, def_path_hash) in table.enumerated_keys_and_path_hashes() {
525 let def_key = self.lazy(def_key);
526 self.tables.def_keys.set_some(def_index, def_key);
527 self.tables.def_path_hashes.set(def_index, def_path_hash.local_hash().as_u64());
528 }
529 }
530 }
531
532 fn encode_def_path_hash_map(&mut self) -> LazyValue<DefPathHashMapRef<'static>> {
533 self.lazy(DefPathHashMapRef::BorrowedFromTcx(self.tcx.def_path_hash_to_def_index_map()))
534 }
535
536 fn encode_source_map(&mut self) -> LazyTable<u32, Option<LazyValue<rustc_span::SourceFile>>> {
537 let source_map = self.tcx.sess.source_map();
538 let all_source_files = source_map.files();
539
540 let required_source_files = self.required_source_files.take().unwrap();
544
545 let working_directory = &self.tcx.sess.opts.working_dir;
546
547 let mut adapted = TableBuilder::default();
548
549 let local_crate_stable_id = self.tcx.stable_crate_id(LOCAL_CRATE);
550
551 for (on_disk_index, &source_file_index) in required_source_files.iter().enumerate() {
556 let source_file = &all_source_files[source_file_index];
557 assert!(!source_file.is_imported() || self.is_proc_macro);
559
560 let mut adapted_source_file = (**source_file).clone();
568
569 match source_file.name {
570 FileName::Real(ref original_file_name) => {
571 let adapted_file_name = source_map
572 .path_mapping()
573 .to_embeddable_absolute_path(original_file_name.clone(), working_directory);
574
575 adapted_source_file.name = FileName::Real(adapted_file_name);
576 }
577 _ => {
578 }
580 };
581
582 if self.is_proc_macro {
589 adapted_source_file.cnum = LOCAL_CRATE;
590 }
591
592 adapted_source_file.stable_id = StableSourceFileId::from_filename_for_export(
596 &adapted_source_file.name,
597 local_crate_stable_id,
598 );
599
600 let on_disk_index: u32 =
601 on_disk_index.try_into().expect("cannot export more than U32_MAX files");
602 adapted.set_some(on_disk_index, self.lazy(adapted_source_file));
603 }
604
605 adapted.encode(&mut self.opaque)
606 }
607
608 fn encode_crate_root(&mut self) -> LazyValue<CrateRoot> {
609 let tcx = self.tcx;
610 let mut stats: Vec<(&'static str, usize)> = Vec::with_capacity(32);
611
612 macro_rules! stat {
613 ($label:literal, $f:expr) => {{
614 let orig_pos = self.position();
615 let res = $f();
616 stats.push(($label, self.position() - orig_pos));
617 res
618 }};
619 }
620
621 stats.push(("preamble", self.position()));
623
624 let (crate_deps, dylib_dependency_formats) =
625 stat!("dep", || (self.encode_crate_deps(), self.encode_dylib_dependency_formats()));
626
627 let lib_features = stat!("lib-features", || self.encode_lib_features());
628
629 let stability_implications =
630 stat!("stability-implications", || self.encode_stability_implications());
631
632 let (lang_items, lang_items_missing) = stat!("lang-items", || {
633 (self.encode_lang_items(), self.encode_lang_items_missing())
634 });
635
636 let stripped_cfg_items = stat!("stripped-cfg-items", || self.encode_stripped_cfg_items());
637
638 let diagnostic_items = stat!("diagnostic-items", || self.encode_diagnostic_items());
639
640 let native_libraries = stat!("native-libs", || self.encode_native_libraries());
641
642 let foreign_modules = stat!("foreign-modules", || self.encode_foreign_modules());
643
644 _ = stat!("def-path-table", || self.encode_def_path_table());
645
646 let traits = stat!("traits", || self.encode_traits());
648
649 let impls = stat!("impls", || self.encode_impls());
651
652 let incoherent_impls = stat!("incoherent-impls", || self.encode_incoherent_impls());
653
654 _ = stat!("mir", || self.encode_mir());
655
656 _ = stat!("def-ids", || self.encode_def_ids());
657
658 let interpret_alloc_index = stat!("interpret-alloc-index", || {
659 let mut interpret_alloc_index = Vec::new();
660 let mut n = 0;
661 trace!("beginning to encode alloc ids");
662 loop {
663 let new_n = self.interpret_allocs.len();
664 if n == new_n {
666 break;
668 }
669 trace!("encoding {} further alloc ids", new_n - n);
670 for idx in n..new_n {
671 let id = self.interpret_allocs[idx];
672 let pos = self.position() as u64;
673 interpret_alloc_index.push(pos);
674 interpret::specialized_encode_alloc_id(self, tcx, id);
675 }
676 n = new_n;
677 }
678 self.lazy_array(interpret_alloc_index)
679 });
680
681 let proc_macro_data = stat!("proc-macro-data", || self.encode_proc_macros());
685
686 let tables = stat!("tables", || self.tables.encode(&mut self.opaque));
687
688 let debugger_visualizers =
689 stat!("debugger-visualizers", || self.encode_debugger_visualizers());
690
691 let exportable_items = stat!("exportable-items", || self.encode_exportable_items());
692
693 let stable_order_of_exportable_impls =
694 stat!("exportable-items", || self.encode_stable_order_of_exportable_impls());
695
696 let (exported_non_generic_symbols, exported_generic_symbols) =
698 stat!("exported-symbols", || {
699 (
700 self.encode_exported_symbols(tcx.exported_non_generic_symbols(LOCAL_CRATE)),
701 self.encode_exported_symbols(tcx.exported_generic_symbols(LOCAL_CRATE)),
702 )
703 });
704
705 let (syntax_contexts, expn_data, expn_hashes) = stat!("hygiene", || self.encode_hygiene());
712
713 let def_path_hash_map = stat!("def-path-hash-map", || self.encode_def_path_hash_map());
714
715 let source_map = stat!("source-map", || self.encode_source_map());
718 let target_modifiers = stat!("target-modifiers", || self.encode_target_modifiers());
719
720 let root = stat!("final", || {
721 let attrs = tcx.hir_krate_attrs();
722 self.lazy(CrateRoot {
723 header: CrateHeader {
724 name: tcx.crate_name(LOCAL_CRATE),
725 triple: tcx.sess.opts.target_triple.clone(),
726 hash: tcx.crate_hash(LOCAL_CRATE),
727 is_proc_macro_crate: proc_macro_data.is_some(),
728 is_stub: false,
729 },
730 extra_filename: tcx.sess.opts.cg.extra_filename.clone(),
731 stable_crate_id: tcx.def_path_hash(LOCAL_CRATE.as_def_id()).stable_crate_id(),
732 required_panic_strategy: tcx.required_panic_strategy(LOCAL_CRATE),
733 panic_in_drop_strategy: tcx.sess.opts.unstable_opts.panic_in_drop,
734 edition: tcx.sess.edition(),
735 has_global_allocator: tcx.has_global_allocator(LOCAL_CRATE),
736 has_alloc_error_handler: tcx.has_alloc_error_handler(LOCAL_CRATE),
737 has_panic_handler: tcx.has_panic_handler(LOCAL_CRATE),
738 has_default_lib_allocator: ast::attr::contains_name(
739 attrs,
740 sym::default_lib_allocator,
741 ),
742 proc_macro_data,
743 debugger_visualizers,
744 compiler_builtins: ast::attr::contains_name(attrs, sym::compiler_builtins),
745 needs_allocator: ast::attr::contains_name(attrs, sym::needs_allocator),
746 needs_panic_runtime: ast::attr::contains_name(attrs, sym::needs_panic_runtime),
747 no_builtins: ast::attr::contains_name(attrs, sym::no_builtins),
748 panic_runtime: ast::attr::contains_name(attrs, sym::panic_runtime),
749 profiler_runtime: ast::attr::contains_name(attrs, sym::profiler_runtime),
750 symbol_mangling_version: tcx.sess.opts.get_symbol_mangling_version(),
751
752 crate_deps,
753 dylib_dependency_formats,
754 lib_features,
755 stability_implications,
756 lang_items,
757 diagnostic_items,
758 lang_items_missing,
759 stripped_cfg_items,
760 native_libraries,
761 foreign_modules,
762 source_map,
763 target_modifiers,
764 traits,
765 impls,
766 incoherent_impls,
767 exportable_items,
768 stable_order_of_exportable_impls,
769 exported_non_generic_symbols,
770 exported_generic_symbols,
771 interpret_alloc_index,
772 tables,
773 syntax_contexts,
774 expn_data,
775 expn_hashes,
776 def_path_hash_map,
777 specialization_enabled_in: tcx.specialization_enabled_in(LOCAL_CRATE),
778 })
779 });
780
781 let total_bytes = self.position();
782
783 let computed_total_bytes: usize = stats.iter().map(|(_, size)| size).sum();
784 assert_eq!(total_bytes, computed_total_bytes);
785
786 if tcx.sess.opts.unstable_opts.meta_stats {
787 use std::fmt::Write;
788
789 self.opaque.flush();
790
791 let pos_before_rewind = self.opaque.file().stream_position().unwrap();
793 let mut zero_bytes = 0;
794 self.opaque.file().rewind().unwrap();
795 let file = std::io::BufReader::new(self.opaque.file());
796 for e in file.bytes() {
797 if e.unwrap() == 0 {
798 zero_bytes += 1;
799 }
800 }
801 assert_eq!(self.opaque.file().stream_position().unwrap(), pos_before_rewind);
802
803 stats.sort_by_key(|&(_, usize)| usize);
804 stats.reverse(); let prefix = "meta-stats";
807 let perc = |bytes| (bytes * 100) as f64 / total_bytes as f64;
808
809 let section_w = 23;
810 let size_w = 10;
811 let banner_w = 64;
812
813 let mut s = String::new();
819 _ = writeln!(s, "{prefix} {}", "=".repeat(banner_w));
820 _ = writeln!(s, "{prefix} METADATA STATS: {}", tcx.crate_name(LOCAL_CRATE));
821 _ = writeln!(s, "{prefix} {:<section_w$}{:>size_w$}", "Section", "Size");
822 _ = writeln!(s, "{prefix} {}", "-".repeat(banner_w));
823 for (label, size) in stats {
824 _ = writeln!(
825 s,
826 "{prefix} {:<section_w$}{:>size_w$} ({:4.1}%)",
827 label,
828 usize_with_underscores(size),
829 perc(size)
830 );
831 }
832 _ = writeln!(s, "{prefix} {}", "-".repeat(banner_w));
833 _ = writeln!(
834 s,
835 "{prefix} {:<section_w$}{:>size_w$} (of which {:.1}% are zero bytes)",
836 "Total",
837 usize_with_underscores(total_bytes),
838 perc(zero_bytes)
839 );
840 _ = writeln!(s, "{prefix} {}", "=".repeat(banner_w));
841 eprint!("{s}");
842 }
843
844 root
845 }
846}
847
848struct AnalyzeAttrState<'a> {
849 is_exported: bool,
850 is_doc_hidden: bool,
851 features: &'a Features,
852}
853
854#[inline]
864fn analyze_attr(attr: &hir::Attribute, state: &mut AnalyzeAttrState<'_>) -> bool {
865 let mut should_encode = false;
866 if let hir::Attribute::Parsed(p) = attr
867 && p.encode_cross_crate() == EncodeCrossCrate::No
868 {
869 } else if let Some(name) = attr.name()
871 && !rustc_feature::encode_cross_crate(name)
872 {
873 } else if attr.doc_str().is_some() {
875 if state.is_exported {
879 should_encode = true;
880 }
881 } else if attr.has_name(sym::doc) {
882 if let Some(item_list) = attr.meta_item_list() {
885 for item in item_list {
886 if !item.has_name(sym::inline) {
887 should_encode = true;
888 if item.has_name(sym::hidden) {
889 state.is_doc_hidden = true;
890 break;
891 }
892 }
893 }
894 }
895 } else if let &[sym::diagnostic, seg] = &*attr.path() {
896 should_encode = rustc_feature::is_stable_diagnostic_attribute(seg, state.features);
897 } else {
898 should_encode = true;
899 }
900 should_encode
901}
902
903fn should_encode_span(def_kind: DefKind) -> bool {
904 match def_kind {
905 DefKind::Mod
906 | DefKind::Struct
907 | DefKind::Union
908 | DefKind::Enum
909 | DefKind::Variant
910 | DefKind::Trait
911 | DefKind::TyAlias
912 | DefKind::ForeignTy
913 | DefKind::TraitAlias
914 | DefKind::AssocTy
915 | DefKind::TyParam
916 | DefKind::ConstParam
917 | DefKind::LifetimeParam
918 | DefKind::Fn
919 | DefKind::Const
920 | DefKind::Static { .. }
921 | DefKind::Ctor(..)
922 | DefKind::AssocFn
923 | DefKind::AssocConst
924 | DefKind::Macro(_)
925 | DefKind::ExternCrate
926 | DefKind::Use
927 | DefKind::AnonConst
928 | DefKind::InlineConst
929 | DefKind::OpaqueTy
930 | DefKind::Field
931 | DefKind::Impl { .. }
932 | DefKind::Closure
933 | DefKind::SyntheticCoroutineBody => true,
934 DefKind::ForeignMod | DefKind::GlobalAsm => false,
935 }
936}
937
938fn should_encode_attrs(def_kind: DefKind) -> bool {
939 match def_kind {
940 DefKind::Mod
941 | DefKind::Struct
942 | DefKind::Union
943 | DefKind::Enum
944 | DefKind::Variant
945 | DefKind::Trait
946 | DefKind::TyAlias
947 | DefKind::ForeignTy
948 | DefKind::TraitAlias
949 | DefKind::AssocTy
950 | DefKind::Fn
951 | DefKind::Const
952 | DefKind::Static { nested: false, .. }
953 | DefKind::AssocFn
954 | DefKind::AssocConst
955 | DefKind::Macro(_)
956 | DefKind::Field
957 | DefKind::Impl { .. } => true,
958 DefKind::Closure => true,
963 DefKind::SyntheticCoroutineBody => false,
964 DefKind::TyParam
965 | DefKind::ConstParam
966 | DefKind::Ctor(..)
967 | DefKind::ExternCrate
968 | DefKind::Use
969 | DefKind::ForeignMod
970 | DefKind::AnonConst
971 | DefKind::InlineConst
972 | DefKind::OpaqueTy
973 | DefKind::LifetimeParam
974 | DefKind::Static { nested: true, .. }
975 | DefKind::GlobalAsm => false,
976 }
977}
978
979fn should_encode_expn_that_defined(def_kind: DefKind) -> bool {
980 match def_kind {
981 DefKind::Mod
982 | DefKind::Struct
983 | DefKind::Union
984 | DefKind::Enum
985 | DefKind::Variant
986 | DefKind::Trait
987 | DefKind::Impl { .. } => true,
988 DefKind::TyAlias
989 | DefKind::ForeignTy
990 | DefKind::TraitAlias
991 | DefKind::AssocTy
992 | DefKind::TyParam
993 | DefKind::Fn
994 | DefKind::Const
995 | DefKind::ConstParam
996 | DefKind::Static { .. }
997 | DefKind::Ctor(..)
998 | DefKind::AssocFn
999 | DefKind::AssocConst
1000 | DefKind::Macro(_)
1001 | DefKind::ExternCrate
1002 | DefKind::Use
1003 | DefKind::ForeignMod
1004 | DefKind::AnonConst
1005 | DefKind::InlineConst
1006 | DefKind::OpaqueTy
1007 | DefKind::Field
1008 | DefKind::LifetimeParam
1009 | DefKind::GlobalAsm
1010 | DefKind::Closure
1011 | DefKind::SyntheticCoroutineBody => false,
1012 }
1013}
1014
1015fn should_encode_visibility(def_kind: DefKind) -> bool {
1016 match def_kind {
1017 DefKind::Mod
1018 | DefKind::Struct
1019 | DefKind::Union
1020 | DefKind::Enum
1021 | DefKind::Variant
1022 | DefKind::Trait
1023 | DefKind::TyAlias
1024 | DefKind::ForeignTy
1025 | DefKind::TraitAlias
1026 | DefKind::AssocTy
1027 | DefKind::Fn
1028 | DefKind::Const
1029 | DefKind::Static { nested: false, .. }
1030 | DefKind::Ctor(..)
1031 | DefKind::AssocFn
1032 | DefKind::AssocConst
1033 | DefKind::Macro(..)
1034 | DefKind::Field => true,
1035 DefKind::Use
1036 | DefKind::ForeignMod
1037 | DefKind::TyParam
1038 | DefKind::ConstParam
1039 | DefKind::LifetimeParam
1040 | DefKind::AnonConst
1041 | DefKind::InlineConst
1042 | DefKind::Static { nested: true, .. }
1043 | DefKind::OpaqueTy
1044 | DefKind::GlobalAsm
1045 | DefKind::Impl { .. }
1046 | DefKind::Closure
1047 | DefKind::ExternCrate
1048 | DefKind::SyntheticCoroutineBody => false,
1049 }
1050}
1051
1052fn should_encode_stability(def_kind: DefKind) -> bool {
1053 match def_kind {
1054 DefKind::Mod
1055 | DefKind::Ctor(..)
1056 | DefKind::Variant
1057 | DefKind::Field
1058 | DefKind::Struct
1059 | DefKind::AssocTy
1060 | DefKind::AssocFn
1061 | DefKind::AssocConst
1062 | DefKind::TyParam
1063 | DefKind::ConstParam
1064 | DefKind::Static { .. }
1065 | DefKind::Const
1066 | DefKind::Fn
1067 | DefKind::ForeignMod
1068 | DefKind::TyAlias
1069 | DefKind::OpaqueTy
1070 | DefKind::Enum
1071 | DefKind::Union
1072 | DefKind::Impl { .. }
1073 | DefKind::Trait
1074 | DefKind::TraitAlias
1075 | DefKind::Macro(..)
1076 | DefKind::ForeignTy => true,
1077 DefKind::Use
1078 | DefKind::LifetimeParam
1079 | DefKind::AnonConst
1080 | DefKind::InlineConst
1081 | DefKind::GlobalAsm
1082 | DefKind::Closure
1083 | DefKind::ExternCrate
1084 | DefKind::SyntheticCoroutineBody => false,
1085 }
1086}
1087
1088fn should_encode_mir(
1109 tcx: TyCtxt<'_>,
1110 reachable_set: &LocalDefIdSet,
1111 def_id: LocalDefId,
1112) -> (bool, bool) {
1113 match tcx.def_kind(def_id) {
1114 DefKind::Ctor(_, _) => {
1116 let mir_opt_base = tcx.sess.opts.output_types.should_codegen()
1117 || tcx.sess.opts.unstable_opts.always_encode_mir;
1118 (true, mir_opt_base)
1119 }
1120 DefKind::AnonConst | DefKind::InlineConst | DefKind::AssocConst | DefKind::Const => {
1122 (true, false)
1123 }
1124 DefKind::Closure if tcx.is_coroutine(def_id.to_def_id()) => (false, true),
1126 DefKind::SyntheticCoroutineBody => (false, true),
1127 DefKind::AssocFn | DefKind::Fn | DefKind::Closure => {
1129 let generics = tcx.generics_of(def_id);
1130 let opt = tcx.sess.opts.unstable_opts.always_encode_mir
1131 || (tcx.sess.opts.output_types.should_codegen()
1132 && reachable_set.contains(&def_id)
1133 && (generics.requires_monomorphization(tcx)
1134 || tcx.cross_crate_inlinable(def_id)));
1135 let is_const_fn = tcx.is_const_fn(def_id.to_def_id())
1137 || tcx.is_const_default_method(def_id.to_def_id());
1138 (is_const_fn, opt)
1139 }
1140 _ => (false, false),
1142 }
1143}
1144
1145fn should_encode_variances<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, def_kind: DefKind) -> bool {
1146 match def_kind {
1147 DefKind::Struct
1148 | DefKind::Union
1149 | DefKind::Enum
1150 | DefKind::OpaqueTy
1151 | DefKind::Fn
1152 | DefKind::Ctor(..)
1153 | DefKind::AssocFn => true,
1154 DefKind::AssocTy => {
1155 matches!(tcx.opt_rpitit_info(def_id), Some(ty::ImplTraitInTraitData::Trait { .. }))
1157 }
1158 DefKind::Mod
1159 | DefKind::Variant
1160 | DefKind::Field
1161 | DefKind::AssocConst
1162 | DefKind::TyParam
1163 | DefKind::ConstParam
1164 | DefKind::Static { .. }
1165 | DefKind::Const
1166 | DefKind::ForeignMod
1167 | DefKind::Impl { .. }
1168 | DefKind::Trait
1169 | DefKind::TraitAlias
1170 | DefKind::Macro(..)
1171 | DefKind::ForeignTy
1172 | DefKind::Use
1173 | DefKind::LifetimeParam
1174 | DefKind::AnonConst
1175 | DefKind::InlineConst
1176 | DefKind::GlobalAsm
1177 | DefKind::Closure
1178 | DefKind::ExternCrate
1179 | DefKind::SyntheticCoroutineBody => false,
1180 DefKind::TyAlias => tcx.type_alias_is_lazy(def_id),
1181 }
1182}
1183
1184fn should_encode_generics(def_kind: DefKind) -> bool {
1185 match def_kind {
1186 DefKind::Struct
1187 | DefKind::Union
1188 | DefKind::Enum
1189 | DefKind::Variant
1190 | DefKind::Trait
1191 | DefKind::TyAlias
1192 | DefKind::ForeignTy
1193 | DefKind::TraitAlias
1194 | DefKind::AssocTy
1195 | DefKind::Fn
1196 | DefKind::Const
1197 | DefKind::Static { .. }
1198 | DefKind::Ctor(..)
1199 | DefKind::AssocFn
1200 | DefKind::AssocConst
1201 | DefKind::AnonConst
1202 | DefKind::InlineConst
1203 | DefKind::OpaqueTy
1204 | DefKind::Impl { .. }
1205 | DefKind::Field
1206 | DefKind::TyParam
1207 | DefKind::Closure
1208 | DefKind::SyntheticCoroutineBody => true,
1209 DefKind::Mod
1210 | DefKind::ForeignMod
1211 | DefKind::ConstParam
1212 | DefKind::Macro(..)
1213 | DefKind::Use
1214 | DefKind::LifetimeParam
1215 | DefKind::GlobalAsm
1216 | DefKind::ExternCrate => false,
1217 }
1218}
1219
1220fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) -> bool {
1221 match def_kind {
1222 DefKind::Struct
1223 | DefKind::Union
1224 | DefKind::Enum
1225 | DefKind::Variant
1226 | DefKind::Ctor(..)
1227 | DefKind::Field
1228 | DefKind::Fn
1229 | DefKind::Const
1230 | DefKind::Static { nested: false, .. }
1231 | DefKind::TyAlias
1232 | DefKind::ForeignTy
1233 | DefKind::Impl { .. }
1234 | DefKind::AssocFn
1235 | DefKind::AssocConst
1236 | DefKind::Closure
1237 | DefKind::ConstParam
1238 | DefKind::AnonConst
1239 | DefKind::InlineConst
1240 | DefKind::SyntheticCoroutineBody => true,
1241
1242 DefKind::OpaqueTy => {
1243 let origin = tcx.local_opaque_ty_origin(def_id);
1244 if let hir::OpaqueTyOrigin::FnReturn { parent, .. }
1245 | hir::OpaqueTyOrigin::AsyncFn { parent, .. } = origin
1246 && let hir::Node::TraitItem(trait_item) = tcx.hir_node_by_def_id(parent)
1247 && let (_, hir::TraitFn::Required(..)) = trait_item.expect_fn()
1248 {
1249 false
1250 } else {
1251 true
1252 }
1253 }
1254
1255 DefKind::AssocTy => {
1256 let assoc_item = tcx.associated_item(def_id);
1257 match assoc_item.container {
1258 ty::AssocItemContainer::Impl => true,
1259 ty::AssocItemContainer::Trait => assoc_item.defaultness(tcx).has_value(),
1260 }
1261 }
1262 DefKind::TyParam => {
1263 let hir::Node::GenericParam(param) = tcx.hir_node_by_def_id(def_id) else { bug!() };
1264 let hir::GenericParamKind::Type { default, .. } = param.kind else { bug!() };
1265 default.is_some()
1266 }
1267
1268 DefKind::Trait
1269 | DefKind::TraitAlias
1270 | DefKind::Mod
1271 | DefKind::ForeignMod
1272 | DefKind::Macro(..)
1273 | DefKind::Static { nested: true, .. }
1274 | DefKind::Use
1275 | DefKind::LifetimeParam
1276 | DefKind::GlobalAsm
1277 | DefKind::ExternCrate => false,
1278 }
1279}
1280
1281fn should_encode_fn_sig(def_kind: DefKind) -> bool {
1282 match def_kind {
1283 DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fn) => true,
1284
1285 DefKind::Struct
1286 | DefKind::Union
1287 | DefKind::Enum
1288 | DefKind::Variant
1289 | DefKind::Field
1290 | DefKind::Const
1291 | DefKind::Static { .. }
1292 | DefKind::Ctor(..)
1293 | DefKind::TyAlias
1294 | DefKind::OpaqueTy
1295 | DefKind::ForeignTy
1296 | DefKind::Impl { .. }
1297 | DefKind::AssocConst
1298 | DefKind::Closure
1299 | DefKind::ConstParam
1300 | DefKind::AnonConst
1301 | DefKind::InlineConst
1302 | DefKind::AssocTy
1303 | DefKind::TyParam
1304 | DefKind::Trait
1305 | DefKind::TraitAlias
1306 | DefKind::Mod
1307 | DefKind::ForeignMod
1308 | DefKind::Macro(..)
1309 | DefKind::Use
1310 | DefKind::LifetimeParam
1311 | DefKind::GlobalAsm
1312 | DefKind::ExternCrate
1313 | DefKind::SyntheticCoroutineBody => false,
1314 }
1315}
1316
1317fn should_encode_constness(def_kind: DefKind) -> bool {
1318 match def_kind {
1319 DefKind::Fn | DefKind::AssocFn | DefKind::Closure | DefKind::Ctor(_, CtorKind::Fn) => true,
1320
1321 DefKind::Struct
1322 | DefKind::Union
1323 | DefKind::Enum
1324 | DefKind::Field
1325 | DefKind::Const
1326 | DefKind::AssocConst
1327 | DefKind::AnonConst
1328 | DefKind::Static { .. }
1329 | DefKind::TyAlias
1330 | DefKind::OpaqueTy
1331 | DefKind::Impl { .. }
1332 | DefKind::ForeignTy
1333 | DefKind::ConstParam
1334 | DefKind::InlineConst
1335 | DefKind::AssocTy
1336 | DefKind::TyParam
1337 | DefKind::Trait
1338 | DefKind::TraitAlias
1339 | DefKind::Mod
1340 | DefKind::ForeignMod
1341 | DefKind::Macro(..)
1342 | DefKind::Use
1343 | DefKind::LifetimeParam
1344 | DefKind::GlobalAsm
1345 | DefKind::ExternCrate
1346 | DefKind::Ctor(_, CtorKind::Const)
1347 | DefKind::Variant
1348 | DefKind::SyntheticCoroutineBody => false,
1349 }
1350}
1351
1352fn should_encode_const(def_kind: DefKind) -> bool {
1353 match def_kind {
1354 DefKind::Const | DefKind::AssocConst | DefKind::AnonConst | DefKind::InlineConst => true,
1355
1356 DefKind::Struct
1357 | DefKind::Union
1358 | DefKind::Enum
1359 | DefKind::Variant
1360 | DefKind::Ctor(..)
1361 | DefKind::Field
1362 | DefKind::Fn
1363 | DefKind::Static { .. }
1364 | DefKind::TyAlias
1365 | DefKind::OpaqueTy
1366 | DefKind::ForeignTy
1367 | DefKind::Impl { .. }
1368 | DefKind::AssocFn
1369 | DefKind::Closure
1370 | DefKind::ConstParam
1371 | DefKind::AssocTy
1372 | DefKind::TyParam
1373 | DefKind::Trait
1374 | DefKind::TraitAlias
1375 | DefKind::Mod
1376 | DefKind::ForeignMod
1377 | DefKind::Macro(..)
1378 | DefKind::Use
1379 | DefKind::LifetimeParam
1380 | DefKind::GlobalAsm
1381 | DefKind::ExternCrate
1382 | DefKind::SyntheticCoroutineBody => false,
1383 }
1384}
1385
1386impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
1387 fn encode_attrs(&mut self, def_id: LocalDefId) {
1388 let tcx = self.tcx;
1389 let mut state = AnalyzeAttrState {
1390 is_exported: tcx.effective_visibilities(()).is_exported(def_id),
1391 is_doc_hidden: false,
1392 features: &tcx.features(),
1393 };
1394 let attr_iter = tcx
1395 .hir_attrs(tcx.local_def_id_to_hir_id(def_id))
1396 .iter()
1397 .filter(|attr| analyze_attr(*attr, &mut state));
1398
1399 record_array!(self.tables.attributes[def_id.to_def_id()] <- attr_iter);
1400
1401 let mut attr_flags = AttrFlags::empty();
1402 if state.is_doc_hidden {
1403 attr_flags |= AttrFlags::IS_DOC_HIDDEN;
1404 }
1405 self.tables.attr_flags.set(def_id.local_def_index, attr_flags);
1406 }
1407
1408 fn encode_def_ids(&mut self) {
1409 self.encode_info_for_mod(CRATE_DEF_ID);
1410
1411 if self.is_proc_macro {
1414 return;
1415 }
1416
1417 let tcx = self.tcx;
1418
1419 for local_id in tcx.iter_local_def_id() {
1420 let def_id = local_id.to_def_id();
1421 let def_kind = tcx.def_kind(local_id);
1422 self.tables.def_kind.set_some(def_id.index, def_kind);
1423
1424 if def_kind == DefKind::AnonConst
1429 && match tcx.hir_node_by_def_id(local_id) {
1430 hir::Node::ConstArg(hir::ConstArg { kind, .. }) => match kind {
1431 hir::ConstArgKind::Path(..) | hir::ConstArgKind::Infer(..) => true,
1433 hir::ConstArgKind::Anon(..) => false,
1434 },
1435 _ => false,
1436 }
1437 {
1438 continue;
1439 }
1440
1441 if def_kind == DefKind::Field
1442 && let hir::Node::Field(field) = tcx.hir_node_by_def_id(local_id)
1443 && let Some(anon) = field.default
1444 {
1445 record!(self.tables.default_fields[def_id] <- anon.def_id.to_def_id());
1446 }
1447
1448 if should_encode_span(def_kind) {
1449 let def_span = tcx.def_span(local_id);
1450 record!(self.tables.def_span[def_id] <- def_span);
1451 }
1452 if should_encode_attrs(def_kind) {
1453 self.encode_attrs(local_id);
1454 }
1455 if should_encode_expn_that_defined(def_kind) {
1456 record!(self.tables.expn_that_defined[def_id] <- self.tcx.expn_that_defined(def_id));
1457 }
1458 if should_encode_span(def_kind)
1459 && let Some(ident_span) = tcx.def_ident_span(def_id)
1460 {
1461 record!(self.tables.def_ident_span[def_id] <- ident_span);
1462 }
1463 if def_kind.has_codegen_attrs() {
1464 record!(self.tables.codegen_fn_attrs[def_id] <- self.tcx.codegen_fn_attrs(def_id));
1465 }
1466 if should_encode_visibility(def_kind) {
1467 let vis =
1468 self.tcx.local_visibility(local_id).map_id(|def_id| def_id.local_def_index);
1469 record!(self.tables.visibility[def_id] <- vis);
1470 }
1471 if should_encode_stability(def_kind) {
1472 self.encode_stability(def_id);
1473 self.encode_const_stability(def_id);
1474 self.encode_default_body_stability(def_id);
1475 self.encode_deprecation(def_id);
1476 }
1477 if should_encode_variances(tcx, def_id, def_kind) {
1478 let v = self.tcx.variances_of(def_id);
1479 record_array!(self.tables.variances_of[def_id] <- v);
1480 }
1481 if should_encode_fn_sig(def_kind) {
1482 record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
1483 }
1484 if should_encode_generics(def_kind) {
1485 let g = tcx.generics_of(def_id);
1486 record!(self.tables.generics_of[def_id] <- g);
1487 record!(self.tables.explicit_predicates_of[def_id] <- self.tcx.explicit_predicates_of(def_id));
1488 let inferred_outlives = self.tcx.inferred_outlives_of(def_id);
1489 record_defaulted_array!(self.tables.inferred_outlives_of[def_id] <- inferred_outlives);
1490
1491 for param in &g.own_params {
1492 if let ty::GenericParamDefKind::Const { has_default: true, .. } = param.kind {
1493 let default = self.tcx.const_param_default(param.def_id);
1494 record!(self.tables.const_param_default[param.def_id] <- default);
1495 }
1496 }
1497 }
1498 if tcx.is_conditionally_const(def_id) {
1499 record!(self.tables.const_conditions[def_id] <- self.tcx.const_conditions(def_id));
1500 }
1501 if should_encode_type(tcx, local_id, def_kind) {
1502 record!(self.tables.type_of[def_id] <- self.tcx.type_of(def_id));
1503 }
1504 if should_encode_constness(def_kind) {
1505 self.tables.constness.set_some(def_id.index, self.tcx.constness(def_id));
1506 }
1507 if let DefKind::Fn | DefKind::AssocFn = def_kind {
1508 self.tables.asyncness.set_some(def_id.index, tcx.asyncness(def_id));
1509 record_array!(self.tables.fn_arg_idents[def_id] <- tcx.fn_arg_idents(def_id));
1510 }
1511 if let Some(name) = tcx.intrinsic(def_id) {
1512 record!(self.tables.intrinsic[def_id] <- name);
1513 }
1514 if let DefKind::TyParam = def_kind {
1515 let default = self.tcx.object_lifetime_default(def_id);
1516 record!(self.tables.object_lifetime_default[def_id] <- default);
1517 }
1518 if let DefKind::Trait = def_kind {
1519 record!(self.tables.trait_def[def_id] <- self.tcx.trait_def(def_id));
1520 record_defaulted_array!(self.tables.explicit_super_predicates_of[def_id] <-
1521 self.tcx.explicit_super_predicates_of(def_id).skip_binder());
1522 record_defaulted_array!(self.tables.explicit_implied_predicates_of[def_id] <-
1523 self.tcx.explicit_implied_predicates_of(def_id).skip_binder());
1524 let module_children = self.tcx.module_children_local(local_id);
1525 record_array!(self.tables.module_children_non_reexports[def_id] <-
1526 module_children.iter().map(|child| child.res.def_id().index));
1527 if self.tcx.is_const_trait(def_id) {
1528 record_defaulted_array!(self.tables.explicit_implied_const_bounds[def_id]
1529 <- self.tcx.explicit_implied_const_bounds(def_id).skip_binder());
1530 }
1531 }
1532 if let DefKind::TraitAlias = def_kind {
1533 record!(self.tables.trait_def[def_id] <- self.tcx.trait_def(def_id));
1534 record_defaulted_array!(self.tables.explicit_super_predicates_of[def_id] <-
1535 self.tcx.explicit_super_predicates_of(def_id).skip_binder());
1536 record_defaulted_array!(self.tables.explicit_implied_predicates_of[def_id] <-
1537 self.tcx.explicit_implied_predicates_of(def_id).skip_binder());
1538 }
1539 if let DefKind::Trait | DefKind::Impl { .. } = def_kind {
1540 let associated_item_def_ids = self.tcx.associated_item_def_ids(def_id);
1541 record_array!(self.tables.associated_item_or_field_def_ids[def_id] <-
1542 associated_item_def_ids.iter().map(|&def_id| {
1543 assert!(def_id.is_local());
1544 def_id.index
1545 })
1546 );
1547 for &def_id in associated_item_def_ids {
1548 self.encode_info_for_assoc_item(def_id);
1549 }
1550 }
1551 if let DefKind::Closure | DefKind::SyntheticCoroutineBody = def_kind
1552 && let Some(coroutine_kind) = self.tcx.coroutine_kind(def_id)
1553 {
1554 self.tables.coroutine_kind.set(def_id.index, Some(coroutine_kind))
1555 }
1556 if def_kind == DefKind::Closure
1557 && tcx.type_of(def_id).skip_binder().is_coroutine_closure()
1558 {
1559 let coroutine_for_closure = self.tcx.coroutine_for_closure(def_id);
1560 self.tables
1561 .coroutine_for_closure
1562 .set_some(def_id.index, coroutine_for_closure.into());
1563
1564 if tcx.needs_coroutine_by_move_body_def_id(coroutine_for_closure) {
1566 self.tables.coroutine_by_move_body_def_id.set_some(
1567 coroutine_for_closure.index,
1568 self.tcx.coroutine_by_move_body_def_id(coroutine_for_closure).into(),
1569 );
1570 }
1571 }
1572 if let DefKind::Static { .. } = def_kind {
1573 if !self.tcx.is_foreign_item(def_id) {
1574 let data = self.tcx.eval_static_initializer(def_id).unwrap();
1575 record!(self.tables.eval_static_initializer[def_id] <- data);
1576 }
1577 }
1578 if let DefKind::Enum | DefKind::Struct | DefKind::Union = def_kind {
1579 self.encode_info_for_adt(local_id);
1580 }
1581 if let DefKind::Mod = def_kind {
1582 self.encode_info_for_mod(local_id);
1583 }
1584 if let DefKind::Macro(_) = def_kind {
1585 self.encode_info_for_macro(local_id);
1586 }
1587 if let DefKind::TyAlias = def_kind {
1588 self.tables
1589 .type_alias_is_lazy
1590 .set(def_id.index, self.tcx.type_alias_is_lazy(def_id));
1591 }
1592 if let DefKind::OpaqueTy = def_kind {
1593 self.encode_explicit_item_bounds(def_id);
1594 self.encode_explicit_item_self_bounds(def_id);
1595 record!(self.tables.opaque_ty_origin[def_id] <- self.tcx.opaque_ty_origin(def_id));
1596 self.encode_precise_capturing_args(def_id);
1597 if tcx.is_conditionally_const(def_id) {
1598 record_defaulted_array!(self.tables.explicit_implied_const_bounds[def_id]
1599 <- tcx.explicit_implied_const_bounds(def_id).skip_binder());
1600 }
1601 }
1602 if let DefKind::AnonConst = def_kind {
1603 record!(self.tables.anon_const_kind[def_id] <- self.tcx.anon_const_kind(def_id));
1604 }
1605 if tcx.impl_method_has_trait_impl_trait_tys(def_id)
1606 && let Ok(table) = self.tcx.collect_return_position_impl_trait_in_trait_tys(def_id)
1607 {
1608 record!(self.tables.trait_impl_trait_tys[def_id] <- table);
1609 }
1610 if let DefKind::Impl { .. } | DefKind::Trait = def_kind {
1611 let table = tcx.associated_types_for_impl_traits_in_trait_or_impl(def_id);
1612 record!(self.tables.associated_types_for_impl_traits_in_trait_or_impl[def_id] <- table);
1613 }
1614 }
1615
1616 for (def_id, impls) in &tcx.crate_inherent_impls(()).0.inherent_impls {
1617 record_defaulted_array!(self.tables.inherent_impls[def_id.to_def_id()] <- impls.iter().map(|def_id| {
1618 assert!(def_id.is_local());
1619 def_id.index
1620 }));
1621 }
1622
1623 for (def_id, res_map) in &tcx.resolutions(()).doc_link_resolutions {
1624 record!(self.tables.doc_link_resolutions[def_id.to_def_id()] <- res_map);
1625 }
1626
1627 for (def_id, traits) in &tcx.resolutions(()).doc_link_traits_in_scope {
1628 record_array!(self.tables.doc_link_traits_in_scope[def_id.to_def_id()] <- traits);
1629 }
1630 }
1631
1632 #[instrument(level = "trace", skip(self))]
1633 fn encode_info_for_adt(&mut self, local_def_id: LocalDefId) {
1634 let def_id = local_def_id.to_def_id();
1635 let tcx = self.tcx;
1636 let adt_def = tcx.adt_def(def_id);
1637 record!(self.tables.repr_options[def_id] <- adt_def.repr());
1638
1639 let params_in_repr = self.tcx.params_in_repr(def_id);
1640 record!(self.tables.params_in_repr[def_id] <- params_in_repr);
1641
1642 if adt_def.is_enum() {
1643 let module_children = tcx.module_children_local(local_def_id);
1644 record_array!(self.tables.module_children_non_reexports[def_id] <-
1645 module_children.iter().map(|child| child.res.def_id().index));
1646 } else {
1647 debug_assert_eq!(adt_def.variants().len(), 1);
1649 debug_assert_eq!(adt_def.non_enum_variant().def_id, def_id);
1650 }
1652
1653 for (idx, variant) in adt_def.variants().iter_enumerated() {
1654 let data = VariantData {
1655 discr: variant.discr,
1656 idx,
1657 ctor: variant.ctor.map(|(kind, def_id)| (kind, def_id.index)),
1658 is_non_exhaustive: variant.is_field_list_non_exhaustive(),
1659 };
1660 record!(self.tables.variant_data[variant.def_id] <- data);
1661
1662 record_array!(self.tables.associated_item_or_field_def_ids[variant.def_id] <- variant.fields.iter().map(|f| {
1663 assert!(f.did.is_local());
1664 f.did.index
1665 }));
1666
1667 for field in &variant.fields {
1668 self.tables.safety.set_some(field.did.index, field.safety);
1669 }
1670
1671 if let Some((CtorKind::Fn, ctor_def_id)) = variant.ctor {
1672 let fn_sig = tcx.fn_sig(ctor_def_id);
1673 record!(self.tables.fn_sig[variant.def_id] <- fn_sig);
1675 }
1676 }
1677
1678 if let Some(destructor) = tcx.adt_destructor(local_def_id) {
1679 record!(self.tables.adt_destructor[def_id] <- destructor);
1680 }
1681
1682 if let Some(destructor) = tcx.adt_async_destructor(local_def_id) {
1683 record!(self.tables.adt_async_destructor[def_id] <- destructor);
1684 }
1685 }
1686
1687 #[instrument(level = "debug", skip(self))]
1688 fn encode_info_for_mod(&mut self, local_def_id: LocalDefId) {
1689 let tcx = self.tcx;
1690 let def_id = local_def_id.to_def_id();
1691
1692 if self.is_proc_macro {
1698 record!(self.tables.expn_that_defined[def_id] <- tcx.expn_that_defined(local_def_id));
1700 } else {
1701 let module_children = tcx.module_children_local(local_def_id);
1702
1703 record_array!(self.tables.module_children_non_reexports[def_id] <-
1704 module_children.iter().filter(|child| child.reexport_chain.is_empty())
1705 .map(|child| child.res.def_id().index));
1706
1707 record_defaulted_array!(self.tables.module_children_reexports[def_id] <-
1708 module_children.iter().filter(|child| !child.reexport_chain.is_empty()));
1709 }
1710 }
1711
1712 fn encode_explicit_item_bounds(&mut self, def_id: DefId) {
1713 debug!("EncodeContext::encode_explicit_item_bounds({:?})", def_id);
1714 let bounds = self.tcx.explicit_item_bounds(def_id).skip_binder();
1715 record_defaulted_array!(self.tables.explicit_item_bounds[def_id] <- bounds);
1716 }
1717
1718 fn encode_explicit_item_self_bounds(&mut self, def_id: DefId) {
1719 debug!("EncodeContext::encode_explicit_item_self_bounds({:?})", def_id);
1720 let bounds = self.tcx.explicit_item_self_bounds(def_id).skip_binder();
1721 record_defaulted_array!(self.tables.explicit_item_self_bounds[def_id] <- bounds);
1722 }
1723
1724 #[instrument(level = "debug", skip(self))]
1725 fn encode_info_for_assoc_item(&mut self, def_id: DefId) {
1726 let tcx = self.tcx;
1727 let item = tcx.associated_item(def_id);
1728
1729 self.tables.defaultness.set_some(def_id.index, item.defaultness(tcx));
1730 self.tables.assoc_container.set_some(def_id.index, item.container);
1731
1732 match item.container {
1733 AssocItemContainer::Trait => {
1734 if item.is_type() {
1735 self.encode_explicit_item_bounds(def_id);
1736 self.encode_explicit_item_self_bounds(def_id);
1737 if tcx.is_conditionally_const(def_id) {
1738 record_defaulted_array!(self.tables.explicit_implied_const_bounds[def_id]
1739 <- self.tcx.explicit_implied_const_bounds(def_id).skip_binder());
1740 }
1741 }
1742 }
1743 AssocItemContainer::Impl => {
1744 if let Some(trait_item_def_id) = item.trait_item_def_id {
1745 self.tables.trait_item_def_id.set_some(def_id.index, trait_item_def_id.into());
1746 }
1747 }
1748 }
1749 if let ty::AssocKind::Type { data: ty::AssocTypeData::Rpitit(rpitit_info) } = item.kind {
1750 record!(self.tables.opt_rpitit_info[def_id] <- rpitit_info);
1751 if matches!(rpitit_info, ty::ImplTraitInTraitData::Trait { .. }) {
1752 record_array!(
1753 self.tables.assumed_wf_types_for_rpitit[def_id]
1754 <- self.tcx.assumed_wf_types_for_rpitit(def_id)
1755 );
1756 self.encode_precise_capturing_args(def_id);
1757 }
1758 }
1759 }
1760
1761 fn encode_precise_capturing_args(&mut self, def_id: DefId) {
1762 let Some(precise_capturing_args) = self.tcx.rendered_precise_capturing_args(def_id) else {
1763 return;
1764 };
1765
1766 record_array!(self.tables.rendered_precise_capturing_args[def_id] <- precise_capturing_args);
1767 }
1768
1769 fn encode_mir(&mut self) {
1770 if self.is_proc_macro {
1771 return;
1772 }
1773
1774 let tcx = self.tcx;
1775 let reachable_set = tcx.reachable_set(());
1776
1777 let keys_and_jobs = tcx.mir_keys(()).iter().filter_map(|&def_id| {
1778 let (encode_const, encode_opt) = should_encode_mir(tcx, reachable_set, def_id);
1779 if encode_const || encode_opt { Some((def_id, encode_const, encode_opt)) } else { None }
1780 });
1781 for (def_id, encode_const, encode_opt) in keys_and_jobs {
1782 debug_assert!(encode_const || encode_opt);
1783
1784 debug!("EntryBuilder::encode_mir({:?})", def_id);
1785 if encode_opt {
1786 record!(self.tables.optimized_mir[def_id.to_def_id()] <- tcx.optimized_mir(def_id));
1787 self.tables
1788 .cross_crate_inlinable
1789 .set(def_id.to_def_id().index, self.tcx.cross_crate_inlinable(def_id));
1790 record!(self.tables.closure_saved_names_of_captured_variables[def_id.to_def_id()]
1791 <- tcx.closure_saved_names_of_captured_variables(def_id));
1792
1793 if self.tcx.is_coroutine(def_id.to_def_id())
1794 && let Some(witnesses) = tcx.mir_coroutine_witnesses(def_id)
1795 {
1796 record!(self.tables.mir_coroutine_witnesses[def_id.to_def_id()] <- witnesses);
1797 }
1798 }
1799 if encode_const {
1800 record!(self.tables.mir_for_ctfe[def_id.to_def_id()] <- tcx.mir_for_ctfe(def_id));
1801
1802 let abstract_const = tcx.thir_abstract_const(def_id);
1804 if let Ok(Some(abstract_const)) = abstract_const {
1805 record!(self.tables.thir_abstract_const[def_id.to_def_id()] <- abstract_const);
1806 }
1807
1808 if should_encode_const(tcx.def_kind(def_id)) {
1809 let qualifs = tcx.mir_const_qualif(def_id);
1810 record!(self.tables.mir_const_qualif[def_id.to_def_id()] <- qualifs);
1811 let body = tcx.hir_maybe_body_owned_by(def_id);
1812 if let Some(body) = body {
1813 let const_data = rendered_const(self.tcx, &body, def_id);
1814 record!(self.tables.rendered_const[def_id.to_def_id()] <- const_data);
1815 }
1816 }
1817 }
1818 record!(self.tables.promoted_mir[def_id.to_def_id()] <- tcx.promoted_mir(def_id));
1819
1820 if self.tcx.is_coroutine(def_id.to_def_id())
1821 && let Some(witnesses) = tcx.mir_coroutine_witnesses(def_id)
1822 {
1823 record!(self.tables.mir_coroutine_witnesses[def_id.to_def_id()] <- witnesses);
1824 }
1825 }
1826
1827 if tcx.sess.opts.output_types.should_codegen()
1831 && tcx.sess.opts.optimize != OptLevel::No
1832 && tcx.sess.opts.incremental.is_none()
1833 {
1834 for &local_def_id in tcx.mir_keys(()) {
1835 if let DefKind::AssocFn | DefKind::Fn = tcx.def_kind(local_def_id) {
1836 record_array!(self.tables.deduced_param_attrs[local_def_id.to_def_id()] <-
1837 self.tcx.deduced_param_attrs(local_def_id.to_def_id()));
1838 }
1839 }
1840 }
1841 }
1842
1843 #[instrument(level = "debug", skip(self))]
1844 fn encode_stability(&mut self, def_id: DefId) {
1845 if self.feat.staged_api() || self.tcx.sess.opts.unstable_opts.force_unstable_if_unmarked {
1848 if let Some(stab) = self.tcx.lookup_stability(def_id) {
1849 record!(self.tables.lookup_stability[def_id] <- stab)
1850 }
1851 }
1852 }
1853
1854 #[instrument(level = "debug", skip(self))]
1855 fn encode_const_stability(&mut self, def_id: DefId) {
1856 if self.feat.staged_api() || self.tcx.sess.opts.unstable_opts.force_unstable_if_unmarked {
1859 if let Some(stab) = self.tcx.lookup_const_stability(def_id) {
1860 record!(self.tables.lookup_const_stability[def_id] <- stab)
1861 }
1862 }
1863 }
1864
1865 #[instrument(level = "debug", skip(self))]
1866 fn encode_default_body_stability(&mut self, def_id: DefId) {
1867 if self.feat.staged_api() || self.tcx.sess.opts.unstable_opts.force_unstable_if_unmarked {
1870 if let Some(stab) = self.tcx.lookup_default_body_stability(def_id) {
1871 record!(self.tables.lookup_default_body_stability[def_id] <- stab)
1872 }
1873 }
1874 }
1875
1876 #[instrument(level = "debug", skip(self))]
1877 fn encode_deprecation(&mut self, def_id: DefId) {
1878 if let Some(depr) = self.tcx.lookup_deprecation(def_id) {
1879 record!(self.tables.lookup_deprecation_entry[def_id] <- depr);
1880 }
1881 }
1882
1883 #[instrument(level = "debug", skip(self))]
1884 fn encode_info_for_macro(&mut self, def_id: LocalDefId) {
1885 let tcx = self.tcx;
1886
1887 let (_, macro_def, _) = tcx.hir_expect_item(def_id).expect_macro();
1888 self.tables.is_macro_rules.set(def_id.local_def_index, macro_def.macro_rules);
1889 record!(self.tables.macro_definition[def_id.to_def_id()] <- &*macro_def.body);
1890 }
1891
1892 fn encode_native_libraries(&mut self) -> LazyArray<NativeLib> {
1893 empty_proc_macro!(self);
1894 let used_libraries = self.tcx.native_libraries(LOCAL_CRATE);
1895 self.lazy_array(used_libraries.iter())
1896 }
1897
1898 fn encode_foreign_modules(&mut self) -> LazyArray<ForeignModule> {
1899 empty_proc_macro!(self);
1900 let foreign_modules = self.tcx.foreign_modules(LOCAL_CRATE);
1901 self.lazy_array(foreign_modules.iter().map(|(_, m)| m).cloned())
1902 }
1903
1904 fn encode_hygiene(&mut self) -> (SyntaxContextTable, ExpnDataTable, ExpnHashTable) {
1905 let mut syntax_contexts: TableBuilder<_, _> = Default::default();
1906 let mut expn_data_table: TableBuilder<_, _> = Default::default();
1907 let mut expn_hash_table: TableBuilder<_, _> = Default::default();
1908
1909 self.hygiene_ctxt.encode(
1910 &mut (&mut *self, &mut syntax_contexts, &mut expn_data_table, &mut expn_hash_table),
1911 |(this, syntax_contexts, _, _), index, ctxt_data| {
1912 syntax_contexts.set_some(index, this.lazy(ctxt_data));
1913 },
1914 |(this, _, expn_data_table, expn_hash_table), index, expn_data, hash| {
1915 if let Some(index) = index.as_local() {
1916 expn_data_table.set_some(index.as_raw(), this.lazy(expn_data));
1917 expn_hash_table.set_some(index.as_raw(), this.lazy(hash));
1918 }
1919 },
1920 );
1921
1922 (
1923 syntax_contexts.encode(&mut self.opaque),
1924 expn_data_table.encode(&mut self.opaque),
1925 expn_hash_table.encode(&mut self.opaque),
1926 )
1927 }
1928
1929 fn encode_proc_macros(&mut self) -> Option<ProcMacroData> {
1930 let is_proc_macro = self.tcx.crate_types().contains(&CrateType::ProcMacro);
1931 if is_proc_macro {
1932 let tcx = self.tcx;
1933 let proc_macro_decls_static = tcx.proc_macro_decls_static(()).unwrap().local_def_index;
1934 let stability = tcx.lookup_stability(CRATE_DEF_ID);
1935 let macros =
1936 self.lazy_array(tcx.resolutions(()).proc_macros.iter().map(|p| p.local_def_index));
1937 for (i, span) in self.tcx.sess.psess.proc_macro_quoted_spans() {
1938 let span = self.lazy(span);
1939 self.tables.proc_macro_quoted_spans.set_some(i, span);
1940 }
1941
1942 self.tables.def_kind.set_some(LOCAL_CRATE.as_def_id().index, DefKind::Mod);
1943 record!(self.tables.def_span[LOCAL_CRATE.as_def_id()] <- tcx.def_span(LOCAL_CRATE.as_def_id()));
1944 self.encode_attrs(LOCAL_CRATE.as_def_id().expect_local());
1945 let vis = tcx.local_visibility(CRATE_DEF_ID).map_id(|def_id| def_id.local_def_index);
1946 record!(self.tables.visibility[LOCAL_CRATE.as_def_id()] <- vis);
1947 if let Some(stability) = stability {
1948 record!(self.tables.lookup_stability[LOCAL_CRATE.as_def_id()] <- stability);
1949 }
1950 self.encode_deprecation(LOCAL_CRATE.as_def_id());
1951 if let Some(res_map) = tcx.resolutions(()).doc_link_resolutions.get(&CRATE_DEF_ID) {
1952 record!(self.tables.doc_link_resolutions[LOCAL_CRATE.as_def_id()] <- res_map);
1953 }
1954 if let Some(traits) = tcx.resolutions(()).doc_link_traits_in_scope.get(&CRATE_DEF_ID) {
1955 record_array!(self.tables.doc_link_traits_in_scope[LOCAL_CRATE.as_def_id()] <- traits);
1956 }
1957
1958 for &proc_macro in &tcx.resolutions(()).proc_macros {
1962 let id = proc_macro;
1963 let proc_macro = tcx.local_def_id_to_hir_id(proc_macro);
1964 let mut name = tcx.hir_name(proc_macro);
1965 let span = tcx.hir_span(proc_macro);
1966 let attrs = tcx.hir_attrs(proc_macro);
1969 let macro_kind = if find_attr!(attrs, AttributeKind::ProcMacro(..)) {
1970 MacroKind::Bang
1971 } else if find_attr!(attrs, AttributeKind::ProcMacroAttribute(..)) {
1972 MacroKind::Attr
1973 } else if let Some(trait_name) = find_attr!(attrs, AttributeKind::ProcMacroDerive { trait_name, ..} => trait_name)
1974 {
1975 name = *trait_name;
1976 MacroKind::Derive
1977 } else {
1978 bug!("Unknown proc-macro type for item {:?}", id);
1979 };
1980
1981 let mut def_key = self.tcx.hir_def_key(id);
1982 def_key.disambiguated_data.data = DefPathData::MacroNs(name);
1983
1984 let def_id = id.to_def_id();
1985 self.tables.def_kind.set_some(def_id.index, DefKind::Macro(macro_kind));
1986 self.tables.proc_macro.set_some(def_id.index, macro_kind);
1987 self.encode_attrs(id);
1988 record!(self.tables.def_keys[def_id] <- def_key);
1989 record!(self.tables.def_ident_span[def_id] <- span);
1990 record!(self.tables.def_span[def_id] <- span);
1991 record!(self.tables.visibility[def_id] <- ty::Visibility::Public);
1992 if let Some(stability) = stability {
1993 record!(self.tables.lookup_stability[def_id] <- stability);
1994 }
1995 }
1996
1997 Some(ProcMacroData { proc_macro_decls_static, stability, macros })
1998 } else {
1999 None
2000 }
2001 }
2002
2003 fn encode_debugger_visualizers(&mut self) -> LazyArray<DebuggerVisualizerFile> {
2004 empty_proc_macro!(self);
2005 self.lazy_array(
2006 self.tcx
2007 .debugger_visualizers(LOCAL_CRATE)
2008 .iter()
2009 .map(DebuggerVisualizerFile::path_erased),
2014 )
2015 }
2016
2017 fn encode_crate_deps(&mut self) -> LazyArray<CrateDep> {
2018 empty_proc_macro!(self);
2019
2020 let deps = self
2021 .tcx
2022 .crates(())
2023 .iter()
2024 .map(|&cnum| {
2025 let dep = CrateDep {
2026 name: self.tcx.crate_name(cnum),
2027 hash: self.tcx.crate_hash(cnum),
2028 host_hash: self.tcx.crate_host_hash(cnum),
2029 kind: self.tcx.dep_kind(cnum),
2030 extra_filename: self.tcx.extra_filename(cnum).clone(),
2031 is_private: self.tcx.is_private_dep(cnum),
2032 };
2033 (cnum, dep)
2034 })
2035 .collect::<Vec<_>>();
2036
2037 {
2038 let mut expected_cnum = 1;
2040 for &(n, _) in &deps {
2041 assert_eq!(n, CrateNum::new(expected_cnum));
2042 expected_cnum += 1;
2043 }
2044 }
2045
2046 self.lazy_array(deps.iter().map(|(_, dep)| dep))
2051 }
2052
2053 fn encode_target_modifiers(&mut self) -> LazyArray<TargetModifier> {
2054 empty_proc_macro!(self);
2055 let tcx = self.tcx;
2056 self.lazy_array(tcx.sess.opts.gather_target_modifiers())
2057 }
2058
2059 fn encode_lib_features(&mut self) -> LazyArray<(Symbol, FeatureStability)> {
2060 empty_proc_macro!(self);
2061 let tcx = self.tcx;
2062 let lib_features = tcx.lib_features(LOCAL_CRATE);
2063 self.lazy_array(lib_features.to_sorted_vec())
2064 }
2065
2066 fn encode_stability_implications(&mut self) -> LazyArray<(Symbol, Symbol)> {
2067 empty_proc_macro!(self);
2068 let tcx = self.tcx;
2069 let implications = tcx.stability_implications(LOCAL_CRATE);
2070 let sorted = implications.to_sorted_stable_ord();
2071 self.lazy_array(sorted.into_iter().map(|(k, v)| (*k, *v)))
2072 }
2073
2074 fn encode_diagnostic_items(&mut self) -> LazyArray<(Symbol, DefIndex)> {
2075 empty_proc_macro!(self);
2076 let tcx = self.tcx;
2077 let diagnostic_items = &tcx.diagnostic_items(LOCAL_CRATE).name_to_id;
2078 self.lazy_array(diagnostic_items.iter().map(|(&name, def_id)| (name, def_id.index)))
2079 }
2080
2081 fn encode_lang_items(&mut self) -> LazyArray<(DefIndex, LangItem)> {
2082 empty_proc_macro!(self);
2083 let lang_items = self.tcx.lang_items().iter();
2084 self.lazy_array(lang_items.filter_map(|(lang_item, def_id)| {
2085 def_id.as_local().map(|id| (id.local_def_index, lang_item))
2086 }))
2087 }
2088
2089 fn encode_lang_items_missing(&mut self) -> LazyArray<LangItem> {
2090 empty_proc_macro!(self);
2091 let tcx = self.tcx;
2092 self.lazy_array(&tcx.lang_items().missing)
2093 }
2094
2095 fn encode_stripped_cfg_items(&mut self) -> LazyArray<StrippedCfgItem<DefIndex>> {
2096 self.lazy_array(
2097 self.tcx
2098 .stripped_cfg_items(LOCAL_CRATE)
2099 .into_iter()
2100 .map(|item| item.clone().map_mod_id(|def_id| def_id.index)),
2101 )
2102 }
2103
2104 fn encode_traits(&mut self) -> LazyArray<DefIndex> {
2105 empty_proc_macro!(self);
2106 self.lazy_array(self.tcx.traits(LOCAL_CRATE).iter().map(|def_id| def_id.index))
2107 }
2108
2109 #[instrument(level = "debug", skip(self))]
2111 fn encode_impls(&mut self) -> LazyArray<TraitImpls> {
2112 empty_proc_macro!(self);
2113 let tcx = self.tcx;
2114 let mut trait_impls: FxIndexMap<DefId, Vec<(DefIndex, Option<SimplifiedType>)>> =
2115 FxIndexMap::default();
2116
2117 for id in tcx.hir_free_items() {
2118 let DefKind::Impl { of_trait } = tcx.def_kind(id.owner_id) else {
2119 continue;
2120 };
2121 let def_id = id.owner_id.to_def_id();
2122
2123 if of_trait && let Some(header) = tcx.impl_trait_header(def_id) {
2124 record!(self.tables.impl_trait_header[def_id] <- header);
2125
2126 self.tables.defaultness.set_some(def_id.index, tcx.defaultness(def_id));
2127
2128 let trait_ref = header.trait_ref.instantiate_identity();
2129 let simplified_self_ty = fast_reject::simplify_type(
2130 self.tcx,
2131 trait_ref.self_ty(),
2132 TreatParams::InstantiateWithInfer,
2133 );
2134 trait_impls
2135 .entry(trait_ref.def_id)
2136 .or_default()
2137 .push((id.owner_id.def_id.local_def_index, simplified_self_ty));
2138
2139 let trait_def = tcx.trait_def(trait_ref.def_id);
2140 if let Ok(mut an) = trait_def.ancestors(tcx, def_id)
2141 && let Some(specialization_graph::Node::Impl(parent)) = an.nth(1)
2142 {
2143 self.tables.impl_parent.set_some(def_id.index, parent.into());
2144 }
2145
2146 if tcx.is_lang_item(trait_ref.def_id, LangItem::CoerceUnsized) {
2149 let coerce_unsized_info = tcx.coerce_unsized_info(def_id).unwrap();
2150 record!(self.tables.coerce_unsized_info[def_id] <- coerce_unsized_info);
2151 }
2152 }
2153 }
2154
2155 let trait_impls: Vec<_> = trait_impls
2156 .into_iter()
2157 .map(|(trait_def_id, impls)| TraitImpls {
2158 trait_id: (trait_def_id.krate.as_u32(), trait_def_id.index),
2159 impls: self.lazy_array(&impls),
2160 })
2161 .collect();
2162
2163 self.lazy_array(&trait_impls)
2164 }
2165
2166 #[instrument(level = "debug", skip(self))]
2167 fn encode_incoherent_impls(&mut self) -> LazyArray<IncoherentImpls> {
2168 empty_proc_macro!(self);
2169 let tcx = self.tcx;
2170
2171 let all_impls: Vec<_> = tcx
2172 .crate_inherent_impls(())
2173 .0
2174 .incoherent_impls
2175 .iter()
2176 .map(|(&simp, impls)| IncoherentImpls {
2177 self_ty: simp,
2178 impls: self.lazy_array(impls.iter().map(|def_id| def_id.local_def_index)),
2179 })
2180 .collect();
2181
2182 self.lazy_array(&all_impls)
2183 }
2184
2185 fn encode_exportable_items(&mut self) -> LazyArray<DefIndex> {
2186 empty_proc_macro!(self);
2187 self.lazy_array(self.tcx.exportable_items(LOCAL_CRATE).iter().map(|def_id| def_id.index))
2188 }
2189
2190 fn encode_stable_order_of_exportable_impls(&mut self) -> LazyArray<(DefIndex, usize)> {
2191 empty_proc_macro!(self);
2192 let stable_order_of_exportable_impls =
2193 self.tcx.stable_order_of_exportable_impls(LOCAL_CRATE);
2194 self.lazy_array(
2195 stable_order_of_exportable_impls.iter().map(|(def_id, idx)| (def_id.index, *idx)),
2196 )
2197 }
2198
2199 fn encode_exported_symbols(
2206 &mut self,
2207 exported_symbols: &[(ExportedSymbol<'tcx>, SymbolExportInfo)],
2208 ) -> LazyArray<(ExportedSymbol<'static>, SymbolExportInfo)> {
2209 empty_proc_macro!(self);
2210 let metadata_symbol_name = SymbolName::new(self.tcx, &metadata_symbol_name(self.tcx));
2213
2214 self.lazy_array(
2215 exported_symbols
2216 .iter()
2217 .filter(|&(exported_symbol, _)| match *exported_symbol {
2218 ExportedSymbol::NoDefId(symbol_name) => symbol_name != metadata_symbol_name,
2219 _ => true,
2220 })
2221 .cloned(),
2222 )
2223 }
2224
2225 fn encode_dylib_dependency_formats(&mut self) -> LazyArray<Option<LinkagePreference>> {
2226 empty_proc_macro!(self);
2227 let formats = self.tcx.dependency_formats(());
2228 if let Some(arr) = formats.get(&CrateType::Dylib) {
2229 return self.lazy_array(arr.iter().skip(1 ).map(
2230 |slot| match *slot {
2231 Linkage::NotLinked | Linkage::IncludedFromDylib => None,
2232
2233 Linkage::Dynamic => Some(LinkagePreference::RequireDynamic),
2234 Linkage::Static => Some(LinkagePreference::RequireStatic),
2235 },
2236 ));
2237 }
2238 LazyArray::default()
2239 }
2240}
2241
2242fn prefetch_mir(tcx: TyCtxt<'_>) {
2245 if !tcx.sess.opts.output_types.should_codegen() {
2246 return;
2248 }
2249
2250 let reachable_set = tcx.reachable_set(());
2251 par_for_each_in(tcx.mir_keys(()), |&&def_id| {
2252 let (encode_const, encode_opt) = should_encode_mir(tcx, reachable_set, def_id);
2253
2254 if encode_const {
2255 tcx.ensure_done().mir_for_ctfe(def_id);
2256 }
2257 if encode_opt {
2258 tcx.ensure_done().optimized_mir(def_id);
2259 }
2260 if encode_opt || encode_const {
2261 tcx.ensure_done().promoted_mir(def_id);
2262 }
2263 })
2264}
2265
2266pub struct EncodedMetadata {
2290 full_metadata: Option<Mmap>,
2293 stub_metadata: Option<Vec<u8>>,
2296 path: Option<Box<Path>>,
2298 _temp_dir: Option<MaybeTempDir>,
2301}
2302
2303impl EncodedMetadata {
2304 #[inline]
2305 pub fn from_path(
2306 path: PathBuf,
2307 stub_path: Option<PathBuf>,
2308 temp_dir: Option<MaybeTempDir>,
2309 ) -> std::io::Result<Self> {
2310 let file = std::fs::File::open(&path)?;
2311 let file_metadata = file.metadata()?;
2312 if file_metadata.len() == 0 {
2313 return Ok(Self {
2314 full_metadata: None,
2315 stub_metadata: None,
2316 path: None,
2317 _temp_dir: None,
2318 });
2319 }
2320 let full_mmap = unsafe { Some(Mmap::map(file)?) };
2321
2322 let stub =
2323 if let Some(stub_path) = stub_path { Some(std::fs::read(stub_path)?) } else { None };
2324
2325 Ok(Self {
2326 full_metadata: full_mmap,
2327 stub_metadata: stub,
2328 path: Some(path.into()),
2329 _temp_dir: temp_dir,
2330 })
2331 }
2332
2333 #[inline]
2334 pub fn full(&self) -> &[u8] {
2335 &self.full_metadata.as_deref().unwrap_or_default()
2336 }
2337
2338 #[inline]
2339 pub fn stub_or_full(&self) -> &[u8] {
2340 self.stub_metadata.as_deref().unwrap_or(self.full())
2341 }
2342
2343 #[inline]
2344 pub fn path(&self) -> Option<&Path> {
2345 self.path.as_deref()
2346 }
2347}
2348
2349impl<S: Encoder> Encodable<S> for EncodedMetadata {
2350 fn encode(&self, s: &mut S) {
2351 self.stub_metadata.encode(s);
2352
2353 let slice = self.full();
2354 slice.encode(s)
2355 }
2356}
2357
2358impl<D: Decoder> Decodable<D> for EncodedMetadata {
2359 fn decode(d: &mut D) -> Self {
2360 let stub = <Option<Vec<u8>>>::decode(d);
2361
2362 let len = d.read_usize();
2363 let full_metadata = if len > 0 {
2364 let mut mmap = MmapMut::map_anon(len).unwrap();
2365 mmap.copy_from_slice(d.read_raw_bytes(len));
2366 Some(mmap.make_read_only().unwrap())
2367 } else {
2368 None
2369 };
2370
2371 Self { full_metadata, stub_metadata: stub, path: None, _temp_dir: None }
2372 }
2373}
2374
2375#[instrument(level = "trace", skip(tcx))]
2376pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path, ref_path: Option<&Path>) {
2377 tcx.dep_graph.assert_ignored();
2380
2381 if let Some(ref_path) = ref_path {
2383 let _prof_timer = tcx.prof.verbose_generic_activity("generate_crate_metadata_stub");
2384
2385 with_encode_metadata_header(tcx, ref_path, |ecx| {
2386 let header: LazyValue<CrateHeader> = ecx.lazy(CrateHeader {
2387 name: tcx.crate_name(LOCAL_CRATE),
2388 triple: tcx.sess.opts.target_triple.clone(),
2389 hash: tcx.crate_hash(LOCAL_CRATE),
2390 is_proc_macro_crate: false,
2391 is_stub: true,
2392 });
2393 header.position.get()
2394 })
2395 }
2396
2397 let _prof_timer = tcx.prof.verbose_generic_activity("generate_crate_metadata");
2398
2399 let dep_node = tcx.metadata_dep_node();
2400
2401 if tcx.dep_graph.is_fully_enabled()
2403 && let work_product_id = WorkProductId::from_cgu_name("metadata")
2404 && let Some(work_product) = tcx.dep_graph.previous_work_product(&work_product_id)
2405 && tcx.try_mark_green(&dep_node)
2406 {
2407 let saved_path = &work_product.saved_files["rmeta"];
2408 let incr_comp_session_dir = tcx.sess.incr_comp_session_dir_opt().unwrap();
2409 let source_file = rustc_incremental::in_incr_comp_dir(&incr_comp_session_dir, saved_path);
2410 debug!("copying preexisting metadata from {source_file:?} to {path:?}");
2411 match rustc_fs_util::link_or_copy(&source_file, path) {
2412 Ok(_) => {}
2413 Err(err) => tcx.dcx().emit_fatal(FailCreateFileEncoder { err }),
2414 };
2415 return;
2416 };
2417
2418 if tcx.sess.threads() != 1 {
2419 join(
2423 || prefetch_mir(tcx),
2424 || {
2425 let _ = tcx.exported_non_generic_symbols(LOCAL_CRATE);
2426 let _ = tcx.exported_generic_symbols(LOCAL_CRATE);
2427 },
2428 );
2429 }
2430
2431 tcx.dep_graph.with_task(
2434 dep_node,
2435 tcx,
2436 path,
2437 |tcx, path| {
2438 with_encode_metadata_header(tcx, path, |ecx| {
2439 let root = ecx.encode_crate_root();
2442
2443 ecx.opaque.flush();
2445 tcx.prof.artifact_size(
2447 "crate_metadata",
2448 "crate_metadata",
2449 ecx.opaque.file().metadata().unwrap().len(),
2450 );
2451
2452 root.position.get()
2453 })
2454 },
2455 None,
2456 );
2457}
2458
2459fn with_encode_metadata_header(
2460 tcx: TyCtxt<'_>,
2461 path: &Path,
2462 f: impl FnOnce(&mut EncodeContext<'_, '_>) -> usize,
2463) {
2464 let mut encoder = opaque::FileEncoder::new(path)
2465 .unwrap_or_else(|err| tcx.dcx().emit_fatal(FailCreateFileEncoder { err }));
2466 encoder.emit_raw_bytes(METADATA_HEADER);
2467
2468 encoder.emit_raw_bytes(&0u64.to_le_bytes());
2470
2471 let source_map_files = tcx.sess.source_map().files();
2472 let source_file_cache = (Arc::clone(&source_map_files[0]), 0);
2473 let required_source_files = Some(FxIndexSet::default());
2474 drop(source_map_files);
2475
2476 let hygiene_ctxt = HygieneEncodeContext::default();
2477
2478 let mut ecx = EncodeContext {
2479 opaque: encoder,
2480 tcx,
2481 feat: tcx.features(),
2482 tables: Default::default(),
2483 lazy_state: LazyState::NoNode,
2484 span_shorthands: Default::default(),
2485 type_shorthands: Default::default(),
2486 predicate_shorthands: Default::default(),
2487 source_file_cache,
2488 interpret_allocs: Default::default(),
2489 required_source_files,
2490 is_proc_macro: tcx.crate_types().contains(&CrateType::ProcMacro),
2491 hygiene_ctxt: &hygiene_ctxt,
2492 symbol_index_table: Default::default(),
2493 };
2494
2495 rustc_version(tcx.sess.cfg_version).encode(&mut ecx);
2497
2498 let root_position = f(&mut ecx);
2499
2500 if let Err((path, err)) = ecx.opaque.finish() {
2504 tcx.dcx().emit_fatal(FailWriteFile { path: &path, err });
2505 }
2506
2507 let file = ecx.opaque.file();
2508 if let Err(err) = encode_root_position(file, root_position) {
2509 tcx.dcx().emit_fatal(FailWriteFile { path: ecx.opaque.path(), err });
2510 }
2511}
2512
2513fn encode_root_position(mut file: &File, pos: usize) -> Result<(), std::io::Error> {
2514 let pos_before_seek = file.stream_position().unwrap();
2516
2517 let header = METADATA_HEADER.len();
2519 file.seek(std::io::SeekFrom::Start(header as u64))?;
2520 file.write_all(&pos.to_le_bytes())?;
2521
2522 file.seek(std::io::SeekFrom::Start(pos_before_seek))?;
2524 Ok(())
2525}
2526
2527pub(crate) fn provide(providers: &mut Providers) {
2528 *providers = Providers {
2529 doc_link_resolutions: |tcx, def_id| {
2530 tcx.resolutions(())
2531 .doc_link_resolutions
2532 .get(&def_id)
2533 .unwrap_or_else(|| span_bug!(tcx.def_span(def_id), "no resolutions for a doc link"))
2534 },
2535 doc_link_traits_in_scope: |tcx, def_id| {
2536 tcx.resolutions(()).doc_link_traits_in_scope.get(&def_id).unwrap_or_else(|| {
2537 span_bug!(tcx.def_span(def_id), "no traits in scope for a doc link")
2538 })
2539 },
2540
2541 ..*providers
2542 }
2543}
2544
2545pub fn rendered_const<'tcx>(tcx: TyCtxt<'tcx>, body: &hir::Body<'_>, def_id: LocalDefId) -> String {
2573 let value = body.value;
2574
2575 #[derive(PartialEq, Eq)]
2576 enum Classification {
2577 Literal,
2578 Simple,
2579 Complex,
2580 }
2581
2582 use Classification::*;
2583
2584 fn classify(expr: &hir::Expr<'_>) -> Classification {
2585 match &expr.kind {
2586 hir::ExprKind::Unary(hir::UnOp::Neg, expr) => {
2587 if matches!(expr.kind, hir::ExprKind::Lit(_)) { Literal } else { Complex }
2588 }
2589 hir::ExprKind::Lit(_) => Literal,
2590 hir::ExprKind::Tup([]) => Simple,
2591 hir::ExprKind::Block(hir::Block { stmts: [], expr: Some(expr), .. }, _) => {
2592 if classify(expr) == Complex { Complex } else { Simple }
2593 }
2594 hir::ExprKind::Path(hir::QPath::Resolved(_, hir::Path { segments, .. })) => {
2599 if segments.iter().all(|segment| segment.args.is_none()) { Simple } else { Complex }
2600 }
2601 hir::ExprKind::Path(hir::QPath::TypeRelative(..)) => Simple,
2604 hir::ExprKind::Path(hir::QPath::LangItem(..)) => Simple,
2606 _ => Complex,
2607 }
2608 }
2609
2610 match classify(value) {
2611 Literal
2621 if !value.span.from_expansion()
2622 && let Ok(snippet) = tcx.sess.source_map().span_to_snippet(value.span) =>
2623 {
2624 snippet
2625 }
2626
2627 Literal | Simple => id_to_string(&tcx, body.id().hir_id),
2630
2631 Complex => {
2635 if tcx.def_kind(def_id) == DefKind::AnonConst {
2636 "{ _ }".to_owned()
2637 } else {
2638 "_".to_owned()
2639 }
2640 }
2641 }
2642}