Skip to content

Commit 4be2f54

Browse files
authored
Explain the internal #[rustc_*] TEST attributes used for debugging and inside tests (#2046)
1 parent 918486d commit 4be2f54

File tree

2 files changed

+81
-30
lines changed

2 files changed

+81
-30
lines changed

src/compiler-debugging.md

+68-30
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,6 @@ By default, if rustc encounters an Internal Compiler Error (ICE) it will dump th
5252
ICE file within the current working directory named `rustc-ice-<timestamp>-<pid>.txt`. If this is
5353
not desirable, you can prevent the ICE file from being created with `RUSTC_ICE=0`.
5454

55-
## `-Z` flags
56-
57-
The compiler has a bunch of `-Z` flags. These are unstable flags that are only
58-
enabled on nightly. Many of them are useful for debugging. To get a full listing
59-
of `-Z` flags, use `-Z help`.
60-
61-
One useful flag is `-Z verbose-internals`, which generally enables printing more
62-
info that could be useful for debugging.
63-
6455
## Getting a backtrace
6556
[getting-a-backtrace]: #getting-a-backtrace
6657

@@ -109,7 +100,18 @@ stack backtrace:
109100
at /home/user/rust/compiler/rustc_driver/src/lib.rs:253
110101
```
111102

112-
## Getting a backtrace for errors
103+
## `-Z` flags
104+
105+
The compiler has a bunch of `-Z *` flags. These are unstable flags that are only
106+
enabled on nightly. Many of them are useful for debugging. To get a full listing
107+
of `-Z` flags, use `-Z help`.
108+
109+
One useful flag is `-Z verbose-internals`, which generally enables printing more
110+
info that could be useful for debugging.
111+
112+
Right below you can find elaborate explainers on a selected few.
113+
114+
### Getting a backtrace for errors
113115
[getting-a-backtrace-for-errors]: #getting-a-backtrace-for-errors
114116

115117
If you want to get a backtrace to the point where the compiler emits an
@@ -186,14 +188,14 @@ stack backtrace:
186188

187189
Cool, now I have a backtrace for the error!
188190

189-
## Debugging delayed bugs
191+
### Debugging delayed bugs
190192

191193
The `-Z eagerly-emit-delayed-bugs` option makes it easy to debug delayed bugs.
192194
It turns them into normal errors, i.e. makes them visible. This can be used in
193195
combination with `-Z treat-err-as-bug` to stop at a particular delayed bug and
194196
get a backtrace.
195197

196-
## Getting the error creation location
198+
### Getting the error creation location
197199

198200
`-Z track-diagnostics` can help figure out where errors are emitted. It uses `#[track_caller]`
199201
for this and prints its location alongside the error:
@@ -235,21 +237,6 @@ The compiler uses the [`tracing`] crate for logging.
235237

236238
For details see [the guide section on tracing](./tracing.md)
237239

238-
## Formatting Graphviz output (.dot files)
239-
[formatting-graphviz-output]: #formatting-graphviz-output
240-
241-
Some compiler options for debugging specific features yield graphviz graphs -
242-
e.g. the `#[rustc_mir(borrowck_graphviz_postflow="suffix.dot")]` attribute
243-
dumps various borrow-checker dataflow graphs.
244-
245-
These all produce `.dot` files. To view these files, install graphviz (e.g.
246-
`apt-get install graphviz`) and then run the following commands:
247-
248-
```bash
249-
$ dot -T pdf maybe_init_suffix.dot > maybe_init_suffix.pdf
250-
$ firefox maybe_init_suffix.pdf # Or your favorite pdf viewer
251-
```
252-
253240
## Narrowing (Bisecting) Regressions
254241

255242
The [cargo-bisect-rustc][bisect] tool can be used as a quick and easy way to
@@ -273,10 +260,61 @@ without doing the build yourself.
273260

274261
[rtim]: https://github.com/kennytm/rustup-toolchain-install-master
275262

276-
## Debugging type layouts
263+
## `#[rustc_*]` TEST attributes
264+
265+
The compiler defines a whole lot of internal (perma-unstable) attributes some of which are useful
266+
for debugging by dumping extra compiler-internal information. These are prefixed with `rustc_` and
267+
are gated behind the internal feature `rustc_attrs` (enabled via e.g. `#![feature(rustc_attrs)]`).
268+
269+
For a complete and up to date list, see [`builtin_attrs`]. More specifically, the ones marked `TEST`.
270+
Here are some notable ones:
271+
272+
| Attribute | Description |
273+
|----------------|-------------|
274+
| `rustc_def_path` | Dumps the [`def_path_str`] of an item. |
275+
| `rustc_dump_def_parents` | Dumps the chain of `DefId` parents of certain definitions. |
276+
| `rustc_dump_item_bounds` | Dumps the [`item_bounds`] of an item. |
277+
| `rustc_dump_predicates` | Dumps the [`predicates_of`] an item. |
278+
| `rustc_dump_vtable` | |
279+
| `rustc_hidden_type_of_opaques` | Dumps the [hidden type of all opaque types][opaq] in the crate. |
280+
| `rustc_layout` | [See this section](#debugging-type-layouts). |
281+
| `rustc_object_lifetime_default` | Dumps the [object lifetime defaults] of an item. |
282+
| `rustc_outlives` | Dumps implied bounds of an item. More precisely, the [`inferred_outlives_of`] an item. |
283+
| `rustc_regions` | Dumps NLL closure region requirements. |
284+
| `rustc_symbol_name` | Dumps the mangled & demangled [`symbol_name`] of an item. |
285+
| `rustc_variances` | Dumps the [variances] of an item. |
286+
287+
Right below you can find elaborate explainers on a selected few.
288+
289+
[`builtin_attrs`]: https://github.com/rust-lang/rust/blob/master/compiler/rustc_feature/src/builtin_attrs.rs
290+
[`def_path_str`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/context/struct.TyCtxt.html#method.def_path_str
291+
[`inferred_outlives_of`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/context/struct.TyCtxt.html#method.inferred_outlives_of
292+
[`item_bounds`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/context/struct.TyCtxt.html#method.item_bounds
293+
[`predicates_of`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/context/struct.TyCtxt.html#method.predicates_of
294+
[`symbol_name`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/context/struct.TyCtxt.html#method.symbol_name
295+
[object lifetime defaults]: https://doc.rust-lang.org/reference/lifetime-elision.html#default-trait-object-lifetimes
296+
[opaq]: ./opaque-types-impl-trait-inference.md
297+
[variances]: ./variance.md
298+
299+
### Formatting Graphviz output (.dot files)
300+
[formatting-graphviz-output]: #formatting-graphviz-output
301+
302+
Some compiler options for debugging specific features yield graphviz graphs -
303+
e.g. the `#[rustc_mir(borrowck_graphviz_postflow="suffix.dot")]` attribute
304+
dumps various borrow-checker dataflow graphs.
305+
306+
These all produce `.dot` files. To view these files, install graphviz (e.g.
307+
`apt-get install graphviz`) and then run the following commands:
308+
309+
```bash
310+
$ dot -T pdf maybe_init_suffix.dot > maybe_init_suffix.pdf
311+
$ firefox maybe_init_suffix.pdf # Or your favorite pdf viewer
312+
```
313+
314+
### Debugging type layouts
277315

278-
The (permanently) unstable `#[rustc_layout]` attribute can be used to dump
279-
the [`Layout`] of the type it is attached to. For example:
316+
The internal attribute `#[rustc_layout]` can be used to dump the [`Layout`] of
317+
the type it is attached to. For example:
280318

281319
```rust
282320
#![feature(rustc_attrs)]

src/tests/ui.md

+13
Original file line numberDiff line numberDiff line change
@@ -530,3 +530,16 @@ run something like the following to generate the alternate stderr file:
530530
```
531531

532532
Currently none of the compare modes are checked in CI for UI tests.
533+
534+
## `rustc_*` TEST attributes
535+
536+
The compiler defines several perma-unstable `#[rustc_*]` attributes gated behind the internal feature
537+
`rustc_attrs` that dump extra compiler-internal information. See the analogous subsection in
538+
[compiler debugging] for more details.
539+
540+
They can be used in tests to more precisely, legibly and easily test internal compiler state in cases
541+
where it would otherwise be very hard to do the same with "user-facing" Rust alone. Indeed, one could
542+
say that this slightly abuses the term "UI" (*user* interfacing) and turns such UI tests from black-box
543+
tests into white-box ones. Use them carefully and sparingly.
544+
545+
[compiler debugging]: ../compiler-debugging.md#rustc_-attributes

0 commit comments

Comments
 (0)