about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2023-03-12 08:13:26 +0100
committerGitHub <noreply@github.com>2023-03-12 08:13:26 +0100
commitb16ed694f7967d1381da5b43085dfbce2f1de723 (patch)
treea66e3c0b83c47e661cbacf72499023dc260def18
parent9668ae5eb8b169557a12d85e4088274780ef7d48 (diff)
parent63396b30cf6e4f9148c7af8a21727b4d2109b632 (diff)
downloadrust-b16ed694f7967d1381da5b43085dfbce2f1de723.tar.gz
rust-b16ed694f7967d1381da5b43085dfbce2f1de723.zip
Rollup merge of #108797 - thomcc:sourcemap_include_binary_file, r=compiler-errors
Allow binary files to go through the `FileLoader`

I'd like for `include_bytes!` to go through the `FileLoader` in an out-of-tree `rustc_driver` wrapper, and I can't find a reason it's not already done. It seems like most folks providing a custom `FileLoader` would want this too, so I added it.

I can solve my problem in other ways if there's a strong reason not to do it, but it seems simple and harmless.
-rw-r--r--compiler/rustc_span/src/source_map.rs11
1 files changed, 8 insertions, 3 deletions
diff --git a/compiler/rustc_span/src/source_map.rs b/compiler/rustc_span/src/source_map.rs
index 2e339a9d2d2..a1cb810a429 100644
--- a/compiler/rustc_span/src/source_map.rs
+++ b/compiler/rustc_span/src/source_map.rs
@@ -100,6 +100,9 @@ pub trait FileLoader {
 
     /// Read the contents of a UTF-8 file into memory.
     fn read_file(&self, path: &Path) -> io::Result<String>;
+
+    /// Read the contents of a potentially non-UTF-8 file into memory.
+    fn read_binary_file(&self, path: &Path) -> io::Result<Vec<u8>>;
 }
 
 /// A FileLoader that uses std::fs to load real files.
@@ -113,6 +116,10 @@ impl FileLoader for RealFileLoader {
     fn read_file(&self, path: &Path) -> io::Result<String> {
         fs::read_to_string(path)
     }
+
+    fn read_binary_file(&self, path: &Path) -> io::Result<Vec<u8>> {
+        fs::read(path)
+    }
 }
 
 /// This is a [SourceFile] identifier that is used to correlate source files between
@@ -220,9 +227,7 @@ impl SourceMap {
     /// Unlike `load_file`, guarantees that no normalization like BOM-removal
     /// takes place.
     pub fn load_binary_file(&self, path: &Path) -> io::Result<Vec<u8>> {
-        // Ideally, this should use `self.file_loader`, but it can't
-        // deal with binary files yet.
-        let bytes = fs::read(path)?;
+        let bytes = self.file_loader.read_binary_file(path)?;
 
         // We need to add file to the `SourceMap`, so that it is present
         // in dep-info. There's also an edge case that file might be both