-
Notifications
You must be signed in to change notification settings - Fork 13.8k
New feature panic color errors #125614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New feature panic color errors #125614
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -213,6 +213,8 @@ declare_features! ( | |
(internal, negative_bounds, "1.71.0", None), | ||
/// Allows using `#[omit_gdb_pretty_printer_section]`. | ||
(internal, omit_gdb_pretty_printer_section, "1.5.0", None), | ||
/// Highlights the error message of pannic calls. | ||
(unstable, panic_color_errors,"CURRENT_RUSTC_VERSION", None), | ||
|
||
/// Set the maximum pattern complexity allowed (not limited by default). | ||
(internal, pattern_complexity, "1.78.0", None), | ||
/// Allows using pattern types. | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -23,6 +23,8 @@ use crate::sys::stdio::panic_output; | |||||||||||||
use crate::sys_common::backtrace; | ||||||||||||||
use crate::thread; | ||||||||||||||
|
||||||||||||||
use crate::io::{self, IsTerminal}; | ||||||||||||||
|
||||||||||||||
#[cfg(not(test))] | ||||||||||||||
use crate::io::try_set_output_capture; | ||||||||||||||
// make sure to use the stderr output configured | ||||||||||||||
|
@@ -233,6 +235,42 @@ where | |||||||||||||
*hook = Hook::Custom(Box::new(move |info| hook_fn(&prev, info))); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// Adjusts the visual representation of panic messages by enabling or | ||||||||||||||
/// disabling color highlighting, or leaving it with a default behaviour. This | ||||||||||||||
/// function sets, unsets or remove an environment variable (RUST_COLOR_ERRORS) | ||||||||||||||
|
/// function sets, unsets or remove an environment variable (RUST_COLOR_ERRORS) | |
/// function sets, unsets or removes an environment variable (RUST_COLOR_ERRORS) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does CI not fail on this formatting?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's wrong here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anomalous formatting.
Some(true) => {crate::env::set_var("RUST_COLOR_ERRORS", "1")} | |
Some(false) => {crate::env::set_var("RUST_COLOR_ERRORS", "0")} | |
_ => {crate::env::remove_var("RUST_COLOR_ERRORS")} | |
Some(true) => crate::env::set_var("RUST_COLOR_ERRORS", "1"), | |
Some(false) => crate::env::set_var("RUST_COLOR_ERRORS", "0"), | |
_ => crate::env::remove_var("RUST_COLOR_ERRORS"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both set_var
and remove_var
are unsafe fn
because they update the environment in an unsynchronized way, but this function is marked as safe. We cannot accept this into the standard library.
This requires an API design that does not depend on racily setting the environment variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should cache this instead of checking it every time, as we do with most env vars.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not need to allocate to function. It is desirable if backtraces minimize the number of allocations they perform, even with std enabled. It should instead write to a formatter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// We should check if the feature is active and only then procede to format the message, but we dont know how to do it for now. | |
// We should check if the feature is active and only then proceed to format the message, but we dont know how to do it for now. |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,7 @@ | ||||||
# `panic_color_errors` | ||||||
|
||||||
This feature has no tracking issue yet. | ||||||
|
||||||
------------------------ | ||||||
|
||||||
With this feature enabled, error messages generated during panics are highlighted for improved visibility and quick identification. By employing color highlighting, developers can easily distinguish error messages from other output, making debugging more efficient and intuitive. This is optional and can be toggled on or off using a dedicated function. The default behaviour is highlight the errors when there's a terminal | ||||||
|
With this feature enabled, error messages generated during panics are highlighted for improved visibility and quick identification. By employing color highlighting, developers can easily distinguish error messages from other output, making debugging more efficient and intuitive. This is optional and can be toggled on or off using a dedicated function. The default behaviour is highlight the errors when there's a terminal | |
With this feature enabled, error messages generated during panics are highlighted for improved visibility and quick identification. By employing color highlighting, developers can easily distinguish error messages from other output, making debugging more efficient and intuitive. This is optional and can be toggled on or off using a dedicated function. The default behaviour is to highlight the errors when there's a terminal |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use std::panic; | ||
|
||
fn main(){ | ||
panic::highlight_errors(Some(true)); | ||
//~^ ERROR E0658 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
error[E0658]: use of unstable library feature 'panic_color_errors' | ||
--> $DIR/feature-gate-panic_color_errors.rs:4:5 | ||
| | ||
LL | panic::highlight_errors(Some(true)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: add `#![feature(panic_color_errors)]` to the crate attributes to enable | ||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0658`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#![feature(panic_color_errors)] | ||
|
||
//@ run-fail | ||
//@ check-run-results | ||
//@ exec-env:RUST_BACKTRACE=0 | ||
|
||
fn foo(x: u64) { | ||
if x == 0 { | ||
panic!("Oops sometging went wrong"); | ||
} else { | ||
foo(x-1); | ||
} | ||
} | ||
|
||
fn main() { | ||
std::panic::highlight_errors(None); | ||
foo(100); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
thread 'main' panicked at $DIR/explicit_panicking_with_backtrace_highlight_errors_default.rs:9:9: | ||
Oops sometging went wrong | ||
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,18 @@ | ||||||
#![feature(panic_color_errors)] | ||||||
|
||||||
//@ run-fail | ||||||
//@ check-run-results | ||||||
//@ exec-env:RUST_BACKTRACE=0 | ||||||
|
||||||
fn foo(x: u64) { | ||||||
if x == 0 { | ||||||
panic!("Oops sometging went wrong"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} else { | ||||||
foo(x-1); | ||||||
} | ||||||
} | ||||||
|
||||||
fn main() { | ||||||
std::panic::highlight_errors(Some(false)); | ||||||
foo(100); | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
thread 'main' panicked at $DIR/explicit_panicking_with_backtrace_highlight_errors_off.rs:9:9: | ||
Oops sometging went wrong | ||
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,18 @@ | ||||||
#![feature(panic_color_errors)] | ||||||
|
||||||
//@ run-fail | ||||||
//@ check-run-results | ||||||
//@ exec-env:RUST_BACKTRACE=0 | ||||||
|
||||||
fn foo(x: u64) { | ||||||
if x == 0 { | ||||||
panic!("Oops sometging went wrong"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} else { | ||||||
foo(x-1); | ||||||
} | ||||||
} | ||||||
|
||||||
fn main() { | ||||||
std::panic::highlight_errors(Some(true)); | ||||||
foo(100); | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
thread 'main' panicked at $DIR/explicit_panicking_with_backtrace_highlight_errors_on.rs:9:9: | ||
[31mOops sometging went wrong[0m | ||
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#![feature(panic_color_errors)] | ||
|
||
//@ run-fail | ||
//@ check-run-results | ||
//@ exec-env:RUST_BACKTRACE=0 | ||
|
||
static mut I: [u64; 2] = [0; 2]; | ||
|
||
fn foo(x: u64) { | ||
if x == 0 { | ||
unsafe{ | ||
let j = 12; | ||
I[j] = 0; | ||
} | ||
} else { | ||
foo(x-1); | ||
} | ||
} | ||
|
||
fn main() { | ||
std::panic::highlight_errors(None); | ||
foo(100); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
thread 'main' panicked at $DIR/forced_error_panicking_highlight_errors_default.rs:13:13: | ||
index out of bounds: the len is 2 but the index is 12 | ||
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#![feature(panic_color_errors)] | ||
|
||
//@ run-fail | ||
//@ check-run-results | ||
//@ exec-env:RUST_BACKTRACE=0 | ||
|
||
static mut I: [u64; 2] = [0; 2]; | ||
|
||
fn foo(x: u64) { | ||
if x == 0 { | ||
unsafe{ | ||
let j = 12; | ||
I[j] = 0; | ||
} | ||
} else { | ||
foo(x-1); | ||
} | ||
} | ||
|
||
fn main() { | ||
std::panic::highlight_errors(Some(false)); | ||
foo(100); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
thread 'main' panicked at $DIR/forced_error_panicking_highlight_errors_off.rs:13:13: | ||
index out of bounds: the len is 2 but the index is 12 | ||
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#![feature(panic_color_errors)] | ||
|
||
//@ run-fail | ||
//@ check-run-results | ||
//@ exec-env:RUST_BACKTRACE=0 | ||
|
||
static mut I: [u64; 2] = [0; 2]; | ||
|
||
fn foo(x: u64) { | ||
if x == 0 { | ||
unsafe{ | ||
let j = 12; | ||
I[j] = 0; | ||
} | ||
} else { | ||
foo(x-1); | ||
} | ||
} | ||
|
||
fn main() { | ||
std::panic::highlight_errors(Some(true)); | ||
foo(100); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
thread 'main' panicked at $DIR/forced_error_panicking_highlight_errors_on.rs:13:13: | ||
[31mindex out of bounds: the len is 2 but the index is 12[0m | ||
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.