Skip to content

Commit 1642fdf

Browse files
committed
Add -Zassert-incr-state to assert state of incremental cache
1 parent 220ed09 commit 1642fdf

File tree

6 files changed

+63
-4
lines changed

6 files changed

+63
-4
lines changed

compiler/rustc_incremental/src/persist/load.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_middle::dep_graph::{SerializedDepGraph, WorkProduct, WorkProductId};
66
use rustc_middle::ty::OnDiskCache;
77
use rustc_serialize::opaque::Decoder;
88
use rustc_serialize::Decodable;
9+
use rustc_session::config::IncrementalStateAssertion;
910
use rustc_session::Session;
1011
use std::path::Path;
1112

@@ -16,6 +17,7 @@ use super::work_product;
1617

1718
type WorkProductMap = FxHashMap<WorkProductId, WorkProduct>;
1819

20+
#[derive(Debug)]
1921
pub enum LoadResult<T> {
2022
Ok { data: T },
2123
DataOutOfDate,
@@ -24,6 +26,26 @@ pub enum LoadResult<T> {
2426

2527
impl<T: Default> LoadResult<T> {
2628
pub fn open(self, sess: &Session) -> T {
29+
// Check for errors when using `-Zassert-incremental-state`
30+
match (sess.opts.assert_incr_state, &self) {
31+
(Some(IncrementalStateAssertion::NotLoaded), LoadResult::Ok { .. }) => {
32+
sess.fatal(
33+
"We asserted that the incremental cache should not be loaded, \
34+
but it was loaded.",
35+
);
36+
}
37+
(
38+
Some(IncrementalStateAssertion::Loaded),
39+
LoadResult::Error { .. } | LoadResult::DataOutOfDate,
40+
) => {
41+
sess.fatal(
42+
"We asserted that an existing incremental cache directory should \
43+
be successfully loaded, but it was not.",
44+
);
45+
}
46+
_ => {}
47+
};
48+
2749
match self {
2850
LoadResult::Error { message } => {
2951
sess.warn(&message);
@@ -33,7 +55,7 @@ impl<T: Default> LoadResult<T> {
3355
if let Err(err) = delete_all_session_dir_contents(sess) {
3456
sess.err(&format!(
3557
"Failed to delete invalidated or incompatible \
36-
incremental compilation session directory contents `{}`: {}.",
58+
incremental compilation session directory contents `{}`: {}.",
3759
dep_graph_path(sess).display(),
3860
err
3961
));

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ fn test_debugging_options_tracking_hash() {
635635

636636
// Make sure that changing an [UNTRACKED] option leaves the hash unchanged.
637637
// This list is in alphabetical order.
638+
untracked!(assert_incr_state, Some(String::from("loaded")));
638639
untracked!(ast_json, true);
639640
untracked!(ast_json_noexpand, true);
640641
untracked!(borrowck, String::from("other"));

compiler/rustc_session/src/config.rs

+32
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,18 @@ pub enum LinkerPluginLto {
165165
Disabled,
166166
}
167167

168+
/// Used with `-Z assert-incr-state`.
169+
#[derive(Clone, Copy, PartialEq, Hash, Debug)]
170+
pub enum IncrementalStateAssertion {
171+
/// Found and loaded an existing session directory.
172+
///
173+
/// Note that this says nothing about whether any particular query
174+
/// will be found to be red or green.
175+
Loaded,
176+
/// Did not load an existing session directory.
177+
NotLoaded,
178+
}
179+
168180
impl LinkerPluginLto {
169181
pub fn enabled(&self) -> bool {
170182
match *self {
@@ -704,6 +716,7 @@ pub fn host_triple() -> &'static str {
704716
impl Default for Options {
705717
fn default() -> Options {
706718
Options {
719+
assert_incr_state: None,
707720
crate_types: Vec::new(),
708721
optimize: OptLevel::No,
709722
debuginfo: DebugInfo::None,
@@ -1626,6 +1639,21 @@ fn select_debuginfo(
16261639
}
16271640
}
16281641

1642+
crate fn parse_assert_incr_state(
1643+
opt_assertion: &Option<String>,
1644+
error_format: ErrorOutputType,
1645+
) -> Option<IncrementalStateAssertion> {
1646+
match opt_assertion {
1647+
Some(s) if s.as_str() == "loaded" => Some(IncrementalStateAssertion::Loaded),
1648+
Some(s) if s.as_str() == "not-loaded" => Some(IncrementalStateAssertion::NotLoaded),
1649+
Some(s) => early_error(
1650+
error_format,
1651+
&format!("unexpected incremental state assertion value: {}", s),
1652+
),
1653+
None => None,
1654+
}
1655+
}
1656+
16291657
fn parse_native_lib_kind(
16301658
matches: &getopts::Matches,
16311659
kind: &str,
@@ -2015,6 +2043,9 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
20152043

20162044
let incremental = cg.incremental.as_ref().map(PathBuf::from);
20172045

2046+
let assert_incr_state =
2047+
parse_assert_incr_state(&debugging_opts.assert_incr_state, error_format);
2048+
20182049
if debugging_opts.profile && incremental.is_some() {
20192050
early_error(
20202051
error_format,
@@ -2179,6 +2210,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
21792210
};
21802211

21812212
Options {
2213+
assert_incr_state,
21822214
crate_types,
21832215
optimize: opt_level,
21842216
debuginfo,

compiler/rustc_session/src/options.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::early_error;
44
use crate::lint;
55
use crate::search_paths::SearchPath;
66
use crate::utils::NativeLib;
7-
87
use rustc_target::spec::{CodeModel, LinkerFlavor, MergeFunctions, PanicStrategy, SanitizerSet};
98
use rustc_target::spec::{RelocModel, RelroLevel, SplitDebuginfo, TargetTriple, TlsModel};
109

@@ -150,6 +149,7 @@ top_level_options!(
150149
/// If `Some`, enable incremental compilation, using the given
151150
/// directory to store intermediate results.
152151
incremental: Option<PathBuf> [UNTRACKED],
152+
assert_incr_state: Option<IncrementalStateAssertion> [UNTRACKED],
153153

154154
debugging_opts: DebuggingOptions [SUBSTRUCT],
155155
prints: Vec<PrintRequest> [UNTRACKED],
@@ -1042,6 +1042,9 @@ options! {
10421042
"make cfg(version) treat the current version as incomplete (default: no)"),
10431043
asm_comments: bool = (false, parse_bool, [TRACKED],
10441044
"generate comments into the assembly (may change behavior) (default: no)"),
1045+
assert_incr_state: Option<String> = (None, parse_opt_string, [UNTRACKED],
1046+
"assert that the incremental cache is in given state: \
1047+
either `loaded` or `not-loaded`."),
10451048
ast_json: bool = (false, parse_bool, [UNTRACKED],
10461049
"print the AST as JSON and halt (default: no)"),
10471050
ast_json_noexpand: bool = (false, parse_bool, [UNTRACKED],
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// no-prefer-dynamic
2-
//[cfail1] compile-flags: -lbar -lfoo --crate-type lib
3-
//[cfail2] compile-flags: -lfoo -lbar --crate-type lib
2+
//[cfail1] compile-flags: -lbar -lfoo --crate-type lib -Zassert-incr-state=not-loaded
3+
//[cfail2] compile-flags: -lfoo -lbar --crate-type lib -Zassert-incr-state=not-loaded

src/test/incremental/struct_change_field_name.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
// revisions:rpass1 cfail2
55
// compile-flags: -Z query-dep-graph
6+
// [cfail2] compile-flags: -Z query-dep-graph -Z assert-incr-state=loaded
67

78
#![feature(rustc_attrs)]
89

0 commit comments

Comments
 (0)