From 218caa410aa38c29984be31a5229b9fa717560ee Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:19:13 +0200 Subject: Merging upstream version 1.68.2+dfsg1. Signed-off-by: Daniel Baumann --- compiler/rustc_metadata/src/rmeta/decoder.rs | 46 ++++++------ .../src/rmeta/decoder/cstore_impl.rs | 14 ++-- .../rustc_metadata/src/rmeta/def_path_hash_map.rs | 2 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 84 +++++++++------------- compiler/rustc_metadata/src/rmeta/mod.rs | 15 ++-- compiler/rustc_metadata/src/rmeta/table.rs | 1 - 6 files changed, 70 insertions(+), 92 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 af7b0793a..143d8f2f1 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -78,10 +78,6 @@ pub(crate) struct CrateMetadata { blob: MetadataBlob, // --- Some data pre-decoded from the metadata blob, usually for performance --- - /// NOTE(eddyb) we pass `'static` to a `'tcx` parameter because this - /// lifetime is only used behind `LazyValue`, `LazyArray`, or `LazyTable`, and therefore acts like a - /// universal (`for<'tcx>`), that is paired up with whichever `TyCtxt` - /// is being used to decode those values. root: CrateRoot, /// Trait impl data. /// FIXME: Used only from queries and can use query cache, @@ -466,7 +462,7 @@ impl<'a, 'tcx> Decodable> for SyntaxContext { .root .syntax_contexts .get(cdata, id) - .unwrap_or_else(|| panic!("Missing SyntaxContext {:?} for crate {:?}", id, cname)) + .unwrap_or_else(|| panic!("Missing SyntaxContext {id:?} for crate {cname:?}")) .decode((cdata, sess)) }) } @@ -688,10 +684,10 @@ impl MetadataBlob { pub(crate) fn get_root(&self) -> CrateRoot { let slice = &self.blob()[..]; let offset = METADATA_HEADER.len(); - let pos = (((slice[offset + 0] as u32) << 24) - | ((slice[offset + 1] as u32) << 16) - | ((slice[offset + 2] as u32) << 8) - | ((slice[offset + 3] as u32) << 0)) as usize; + + 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) } @@ -702,16 +698,14 @@ impl MetadataBlob { writeln!(out, "hash {} stable_crate_id {:?}", root.hash, root.stable_crate_id)?; writeln!(out, "proc_macro {:?}", root.proc_macro_data.is_some())?; writeln!(out, "=External Dependencies=")?; + for (i, dep) in root.crate_deps.decode(self).enumerate() { + let CrateDep { name, extra_filename, hash, host_hash, kind } = dep; + let number = i + 1; + writeln!( out, - "{} {}{} hash {} host_hash {:?} kind {:?}", - i + 1, - dep.name, - dep.extra_filename, - dep.hash, - dep.host_hash, - dep.kind + "{number} {name}{extra_filename} hash {hash} host_hash {host_hash:?} kind {kind:?}" )?; } write!(out, "\n")?; @@ -812,7 +806,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { .tables .def_span .get(self, index) - .unwrap_or_else(|| panic!("Missing span for {:?}", index)) + .unwrap_or_else(|| panic!("Missing span for {index:?}")) .decode((self, sess)) } @@ -1255,7 +1249,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { .tables .proc_macro_quoted_spans .get(self, index) - .unwrap_or_else(|| panic!("Missing proc macro quoted span: {:?}", index)) + .unwrap_or_else(|| panic!("Missing proc macro quoted span: {index:?}")) .decode((self, sess)) } @@ -1527,13 +1521,15 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { if let Some(virtual_dir) = &sess.opts.unstable_opts.simulate_remapped_rust_src_base { if let Some(real_dir) = &sess.opts.real_rust_source_base_dir { - if let rustc_span::FileName::Real(ref mut old_name) = name { - if let rustc_span::RealFileName::LocalPath(local) = old_name { - if let Ok(rest) = local.strip_prefix(real_dir) { - *old_name = rustc_span::RealFileName::Remapped { - local_path: None, - virtual_name: virtual_dir.join(rest), - }; + for subdir in ["library", "compiler"] { + if let rustc_span::FileName::Real(ref mut old_name) = name { + if let rustc_span::RealFileName::LocalPath(local) = old_name { + if let Ok(rest) = local.strip_prefix(real_dir.join(subdir)) { + *old_name = rustc_span::RealFileName::Remapped { + local_path: None, + virtual_name: virtual_dir.join(subdir).join(rest), + }; + } } } } diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index c5d4da079..6fd5bd52a 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -232,14 +232,14 @@ provide! { tcx, def_id, other, cdata, .get(cdata, def_id.index) .is_some() } - collect_trait_impl_trait_tys => { + collect_return_position_impl_trait_in_trait_tys => { Ok(cdata .root .tables .trait_impl_trait_tys .get(cdata, def_id.index) .map(|lazy| lazy.decode((cdata, tcx))) - .process_decoded(tcx, || panic!("{:?} does not have trait_impl_trait_tys", def_id))) + .process_decoded(tcx, || panic!("{def_id:?} does not have trait_impl_trait_tys"))) } visibility => { cdata.get_visibility(def_id.index) } @@ -391,7 +391,7 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) { // keys from the former. // This is a rudimentary check that does not catch all cases, // just the easiest. - let mut fallback_map: DefIdMap = Default::default(); + let mut fallback_map: Vec<(DefId, DefId)> = Default::default(); // Issue 46112: We want the map to prefer the shortest // paths when reporting the path to an item. Therefore we @@ -421,12 +421,12 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) { if let Some(def_id) = child.res.opt_def_id() { if child.ident.name == kw::Underscore { - fallback_map.insert(def_id, parent); + fallback_map.push((def_id, parent)); return; } if ty::util::is_doc_hidden(tcx, parent) { - fallback_map.insert(def_id, parent); + fallback_map.push((def_id, parent)); return; } @@ -460,6 +460,7 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) { // Fill in any missing entries with the less preferable path. // If this path re-exports the child as `_`, we still use this // path in a diagnostic that suggests importing `::*`. + for (child, parent) in fallback_map { visible_parent_map.entry(child).or_insert(parent); } @@ -638,6 +639,9 @@ impl CrateStore for CStore { fn as_any(&self) -> &dyn Any { self } + fn untracked_as_any(&mut self) -> &mut dyn Any { + self + } fn crate_name(&self, cnum: CrateNum) -> Symbol { self.get_crate_data(cnum).root.name diff --git a/compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs b/compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs index 40c94b372..a6133f1b4 100644 --- a/compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs +++ b/compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs @@ -58,7 +58,7 @@ impl<'a, 'tcx> Decodable> for DefPathHashMapRef<'static> let _ = d.read_raw_bytes(len); let inner = odht::HashTable::from_raw_bytes(o).unwrap_or_else(|e| { - panic!("decode error: {}", e); + panic!("decode error: {e}"); }); DefPathHashMapRef::OwnedFromMetadata(inner) } diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 7304c891e..8f7a61b72 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -76,7 +76,7 @@ pub(super) struct EncodeContext<'a, 'tcx> { symbol_table: FxHashMap, } -/// If the current crate is a proc-macro, returns early with `Lazy:empty()`. +/// If the current crate is a proc-macro, returns early with `LazyArray::empty()`. /// This is useful for skipping the encoding of things that aren't needed /// for proc-macro crates. macro_rules! empty_proc_macro { @@ -145,7 +145,7 @@ impl<'a, 'tcx, I, T> Encodable> for LazyTable { impl<'a, 'tcx> Encodable> for CrateNum { fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) { if *self != LOCAL_CRATE && s.is_proc_macro { - panic!("Attempted to encode non-local CrateNum {:?} for proc-macro crate", self); + panic!("Attempted to encode non-local CrateNum {self:?} for proc-macro crate"); } s.emit_u32(self.as_u32()); } @@ -172,7 +172,7 @@ impl<'a, 'tcx> Encodable> for SyntaxContext { impl<'a, 'tcx> Encodable> for ExpnId { fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) { if self.krate == LOCAL_CRATE { - // We will only write details for local expansions. Non-local expansions will fetch + // We will only write details for local expansions. Non-local expansions will fetch // data from the corresponding crate's metadata. // FIXME(#43047) FIXME(#74731) We may eventually want to avoid relying on external // metadata from proc-macro crates. @@ -276,7 +276,7 @@ impl<'a, 'tcx> Encodable> for Span { // Introduce a new scope so that we drop the 'lock()' temporary match &*source_file.external_src.lock() { ExternalSource::Foreign { metadata_index, .. } => *metadata_index, - src => panic!("Unexpected external source {:?}", src), + src => panic!("Unexpected external source {src:?}"), } }; @@ -332,7 +332,7 @@ impl<'a, 'tcx> Encodable> for Symbol { s.emit_str(self.as_str()); } Entry::Occupied(o) => { - let x = o.get().clone(); + let x = *o.get(); s.emit_u8(SYMBOL_OFFSET); s.emit_usize(x); } @@ -713,7 +713,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let computed_total_bytes: usize = stats.iter().map(|(_, size)| size).sum(); assert_eq!(total_bytes, computed_total_bytes); - if tcx.sess.meta_stats() { + if tcx.sess.opts.unstable_opts.meta_stats { self.opaque.flush(); // Rewind and re-read all the metadata to count the zero bytes we wrote. @@ -733,12 +733,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let prefix = "meta-stats"; let perc = |bytes| (bytes * 100) as f64 / total_bytes as f64; - eprintln!("{} METADATA STATS", prefix); + eprintln!("{prefix} METADATA STATS"); eprintln!("{} {:<23}{:>10}", prefix, "Section", "Size"); - eprintln!( - "{} ----------------------------------------------------------------", - prefix - ); + eprintln!("{prefix} ----------------------------------------------------------------"); for (label, size) in stats { eprintln!( "{} {:<23}{:>10} ({:4.1}%)", @@ -748,10 +745,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { perc(size) ); } - eprintln!( - "{} ----------------------------------------------------------------", - prefix - ); + eprintln!("{prefix} ----------------------------------------------------------------"); eprintln!( "{} {:<23}{:>10} (of which {:.1}% are zero bytes)", prefix, @@ -759,7 +753,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { to_readable_str(total_bytes), perc(zero_bytes) ); - eprintln!("{}", prefix); + eprintln!("{prefix}"); } root @@ -894,8 +888,8 @@ fn should_encode_mir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> (bool, bool) { | DefKind::AssocConst | DefKind::Static(..) | DefKind::Const => (true, false), - // Full-fledged functions - DefKind::AssocFn | DefKind::Fn => { + // Full-fledged functions + closures + DefKind::AssocFn | DefKind::Fn | DefKind::Closure => { let generics = tcx.generics_of(def_id); let needs_inline = (generics.requires_monomorphization(tcx) || tcx.codegen_fn_attrs(def_id).requests_inline()) @@ -906,15 +900,6 @@ fn should_encode_mir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> (bool, bool) { let always_encode_mir = tcx.sess.opts.unstable_opts.always_encode_mir; (is_const_fn, needs_inline || always_encode_mir) } - // Closures can't be const fn. - DefKind::Closure => { - let generics = tcx.generics_of(def_id); - let needs_inline = (generics.requires_monomorphization(tcx) - || tcx.codegen_fn_attrs(def_id).requests_inline()) - && tcx.sess.opts.output_types.should_codegen(); - let always_encode_mir = tcx.sess.opts.unstable_opts.always_encode_mir; - (false, needs_inline || always_encode_mir) - } // Generators require optimized MIR to compute layout. DefKind::Generator => (false, true), // The others don't have MIR. @@ -1093,7 +1078,7 @@ fn should_encode_const(def_kind: DefKind) -> bool { } } -fn should_encode_trait_impl_trait_tys<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool { +fn should_encode_trait_impl_trait_tys(tcx: TyCtxt<'_>, def_id: DefId) -> bool { if tcx.def_kind(def_id) != DefKind::AssocFn { return false; } @@ -1111,8 +1096,8 @@ fn should_encode_trait_impl_trait_tys<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> // 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 + && let ty::Alias(ty::Projection, data) = ty.kind() + && tcx.def_kind(data.def_id) == DefKind::ImplTraitPlaceholder { true } else { @@ -1197,13 +1182,16 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { 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) + && let Ok(table) = self.tcx.collect_return_position_impl_trait_in_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() { + let inherent_impls = tcx.with_stable_hashing_context(|hcx| { + tcx.crate_inherent_impls(()).inherent_impls.to_sorted(&hcx, true) + }); + + for (def_id, implementations) in inherent_impls { if implementations.is_empty() { continue; } @@ -1337,24 +1325,16 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { debug!("EncodeContext::encode_info_for_trait_item({:?})", def_id); let tcx = self.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 impl_defaultness = tcx.impl_defaultness(def_id.expect_local()); + self.tables.impl_defaultness.set(def_id.index, impl_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 => {} ty::AssocKind::Fn => { - let hir::TraitItemKind::Fn(m_sig, m) = &ast_item.kind else { bug!() }; - match *m { - hir::TraitFn::Required(ref names) => { - record_array!(self.tables.fn_arg_names[def_id] <- *names) - } - hir::TraitFn::Provided(body) => { - record_array!(self.tables.fn_arg_names[def_id] <- self.tcx.hir().body_param_names(body)) - } - }; - self.tables.asyncness.set(def_id.index, m_sig.header.asyncness); + record_array!(self.tables.fn_arg_names[def_id] <- tcx.fn_arg_names(def_id)); + self.tables.asyncness.set(def_id.index, tcx.asyncness(def_id)); self.tables.constness.set(def_id.index, hir::Constness::NotConst); } ty::AssocKind::Type => { @@ -1443,7 +1423,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let instance = ty::InstanceDef::Item(ty::WithOptConstParam::unknown(def_id.to_def_id())); let unused = tcx.unused_generic_params(instance); - if !unused.is_empty() { + if !unused.all_used() { record!(self.tables.unused_generic_params[def_id.to_def_id()] <- unused); } } @@ -1572,10 +1552,10 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { self.tables.impl_defaultness.set(def_id.index, *defaultness); self.tables.constness.set(def_id.index, *constness); - let trait_ref = self.tcx.impl_trait_ref(def_id); + let trait_ref = self.tcx.impl_trait_ref(def_id).map(ty::EarlyBinder::skip_binder); if let Some(trait_ref) = trait_ref { let trait_def = self.tcx.trait_def(trait_ref.def_id); - if let Some(mut an) = trait_def.ancestors(self.tcx, def_id).ok() { + if let Ok(mut an) = trait_def.ancestors(self.tcx, def_id) { if let Some(specialization_graph::Node::Impl(parent)) = an.nth(1) { self.tables.impl_parent.set(def_id.index, parent.into()); } @@ -1703,6 +1683,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } ty::Closure(_, substs) => { + let constness = self.tcx.constness(def_id.to_def_id()); + self.tables.constness.set(def_id.to_def_id().index, constness); record!(self.tables.fn_sig[def_id.to_def_id()] <- substs.as_closure().sig()); } @@ -1860,7 +1842,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { // the assumption that they are numbered 1 to n. // FIXME (#2166): This is not nearly enough to support correct versioning // but is enough to get transitive crate dependencies working. - self.lazy_array(deps.iter().map(|&(_, ref dep)| dep)) + self.lazy_array(deps.iter().map(|(_, dep)| dep)) } fn encode_lib_features(&mut self) -> LazyArray<(Symbol, Option)> { @@ -1914,6 +1896,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { for id in tcx.hir().items() { if matches!(tcx.def_kind(id.owner_id), DefKind::Impl) { if let Some(trait_ref) = tcx.impl_trait_ref(id.owner_id) { + let trait_ref = trait_ref.subst_identity(); + let simplified_self_ty = fast_reject::simplify_type( self.tcx, trait_ref.self_ty(), @@ -1997,7 +1981,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { self.lazy_array( exported_symbols .iter() - .filter(|&&(ref exported_symbol, _)| match *exported_symbol { + .filter(|&(exported_symbol, _)| match *exported_symbol { ExportedSymbol::NoDefId(symbol_name) => symbol_name != metadata_symbol_name, _ => true, }) diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index 571804644..5066dbbb9 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -13,7 +13,7 @@ use rustc_hir::def::{CtorKind, DefKind}; use rustc_hir::def_id::{CrateNum, DefId, DefIndex, DefPathHash, StableCrateId}; use rustc_hir::definitions::DefKey; use rustc_hir::lang_items::LangItem; -use rustc_index::bit_set::{BitSet, FiniteBitSet}; +use rustc_index::bit_set::BitSet; use rustc_index::vec::IndexVec; use rustc_middle::metadata::ModChild; use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs; @@ -22,7 +22,7 @@ use rustc_middle::middle::resolve_lifetime::ObjectLifetimeDefault; use rustc_middle::mir; use rustc_middle::ty::fast_reject::SimplifiedType; use rustc_middle::ty::query::Providers; -use rustc_middle::ty::{self, ReprOptions, Ty}; +use rustc_middle::ty::{self, ReprOptions, Ty, UnusedGenericParams}; use rustc_middle::ty::{DeducedParamAttrs, GeneratorDiagnosticData, ParameterizedOverTcx, TyCtxt}; use rustc_serialize::opaque::FileEncoder; use rustc_session::config::SymbolManglingVersion; @@ -359,8 +359,8 @@ define_tables! { variances_of: Table>, fn_sig: Table>>, codegen_fn_attrs: Table>, - impl_trait_ref: Table>>, - const_param_default: Table>>, + impl_trait_ref: Table>>>, + const_param_default: Table>>>, object_lifetime_default: Table>, optimized_mir: Table>>, mir_for_ctfe: Table>>, @@ -384,7 +384,7 @@ define_tables! { trait_item_def_id: Table, inherent_impls: Table>, expn_that_defined: Table>, - unused_generic_params: Table>>, + unused_generic_params: Table>, params_in_repr: Table>>, repr_options: Table>, // `def_keys` and `def_path_hashes` represent a lazy version of a @@ -418,11 +418,6 @@ struct VariantData { is_non_exhaustive: bool, } -#[derive(TyEncodable, TyDecodable)] -struct GeneratorData<'tcx> { - layout: mir::GeneratorLayout<'tcx>, -} - // Tags used for encoding Spans: const TAG_VALID_SPAN_LOCAL: u8 = 0; const TAG_VALID_SPAN_FOREIGN: u8 = 1; diff --git a/compiler/rustc_metadata/src/rmeta/table.rs b/compiler/rustc_metadata/src/rmeta/table.rs index 29fe61107..716655c7f 100644 --- a/compiler/rustc_metadata/src/rmeta/table.rs +++ b/compiler/rustc_metadata/src/rmeta/table.rs @@ -7,7 +7,6 @@ use rustc_middle::ty::ParameterizedOverTcx; use rustc_serialize::opaque::FileEncoder; use rustc_serialize::Encoder as _; use rustc_span::hygiene::MacroKind; -use std::convert::TryInto; use std::marker::PhantomData; use std::num::NonZeroUsize; -- cgit v1.2.3