Home Explore Blog CI



rustc

1st chunk of `src/incrcomp-debugging.md`
1fdd995b90447bd6d62acf584ce48e1000db4adbbdf551b400000001000008aa
# Debugging and testing dependencies

## Testing the dependency graph

There are various ways to write tests against the dependency graph.  The
simplest mechanisms are the `#[rustc_if_this_changed]` and
`#[rustc_then_this_would_need]` annotations. These are used in [ui] tests to test
whether the expected set of paths exist in the dependency graph.


As an example, see [`tests/ui/dep-graph/dep-graph-caller-callee.rs`], or the
tests below.

```rust,ignore
#[rustc_if_this_changed]
fn foo() { }

#[rustc_then_this_would_need(TypeckTables)] //~ ERROR OK
fn bar() { foo(); }
```

This should be read as
> If this (`foo`) is changed, then this (i.e. `bar`)'s TypeckTables would need to be changed.

Technically, what occurs is that the test is expected to emit the string "OK" on
stderr, associated to this line.

You could also add the lines

```rust,ignore
#[rustc_then_this_would_need(TypeckTables)] //~ ERROR no path
fn baz() { }
```

Whose meaning is
> If `foo` is changed, then `baz`'s TypeckTables does not need to be changed.
> The macro must emit an error, and the error message must contains "no path".

Recall that the `//~ ERROR OK` is a comment from the point of view of the Rust
code we test, but is meaningful from the point of view of the test itself.

## Debugging the dependency graph

### Dumping the graph

The compiler is also capable of dumping the dependency graph for your
debugging pleasure. To do so, pass the `-Z dump-dep-graph` flag. The
graph will be dumped to `dep_graph.{txt,dot}` in the current
directory.  You can override the filename with the `RUST_DEP_GRAPH`
environment variable.

Frequently, though, the full dep graph is quite overwhelming and not
particularly helpful. Therefore, the compiler also allows you to filter
the graph. You can filter in three ways:

1. All edges originating in a particular set of nodes (usually a single node).
2. All edges reaching a particular set of nodes.
3. All edges that lie between given start and end nodes.

To filter, use the `RUST_DEP_GRAPH_FILTER` environment variable, which should
look like one of the following:

```text
source_filter     // nodes originating from source_filter
-> target_filter  // nodes that can reach target_filter

Title: Debugging and Testing the Dependency Graph in Rust Compiler
Summary
This section details how to test and debug the dependency graph within the Rust compiler. It covers using `#[rustc_if_this_changed]` and `#[rustc_then_this_would_need]` annotations in ui tests to verify the existence of expected paths in the dependency graph. Additionally, it explains how to dump the dependency graph using the `-Z dump-dep-graph` flag and filter it using the `RUST_DEP_GRAPH_FILTER` environment variable for more focused debugging.