Skip to content

Commit a213de4

Browse files
mejrstshepang
mejrs
authored andcommitted
Add -Ztrack-diagnostics information
1 parent 58077bf commit a213de4

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/compiler-debugging.md

+34
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,40 @@ stack backtrace:
185185

186186
Cool, now I have a backtrace for the error!
187187

188+
## Getting the the error creation location
189+
190+
`-Z track-diagnostics` can help figure out where errors are emitted. It uses `#[track_caller]`
191+
for this and prints its location alongside the error:
192+
193+
```bash
194+
$ RUST_BACKTRACE=1 rustc +stage1 error.rs -Z track-diagnostics
195+
error[E0277]: cannot add `()` to `{integer}`
196+
--> src\error.rs:2:7
197+
|
198+
2 | 1 + ();
199+
| ^ no implementation for `{integer} + ()`
200+
-Ztrack-diagnostics: created at compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs:638:39
201+
|
202+
= help: the trait `Add<()>` is not implemented for `{integer}`
203+
= help: the following other types implement trait `Add<Rhs>`:
204+
<&'a f32 as Add<f32>>
205+
<&'a f64 as Add<f64>>
206+
<&'a i128 as Add<i128>>
207+
<&'a i16 as Add<i16>>
208+
<&'a i32 as Add<i32>>
209+
<&'a i64 as Add<i64>>
210+
<&'a i8 as Add<i8>>
211+
<&'a isize as Add<isize>>
212+
and 48 others
213+
214+
For more information about this error, try `rustc --explain E0277`.
215+
```
216+
217+
This is similar but different to `-Z treat-err-as-bug`:
218+
- it will print the locations for all errors emitted
219+
- it does not require a compiler built with debug symbols
220+
- you don't have to read through a big stack trace.
221+
188222
## Getting logging output
189223
190224
The compiler uses the [`tracing`] crate for logging.

src/diagnostics.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ book][rustc-lint-levels] and the [reference][reference-diagnostics].
269269

270270
### Finding the source of errors
271271

272-
There are two main ways to find where a given error is emitted:
272+
There are three main ways to find where a given error is emitted:
273273

274274
- `grep` for either a sub-part of the error message/label or error code. This
275275
usually works well and is straightforward, but there are some cases where
@@ -287,6 +287,8 @@ There are two main ways to find where a given error is emitted:
287287
- The _construction_ of the error is far away from where it is _emitted_,
288288
a problem similar to the one we faced with the `grep` approach.
289289
In some cases, we buffer multiple errors in order to emit them in order.
290+
- Invoking `rustc` with the nightly-only flag `-Z track-diagnostics` will print error creation
291+
locations alongside the error.
290292

291293
The regular development practices apply: judicious use of `debug!()` statements
292294
and use of a debugger to trigger break points in order to figure out in what

0 commit comments

Comments
 (0)