From 776f114aaeead78d0a27dccc713ba739a16c7e75 Mon Sep 17 00:00:00 2001 From: mejrs <> Date: Sat, 5 Nov 2022 18:32:06 +0100 Subject: [PATCH 1/2] Add -Ztrack-diagnostics information --- src/compiler-debugging.md | 34 ++++++++++++++++++++++++++++++++++ src/diagnostics.md | 4 +++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/compiler-debugging.md b/src/compiler-debugging.md index 35458b55c..4d04b352b 100644 --- a/src/compiler-debugging.md +++ b/src/compiler-debugging.md @@ -185,6 +185,40 @@ stack backtrace: Cool, now I have a backtrace for the error! +## Getting the the error creation location + +`-Z track-diagnostics` can help figure out where errors are emitted. It uses `#[track_caller]` +for this and prints its location alongside the error: + +```bash +$ RUST_BACKTRACE=1 rustc +stage1 error.rs -Z track-diagnostics +error[E0277]: cannot add `()` to `{integer}` + --> src\error.rs:2:7 + | +2 | 1 + (); + | ^ no implementation for `{integer} + ()` +-Ztrack-diagnostics: created at compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs:638:39 + | + = help: the trait `Add<()>` is not implemented for `{integer}` + = help: the following other types implement trait `Add`: + <&'a f32 as Add> + <&'a f64 as Add> + <&'a i128 as Add> + <&'a i16 as Add> + <&'a i32 as Add> + <&'a i64 as Add> + <&'a i8 as Add> + <&'a isize as Add> + and 48 others + +For more information about this error, try `rustc --explain E0277`. +``` + +This is similar but different to `-Z treat-err-as-bug`: +- it will print the locations for all errors emitted +- it does not require a compiler built with debug symbols +- you don't have to read through a big stack trace. + ## Getting logging output The compiler uses the [`tracing`] crate for logging. diff --git a/src/diagnostics.md b/src/diagnostics.md index e1d5fbe1a..59cda3e67 100644 --- a/src/diagnostics.md +++ b/src/diagnostics.md @@ -269,7 +269,7 @@ book][rustc-lint-levels] and the [reference][reference-diagnostics]. ### Finding the source of errors -There are two main ways to find where a given error is emitted: +There are three main ways to find where a given error is emitted: - `grep` for either a sub-part of the error message/label or error code. This 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: - The _construction_ of the error is far away from where it is _emitted_, a problem similar to the one we faced with the `grep` approach. In some cases, we buffer multiple errors in order to emit them in order. +- Invoking `rustc` with the nightly-only flag `-Z track-diagnostics` will print error creation + locations alongside the error. The regular development practices apply: judicious use of `debug!()` statements and use of a debugger to trigger break points in order to figure out in what From f403eeba52000dc497941ae0cc8e26c0cf76cd0f Mon Sep 17 00:00:00 2001 From: mejrs <> Date: Sat, 17 Dec 2022 22:51:42 +0100 Subject: [PATCH 2/2] Apply feedback --- src/compiler-debugging.md | 6 +++--- src/diagnostics.md | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/compiler-debugging.md b/src/compiler-debugging.md index 4d04b352b..eac9aeb6d 100644 --- a/src/compiler-debugging.md +++ b/src/compiler-debugging.md @@ -128,7 +128,7 @@ fn main() { } ``` -```bash +``` $ rustc +stage1 error.rs error[E0277]: cannot add `()` to `{integer}` --> error.rs:2:7 @@ -143,7 +143,7 @@ error: aborting due to previous error Now, where does the error above come from? -```bash +``` $ RUST_BACKTRACE=1 rustc +stage1 error.rs -Z treat-err-as-bug error[E0277]: the trait bound `{integer}: std::ops::Add<()>` is not satisfied --> error.rs:2:7 @@ -190,7 +190,7 @@ Cool, now I have a backtrace for the error! `-Z track-diagnostics` can help figure out where errors are emitted. It uses `#[track_caller]` for this and prints its location alongside the error: -```bash +``` $ RUST_BACKTRACE=1 rustc +stage1 error.rs -Z track-diagnostics error[E0277]: cannot add `()` to `{integer}` --> src\error.rs:2:7 diff --git a/src/diagnostics.md b/src/diagnostics.md index 59cda3e67..b50b7bd18 100644 --- a/src/diagnostics.md +++ b/src/diagnostics.md @@ -287,7 +287,7 @@ There are three main ways to find where a given error is emitted: - The _construction_ of the error is far away from where it is _emitted_, a problem similar to the one we faced with the `grep` approach. In some cases, we buffer multiple errors in order to emit them in order. -- Invoking `rustc` with the nightly-only flag `-Z track-diagnostics` will print error creation +- Invoking `rustc` with `-Z track-diagnostics` will print error creation locations alongside the error. The regular development practices apply: judicious use of `debug!()` statements