diff options
Diffstat (limited to 'compiler/rustc_metadata')
| -rw-r--r-- | compiler/rustc_metadata/src/creader.rs | 8 | ||||
| -rw-r--r-- | compiler/rustc_metadata/src/dependency_format.rs | 9 | ||||
| -rw-r--r-- | compiler/rustc_metadata/src/errors.rs | 22 | ||||
| -rw-r--r-- | compiler/rustc_metadata/src/locator.rs | 79 | ||||
| -rw-r--r-- | compiler/rustc_metadata/src/native_libs.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_metadata/src/rmeta/decoder.rs | 30 | ||||
| -rw-r--r-- | compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_metadata/src/rmeta/encoder.rs | 28 | ||||
| -rw-r--r-- | compiler/rustc_metadata/src/rmeta/mod.rs | 5 |
10 files changed, 79 insertions, 110 deletions
diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index 9ce3ff98ba9..653f2b39d3e 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -112,7 +112,7 @@ impl<'a> std::fmt::Debug for CrateDump<'a> { writeln!(fmt, "resolved crates:")?; for (cnum, data) in self.0.iter_crate_data() { writeln!(fmt, " name: {}", data.name())?; - writeln!(fmt, " cnum: {}", cnum)?; + writeln!(fmt, " cnum: {cnum}")?; writeln!(fmt, " hash: {}", data.hash())?; writeln!(fmt, " reqd: {:?}", data.dep_kind())?; let CrateSource { dylib, rlib, rmeta } = data.source(); @@ -150,7 +150,7 @@ impl CStore { pub(crate) fn get_crate_data(&self, cnum: CrateNum) -> CrateMetadataRef<'_> { let cdata = self.metas[cnum] .as_ref() - .unwrap_or_else(|| panic!("Failed to get crate data for {:?}", cnum)); + .unwrap_or_else(|| panic!("Failed to get crate data for {cnum:?}")); CrateMetadataRef { cdata, cstore: self } } @@ -520,8 +520,8 @@ impl<'a> CrateLoader<'a> { })) } - fn resolve_crate<'b>( - &'b mut self, + fn resolve_crate( + &mut self, name: Symbol, span: Span, dep_kind: CrateDepKind, diff --git a/compiler/rustc_metadata/src/dependency_format.rs b/compiler/rustc_metadata/src/dependency_format.rs index 6112ec9e4e9..cee4ba56a9d 100644 --- a/compiler/rustc_metadata/src/dependency_format.rs +++ b/compiler/rustc_metadata/src/dependency_format.rs @@ -54,7 +54,7 @@ use crate::creader::CStore; use crate::errors::{ BadPanicStrategy, CrateDepMultiple, IncompatiblePanicInDropStrategy, LibRequired, - RequiredPanicStrategy, RlibRequired, TwoPanicRuntimes, + RequiredPanicStrategy, RlibRequired, RustcLibRequired, TwoPanicRuntimes, }; use rustc_data_structures::fx::FxHashMap; @@ -224,7 +224,12 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList { Linkage::Static => "rlib", _ => "dylib", }; - sess.emit_err(LibRequired { crate_name: tcx.crate_name(cnum), kind: kind }); + let crate_name = tcx.crate_name(cnum); + if crate_name.as_str().starts_with("rustc_") { + sess.emit_err(RustcLibRequired { crate_name, kind }); + } else { + sess.emit_err(LibRequired { crate_name, kind }); + } } } } diff --git a/compiler/rustc_metadata/src/errors.rs b/compiler/rustc_metadata/src/errors.rs index de2a879f1d7..02c03114eb6 100644 --- a/compiler/rustc_metadata/src/errors.rs +++ b/compiler/rustc_metadata/src/errors.rs @@ -25,6 +25,14 @@ pub struct LibRequired<'a> { } #[derive(Diagnostic)] +#[diag(metadata_rustc_lib_required)] +#[help] +pub struct RustcLibRequired<'a> { + pub crate_name: Symbol, + pub kind: &'a str, +} + +#[derive(Diagnostic)] #[diag(metadata_crate_dep_multiple)] #[help] pub struct CrateDepMultiple { @@ -486,26 +494,16 @@ impl IntoDiagnostic<'_> for MultipleCandidates { let mut diag = handler.struct_err(rustc_errors::fluent::metadata_multiple_candidates); diag.set_arg("crate_name", self.crate_name); diag.set_arg("flavor", self.flavor); - diag.code(error_code!(E0465)); + diag.code(error_code!(E0464)); diag.set_span(self.span); for (i, candidate) in self.candidates.iter().enumerate() { - diag.span_note(self.span, &format!("candidate #{}: {}", i + 1, candidate.display())); + diag.note(&format!("candidate #{}: {}", i + 1, candidate.display())); } diag } } #[derive(Diagnostic)] -#[diag(metadata_multiple_matching_crates, code = "E0464")] -#[note] -pub struct MultipleMatchingCrates { - #[primary_span] - pub span: Span, - pub crate_name: Symbol, - pub candidates: String, -} - -#[derive(Diagnostic)] #[diag(metadata_symbol_conflicts_current, code = "E0519")] pub struct SymbolConflictsCurrent { #[primary_span] diff --git a/compiler/rustc_metadata/src/locator.rs b/compiler/rustc_metadata/src/locator.rs index 15546092e41..92dc5bd41cb 100644 --- a/compiler/rustc_metadata/src/locator.rs +++ b/compiler/rustc_metadata/src/locator.rs @@ -216,9 +216,8 @@ use crate::creader::Library; use crate::errors::{ CannotFindCrate, CrateLocationUnknownType, DlError, ExternLocationNotExist, ExternLocationNotFile, FoundStaticlib, IncompatibleRustc, InvalidMetadataFiles, - LibFilenameForm, MultipleCandidates, MultipleMatchingCrates, NewerCrateVersion, - NoCrateWithTriple, NoDylibPlugin, NonAsciiName, StableCrateIdCollision, SymbolConflictsCurrent, - SymbolConflictsOthers, + LibFilenameForm, MultipleCandidates, NewerCrateVersion, NoCrateWithTriple, NoDylibPlugin, + NonAsciiName, StableCrateIdCollision, SymbolConflictsCurrent, SymbolConflictsOthers, }; use crate::rmeta::{rustc_version, MetadataBlob, METADATA_HEADER}; @@ -240,7 +239,6 @@ use rustc_target::spec::{Target, TargetTriple}; use snap::read::FrameDecoder; use std::borrow::Cow; -use std::fmt::Write as _; use std::io::{Read, Result as IoResult, Write}; use std::path::{Path, PathBuf}; use std::{cmp, fmt, fs}; @@ -482,7 +480,22 @@ impl<'a> CrateLocator<'a> { match libraries.len() { 0 => Ok(None), 1 => Ok(Some(libraries.into_iter().next().unwrap().1)), - _ => Err(CrateError::MultipleMatchingCrates(self.crate_name, libraries)), + _ => { + let mut libraries: Vec<_> = libraries.into_values().collect(); + + libraries.sort_by_cached_key(|lib| lib.source.paths().next().unwrap().clone()); + let candidates = libraries + .iter() + .map(|lib| lib.source.paths().next().unwrap().clone()) + .collect::<Vec<_>>(); + + Err(CrateError::MultipleCandidates( + self.crate_name, + // these are the same for all candidates + get_flavor_from_path(candidates.first().unwrap()), + candidates, + )) + } } } @@ -882,17 +895,22 @@ pub fn list_file_metadata( metadata_loader: &dyn MetadataLoader, out: &mut dyn Write, ) -> IoResult<()> { + let flavor = get_flavor_from_path(path); + match get_metadata_section(target, flavor, path, metadata_loader) { + Ok(metadata) => metadata.list_crate_metadata(out), + Err(msg) => write!(out, "{}\n", msg), + } +} + +fn get_flavor_from_path(path: &Path) -> CrateFlavor { let filename = path.file_name().unwrap().to_str().unwrap(); - let flavor = if filename.ends_with(".rlib") { + + if filename.ends_with(".rlib") { CrateFlavor::Rlib } else if filename.ends_with(".rmeta") { CrateFlavor::Rmeta } else { CrateFlavor::Dylib - }; - match get_metadata_section(target, flavor, path, metadata_loader) { - Ok(metadata) => metadata.list_crate_metadata(out), - Err(msg) => write!(out, "{}\n", msg), } } @@ -931,7 +949,6 @@ pub(crate) enum CrateError { ExternLocationNotExist(Symbol, PathBuf), ExternLocationNotFile(Symbol, PathBuf), MultipleCandidates(Symbol, CrateFlavor, Vec<PathBuf>), - MultipleMatchingCrates(Symbol, FxHashMap<Svh, Library>), SymbolConflictsCurrent(Symbol), SymbolConflictsOthers(Symbol), StableCrateIdCollision(Symbol, Symbol), @@ -972,37 +989,7 @@ impl CrateError { sess.emit_err(ExternLocationNotFile { span, crate_name, location: &loc }); } CrateError::MultipleCandidates(crate_name, flavor, candidates) => { - sess.emit_err(MultipleCandidates { span, flavor: flavor, crate_name, candidates }); - } - CrateError::MultipleMatchingCrates(crate_name, libraries) => { - let mut libraries: Vec<_> = libraries.into_values().collect(); - // Make ordering of candidates deterministic. - // This has to `clone()` to work around lifetime restrictions with `sort_by_key()`. - // `sort_by()` could be used instead, but this is in the error path, - // so the performance shouldn't matter. - libraries.sort_by_cached_key(|lib| lib.source.paths().next().unwrap().clone()); - let candidates = libraries - .iter() - .map(|lib| { - let crate_name = lib.metadata.get_root().name(); - let crate_name = crate_name.as_str(); - let mut paths = lib.source.paths(); - - // This `unwrap()` should be okay because there has to be at least one - // source file. `CrateSource`'s docs confirm that too. - let mut s = format!( - "\ncrate `{}`: {}", - crate_name, - paths.next().unwrap().display() - ); - let padding = 8 + crate_name.len(); - for path in paths { - write!(s, "\n{:>padding$}", path.display(), padding = padding).unwrap(); - } - s - }) - .collect::<String>(); - sess.emit_err(MultipleMatchingCrates { span, crate_name, candidates }); + sess.emit_err(MultipleCandidates { span, crate_name, flavor, candidates }); } CrateError::SymbolConflictsCurrent(root_name) => { sess.emit_err(SymbolConflictsCurrent { span, crate_name: root_name }); @@ -1011,11 +998,7 @@ impl CrateError { sess.emit_err(SymbolConflictsOthers { span, crate_name: root_name }); } CrateError::StableCrateIdCollision(crate_name0, crate_name1) => { - sess.emit_err(StableCrateIdCollision { - span, - crate_name0: crate_name0, - crate_name1: crate_name1, - }); + sess.emit_err(StableCrateIdCollision { span, crate_name0, crate_name1 }); } CrateError::DlOpen(s) | CrateError::DlSym(s) => { sess.emit_err(DlError { span, err: s }); @@ -1074,7 +1057,7 @@ impl CrateError { } sess.emit_err(NoCrateWithTriple { span, - crate_name: crate_name, + crate_name, locator_triple: locator.triple.triple(), add_info, found_crates, diff --git a/compiler/rustc_metadata/src/native_libs.rs b/compiler/rustc_metadata/src/native_libs.rs index 1fd35adf1bd..59869ee4173 100644 --- a/compiler/rustc_metadata/src/native_libs.rs +++ b/compiler/rustc_metadata/src/native_libs.rs @@ -45,7 +45,7 @@ pub fn find_native_static_library( for path in search_paths { for (prefix, suffix) in &formats { - let test = path.join(format!("{}{}{}", prefix, name, suffix)); + let test = path.join(format!("{prefix}{name}{suffix}")); if test.exists() { return test; } diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 4370d4bd758..143d8f2f1e1 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<DecodeContext<'a, 'tcx>> 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::<CrateRoot>::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)) } diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 9d0ccfeb168..cb451931dfe 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -223,14 +223,14 @@ provide! { tcx, def_id, other, cdata, generator_kind => { table } trait_def => { table } deduced_param_attrs => { table } - 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) } 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 40c94b372bb..a6133f1b417 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<DecodeContext<'a, 'tcx>> 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 4af423f2a22..0d9f216700f 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<Symbol, usize>, } -/// 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<EncodeContext<'a, 'tcx>> for LazyTable<I, T> { impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> 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()); } @@ -276,7 +276,7 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> 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:?}"), } }; @@ -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 @@ -1093,7 +1087,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; } @@ -1197,7 +1191,7 @@ 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); } @@ -1564,7 +1558,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let trait_ref = self.tcx.impl_trait_ref(def_id); 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()); } diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index 6b60577c902..26a41f633ff 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -416,11 +416,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; |
