Skip to content

Load include_bytes! directly into an Lrc #115296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/source_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ pub fn expand_include_bytes(
};
match cx.source_map().load_binary_file(&file) {
Ok(bytes) => {
let expr = cx.expr(sp, ast::ExprKind::IncludedBytes(bytes.into()));
let expr = cx.expr(sp, ast::ExprKind::IncludedBytes(bytes));
base::MacEager::expr(expr)
}
Err(e) => {
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#![feature(rustc_attrs)]
#![feature(let_chains)]
#![feature(round_char_boundary)]
#![feature(read_buf)]
#![feature(new_uninit)]
#![deny(rustc::untranslatable_diagnostic)]
#![deny(rustc::diagnostic_outside_of_impl)]
#![allow(internal_features)]
Expand Down
21 changes: 17 additions & 4 deletions compiler/rustc_span/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ use std::sync::atomic::Ordering;

use std::fs;
use std::io;
use std::io::BorrowedBuf;
use std::io::Read;

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -101,10 +103,13 @@ pub trait FileLoader {
fn file_exists(&self, path: &Path) -> bool;

/// Read the contents of a UTF-8 file into memory.
/// This function must return a String because we normalize
/// source files, which may require resizing.
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>>;
/// We don't normalize binary files, so we can start in an Lrc.
fn read_binary_file(&self, path: &Path) -> io::Result<Lrc<[u8]>>;
}

/// A FileLoader that uses std::fs to load real files.
Expand All @@ -119,8 +124,16 @@ impl FileLoader for RealFileLoader {
fs::read_to_string(path)
}

fn read_binary_file(&self, path: &Path) -> io::Result<Vec<u8>> {
fs::read(path)
fn read_binary_file(&self, path: &Path) -> io::Result<Lrc<[u8]>> {
let mut file = fs::File::open(path)?;
let len = file.metadata()?.len();

let mut bytes = Lrc::new_uninit_slice(len as usize);
let mut buf = BorrowedBuf::from(Lrc::get_mut(&mut bytes).unwrap());
file.read_buf_exact(buf.unfilled())?;
// SAFETY: If the read_buf_exact call returns Ok(()), then we have
// read len bytes and initialized the buffer.
Ok(unsafe { bytes.assume_init() })
}
}

Expand Down Expand Up @@ -228,7 +241,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>> {
pub fn load_binary_file(&self, path: &Path) -> io::Result<Lrc<[u8]>> {
let bytes = self.file_loader.read_binary_file(path)?;

// We need to add file to the `SourceMap`, so that it is present
Expand Down