From 5e5a3d58678d70ad49b87251a12d11dea52e69b0 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 b47a166feb5..9964c9c94c9 100644 --- a/src/librustc_metadata/rmeta/encoder.rs +++ b/src/librustc_metadata/rmeta/encoder.rs @@ -396,7 +396,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 f18a863d720..e17fe1fa782 100644 --- a/src/librustc_span/lib.rs +++ b/src/librustc_span/lib.rs @@ -99,6 +99,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) @@ -107,12 +108,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 c432de3e847..c33c1dd29cb 100644 --- a/src/librustc_span/source_map.rs +++ b/src/librustc_span/source_map.rs @@ -86,6 +86,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( @@ -102,7 +104,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);