Skip to content

Commit 0d19fb3

Browse files
committed
Use the virtual name for libstd files in StableSourceFileId and also in the
encoded build artifacts. Fix #70924.
1 parent d0db71a commit 0d19fb3

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/librustc_metadata/rmeta/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ impl<'tcx> EncodeContext<'tcx> {
398398
// any relative paths are potentially relative to a
399399
// wrong directory.
400400
FileName::Real(ref name) => {
401-
let name = name.local_path();
401+
let name = name.stable_name();
402402
let mut adapted = (**source_file).clone();
403403
adapted.name = Path::new(&working_dir).join(name).into();
404404
adapted.name_hash = {

src/librustc_span/lib.rs

+13
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ pub enum RealFileName {
9595

9696
impl RealFileName {
9797
/// Returns the path suitable for reading from the file system on the local host.
98+
/// Avoid embedding this in build artifacts; see `stable_name` for that.
9899
pub fn local_path(&self) -> &Path {
99100
match self {
100101
RealFileName::Named(p)
@@ -103,12 +104,24 @@ impl RealFileName {
103104
}
104105

105106
/// Returns the path suitable for reading from the file system on the local host.
107+
/// Avoid embedding this in build artifacts; see `stable_name` for that.
106108
pub fn into_local_path(self) -> PathBuf {
107109
match self {
108110
RealFileName::Named(p)
109111
| RealFileName::Devirtualized { local_path: p, virtual_name: _ } => p,
110112
}
111113
}
114+
115+
/// Returns the path suitable for embedding into build artifacts. Note that
116+
/// a virtualized path will not correspond to a valid file system path; see
117+
/// `local_path` for something that is more likely to return paths into the
118+
/// local host file system.
119+
pub fn stable_name(&self) -> &Path {
120+
match self {
121+
RealFileName::Named(p)
122+
| RealFileName::Devirtualized { local_path: _, virtual_name: p } => &p,
123+
}
124+
}
112125
}
113126

114127
/// Differentiates between real files and common virtual files.

src/librustc_span/source_map.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ impl FileLoader for RealFileLoader {
9898
#[derive(Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Debug)]
9999
pub struct StableSourceFileId(u128);
100100

101+
// FIXME: we need a more globally consistent approach to the problem solved by
102+
// StableSourceFileId, perhaps built atop source_file.name_hash.
101103
impl StableSourceFileId {
102104
pub fn new(source_file: &SourceFile) -> StableSourceFileId {
103105
StableSourceFileId::new_from_pieces(
@@ -114,7 +116,14 @@ impl StableSourceFileId {
114116
) -> StableSourceFileId {
115117
let mut hasher = StableHasher::new();
116118

117-
name.hash(&mut hasher);
119+
if let FileName::Real(real_name) = name {
120+
// rust-lang/rust#70924: Use the stable (virtualized) name when
121+
// available. (We do not want artifacts from transient file system
122+
// paths for libstd to leak into our build artifacts.)
123+
real_name.stable_name().hash(&mut hasher)
124+
} else {
125+
name.hash(&mut hasher);
126+
}
118127
name_was_remapped.hash(&mut hasher);
119128
unmapped_path.hash(&mut hasher);
120129

0 commit comments

Comments
 (0)