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-28 02:47:46 +0000
committerbors <bors@rust-lang.org>2024-03-28 02:47:46 +0000
commit463a11bef4d6378439afebf2a9543aef36ccf2c1 (patch)
tree067185805e8b595771bec3529730b98d5a676a49 /compiler/rustc_span/src
parentd779a7a25f67fced5f8fea232ef407c5b228a22f (diff)
parent826ddb30180373a1972cdf5787044d12f8ad868e (diff)
downloadrust-463a11bef4d6378439afebf2a9543aef36ccf2c1.tar.gz
rust-463a11bef4d6378439afebf2a9543aef36ccf2c1.zip
Auto merge of #121833 - kornelski:parent_include, r=estebank
Suggest correct path in include_bytes!

`include_bytes!` paths are relative, and I'm often not sure how nested is the `.rs` file that I'm editing, so I have to guess the number of `"../.."`. This change searches `..` and `../..` for the given file and offers corrected path as a suggestion.

I wasn't sure how to get the right span, and how to properly escape it.

```text
error: couldn't read src/file.txt: No such file or directory (os error 2)
 --> src/main.rs:2:13
  |
2 |     let x = include_bytes!("file.txt");
  |             ^^^^^^^^^^^^^^^----------^
  |                            |
  |                            help: it's in a parent directory: `"../../file.txt"`
```
Diffstat (limited to 'compiler/rustc_span/src')
-rw-r--r--compiler/rustc_span/src/lib.rs11
1 files changed, 11 insertions, 0 deletions
diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs
index 616a7ccc7c6..0c974ef4ca3 100644
--- a/compiler/rustc_span/src/lib.rs
+++ b/compiler/rustc_span/src/lib.rs
@@ -427,6 +427,17 @@ impl FileName {
         src.hash(&mut hasher);
         FileName::InlineAsm(hasher.finish())
     }
+
+    /// 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 `remapped_path_if_available()` for that.
+    pub fn into_local_path(self) -> Option<PathBuf> {
+        match self {
+            FileName::Real(path) => path.into_local_path(),
+            FileName::DocTest(path, _) => Some(path),
+            _ => None,
+        }
+    }
 }
 
 /// Represents a span.