Skip to content

Commit a97cec0

Browse files
committed
Get basic (de)serialization working
1 parent ec5d302 commit a97cec0

File tree

2 files changed

+11
-43
lines changed

2 files changed

+11
-43
lines changed

src/rustc/src/analyzer/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ mod sym;
2323
mod template;
2424
mod trace;
2525

26-
// TODO: Double-check the short-circuits in the analysis routine to make sure we're not ignoring
27-
// important items.
2826
pub fn analyze(tcx: TyCtxt<'_>) {
2927
// Fetch the MIR for each local definition to populate the `MirBuiltStasher`.
3028
for local_def in iter_all_local_def_ids(tcx) {
@@ -34,8 +32,6 @@ pub fn analyze(tcx: TyCtxt<'_>) {
3432
}
3533

3634
// Generate borrow-checking templates for each local function.
37-
//
38-
// TODO: Serialize these across crates.
3935
assert!(!tcx.untracked().definitions.is_frozen());
4036

4137
let mut templates = FxHashMap::default();

src/rustc/src/util/meta.rs

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use rustc_incremental::in_incr_comp_dir_sess;
1+
use std::path::Path;
2+
23
use rustc_middle::ty::TyCtxt;
34
use rustc_serialize::{Decodable, Encodable};
45

@@ -7,25 +8,21 @@ pub use self::adapted_encoding::{CacheDecoder, CacheEncoder};
78
// Adapted from: compiler/rustc_incremental/src/persist/save.rs:27
89
pub fn save_to_file<'tcx>(
910
tcx: TyCtxt<'tcx>,
10-
file_name: &str,
1111
name: &str,
12+
path: &Path,
1213
item: &impl for<'a> Encodable<CacheEncoder<'a, 'tcx>>,
1314
) {
14-
let path = in_incr_comp_dir_sess(tcx.sess, file_name);
15-
16-
adapted_io::save_in(tcx.sess, path, name, |e| {
15+
adapted_io::save_in(tcx.sess, path.to_path_buf(), name, |e| {
1716
adapted_encoding::save_to_encoder(tcx, e, item)
1817
});
1918
}
2019

2120
// Adapted from: compiler/rustc_incremental/src/persist/load.rs:176
22-
pub fn try_load_from_file<'tcx, D>(tcx: TyCtxt<'tcx>, file_name: &str, name: &str) -> Option<D>
21+
pub fn try_load_from_file<'tcx, D>(tcx: TyCtxt<'tcx>, name: &str, path: &Path) -> Option<D>
2322
where
2423
D: for<'a> Decodable<CacheDecoder<'a, 'tcx>>,
2524
{
26-
let path = in_incr_comp_dir_sess(tcx.sess, file_name);
27-
28-
match adapted_io::read_file(&path) {
25+
match adapted_io::read_file(path) {
2926
Ok(Some((mmap, start_pos))) => {
3027
Some(adapted_encoding::load_from_decoder(tcx, mmap, start_pos))
3128
}
@@ -54,19 +51,7 @@ mod adapted_io {
5451
};
5552
use rustc_session::Session;
5653

57-
/// The first few bytes of files generated by incremental compilation.
58-
const FILE_MAGIC: &[u8] = b"AUTOKEN-v1";
59-
60-
/// Change this if the header format changes.
61-
const HEADER_FORMAT_VERSION: u16 = 0;
62-
63-
fn write_file_header(stream: &mut FileEncoder) {
64-
stream.emit_raw_bytes(FILE_MAGIC);
65-
stream.emit_raw_bytes(&[
66-
(HEADER_FORMAT_VERSION) as u8,
67-
(HEADER_FORMAT_VERSION >> 8) as u8,
68-
]);
69-
}
54+
const FILE_MAGIC: &[u8] = b"autoken-1";
7055

7156
pub fn save_in<F>(sess: &Session, path_buf: PathBuf, name: &str, encode: F)
7257
where
@@ -90,7 +75,7 @@ mod adapted_io {
9075
}
9176
};
9277

93-
write_file_header(&mut encoder);
78+
encoder.emit_raw_bytes(FILE_MAGIC);
9479

9580
match encode(encoder) {
9681
Ok(_) => {}
@@ -100,27 +85,14 @@ mod adapted_io {
10085
}
10186
}
10287

103-
/// Reads the contents of a file with a file header as defined in this module.
104-
///
105-
/// - Returns `Ok(Some(data, pos))` if the file existed and was generated by a
106-
/// compatible compiler version. `data` is the entire contents of the file
107-
/// and `pos` points to the first byte after the header.
108-
/// - Returns `Ok(None)` if the file did not exist or was generated by an
109-
/// incompatible version of the compiler.
110-
/// - Returns `Err(..)` if some kind of IO error occurred while reading the
111-
/// file.
11288
pub fn read_file(path: &Path) -> io::Result<Option<(Mmap, usize)>> {
89+
// Open file
11390
let file = match fs::File::open(path) {
11491
Ok(file) => file,
11592
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(None),
11693
Err(err) => return Err(err),
11794
};
118-
// SAFETY: This process must not modify nor remove the backing file while the memory map lives.
119-
// For the dep-graph and the work product index, it is as soon as the decoding is done.
120-
// For the query result cache, the memory map is dropped in save_dep_graph before calling
121-
// save_in and trying to remove the backing file.
122-
//
123-
// There is no way to prevent another process from modifying this file.
95+
12496
let mmap = unsafe { Mmap::map(file) }?;
12597

12698
let mut file = io::Cursor::new(&*mmap);
@@ -315,7 +287,7 @@ mod adapted_encoding {
315287
};
316288

317289
// Adapted from compiler/rustc_middle/src/query/on_disk_cache.rs:156
318-
let pos = AbsoluteBytePos(0);
290+
let pos = AbsoluteBytePos(start_pos as u64);
319291

320292
let alloc_decoding_session = AllocDecodingState::new(footer.interpret_alloc_index);
321293

0 commit comments

Comments
 (0)