1use std::env;
37use std::fs::{self, File};
38use std::io::Write;
39
40use rustc_data_structures::fx::FxIndexSet;
41use rustc_data_structures::graph::linked_graph::{Direction, INCOMING, NodeIndex, OUTGOING};
42use rustc_graphviz as dot;
43use rustc_hir as hir;
44use rustc_hir::Attribute;
45use rustc_hir::attrs::AttributeKind;
46use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId};
47use rustc_hir::intravisit::{self, Visitor};
48use rustc_middle::bug;
49use rustc_middle::dep_graph::{DepKind, DepNode, DepNodeFilter, EdgeFilter, RetainedDepGraph};
50use rustc_middle::hir::nested_filter;
51use rustc_middle::ty::TyCtxt;
52use rustc_span::{Span, Symbol, sym};
53use tracing::debug;
54
55use crate::errors;
56
57#[allow(missing_docs)]
58pub(crate) fn assert_dep_graph(tcx: TyCtxt<'_>) {
59 tcx.dep_graph.with_ignore(|| {
60 if tcx.sess.opts.unstable_opts.dump_dep_graph {
61 tcx.dep_graph.with_retained_dep_graph(dump_graph);
62 }
63
64 if !tcx.sess.opts.unstable_opts.query_dep_graph {
65 return;
66 }
67
68 if !tcx.features().rustc_attrs() {
72 return;
73 }
74
75 let (if_this_changed, then_this_would_need) = {
77 let mut visitor =
78 IfThisChanged { tcx, if_this_changed: ::alloc::vec::Vec::new()vec![], then_this_would_need: ::alloc::vec::Vec::new()vec![] };
79 visitor.process_attrs(CRATE_DEF_ID);
80 tcx.hir_visit_all_item_likes_in_crate(&mut visitor);
81 (visitor.if_this_changed, visitor.then_this_would_need)
82 };
83
84 if !if_this_changed.is_empty() || !then_this_would_need.is_empty() {
85 if !tcx.sess.opts.unstable_opts.query_dep_graph {
{
::core::panicking::panic_fmt(format_args!("cannot use the `#[{0}]` or `#[{1}]` annotations without supplying `-Z query-dep-graph`",
sym::rustc_if_this_changed, sym::rustc_then_this_would_need));
}
};assert!(
86 tcx.sess.opts.unstable_opts.query_dep_graph,
87 "cannot use the `#[{}]` or `#[{}]` annotations \
88 without supplying `-Z query-dep-graph`",
89 sym::rustc_if_this_changed,
90 sym::rustc_then_this_would_need
91 );
92 }
93
94 check_paths(tcx, &if_this_changed, &then_this_would_need);
96 })
97}
98
99type Sources = Vec<(Span, DefId, DepNode)>;
100type Targets = Vec<(Span, Symbol, hir::HirId, DepNode)>;
101
102struct IfThisChanged<'tcx> {
103 tcx: TyCtxt<'tcx>,
104 if_this_changed: Sources,
105 then_this_would_need: Targets,
106}
107
108impl<'tcx> IfThisChanged<'tcx> {
109 fn process_attrs(&mut self, def_id: LocalDefId) {
110 let def_path_hash = self.tcx.def_path_hash(def_id.to_def_id());
111 let hir_id = self.tcx.local_def_id_to_hir_id(def_id);
112 let attrs = self.tcx.hir_attrs(hir_id);
113 for attr in attrs {
114 if let Attribute::Parsed(AttributeKind::RustcIfThisChanged(span, dep_node)) = *attr {
115 let dep_node = match dep_node {
116 None => DepNode::from_def_path_hash(
117 self.tcx,
118 def_path_hash,
119 DepKind::opt_hir_owner_nodes,
120 ),
121 Some(n) => {
122 match DepNode::from_label_string(self.tcx, n.as_str(), def_path_hash) {
123 Ok(n) => n,
124 Err(()) => self
125 .tcx
126 .dcx()
127 .emit_fatal(errors::UnrecognizedDepNode { span, name: n }),
128 }
129 }
130 };
131 self.if_this_changed.push((span, def_id.to_def_id(), dep_node));
132 } else if let Attribute::Parsed(AttributeKind::RustcThenThisWouldNeed(
133 _,
134 ref dep_nodes,
135 )) = *attr
136 {
137 for &n in dep_nodes {
138 let Ok(dep_node) =
139 DepNode::from_label_string(self.tcx, n.as_str(), def_path_hash)
140 else {
141 self.tcx
142 .dcx()
143 .emit_fatal(errors::UnrecognizedDepNode { span: n.span, name: n.name });
144 };
145 self.then_this_would_need.push((n.span, n.name, hir_id, dep_node));
146 }
147 }
148 }
149 }
150}
151
152impl<'tcx> Visitor<'tcx> for IfThisChanged<'tcx> {
153 type NestedFilter = nested_filter::OnlyBodies;
154
155 fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
156 self.tcx
157 }
158
159 fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
160 self.process_attrs(item.owner_id.def_id);
161 intravisit::walk_item(self, item);
162 }
163
164 fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
165 self.process_attrs(trait_item.owner_id.def_id);
166 intravisit::walk_trait_item(self, trait_item);
167 }
168
169 fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
170 self.process_attrs(impl_item.owner_id.def_id);
171 intravisit::walk_impl_item(self, impl_item);
172 }
173
174 fn visit_field_def(&mut self, s: &'tcx hir::FieldDef<'tcx>) {
175 self.process_attrs(s.def_id);
176 intravisit::walk_field_def(self, s);
177 }
178}
179
180fn check_paths<'tcx>(tcx: TyCtxt<'tcx>, if_this_changed: &Sources, then_this_would_need: &Targets) {
181 if if_this_changed.is_empty() {
183 for &(target_span, _, _, _) in then_this_would_need {
184 tcx.dcx().emit_err(errors::MissingIfThisChanged { span: target_span });
185 }
186 return;
187 }
188 tcx.dep_graph.with_retained_dep_graph(|query| {
189 for &(_, source_def_id, ref source_dep_node) in if_this_changed {
190 let dependents = query.transitive_predecessors(source_dep_node);
191 for &(target_span, ref target_pass, _, ref target_dep_node) in then_this_would_need {
192 if !dependents.contains(&target_dep_node) {
193 tcx.dcx().emit_err(errors::NoPath {
194 span: target_span,
195 source: tcx.def_path_str(source_def_id),
196 target: *target_pass,
197 });
198 } else {
199 tcx.dcx().emit_err(errors::Ok { span: target_span });
200 }
201 }
202 }
203 });
204}
205
206fn dump_graph(graph: &RetainedDepGraph) {
207 let path: String = env::var("RUST_DEP_GRAPH").unwrap_or_else(|_| "dep_graph".to_string());
208
209 let nodes = match env::var("RUST_DEP_GRAPH_FILTER") {
210 Ok(string) => {
211 let edge_filter =
213 EdgeFilter::new(&string).unwrap_or_else(|e| ::rustc_middle::util::bug::bug_fmt(format_args!("invalid filter: {0}", e))bug!("invalid filter: {}", e));
214 let sources = node_set(graph, &edge_filter.source);
215 let targets = node_set(graph, &edge_filter.target);
216 filter_nodes(graph, &sources, &targets)
217 }
218 Err(_) => graph.nodes().into_iter().map(|n| n.kind).collect(),
219 };
220 let edges = filter_edges(graph, &nodes);
221
222 {
223 let txt_path = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}.txt", path))
})format!("{path}.txt");
225 let mut file = File::create_buffered(&txt_path).unwrap();
226 for (source, target) in &edges {
227 file.write_fmt(format_args!("{0:?} -> {1:?}\n", source, target))write!(file, "{source:?} -> {target:?}\n").unwrap();
228 }
229 }
230
231 {
232 let dot_path = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}.dot", path))
})format!("{path}.dot");
234 let mut v = Vec::new();
235 dot::render(&GraphvizDepGraph(nodes, edges), &mut v).unwrap();
236 fs::write(dot_path, v).unwrap();
237 }
238}
239
240#[allow(missing_docs)]
241struct GraphvizDepGraph(FxIndexSet<DepKind>, Vec<(DepKind, DepKind)>);
242
243impl<'a> dot::GraphWalk<'a> for GraphvizDepGraph {
244 type Node = DepKind;
245 type Edge = (DepKind, DepKind);
246 fn nodes(&self) -> dot::Nodes<'_, DepKind> {
247 let nodes: Vec<_> = self.0.iter().cloned().collect();
248 nodes.into()
249 }
250 fn edges(&self) -> dot::Edges<'_, (DepKind, DepKind)> {
251 self.1[..].into()
252 }
253 fn source(&self, edge: &(DepKind, DepKind)) -> DepKind {
254 edge.0
255 }
256 fn target(&self, edge: &(DepKind, DepKind)) -> DepKind {
257 edge.1
258 }
259}
260
261impl<'a> dot::Labeller<'a> for GraphvizDepGraph {
262 type Node = DepKind;
263 type Edge = (DepKind, DepKind);
264 fn graph_id(&self) -> dot::Id<'_> {
265 dot::Id::new("DependencyGraph").unwrap()
266 }
267 fn node_id(&self, n: &DepKind) -> dot::Id<'_> {
268 let s: String = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", n))
})format!("{n:?}")
269 .chars()
270 .map(|c| if c == '_' || c.is_alphanumeric() { c } else { '_' })
271 .collect();
272 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/assert_dep_graph.rs:272",
"rustc_incremental::assert_dep_graph",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/assert_dep_graph.rs"),
::tracing_core::__macro_support::Option::Some(272u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::assert_dep_graph"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("n={0:?} s={1:?}",
n, s) as &dyn Value))])
});
} else { ; }
};debug!("n={:?} s={:?}", n, s);
273 dot::Id::new(s).unwrap()
274 }
275 fn node_label(&self, n: &DepKind) -> dot::LabelText<'_> {
276 dot::LabelText::label(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", n))
})format!("{n:?}"))
277 }
278}
279
280fn node_set<'g>(
284 graph: &'g RetainedDepGraph,
285 filter: &DepNodeFilter,
286) -> Option<FxIndexSet<&'g DepNode>> {
287 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/assert_dep_graph.rs:287",
"rustc_incremental::assert_dep_graph",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/assert_dep_graph.rs"),
::tracing_core::__macro_support::Option::Some(287u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::assert_dep_graph"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("node_set(filter={0:?})",
filter) as &dyn Value))])
});
} else { ; }
};debug!("node_set(filter={:?})", filter);
288
289 if filter.accepts_all() {
290 return None;
291 }
292
293 Some(graph.nodes().into_iter().filter(|n| filter.test(n)).collect())
294}
295
296fn filter_nodes<'g>(
297 graph: &'g RetainedDepGraph,
298 sources: &Option<FxIndexSet<&'g DepNode>>,
299 targets: &Option<FxIndexSet<&'g DepNode>>,
300) -> FxIndexSet<DepKind> {
301 if let Some(sources) = sources {
302 if let Some(targets) = targets {
303 walk_between(graph, sources, targets)
304 } else {
305 walk_nodes(graph, sources, OUTGOING)
306 }
307 } else if let Some(targets) = targets {
308 walk_nodes(graph, targets, INCOMING)
309 } else {
310 graph.nodes().into_iter().map(|n| n.kind).collect()
311 }
312}
313
314fn walk_nodes<'g>(
315 graph: &'g RetainedDepGraph,
316 starts: &FxIndexSet<&'g DepNode>,
317 direction: Direction,
318) -> FxIndexSet<DepKind> {
319 let mut set = FxIndexSet::default();
320 for &start in starts {
321 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/assert_dep_graph.rs:321",
"rustc_incremental::assert_dep_graph",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/assert_dep_graph.rs"),
::tracing_core::__macro_support::Option::Some(321u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::assert_dep_graph"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("walk_nodes: start={0:?} outgoing?={1:?}",
start, direction == OUTGOING) as &dyn Value))])
});
} else { ; }
};debug!("walk_nodes: start={:?} outgoing?={:?}", start, direction == OUTGOING);
322 if set.insert(start.kind) {
323 let mut stack = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[graph.indices[start]]))vec![graph.indices[start]];
324 while let Some(index) = stack.pop() {
325 for (_, edge) in graph.inner.adjacent_edges(index, direction) {
326 let neighbor_index = edge.source_or_target(direction);
327 let neighbor = graph.inner.node_data(neighbor_index);
328 if set.insert(neighbor.kind) {
329 stack.push(neighbor_index);
330 }
331 }
332 }
333 }
334 }
335 set
336}
337
338fn walk_between<'g>(
339 graph: &'g RetainedDepGraph,
340 sources: &FxIndexSet<&'g DepNode>,
341 targets: &FxIndexSet<&'g DepNode>,
342) -> FxIndexSet<DepKind> {
343 #[derive(#[automatically_derived]
impl ::core::marker::Copy for State { }Copy, #[automatically_derived]
impl ::core::clone::Clone for State {
#[inline]
fn clone(&self) -> State { *self }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for State {
#[inline]
fn eq(&self, other: &State) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq)]
349 enum State {
350 Undecided,
351 Deciding,
352 Included,
353 Excluded,
354 }
355
356 let mut node_states = ::alloc::vec::from_elem(State::Undecided, graph.inner.len_nodes())vec![State::Undecided; graph.inner.len_nodes()];
357
358 for &target in targets {
359 node_states[graph.indices[target].0] = State::Included;
360 }
361
362 for source in sources.iter().map(|&n| graph.indices[n]) {
363 recurse(graph, &mut node_states, source);
364 }
365
366 return graph
367 .nodes()
368 .into_iter()
369 .filter(|&n| {
370 let index = graph.indices[n];
371 node_states[index.0] == State::Included
372 })
373 .map(|n| n.kind)
374 .collect();
375
376 fn recurse(graph: &RetainedDepGraph, node_states: &mut [State], node: NodeIndex) -> bool {
377 match node_states[node.0] {
378 State::Included => return true,
380
381 State::Excluded => return false,
383
384 State::Deciding => return false,
386
387 State::Undecided => {}
388 }
389
390 node_states[node.0] = State::Deciding;
391
392 for neighbor_index in graph.inner.successor_nodes(node) {
393 if recurse(graph, node_states, neighbor_index) {
394 node_states[node.0] = State::Included;
395 }
396 }
397
398 if node_states[node.0] == State::Deciding {
400 node_states[node.0] = State::Excluded;
401 false
402 } else {
403 if !(node_states[node.0] == State::Included) {
::core::panicking::panic("assertion failed: node_states[node.0] == State::Included")
};assert!(node_states[node.0] == State::Included);
404 true
405 }
406 }
407}
408
409fn filter_edges(graph: &RetainedDepGraph, nodes: &FxIndexSet<DepKind>) -> Vec<(DepKind, DepKind)> {
410 let uniq: FxIndexSet<_> = graph
411 .edges()
412 .into_iter()
413 .map(|(s, t)| (s.kind, t.kind))
414 .filter(|(source, target)| nodes.contains(source) && nodes.contains(target))
415 .collect();
416 uniq.into_iter().collect()
417}