Skip to content

Commit 1e5c253

Browse files
incr.comp.: Add test case for cache artifact file headers.
1 parent 76f76ae commit 1e5c253

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

src/librustc_incremental/persist/file_format.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
use std::io::{self, Read};
2323
use std::path::Path;
2424
use std::fs::File;
25+
use std::env;
26+
27+
use rustc::session::config::nightly_options;
2528

2629
/// The first few bytes of files generated by incremental compilation
2730
const FILE_MAGIC: &'static [u8] = b"RSIC";
@@ -38,9 +41,11 @@ pub fn write_file_header<W: io::Write>(stream: &mut W) -> io::Result<()> {
3841
stream.write_all(FILE_MAGIC)?;
3942
stream.write_all(&[(HEADER_FORMAT_VERSION >> 0) as u8,
4043
(HEADER_FORMAT_VERSION >> 8) as u8])?;
41-
assert_eq!(RUSTC_VERSION.len(), (RUSTC_VERSION.len() as u8) as usize);
42-
stream.write_all(&[RUSTC_VERSION.len() as u8])?;
43-
stream.write_all(RUSTC_VERSION.as_bytes())?;
44+
45+
let rustc_version = rustc_version();
46+
assert_eq!(rustc_version.len(), (rustc_version.len() as u8) as usize);
47+
stream.write_all(&[rustc_version.len() as u8])?;
48+
stream.write_all(rustc_version.as_bytes())?;
4449

4550
Ok(())
4651
}
@@ -103,3 +108,13 @@ pub fn read_file(path: &Path) -> io::Result<Option<Vec<u8>>> {
103108

104109
Ok(Some(data))
105110
}
111+
112+
fn rustc_version() -> String {
113+
if nightly_options::is_nightly_build() {
114+
if let Some(val) = env::var_os("RUSTC_FORCE_INCR_COMP_ARTIFACT_HEADER") {
115+
return val.to_string_lossy().into_owned()
116+
}
117+
}
118+
119+
RUSTC_VERSION.to_string()
120+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// This test case makes sure that the compiler does not try to re-use anything
12+
// from the incremental compilation cache if the cache was produced by a
13+
// different compiler version. This is tested by artificially forcing the
14+
// emission of a different compiler version in the header of rpass1 artifacts,
15+
// and then making sure that the only object file of the test program gets
16+
// re-translated although the program stays unchanged.
17+
18+
// The `l33t haxx0r` Rust compiler is known to produce incr. comp. artifacts
19+
// that are outrageously incompatible with just about anything, even itself:
20+
//[rpass1] rustc-env:RUSTC_FORCE_INCR_COMP_ARTIFACT_HEADER="l33t haxx0r rustc 2.1 LTS"
21+
22+
// revisions:rpass1 rpass2
23+
24+
#![feature(rustc_attrs)]
25+
#![rustc_partition_translated(module="cache_file_headers", cfg="rpass2")]
26+
27+
fn main() {
28+
// empty
29+
}

0 commit comments

Comments
 (0)