From 018c4950b9406055dec02ef0fb52f132e2bb1e2c Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 19 Jun 2024 11:25:56 +0200 Subject: Merging upstream version 1.76.0+dfsg1. Signed-off-by: Daniel Baumann --- compiler/rustc_metadata/src/rmeta/decoder.rs | 84 ++++----- .../src/rmeta/decoder/cstore_impl.rs | 6 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 191 ++++++++++----------- compiler/rustc_metadata/src/rmeta/mod.rs | 9 +- compiler/rustc_metadata/src/rmeta/table.rs | 14 +- 5 files changed, 136 insertions(+), 168 deletions(-) (limited to 'compiler/rustc_metadata/src/rmeta') diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 354023cea..281a0eafe 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -1,43 +1,34 @@ // Decoding metadata from a single crate's metadata -use crate::creader::{CStore, CrateMetadataRef}; +use crate::creader::CStore; use crate::rmeta::table::IsDefault; use crate::rmeta::*; use rustc_ast as ast; use rustc_data_structures::captures::Captures; -use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::owned_slice::OwnedSlice; -use rustc_data_structures::svh::Svh; use rustc_data_structures::sync::{AppendOnlyVec, AtomicBool, Lock, Lrc, OnceLock}; use rustc_data_structures::unhash::UnhashMap; use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind}; use rustc_expand::proc_macro::{AttrProcMacro, BangProcMacro, DeriveProcMacro}; -use rustc_hir::def::{CtorKind, DefKind, DocLinkResMap, Res}; -use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE}; -use rustc_hir::definitions::{DefKey, DefPath, DefPathData, DefPathHash}; +use rustc_hir::def::Res; +use rustc_hir::def_id::{DefIdMap, CRATE_DEF_INDEX, LOCAL_CRATE}; +use rustc_hir::definitions::{DefPath, DefPathData}; use rustc_hir::diagnostic_items::DiagnosticItems; -use rustc_index::{Idx, IndexVec}; -use rustc_middle::metadata::ModChild; -use rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile; -use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo}; +use rustc_index::Idx; +use rustc_middle::middle::lib_features::LibFeatures; use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState}; use rustc_middle::ty::codec::TyDecoder; -use rustc_middle::ty::fast_reject::SimplifiedType; -use rustc_middle::ty::{self, ParameterizedOverTcx, Ty, TyCtxt, Visibility}; +use rustc_middle::ty::Visibility; use rustc_serialize::opaque::MemDecoder; use rustc_serialize::{Decodable, Decoder}; -use rustc_session::cstore::{ - CrateSource, ExternCrate, ForeignModule, LinkagePreference, NativeLib, -}; +use rustc_session::cstore::{CrateSource, ExternCrate}; use rustc_session::Session; -use rustc_span::hygiene::ExpnIndex; -use rustc_span::symbol::{kw, Ident, Symbol}; -use rustc_span::{self, BytePos, ExpnId, Pos, Span, SpanData, SyntaxContext, DUMMY_SP}; +use rustc_span::symbol::kw; +use rustc_span::{BytePos, Pos, SpanData, SyntaxContext, DUMMY_SP}; use proc_macro::bridge::client::ProcMacro; use std::iter::TrustedLen; -use std::num::NonZeroUsize; use std::path::Path; use std::sync::atomic::Ordering; use std::{io, iter, mem}; @@ -699,28 +690,25 @@ impl MetadataBlob { } pub(crate) fn get_rustc_version(&self) -> String { - LazyValue::::from_position(NonZeroUsize::new(METADATA_HEADER.len() + 4).unwrap()) + LazyValue::::from_position(NonZeroUsize::new(METADATA_HEADER.len() + 8).unwrap()) .decode(self) } - pub(crate) fn get_header(&self) -> CrateHeader { - let slice = &self.blob()[..]; + fn root_pos(&self) -> NonZeroUsize { let offset = METADATA_HEADER.len(); + let pos_bytes = self.blob()[offset..][..8].try_into().unwrap(); + let pos = u64::from_le_bytes(pos_bytes); + NonZeroUsize::new(pos as usize).unwrap() + } - let pos_bytes = slice[offset..][..4].try_into().unwrap(); - let pos = u32::from_be_bytes(pos_bytes) as usize; - - LazyValue::::from_position(NonZeroUsize::new(pos).unwrap()).decode(self) + pub(crate) fn get_header(&self) -> CrateHeader { + let pos = self.root_pos(); + LazyValue::::from_position(pos).decode(self) } pub(crate) fn get_root(&self) -> CrateRoot { - let slice = &self.blob()[..]; - let offset = METADATA_HEADER.len(); - - let pos_bytes = slice[offset..][..4].try_into().unwrap(); - let pos = u32::from_be_bytes(pos_bytes) as usize; - - LazyValue::::from_position(NonZeroUsize::new(pos).unwrap()).decode(self) + let pos = self.root_pos(); + LazyValue::::from_position(pos).decode(self) } pub(crate) fn list_crate_metadata( @@ -828,7 +816,7 @@ impl MetadataBlob { out, "{}{}", feature, - if let Some(since) = since { + if let FeatureStability::AcceptedSince(since) = since { format!(" since {since}") } else { String::new() @@ -849,7 +837,7 @@ impl MetadataBlob { ) -> io::Result<()> { let root = blob.get_root(); - let def_kind = root.tables.opt_def_kind.get(blob, item).unwrap(); + let def_kind = root.tables.def_kind.get(blob, item).unwrap(); let def_key = root.tables.def_keys.get(blob, item).unwrap().decode(blob); let def_name = if item == CRATE_DEF_INDEX { rustc_span::symbol::kw::Crate @@ -1000,14 +988,11 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { } fn def_kind(self, item_id: DefIndex) -> DefKind { - self.root.tables.opt_def_kind.get(self, item_id).unwrap_or_else(|| { - bug!( - "CrateMetadata::def_kind({:?}): id not found, in crate {:?} with number {}", - item_id, - self.root.name(), - self.cnum, - ) - }) + self.root + .tables + .def_kind + .get(self, item_id) + .unwrap_or_else(|| self.missing("def_kind", item_id)) } fn get_span(self, index: DefIndex, sess: &Session) -> Span { @@ -1176,8 +1161,15 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { } /// Iterates over all the stability attributes in the given crate. - fn get_lib_features(self, tcx: TyCtxt<'tcx>) -> &'tcx [(Symbol, Option)] { - tcx.arena.alloc_from_iter(self.root.lib_features.decode(self)) + fn get_lib_features(self) -> LibFeatures { + LibFeatures { + stability: self + .root + .lib_features + .decode(self) + .map(|(sym, stab)| (sym, (stab, DUMMY_SP))) + .collect(), + } } /// Iterates over the stability implications in the given crate (when a `#[unstable]` attribute @@ -1208,7 +1200,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { /// Iterates over the diagnostic items in the given crate. fn get_diagnostic_items(self) -> DiagnosticItems { - let mut id_to_name = FxHashMap::default(); + let mut id_to_name = DefIdMap::default(); let name_to_id = self .root .diagnostic_items diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 595d816e9..f6cd013d2 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -231,7 +231,7 @@ provide! { tcx, def_id, other, cdata, lookup_deprecation_entry => { table } params_in_repr => { table } unused_generic_params => { cdata.root.tables.unused_generic_params.get(cdata, def_id.index) } - opt_def_kind => { table_direct } + def_kind => { cdata.def_kind(def_id.index) } impl_parent => { table } impl_polarity => { table_direct } defaultness => { table_direct } @@ -346,7 +346,7 @@ provide! { tcx, def_id, other, cdata, module_children => { tcx.arena.alloc_from_iter(cdata.get_module_children(def_id.index, tcx.sess)) } - defined_lib_features => { cdata.get_lib_features(tcx) } + lib_features => { cdata.get_lib_features() } stability_implications => { cdata.get_stability_implications(tcx).iter().copied().collect() } @@ -394,7 +394,7 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) { native_library: |tcx, id| { tcx.native_libraries(id.krate) .iter() - .filter(|lib| native_libs::relevant_lib(&tcx.sess, lib)) + .filter(|lib| native_libs::relevant_lib(tcx.sess, lib)) .find(|lib| { let Some(fm_id) = lib.foreign_module else { return false; diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 2042863d1..ad3fea65e 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1,49 +1,37 @@ -use crate::errors::{FailCreateFileEncoder, FailSeekFile, FailWriteFile}; -use crate::rmeta::def_path_hash_map::DefPathHashMapRef; -use crate::rmeta::table::TableBuilder; +use crate::errors::{FailCreateFileEncoder, FailWriteFile}; use crate::rmeta::*; -use rustc_ast::expand::StrippedCfgItem; use rustc_ast::Attribute; use rustc_data_structures::fingerprint::Fingerprint; -use rustc_data_structures::fx::{FxHashMap, FxIndexSet}; +use rustc_data_structures::fx::FxIndexSet; use rustc_data_structures::memmap::{Mmap, MmapMut}; use rustc_data_structures::stable_hasher::{Hash128, HashStable, StableHasher}; use rustc_data_structures::sync::{join, par_for_each_in, Lrc}; use rustc_data_structures::temp_dir::MaybeTempDir; use rustc_hir as hir; -use rustc_hir::def::DefKind; -use rustc_hir::def_id::{ - CrateNum, DefId, DefIndex, LocalDefId, LocalDefIdSet, CRATE_DEF_ID, CRATE_DEF_INDEX, - LOCAL_CRATE, -}; +use rustc_hir::def_id::{LocalDefId, LocalDefIdSet, CRATE_DEF_ID, CRATE_DEF_INDEX, LOCAL_CRATE}; use rustc_hir::definitions::DefPathData; -use rustc_hir::lang_items::LangItem; use rustc_hir_pretty::id_to_string; -use rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile; use rustc_middle::middle::dependency_format::Linkage; -use rustc_middle::middle::exported_symbols::{ - metadata_symbol_name, ExportedSymbol, SymbolExportInfo, -}; +use rustc_middle::middle::exported_symbols::metadata_symbol_name; use rustc_middle::mir::interpret; use rustc_middle::query::LocalCrate; use rustc_middle::query::Providers; use rustc_middle::traits::specialization_graph; use rustc_middle::ty::codec::TyEncoder; -use rustc_middle::ty::fast_reject::{self, SimplifiedType, TreatParams}; -use rustc_middle::ty::{self, AssocItemContainer, SymbolName, Ty, TyCtxt}; +use rustc_middle::ty::fast_reject::{self, TreatParams}; +use rustc_middle::ty::{AssocItemContainer, SymbolName}; use rustc_middle::util::common::to_readable_str; use rustc_serialize::{opaque, Decodable, Decoder, Encodable, Encoder}; 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}; -use rustc_span::{self, ExternalSource, FileName, SourceFile, Span, SpanData, SyntaxContext}; +use rustc_span::hygiene::HygieneEncodeContext; +use rustc_span::symbol::sym; +use rustc_span::{ExternalSource, FileName, SourceFile, SpanData, SyntaxContext}; use std::borrow::Borrow; use std::collections::hash_map::Entry; +use std::fs::File; use std::hash::Hash; use std::io::{Read, Seek, Write}; -use std::num::NonZeroUsize; use std::path::{Path, PathBuf}; pub(super) struct EncodeContext<'a, 'tcx> { @@ -159,7 +147,7 @@ impl<'a, 'tcx> Encodable> for ExpnIndex { impl<'a, 'tcx> Encodable> for SyntaxContext { fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) { - rustc_span::hygiene::raw_encode_syntax_context(*self, &s.hygiene_ctxt, s); + rustc_span::hygiene::raw_encode_syntax_context(*self, s.hygiene_ctxt, s); } } @@ -656,10 +644,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { 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. + // Encode exported symbols info. This is prefetched in `encode_metadata`. let exported_symbols = stat!("exported-symbols", || { - self.encode_exported_symbols(&tcx.exported_symbols(LOCAL_CRATE)) + self.encode_exported_symbols(tcx.exported_symbols(LOCAL_CRATE)) }); // Encode the hygiene data. @@ -693,15 +680,15 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { has_global_allocator: tcx.has_global_allocator(LOCAL_CRATE), has_alloc_error_handler: tcx.has_alloc_error_handler(LOCAL_CRATE), has_panic_handler: tcx.has_panic_handler(LOCAL_CRATE), - has_default_lib_allocator: attr::contains_name(&attrs, sym::default_lib_allocator), + has_default_lib_allocator: attr::contains_name(attrs, sym::default_lib_allocator), proc_macro_data, debugger_visualizers, - compiler_builtins: attr::contains_name(&attrs, sym::compiler_builtins), - needs_allocator: attr::contains_name(&attrs, sym::needs_allocator), - needs_panic_runtime: attr::contains_name(&attrs, sym::needs_panic_runtime), - no_builtins: attr::contains_name(&attrs, sym::no_builtins), - panic_runtime: attr::contains_name(&attrs, sym::panic_runtime), - profiler_runtime: attr::contains_name(&attrs, sym::profiler_runtime), + compiler_builtins: attr::contains_name(attrs, sym::compiler_builtins), + needs_allocator: attr::contains_name(attrs, sym::needs_allocator), + needs_panic_runtime: attr::contains_name(attrs, sym::needs_panic_runtime), + no_builtins: attr::contains_name(attrs, sym::no_builtins), + panic_runtime: attr::contains_name(attrs, sym::panic_runtime), + profiler_runtime: attr::contains_name(attrs, sym::profiler_runtime), symbol_mangling_version: tcx.sess.opts.get_symbol_mangling_version(), crate_deps, @@ -855,8 +842,7 @@ fn should_encode_span(def_kind: DefKind) -> bool { | DefKind::OpaqueTy | DefKind::Field | DefKind::Impl { .. } - | DefKind::Closure - | DefKind::Coroutine => true, + | DefKind::Closure => true, DefKind::ForeignMod | DefKind::GlobalAsm => false, } } @@ -896,8 +882,7 @@ fn should_encode_attrs(def_kind: DefKind) -> bool { | DefKind::InlineConst | DefKind::OpaqueTy | DefKind::LifetimeParam - | DefKind::GlobalAsm - | DefKind::Coroutine => false, + | DefKind::GlobalAsm => false, } } @@ -932,8 +917,7 @@ fn should_encode_expn_that_defined(def_kind: DefKind) -> bool { | DefKind::Field | DefKind::LifetimeParam | DefKind::GlobalAsm - | DefKind::Closure - | DefKind::Coroutine => false, + | DefKind::Closure => false, } } @@ -968,7 +952,6 @@ fn should_encode_visibility(def_kind: DefKind) -> bool { | DefKind::GlobalAsm | DefKind::Impl { .. } | DefKind::Closure - | DefKind::Coroutine | DefKind::ExternCrate => false, } } @@ -1004,7 +987,6 @@ fn should_encode_stability(def_kind: DefKind) -> bool { | DefKind::InlineConst | DefKind::GlobalAsm | DefKind::Closure - | DefKind::Coroutine | DefKind::ExternCrate => false, } } @@ -1047,6 +1029,8 @@ fn should_encode_mir( | DefKind::AssocConst | DefKind::Static(..) | DefKind::Const => (true, false), + // Coroutines require optimized MIR to compute layout. + DefKind::Closure if tcx.is_coroutine(def_id.to_def_id()) => (false, true), // Full-fledged functions + closures DefKind::AssocFn | DefKind::Fn | DefKind::Closure => { let generics = tcx.generics_of(def_id); @@ -1060,8 +1044,6 @@ fn should_encode_mir( || tcx.is_const_default_method(def_id.to_def_id()); (is_const_fn, opt) } - // Coroutines require optimized MIR to compute layout. - DefKind::Coroutine => (false, true), // The others don't have MIR. _ => (false, false), } @@ -1097,7 +1079,6 @@ fn should_encode_variances<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, def_kind: Def | DefKind::InlineConst | DefKind::GlobalAsm | DefKind::Closure - | DefKind::Coroutine | DefKind::ExternCrate => false, DefKind::TyAlias => tcx.type_alias_is_lazy(def_id), } @@ -1126,8 +1107,7 @@ fn should_encode_generics(def_kind: DefKind) -> bool { | DefKind::Impl { .. } | DefKind::Field | DefKind::TyParam - | DefKind::Closure - | DefKind::Coroutine => true, + | DefKind::Closure => true, DefKind::Mod | DefKind::ForeignMod | DefKind::ConstParam @@ -1156,7 +1136,6 @@ fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) -> | DefKind::AssocFn | DefKind::AssocConst | DefKind::Closure - | DefKind::Coroutine | DefKind::ConstParam | DefKind::AnonConst | DefKind::InlineConst => true, @@ -1165,7 +1144,7 @@ fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) -> let origin = tcx.opaque_type_origin(def_id); if let hir::OpaqueTyOrigin::FnReturn(fn_def_id) | hir::OpaqueTyOrigin::AsyncFn(fn_def_id) = origin - && let hir::Node::TraitItem(trait_item) = tcx.hir().get_by_def_id(fn_def_id) + && let hir::Node::TraitItem(trait_item) = tcx.hir_node_by_def_id(fn_def_id) && let (_, hir::TraitFn::Required(..)) = trait_item.expect_fn() { false @@ -1182,7 +1161,7 @@ fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) -> } } DefKind::TyParam => { - let hir::Node::GenericParam(param) = tcx.hir().get_by_def_id(def_id) else { bug!() }; + let hir::Node::GenericParam(param) = tcx.hir_node_by_def_id(def_id) else { bug!() }; let hir::GenericParamKind::Type { default, .. } = param.kind else { bug!() }; default.is_some() } @@ -1217,7 +1196,6 @@ fn should_encode_fn_sig(def_kind: DefKind) -> bool { | DefKind::Impl { .. } | DefKind::AssocConst | DefKind::Closure - | DefKind::Coroutine | DefKind::ConstParam | DefKind::AnonConst | DefKind::InlineConst @@ -1256,7 +1234,6 @@ fn should_encode_constness(def_kind: DefKind) -> bool { | DefKind::OpaqueTy | DefKind::Impl { of_trait: false } | DefKind::ForeignTy - | DefKind::Coroutine | DefKind::ConstParam | DefKind::InlineConst | DefKind::AssocTy @@ -1291,7 +1268,6 @@ fn should_encode_const(def_kind: DefKind) -> bool { | DefKind::Impl { .. } | DefKind::AssocFn | DefKind::Closure - | DefKind::Coroutine | DefKind::ConstParam | DefKind::AssocTy | DefKind::TyParam @@ -1353,9 +1329,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { for local_id in tcx.iter_local_def_id() { let def_id = local_id.to_def_id(); - let def_kind = tcx.opt_def_kind(local_id); - let Some(def_kind) = def_kind else { continue }; - self.tables.opt_def_kind.set_some(def_id.index, def_kind); + let def_kind = tcx.def_kind(local_id); + self.tables.def_kind.set_some(def_id.index, def_kind); if should_encode_span(def_kind) { let def_span = tcx.def_span(local_id); record!(self.tables.def_span[def_id] <- def_span); @@ -1392,7 +1367,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { if should_encode_fn_sig(def_kind) { record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id)); } - if should_encode_generics(def_kind) { + // FIXME: Some anonymous constants produced by `#[rustc_legacy_const_generics]` + // do not have corresponding HIR nodes, so some queries usually making sense for + // anonymous constants will not work on them and panic. It's not clear whether it + // can cause any observable issues or not. + let anon_const_without_hir = def_kind == DefKind::AnonConst + && tcx.opt_hir_node(tcx.local_def_id_to_hir_id(local_id)).is_none(); + if should_encode_generics(def_kind) && !anon_const_without_hir { let g = tcx.generics_of(def_id); record!(self.tables.generics_of[def_id] <- g); record!(self.tables.explicit_predicates_of[def_id] <- self.tcx.explicit_predicates_of(def_id)); @@ -1406,7 +1387,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } } } - if should_encode_type(tcx, local_id, def_kind) { + if should_encode_type(tcx, local_id, def_kind) && !anon_const_without_hir { record!(self.tables.type_of[def_id] <- self.tcx.type_of(def_id)); } if should_encode_constness(def_kind) { @@ -1446,8 +1427,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { self.encode_info_for_assoc_item(def_id); } } - if let DefKind::Coroutine = def_kind { - let data = self.tcx.coroutine_kind(def_id).unwrap(); + if def_kind == DefKind::Closure + && let Some(data) = self.tcx.coroutine_kind(def_id) + { record!(self.tables.coroutine_kind[def_id] <- data); } if let DefKind::Enum | DefKind::Struct | DefKind::Union = def_kind { @@ -1629,7 +1611,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { record!(self.tables.closure_saved_names_of_captured_variables[def_id.to_def_id()] <- tcx.closure_saved_names_of_captured_variables(def_id)); - if let DefKind::Coroutine = self.tcx.def_kind(def_id) + if self.tcx.is_coroutine(def_id.to_def_id()) && let Some(witnesses) = tcx.mir_coroutine_witnesses(def_id) { record!(self.tables.mir_coroutine_witnesses[def_id.to_def_id()] <- witnesses); @@ -1656,7 +1638,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } record!(self.tables.promoted_mir[def_id.to_def_id()] <- tcx.promoted_mir(def_id)); - if let DefKind::Coroutine = self.tcx.def_kind(def_id) + if self.tcx.is_coroutine(def_id.to_def_id()) && let Some(witnesses) = tcx.mir_coroutine_witnesses(def_id) { record!(self.tables.mir_coroutine_witnesses[def_id.to_def_id()] <- witnesses); @@ -1727,9 +1709,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { fn encode_info_for_macro(&mut self, def_id: LocalDefId) { let tcx = self.tcx; - let hir::ItemKind::Macro(ref macro_def, _) = tcx.hir().expect_item(def_id).kind else { - bug!() - }; + let hir::ItemKind::Macro(macro_def, _) = tcx.hir().expect_item(def_id).kind else { bug!() }; self.tables.is_macro_rules.set(def_id.local_def_index, macro_def.macro_rules); record!(self.tables.macro_definition[def_id.to_def_id()] <- &*macro_def.body); } @@ -1786,7 +1766,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { self.tables.proc_macro_quoted_spans.set_some(i, span); } - self.tables.opt_def_kind.set_some(LOCAL_CRATE.as_def_id().index, DefKind::Mod); + self.tables.def_kind.set_some(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()); let vis = tcx.local_visibility(CRATE_DEF_ID).map_id(|def_id| def_id.local_def_index); @@ -1807,7 +1787,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { // so we manually encode just the information that we need for &proc_macro in &tcx.resolutions(()).proc_macros { let id = proc_macro; - let proc_macro = hir.local_def_id_to_hir_id(proc_macro); + let proc_macro = tcx.local_def_id_to_hir_id(proc_macro); let mut name = hir.name(proc_macro); let span = hir.span(proc_macro); // Proc-macros may have attributes like `#[allow_internal_unstable]`, @@ -1834,7 +1814,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { def_key.disambiguated_data.data = DefPathData::MacroNs(name); let def_id = id.to_def_id(); - self.tables.opt_def_kind.set_some(def_id.index, DefKind::Macro(macro_kind)); + self.tables.def_kind.set_some(def_id.index, DefKind::Macro(macro_kind)); self.tables.proc_macro.set_some(def_id.index, macro_kind); self.encode_attrs(id); record!(self.tables.def_keys[def_id] <- def_key); @@ -1902,10 +1882,10 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { self.lazy_array(deps.iter().map(|(_, dep)| dep)) } - fn encode_lib_features(&mut self) -> LazyArray<(Symbol, Option)> { + fn encode_lib_features(&mut self) -> LazyArray<(Symbol, FeatureStability)> { empty_proc_macro!(self); let tcx = self.tcx; - let lib_features = tcx.lib_features(()); + let lib_features = tcx.lib_features(LOCAL_CRATE); self.lazy_array(lib_features.to_vec()) } @@ -2198,27 +2178,19 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path) { // there's no need to do dep-graph tracking for any of it. tcx.dep_graph.assert_ignored(); - join( - || encode_metadata_impl(tcx, path), - || { - if tcx.sess.threads() == 1 { - return; - } - // Prefetch some queries used by metadata encoding. - // This is not necessary for correctness, but is only done for performance reasons. - // It can be removed if it turns out to cause trouble or be detrimental to performance. - join(|| prefetch_mir(tcx), || tcx.exported_symbols(LOCAL_CRATE)); - }, - ); -} + if tcx.sess.threads() != 1 { + // Prefetch some queries used by metadata encoding. + // This is not necessary for correctness, but is only done for performance reasons. + // It can be removed if it turns out to cause trouble or be detrimental to performance. + join(|| prefetch_mir(tcx), || tcx.exported_symbols(LOCAL_CRATE)); + } -fn encode_metadata_impl(tcx: TyCtxt<'_>, path: &Path) { let mut encoder = opaque::FileEncoder::new(path) .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. - encoder.emit_raw_bytes(&[0, 0, 0, 0]); + encoder.emit_raw_bytes(&0u64.to_le_bytes()); let source_map_files = tcx.sess.source_map().files(); let source_file_cache = (source_map_files[0].clone(), 0); @@ -2251,29 +2223,34 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>, path: &Path) { // culminating in the `CrateRoot` which points to all of it. let root = ecx.encode_crate_root(); - ecx.opaque.flush(); + // Make sure we report any errors from writing to the file. + // If we forget this, compilation can succeed with an incomplete rmeta file, + // causing an ICE when the rmeta file is read by another compilation. + if let Err((path, err)) = ecx.opaque.finish() { + tcx.sess.emit_err(FailWriteFile { path: &path, err }); + } + + let file = ecx.opaque.file(); + if let Err(err) = encode_root_position(file, root.position.get()) { + tcx.sess.emit_err(FailWriteFile { path: ecx.opaque.path(), err }); + } + + // Record metadata size for self-profiling + tcx.prof.artifact_size("crate_metadata", "crate_metadata", file.metadata().unwrap().len()); +} - let mut file = ecx.opaque.file(); +fn encode_root_position(mut file: &File, pos: usize) -> Result<(), std::io::Error> { // We will return to this position after writing the root position. let pos_before_seek = file.stream_position().unwrap(); // Encode the root position. let header = METADATA_HEADER.len(); - file.seek(std::io::SeekFrom::Start(header as u64)) - .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.emit_fatal(FailWriteFile { err })); + file.seek(std::io::SeekFrom::Start(header as u64))?; + file.write_all(&pos.to_le_bytes())?; // Return to the position where we are before writing the root position. - file.seek(std::io::SeekFrom::Start(pos_before_seek)).unwrap(); - - // Record metadata size for self-profiling - tcx.prof.artifact_size( - "crate_metadata", - "crate_metadata", - file.metadata().unwrap().len() as u64, - ); + file.seek(std::io::SeekFrom::Start(pos_before_seek))?; + Ok(()) } pub fn provide(providers: &mut Providers) { @@ -2396,8 +2373,10 @@ pub fn rendered_const<'tcx>(tcx: TyCtxt<'tcx>, body: hir::BodyId) -> String { // * character escapes // // FIXME: This passes through `-/*spacer*/0` verbatim. - Literal if !value.span.from_expansion() - && let Ok(snippet) = tcx.sess.source_map().span_to_snippet(value.span) => { + Literal + if !value.span.from_expansion() + && let Ok(snippet) = tcx.sess.source_map().span_to_snippet(value.span) => + { snippet } @@ -2408,10 +2387,12 @@ pub fn rendered_const<'tcx>(tcx: TyCtxt<'tcx>, body: hir::BodyId) -> String { // FIXME: Omit the curly braces if the enclosing expression is an array literal // with a repeated element (an `ExprKind::Repeat`) as in such case it // would not actually need any disambiguation. - Complex => if tcx.def_kind(hir.body_owner_def_id(body).to_def_id()) == DefKind::AnonConst { - "{ _ }".to_owned() - } else { - "_".to_owned() + Complex => { + if tcx.def_kind(hir.body_owner_def_id(body).to_def_id()) == DefKind::AnonConst { + "{ _ }".to_owned() + } else { + "_".to_owned() + } } } } diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index 9ae5c0af0..a85822848 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -3,6 +3,7 @@ use decoder::Metadata; use def_path_hash_map::DefPathHashMapRef; use rustc_data_structures::fx::FxHashMap; use rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile; +use rustc_middle::middle::lib_features::FeatureStability; use table::TableBuilder; use rustc_ast as ast; @@ -56,12 +57,12 @@ pub(crate) fn rustc_version(cfg_version: &'static str) -> String { /// Metadata encoding version. /// N.B., increment this if you change the format of metadata such that /// the rustc version can't be found to compare with `rustc_version()`. -const METADATA_VERSION: u8 = 8; +const METADATA_VERSION: u8 = 9; /// Metadata header which includes `METADATA_VERSION`. /// /// This header is followed by the length of the compressed data, then -/// the position of the `CrateRoot`, which is encoded as a 32-bit big-endian +/// the position of the `CrateRoot`, which is encoded as a 64-bit little-endian /// unsigned integer, and further followed by the rustc version string. pub const METADATA_HEADER: &[u8] = &[b'r', b'u', b's', b't', 0, 0, 0, METADATA_VERSION]; @@ -263,7 +264,7 @@ pub(crate) struct CrateRoot { crate_deps: LazyArray, dylib_dependency_formats: LazyArray>, - lib_features: LazyArray<(Symbol, Option)>, + lib_features: LazyArray<(Symbol, FeatureStability)>, stability_implications: LazyArray<(Symbol, Symbol)>, lang_items: LazyArray<(DefIndex, LangItem)>, lang_items_missing: LazyArray, @@ -404,7 +405,7 @@ define_tables! { // so we can take their names, visibilities etc from other encoded tables. module_children_non_reexports: Table>, associated_item_or_field_def_ids: Table>, - opt_def_kind: Table, + def_kind: Table, visibility: Table>>, def_span: Table>, def_ident_span: Table>, diff --git a/compiler/rustc_metadata/src/rmeta/table.rs b/compiler/rustc_metadata/src/rmeta/table.rs index 027994c40..3fc6d9db3 100644 --- a/compiler/rustc_metadata/src/rmeta/table.rs +++ b/compiler/rustc_metadata/src/rmeta/table.rs @@ -1,13 +1,8 @@ use crate::rmeta::*; use rustc_data_structures::fingerprint::Fingerprint; -use rustc_hir::def::{CtorKind, CtorOf}; +use rustc_hir::def::CtorOf; use rustc_index::Idx; -use rustc_middle::ty::{ParameterizedOverTcx, UnusedGenericParams}; -use rustc_serialize::opaque::FileEncoder; -use rustc_span::hygiene::MacroKind; -use std::marker::PhantomData; -use std::num::NonZeroUsize; pub(super) trait IsDefault: Default { fn is_default(&self) -> bool; @@ -167,7 +162,6 @@ fixed_size_enum! { ( Impl { of_trait: false } ) ( Impl { of_trait: true } ) ( Closure ) - ( Coroutine ) ( Static(ast::Mutability::Not) ) ( Static(ast::Mutability::Mut) ) ( Ctor(CtorOf::Struct, CtorKind::Fn) ) @@ -382,8 +376,8 @@ impl LazyArray { } fn from_bytes_impl(position: &[u8; 8], meta: &[u8; 8]) -> Option> { - let position = NonZeroUsize::new(u64::from_bytes(&position) as usize)?; - let len = u64::from_bytes(&meta) as usize; + let position = NonZeroUsize::new(u64::from_bytes(position) as usize)?; + let len = u64::from_bytes(meta) as usize; Some(LazyArray::from_position_and_num_elems(position, len)) } } @@ -497,7 +491,7 @@ impl> TableBui } LazyTable::from_position_and_encoded_size( - NonZeroUsize::new(pos as usize).unwrap(), + NonZeroUsize::new(pos).unwrap(), width, self.blocks.len(), ) -- cgit v1.2.3