From c7a1ba3305b04a9b06c34ebfdbb8787f3f9df7e8 Mon Sep 17 00:00:00 2001 From: Alexandre Vudvud Date: Sat, 25 May 2024 16:57:52 +0100 Subject: [PATCH 1/3] feat: added feature coloring error (uncomplete) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Henrique Carrão --- compiler/rustc_feature/src/unstable.rs | 2 ++ compiler/rustc_span/src/symbol.rs | 1 + library/std/src/panic.rs | 3 ++ library/std/src/panicking.rs | 36 ++++++++++++++++++- .../feature-gate-panic_color_errors.rs | 6 ++++ .../feature-gate-panic_color_errors.stderr | 12 +++++++ tests/ui/panic_color_errors/in1.rs | 19 ++++++++++ tests/ui/panic_color_errors/in2.rs | 14 ++++++++ tests/ui/panic_color_errors/in3.rs | 19 ++++++++++ tests/ui/panic_color_errors/in4.rs | 14 ++++++++ tests/ui/panic_color_errors/in5.rs | 19 ++++++++++ tests/ui/panic_color_errors/in6.rs | 14 ++++++++ 12 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 tests/ui/feature-gates/feature-gate-panic_color_errors.rs create mode 100644 tests/ui/feature-gates/feature-gate-panic_color_errors.stderr create mode 100644 tests/ui/panic_color_errors/in1.rs create mode 100644 tests/ui/panic_color_errors/in2.rs create mode 100644 tests/ui/panic_color_errors/in3.rs create mode 100644 tests/ui/panic_color_errors/in4.rs create mode 100644 tests/ui/panic_color_errors/in5.rs create mode 100644 tests/ui/panic_color_errors/in6.rs diff --git a/compiler/rustc_feature/src/unstable.rs b/compiler/rustc_feature/src/unstable.rs index dc4807bab2d3d..0b12d25d6ef73 100644 --- a/compiler/rustc_feature/src/unstable.rs +++ b/compiler/rustc_feature/src/unstable.rs @@ -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. diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index ace4dff46aa0a..79c3aab314b94 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1325,6 +1325,7 @@ symbols! { panic_abort, panic_bounds_check, panic_cannot_unwind, + panic_color_errors, panic_const_add_overflow, panic_const_async_fn_resumed, panic_const_async_fn_resumed_panic, diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index e63b46ab70548..5431c53d6dbd5 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -42,6 +42,9 @@ pub use crate::panicking::{set_hook, take_hook}; #[unstable(feature = "panic_update_hook", issue = "92649")] pub use crate::panicking::update_hook; +#[unstable(feature = "panic_color_errors", issue = "none")] +pub use crate::panicking::highlight_errors; + #[stable(feature = "panic_hooks", since = "1.10.0")] pub use core::panic::{Location, PanicInfo}; diff --git a/library/std/src/panicking.rs b/library/std/src/panicking.rs index 5699937cdb49b..181fa7f88c9fc 100644 --- a/library/std/src/panicking.rs +++ b/library/std/src/panicking.rs @@ -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,32 @@ where *hook = Hook::Custom(Box::new(move |info| hook_fn(&prev, info))); } +#[allow(missing_docs)] +#[unstable(feature = "panic_color_errors", issue = "none")] +pub fn highlight_errors(set_color: bool){ + if set_color { + crate::env::set_var("RUST_COLOR_ERRORS", "1"); + } else { + crate::env::set_var("RUST_COLOR_ERRORS", "0"); + } +} + +#[unstable(feature = "panic_color_errors", issue = "none")] +fn format_error_message (msg: &str) -> String { + match crate::env::var_os("RUST_COLOR_ERRORS") { + Some(x) if x == "1" => format!("\x1b[31m{msg}\x1b[0m"), + None => { + if io::stderr().is_terminal() { + format!("\x1b[31m{msg}\x1b[0m") + } + else { + msg.to_string() + } + }, + _ => msg.to_string() + } +} + /// The default panic handler. fn default_hook(info: &PanicInfo<'_>) { // If this is a double panic, make sure that we print a backtrace @@ -259,7 +287,13 @@ fn default_hook(info: &PanicInfo<'_>) { let name = thread.as_ref().and_then(|t| t.name()).unwrap_or(""); let write = |err: &mut dyn crate::io::Write| { - let _ = writeln!(err, "thread '{name}' panicked at {location}:\n{msg}"); + let msg_fmt = if cfg!(feature="panic_color_errors") { + format_error_message(msg) + } else { + msg.to_string() + }; + + let _ = writeln!(err, "thread '{name}' panicked at {location}:\n{msg_fmt}"); static FIRST_PANIC: AtomicBool = AtomicBool::new(true); diff --git a/tests/ui/feature-gates/feature-gate-panic_color_errors.rs b/tests/ui/feature-gates/feature-gate-panic_color_errors.rs new file mode 100644 index 0000000000000..0547e405bbf6d --- /dev/null +++ b/tests/ui/feature-gates/feature-gate-panic_color_errors.rs @@ -0,0 +1,6 @@ +use std::panic; + +fn main(){ + panic::highlight_errors(true); + //~^ ERROR E0658 +} diff --git a/tests/ui/feature-gates/feature-gate-panic_color_errors.stderr b/tests/ui/feature-gates/feature-gate-panic_color_errors.stderr new file mode 100644 index 0000000000000..f541c71115499 --- /dev/null +++ b/tests/ui/feature-gates/feature-gate-panic_color_errors.stderr @@ -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(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`. diff --git a/tests/ui/panic_color_errors/in1.rs b/tests/ui/panic_color_errors/in1.rs new file mode 100644 index 0000000000000..76fa0dff2c746 --- /dev/null +++ b/tests/ui/panic_color_errors/in1.rs @@ -0,0 +1,19 @@ +#![feature(panic_color_errors)] + +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(true); + foo(100); +} \ No newline at end of file diff --git a/tests/ui/panic_color_errors/in2.rs b/tests/ui/panic_color_errors/in2.rs new file mode 100644 index 0000000000000..16c55b72406c1 --- /dev/null +++ b/tests/ui/panic_color_errors/in2.rs @@ -0,0 +1,14 @@ +#![feature(panic_color_errors)] + +fn foo(x: u64) { + if x == 0 { + panic!("Oops sometging went wrong"); + } else { + foo(x-1); + } +} + +fn main() { + std::panic::highlight_errors(true); + foo(100); +} \ No newline at end of file diff --git a/tests/ui/panic_color_errors/in3.rs b/tests/ui/panic_color_errors/in3.rs new file mode 100644 index 0000000000000..9e3d59649e0a1 --- /dev/null +++ b/tests/ui/panic_color_errors/in3.rs @@ -0,0 +1,19 @@ +#![feature(panic_color_errors)] + +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(false); + foo(100); +} \ No newline at end of file diff --git a/tests/ui/panic_color_errors/in4.rs b/tests/ui/panic_color_errors/in4.rs new file mode 100644 index 0000000000000..f82f23a5cc9a2 --- /dev/null +++ b/tests/ui/panic_color_errors/in4.rs @@ -0,0 +1,14 @@ +#![feature(panic_color_errors)] + +fn foo(x: u64) { + if x == 0 { + panic!("Oops sometging went wrong"); + } else { + foo(x-1); + } +} + +fn main() { + std::panic::highlight_errors(false); + foo(100); +} \ No newline at end of file diff --git a/tests/ui/panic_color_errors/in5.rs b/tests/ui/panic_color_errors/in5.rs new file mode 100644 index 0000000000000..9e3d59649e0a1 --- /dev/null +++ b/tests/ui/panic_color_errors/in5.rs @@ -0,0 +1,19 @@ +#![feature(panic_color_errors)] + +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(false); + foo(100); +} \ No newline at end of file diff --git a/tests/ui/panic_color_errors/in6.rs b/tests/ui/panic_color_errors/in6.rs new file mode 100644 index 0000000000000..f82f23a5cc9a2 --- /dev/null +++ b/tests/ui/panic_color_errors/in6.rs @@ -0,0 +1,14 @@ +#![feature(panic_color_errors)] + +fn foo(x: u64) { + if x == 0 { + panic!("Oops sometging went wrong"); + } else { + foo(x-1); + } +} + +fn main() { + std::panic::highlight_errors(false); + foo(100); +} \ No newline at end of file From ba68d4918890c9ca39c645663a3d4a08b2a99cc6 Mon Sep 17 00:00:00 2001 From: Alexandre Vudvud Date: Sat, 25 May 2024 20:37:03 +0100 Subject: [PATCH 2/3] fix:implemented feature MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Henrique Carrão --- library/std/src/panicking.rs | 29 ++++++++++++------- .../library-features/panic-color-errors.md | 7 +++++ .../feature-gate-panic_color_errors.rs | 2 +- .../feature-gate-panic_color_errors.stderr | 2 +- ...ith_backtrace_highlight_errors_default.rs} | 8 +++-- ...ktrace_highlight_errors_default.run.stderr | 3 ++ ...ng_with_backtrace_highlight_errors_off.rs} | 8 +++-- ..._backtrace_highlight_errors_off.run.stderr | 3 ++ ...ing_with_backtrace_highlight_errors_on.rs} | 8 +++-- ...h_backtrace_highlight_errors_on.run.stderr | 3 ++ ...ror_panicking_highlight_errors_default.rs} | 8 +++-- ...icking_highlight_errors_default.run.stderr | 3 ++ ...d_error_panicking_highlight_errors_off.rs} | 8 +++-- ..._panicking_highlight_errors_off.run.stderr | 3 ++ ...ed_error_panicking_highlight_errors_on.rs} | 8 +++-- ...r_panicking_highlight_errors_on.run.stderr | 3 ++ 16 files changed, 81 insertions(+), 25 deletions(-) create mode 100644 src/doc/unstable-book/src/library-features/panic-color-errors.md rename tests/ui/{panic_color_errors/in6.rs => panics/panic_color_errors/explicit_panicking_with_backtrace_highlight_errors_default.rs} (62%) create mode 100644 tests/ui/panics/panic_color_errors/explicit_panicking_with_backtrace_highlight_errors_default.run.stderr rename tests/ui/{panic_color_errors/in4.rs => panics/panic_color_errors/explicit_panicking_with_backtrace_highlight_errors_off.rs} (60%) create mode 100644 tests/ui/panics/panic_color_errors/explicit_panicking_with_backtrace_highlight_errors_off.run.stderr rename tests/ui/{panic_color_errors/in2.rs => panics/panic_color_errors/explicit_panicking_with_backtrace_highlight_errors_on.rs} (60%) create mode 100644 tests/ui/panics/panic_color_errors/explicit_panicking_with_backtrace_highlight_errors_on.run.stderr rename tests/ui/{panic_color_errors/in1.rs => panics/panic_color_errors/forced_error_panicking_highlight_errors_default.rs} (68%) create mode 100644 tests/ui/panics/panic_color_errors/forced_error_panicking_highlight_errors_default.run.stderr rename tests/ui/{panic_color_errors/in3.rs => panics/panic_color_errors/forced_error_panicking_highlight_errors_off.rs} (67%) create mode 100644 tests/ui/panics/panic_color_errors/forced_error_panicking_highlight_errors_off.run.stderr rename tests/ui/{panic_color_errors/in5.rs => panics/panic_color_errors/forced_error_panicking_highlight_errors_on.rs} (67%) create mode 100644 tests/ui/panics/panic_color_errors/forced_error_panicking_highlight_errors_on.run.stderr diff --git a/library/std/src/panicking.rs b/library/std/src/panicking.rs index 181fa7f88c9fc..77b54a317c672 100644 --- a/library/std/src/panicking.rs +++ b/library/std/src/panicking.rs @@ -235,16 +235,26 @@ where *hook = Hook::Custom(Box::new(move |info| hook_fn(&prev, info))); } -#[allow(missing_docs)] +/// 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) +/// accordingly, influencing downstream formatting functions to produce colorful +/// error messages. #[unstable(feature = "panic_color_errors", issue = "none")] -pub fn highlight_errors(set_color: bool){ - if set_color { - crate::env::set_var("RUST_COLOR_ERRORS", "1"); - } else { - crate::env::set_var("RUST_COLOR_ERRORS", "0"); +pub fn highlight_errors(set_color: Option){ + match set_color { + 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")} } } +/// Alters the format of error messages by adding color codes to them. +/// It reads the RUST_COLOR_ERRORS environment variable to determine whether to +/// apply color formatting. If enabled, it formats the error message with ANSI +/// escape codes to display it in red, indicating an error state. The default +/// behavior is to apply highlighting when printing to a terminal, and no +/// highlighting otherwise. #[unstable(feature = "panic_color_errors", issue = "none")] fn format_error_message (msg: &str) -> String { match crate::env::var_os("RUST_COLOR_ERRORS") { @@ -287,11 +297,8 @@ fn default_hook(info: &PanicInfo<'_>) { let name = thread.as_ref().and_then(|t| t.name()).unwrap_or(""); let write = |err: &mut dyn crate::io::Write| { - let msg_fmt = if cfg!(feature="panic_color_errors") { - format_error_message(msg) - } else { - msg.to_string() - }; + // 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. + let msg_fmt = format_error_message(msg); let _ = writeln!(err, "thread '{name}' panicked at {location}:\n{msg_fmt}"); diff --git a/src/doc/unstable-book/src/library-features/panic-color-errors.md b/src/doc/unstable-book/src/library-features/panic-color-errors.md new file mode 100644 index 0000000000000..a0c549f2c1b69 --- /dev/null +++ b/src/doc/unstable-book/src/library-features/panic-color-errors.md @@ -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 diff --git a/tests/ui/feature-gates/feature-gate-panic_color_errors.rs b/tests/ui/feature-gates/feature-gate-panic_color_errors.rs index 0547e405bbf6d..6c51303731168 100644 --- a/tests/ui/feature-gates/feature-gate-panic_color_errors.rs +++ b/tests/ui/feature-gates/feature-gate-panic_color_errors.rs @@ -1,6 +1,6 @@ use std::panic; fn main(){ - panic::highlight_errors(true); + panic::highlight_errors(Some(true)); //~^ ERROR E0658 } diff --git a/tests/ui/feature-gates/feature-gate-panic_color_errors.stderr b/tests/ui/feature-gates/feature-gate-panic_color_errors.stderr index f541c71115499..1b4e7dd3ccf5e 100644 --- a/tests/ui/feature-gates/feature-gate-panic_color_errors.stderr +++ b/tests/ui/feature-gates/feature-gate-panic_color_errors.stderr @@ -1,7 +1,7 @@ error[E0658]: use of unstable library feature 'panic_color_errors' --> $DIR/feature-gate-panic_color_errors.rs:4:5 | -LL | panic::highlight_errors(true); +LL | panic::highlight_errors(Some(true)); | ^^^^^^^^^^^^^^^^^^^^^^^ | = help: add `#![feature(panic_color_errors)]` to the crate attributes to enable diff --git a/tests/ui/panic_color_errors/in6.rs b/tests/ui/panics/panic_color_errors/explicit_panicking_with_backtrace_highlight_errors_default.rs similarity index 62% rename from tests/ui/panic_color_errors/in6.rs rename to tests/ui/panics/panic_color_errors/explicit_panicking_with_backtrace_highlight_errors_default.rs index f82f23a5cc9a2..8deee33c42e88 100644 --- a/tests/ui/panic_color_errors/in6.rs +++ b/tests/ui/panics/panic_color_errors/explicit_panicking_with_backtrace_highlight_errors_default.rs @@ -1,5 +1,9 @@ #![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"); @@ -9,6 +13,6 @@ fn foo(x: u64) { } fn main() { - std::panic::highlight_errors(false); + std::panic::highlight_errors(None); foo(100); -} \ No newline at end of file +} diff --git a/tests/ui/panics/panic_color_errors/explicit_panicking_with_backtrace_highlight_errors_default.run.stderr b/tests/ui/panics/panic_color_errors/explicit_panicking_with_backtrace_highlight_errors_default.run.stderr new file mode 100644 index 0000000000000..11bdcf8c2966b --- /dev/null +++ b/tests/ui/panics/panic_color_errors/explicit_panicking_with_backtrace_highlight_errors_default.run.stderr @@ -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 diff --git a/tests/ui/panic_color_errors/in4.rs b/tests/ui/panics/panic_color_errors/explicit_panicking_with_backtrace_highlight_errors_off.rs similarity index 60% rename from tests/ui/panic_color_errors/in4.rs rename to tests/ui/panics/panic_color_errors/explicit_panicking_with_backtrace_highlight_errors_off.rs index f82f23a5cc9a2..e384fc52071e9 100644 --- a/tests/ui/panic_color_errors/in4.rs +++ b/tests/ui/panics/panic_color_errors/explicit_panicking_with_backtrace_highlight_errors_off.rs @@ -1,5 +1,9 @@ #![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"); @@ -9,6 +13,6 @@ fn foo(x: u64) { } fn main() { - std::panic::highlight_errors(false); + std::panic::highlight_errors(Some(false)); foo(100); -} \ No newline at end of file +} diff --git a/tests/ui/panics/panic_color_errors/explicit_panicking_with_backtrace_highlight_errors_off.run.stderr b/tests/ui/panics/panic_color_errors/explicit_panicking_with_backtrace_highlight_errors_off.run.stderr new file mode 100644 index 0000000000000..b7f3a0aaa0b1c --- /dev/null +++ b/tests/ui/panics/panic_color_errors/explicit_panicking_with_backtrace_highlight_errors_off.run.stderr @@ -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 diff --git a/tests/ui/panic_color_errors/in2.rs b/tests/ui/panics/panic_color_errors/explicit_panicking_with_backtrace_highlight_errors_on.rs similarity index 60% rename from tests/ui/panic_color_errors/in2.rs rename to tests/ui/panics/panic_color_errors/explicit_panicking_with_backtrace_highlight_errors_on.rs index 16c55b72406c1..a88dc43cf5460 100644 --- a/tests/ui/panic_color_errors/in2.rs +++ b/tests/ui/panics/panic_color_errors/explicit_panicking_with_backtrace_highlight_errors_on.rs @@ -1,5 +1,9 @@ #![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"); @@ -9,6 +13,6 @@ fn foo(x: u64) { } fn main() { - std::panic::highlight_errors(true); + std::panic::highlight_errors(Some(true)); foo(100); -} \ No newline at end of file +} diff --git a/tests/ui/panics/panic_color_errors/explicit_panicking_with_backtrace_highlight_errors_on.run.stderr b/tests/ui/panics/panic_color_errors/explicit_panicking_with_backtrace_highlight_errors_on.run.stderr new file mode 100644 index 0000000000000..b0ab6e2c2589a --- /dev/null +++ b/tests/ui/panics/panic_color_errors/explicit_panicking_with_backtrace_highlight_errors_on.run.stderr @@ -0,0 +1,3 @@ +thread 'main' panicked at $DIR/explicit_panicking_with_backtrace_highlight_errors_on.rs:9:9: +Oops sometging went wrong +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace diff --git a/tests/ui/panic_color_errors/in1.rs b/tests/ui/panics/panic_color_errors/forced_error_panicking_highlight_errors_default.rs similarity index 68% rename from tests/ui/panic_color_errors/in1.rs rename to tests/ui/panics/panic_color_errors/forced_error_panicking_highlight_errors_default.rs index 76fa0dff2c746..6b811c9183b04 100644 --- a/tests/ui/panic_color_errors/in1.rs +++ b/tests/ui/panics/panic_color_errors/forced_error_panicking_highlight_errors_default.rs @@ -1,5 +1,9 @@ #![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) { @@ -14,6 +18,6 @@ fn foo(x: u64) { } fn main() { - std::panic::highlight_errors(true); + std::panic::highlight_errors(None); foo(100); -} \ No newline at end of file +} diff --git a/tests/ui/panics/panic_color_errors/forced_error_panicking_highlight_errors_default.run.stderr b/tests/ui/panics/panic_color_errors/forced_error_panicking_highlight_errors_default.run.stderr new file mode 100644 index 0000000000000..1dabd42d9c91e --- /dev/null +++ b/tests/ui/panics/panic_color_errors/forced_error_panicking_highlight_errors_default.run.stderr @@ -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 diff --git a/tests/ui/panic_color_errors/in3.rs b/tests/ui/panics/panic_color_errors/forced_error_panicking_highlight_errors_off.rs similarity index 67% rename from tests/ui/panic_color_errors/in3.rs rename to tests/ui/panics/panic_color_errors/forced_error_panicking_highlight_errors_off.rs index 9e3d59649e0a1..194781faf0b5b 100644 --- a/tests/ui/panic_color_errors/in3.rs +++ b/tests/ui/panics/panic_color_errors/forced_error_panicking_highlight_errors_off.rs @@ -1,5 +1,9 @@ #![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) { @@ -14,6 +18,6 @@ fn foo(x: u64) { } fn main() { - std::panic::highlight_errors(false); + std::panic::highlight_errors(Some(false)); foo(100); -} \ No newline at end of file +} diff --git a/tests/ui/panics/panic_color_errors/forced_error_panicking_highlight_errors_off.run.stderr b/tests/ui/panics/panic_color_errors/forced_error_panicking_highlight_errors_off.run.stderr new file mode 100644 index 0000000000000..768e24a402feb --- /dev/null +++ b/tests/ui/panics/panic_color_errors/forced_error_panicking_highlight_errors_off.run.stderr @@ -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 diff --git a/tests/ui/panic_color_errors/in5.rs b/tests/ui/panics/panic_color_errors/forced_error_panicking_highlight_errors_on.rs similarity index 67% rename from tests/ui/panic_color_errors/in5.rs rename to tests/ui/panics/panic_color_errors/forced_error_panicking_highlight_errors_on.rs index 9e3d59649e0a1..2b1b772916acc 100644 --- a/tests/ui/panic_color_errors/in5.rs +++ b/tests/ui/panics/panic_color_errors/forced_error_panicking_highlight_errors_on.rs @@ -1,5 +1,9 @@ #![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) { @@ -14,6 +18,6 @@ fn foo(x: u64) { } fn main() { - std::panic::highlight_errors(false); + std::panic::highlight_errors(Some(true)); foo(100); -} \ No newline at end of file +} diff --git a/tests/ui/panics/panic_color_errors/forced_error_panicking_highlight_errors_on.run.stderr b/tests/ui/panics/panic_color_errors/forced_error_panicking_highlight_errors_on.run.stderr new file mode 100644 index 0000000000000..527029f2e151a --- /dev/null +++ b/tests/ui/panics/panic_color_errors/forced_error_panicking_highlight_errors_on.run.stderr @@ -0,0 +1,3 @@ +thread 'main' panicked at $DIR/forced_error_panicking_highlight_errors_on.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 From d7b6b1fcd9ead002f9be4a7a9e3e7318a50436d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrique=20Carr=C3=A3o?= Date: Tue, 4 Jun 2024 00:16:34 +0100 Subject: [PATCH 3/3] fix: some typos and useless code for library features --- compiler/rustc_feature/src/unstable.rs | 2 -- library/std/src/panicking.rs | 2 +- .../unstable-book/src/library-features/panic-color-errors.md | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_feature/src/unstable.rs b/compiler/rustc_feature/src/unstable.rs index 0b12d25d6ef73..dc4807bab2d3d 100644 --- a/compiler/rustc_feature/src/unstable.rs +++ b/compiler/rustc_feature/src/unstable.rs @@ -213,8 +213,6 @@ 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. diff --git a/library/std/src/panicking.rs b/library/std/src/panicking.rs index 77b54a317c672..2a9142a7dbeaf 100644 --- a/library/std/src/panicking.rs +++ b/library/std/src/panicking.rs @@ -237,7 +237,7 @@ where /// 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 removes an environment variable (RUST_COLOR_ERRORS) /// accordingly, influencing downstream formatting functions to produce colorful /// error messages. #[unstable(feature = "panic_color_errors", issue = "none")] diff --git a/src/doc/unstable-book/src/library-features/panic-color-errors.md b/src/doc/unstable-book/src/library-features/panic-color-errors.md index a0c549f2c1b69..97acc2bd2951b 100644 --- a/src/doc/unstable-book/src/library-features/panic-color-errors.md +++ b/src/doc/unstable-book/src/library-features/panic-color-errors.md @@ -4,4 +4,4 @@ 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 to highlight the errors when there's a terminal.