diff options
Diffstat (limited to 'compiler/rustc_metadata/src/rmeta/encoder.rs')
-rw-r--r-- | compiler/rustc_metadata/src/rmeta/encoder.rs | 1193 |
1 files changed, 611 insertions, 582 deletions
diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 33278367c..049514ec7 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1,7 +1,9 @@ +use crate::errors::{FailCreateFileEncoder, FailSeekFile, FailWriteFile}; use crate::rmeta::def_path_hash_map::DefPathHashMapRef; use crate::rmeta::table::TableBuilder; use crate::rmeta::*; +use rustc_ast::Attribute; use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fx::{FxHashMap, FxIndexSet}; use rustc_data_structures::memmap::{Mmap, MmapMut}; @@ -16,8 +18,6 @@ use rustc_hir::def_id::{ use rustc_hir::definitions::DefPathData; use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::lang_items; -use rustc_hir::{AnonConst, GenericParamKind}; -use rustc_index::bit_set::GrowableBitSet; use rustc_middle::hir::nested_filter; use rustc_middle::middle::dependency_format::Linkage; use rustc_middle::middle::exported_symbols::{ @@ -29,8 +29,9 @@ use rustc_middle::ty::codec::TyEncoder; use rustc_middle::ty::fast_reject::{self, SimplifiedType, TreatParams}; use rustc_middle::ty::query::Providers; use rustc_middle::ty::{self, SymbolName, Ty, TyCtxt}; +use rustc_middle::util::common::to_readable_str; use rustc_serialize::{opaque, Decodable, Decoder, Encodable, Encoder}; -use rustc_session::config::CrateType; +use rustc_session::config::{CrateType, OptLevel}; use rustc_session::cstore::{ForeignModule, LinkagePreference, NativeLib}; use rustc_span::hygiene::{ExpnIndex, HygieneEncodeContext, MacroKind}; use rustc_span::symbol::{sym, Symbol}; @@ -39,12 +40,12 @@ use rustc_span::{ }; use rustc_target::abi::VariantIdx; use std::borrow::Borrow; +use std::collections::hash_map::Entry; use std::hash::Hash; use std::io::{Read, Seek, Write}; use std::iter; use std::num::NonZeroUsize; use std::path::{Path, PathBuf}; -use tracing::{debug, trace}; pub(super) struct EncodeContext<'a, 'tcx> { opaque: opaque::FileEncoder, @@ -66,15 +67,13 @@ pub(super) struct EncodeContext<'a, 'tcx> { // The indices (into the `SourceMap`'s `MonotonicVec`) // of all of the `SourceFiles` that we need to serialize. // When we serialize a `Span`, we insert the index of its - // `SourceFile` into the `GrowableBitSet`. - // - // This needs to be a `GrowableBitSet` and not a - // regular `BitSet` because we may actually import new `SourceFiles` - // during metadata encoding, due to executing a query - // with a result containing a foreign `Span`. - required_source_files: Option<GrowableBitSet<usize>>, + // `SourceFile` into the `FxIndexSet`. + // The order inside the `FxIndexSet` is used as on-disk + // order of `SourceFiles`, and encoded inside `Span`s. + required_source_files: Option<FxIndexSet<usize>>, is_proc_macro: bool, hygiene_ctxt: &'a HygieneEncodeContext, + symbol_table: FxHashMap<Symbol, usize>, } /// If the current crate is a proc-macro, returns early with `Lazy:empty()`. @@ -238,17 +237,15 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for Span { s.source_file_cache = (source_map.files()[source_file_index].clone(), source_file_index); } + let (ref source_file, source_file_index) = s.source_file_cache; + debug_assert!(source_file.contains(span.lo)); - if !s.source_file_cache.0.contains(span.hi) { + if !source_file.contains(span.hi) { // Unfortunately, macro expansion still sometimes generates Spans // that malformed in this way. return TAG_PARTIAL_SPAN.encode(s); } - let source_files = s.required_source_files.as_mut().expect("Already encoded SourceMap!"); - // Record the fact that we need to encode the data for this `SourceFile` - source_files.insert(s.source_file_cache.1); - // There are two possible cases here: // 1. This span comes from a 'foreign' crate - e.g. some crate upstream of the // crate we are writing metadata for. When the metadata for *this* crate gets @@ -265,39 +262,51 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for Span { // if we're a proc-macro crate. // This allows us to avoid loading the dependencies of proc-macro crates: all of // the information we need to decode `Span`s is stored in the proc-macro crate. - let (tag, lo, hi) = if s.source_file_cache.0.is_imported() && !s.is_proc_macro { - // To simplify deserialization, we 'rebase' this span onto the crate it originally came from - // (the crate that 'owns' the file it references. These rebased 'lo' and 'hi' values - // are relative to the source map information for the 'foreign' crate whose CrateNum - // we write into the metadata. This allows `imported_source_files` to binary + let (tag, metadata_index) = if source_file.is_imported() && !s.is_proc_macro { + // To simplify deserialization, we 'rebase' this span onto the crate it originally came + // from (the crate that 'owns' the file it references. These rebased 'lo' and 'hi' + // values are relative to the source map information for the 'foreign' crate whose + // CrateNum we write into the metadata. This allows `imported_source_files` to binary // search through the 'foreign' crate's source map information, using the // deserialized 'lo' and 'hi' values directly. // // All of this logic ensures that the final result of deserialization is a 'normal' // Span that can be used without any additional trouble. - let external_start_pos = { + let metadata_index = { // Introduce a new scope so that we drop the 'lock()' temporary - match &*s.source_file_cache.0.external_src.lock() { - ExternalSource::Foreign { original_start_pos, .. } => *original_start_pos, + match &*source_file.external_src.lock() { + ExternalSource::Foreign { metadata_index, .. } => *metadata_index, src => panic!("Unexpected external source {:?}", src), } }; - let lo = (span.lo - s.source_file_cache.0.start_pos) + external_start_pos; - let hi = (span.hi - s.source_file_cache.0.start_pos) + external_start_pos; - (TAG_VALID_SPAN_FOREIGN, lo, hi) + (TAG_VALID_SPAN_FOREIGN, metadata_index) } else { - (TAG_VALID_SPAN_LOCAL, span.lo, span.hi) + // Record the fact that we need to encode the data for this `SourceFile` + let source_files = + s.required_source_files.as_mut().expect("Already encoded SourceMap!"); + let (metadata_index, _) = source_files.insert_full(source_file_index); + let metadata_index: u32 = + metadata_index.try_into().expect("cannot export more than U32_MAX files"); + + (TAG_VALID_SPAN_LOCAL, metadata_index) }; - tag.encode(s); - lo.encode(s); + // Encode the start position relative to the file start, so we profit more from the + // variable-length integer encoding. + let lo = span.lo - source_file.start_pos; // Encode length which is usually less than span.hi and profits more // from the variable-length integer encoding that we use. - let len = hi - lo; + let len = span.hi - span.lo; + + tag.encode(s); + lo.encode(s); len.encode(s); + // Encode the index of the `SourceFile` for the span, in order to make decoding faster. + metadata_index.encode(s); + if tag == TAG_VALID_SPAN_FOREIGN { // This needs to be two lines to avoid holding the `s.source_file_cache` // while calling `cnum.encode(s)` @@ -307,6 +316,31 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for Span { } } +impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for Symbol { + fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) { + // if symbol preinterned, emit tag and symbol index + if self.is_preinterned() { + s.opaque.emit_u8(SYMBOL_PREINTERNED); + s.opaque.emit_u32(self.as_u32()); + } else { + // otherwise write it as string or as offset to it + match s.symbol_table.entry(*self) { + Entry::Vacant(o) => { + s.opaque.emit_u8(SYMBOL_STR); + let pos = s.opaque.position(); + o.insert(pos); + s.emit_str(self.as_str()); + } + Entry::Occupied(o) => { + let x = o.get().clone(); + s.emit_u8(SYMBOL_OFFSET); + s.emit_usize(x); + } + } + } + } +} + impl<'a, 'tcx> TyEncoder for EncodeContext<'a, 'tcx> { const CLEAR_CROSS_CRATE: bool = true; @@ -449,7 +483,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { self.lazy(DefPathHashMapRef::BorrowedFromTcx(self.tcx.def_path_hash_to_def_index_map())) } - fn encode_source_map(&mut self) -> LazyArray<rustc_span::SourceFile> { + fn encode_source_map(&mut self) -> LazyTable<u32, LazyValue<rustc_span::SourceFile>> { let source_map = self.tcx.sess.source_map(); let all_source_files = source_map.files(); @@ -460,142 +494,118 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let working_directory = &self.tcx.sess.opts.working_dir; - let adapted = all_source_files - .iter() - .enumerate() - .filter(|(idx, source_file)| { - // Only serialize `SourceFile`s that were used - // during the encoding of a `Span` - required_source_files.contains(*idx) && - // Don't serialize imported `SourceFile`s, unless - // we're in a proc-macro crate. - (!source_file.is_imported() || self.is_proc_macro) - }) - .map(|(_, source_file)| { - // At export time we expand all source file paths to absolute paths because - // downstream compilation sessions can have a different compiler working - // directory, so relative paths from this or any other upstream crate - // won't be valid anymore. - // - // At this point we also erase the actual on-disk path and only keep - // the remapped version -- as is necessary for reproducible builds. - match source_file.name { - FileName::Real(ref original_file_name) => { - let adapted_file_name = - source_map.path_mapping().to_embeddable_absolute_path( - original_file_name.clone(), - working_directory, - ); - - if adapted_file_name != *original_file_name { - let mut adapted: SourceFile = (**source_file).clone(); - adapted.name = FileName::Real(adapted_file_name); - adapted.name_hash = { - let mut hasher: StableHasher = StableHasher::new(); - adapted.name.hash(&mut hasher); - hasher.finish::<u128>() - }; - Lrc::new(adapted) - } else { - // Nothing to adapt - source_file.clone() - } + let mut adapted = TableBuilder::default(); + + // Only serialize `SourceFile`s that were used during the encoding of a `Span`. + // + // The order in which we encode source files is important here: the on-disk format for + // `Span` contains the index of the corresponding `SourceFile`. + for (on_disk_index, &source_file_index) in required_source_files.iter().enumerate() { + let source_file = &all_source_files[source_file_index]; + // Don't serialize imported `SourceFile`s, unless we're in a proc-macro crate. + assert!(!source_file.is_imported() || self.is_proc_macro); + + // At export time we expand all source file paths to absolute paths because + // downstream compilation sessions can have a different compiler working + // directory, so relative paths from this or any other upstream crate + // won't be valid anymore. + // + // At this point we also erase the actual on-disk path and only keep + // the remapped version -- as is necessary for reproducible builds. + let mut source_file = match source_file.name { + FileName::Real(ref original_file_name) => { + let adapted_file_name = source_map + .path_mapping() + .to_embeddable_absolute_path(original_file_name.clone(), working_directory); + + if adapted_file_name != *original_file_name { + let mut adapted: SourceFile = (**source_file).clone(); + adapted.name = FileName::Real(adapted_file_name); + adapted.name_hash = { + let mut hasher: StableHasher = StableHasher::new(); + adapted.name.hash(&mut hasher); + hasher.finish::<u128>() + }; + Lrc::new(adapted) + } else { + // Nothing to adapt + source_file.clone() } - // expanded code, not from a file - _ => source_file.clone(), - } - }) - .map(|mut source_file| { - // We're serializing this `SourceFile` into our crate metadata, - // so mark it as coming from this crate. - // This also ensures that we don't try to deserialize the - // `CrateNum` for a proc-macro dependency - since proc macro - // dependencies aren't loaded when we deserialize a proc-macro, - // trying to remap the `CrateNum` would fail. - if self.is_proc_macro { - Lrc::make_mut(&mut source_file).cnum = LOCAL_CRATE; } - source_file - }) - .collect::<Vec<_>>(); + // expanded code, not from a file + _ => source_file.clone(), + }; + + // We're serializing this `SourceFile` into our crate metadata, + // so mark it as coming from this crate. + // This also ensures that we don't try to deserialize the + // `CrateNum` for a proc-macro dependency - since proc macro + // dependencies aren't loaded when we deserialize a proc-macro, + // trying to remap the `CrateNum` would fail. + if self.is_proc_macro { + Lrc::make_mut(&mut source_file).cnum = LOCAL_CRATE; + } - self.lazy_array(adapted.iter().map(|rc| &**rc)) + let on_disk_index: u32 = + on_disk_index.try_into().expect("cannot export more than U32_MAX files"); + adapted.set(on_disk_index, self.lazy(source_file)); + } + + adapted.encode(&mut self.opaque) } fn encode_crate_root(&mut self) -> LazyValue<CrateRoot> { let tcx = self.tcx; - let mut i = 0; - let preamble_bytes = self.position() - i; - - // Encode the crate deps - i = self.position(); - let crate_deps = self.encode_crate_deps(); - let dylib_dependency_formats = self.encode_dylib_dependency_formats(); - let dep_bytes = self.position() - i; - - // Encode the lib features. - i = self.position(); - let lib_features = self.encode_lib_features(); - let lib_feature_bytes = self.position() - i; - - // Encode the stability implications. - i = self.position(); - let stability_implications = self.encode_stability_implications(); - let stability_implications_bytes = self.position() - i; - - // Encode the language items. - i = self.position(); - let lang_items = self.encode_lang_items(); - let lang_items_missing = self.encode_lang_items_missing(); - let lang_item_bytes = self.position() - i; - - // Encode the diagnostic items. - i = self.position(); - let diagnostic_items = self.encode_diagnostic_items(); - let diagnostic_item_bytes = self.position() - i; - - // Encode the native libraries used - i = self.position(); - let native_libraries = self.encode_native_libraries(); - let native_lib_bytes = self.position() - i; - - i = self.position(); - let foreign_modules = self.encode_foreign_modules(); - let foreign_modules_bytes = self.position() - i; - - // Encode DefPathTable - i = self.position(); - self.encode_def_path_table(); - let def_path_table_bytes = self.position() - i; + let mut stats: Vec<(&'static str, usize)> = Vec::with_capacity(32); + + macro_rules! stat { + ($label:literal, $f:expr) => {{ + let orig_pos = self.position(); + let res = $f(); + stats.push(($label, self.position() - orig_pos)); + res + }}; + } + + // We have already encoded some things. Get their combined size from the current position. + stats.push(("preamble", self.position())); + + let (crate_deps, dylib_dependency_formats) = + stat!("dep", || (self.encode_crate_deps(), self.encode_dylib_dependency_formats())); + + let lib_features = stat!("lib-features", || self.encode_lib_features()); + + let stability_implications = + stat!("stability-implications", || self.encode_stability_implications()); + + let (lang_items, lang_items_missing) = stat!("lang-items", || { + (self.encode_lang_items(), self.encode_lang_items_missing()) + }); + + let diagnostic_items = stat!("diagnostic-items", || self.encode_diagnostic_items()); + + let native_libraries = stat!("native-libs", || self.encode_native_libraries()); + + let foreign_modules = stat!("foreign-modules", || self.encode_foreign_modules()); + + _ = stat!("def-path-table", || self.encode_def_path_table()); // Encode the def IDs of traits, for rustdoc and diagnostics. - i = self.position(); - let traits = self.encode_traits(); - let traits_bytes = self.position() - i; + let traits = stat!("traits", || self.encode_traits()); // Encode the def IDs of impls, for coherence checking. - i = self.position(); - let impls = self.encode_impls(); - let impls_bytes = self.position() - i; - - i = self.position(); - let incoherent_impls = self.encode_incoherent_impls(); - let incoherent_impls_bytes = self.position() - i; - - // Encode MIR. - i = self.position(); - self.encode_mir(); - let mir_bytes = self.position() - i; - - // Encode the items. - i = self.position(); - self.encode_def_ids(); - self.encode_info_for_items(); - let item_bytes = self.position() - i; - - // Encode the allocation index - i = self.position(); - let interpret_alloc_index = { + let impls = stat!("impls", || self.encode_impls()); + + let incoherent_impls = stat!("incoherent-impls", || self.encode_incoherent_impls()); + + _ = stat!("mir", || self.encode_mir()); + + _ = stat!("items", || { + self.encode_def_ids(); + self.encode_info_for_items(); + }); + + let interpret_alloc_index = stat!("interpret-alloc-index", || { let mut interpret_alloc_index = Vec::new(); let mut n = 0; trace!("beginning to encode alloc ids"); @@ -616,126 +626,90 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { n = new_n; } self.lazy_array(interpret_alloc_index) - }; - let interpret_alloc_index_bytes = self.position() - i; + }); - // Encode the proc macro data. This affects 'tables', - // so we need to do this before we encode the tables. - // This overwrites def_keys, so it must happen after encode_def_path_table. - i = self.position(); - let proc_macro_data = self.encode_proc_macros(); - let proc_macro_data_bytes = self.position() - i; + // Encode the proc macro data. This affects `tables`, so we need to do this before we + // encode the tables. This overwrites def_keys, so it must happen after + // encode_def_path_table. + let proc_macro_data = stat!("proc-macro-data", || self.encode_proc_macros()); - i = self.position(); - let tables = self.tables.encode(&mut self.opaque); - let tables_bytes = self.position() - i; + let tables = stat!("tables", || self.tables.encode(&mut self.opaque)); - i = self.position(); - let debugger_visualizers = self.encode_debugger_visualizers(); - let debugger_visualizers_bytes = self.position() - i; + let debugger_visualizers = + stat!("debugger-visualizers", || self.encode_debugger_visualizers()); // Encode exported symbols info. This is prefetched in `encode_metadata` so we encode // this as late as possible to give the prefetching as much time as possible to complete. - i = self.position(); - let exported_symbols = tcx.exported_symbols(LOCAL_CRATE); - let exported_symbols = self.encode_exported_symbols(&exported_symbols); - let exported_symbols_bytes = self.position() - i; - - // Encode the hygiene data, - // IMPORTANT: this *must* be the last thing that we encode (other than `SourceMap`). The process - // of encoding other items (e.g. `optimized_mir`) may cause us to load - // data from the incremental cache. If this causes us to deserialize a `Span`, - // then we may load additional `SyntaxContext`s into the global `HygieneData`. - // Therefore, we need to encode the hygiene data last to ensure that we encode - // any `SyntaxContext`s that might be used. - i = self.position(); - let (syntax_contexts, expn_data, expn_hashes) = self.encode_hygiene(); - let hygiene_bytes = self.position() - i; - - i = self.position(); - let def_path_hash_map = self.encode_def_path_hash_map(); - let def_path_hash_map_bytes = self.position() - i; - - // Encode source_map. This needs to be done last, - // since encoding `Span`s tells us which `SourceFiles` we actually - // need to encode. - i = self.position(); - let source_map = self.encode_source_map(); - let source_map_bytes = self.position() - i; - - i = self.position(); - let attrs = tcx.hir().krate_attrs(); - let has_default_lib_allocator = tcx.sess.contains_name(&attrs, sym::default_lib_allocator); - let root = self.lazy(CrateRoot { - name: tcx.crate_name(LOCAL_CRATE), - extra_filename: tcx.sess.opts.cg.extra_filename.clone(), - triple: tcx.sess.opts.target_triple.clone(), - hash: tcx.crate_hash(LOCAL_CRATE), - stable_crate_id: tcx.def_path_hash(LOCAL_CRATE.as_def_id()).stable_crate_id(), - required_panic_strategy: tcx.required_panic_strategy(LOCAL_CRATE), - panic_in_drop_strategy: tcx.sess.opts.unstable_opts.panic_in_drop, - edition: tcx.sess.edition(), - has_global_allocator: tcx.has_global_allocator(LOCAL_CRATE), - has_panic_handler: tcx.has_panic_handler(LOCAL_CRATE), - has_default_lib_allocator, - proc_macro_data, - debugger_visualizers, - compiler_builtins: tcx.sess.contains_name(&attrs, sym::compiler_builtins), - needs_allocator: tcx.sess.contains_name(&attrs, sym::needs_allocator), - needs_panic_runtime: tcx.sess.contains_name(&attrs, sym::needs_panic_runtime), - no_builtins: tcx.sess.contains_name(&attrs, sym::no_builtins), - panic_runtime: tcx.sess.contains_name(&attrs, sym::panic_runtime), - profiler_runtime: tcx.sess.contains_name(&attrs, sym::profiler_runtime), - symbol_mangling_version: tcx.sess.opts.get_symbol_mangling_version(), - - crate_deps, - dylib_dependency_formats, - lib_features, - stability_implications, - lang_items, - diagnostic_items, - lang_items_missing, - native_libraries, - foreign_modules, - source_map, - traits, - impls, - incoherent_impls, - exported_symbols, - interpret_alloc_index, - tables, - syntax_contexts, - expn_data, - expn_hashes, - def_path_hash_map, + let exported_symbols = stat!("exported-symbols", || { + self.encode_exported_symbols(&tcx.exported_symbols(LOCAL_CRATE)) + }); + + // Encode the hygiene data. + // IMPORTANT: this *must* be the last thing that we encode (other than `SourceMap`). The + // process of encoding other items (e.g. `optimized_mir`) may cause us to load data from + // the incremental cache. If this causes us to deserialize a `Span`, then we may load + // additional `SyntaxContext`s into the global `HygieneData`. Therefore, we need to encode + // the hygiene data last to ensure that we encode any `SyntaxContext`s that might be used. + let (syntax_contexts, expn_data, expn_hashes) = stat!("hygiene", || self.encode_hygiene()); + + let def_path_hash_map = stat!("def-path-hash-map", || self.encode_def_path_hash_map()); + + // Encode source_map. This needs to be done last, because encoding `Span`s tells us which + // `SourceFiles` we actually need to encode. + let source_map = stat!("source-map", || self.encode_source_map()); + + let root = stat!("final", || { + let attrs = tcx.hir().krate_attrs(); + self.lazy(CrateRoot { + name: tcx.crate_name(LOCAL_CRATE), + extra_filename: tcx.sess.opts.cg.extra_filename.clone(), + triple: tcx.sess.opts.target_triple.clone(), + hash: tcx.crate_hash(LOCAL_CRATE), + stable_crate_id: tcx.def_path_hash(LOCAL_CRATE.as_def_id()).stable_crate_id(), + required_panic_strategy: tcx.required_panic_strategy(LOCAL_CRATE), + panic_in_drop_strategy: tcx.sess.opts.unstable_opts.panic_in_drop, + edition: tcx.sess.edition(), + has_global_allocator: tcx.has_global_allocator(LOCAL_CRATE), + has_panic_handler: tcx.has_panic_handler(LOCAL_CRATE), + has_default_lib_allocator: tcx + .sess + .contains_name(&attrs, sym::default_lib_allocator), + proc_macro_data, + debugger_visualizers, + compiler_builtins: tcx.sess.contains_name(&attrs, sym::compiler_builtins), + needs_allocator: tcx.sess.contains_name(&attrs, sym::needs_allocator), + needs_panic_runtime: tcx.sess.contains_name(&attrs, sym::needs_panic_runtime), + no_builtins: tcx.sess.contains_name(&attrs, sym::no_builtins), + panic_runtime: tcx.sess.contains_name(&attrs, sym::panic_runtime), + profiler_runtime: tcx.sess.contains_name(&attrs, sym::profiler_runtime), + symbol_mangling_version: tcx.sess.opts.get_symbol_mangling_version(), + + crate_deps, + dylib_dependency_formats, + lib_features, + stability_implications, + lang_items, + diagnostic_items, + lang_items_missing, + native_libraries, + foreign_modules, + source_map, + traits, + impls, + incoherent_impls, + exported_symbols, + interpret_alloc_index, + tables, + syntax_contexts, + expn_data, + expn_hashes, + def_path_hash_map, + }) }); - let final_bytes = self.position() - i; let total_bytes = self.position(); - let computed_total_bytes = preamble_bytes - + dep_bytes - + lib_feature_bytes - + stability_implications_bytes - + lang_item_bytes - + diagnostic_item_bytes - + native_lib_bytes - + foreign_modules_bytes - + def_path_table_bytes - + traits_bytes - + impls_bytes - + incoherent_impls_bytes - + mir_bytes - + item_bytes - + interpret_alloc_index_bytes - + proc_macro_data_bytes - + tables_bytes - + debugger_visualizers_bytes - + exported_symbols_bytes - + hygiene_bytes - + def_path_hash_map_bytes - + source_map_bytes - + final_bytes; + let computed_total_bytes: usize = stats.iter().map(|(_, size)| size).sum(); assert_eq!(total_bytes, computed_total_bytes); if tcx.sess.meta_stats() { @@ -753,48 +727,77 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } assert_eq!(self.opaque.file().stream_position().unwrap(), pos_before_rewind); + stats.sort_by_key(|&(_, usize)| usize); + + let prefix = "meta-stats"; let perc = |bytes| (bytes * 100) as f64 / total_bytes as f64; - let p = |label, bytes| { - eprintln!("{:>21}: {:>8} bytes ({:4.1}%)", label, bytes, perc(bytes)); - }; - eprintln!(""); + eprintln!("{} METADATA STATS", prefix); + eprintln!("{} {:<23}{:>10}", prefix, "Section", "Size"); + eprintln!( + "{} ----------------------------------------------------------------", + prefix + ); + for (label, size) in stats { + eprintln!( + "{} {:<23}{:>10} ({:4.1}%)", + prefix, + label, + to_readable_str(size), + perc(size) + ); + } eprintln!( - "{} metadata bytes, of which {} bytes ({:.1}%) are zero", - total_bytes, - zero_bytes, + "{} ----------------------------------------------------------------", + prefix + ); + eprintln!( + "{} {:<23}{:>10} (of which {:.1}% are zero bytes)", + prefix, + "Total", + to_readable_str(total_bytes), perc(zero_bytes) ); - p("preamble", preamble_bytes); - p("dep", dep_bytes); - p("lib feature", lib_feature_bytes); - p("stability_implications", stability_implications_bytes); - p("lang item", lang_item_bytes); - p("diagnostic item", diagnostic_item_bytes); - p("native lib", native_lib_bytes); - p("foreign modules", foreign_modules_bytes); - p("def-path table", def_path_table_bytes); - p("traits", traits_bytes); - p("impls", impls_bytes); - p("incoherent_impls", incoherent_impls_bytes); - p("mir", mir_bytes); - p("item", item_bytes); - p("interpret_alloc_index", interpret_alloc_index_bytes); - p("proc-macro-data", proc_macro_data_bytes); - p("tables", tables_bytes); - p("debugger visualizers", debugger_visualizers_bytes); - p("exported symbols", exported_symbols_bytes); - p("hygiene", hygiene_bytes); - p("def-path hashes", def_path_hash_map_bytes); - p("source_map", source_map_bytes); - p("final", final_bytes); - eprintln!(""); + eprintln!("{}", prefix); } root } } +/// Returns whether an attribute needs to be recorded in metadata, that is, if it's usable and +/// useful in downstream crates. Local-only attributes are an obvious example, but some +/// rustdoc-specific attributes can equally be of use while documenting the current crate only. +/// +/// Removing these superfluous attributes speeds up compilation by making the metadata smaller. +/// +/// Note: the `is_def_id_public` parameter is used to cache whether the given `DefId` has a public +/// visibility: this is a piece of data that can be computed once per defid, and not once per +/// attribute. Some attributes would only be usable downstream if they are public. +#[inline] +fn should_encode_attr( + tcx: TyCtxt<'_>, + attr: &Attribute, + def_id: LocalDefId, + is_def_id_public: &mut Option<bool>, +) -> bool { + if rustc_feature::is_builtin_only_local(attr.name_or_empty()) { + // Attributes marked local-only don't need to be encoded for downstream crates. + false + } else if attr.doc_str().is_some() { + // We keep all public doc comments because they might be "imported" into downstream crates + // if they use `#[doc(inline)]` to copy an item's documentation into their own. + *is_def_id_public + .get_or_insert_with(|| tcx.effective_visibilities(()).effective_vis(def_id).is_some()) + } else if attr.has_name(sym::doc) { + // If this is a `doc` attribute, and it's marked `inline` (as in `#[doc(inline)]`), we can + // remove it. It won't be inlinable in downstream crates. + attr.meta_item_list().map(|l| l.iter().any(|l| !l.has_name(sym::inline))).unwrap_or(false) + } else { + true + } +} + fn should_encode_visibility(def_kind: DefKind) -> bool { match def_kind { DefKind::Mod @@ -817,6 +820,7 @@ fn should_encode_visibility(def_kind: DefKind) -> bool { | DefKind::Use | DefKind::ForeignMod | DefKind::OpaqueTy + | DefKind::ImplTraitPlaceholder | DefKind::Impl | DefKind::Field => true, DefKind::TyParam @@ -849,6 +853,7 @@ fn should_encode_stability(def_kind: DefKind) -> bool { | DefKind::ForeignMod | DefKind::TyAlias | DefKind::OpaqueTy + | DefKind::ImplTraitPlaceholder | DefKind::Enum | DefKind::Union | DefKind::Impl @@ -937,6 +942,7 @@ fn should_encode_variances(def_kind: DefKind) -> bool { | DefKind::ForeignMod | DefKind::TyAlias | DefKind::OpaqueTy + | DefKind::ImplTraitPlaceholder | DefKind::Impl | DefKind::Trait | DefKind::TraitAlias @@ -973,6 +979,7 @@ fn should_encode_generics(def_kind: DefKind) -> bool { | DefKind::AnonConst | DefKind::InlineConst | DefKind::OpaqueTy + | DefKind::ImplTraitPlaceholder | DefKind::Impl | DefKind::Field | DefKind::TyParam @@ -989,14 +996,141 @@ fn should_encode_generics(def_kind: DefKind) -> bool { } } +fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) -> bool { + match def_kind { + DefKind::Struct + | DefKind::Union + | DefKind::Enum + | DefKind::Variant + | DefKind::Ctor(..) + | DefKind::Field + | DefKind::Fn + | DefKind::Const + | DefKind::Static(..) + | DefKind::TyAlias + | DefKind::OpaqueTy + | DefKind::ForeignTy + | DefKind::Impl + | DefKind::AssocFn + | DefKind::AssocConst + | DefKind::Closure + | DefKind::Generator + | DefKind::ConstParam + | DefKind::AnonConst + | DefKind::InlineConst => true, + + DefKind::ImplTraitPlaceholder => { + let parent_def_id = tcx.impl_trait_in_trait_parent(def_id.to_def_id()); + let assoc_item = tcx.associated_item(parent_def_id); + match assoc_item.container { + // Always encode an RPIT in an impl fn, since it always has a body + ty::AssocItemContainer::ImplContainer => true, + ty::AssocItemContainer::TraitContainer => { + // Encode an RPIT for a trait only if the trait has a default body + assoc_item.defaultness(tcx).has_value() + } + } + } + + DefKind::AssocTy => { + let assoc_item = tcx.associated_item(def_id); + match assoc_item.container { + ty::AssocItemContainer::ImplContainer => true, + ty::AssocItemContainer::TraitContainer => assoc_item.defaultness(tcx).has_value(), + } + } + DefKind::TyParam => { + let hir::Node::GenericParam(param) = tcx.hir().get_by_def_id(def_id) else { bug!() }; + let hir::GenericParamKind::Type { default, .. } = param.kind else { bug!() }; + default.is_some() + } + + DefKind::Trait + | DefKind::TraitAlias + | DefKind::Mod + | DefKind::ForeignMod + | DefKind::Macro(..) + | DefKind::Use + | DefKind::LifetimeParam + | DefKind::GlobalAsm + | DefKind::ExternCrate => false, + } +} + +fn should_encode_const(def_kind: DefKind) -> bool { + match def_kind { + DefKind::Const | DefKind::AssocConst | DefKind::AnonConst => true, + + DefKind::Struct + | DefKind::Union + | DefKind::Enum + | DefKind::Variant + | DefKind::Ctor(..) + | DefKind::Field + | DefKind::Fn + | DefKind::Static(..) + | DefKind::TyAlias + | DefKind::OpaqueTy + | DefKind::ImplTraitPlaceholder + | DefKind::ForeignTy + | DefKind::Impl + | DefKind::AssocFn + | DefKind::Closure + | DefKind::Generator + | DefKind::ConstParam + | DefKind::InlineConst + | DefKind::AssocTy + | DefKind::TyParam + | DefKind::Trait + | DefKind::TraitAlias + | DefKind::Mod + | DefKind::ForeignMod + | DefKind::Macro(..) + | DefKind::Use + | DefKind::LifetimeParam + | DefKind::GlobalAsm + | DefKind::ExternCrate => false, + } +} + +fn should_encode_trait_impl_trait_tys<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool { + if tcx.def_kind(def_id) != DefKind::AssocFn { + return false; + } + + let Some(item) = tcx.opt_associated_item(def_id) else { return false; }; + if item.container != ty::AssocItemContainer::ImplContainer { + return false; + } + + let Some(trait_item_def_id) = item.trait_item_def_id else { return false; }; + + // FIXME(RPITIT): This does a somewhat manual walk through the signature + // of the trait fn to look for any RPITITs, but that's kinda doing a lot + // of work. We can probably remove this when we refactor RPITITs to be + // associated types. + tcx.fn_sig(trait_item_def_id).skip_binder().output().walk().any(|arg| { + if let ty::GenericArgKind::Type(ty) = arg.unpack() + && let ty::Projection(data) = ty.kind() + && tcx.def_kind(data.item_def_id) == DefKind::ImplTraitPlaceholder + { + true + } else { + false + } + }) +} + impl<'a, 'tcx> EncodeContext<'a, 'tcx> { fn encode_attrs(&mut self, def_id: LocalDefId) { - let mut attrs = self - .tcx + let tcx = self.tcx; + let mut is_public: Option<bool> = None; + + let mut attrs = tcx .hir() - .attrs(self.tcx.hir().local_def_id_to_hir_id(def_id)) + .attrs(tcx.hir().local_def_id_to_hir_id(def_id)) .iter() - .filter(|attr| !rustc_feature::is_builtin_only_local(attr.name_or_empty())); + .filter(move |attr| should_encode_attr(tcx, attr, def_id, &mut is_public)); record_array!(self.tables.attributes[def_id.to_def_id()] <- attrs.clone()); if attrs.any(|attr| attr.may_have_doc_links()) { @@ -1014,7 +1148,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let def_kind = tcx.opt_def_kind(local_id); let Some(def_kind) = def_kind else { continue }; self.tables.opt_def_kind.set(def_id.index, def_kind); - record!(self.tables.def_span[def_id] <- tcx.def_span(def_id)); + let def_span = tcx.def_span(local_id); + record!(self.tables.def_span[def_id] <- def_span); self.encode_attrs(local_id); record!(self.tables.expn_that_defined[def_id] <- self.tcx.expn_that_defined(def_id)); if let Some(ident_span) = tcx.def_ident_span(def_id) { @@ -1024,11 +1159,14 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { record!(self.tables.codegen_fn_attrs[def_id] <- self.tcx.codegen_fn_attrs(def_id)); } if should_encode_visibility(def_kind) { - record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); + let vis = + self.tcx.local_visibility(local_id).map_id(|def_id| def_id.local_def_index); + record!(self.tables.visibility[def_id] <- vis); } if should_encode_stability(def_kind) { self.encode_stability(def_id); self.encode_const_stability(def_id); + self.encode_default_body_stability(def_id); self.encode_deprecation(def_id); } if should_encode_variances(def_kind) { @@ -1044,9 +1182,25 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { record_array!(self.tables.inferred_outlives_of[def_id] <- inferred_outlives); } } + if should_encode_type(tcx, local_id, def_kind) { + record!(self.tables.type_of[def_id] <- self.tcx.type_of(def_id)); + } + if let DefKind::TyParam = def_kind { + let default = self.tcx.object_lifetime_default(def_id); + record!(self.tables.object_lifetime_default[def_id] <- default); + } if let DefKind::Trait | DefKind::TraitAlias = def_kind { record!(self.tables.super_predicates_of[def_id] <- self.tcx.super_predicates_of(def_id)); } + if let DefKind::Enum | DefKind::Struct | DefKind::Union = def_kind { + let params_in_repr = self.tcx.params_in_repr(def_id); + record!(self.tables.params_in_repr[def_id] <- params_in_repr); + } + if should_encode_trait_impl_trait_tys(tcx, def_id) + && let Ok(table) = self.tcx.collect_trait_impl_trait_tys(def_id) + { + record!(self.tables.trait_impl_trait_tys[def_id] <- table); + } } let inherent_impls = tcx.crate_inherent_impls(()); for (def_id, implementations) in inherent_impls.inherent_impls.iter() { @@ -1060,11 +1214,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } } - fn encode_item_type(&mut self, def_id: DefId) { - debug!("EncodeContext::encode_item_type({:?})", def_id); - record!(self.tables.type_of[def_id] <- self.tcx.type_of(def_id)); - } - fn encode_enum_variant_info(&mut self, def: ty::AdtDef<'tcx>, index: VariantIdx) { let tcx = self.tcx; let variant = &def.variant(index); @@ -1078,13 +1227,12 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { is_non_exhaustive: variant.is_field_list_non_exhaustive(), }; - record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data))); + record!(self.tables.variant_data[def_id] <- data); self.tables.constness.set(def_id.index, hir::Constness::Const); record_array!(self.tables.children[def_id] <- variant.fields.iter().map(|f| { assert!(f.did.is_local()); f.did.index })); - self.encode_item_type(def_id); if variant.ctor_kind == CtorKind::Fn { // FIXME(eddyb) encode signature only in `encode_enum_variant_ctor`. if let Some(ctor_def_id) = variant.ctor_def_id { @@ -1107,9 +1255,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { is_non_exhaustive: variant.is_field_list_non_exhaustive(), }; - record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data))); + record!(self.tables.variant_data[def_id] <- data); self.tables.constness.set(def_id.index, hir::Constness::Const); - self.encode_item_type(def_id); if variant.ctor_kind == CtorKind::Fn { record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id)); } @@ -1126,15 +1273,12 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { // code uses it). However, we skip encoding anything relating to child // items - we encode information about proc-macros later on. let reexports = if !self.is_proc_macro { - match tcx.module_reexports(local_def_id) { - Some(exports) => self.lazy_array(exports), - _ => LazyArray::empty(), - } + tcx.module_reexports(local_def_id).unwrap_or(&[]) } else { - LazyArray::empty() + &[] }; - record!(self.tables.kind[def_id] <- EntryKind::Mod(reexports)); + record_array!(self.tables.module_reexports[def_id] <- reexports); if self.is_proc_macro { // Encode this here because we don't do it in encode_def_ids. record!(self.tables.expn_that_defined[def_id] <- tcx.expn_that_defined(local_def_id)); @@ -1146,14 +1290,21 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { // from name resolution point of view. hir::ItemKind::ForeignMod { items, .. } => { for foreign_item in items { - yield foreign_item.id.def_id.local_def_index; + yield foreign_item.id.owner_id.def_id.local_def_index; } } // Only encode named non-reexport children, reexports are encoded // separately and unnamed items are not used by name resolution. hir::ItemKind::ExternCrate(..) => continue, - _ if tcx.def_key(item_id.def_id.to_def_id()).get_opt_name().is_some() => { - yield item_id.def_id.local_def_index; + hir::ItemKind::Struct(ref vdata, _) => { + yield item_id.owner_id.def_id.local_def_index; + // Encode constructors which take a separate slot in value namespace. + if let Some(ctor_hir_id) = vdata.ctor_hir_id() { + yield tcx.hir().local_def_id(ctor_hir_id).local_def_index; + } + } + _ if tcx.def_key(item_id.owner_id.to_def_id()).get_opt_name().is_some() => { + yield item_id.owner_id.def_id.local_def_index; } _ => continue, } @@ -1162,22 +1313,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } } - fn encode_field( - &mut self, - adt_def: ty::AdtDef<'tcx>, - variant_index: VariantIdx, - field_index: usize, - ) { - let variant = &adt_def.variant(variant_index); - let field = &variant.fields[field_index]; - - let def_id = field.did; - debug!("EncodeContext::encode_field({:?})", def_id); - - record!(self.tables.kind[def_id] <- EntryKind::Field); - self.encode_item_type(def_id); - } - fn encode_struct_ctor(&mut self, adt_def: ty::AdtDef<'tcx>, def_id: DefId) { debug!("EncodeContext::encode_struct_ctor({:?})", def_id); let tcx = self.tcx; @@ -1191,9 +1326,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { }; record!(self.tables.repr_options[def_id] <- adt_def.repr()); + record!(self.tables.variant_data[def_id] <- data); self.tables.constness.set(def_id.index, hir::Constness::Const); - record!(self.tables.kind[def_id] <- EntryKind::Struct(self.lazy(data))); - self.encode_item_type(def_id); if variant.ctor_kind == CtorKind::Fn { record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id)); } @@ -1214,18 +1348,10 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let ast_item = tcx.hir().expect_trait_item(def_id.expect_local()); self.tables.impl_defaultness.set(def_id.index, ast_item.defaultness); let trait_item = tcx.associated_item(def_id); + self.tables.assoc_container.set(def_id.index, trait_item.container); match trait_item.kind { - ty::AssocKind::Const => { - let rendered = rustc_hir_pretty::to_string( - &(&self.tcx.hir() as &dyn intravisit::Map<'_>), - |s| s.print_trait_item(ast_item), - ); - - record!(self.tables.kind[def_id] <- EntryKind::AssocConst(ty::AssocItemContainer::TraitContainer)); - record!(self.tables.mir_const_qualif[def_id] <- mir::ConstQualifs::default()); - record!(self.tables.rendered_const[def_id] <- rendered); - } + ty::AssocKind::Const => {} ty::AssocKind::Fn => { let hir::TraitItemKind::Fn(m_sig, m) = &ast_item.kind else { bug!() }; match *m { @@ -1238,24 +1364,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { }; self.tables.asyncness.set(def_id.index, m_sig.header.asyncness); self.tables.constness.set(def_id.index, hir::Constness::NotConst); - record!(self.tables.kind[def_id] <- EntryKind::AssocFn { - container: ty::AssocItemContainer::TraitContainer, - has_self: trait_item.fn_has_self_parameter, - }); } ty::AssocKind::Type => { self.encode_explicit_item_bounds(def_id); - record!(self.tables.kind[def_id] <- EntryKind::AssocType(ty::AssocItemContainer::TraitContainer)); - } - } - match trait_item.kind { - ty::AssocKind::Const | ty::AssocKind::Fn => { - self.encode_item_type(def_id); - } - ty::AssocKind::Type => { - if ast_item.defaultness.has_value() { - self.encode_item_type(def_id); - } } } if trait_item.kind == ty::AssocKind::Fn { @@ -1270,20 +1381,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let ast_item = self.tcx.hir().expect_impl_item(def_id.expect_local()); self.tables.impl_defaultness.set(def_id.index, ast_item.defaultness); let impl_item = self.tcx.associated_item(def_id); + self.tables.assoc_container.set(def_id.index, impl_item.container); match impl_item.kind { - ty::AssocKind::Const => { - if let hir::ImplItemKind::Const(_, body_id) = ast_item.kind { - let qualifs = self.tcx.at(ast_item.span).mir_const_qualif(def_id); - let const_data = self.encode_rendered_const_for_body(body_id); - - record!(self.tables.kind[def_id] <- EntryKind::AssocConst(ty::AssocItemContainer::ImplContainer)); - record!(self.tables.mir_const_qualif[def_id] <- qualifs); - record!(self.tables.rendered_const[def_id] <- const_data); - } else { - bug!() - } - } ty::AssocKind::Fn => { let hir::ImplItemKind::Fn(ref sig, body) = ast_item.kind else { bug!() }; self.tables.asyncness.set(def_id.index, sig.header.asyncness); @@ -1295,16 +1395,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { hir::Constness::NotConst }; self.tables.constness.set(def_id.index, constness); - record!(self.tables.kind[def_id] <- EntryKind::AssocFn { - container: ty::AssocItemContainer::ImplContainer, - has_self: impl_item.fn_has_self_parameter, - }); - } - ty::AssocKind::Type => { - record!(self.tables.kind[def_id] <- EntryKind::AssocType(ty::AssocItemContainer::ImplContainer)); } + ty::AssocKind::Const | ty::AssocKind::Type => {} } - self.encode_item_type(def_id); if let Some(trait_item_def_id) = impl_item.trait_item_def_id { self.tables.trait_item_def_id.set(def_id.index, trait_item_def_id.into()); } @@ -1321,44 +1414,62 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { return; } - let keys_and_jobs = self - .tcx - .mir_keys(()) - .iter() - .filter_map(|&def_id| { - let (encode_const, encode_opt) = should_encode_mir(self.tcx, def_id); - if encode_const || encode_opt { - Some((def_id, encode_const, encode_opt)) - } else { - None - } - }) - .collect::<Vec<_>>(); - for (def_id, encode_const, encode_opt) in keys_and_jobs.into_iter() { + let tcx = self.tcx; + + let keys_and_jobs = tcx.mir_keys(()).iter().filter_map(|&def_id| { + let (encode_const, encode_opt) = should_encode_mir(tcx, def_id); + if encode_const || encode_opt { Some((def_id, encode_const, encode_opt)) } else { None } + }); + for (def_id, encode_const, encode_opt) in keys_and_jobs { debug_assert!(encode_const || encode_opt); debug!("EntryBuilder::encode_mir({:?})", def_id); if encode_opt { - record!(self.tables.optimized_mir[def_id.to_def_id()] <- self.tcx.optimized_mir(def_id)); + record!(self.tables.optimized_mir[def_id.to_def_id()] <- tcx.optimized_mir(def_id)); } if encode_const { - record!(self.tables.mir_for_ctfe[def_id.to_def_id()] <- self.tcx.mir_for_ctfe(def_id)); + record!(self.tables.mir_for_ctfe[def_id.to_def_id()] <- tcx.mir_for_ctfe(def_id)); // FIXME(generic_const_exprs): this feels wrong to have in `encode_mir` - let abstract_const = self.tcx.thir_abstract_const(def_id); + let abstract_const = tcx.thir_abstract_const(def_id); if let Ok(Some(abstract_const)) = abstract_const { record!(self.tables.thir_abstract_const[def_id.to_def_id()] <- abstract_const); } + + if should_encode_const(tcx.def_kind(def_id)) { + let qualifs = tcx.mir_const_qualif(def_id); + record!(self.tables.mir_const_qualif[def_id.to_def_id()] <- qualifs); + let body_id = tcx.hir().maybe_body_owned_by(def_id); + if let Some(body_id) = body_id { + let const_data = self.encode_rendered_const_for_body(body_id); + record!(self.tables.rendered_const[def_id.to_def_id()] <- const_data); + } + } } - record!(self.tables.promoted_mir[def_id.to_def_id()] <- self.tcx.promoted_mir(def_id)); + record!(self.tables.promoted_mir[def_id.to_def_id()] <- tcx.promoted_mir(def_id)); let instance = ty::InstanceDef::Item(ty::WithOptConstParam::unknown(def_id.to_def_id())); - let unused = self.tcx.unused_generic_params(instance); + let unused = tcx.unused_generic_params(instance); if !unused.is_empty() { record!(self.tables.unused_generic_params[def_id.to_def_id()] <- unused); } } + + // Encode all the deduced parameter attributes for everything that has MIR, even for items + // that can't be inlined. But don't if we aren't optimizing in non-incremental mode, to + // save the query traffic. + if tcx.sess.opts.output_types.should_codegen() + && tcx.sess.opts.optimize != OptLevel::No + && tcx.sess.opts.incremental.is_none() + { + for &local_def_id in tcx.mir_keys(()) { + if let DefKind::AssocFn | DefKind::Fn = tcx.def_kind(local_def_id) { + record_array!(self.tables.deduced_param_attrs[local_def_id.to_def_id()] <- + self.tcx.deduced_param_attrs(local_def_id.to_def_id())); + } + } + } } fn encode_stability(&mut self, def_id: DefId) { @@ -1385,6 +1496,18 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } } + fn encode_default_body_stability(&mut self, def_id: DefId) { + debug!("EncodeContext::encode_default_body_stability({:?})", def_id); + + // The query lookup can take a measurable amount of time in crates with many items. Check if + // the stability attributes are even enabled before using their queries. + if self.feat.staged_api || self.tcx.sess.opts.unstable_opts.force_unstable_if_unmarked { + if let Some(stab) = self.tcx.lookup_default_body_stability(def_id) { + record!(self.tables.lookup_default_body_stability[def_id] <- stab) + } + } + } + fn encode_deprecation(&mut self, def_id: DefId) { debug!("EncodeContext::encode_deprecation({:?})", def_id); if let Some(depr) = self.tcx.lookup_deprecation(def_id) { @@ -1405,38 +1528,27 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { debug!("EncodeContext::encode_info_for_item({:?})", def_id); - let entry_kind = match item.kind { - hir::ItemKind::Static(..) => EntryKind::Static, - hir::ItemKind::Const(_, body_id) => { - let qualifs = self.tcx.at(item.span).mir_const_qualif(def_id); - let const_data = self.encode_rendered_const_for_body(body_id); - record!(self.tables.mir_const_qualif[def_id] <- qualifs); - record!(self.tables.rendered_const[def_id] <- const_data); - EntryKind::Const - } + match item.kind { hir::ItemKind::Fn(ref sig, .., body) => { self.tables.asyncness.set(def_id.index, sig.header.asyncness); record_array!(self.tables.fn_arg_names[def_id] <- self.tcx.hir().body_param_names(body)); self.tables.constness.set(def_id.index, sig.header.constness); - EntryKind::Fn } hir::ItemKind::Macro(ref macro_def, _) => { - EntryKind::MacroDef(self.lazy(&*macro_def.body), macro_def.macro_rules) + if macro_def.macro_rules { + self.tables.macro_rules.set(def_id.index, ()); + } + record!(self.tables.macro_definition[def_id] <- &*macro_def.body); } hir::ItemKind::Mod(ref m) => { - return self.encode_info_for_mod(item.def_id, m); + return self.encode_info_for_mod(item.owner_id.def_id, m); } - hir::ItemKind::ForeignMod { .. } => EntryKind::ForeignMod, - hir::ItemKind::GlobalAsm(..) => EntryKind::GlobalAsm, - hir::ItemKind::TyAlias(..) => EntryKind::Type, hir::ItemKind::OpaqueTy(..) => { self.encode_explicit_item_bounds(def_id); - EntryKind::OpaqueTy } hir::ItemKind::Enum(..) => { let adt_def = self.tcx.adt_def(def_id); record!(self.tables.repr_options[def_id] <- adt_def.repr()); - EntryKind::Enum } hir::ItemKind::Struct(ref struct_def, _) => { let adt_def = self.tcx.adt_def(def_id); @@ -1451,24 +1563,24 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { .map(|ctor_hir_id| self.tcx.hir().local_def_id(ctor_hir_id).local_def_index); let variant = adt_def.non_enum_variant(); - EntryKind::Struct(self.lazy(VariantData { + record!(self.tables.variant_data[def_id] <- VariantData { ctor_kind: variant.ctor_kind, discr: variant.discr, ctor, is_non_exhaustive: variant.is_field_list_non_exhaustive(), - })) + }); } hir::ItemKind::Union(..) => { let adt_def = self.tcx.adt_def(def_id); record!(self.tables.repr_options[def_id] <- adt_def.repr()); let variant = adt_def.non_enum_variant(); - EntryKind::Union(self.lazy(VariantData { + record!(self.tables.variant_data[def_id] <- VariantData { ctor_kind: variant.ctor_kind, discr: variant.discr, ctor: None, is_non_exhaustive: variant.is_field_list_non_exhaustive(), - })) + }); } hir::ItemKind::Impl(hir::Impl { defaultness, constness, .. }) => { self.tables.impl_defaultness.set(def_id.index, *defaultness); @@ -1494,34 +1606,37 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let polarity = self.tcx.impl_polarity(def_id); self.tables.impl_polarity.set(def_id.index, polarity); - - EntryKind::Impl } hir::ItemKind::Trait(..) => { let trait_def = self.tcx.trait_def(def_id); record!(self.tables.trait_def[def_id] <- trait_def); - - EntryKind::Trait } hir::ItemKind::TraitAlias(..) => { let trait_def = self.tcx.trait_def(def_id); record!(self.tables.trait_def[def_id] <- trait_def); - - EntryKind::TraitAlias } hir::ItemKind::ExternCrate(_) | hir::ItemKind::Use(..) => { bug!("cannot encode info for item {:?}", item) } + hir::ItemKind::Static(..) + | hir::ItemKind::Const(..) + | hir::ItemKind::ForeignMod { .. } + | hir::ItemKind::GlobalAsm(..) + | hir::ItemKind::TyAlias(..) => {} }; - record!(self.tables.kind[def_id] <- entry_kind); // FIXME(eddyb) there should be a nicer way to do this. match item.kind { - hir::ItemKind::Enum(..) => record_array!(self.tables.children[def_id] <- - self.tcx.adt_def(def_id).variants().iter().map(|v| { - assert!(v.def_id.is_local()); - v.def_id.index - }) - ), + hir::ItemKind::Enum(..) => { + record_array!(self.tables.children[def_id] <- iter::from_generator(|| + for variant in tcx.adt_def(def_id).variants() { + yield variant.def_id.index; + // Encode constructors which take a separate slot in value namespace. + if let Some(ctor_def_id) = variant.ctor_def_id { + yield ctor_def_id.index; + } + } + )) + } hir::ItemKind::Struct(..) | hir::ItemKind::Union(..) => { record_array!(self.tables.children[def_id] <- self.tcx.adt_def(def_id).non_enum_variant().fields.iter().map(|f| { @@ -1541,18 +1656,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } _ => {} } - match item.kind { - hir::ItemKind::Static(..) - | hir::ItemKind::Const(..) - | hir::ItemKind::Fn(..) - | hir::ItemKind::TyAlias(..) - | hir::ItemKind::OpaqueTy(..) - | hir::ItemKind::Enum(..) - | hir::ItemKind::Struct(..) - | hir::ItemKind::Union(..) - | hir::ItemKind::Impl { .. } => self.encode_item_type(def_id), - _ => {} - } if let hir::ItemKind::Fn(..) = item.kind { record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id)); if tcx.is_intrinsic(def_id) { @@ -1564,12 +1667,44 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { record!(self.tables.impl_trait_ref[def_id] <- trait_ref); } } - } + // In some cases, along with the item itself, we also + // encode some sub-items. Usually we want some info from the item + // so it's easier to do that here then to wait until we would encounter + // normally in the visitor walk. + match item.kind { + hir::ItemKind::Enum(..) => { + let def = self.tcx.adt_def(item.owner_id.to_def_id()); + for (i, variant) in def.variants().iter_enumerated() { + self.encode_enum_variant_info(def, i); - fn encode_info_for_generic_param(&mut self, def_id: DefId, kind: EntryKind, encode_type: bool) { - record!(self.tables.kind[def_id] <- kind); - if encode_type { - self.encode_item_type(def_id); + if let Some(_ctor_def_id) = variant.ctor_def_id { + self.encode_enum_variant_ctor(def, i); + } + } + } + hir::ItemKind::Struct(ref struct_def, _) => { + let def = self.tcx.adt_def(item.owner_id.to_def_id()); + // If the struct has a constructor, encode it. + if let Some(ctor_hir_id) = struct_def.ctor_hir_id() { + let ctor_def_id = self.tcx.hir().local_def_id(ctor_hir_id); + self.encode_struct_ctor(def, ctor_def_id.to_def_id()); + } + } + hir::ItemKind::Impl { .. } => { + for &trait_item_def_id in + self.tcx.associated_item_def_ids(item.owner_id.to_def_id()).iter() + { + self.encode_info_for_impl_item(trait_item_def_id); + } + } + hir::ItemKind::Trait(..) => { + for &item_def_id in + self.tcx.associated_item_def_ids(item.owner_id.to_def_id()).iter() + { + self.encode_info_for_trait_item(item_def_id); + } + } + _ => {} } } @@ -1584,34 +1719,16 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { ty::Generator(..) => { let data = self.tcx.generator_kind(def_id).unwrap(); let generator_diagnostic_data = typeck_result.get_generator_diagnostic_data(); - record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::Generator); record!(self.tables.generator_kind[def_id.to_def_id()] <- data); record!(self.tables.generator_diagnostic_data[def_id.to_def_id()] <- generator_diagnostic_data); } - ty::Closure(..) => { - record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::Closure); + ty::Closure(_, substs) => { + record!(self.tables.fn_sig[def_id.to_def_id()] <- substs.as_closure().sig()); } _ => bug!("closure that is neither generator nor closure"), } - self.encode_item_type(def_id.to_def_id()); - if let ty::Closure(def_id, substs) = *ty.kind() { - record!(self.tables.fn_sig[def_id] <- substs.as_closure().sig()); - } - } - - fn encode_info_for_anon_const(&mut self, id: hir::HirId) { - let def_id = self.tcx.hir().local_def_id(id); - debug!("EncodeContext::encode_info_for_anon_const({:?})", def_id); - let body_id = self.tcx.hir().body_owned_by(def_id); - let const_data = self.encode_rendered_const_for_body(body_id); - let qualifs = self.tcx.mir_const_qualif(def_id); - - record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::AnonConst); - record!(self.tables.mir_const_qualif[def_id.to_def_id()] <- qualifs); - record!(self.tables.rendered_const[def_id.to_def_id()] <- const_data); - self.encode_item_type(def_id.to_def_id()); } fn encode_native_libraries(&mut self) -> LazyArray<NativeLib> { @@ -1670,7 +1787,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { self.tables.opt_def_kind.set(LOCAL_CRATE.as_def_id().index, DefKind::Mod); record!(self.tables.def_span[LOCAL_CRATE.as_def_id()] <- tcx.def_span(LOCAL_CRATE.as_def_id())); self.encode_attrs(LOCAL_CRATE.as_def_id().expect_local()); - record!(self.tables.visibility[LOCAL_CRATE.as_def_id()] <- tcx.visibility(LOCAL_CRATE.as_def_id())); + let vis = tcx.local_visibility(CRATE_DEF_ID).map_id(|def_id| def_id.local_def_index); + record!(self.tables.visibility[LOCAL_CRATE.as_def_id()] <- vis); if let Some(stability) = stability { record!(self.tables.lookup_stability[LOCAL_CRATE.as_def_id()] <- stability); } @@ -1709,7 +1827,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let def_id = id.to_def_id(); self.tables.opt_def_kind.set(def_id.index, DefKind::Macro(macro_kind)); - record!(self.tables.kind[def_id] <- EntryKind::ProcMacro(macro_kind)); + self.tables.proc_macro.set(def_id.index, macro_kind); self.encode_attrs(id); record!(self.tables.def_keys[def_id] <- def_key); record!(self.tables.def_ident_span[def_id] <- span); @@ -1822,8 +1940,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { FxHashMap::default(); for id in tcx.hir().items() { - if matches!(tcx.def_kind(id.def_id), DefKind::Impl) { - if let Some(trait_ref) = tcx.impl_trait_ref(id.def_id.to_def_id()) { + if matches!(tcx.def_kind(id.owner_id), DefKind::Impl) { + if let Some(trait_ref) = tcx.impl_trait_ref(id.owner_id) { let simplified_self_ty = fast_reject::simplify_type( self.tcx, trait_ref.self_ty(), @@ -1833,7 +1951,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { fx_hash_map .entry(trait_ref.def_id) .or_default() - .push((id.def_id.local_def_index, simplified_self_ty)); + .push((id.owner_id.def_id.local_def_index, simplified_self_ty)); } } } @@ -1947,18 +2065,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { hir::Constness::NotConst }; self.tables.constness.set(def_id.index, constness); - record!(self.tables.kind[def_id] <- EntryKind::ForeignFn); - } - hir::ForeignItemKind::Static(..) => { - record!(self.tables.kind[def_id] <- EntryKind::ForeignStatic); - } - hir::ForeignItemKind::Type => { - record!(self.tables.kind[def_id] <- EntryKind::ForeignType); + record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id)); } + hir::ForeignItemKind::Static(..) | hir::ForeignItemKind::Type => {} } - self.encode_item_type(def_id); if let hir::ForeignItemKind::Fn(..) = nitem.kind { - record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id)); if tcx.is_intrinsic(def_id) { self.tables.is_intrinsic.set(def_id.index, ()); } @@ -1977,21 +2088,16 @@ impl<'a, 'tcx> Visitor<'tcx> for EncodeContext<'a, 'tcx> { intravisit::walk_expr(self, ex); self.encode_info_for_expr(ex); } - fn visit_anon_const(&mut self, c: &'tcx AnonConst) { - intravisit::walk_anon_const(self, c); - self.encode_info_for_anon_const(c.hir_id); - } fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) { intravisit::walk_item(self, item); match item.kind { hir::ItemKind::ExternCrate(_) | hir::ItemKind::Use(..) => {} // ignore these - _ => self.encode_info_for_item(item.def_id.to_def_id(), item), + _ => self.encode_info_for_item(item.owner_id.to_def_id(), item), } - self.encode_addl_info_for_item(item); } fn visit_foreign_item(&mut self, ni: &'tcx hir::ForeignItem<'tcx>) { intravisit::walk_foreign_item(self, ni); - self.encode_info_for_foreign_item(ni.def_id.to_def_id(), ni); + self.encode_info_for_foreign_item(ni.owner_id.to_def_id(), ni); } fn visit_generics(&mut self, generics: &'tcx hir::Generics<'tcx>) { intravisit::walk_generics(self, generics); @@ -2000,29 +2106,13 @@ impl<'a, 'tcx> Visitor<'tcx> for EncodeContext<'a, 'tcx> { } impl<'a, 'tcx> EncodeContext<'a, 'tcx> { - fn encode_fields(&mut self, adt_def: ty::AdtDef<'tcx>) { - for (variant_index, variant) in adt_def.variants().iter_enumerated() { - for (field_index, _field) in variant.fields.iter().enumerate() { - self.encode_field(adt_def, variant_index, field_index); - } - } - } - fn encode_info_for_generics(&mut self, generics: &hir::Generics<'tcx>) { for param in generics.params { let def_id = self.tcx.hir().local_def_id(param.hir_id); match param.kind { - GenericParamKind::Lifetime { .. } => continue, - GenericParamKind::Type { default, .. } => { - self.encode_info_for_generic_param( - def_id.to_def_id(), - EntryKind::TypeParam, - default.is_some(), - ); - } - GenericParamKind::Const { ref default, .. } => { + hir::GenericParamKind::Lifetime { .. } | hir::GenericParamKind::Type { .. } => {} + hir::GenericParamKind::Const { ref default, .. } => { let def_id = def_id.to_def_id(); - self.encode_info_for_generic_param(def_id, EntryKind::ConstParam, true); if default.is_some() { record!(self.tables.const_param_default[def_id] <- self.tcx.const_param_default(def_id)) } @@ -2036,68 +2126,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { self.encode_info_for_closure(expr.hir_id); } } - - /// In some cases, along with the item itself, we also - /// encode some sub-items. Usually we want some info from the item - /// so it's easier to do that here then to wait until we would encounter - /// normally in the visitor walk. - fn encode_addl_info_for_item(&mut self, item: &hir::Item<'_>) { - match item.kind { - hir::ItemKind::Static(..) - | hir::ItemKind::Const(..) - | hir::ItemKind::Fn(..) - | hir::ItemKind::Macro(..) - | hir::ItemKind::Mod(..) - | hir::ItemKind::ForeignMod { .. } - | hir::ItemKind::GlobalAsm(..) - | hir::ItemKind::ExternCrate(..) - | hir::ItemKind::Use(..) - | hir::ItemKind::TyAlias(..) - | hir::ItemKind::OpaqueTy(..) - | hir::ItemKind::TraitAlias(..) => { - // no sub-item recording needed in these cases - } - hir::ItemKind::Enum(..) => { - let def = self.tcx.adt_def(item.def_id.to_def_id()); - self.encode_fields(def); - - for (i, variant) in def.variants().iter_enumerated() { - self.encode_enum_variant_info(def, i); - - if let Some(_ctor_def_id) = variant.ctor_def_id { - self.encode_enum_variant_ctor(def, i); - } - } - } - hir::ItemKind::Struct(ref struct_def, _) => { - let def = self.tcx.adt_def(item.def_id.to_def_id()); - self.encode_fields(def); - - // If the struct has a constructor, encode it. - if let Some(ctor_hir_id) = struct_def.ctor_hir_id() { - let ctor_def_id = self.tcx.hir().local_def_id(ctor_hir_id); - self.encode_struct_ctor(def, ctor_def_id.to_def_id()); - } - } - hir::ItemKind::Union(..) => { - let def = self.tcx.adt_def(item.def_id.to_def_id()); - self.encode_fields(def); - } - hir::ItemKind::Impl { .. } => { - for &trait_item_def_id in - self.tcx.associated_item_def_ids(item.def_id.to_def_id()).iter() - { - self.encode_info_for_impl_item(trait_item_def_id); - } - } - hir::ItemKind::Trait(..) => { - for &item_def_id in self.tcx.associated_item_def_ids(item.def_id.to_def_id()).iter() - { - self.encode_info_for_trait_item(item_def_id); - } - } - } - } } /// Used to prefetch queries which will be needed later by metadata encoding. @@ -2220,7 +2248,7 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path) { fn encode_metadata_impl(tcx: TyCtxt<'_>, path: &Path) { let mut encoder = opaque::FileEncoder::new(path) - .unwrap_or_else(|err| tcx.sess.fatal(&format!("failed to create file encoder: {}", err))); + .unwrap_or_else(|err| tcx.sess.emit_fatal(FailCreateFileEncoder { err })); encoder.emit_raw_bytes(METADATA_HEADER); // Will be filled with the root position after encoding everything. @@ -2228,7 +2256,7 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>, path: &Path) { let source_map_files = tcx.sess.source_map().files(); let source_file_cache = (source_map_files[0].clone(), 0); - let required_source_files = Some(GrowableBitSet::with_capacity(source_map_files.len())); + let required_source_files = Some(FxIndexSet::default()); drop(source_map_files); let hygiene_ctxt = HygieneEncodeContext::default(); @@ -2246,6 +2274,7 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>, path: &Path) { required_source_files, is_proc_macro: tcx.sess.crate_types().contains(&CrateType::ProcMacro), hygiene_ctxt: &hygiene_ctxt, + symbol_table: Default::default(), }; // Encode the rustc version string in a predictable location. @@ -2264,10 +2293,10 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>, path: &Path) { // Encode the root position. let header = METADATA_HEADER.len(); file.seek(std::io::SeekFrom::Start(header as u64)) - .unwrap_or_else(|err| tcx.sess.fatal(&format!("failed to seek the file: {}", err))); + .unwrap_or_else(|err| tcx.sess.emit_fatal(FailSeekFile { err })); let pos = root.position.get(); file.write_all(&[(pos >> 24) as u8, (pos >> 16) as u8, (pos >> 8) as u8, (pos >> 0) as u8]) - .unwrap_or_else(|err| tcx.sess.fatal(&format!("failed to write to the file: {}", err))); + .unwrap_or_else(|err| tcx.sess.emit_fatal(FailWriteFile { err })); // Return to the position where we are before writing the root position. file.seek(std::io::SeekFrom::Start(pos_before_seek)).unwrap(); @@ -2287,8 +2316,8 @@ pub fn provide(providers: &mut Providers) { let mut traits = Vec::new(); for id in tcx.hir().items() { - if matches!(tcx.def_kind(id.def_id), DefKind::Trait | DefKind::TraitAlias) { - traits.push(id.def_id.to_def_id()) + if matches!(tcx.def_kind(id.owner_id), DefKind::Trait | DefKind::TraitAlias) { + traits.push(id.owner_id.to_def_id()) } } |