@@ -1650,56 +1650,63 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
16501650 ) ;
16511651
16521652 for virtual_dir in virtual_rust_source_base_dir. iter ( ) . flatten ( ) {
1653- if let Some ( real_dir) = & sess. opts . real_rust_source_base_dir {
1654- if let rustc_span:: FileName :: Real ( old_name) = name {
1655- if let rustc_span:: RealFileName :: Remapped { local_path : _, virtual_name } =
1656- old_name
1657- {
1658- if let Ok ( rest) = virtual_name. strip_prefix ( virtual_dir) {
1659- let virtual_name = virtual_name. clone ( ) ;
1660-
1661- // The std library crates are in
1662- // `$sysroot/lib/rustlib/src/rust/library`, whereas other crates
1663- // may be in `$sysroot/lib/rustlib/src/rust/` directly. So we
1664- // detect crates from the std libs and handle them specially.
1665- const STD_LIBS : & [ & str ] = & [
1666- "core" ,
1667- "alloc" ,
1668- "std" ,
1669- "test" ,
1670- "term" ,
1671- "unwind" ,
1672- "proc_macro" ,
1673- "panic_abort" ,
1674- "panic_unwind" ,
1675- "profiler_builtins" ,
1676- "rtstartup" ,
1677- "rustc-std-workspace-core" ,
1678- "rustc-std-workspace-alloc" ,
1679- "rustc-std-workspace-std" ,
1680- "backtrace" ,
1681- ] ;
1682- let is_std_lib = STD_LIBS . iter ( ) . any ( |l| rest. starts_with ( l) ) ;
1683-
1684- let new_path = if is_std_lib {
1685- real_dir. join ( "library" ) . join ( rest)
1686- } else {
1687- real_dir. join ( rest)
1688- } ;
1689-
1690- debug ! (
1691- "try_to_translate_virtual_to_real: `{}` -> `{}`" ,
1692- virtual_name. display( ) ,
1693- new_path. display( ) ,
1694- ) ;
1695- let new_name = rustc_span:: RealFileName :: Remapped {
1696- local_path : Some ( new_path) ,
1697- virtual_name,
1698- } ;
1699- * old_name = new_name;
1700- }
1653+ if let Some ( real_dir) = & sess. opts . real_rust_source_base_dir
1654+ && let rustc_span:: FileName :: Real ( old_name) = name
1655+ && let rustc_span:: RealFileName :: Remapped { local_path : _, virtual_name } =
1656+ old_name
1657+ && let Ok ( rest) = virtual_name. strip_prefix ( virtual_dir)
1658+ {
1659+ // The std library crates are in
1660+ // `$sysroot/lib/rustlib/src/rust/library`, whereas other crates
1661+ // may be in `$sysroot/lib/rustlib/src/rust/` directly. So we
1662+ // detect crates from the std libs and handle them specially.
1663+ const STD_LIBS : & [ & str ] = & [
1664+ "core" ,
1665+ "alloc" ,
1666+ "std" ,
1667+ "test" ,
1668+ "term" ,
1669+ "unwind" ,
1670+ "proc_macro" ,
1671+ "panic_abort" ,
1672+ "panic_unwind" ,
1673+ "profiler_builtins" ,
1674+ "rtstartup" ,
1675+ "rustc-std-workspace-core" ,
1676+ "rustc-std-workspace-alloc" ,
1677+ "rustc-std-workspace-std" ,
1678+ "backtrace" ,
1679+ ] ;
1680+ let is_std_lib = STD_LIBS . iter ( ) . any ( |l| rest. starts_with ( l) ) ;
1681+
1682+ let new_path = if is_std_lib {
1683+ real_dir. join ( "library" ) . join ( rest)
1684+ } else {
1685+ real_dir. join ( rest)
1686+ } ;
1687+
1688+ debug ! (
1689+ "try_to_translate_virtual_to_real: `{}` -> `{}`" ,
1690+ virtual_name. display( ) ,
1691+ new_path. display( ) ,
1692+ ) ;
1693+
1694+ // Check if the translated real path is affected by any user-requested
1695+ // remaps via --remap-path-prefix. Apply them if so.
1696+ // Note that this is a special case for imported rust-src paths specified by
1697+ // https://rust-lang.github.io/rfcs/3127-trim-paths.html#handling-sysroot-paths.
1698+ // Other imported paths are not currently remapped (see #66251).
1699+ let ( user_remapped, applied) =
1700+ sess. source_map ( ) . path_mapping ( ) . map_prefix ( & new_path) ;
1701+ let new_name = if applied {
1702+ rustc_span:: RealFileName :: Remapped {
1703+ local_path : Some ( new_path. clone ( ) ) ,
1704+ virtual_name : user_remapped. to_path_buf ( ) ,
17011705 }
1702- }
1706+ } else {
1707+ rustc_span:: RealFileName :: LocalPath ( new_path)
1708+ } ;
1709+ * old_name = new_name;
17031710 }
17041711 }
17051712 } ;
0 commit comments