@@ -569,31 +569,47 @@ impl<'a> CrateLocator<'a> {
569569 debug ! ( "skipping empty file" ) ;
570570 continue ;
571571 }
572- let ( hash, metadata) =
573- match get_metadata_section ( self . target , flavor, & lib, self . metadata_loader ) {
574- Ok ( blob) => {
575- if let Some ( h) = self . crate_matches ( & blob, & lib) {
576- ( h, blob)
577- } else {
578- info ! ( "metadata mismatch" ) ;
579- continue ;
580- }
581- }
582- Err ( MetadataError :: LoadFailure ( err) ) => {
583- info ! ( "no metadata found: {}" , err) ;
584- // The file was present and created by the same compiler version, but we
585- // couldn't load it for some reason. Give a hard error instead of silently
586- // ignoring it, but only if we would have given an error anyway.
587- self . crate_rejections
588- . via_invalid
589- . push ( CrateMismatch { path : lib, got : err } ) ;
590- continue ;
591- }
592- Err ( err @ MetadataError :: NotPresent ( _) ) => {
593- info ! ( "no metadata found: {}" , err) ;
572+ let ( hash, metadata) = match get_metadata_section (
573+ self . target ,
574+ flavor,
575+ & lib,
576+ self . metadata_loader ,
577+ self . cfg_version ,
578+ ) {
579+ Ok ( blob) => {
580+ if let Some ( h) = self . crate_matches ( & blob, & lib) {
581+ ( h, blob)
582+ } else {
583+ info ! ( "metadata mismatch" ) ;
594584 continue ;
595585 }
596- } ;
586+ }
587+ Err ( MetadataError :: VersionMismatch { expected_version, found_version } ) => {
588+ // The file was present and created by the same compiler version, but we
589+ // couldn't load it for some reason. Give a hard error instead of silently
590+ // ignoring it, but only if we would have given an error anyway.
591+ info ! (
592+ "Rejecting via version: expected {} got {}" ,
593+ expected_version, found_version
594+ ) ;
595+ self . crate_rejections
596+ . via_version
597+ . push ( CrateMismatch { path : lib, got : found_version } ) ;
598+ continue ;
599+ }
600+ Err ( MetadataError :: LoadFailure ( err) ) => {
601+ info ! ( "no metadata found: {}" , err) ;
602+ // The file was present and created by the same compiler version, but we
603+ // couldn't load it for some reason. Give a hard error instead of silently
604+ // ignoring it, but only if we would have given an error anyway.
605+ self . crate_rejections . via_invalid . push ( CrateMismatch { path : lib, got : err } ) ;
606+ continue ;
607+ }
608+ Err ( err @ MetadataError :: NotPresent ( _) ) => {
609+ info ! ( "no metadata found: {}" , err) ;
610+ continue ;
611+ }
612+ } ;
597613 // If we see multiple hashes, emit an error about duplicate candidates.
598614 if slot. as_ref ( ) . is_some_and ( |s| s. 0 != hash) {
599615 if let Some ( candidates) = err_data {
@@ -648,16 +664,6 @@ impl<'a> CrateLocator<'a> {
648664 }
649665
650666 fn crate_matches ( & mut self , metadata : & MetadataBlob , libpath : & Path ) -> Option < Svh > {
651- let rustc_version = rustc_version ( self . cfg_version ) ;
652- let found_version = metadata. get_rustc_version ( ) ;
653- if found_version != rustc_version {
654- info ! ( "Rejecting via version: expected {} got {}" , rustc_version, found_version) ;
655- self . crate_rejections
656- . via_version
657- . push ( CrateMismatch { path : libpath. to_path_buf ( ) , got : found_version } ) ;
658- return None ;
659- }
660-
661667 let header = metadata. get_header ( ) ;
662668 if header. is_proc_macro_crate != self . is_proc_macro {
663669 info ! (
@@ -770,6 +776,7 @@ fn get_metadata_section<'p>(
770776 flavor : CrateFlavor ,
771777 filename : & ' p Path ,
772778 loader : & dyn MetadataLoader ,
779+ cfg_version : & ' static str ,
773780) -> Result < MetadataBlob , MetadataError < ' p > > {
774781 if !filename. exists ( ) {
775782 return Err ( MetadataError :: NotPresent ( filename) ) ;
@@ -847,13 +854,18 @@ fn get_metadata_section<'p>(
847854 }
848855 } ;
849856 let blob = MetadataBlob ( raw_bytes) ;
850- if blob. is_compatible ( ) {
851- Ok ( blob)
852- } else {
853- Err ( MetadataError :: LoadFailure ( format ! (
857+ match blob. check_compatibility ( cfg_version) {
858+ Ok ( ( ) ) => Ok ( blob) ,
859+ Err ( None ) => Err ( MetadataError :: LoadFailure ( format ! (
854860 "invalid metadata version found: {}" ,
855861 filename. display( )
856- ) ) )
862+ ) ) ) ,
863+ Err ( Some ( found_version) ) => {
864+ return Err ( MetadataError :: VersionMismatch {
865+ expected_version : rustc_version ( cfg_version) ,
866+ found_version,
867+ } ) ;
868+ }
857869 }
858870}
859871
@@ -864,9 +876,10 @@ pub fn list_file_metadata(
864876 metadata_loader : & dyn MetadataLoader ,
865877 out : & mut dyn Write ,
866878 ls_kinds : & [ String ] ,
879+ cfg_version : & ' static str ,
867880) -> IoResult < ( ) > {
868881 let flavor = get_flavor_from_path ( path) ;
869- match get_metadata_section ( target, flavor, path, metadata_loader) {
882+ match get_metadata_section ( target, flavor, path, metadata_loader, cfg_version ) {
870883 Ok ( metadata) => metadata. list_crate_metadata ( out, ls_kinds) ,
871884 Err ( msg) => write ! ( out, "{msg}\n " ) ,
872885 }
@@ -932,6 +945,8 @@ enum MetadataError<'a> {
932945 NotPresent ( & ' a Path ) ,
933946 /// The file was present and invalid.
934947 LoadFailure ( String ) ,
948+ /// The file was present, but compiled with a different rustc version.
949+ VersionMismatch { expected_version : String , found_version : String } ,
935950}
936951
937952impl fmt:: Display for MetadataError < ' _ > {
@@ -941,6 +956,12 @@ impl fmt::Display for MetadataError<'_> {
941956 f. write_str ( & format ! ( "no such file: '{}'" , filename. display( ) ) )
942957 }
943958 MetadataError :: LoadFailure ( msg) => f. write_str ( msg) ,
959+ MetadataError :: VersionMismatch { expected_version, found_version } => {
960+ f. write_str ( & format ! (
961+ "rustc version mismatch. expected {}, found {}" ,
962+ expected_version, found_version,
963+ ) )
964+ }
944965 }
945966 }
946967}
0 commit comments