Skip to main content

rustc_incremental/
assert_dep_graph.rs

1//! This pass is only used for the UNIT TESTS and DEBUGGING NEEDS
2//! around dependency graph construction. It serves two purposes; it
3//! will dump graphs in graphviz form to disk, and it searches for
4//! `#[rustc_if_this_changed]` and `#[rustc_then_this_would_need]`
5//! annotations. These annotations can be used to test whether paths
6//! exist in the graph. These checks run after codegen, so they view the
7//! the final state of the dependency graph. Note that there are
8//! similar assertions found in `persist::clean` which check the
9//! **initial** state of the dependency graph, just after it has been
10//! loaded from disk.
11//!
12//! In this code, we report errors on each `rustc_if_this_changed`
13//! annotation. If a path exists in all cases, then we would report
14//! "all path(s) exist". Otherwise, we report: "no path to `foo`" for
15//! each case where no path exists. `ui` tests can then be
16//! used to check when paths exist or do not.
17//!
18//! The full form of the `rustc_if_this_changed` annotation is
19//! `#[rustc_if_this_changed("foo")]`, which will report a
20//! source node of `foo(def_id)`. The `"foo"` is optional and
21//! defaults to `"Hir"` if omitted.
22//!
23//! Example:
24//!
25//! ```ignore (needs flags)
26//! #[rustc_if_this_changed(Hir)]
27//! fn foo() { }
28//!
29//! #[rustc_then_this_would_need(codegen)] //~ ERROR no path from `foo`
30//! fn bar() { }
31//!
32//! #[rustc_then_this_would_need(codegen)] //~ ERROR OK
33//! fn baz() { foo(); }
34//! ```
35
36use 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 the `rustc_attrs` feature is not enabled, then the
69        // attributes we are interested in cannot be present anyway, so
70        // skip the walk.
71        if !tcx.features().rustc_attrs() {
72            return;
73        }
74
75        // Find annotations supplied by user (if any).
76        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.
95        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 => {
117                        DepNode::from_def_path_hash(self.tcx, def_path_hash, DepKind::hir_owner)
118                    }
119                    Some(n) => {
120                        match DepNode::from_label_string(self.tcx, n.as_str(), def_path_hash) {
121                            Ok(n) => n,
122                            Err(()) => self
123                                .tcx
124                                .dcx()
125                                .emit_fatal(errors::UnrecognizedDepNode { span, name: n }),
126                        }
127                    }
128                };
129                self.if_this_changed.push((span, def_id.to_def_id(), dep_node));
130            } else if let Attribute::Parsed(AttributeKind::RustcThenThisWouldNeed(dep_nodes)) = attr
131            {
132                for &n in dep_nodes {
133                    let Ok(dep_node) =
134                        DepNode::from_label_string(self.tcx, n.as_str(), def_path_hash)
135                    else {
136                        self.tcx
137                            .dcx()
138                            .emit_fatal(errors::UnrecognizedDepNode { span: n.span, name: n.name });
139                    };
140                    self.then_this_would_need.push((n.span, n.name, hir_id, dep_node));
141                }
142            }
143        }
144    }
145}
146
147impl<'tcx> Visitor<'tcx> for IfThisChanged<'tcx> {
148    type NestedFilter = nested_filter::OnlyBodies;
149
150    fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
151        self.tcx
152    }
153
154    fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
155        self.process_attrs(item.owner_id.def_id);
156        intravisit::walk_item(self, item);
157    }
158
159    fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
160        self.process_attrs(trait_item.owner_id.def_id);
161        intravisit::walk_trait_item(self, trait_item);
162    }
163
164    fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
165        self.process_attrs(impl_item.owner_id.def_id);
166        intravisit::walk_impl_item(self, impl_item);
167    }
168
169    fn visit_field_def(&mut self, s: &'tcx hir::FieldDef<'tcx>) {
170        self.process_attrs(s.def_id);
171        intravisit::walk_field_def(self, s);
172    }
173}
174
175fn check_paths<'tcx>(tcx: TyCtxt<'tcx>, if_this_changed: &Sources, then_this_would_need: &Targets) {
176    // Return early here so as not to construct the query, which is not cheap.
177    if if_this_changed.is_empty() {
178        for &(target_span, _, _, _) in then_this_would_need {
179            tcx.dcx().emit_err(errors::MissingIfThisChanged { span: target_span });
180        }
181        return;
182    }
183    tcx.dep_graph.with_retained_dep_graph(|query| {
184        for &(_, source_def_id, ref source_dep_node) in if_this_changed {
185            let dependents = query.transitive_predecessors(source_dep_node);
186            for &(target_span, ref target_pass, _, ref target_dep_node) in then_this_would_need {
187                if !dependents.contains(&target_dep_node) {
188                    tcx.dcx().emit_err(errors::NoPath {
189                        span: target_span,
190                        source: tcx.def_path_str(source_def_id),
191                        target: *target_pass,
192                    });
193                } else {
194                    tcx.dcx().emit_err(errors::Ok { span: target_span });
195                }
196            }
197        }
198    });
199}
200
201fn dump_graph(graph: &RetainedDepGraph) {
202    let path: String = env::var("RUST_DEP_GRAPH").unwrap_or_else(|_| "dep_graph".to_string());
203
204    let nodes = match env::var("RUST_DEP_GRAPH_FILTER") {
205        Ok(string) => {
206            // Expect one of: "-> target", "source -> target", or "source ->".
207            let edge_filter =
208                EdgeFilter::new(&string).unwrap_or_else(|e| ::rustc_middle::util::bug::bug_fmt(format_args!("invalid filter: {0}", e))bug!("invalid filter: {}", e));
209            let sources = node_set(graph, &edge_filter.source);
210            let targets = node_set(graph, &edge_filter.target);
211            filter_nodes(graph, &sources, &targets)
212        }
213        Err(_) => graph.nodes().into_iter().map(|n| n.kind).collect(),
214    };
215    let edges = filter_edges(graph, &nodes);
216
217    {
218        // dump a .txt file with just the edges:
219        let txt_path = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}.txt", path))
    })format!("{path}.txt");
220        let mut file = File::create_buffered(&txt_path).unwrap();
221        for (source, target) in &edges {
222            file.write_fmt(format_args!("{0:?} -> {1:?}\n", source, target))write!(file, "{source:?} -> {target:?}\n").unwrap();
223        }
224    }
225
226    {
227        // dump a .dot file in graphviz format:
228        let dot_path = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}.dot", path))
    })format!("{path}.dot");
229        let mut v = Vec::new();
230        dot::render(&GraphvizDepGraph(nodes, edges), &mut v).unwrap();
231        fs::write(dot_path, v).unwrap();
232    }
233}
234
235#[allow(missing_docs)]
236struct GraphvizDepGraph(FxIndexSet<DepKind>, Vec<(DepKind, DepKind)>);
237
238impl<'a> dot::GraphWalk<'a> for GraphvizDepGraph {
239    type Node = DepKind;
240    type Edge = (DepKind, DepKind);
241    fn nodes(&self) -> dot::Nodes<'_, DepKind> {
242        let nodes: Vec<_> = self.0.iter().cloned().collect();
243        nodes.into()
244    }
245    fn edges(&self) -> dot::Edges<'_, (DepKind, DepKind)> {
246        self.1[..].into()
247    }
248    fn source(&self, edge: &(DepKind, DepKind)) -> DepKind {
249        edge.0
250    }
251    fn target(&self, edge: &(DepKind, DepKind)) -> DepKind {
252        edge.1
253    }
254}
255
256impl<'a> dot::Labeller<'a> for GraphvizDepGraph {
257    type Node = DepKind;
258    type Edge = (DepKind, DepKind);
259    fn graph_id(&self) -> dot::Id<'_> {
260        dot::Id::new("DependencyGraph").unwrap()
261    }
262    fn node_id(&self, n: &DepKind) -> dot::Id<'_> {
263        let s: String = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0:?}", n))
    })format!("{n:?}")
264            .chars()
265            .map(|c| if c == '_' || c.is_alphanumeric() { c } else { '_' })
266            .collect();
267        {
    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:267",
                        "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(267u32),
                        ::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);
268        dot::Id::new(s).unwrap()
269    }
270    fn node_label(&self, n: &DepKind) -> dot::LabelText<'_> {
271        dot::LabelText::label(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0:?}", n))
    })format!("{n:?}"))
272    }
273}
274
275// Given an optional filter like `"x,y,z"`, returns either `None` (no
276// filter) or the set of nodes whose labels contain all of those
277// substrings.
278fn node_set<'g>(
279    graph: &'g RetainedDepGraph,
280    filter: &DepNodeFilter,
281) -> Option<FxIndexSet<&'g DepNode>> {
282    {
    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:282",
                        "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(282u32),
                        ::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);
283
284    if filter.accepts_all() {
285        return None;
286    }
287
288    Some(graph.nodes().into_iter().filter(|n| filter.test(n)).collect())
289}
290
291fn filter_nodes<'g>(
292    graph: &'g RetainedDepGraph,
293    sources: &Option<FxIndexSet<&'g DepNode>>,
294    targets: &Option<FxIndexSet<&'g DepNode>>,
295) -> FxIndexSet<DepKind> {
296    if let Some(sources) = sources {
297        if let Some(targets) = targets {
298            walk_between(graph, sources, targets)
299        } else {
300            walk_nodes(graph, sources, OUTGOING)
301        }
302    } else if let Some(targets) = targets {
303        walk_nodes(graph, targets, INCOMING)
304    } else {
305        graph.nodes().into_iter().map(|n| n.kind).collect()
306    }
307}
308
309fn walk_nodes<'g>(
310    graph: &'g RetainedDepGraph,
311    starts: &FxIndexSet<&'g DepNode>,
312    direction: Direction,
313) -> FxIndexSet<DepKind> {
314    let mut set = FxIndexSet::default();
315    for &start in starts {
316        {
    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:316",
                        "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(316u32),
                        ::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);
317        if set.insert(start.kind) {
318            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]];
319            while let Some(index) = stack.pop() {
320                for (_, edge) in graph.inner.adjacent_edges(index, direction) {
321                    let neighbor_index = edge.source_or_target(direction);
322                    let neighbor = graph.inner.node_data(neighbor_index);
323                    if set.insert(neighbor.kind) {
324                        stack.push(neighbor_index);
325                    }
326                }
327            }
328        }
329    }
330    set
331}
332
333fn walk_between<'g>(
334    graph: &'g RetainedDepGraph,
335    sources: &FxIndexSet<&'g DepNode>,
336    targets: &FxIndexSet<&'g DepNode>,
337) -> FxIndexSet<DepKind> {
338    // This is a bit tricky. We want to include a node only if it is:
339    // (a) reachable from a source and (b) will reach a target. And we
340    // have to be careful about cycles etc. Luckily efficiency is not
341    // a big concern!
342
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)]
344    enum State {
345        Undecided,
346        Deciding,
347        Included,
348        Excluded,
349    }
350
351    let mut node_states = ::alloc::vec::from_elem(State::Undecided, graph.inner.len_nodes())vec![State::Undecided; graph.inner.len_nodes()];
352
353    for &target in targets {
354        node_states[graph.indices[target].0] = State::Included;
355    }
356
357    for source in sources.iter().map(|&n| graph.indices[n]) {
358        recurse(graph, &mut node_states, source);
359    }
360
361    return graph
362        .nodes()
363        .into_iter()
364        .filter(|&n| {
365            let index = graph.indices[n];
366            node_states[index.0] == State::Included
367        })
368        .map(|n| n.kind)
369        .collect();
370
371    fn recurse(graph: &RetainedDepGraph, node_states: &mut [State], node: NodeIndex) -> bool {
372        match node_states[node.0] {
373            // known to reach a target
374            State::Included => return true,
375
376            // known not to reach a target
377            State::Excluded => return false,
378
379            // backedge, not yet known, say false
380            State::Deciding => return false,
381
382            State::Undecided => {}
383        }
384
385        node_states[node.0] = State::Deciding;
386
387        for neighbor_index in graph.inner.successor_nodes(node) {
388            if recurse(graph, node_states, neighbor_index) {
389                node_states[node.0] = State::Included;
390            }
391        }
392
393        // if we didn't find a path to target, then set to excluded
394        if node_states[node.0] == State::Deciding {
395            node_states[node.0] = State::Excluded;
396            false
397        } else {
398            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);
399            true
400        }
401    }
402}
403
404fn filter_edges(graph: &RetainedDepGraph, nodes: &FxIndexSet<DepKind>) -> Vec<(DepKind, DepKind)> {
405    let uniq: FxIndexSet<_> = graph
406        .edges()
407        .into_iter()
408        .map(|(s, t)| (s.kind, t.kind))
409        .filter(|(source, target)| nodes.contains(source) && nodes.contains(target))
410        .collect();
411    uniq.into_iter().collect()
412}