Skip to content

Commit b16ed69

Browse files
authored
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.
2 parents 9668ae5 + 63396b3 commit b16ed69

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

compiler/rustc_span/src/source_map.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ pub trait FileLoader {
100100

101101
/// Read the contents of a UTF-8 file into memory.
102102
fn read_file(&self, path: &Path) -> io::Result<String>;
103+
104+
/// Read the contents of a potentially non-UTF-8 file into memory.
105+
fn read_binary_file(&self, path: &Path) -> io::Result<Vec<u8>>;
103106
}
104107

105108
/// A FileLoader that uses std::fs to load real files.
@@ -113,6 +116,10 @@ impl FileLoader for RealFileLoader {
113116
fn read_file(&self, path: &Path) -> io::Result<String> {
114117
fs::read_to_string(path)
115118
}
119+
120+
fn read_binary_file(&self, path: &Path) -> io::Result<Vec<u8>> {
121+
fs::read(path)
122+
}
116123
}
117124

118125
/// This is a [SourceFile] identifier that is used to correlate source files between
@@ -220,9 +227,7 @@ impl SourceMap {
220227
/// Unlike `load_file`, guarantees that no normalization like BOM-removal
221228
/// takes place.
222229
pub fn load_binary_file(&self, path: &Path) -> io::Result<Vec<u8>> {
223-
// Ideally, this should use `self.file_loader`, but it can't
224-
// deal with binary files yet.
225-
let bytes = fs::read(path)?;
230+
let bytes = self.file_loader.read_binary_file(path)?;
226231

227232
// We need to add file to the `SourceMap`, so that it is present
228233
// in dep-info. There's also an edge case that file might be both

0 commit comments

Comments
 (0)