about summary refs log tree commit diff
path: root/compiler/rustc_span/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-03-29 14:00:21 +0000
committerbors <bors@rust-lang.org>2024-03-29 14:00:21 +0000
commit685927aae69657b46323cffbeb0062835bd7fa2b (patch)
tree48a5a2694795c9a904f17f9cc0b2553bccb0ab95 /compiler/rustc_span/src
parent45796d1c24445b298567752519471cef2cff3298 (diff)
parentfefb8f1f9cb830e1ca0b0ba449db7c8e7d4ff7ba (diff)
downloadrust-685927aae69657b46323cffbeb0062835bd7fa2b.tar.gz
rust-685927aae69657b46323cffbeb0062835bd7fa2b.zip
Auto merge of #122450 - Urgau:simplify-trim-paths-feature, r=michaelwoerister
Simplify trim-paths feature by merging all debuginfo options together

This PR simplifies the trim-paths feature by merging all debuginfo options together, as described in https://github.com/rust-lang/rust/issues/111540#issuecomment-1994010274.

And also do some correctness fixes found during the review.

cc `@weihanglo`
r? `@michaelwoerister`
Diffstat (limited to 'compiler/rustc_span/src')
-rw-r--r--compiler/rustc_span/src/lib.rs12
-rw-r--r--compiler/rustc_span/src/source_map.rs15
2 files changed, 27 insertions, 0 deletions
diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs
index 0c974ef4ca3..7ce879807ca 100644
--- a/compiler/rustc_span/src/lib.rs
+++ b/compiler/rustc_span/src/lib.rs
@@ -271,6 +271,18 @@ impl RealFileName {
         }
     }
 
+    /// Return the path remmapped or not depending on the [`FileNameDisplayPreference`].
+    ///
+    /// For the purpose of this function, local and short preference are equal.
+    pub fn to_path(&self, display_pref: FileNameDisplayPreference) -> &Path {
+        match display_pref {
+            FileNameDisplayPreference::Local | FileNameDisplayPreference::Short => {
+                self.local_path_if_available()
+            }
+            FileNameDisplayPreference::Remapped => self.remapped_path_if_available(),
+        }
+    }
+
     pub fn to_string_lossy(&self, display_pref: FileNameDisplayPreference) -> Cow<'_, str> {
         match display_pref {
             FileNameDisplayPreference::Local => self.local_path_if_available().to_string_lossy(),
diff --git a/compiler/rustc_span/src/source_map.rs b/compiler/rustc_span/src/source_map.rs
index df7635e447d..770624d331d 100644
--- a/compiler/rustc_span/src/source_map.rs
+++ b/compiler/rustc_span/src/source_map.rs
@@ -1129,6 +1129,21 @@ impl FilePathMapping {
         }
     }
 
+    /// Applies any path prefix substitution as defined by the mapping.
+    /// The return value is the local path with a "virtual path" representing the remapped
+    /// part if any remapping was performed.
+    pub fn to_real_filename<'a>(&self, local_path: impl Into<Cow<'a, Path>>) -> RealFileName {
+        let local_path = local_path.into();
+        if let (remapped_path, true) = self.map_prefix(&*local_path) {
+            RealFileName::Remapped {
+                virtual_name: remapped_path.into_owned(),
+                local_path: Some(local_path.into_owned()),
+            }
+        } else {
+            RealFileName::LocalPath(local_path.into_owned())
+        }
+    }
+
     /// Expand a relative path to an absolute path with remapping taken into account.
     /// Use this when absolute paths are required (e.g. debuginfo or crate metadata).
     ///