diff --git a/compiler/rustc_incremental/src/persist/load.rs b/compiler/rustc_incremental/src/persist/load.rs index 6f84073ff645..c0466a4b5211 100644 --- a/compiler/rustc_incremental/src/persist/load.rs +++ b/compiler/rustc_incremental/src/persist/load.rs @@ -38,19 +38,8 @@ pub enum LoadResult { impl LoadResult { /// Accesses the data returned in [`LoadResult::Ok`]. pub fn open(self, sess: &Session) -> T { - // Check for errors when using `-Zassert-incr-state`. - match (sess.opts.unstable_opts.assert_incr_state, &self) { - (Some(IncrementalStateAssertion::NotLoaded), LoadResult::Ok { .. }) => { - sess.dcx().emit_fatal(errors::AssertNotLoaded); - } - ( - Some(IncrementalStateAssertion::Loaded), - LoadResult::LoadDepGraph(..) | LoadResult::DataOutOfDate, - ) => { - sess.dcx().emit_fatal(errors::AssertLoaded); - } - _ => {} - }; + // Emit a fatal error if `-Zassert-incr-state` is present and unsatisfied. + maybe_assert_incr_state(sess, &self); match self { LoadResult::LoadDepGraph(path, err) => { @@ -188,6 +177,32 @@ pub fn load_query_result_cache(sess: &Session) -> Option { } } +/// Emits a fatal error if the assertion in `-Zassert-incr-state` doesn't match +/// the outcome of trying to load previous-session state. +fn maybe_assert_incr_state(sess: &Session, load_result: &LoadResult) { + // Return immediately if there's nothing to assert. + let Some(assertion) = sess.opts.unstable_opts.assert_incr_state else { return }; + + // Match exhaustively to make sure we don't miss any cases. + let loaded = match load_result { + LoadResult::Ok { .. } => true, + LoadResult::DataOutOfDate | LoadResult::LoadDepGraph(..) => false, + }; + + match assertion { + IncrementalStateAssertion::Loaded => { + if !loaded { + sess.dcx().emit_fatal(errors::AssertLoaded); + } + } + IncrementalStateAssertion::NotLoaded => { + if loaded { + sess.dcx().emit_fatal(errors::AssertNotLoaded) + } + } + } +} + /// Setups the dependency graph by loading an existing graph from disk and set up streaming of a /// new graph to an incremental session directory. pub fn setup_dep_graph(