From 5684eaef5f9b78074a1d4d994ec8976401f5d1f6 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Fri, 29 May 2020 14:04:03 -0400 Subject: [PATCH] Use the virtual name for libstd files in StableSourceFileId and also in the encoded build artifacts. Fix #70924. --- src/librustc_metadata/rmeta/encoder.rs | 2 +- src/librustc_span/lib.rs | 13 +++++++++++++ src/librustc_span/source_map.rs | 11 ++++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/librustc_metadata/rmeta/encoder.rs b/src/librustc_metadata/rmeta/encoder.rs index 715cb52db1c42..b739882b7aa96 100644 --- a/src/librustc_metadata/rmeta/encoder.rs +++ b/src/librustc_metadata/rmeta/encoder.rs @@ -398,7 +398,7 @@ impl<'tcx> EncodeContext<'tcx> { // any relative paths are potentially relative to a // wrong directory. FileName::Real(ref name) => { - let name = name.local_path(); + let name = name.stable_name(); let mut adapted = (**source_file).clone(); adapted.name = Path::new(&working_dir).join(name).into(); adapted.name_hash = { diff --git a/src/librustc_span/lib.rs b/src/librustc_span/lib.rs index 7a12d7da31f6c..94a5acc86107f 100644 --- a/src/librustc_span/lib.rs +++ b/src/librustc_span/lib.rs @@ -95,6 +95,7 @@ pub enum RealFileName { impl RealFileName { /// Returns the path suitable for reading from the file system on the local host. + /// Avoid embedding this in build artifacts; see `stable_name` for that. pub fn local_path(&self) -> &Path { match self { RealFileName::Named(p) @@ -103,12 +104,24 @@ impl RealFileName { } /// Returns the path suitable for reading from the file system on the local host. + /// Avoid embedding this in build artifacts; see `stable_name` for that. pub fn into_local_path(self) -> PathBuf { match self { RealFileName::Named(p) | RealFileName::Devirtualized { local_path: p, virtual_name: _ } => p, } } + + /// Returns the path suitable for embedding into build artifacts. Note that + /// a virtualized path will not correspond to a valid file system path; see + /// `local_path` for something that is more likely to return paths into the + /// local host file system. + pub fn stable_name(&self) -> &Path { + match self { + RealFileName::Named(p) + | RealFileName::Devirtualized { local_path: _, virtual_name: p } => &p, + } + } } /// Differentiates between real files and common virtual files. diff --git a/src/librustc_span/source_map.rs b/src/librustc_span/source_map.rs index e2ae32f6a1b34..54d3dc2c8676e 100644 --- a/src/librustc_span/source_map.rs +++ b/src/librustc_span/source_map.rs @@ -98,6 +98,8 @@ impl FileLoader for RealFileLoader { #[derive(Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Debug)] pub struct StableSourceFileId(u128); +// FIXME: we need a more globally consistent approach to the problem solved by +// StableSourceFileId, perhaps built atop source_file.name_hash. impl StableSourceFileId { pub fn new(source_file: &SourceFile) -> StableSourceFileId { StableSourceFileId::new_from_pieces( @@ -114,7 +116,14 @@ impl StableSourceFileId { ) -> StableSourceFileId { let mut hasher = StableHasher::new(); - name.hash(&mut hasher); + if let FileName::Real(real_name) = name { + // rust-lang/rust#70924: Use the stable (virtualized) name when + // available. (We do not want artifacts from transient file system + // paths for libstd to leak into our build artifacts.) + real_name.stable_name().hash(&mut hasher) + } else { + name.hash(&mut hasher); + } name_was_remapped.hash(&mut hasher); unmapped_path.hash(&mut hasher);