diff --git a/compiler/rustc_middle/src/dep_graph/graph.rs b/compiler/rustc_middle/src/dep_graph/graph.rs index 2ce01ab4c6650..0d515ed82bc89 100644 --- a/compiler/rustc_middle/src/dep_graph/graph.rs +++ b/compiler/rustc_middle/src/dep_graph/graph.rs @@ -826,7 +826,13 @@ impl DepGraph { where F: FnOnce() -> String, { - let dep_node_debug = &self.data.as_ref().unwrap().dep_node_debug; + // Early queries (e.g., `-Z query-dep-graph` on empty crates) can reach here + // before the graph is initialized. Return early to prevent an ICE. + let data = match &self.data { + Some(d) => d, + None => return, + }; + let dep_node_debug = &data.dep_node_debug; if dep_node_debug.borrow().contains_key(&dep_node) { return; diff --git a/tests/ui/dep-graph/query-dep-graph-empty.rs b/tests/ui/dep-graph/query-dep-graph-empty.rs new file mode 100644 index 0000000000000..360dfd7f1126a --- /dev/null +++ b/tests/ui/dep-graph/query-dep-graph-empty.rs @@ -0,0 +1,7 @@ +//@ build-pass +//@ compile-flags: -Zquery-dep-graph --crate-type lib +//@ edition: 2021 + +// This file is intentionally left empty to reproduce issue #153199. +// rustc used to ICE when generating a dependency graph for an empty file +// because early queries would panic when unwrapping an uninitialized graph.