Skip to content

Commit b1a74bd

Browse files
committed
Apply --remap-path-prefix to imported sources
RealFileName::Devirtualized is renamed to Virtualized as it is also used to store remapped paths (in virtual_name) along with original path (in local_path)
1 parent 5fe2970 commit b1a74bd

File tree

3 files changed

+41
-45
lines changed

3 files changed

+41
-45
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1671,7 +1671,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
16711671
virtual_name.display(),
16721672
new_path.display(),
16731673
);
1674-
let new_name = rustc_span::RealFileName::Devirtualized {
1674+
let new_name = rustc_span::RealFileName::Virtualized {
16751675
local_path: new_path,
16761676
virtual_name,
16771677
};

compiler/rustc_span/src/lib.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,16 @@ pub fn with_default_session_globals<R>(f: impl FnOnce() -> R) -> R {
114114
// deserialization.
115115
scoped_tls::scoped_thread_local!(pub static SESSION_GLOBALS: SessionGlobals);
116116

117-
// FIXME: Perhaps this should not implement Rustc{Decodable, Encodable}
118-
//
119117
// FIXME: We should use this enum or something like it to get rid of the
120118
// use of magic `/rust/1.x/...` paths across the board.
121119
#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Hash)]
122120
#[derive(HashStable_Generic, Decodable, Encodable)]
123121
pub enum RealFileName {
124122
Named(PathBuf),
125-
/// For de-virtualized paths (namely paths into libstd that have been mapped
126-
/// to the appropriate spot on the local host's file system),
127-
Devirtualized {
123+
/// For virtualized paths (namely paths into libstd that have been mapped
124+
/// to the appropriate spot on the local host's file system, and local file
125+
/// system paths that have been remapped with `FilePathMapping`),
126+
Virtualized {
128127
/// `local_path` is the (host-dependent) local path to the file.
129128
local_path: PathBuf,
130129
/// `virtual_name` is the stable path rustc will store internally within
@@ -139,7 +138,7 @@ impl RealFileName {
139138
pub fn local_path(&self) -> &Path {
140139
match self {
141140
RealFileName::Named(p)
142-
| RealFileName::Devirtualized { local_path: p, virtual_name: _ } => &p,
141+
| RealFileName::Virtualized { local_path: p, virtual_name: _ } => &p,
143142
}
144143
}
145144

@@ -148,7 +147,7 @@ impl RealFileName {
148147
pub fn into_local_path(self) -> PathBuf {
149148
match self {
150149
RealFileName::Named(p)
151-
| RealFileName::Devirtualized { local_path: p, virtual_name: _ } => p,
150+
| RealFileName::Virtualized { local_path: p, virtual_name: _ } => p,
152151
}
153152
}
154153

@@ -159,7 +158,7 @@ impl RealFileName {
159158
pub fn stable_name(&self) -> &Path {
160159
match self {
161160
RealFileName::Named(p)
162-
| RealFileName::Devirtualized { local_path: _, virtual_name: p } => &p,
161+
| RealFileName::Virtualized { local_path: _, virtual_name: p } => &p,
163162
}
164163
}
165164
}
@@ -192,13 +191,7 @@ impl std::fmt::Display for FileName {
192191
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
193192
use FileName::*;
194193
match *self {
195-
Real(RealFileName::Named(ref path)) => write!(fmt, "{}", path.display()),
196-
// FIXME: might be nice to display both components of Devirtualized.
197-
// But for now (to backport fix for issue #70924), best to not
198-
// perturb diagnostics so its obvious test suite still works.
199-
Real(RealFileName::Devirtualized { ref local_path, virtual_name: _ }) => {
200-
write!(fmt, "{}", local_path.display())
201-
}
194+
Real(ref name) => write!(fmt, "{}", name.stable_name().display()),
202195
QuoteExpansion(_) => write!(fmt, "<quote expansion>"),
203196
MacroExpansion(_) => write!(fmt, "<macro expansion>"),
204197
Anon(_) => write!(fmt, "<anon>"),
@@ -1129,6 +1122,7 @@ pub struct SourceFile {
11291122
pub name_was_remapped: bool,
11301123
/// The unmapped path of the file that the source came from.
11311124
/// Set to `None` if the `SourceFile` was imported from an external crate.
1125+
/// as it is not encoded.
11321126
pub unmapped_path: Option<FileName>,
11331127
/// The complete source code.
11341128
pub src: Option<Lrc<String>>,

compiler/rustc_span/src/source_map.rs

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ pub use crate::*;
1515
use rustc_data_structures::fx::FxHashMap;
1616
use rustc_data_structures::stable_hasher::StableHasher;
1717
use rustc_data_structures::sync::{AtomicU32, Lrc, MappedReadGuard, ReadGuard, RwLock};
18-
use std::cmp;
1918
use std::convert::TryFrom;
2019
use std::hash::Hash;
2120
use std::path::{Path, PathBuf};
2221
use std::sync::atomic::Ordering;
22+
use std::{clone::Clone, cmp};
2323

2424
use std::fs;
2525
use std::io;
@@ -283,45 +283,28 @@ impl SourceMap {
283283

284284
fn try_new_source_file(
285285
&self,
286-
mut filename: FileName,
286+
filename: FileName,
287287
src: String,
288288
) -> Result<Lrc<SourceFile>, OffsetOverflowError> {
289-
// The path is used to determine the directory for loading submodules and
290-
// include files, so it must be before remapping.
289+
// We need to preserve the unmapped path is as it is
290+
// used to determine the directory for loading submodules and include files.
291291
// Note that filename may not be a valid path, eg it may be `<anon>` etc,
292292
// but this is okay because the directory determined by `path.pop()` will
293293
// be empty, so the working directory will be used.
294-
let unmapped_path = filename.clone();
295-
296-
let was_remapped;
297-
if let FileName::Real(real_filename) = &mut filename {
298-
match real_filename {
299-
RealFileName::Named(path_to_be_remapped)
300-
| RealFileName::Devirtualized {
301-
local_path: path_to_be_remapped,
302-
virtual_name: _,
303-
} => {
304-
let mapped = self.path_mapping.map_prefix(path_to_be_remapped.clone());
305-
was_remapped = mapped.1;
306-
*path_to_be_remapped = mapped.0;
307-
}
308-
}
309-
} else {
310-
was_remapped = false;
311-
}
294+
let (mapped_filename, was_remapped) = self.path_mapping.map_filename_prefix(&filename);
312295

313296
let file_id =
314-
StableSourceFileId::new_from_pieces(&filename, was_remapped, Some(&unmapped_path));
297+
StableSourceFileId::new_from_pieces(&mapped_filename, was_remapped, Some(&filename));
315298

316299
let lrc_sf = match self.source_file_by_stable_id(file_id) {
317300
Some(lrc_sf) => lrc_sf,
318301
None => {
319302
let start_pos = self.allocate_address_space(src.len())?;
320303

321304
let source_file = Lrc::new(SourceFile::new(
322-
filename,
305+
mapped_filename,
323306
was_remapped,
324-
unmapped_path,
307+
filename,
325308
src,
326309
Pos::from_usize(start_pos),
327310
self.hash_kind,
@@ -380,9 +363,11 @@ impl SourceMap {
380363
nc.pos = nc.pos + start_pos;
381364
}
382365

366+
let (filename, name_is_remapped) = self.path_mapping.map_filename_prefix(&filename);
367+
383368
let source_file = Lrc::new(SourceFile {
384369
name: filename,
385-
name_was_remapped,
370+
name_was_remapped: name_was_remapped || name_is_remapped,
386371
unmapped_path: None,
387372
src: None,
388373
src_hash,
@@ -1046,9 +1031,26 @@ impl FilePathMapping {
10461031
fn map_filename_prefix(&self, file: &FileName) -> (FileName, bool) {
10471032
match file {
10481033
FileName::Real(realfile) => {
1049-
let path = realfile.local_path();
1050-
let (path, mapped) = self.map_prefix(path.to_path_buf());
1051-
(FileName::Real(RealFileName::Named(path)), mapped)
1034+
// If the file is the Name variant with only local_path, then clearly we want to map that
1035+
// to a virtual_name
1036+
// If the file is already Virtualized, then we want to map virtual_name further
1037+
// but we leave local_path alone
1038+
let path = realfile.stable_name();
1039+
let (mapped_path, mapped) = self.map_prefix(path.to_path_buf());
1040+
if mapped {
1041+
let mapped_realfile = match realfile {
1042+
RealFileName::Named(local_path)
1043+
| RealFileName::Virtualized { local_path, virtual_name: _ } => {
1044+
RealFileName::Virtualized {
1045+
local_path: local_path.clone(),
1046+
virtual_name: mapped_path,
1047+
}
1048+
}
1049+
};
1050+
(FileName::Real(mapped_realfile), mapped)
1051+
} else {
1052+
(file.clone(), false)
1053+
}
10521054
}
10531055
other => (other.clone(), false),
10541056
}

0 commit comments

Comments
 (0)