@@ -216,9 +216,8 @@ use crate::creader::Library;
216216use crate :: errors:: {
217217 CannotFindCrate , CrateLocationUnknownType , DlError , ExternLocationNotExist ,
218218 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 ,
222221} ;
223222use crate :: rmeta:: { rustc_version, MetadataBlob , METADATA_HEADER } ;
224223
@@ -240,7 +239,6 @@ use rustc_target::spec::{Target, TargetTriple};
240239
241240use snap:: read:: FrameDecoder ;
242241use std:: borrow:: Cow ;
243- use std:: fmt:: Write as _;
244242use std:: io:: { Read , Result as IoResult , Write } ;
245243use std:: path:: { Path , PathBuf } ;
246244use std:: { cmp, fmt, fs} ;
@@ -482,7 +480,22 @@ impl<'a> CrateLocator<'a> {
482480 match libraries. len ( ) {
483481 0 => Ok ( None ) ,
484482 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+ }
486499 }
487500 }
488501
@@ -882,17 +895,22 @@ pub fn list_file_metadata(
882895 metadata_loader : & dyn MetadataLoader ,
883896 out : & mut dyn Write ,
884897) -> 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 {
885906 let filename = path. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
886- let flavor = if filename. ends_with ( ".rlib" ) {
907+
908+ if filename. ends_with ( ".rlib" ) {
887909 CrateFlavor :: Rlib
888910 } else if filename. ends_with ( ".rmeta" ) {
889911 CrateFlavor :: Rmeta
890912 } else {
891913 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) ,
896914 }
897915}
898916
@@ -931,7 +949,6 @@ pub(crate) enum CrateError {
931949 ExternLocationNotExist ( Symbol , PathBuf ) ,
932950 ExternLocationNotFile ( Symbol , PathBuf ) ,
933951 MultipleCandidates ( Symbol , CrateFlavor , Vec < PathBuf > ) ,
934- MultipleMatchingCrates ( Symbol , FxHashMap < Svh , Library > ) ,
935952 SymbolConflictsCurrent ( Symbol ) ,
936953 SymbolConflictsOthers ( Symbol ) ,
937954 StableCrateIdCollision ( Symbol , Symbol ) ,
@@ -972,37 +989,7 @@ impl CrateError {
972989 sess. emit_err ( ExternLocationNotFile { span, crate_name, location : & loc } ) ;
973990 }
974991 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- "\n crate `{}`: {}" ,
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 } ) ;
1006993 }
1007994 CrateError :: SymbolConflictsCurrent ( root_name) => {
1008995 sess. emit_err ( SymbolConflictsCurrent { span, crate_name : root_name } ) ;
0 commit comments