@@ -569,31 +569,47 @@ impl<'a> CrateLocator<'a> {
569
569
debug ! ( "skipping empty file" ) ;
570
570
continue ;
571
571
}
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" ) ;
594
584
continue ;
595
585
}
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
+ } ;
597
613
// If we see multiple hashes, emit an error about duplicate candidates.
598
614
if slot. as_ref ( ) . is_some_and ( |s| s. 0 != hash) {
599
615
if let Some ( candidates) = err_data {
@@ -648,16 +664,6 @@ impl<'a> CrateLocator<'a> {
648
664
}
649
665
650
666
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
-
661
667
let header = metadata. get_header ( ) ;
662
668
if header. is_proc_macro_crate != self . is_proc_macro {
663
669
info ! (
@@ -770,6 +776,7 @@ fn get_metadata_section<'p>(
770
776
flavor : CrateFlavor ,
771
777
filename : & ' p Path ,
772
778
loader : & dyn MetadataLoader ,
779
+ cfg_version : & ' static str ,
773
780
) -> Result < MetadataBlob , MetadataError < ' p > > {
774
781
if !filename. exists ( ) {
775
782
return Err ( MetadataError :: NotPresent ( filename) ) ;
@@ -847,13 +854,12 @@ fn get_metadata_section<'p>(
847
854
}
848
855
} ;
849
856
let blob = MetadataBlob ( raw_bytes) ;
850
- if blob. is_compatible ( ) {
851
- Ok ( blob)
852
- } else {
853
- Err ( MetadataError :: LoadFailure ( format ! (
854
- "invalid metadata version found: {}" ,
855
- filename. display( )
856
- ) ) )
857
+ match blob. check_compatibility ( cfg_version) {
858
+ Ok ( ( ) ) => Ok ( blob) ,
859
+ Err ( version) => Err ( MetadataError :: VersionMismatch {
860
+ expected_version : cfg_version,
861
+ found_version : version,
862
+ } ) ,
857
863
}
858
864
}
859
865
@@ -864,9 +870,10 @@ pub fn list_file_metadata(
864
870
metadata_loader : & dyn MetadataLoader ,
865
871
out : & mut dyn Write ,
866
872
ls_kinds : & [ String ] ,
873
+ cfg_version : & ' static str ,
867
874
) -> IoResult < ( ) > {
868
875
let flavor = get_flavor_from_path ( path) ;
869
- match get_metadata_section ( target, flavor, path, metadata_loader) {
876
+ match get_metadata_section ( target, flavor, path, metadata_loader, cfg_version ) {
870
877
Ok ( metadata) => metadata. list_crate_metadata ( out, ls_kinds) ,
871
878
Err ( msg) => write ! ( out, "{msg}\n " ) ,
872
879
}
@@ -932,6 +939,8 @@ enum MetadataError<'a> {
932
939
NotPresent ( & ' a Path ) ,
933
940
/// The file was present and invalid.
934
941
LoadFailure ( String ) ,
942
+ /// The file was present, but compiled with a different rustc version.
943
+ VersionMismatch { expected_version : & ' static str , found_version : String } ,
935
944
}
936
945
937
946
impl fmt:: Display for MetadataError < ' _ > {
@@ -941,6 +950,12 @@ impl fmt::Display for MetadataError<'_> {
941
950
f. write_str ( & format ! ( "no such file: '{}'" , filename. display( ) ) )
942
951
}
943
952
MetadataError :: LoadFailure ( msg) => f. write_str ( msg) ,
953
+ MetadataError :: VersionMismatch { expected_version, found_version } => {
954
+ f. write_str ( & format ! (
955
+ "rustc version mismatch. expected {}, found {}" ,
956
+ expected_version, found_version,
957
+ ) )
958
+ }
944
959
}
945
960
}
946
961
}
0 commit comments