@@ -16,7 +16,7 @@ use rustc_session::config::{self, CrateType, ErrorOutputType};
16
16
use rustc_session:: parse:: ParseSess ;
17
17
use rustc_session:: { lint, Session } ;
18
18
use rustc_span:: edition:: Edition ;
19
- use rustc_span:: source_map:: SourceMap ;
19
+ use rustc_span:: source_map:: { FilePathMapping , SourceMap } ;
20
20
use rustc_span:: symbol:: sym;
21
21
use rustc_span:: { BytePos , FileName , Pos , Span , DUMMY_SP } ;
22
22
use rustc_target:: spec:: { Target , TargetTriple } ;
@@ -86,6 +86,7 @@ pub(crate) fn run(options: RustdocOptions) -> Result<(), ErrorGuaranteed> {
86
86
edition : options. edition ,
87
87
target_triple : options. target . clone ( ) ,
88
88
crate_name : options. crate_name . clone ( ) ,
89
+ remap_path_prefix : options. remap_path_prefix . clone ( ) ,
89
90
..config:: Options :: default ( )
90
91
} ;
91
92
@@ -577,7 +578,6 @@ pub(crate) fn make_test(
577
578
use rustc_errors:: emitter:: { Emitter , HumanEmitter } ;
578
579
use rustc_errors:: DiagCtxt ;
579
580
use rustc_parse:: parser:: ForceCollect ;
580
- use rustc_span:: source_map:: FilePathMapping ;
581
581
582
582
let filename = FileName :: anon_source_code ( s) ;
583
583
let source = crates + everything_else;
@@ -768,7 +768,6 @@ fn check_if_attr_is_complete(source: &str, edition: Edition) -> bool {
768
768
rustc_span:: create_session_if_not_set_then ( edition, |_| {
769
769
use rustc_errors:: emitter:: HumanEmitter ;
770
770
use rustc_errors:: DiagCtxt ;
771
- use rustc_span:: source_map:: FilePathMapping ;
772
771
773
772
let filename = FileName :: anon_source_code ( source) ;
774
773
// Any errors in parsing should also appear when the doctest is compiled for real, so just
@@ -976,7 +975,7 @@ impl Collector {
976
975
if !item_path. is_empty ( ) {
977
976
item_path. push ( ' ' ) ;
978
977
}
979
- format ! ( "{} - {item_path}(line {line})" , filename. prefer_local ( ) )
978
+ format ! ( "{} - {item_path}(line {line})" , filename. prefer_remapped_unconditionaly ( ) )
980
979
}
981
980
982
981
pub ( crate ) fn set_position ( & mut self , position : Span ) {
@@ -986,11 +985,15 @@ impl Collector {
986
985
fn get_filename ( & self ) -> FileName {
987
986
if let Some ( ref source_map) = self . source_map {
988
987
let filename = source_map. span_to_filename ( self . position ) ;
989
- if let FileName :: Real ( ref filename) = filename
990
- && let Ok ( cur_dir) = env:: current_dir ( )
991
- && let Some ( local_path) = filename. local_path ( )
992
- && let Ok ( path) = local_path. strip_prefix ( & cur_dir)
993
- {
988
+ if let FileName :: Real ( ref filename) = filename {
989
+ let path = filename. remapped_path_if_available ( ) ;
990
+
991
+ // Strip the cwd prefix from the path. This will likely exist if
992
+ // the path was not remapped.
993
+ let path = env:: current_dir ( )
994
+ . map ( |cur_dir| path. strip_prefix ( & cur_dir) . unwrap_or ( path) )
995
+ . unwrap_or ( path) ;
996
+
994
997
return path. to_owned ( ) . into ( ) ;
995
998
}
996
999
filename
@@ -1021,20 +1024,13 @@ impl Tester for Collector {
1021
1024
}
1022
1025
1023
1026
let path = match & filename {
1024
- FileName :: Real ( path) => {
1025
- if let Some ( local_path) = path. local_path ( ) {
1026
- local_path. to_path_buf ( )
1027
- } else {
1028
- // Somehow we got the filename from the metadata of another crate, should never happen
1029
- unreachable ! ( "doctest from a different crate" ) ;
1030
- }
1031
- }
1027
+ FileName :: Real ( path) => path. remapped_path_if_available ( ) . to_path_buf ( ) ,
1032
1028
_ => PathBuf :: from ( r"doctest.rs" ) ,
1033
1029
} ;
1034
1030
1035
1031
// For example `module/file.rs` would become `module_file_rs`
1036
1032
let file = filename
1037
- . prefer_local ( )
1033
+ . prefer_remapped_unconditionaly ( )
1038
1034
. to_string_lossy ( )
1039
1035
. chars ( )
1040
1036
. map ( |c| if c. is_ascii_alphanumeric ( ) { c } else { '_' } )
0 commit comments