Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions compiler/rustc_incremental/src/assert_dep_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@

use std::env;
use std::fs::{self, File};
use std::io::Write;
use std::io::{self, Write};
use std::path::PathBuf;

use rustc_data_structures::fx::FxIndexSet;
use rustc_data_structures::graph::linked_graph::{Direction, INCOMING, NodeIndex, OUTGOING};
Expand Down Expand Up @@ -63,7 +64,13 @@ pub(crate) fn assert_dep_graph(tcx: TyCtxt<'_>) {

if tcx.sess.opts.unstable_opts.dump_dep_graph {
if let Some(graph) = &retained_dep_graph {
dump_graph(graph);
if let Err((path, err)) = dump_graph(graph) {
tcx.dcx().emit_fatal(diagnostics::WriteNew {
name: "dependency graph",
path,
err,
});
}
}
}

Expand Down Expand Up @@ -208,7 +215,7 @@ fn check_paths<'tcx>(
}
}

fn dump_graph(graph: &RetainedDepGraph) {
fn dump_graph(graph: &RetainedDepGraph) -> Result<(), (PathBuf, io::Error)> {
let path: String = env::var("RUST_DEP_GRAPH").unwrap_or_else(|_| "dep_graph".to_string());

let nodes = match env::var("RUST_DEP_GRAPH_FILTER") {
Expand All @@ -226,20 +233,23 @@ fn dump_graph(graph: &RetainedDepGraph) {

{
// dump a .txt file with just the edges:
let txt_path = format!("{path}.txt");
let mut file = File::create_buffered(&txt_path).unwrap();
let txt_path = PathBuf::from(format!("{path}.txt"));
let mut file = File::create_buffered(&txt_path).map_err(|err| (txt_path.clone(), err))?;
for (source, target) in &edges {
write!(file, "{source:?} -> {target:?}\n").unwrap();
write!(file, "{source:?} -> {target:?}\n").map_err(|err| (txt_path.clone(), err))?;
}
}

{
// dump a .dot file in graphviz format:
let dot_path = format!("{path}.dot");
let dot_path = PathBuf::from(format!("{path}.dot"));
let mut v = Vec::new();
dot::render(&GraphvizDepGraph(nodes, edges), &mut v).unwrap();
fs::write(dot_path, v).unwrap();
dot::render(&GraphvizDepGraph(nodes, edges), &mut v)
.expect("writing the dependency graph to a byte buffer cannot fail");
fs::write(&dot_path, v).map_err(|err| (dot_path, err))?;
}

Ok(())
}

#[allow(missing_docs)]
Expand Down
10 changes: 10 additions & 0 deletions tests/run-make/dep-graph/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,14 @@ fn main() {

assert!(path("dep-graph.txt").is_file());
assert!(path("dep-graph.dot").is_file());

rustc()
.input("foo.rs")
.incremental(path("incr-fail"))
.arg("-Zquery-dep-graph")
.arg("-Zdump-dep-graph")
.env("RUST_DEP_GRAPH", path("does-not-exist/dep-graph"))
.run_fail()
.assert_stderr_contains("failed to write dependency graph")
.assert_not_ice();
}
Loading