about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAndy Wang <qian.wang19@imperial.ac.uk>2021-04-14 14:12:39 +0100
committerAndy Wang <cbeuw.andy@gmail.com>2021-05-05 15:31:03 +0100
commitfb4f6439f62d4b940bdfab3f78771d76eacab379 (patch)
treed228b5f9453949c70950391acf828a5bb7b2eb03
parentf8e55da6defaba1dff4d1e11f76dda3b366819be (diff)
downloadrust-fb4f6439f62d4b940bdfab3f78771d76eacab379.tar.gz
rust-fb4f6439f62d4b940bdfab3f78771d76eacab379.zip
Revamp RealFileName public methods
-rw-r--r--compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs2
-rw-r--r--compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs2
-rw-r--r--compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs4
-rw-r--r--compiler/rustc_metadata/src/rmeta/encoder.rs35
-rw-r--r--compiler/rustc_save_analysis/src/dump_visitor.rs2
-rw-r--r--compiler/rustc_save_analysis/src/span_utils.rs7
-rw-r--r--compiler/rustc_span/src/lib.rs42
7 files changed, 53 insertions, 41 deletions
diff --git a/compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs b/compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs
index 8578ab33ced..b3f700bc467 100644
--- a/compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs
+++ b/compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs
@@ -66,7 +66,7 @@ fn line_program_add_file(
 ) -> FileId {
     match &file.name {
         FileName::Real(path) => {
-            let (dir_path, file_name) = split_path_dir_and_file(path.stable_name());
+            let (dir_path, file_name) = split_path_dir_and_file(path.remapped_path_if_available());
             let dir_name = osstr_as_utf8_bytes(dir_path.as_os_str());
             let file_name = osstr_as_utf8_bytes(file_name);
 
diff --git a/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs b/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs
index 10d3651789b..61e54a76f29 100644
--- a/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs
+++ b/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs
@@ -64,7 +64,7 @@ impl<'tcx> DebugContext<'tcx> {
         // FIXME: how to get version when building out of tree?
         // Normally this would use option_env!("CFG_VERSION").
         let producer = format!("cg_clif (rustc {})", "unknown version");
-        let comp_dir = tcx.sess.working_dir.stable_name().to_string_lossy().into_owned();
+        let comp_dir = tcx.sess.working_dir.to_string_lossy(false).into_owned();
         let (name, file_info) = match tcx.sess.local_crate_source_file.clone() {
             Some(path) => {
                 let name = path.to_string_lossy().into_owned();
diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
index ccef9530850..6fad1996d7e 100644
--- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
+++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
@@ -764,7 +764,7 @@ pub fn file_metadata(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) -> &'ll
     let hash = Some(&source_file.src_hash);
     let file_name = Some(source_file.name.to_string());
     let directory = if source_file.is_real_file() && !source_file.is_imported() {
-        Some(cx.sess().working_dir.stable_name().to_string_lossy().to_string())
+        Some(cx.sess().working_dir.to_string_lossy(false).to_string())
     } else {
         // If the path comes from an upstream crate we assume it has been made
         // independent of the compiler's working directory one way or another.
@@ -992,7 +992,7 @@ pub fn compile_unit_metadata(
     let producer = format!("clang LLVM ({})", rustc_producer);
 
     let name_in_debuginfo = name_in_debuginfo.to_string_lossy();
-    let work_dir = tcx.sess.working_dir.stable_name().to_string_lossy();
+    let work_dir = tcx.sess.working_dir.to_string_lossy(false);
     let flags = "\0";
     let out_dir = &tcx.output_filenames(LOCAL_CRATE).out_directory;
     let split_name = if tcx.sess.target_can_use_split_dwarf() {
diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs
index a3e0dd8e3b9..fa62b664b9e 100644
--- a/compiler/rustc_metadata/src/rmeta/encoder.rs
+++ b/compiler/rustc_metadata/src/rmeta/encoder.rs
@@ -490,22 +490,27 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
                     FileName::Real(ref realname) => {
                         let mut adapted = (**source_file).clone();
                         adapted.name = FileName::Real(match realname {
-                            RealFileName::LocalPath(local_path) => {
-                                // Prepend path of working directory onto local path.
-                                // because relative paths are potentially relative to a
-                                // wrong directory.
+                            RealFileName::LocalPath(path_to_file) => {
+                                // Prepend path of working directory onto potentially
+                                // relative paths, because they could become relative
+                                // to a wrong directory.
                                 let working_dir = &self.tcx.sess.working_dir;
-                                if let RealFileName::LocalPath(absolute) = working_dir {
-                                    // If working_dir has not been remapped, then we emit a
-                                    // LocalPath variant as it's likely to be a valid path
-                                    RealFileName::LocalPath(Path::new(absolute).join(local_path))
-                                } else {
-                                    // If working_dir has been remapped, then we emit
-                                    // Remapped variant as the expanded path won't be valid
-                                    RealFileName::Remapped {
-                                        local_path: None,
-                                        virtual_name: Path::new(working_dir.stable_name())
-                                            .join(local_path),
+                                match working_dir {
+                                    RealFileName::LocalPath(absolute) => {
+                                        // If working_dir has not been remapped, then we emit a
+                                        // LocalPath variant as it's likely to be a valid path
+                                        RealFileName::LocalPath(
+                                            Path::new(absolute).join(path_to_file),
+                                        )
+                                    }
+                                    RealFileName::Remapped { local_path: _, virtual_name } => {
+                                        // If working_dir has been remapped, then we emit
+                                        // Remapped variant as the expanded path won't be valid
+                                        RealFileName::Remapped {
+                                            local_path: None,
+                                            virtual_name: Path::new(virtual_name)
+                                                .join(path_to_file),
+                                        }
                                     }
                                 }
                             }
diff --git a/compiler/rustc_save_analysis/src/dump_visitor.rs b/compiler/rustc_save_analysis/src/dump_visitor.rs
index ab1b4db6293..06b2e41daf5 100644
--- a/compiler/rustc_save_analysis/src/dump_visitor.rs
+++ b/compiler/rustc_save_analysis/src/dump_visitor.rs
@@ -190,7 +190,7 @@ impl<'tcx> DumpVisitor<'tcx> {
         };
 
         let data = CompilationOptions {
-            directory: self.tcx.sess.working_dir.stable_name().into(),
+            directory: self.tcx.sess.working_dir.remapped_path_if_available().into(),
             program,
             arguments,
             output: self.save_ctxt.compilation_output(crate_name),
diff --git a/compiler/rustc_save_analysis/src/span_utils.rs b/compiler/rustc_save_analysis/src/span_utils.rs
index bcda3a6d006..3c4d7972380 100644
--- a/compiler/rustc_save_analysis/src/span_utils.rs
+++ b/compiler/rustc_save_analysis/src/span_utils.rs
@@ -26,7 +26,12 @@ impl<'a> SpanUtils<'a> {
                         .display()
                         .to_string()
                 } else {
-                    self.sess.working_dir.stable_name().join(&path).display().to_string()
+                    self.sess
+                        .working_dir
+                        .remapped_path_if_available()
+                        .join(&path)
+                        .display()
+                        .to_string()
                 }
             }
             // If the file name was remapped, we assume the user
diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs
index 141ecfd0baf..787fe1d0f78 100644
--- a/compiler/rustc_span/src/lib.rs
+++ b/compiler/rustc_span/src/lib.rs
@@ -172,7 +172,7 @@ impl<S: Encoder> Encodable<S> for RealFileName {
 impl RealFileName {
     /// Returns the path suitable for reading from the file system on the local host,
     /// if this information exists.
-    /// Avoid embedding this in build artifacts; see `stable_name()` for that.
+    /// Avoid embedding this in build artifacts; see `remapped_path_if_available()` for that.
     pub fn local_path(&self) -> Option<&Path> {
         match self {
             RealFileName::LocalPath(p) => Some(p),
@@ -184,7 +184,7 @@ impl RealFileName {
 
     /// Returns the path suitable for reading from the file system on the local host,
     /// if this information exists.
-    /// Avoid embedding this in build artifacts; see `stable_name()` for that.
+    /// Avoid embedding this in build artifacts; see `remapped_path_if_available()` for that.
     pub fn into_local_path(self) -> Option<PathBuf> {
         match self {
             RealFileName::LocalPath(p) => Some(p),
@@ -192,31 +192,33 @@ impl RealFileName {
         }
     }
 
-    /// Returns the path suitable for embedding into build artifacts. Note that
-    /// a remapped 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 {
+    /// Returns the path suitable for embedding into build artifacts. This would still
+    /// be a local path if it has not been remapped. A remapped path will not correspond
+    /// to a valid file system path: see `local_path_if_available()` for something that
+    /// is more likely to return paths into the local host file system.
+    pub fn remapped_path_if_available(&self) -> &Path {
         match self {
             RealFileName::LocalPath(p)
             | RealFileName::Remapped { local_path: _, virtual_name: p } => &p,
         }
     }
 
-    fn to_string_lossy(&self, prefer_local: bool) -> Cow<'_, str> {
-        use RealFileName::*;
+    /// Returns the path suitable for reading from the file system on the local host,
+    /// if this information exists. Otherwise returns the remapped name.
+    /// Avoid embedding this in build artifacts; see `remapped_path_if_available()` for that.
+    pub fn local_path_if_available(&self) -> &Path {
+        match self {
+            RealFileName::LocalPath(path)
+            | RealFileName::Remapped { local_path: None, virtual_name: path }
+            | RealFileName::Remapped { local_path: Some(path), virtual_name: _ } => path,
+        }
+    }
+
+    pub fn to_string_lossy(&self, prefer_local: bool) -> Cow<'_, str> {
         if prefer_local {
-            match self {
-                LocalPath(path)
-                | Remapped { local_path: None, virtual_name: path }
-                | Remapped { local_path: Some(path), virtual_name: _ } => path.to_string_lossy(),
-            }
+            self.local_path_if_available().to_string_lossy()
         } else {
-            match self {
-                LocalPath(path) | Remapped { local_path: _, virtual_name: path } => {
-                    path.to_string_lossy()
-                }
-            }
+            self.remapped_path_if_available().to_string_lossy()
         }
     }
 }
@@ -1358,7 +1360,7 @@ impl<D: Decoder> Decodable<D> for SourceFile {
 
 impl fmt::Debug for SourceFile {
     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(fmt, "SourceFile({})", self.name)
+        write!(fmt, "SourceFile({:?})", self.name)
     }
 }