@@ -216,9 +216,8 @@ use crate::creader::Library;
216
216
use crate::errors::{
217
217
CannotFindCrate, CrateLocationUnknownType, DlError, ExternLocationNotExist,
218
218
ExternLocationNotFile, FoundStaticlib, IncompatibleRustc, InvalidMetadataFiles,
219
- LibFilenameForm, MultipleCandidates, MultipleMatchingCrates, NewerCrateVersion,
220
- NoCrateWithTriple, NoDylibPlugin, NonAsciiName, StableCrateIdCollision, SymbolConflictsCurrent,
221
- SymbolConflictsOthers,
219
+ LibFilenameForm, MultipleCandidates, NewerCrateVersion, NoCrateWithTriple, NoDylibPlugin,
220
+ NonAsciiName, StableCrateIdCollision, SymbolConflictsCurrent, SymbolConflictsOthers,
222
221
};
223
222
use crate::rmeta::{rustc_version, MetadataBlob, METADATA_HEADER};
224
223
@@ -240,7 +239,6 @@ use rustc_target::spec::{Target, TargetTriple};
240
239
241
240
use snap::read::FrameDecoder;
242
241
use std::borrow::Cow;
243
- use std::fmt::Write as _;
244
242
use std::io::{Read, Result as IoResult, Write};
245
243
use std::path::{Path, PathBuf};
246
244
use std::{cmp, fmt, fs};
@@ -482,7 +480,22 @@ impl<'a> CrateLocator<'a> {
482
480
match libraries.len() {
483
481
0 => Ok(None),
484
482
1 => Ok(Some(libraries.into_iter().next().unwrap().1)),
485
- _ => Err(CrateError::MultipleMatchingCrates(self.crate_name, libraries)),
483
+ _ => {
484
+ let mut libraries: Vec<_> = libraries.into_values().collect();
485
+
486
+ libraries.sort_by_cached_key(|lib| lib.source.paths().next().unwrap().clone());
487
+ let candidates = libraries
488
+ .iter()
489
+ .map(|lib| lib.source.paths().next().unwrap().clone())
490
+ .collect::<Vec<_>>();
491
+
492
+ Err(CrateError::MultipleCandidates(
493
+ self.crate_name,
494
+ // these are the same for all candidates
495
+ get_flavor_from_path(candidates.first().unwrap()),
496
+ candidates,
497
+ ))
498
+ }
486
499
}
487
500
}
488
501
@@ -882,17 +895,22 @@ pub fn list_file_metadata(
882
895
metadata_loader: &dyn MetadataLoader,
883
896
out: &mut dyn Write,
884
897
) -> IoResult<()> {
898
+ let flavor = get_flavor_from_path(path);
899
+ match get_metadata_section(target, flavor, path, metadata_loader) {
900
+ Ok(metadata) => metadata.list_crate_metadata(out),
901
+ Err(msg) => write!(out, "{}\n", msg),
902
+ }
903
+ }
904
+
905
+ fn get_flavor_from_path(path: &Path) -> CrateFlavor {
885
906
let filename = path.file_name().unwrap().to_str().unwrap();
886
- let flavor = if filename.ends_with(".rlib") {
907
+
908
+ if filename.ends_with(".rlib") {
887
909
CrateFlavor::Rlib
888
910
} else if filename.ends_with(".rmeta") {
889
911
CrateFlavor::Rmeta
890
912
} else {
891
913
CrateFlavor::Dylib
892
- };
893
- match get_metadata_section(target, flavor, path, metadata_loader) {
894
- Ok(metadata) => metadata.list_crate_metadata(out),
895
- Err(msg) => write!(out, "{}\n", msg),
896
914
}
897
915
}
898
916
@@ -931,7 +949,6 @@ pub(crate) enum CrateError {
931
949
ExternLocationNotExist(Symbol, PathBuf),
932
950
ExternLocationNotFile(Symbol, PathBuf),
933
951
MultipleCandidates(Symbol, CrateFlavor, Vec<PathBuf>),
934
- MultipleMatchingCrates(Symbol, FxHashMap<Svh, Library>),
935
952
SymbolConflictsCurrent(Symbol),
936
953
SymbolConflictsOthers(Symbol),
937
954
StableCrateIdCollision(Symbol, Symbol),
@@ -972,37 +989,7 @@ impl CrateError {
972
989
sess.emit_err(ExternLocationNotFile { span, crate_name, location: &loc });
973
990
}
974
991
CrateError::MultipleCandidates(crate_name, flavor, candidates) => {
975
- sess.emit_err(MultipleCandidates { span, flavor: flavor, crate_name, candidates });
976
- }
977
- CrateError::MultipleMatchingCrates(crate_name, libraries) => {
978
- let mut libraries: Vec<_> = libraries.into_values().collect();
979
- // Make ordering of candidates deterministic.
980
- // This has to `clone()` to work around lifetime restrictions with `sort_by_key()`.
981
- // `sort_by()` could be used instead, but this is in the error path,
982
- // so the performance shouldn't matter.
983
- libraries.sort_by_cached_key(|lib| lib.source.paths().next().unwrap().clone());
984
- let candidates = libraries
985
- .iter()
986
- .map(|lib| {
987
- let crate_name = lib.metadata.get_root().name();
988
- let crate_name = crate_name.as_str();
989
- let mut paths = lib.source.paths();
990
-
991
- // This `unwrap()` should be okay because there has to be at least one
992
- // source file. `CrateSource`'s docs confirm that too.
993
- let mut s = format!(
994
- "\ncrate `{}`: {}",
995
- crate_name,
996
- paths.next().unwrap().display()
997
- );
998
- let padding = 8 + crate_name.len();
999
- for path in paths {
1000
- write!(s, "\n{:>padding$}", path.display(), padding = padding).unwrap();
1001
- }
1002
- s
1003
- })
1004
- .collect::<String>();
1005
- sess.emit_err(MultipleMatchingCrates { span, crate_name, candidates });
992
+ sess.emit_err(MultipleCandidates { span, crate_name, flavor, candidates });
1006
993
}
1007
994
CrateError::SymbolConflictsCurrent(root_name) => {
1008
995
sess.emit_err(SymbolConflictsCurrent { span, crate_name: root_name });
0 commit comments