Skip to content

Commit 5e5a3d5

Browse files
committed
Use the virtual name for libstd files in StableSourceFileId and also in the
encoded build artifacts. Fix #70924.
1 parent da09fd3 commit 5e5a3d5

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
@@ -396,7 +396,7 @@ impl<'tcx> EncodeContext<'tcx> {
396396
// any relative paths are potentially relative to a
397397
// wrong directory.
398398
FileName::Real(ref name) => {
399-
let name = name.local_path();
399+
let name = name.stable_name();
400400
let mut adapted = (**source_file).clone();
401401
adapted.name = Path::new(&working_dir).join(name).into();
402402
adapted.name_hash = {

src/librustc_span/lib.rs

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

100100
impl RealFileName {
101101
/// Returns the path suitable for reading from the file system on the local host.
102+
/// Avoid embedding this in build artifacts; see `stable_name` for that.
102103
pub fn local_path(&self) -> &Path {
103104
match self {
104105
RealFileName::Named(p)
@@ -107,12 +108,24 @@ impl RealFileName {
107108
}
108109

109110
/// Returns the path suitable for reading from the file system on the local host.
111+
/// Avoid embedding this in build artifacts; see `stable_name` for that.
110112
pub fn into_local_path(self) -> PathBuf {
111113
match self {
112114
RealFileName::Named(p)
113115
| RealFileName::Devirtualized { local_path: p, virtual_name: _ } => p,
114116
}
115117
}
118+
119+
/// Returns the path suitable for embedding into build artifacts. Note that
120+
/// a virtualized path will not correspond to a valid file system path; see
121+
/// `local_path` for something that is more likely to return paths into the
122+
/// local host file system.
123+
pub fn stable_name(&self) -> &Path {
124+
match self {
125+
RealFileName::Named(p)
126+
| RealFileName::Devirtualized { local_path: _, virtual_name: p } => &p,
127+
}
128+
}
116129
}
117130

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

src/librustc_span/source_map.rs

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

89+
// FIXME: we need a more globally consistent approach to the problem solved by
90+
// StableSourceFileId, perhaps built atop source_file.name_hash.
8991
impl StableSourceFileId {
9092
pub fn new(source_file: &SourceFile) -> StableSourceFileId {
9193
StableSourceFileId::new_from_pieces(
@@ -102,7 +104,14 @@ impl StableSourceFileId {
102104
) -> StableSourceFileId {
103105
let mut hasher = StableHasher::new();
104106

105-
name.hash(&mut hasher);
107+
if let FileName::Real(real_name) = name {
108+
// rust-lang/rust#70924: Use the stable (virtualized) name when
109+
// available. (We do not want artifacts from transient file system
110+
// paths for libstd to leak into our build artifacts.)
111+
real_name.stable_name().hash(&mut hasher)
112+
} else {
113+
name.hash(&mut hasher);
114+
}
106115
name_was_remapped.hash(&mut hasher);
107116
unmapped_path.hash(&mut hasher);
108117

0 commit comments

Comments
 (0)