Skip to content

Commit e73c56a

Browse files
committed
Auto merge of #154838 - Zalathar:rollup-lqjuxmF, r=Zalathar
Rollup of 5 pull requests Successful merges: - #154529 (Make `setup_dep_graph` incremental-only and more straightforward ) - #154565 (Remove `DepGraphData::dep_node_debug`.) - #154799 (add regression test for EII declaration conflicting with constructor) - #154807 (add regression test for 119686) - #154821 (Add three tests for fixed issues (two ICEs and one coherence checking failure that now passes))
2 parents f92020a + 549c4e4 commit e73c56a

17 files changed

Lines changed: 224 additions & 225 deletions

compiler/rustc_incremental/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ mod errors;
1010
mod persist;
1111

1212
pub use persist::{
13-
LoadResult, copy_cgu_workproduct_to_incr_comp_cache_dir, finalize_session_directory,
14-
in_incr_comp_dir, in_incr_comp_dir_sess, load_query_result_cache, save_work_product_index,
15-
setup_dep_graph,
13+
copy_cgu_workproduct_to_incr_comp_cache_dir, finalize_session_directory, in_incr_comp_dir,
14+
in_incr_comp_dir_sess, load_query_result_cache, save_work_product_index, setup_dep_graph,
1615
};
1716
use rustc_middle::util::Providers;
1817

compiler/rustc_incremental/src/persist/fs.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,7 @@ pub(crate) fn prepare_session_directory(
215215
crate_name: Symbol,
216216
stable_crate_id: StableCrateId,
217217
) {
218-
if sess.opts.incremental.is_none() {
219-
return;
220-
}
218+
assert!(sess.opts.incremental.is_some());
221219

222220
let _timer = sess.timer("incr_comp_prepare_session_directory");
223221

compiler/rustc_incremental/src/persist/load.rs

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,44 @@
11
//! Code to load the dep-graph from files.
22
3+
use std::io;
34
use std::path::PathBuf;
45
use std::sync::Arc;
56

67
use rustc_data_structures::unord::UnordMap;
78
use rustc_hashes::Hash64;
89
use rustc_middle::dep_graph::{DepGraph, SerializedDepGraph, WorkProductMap};
910
use rustc_middle::query::on_disk_cache::OnDiskCache;
10-
use rustc_serialize::Decodable;
11-
use rustc_serialize::opaque::MemDecoder;
11+
use rustc_serialize::opaque::{FileEncoder, MemDecoder};
12+
use rustc_serialize::{Decodable, Encodable};
1213
use rustc_session::config::IncrementalStateAssertion;
1314
use rustc_session::{Session, StableCrateId};
1415
use rustc_span::Symbol;
1516
use tracing::{debug, warn};
1617

1718
use super::data::*;
1819
use super::fs::*;
19-
use super::save::build_dep_graph;
2020
use super::{file_format, work_product};
2121
use crate::errors;
2222
use crate::persist::file_format::{OpenFile, OpenFileError};
2323

2424
#[derive(Debug)]
2525
/// Represents the result of an attempt to load incremental compilation data.
26-
pub enum LoadResult<T> {
26+
enum LoadResult {
2727
/// Loading was successful.
28-
Ok {
29-
#[allow(missing_docs)]
30-
data: T,
31-
},
28+
Ok { prev_graph: Arc<SerializedDepGraph>, prev_work_products: WorkProductMap },
3229
/// The file either didn't exist or was produced by an incompatible compiler version.
3330
DataOutOfDate,
34-
/// Loading the dep graph failed.
35-
LoadDepGraph(PathBuf, std::io::Error),
36-
}
37-
38-
impl<T: Default> LoadResult<T> {
39-
/// Accesses the data returned in [`LoadResult::Ok`].
40-
pub fn open(self, sess: &Session) -> T {
41-
// Emit a fatal error if `-Zassert-incr-state` is present and unsatisfied.
42-
maybe_assert_incr_state(sess, &self);
43-
44-
match self {
45-
LoadResult::LoadDepGraph(path, err) => {
46-
sess.dcx().emit_warn(errors::LoadDepGraph { path, err });
47-
Default::default()
48-
}
49-
LoadResult::DataOutOfDate => {
50-
if let Err(err) = delete_all_session_dir_contents(sess) {
51-
sess.dcx()
52-
.emit_err(errors::DeleteIncompatible { path: dep_graph_path(sess), err });
53-
}
54-
Default::default()
55-
}
56-
LoadResult::Ok { data } => data,
57-
}
58-
}
31+
/// Loading failed due to an unexpected I/O error.
32+
IoError { path: PathBuf, err: io::Error },
5933
}
6034

6135
fn delete_dirty_work_product(sess: &Session, swp: SerializedWorkProduct) {
6236
debug!("delete_dirty_work_product({:?})", swp);
6337
work_product::delete_workproduct_files(sess, &swp.work_product);
6438
}
6539

66-
fn load_dep_graph(sess: &Session) -> LoadResult<(Arc<SerializedDepGraph>, WorkProductMap)> {
67-
let prof = sess.prof.clone();
68-
69-
if sess.opts.incremental.is_none() {
70-
// No incremental compilation.
71-
return LoadResult::Ok { data: Default::default() };
72-
}
40+
fn load_dep_graph(sess: &Session) -> LoadResult {
41+
assert!(sess.opts.incremental.is_some());
7342

7443
let _timer = sess.prof.generic_activity("incr_comp_prepare_load_dep_graph");
7544

@@ -117,11 +86,11 @@ fn load_dep_graph(sess: &Session) -> LoadResult<(Arc<SerializedDepGraph>, WorkPr
11786
}
11887
}
11988

120-
let _prof_timer = prof.generic_activity("incr_comp_load_dep_graph");
89+
let _prof_timer = sess.prof.generic_activity("incr_comp_load_dep_graph");
12190

12291
match file_format::open_incremental_file(sess, &path) {
12392
Err(OpenFileError::NotFoundOrHeaderMismatch) => LoadResult::DataOutOfDate,
124-
Err(OpenFileError::IoError { err }) => LoadResult::LoadDepGraph(path.to_owned(), err),
93+
Err(OpenFileError::IoError { err }) => LoadResult::IoError { path: path.to_owned(), err },
12594
Ok(OpenFile { mmap, start_pos }) => {
12695
let Ok(mut decoder) = MemDecoder::new(&mmap, start_pos) else {
12796
sess.dcx().emit_warn(errors::CorruptFile { path: &path });
@@ -143,9 +112,9 @@ fn load_dep_graph(sess: &Session) -> LoadResult<(Arc<SerializedDepGraph>, WorkPr
143112
return LoadResult::DataOutOfDate;
144113
}
145114

146-
let dep_graph = SerializedDepGraph::decode(&mut decoder);
115+
let prev_graph = SerializedDepGraph::decode(&mut decoder);
147116

148-
LoadResult::Ok { data: (dep_graph, prev_work_products) }
117+
LoadResult::Ok { prev_graph, prev_work_products }
149118
}
150119
}
151120
}
@@ -179,14 +148,14 @@ pub fn load_query_result_cache(sess: &Session) -> Option<OnDiskCache> {
179148

180149
/// Emits a fatal error if the assertion in `-Zassert-incr-state` doesn't match
181150
/// the outcome of trying to load previous-session state.
182-
fn maybe_assert_incr_state(sess: &Session, load_result: &LoadResult<impl Sized>) {
151+
fn maybe_assert_incr_state(sess: &Session, load_result: &LoadResult) {
183152
// Return immediately if there's nothing to assert.
184153
let Some(assertion) = sess.opts.unstable_opts.assert_incr_state else { return };
185154

186155
// Match exhaustively to make sure we don't miss any cases.
187156
let loaded = match load_result {
188157
LoadResult::Ok { .. } => true,
189-
LoadResult::DataOutOfDate | LoadResult::LoadDepGraph(..) => false,
158+
LoadResult::DataOutOfDate | LoadResult::IoError { .. } => false,
190159
};
191160

192161
match assertion {
@@ -203,33 +172,64 @@ fn maybe_assert_incr_state(sess: &Session, load_result: &LoadResult<impl Sized>)
203172
}
204173
}
205174

206-
/// Setups the dependency graph by loading an existing graph from disk and set up streaming of a
207-
/// new graph to an incremental session directory.
175+
/// Loads the previous session's dependency graph from disk if possible, and
176+
/// sets up streaming output for the current session's dep graph data into an
177+
/// incremental session directory.
178+
///
179+
/// In non-incremental mode, a dummy dep graph is returned immediately.
208180
pub fn setup_dep_graph(
209181
sess: &Session,
210182
crate_name: Symbol,
211183
stable_crate_id: StableCrateId,
212184
) -> DepGraph {
185+
if sess.opts.incremental.is_none() {
186+
return DepGraph::new_disabled();
187+
}
188+
213189
// `load_dep_graph` can only be called after `prepare_session_directory`.
214190
prepare_session_directory(sess, crate_name, stable_crate_id);
191+
// Try to load the previous session's dep graph and work products.
192+
let load_result = load_dep_graph(sess);
193+
194+
sess.time("incr_comp_garbage_collect_session_directories", || {
195+
if let Err(e) = garbage_collect_session_directories(sess) {
196+
warn!(
197+
"Error while trying to garbage collect incremental compilation \
198+
cache directory: {e}",
199+
);
200+
}
201+
});
215202

216-
let res = sess.opts.build_dep_graph().then(|| load_dep_graph(sess));
203+
// Emit a fatal error if `-Zassert-incr-state` is present and unsatisfied.
204+
maybe_assert_incr_state(sess, &load_result);
217205

218-
if sess.opts.incremental.is_some() {
219-
sess.time("incr_comp_garbage_collect_session_directories", || {
220-
if let Err(e) = garbage_collect_session_directories(sess) {
221-
warn!(
222-
"Error while trying to garbage collect incremental \
223-
compilation cache directory: {}",
224-
e
225-
);
206+
let (prev_graph, prev_work_products) = match load_result {
207+
LoadResult::IoError { path, err } => {
208+
sess.dcx().emit_warn(errors::LoadDepGraph { path, err });
209+
Default::default()
210+
}
211+
LoadResult::DataOutOfDate => {
212+
if let Err(err) = delete_all_session_dir_contents(sess) {
213+
sess.dcx().emit_err(errors::DeleteIncompatible { path: dep_graph_path(sess), err });
226214
}
227-
});
228-
}
215+
Default::default()
216+
}
217+
LoadResult::Ok { prev_graph, prev_work_products } => (prev_graph, prev_work_products),
218+
};
219+
220+
// Stream the dep-graph to an alternate file, to avoid overwriting anything in case of errors.
221+
let path_buf = staging_dep_graph_path(sess);
222+
223+
let mut encoder = FileEncoder::new(&path_buf).unwrap_or_else(|err| {
224+
// We're in incremental mode but couldn't set up streaming output of the dep graph.
225+
// Exit immediately instead of continuing in an inconsistent and untested state.
226+
sess.dcx().emit_fatal(errors::CreateDepGraph { path: &path_buf, err })
227+
});
228+
229+
file_format::write_file_header(&mut encoder, sess);
230+
231+
// First encode the commandline arguments hash
232+
sess.opts.dep_tracking_hash(false).encode(&mut encoder);
229233

230-
res.and_then(|result| {
231-
let (prev_graph, prev_work_products) = result.open(sess);
232-
build_dep_graph(sess, prev_graph, prev_work_products)
233-
})
234-
.unwrap_or_else(DepGraph::new_disabled)
234+
DepGraph::new(sess, prev_graph, prev_work_products, encoder)
235235
}

compiler/rustc_incremental/src/persist/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ mod save;
1111
mod work_product;
1212

1313
pub use fs::{finalize_session_directory, in_incr_comp_dir, in_incr_comp_dir_sess};
14-
pub use load::{LoadResult, load_query_result_cache, setup_dep_graph};
14+
pub use load::{load_query_result_cache, setup_dep_graph};
1515
pub(crate) use save::save_dep_graph;
1616
pub use save::save_work_product_index;
1717
pub use work_product::copy_cgu_workproduct_to_incr_comp_cache_dir;

compiler/rustc_incremental/src/persist/save.rs

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
use std::fs;
2-
use std::sync::Arc;
32

43
use rustc_data_structures::fx::FxIndexMap;
54
use rustc_data_structures::sync::par_join;
6-
use rustc_middle::dep_graph::{
7-
DepGraph, SerializedDepGraph, WorkProduct, WorkProductId, WorkProductMap,
8-
};
5+
use rustc_middle::dep_graph::{DepGraph, WorkProduct, WorkProductId};
96
use rustc_middle::query::on_disk_cache;
107
use rustc_middle::ty::TyCtxt;
118
use rustc_serialize::Encodable as RustcEncodable;
@@ -149,38 +146,3 @@ fn encode_work_product_index(
149146

150147
serialized_products.encode(encoder)
151148
}
152-
153-
/// Builds the dependency graph.
154-
///
155-
/// This function creates the *staging dep-graph*. When the dep-graph is modified by a query
156-
/// execution, the new dependency information is not kept in memory but directly
157-
/// output to this file. `save_dep_graph` then finalizes the staging dep-graph
158-
/// and moves it to the permanent dep-graph path
159-
pub(crate) fn build_dep_graph(
160-
sess: &Session,
161-
prev_graph: Arc<SerializedDepGraph>,
162-
prev_work_products: WorkProductMap,
163-
) -> Option<DepGraph> {
164-
if sess.opts.incremental.is_none() {
165-
// No incremental compilation.
166-
return None;
167-
}
168-
169-
// Stream the dep-graph to an alternate file, to avoid overwriting anything in case of errors.
170-
let path_buf = staging_dep_graph_path(sess);
171-
172-
let mut encoder = match FileEncoder::new(&path_buf) {
173-
Ok(encoder) => encoder,
174-
Err(err) => {
175-
sess.dcx().emit_err(errors::CreateDepGraph { path: &path_buf, err });
176-
return None;
177-
}
178-
};
179-
180-
file_format::write_file_header(&mut encoder, sess);
181-
182-
// First encode the commandline arguments hash
183-
sess.opts.dep_tracking_hash(false).encode(&mut encoder);
184-
185-
Some(DepGraph::new(sess, prev_graph, prev_work_products, encoder))
186-
}

compiler/rustc_middle/src/dep_graph/dep_node.rs

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,7 @@ impl DepNode {
121121
where
122122
Key: DepNodeKey<'tcx>,
123123
{
124-
let dep_node = DepNode { kind, key_fingerprint: key.to_fingerprint(tcx).into() };
125-
126-
#[cfg(debug_assertions)]
127-
{
128-
if !tcx.key_fingerprint_style(kind).is_maybe_recoverable()
129-
&& (tcx.sess.opts.unstable_opts.incremental_info
130-
|| tcx.sess.opts.unstable_opts.query_dep_graph)
131-
{
132-
tcx.dep_graph.register_dep_node_debug_str(dep_node, || key.to_debug_str(tcx));
133-
}
134-
}
135-
136-
dep_node
124+
DepNode { kind, key_fingerprint: key.to_fingerprint(tcx).into() }
137125
}
138126

139127
/// Construct a DepNode from the given DepKind and DefPathHash. This
@@ -151,24 +139,16 @@ impl DepNode {
151139

152140
impl fmt::Debug for DepNode {
153141
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
154-
write!(f, "{:?}(", self.kind)?;
155-
156142
tls::with_opt(|opt_tcx| {
157-
if let Some(tcx) = opt_tcx {
158-
if let Some(def_id) = self.extract_def_id(tcx) {
159-
write!(f, "{}", tcx.def_path_debug_str(def_id))?;
160-
} else if let Some(ref s) = tcx.dep_graph.dep_node_debug_str(*self) {
161-
write!(f, "{s}")?;
162-
} else {
163-
write!(f, "{}", self.key_fingerprint)?;
164-
}
143+
if let Some(tcx) = opt_tcx
144+
&& let Some(def_id) = self.extract_def_id(tcx)
145+
{
146+
write!(f, "{:?}({})", self.kind, tcx.def_path_debug_str(def_id))?;
165147
} else {
166-
write!(f, "{}", self.key_fingerprint)?;
148+
write!(f, "{:?}({})", self.kind, self.key_fingerprint)?;
167149
}
168150
Ok(())
169-
})?;
170-
171-
write!(f, ")")
151+
})
172152
}
173153
}
174154

0 commit comments

Comments
 (0)