@@ -567,31 +567,47 @@ impl<'a> CrateLocator<'a> {
567
567
debug ! ( "skipping empty file" ) ;
568
568
continue ;
569
569
}
570
- let ( hash, metadata) =
571
- match get_metadata_section ( self . target , flavor, & lib, self . metadata_loader ) {
572
- Ok ( blob) => {
573
- if let Some ( h) = self . crate_matches ( & blob, & lib) {
574
- ( h, blob)
575
- } else {
576
- info ! ( "metadata mismatch" ) ;
577
- continue ;
578
- }
579
- }
580
- Err ( MetadataError :: LoadFailure ( err) ) => {
581
- info ! ( "no metadata found: {}" , err) ;
582
- // The file was present and created by the same compiler version, but we
583
- // couldn't load it for some reason. Give a hard error instead of silently
584
- // ignoring it, but only if we would have given an error anyway.
585
- self . crate_rejections
586
- . via_invalid
587
- . push ( CrateMismatch { path : lib, got : err } ) ;
588
- continue ;
589
- }
590
- Err ( err @ MetadataError :: NotPresent ( _) ) => {
591
- info ! ( "no metadata found: {}" , err) ;
570
+ let ( hash, metadata) = match get_metadata_section (
571
+ self . target ,
572
+ flavor,
573
+ & lib,
574
+ self . metadata_loader ,
575
+ self . cfg_version ,
576
+ ) {
577
+ Ok ( blob) => {
578
+ if let Some ( h) = self . crate_matches ( & blob, & lib) {
579
+ ( h, blob)
580
+ } else {
581
+ info ! ( "metadata mismatch" ) ;
592
582
continue ;
593
583
}
594
- } ;
584
+ }
585
+ Err ( MetadataError :: VersionMismatch { expected_version, found_version } ) => {
586
+ // The file was present and created by the same compiler version, but we
587
+ // couldn't load it for some reason. Give a hard error instead of silently
588
+ // ignoring it, but only if we would have given an error anyway.
589
+ info ! (
590
+ "Rejecting via version: expected {} got {}" ,
591
+ expected_version, found_version
592
+ ) ;
593
+ self . crate_rejections
594
+ . via_version
595
+ . push ( CrateMismatch { path : lib, got : found_version } ) ;
596
+ continue ;
597
+ }
598
+ Err ( MetadataError :: LoadFailure ( err) ) => {
599
+ info ! ( "no metadata found: {}" , err) ;
600
+ // The file was present and created by the same compiler version, but we
601
+ // couldn't load it for some reason. Give a hard error instead of silently
602
+ // ignoring it, but only if we would have given an error anyway.
603
+ self . crate_rejections . via_invalid . push ( CrateMismatch { path : lib, got : err } ) ;
604
+ continue ;
605
+ }
606
+ Err ( err @ MetadataError :: NotPresent ( _) ) => {
607
+ info ! ( "no metadata found: {}" , err) ;
608
+ continue ;
609
+ }
610
+ } ;
595
611
// If we see multiple hashes, emit an error about duplicate candidates.
596
612
if slot. as_ref ( ) . is_some_and ( |s| s. 0 != hash) {
597
613
if let Some ( candidates) = err_data {
@@ -620,16 +636,6 @@ impl<'a> CrateLocator<'a> {
620
636
}
621
637
622
638
fn crate_matches ( & mut self , metadata : & MetadataBlob , libpath : & Path ) -> Option < Svh > {
623
- let rustc_version = rustc_version ( self . cfg_version ) ;
624
- let found_version = metadata. get_rustc_version ( ) ;
625
- if found_version != rustc_version {
626
- info ! ( "Rejecting via version: expected {} got {}" , rustc_version, found_version) ;
627
- self . crate_rejections
628
- . via_version
629
- . push ( CrateMismatch { path : libpath. to_path_buf ( ) , got : found_version } ) ;
630
- return None ;
631
- }
632
-
633
639
let header = metadata. get_header ( ) ;
634
640
if header. is_proc_macro_crate != self . is_proc_macro {
635
641
info ! (
@@ -742,6 +748,7 @@ fn get_metadata_section<'p>(
742
748
flavor : CrateFlavor ,
743
749
filename : & ' p Path ,
744
750
loader : & dyn MetadataLoader ,
751
+ cfg_version : & ' static str ,
745
752
) -> Result < MetadataBlob , MetadataError < ' p > > {
746
753
if !filename. exists ( ) {
747
754
return Err ( MetadataError :: NotPresent ( filename) ) ;
@@ -819,13 +826,18 @@ fn get_metadata_section<'p>(
819
826
}
820
827
} ;
821
828
let blob = MetadataBlob ( raw_bytes) ;
822
- if blob. is_compatible ( ) {
823
- Ok ( blob)
824
- } else {
825
- Err ( MetadataError :: LoadFailure ( format ! (
829
+ match blob. check_compatibility ( cfg_version) {
830
+ Ok ( ( ) ) => Ok ( blob) ,
831
+ Err ( None ) => Err ( MetadataError :: LoadFailure ( format ! (
826
832
"invalid metadata version found: {}" ,
827
833
filename. display( )
828
- ) ) )
834
+ ) ) ) ,
835
+ Err ( Some ( found_version) ) => {
836
+ return Err ( MetadataError :: VersionMismatch {
837
+ expected_version : rustc_version ( cfg_version) ,
838
+ found_version,
839
+ } ) ;
840
+ }
829
841
}
830
842
}
831
843
@@ -836,9 +848,10 @@ pub fn list_file_metadata(
836
848
metadata_loader : & dyn MetadataLoader ,
837
849
out : & mut dyn Write ,
838
850
ls_kinds : & [ String ] ,
851
+ cfg_version : & ' static str ,
839
852
) -> IoResult < ( ) > {
840
853
let flavor = get_flavor_from_path ( path) ;
841
- match get_metadata_section ( target, flavor, path, metadata_loader) {
854
+ match get_metadata_section ( target, flavor, path, metadata_loader, cfg_version ) {
842
855
Ok ( metadata) => metadata. list_crate_metadata ( out, ls_kinds) ,
843
856
Err ( msg) => write ! ( out, "{msg}\n " ) ,
844
857
}
@@ -904,6 +917,8 @@ enum MetadataError<'a> {
904
917
NotPresent ( & ' a Path ) ,
905
918
/// The file was present and invalid.
906
919
LoadFailure ( String ) ,
920
+ /// The file was present, but compiled with a different rustc version.
921
+ VersionMismatch { expected_version : String , found_version : String } ,
907
922
}
908
923
909
924
impl fmt:: Display for MetadataError < ' _ > {
@@ -913,6 +928,12 @@ impl fmt::Display for MetadataError<'_> {
913
928
f. write_str ( & format ! ( "no such file: '{}'" , filename. display( ) ) )
914
929
}
915
930
MetadataError :: LoadFailure ( msg) => f. write_str ( msg) ,
931
+ MetadataError :: VersionMismatch { expected_version, found_version } => {
932
+ f. write_str ( & format ! (
933
+ "rustc version mismatch. expected {}, found {}" ,
934
+ expected_version, found_version,
935
+ ) )
936
+ }
916
937
}
917
938
}
918
939
}
0 commit comments