Skip to content

Commit 8971f10

Browse files
Vudvudc4rrao
authored andcommitted
feat: added feature coloring error (uncomplete)
Co-authored-by: Henrique Carrão [email protected]
1 parent 4649877 commit 8971f10

File tree

12 files changed

+158
-1
lines changed

12 files changed

+158
-1
lines changed

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ declare_features! (
213213
(internal, negative_bounds, "1.71.0", None),
214214
/// Allows using `#[omit_gdb_pretty_printer_section]`.
215215
(internal, omit_gdb_pretty_printer_section, "1.5.0", None),
216+
/// Highlights the error message of pannic calls.
217+
(unstable, panic_color_errors,"CURRENT_RUSTC_VERSION", None),
216218
/// Set the maximum pattern complexity allowed (not limited by default).
217219
(internal, pattern_complexity, "1.78.0", None),
218220
/// Allows using pattern types.

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,7 @@ symbols! {
13251325
panic_abort,
13261326
panic_bounds_check,
13271327
panic_cannot_unwind,
1328+
panic_color_errors,
13281329
panic_const_add_overflow,
13291330
panic_const_async_fn_resumed,
13301331
panic_const_async_fn_resumed_panic,

library/std/src/panic.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ pub use crate::panicking::{set_hook, take_hook};
4242
#[unstable(feature = "panic_update_hook", issue = "92649")]
4343
pub use crate::panicking::update_hook;
4444

45+
#[unstable(feature = "panic_color_errors", issue = "none")]
46+
pub use crate::panicking::highlight_errors;
47+
4548
#[stable(feature = "panic_hooks", since = "1.10.0")]
4649
pub use core::panic::{Location, PanicInfo};
4750

library/std/src/panicking.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ use crate::sys::stdio::panic_output;
2323
use crate::sys_common::backtrace;
2424
use crate::thread;
2525

26+
use crate::io::{self, IsTerminal};
27+
2628
#[cfg(not(test))]
2729
use crate::io::try_set_output_capture;
2830
// make sure to use the stderr output configured
@@ -233,6 +235,32 @@ where
233235
*hook = Hook::Custom(Box::new(move |info| hook_fn(&prev, info)));
234236
}
235237

238+
#[allow(missing_docs)]
239+
#[unstable(feature = "panic_color_errors", issue = "none")]
240+
pub fn highlight_errors(set_color: bool){
241+
if set_color {
242+
crate::env::set_var("RUST_COLOR_ERRORS", "1");
243+
} else {
244+
crate::env::set_var("RUST_COLOR_ERRORS", "0");
245+
}
246+
}
247+
248+
#[unstable(feature = "panic_color_errors", issue = "none")]
249+
fn format_error_message (msg: &str) -> String {
250+
match crate::env::var_os("RUST_COLOR_ERRORS") {
251+
Some(x) if x == "1" => format!("\x1b[31m{msg}\x1b[0m"),
252+
None => {
253+
if io::stderr().is_terminal() {
254+
format!("\x1b[31m{msg}\x1b[0m")
255+
}
256+
else {
257+
msg.to_string()
258+
}
259+
},
260+
_ => msg.to_string()
261+
}
262+
}
263+
236264
/// The default panic handler.
237265
fn default_hook(info: &PanicInfo<'_>) {
238266
// If this is a double panic, make sure that we print a backtrace
@@ -259,7 +287,13 @@ fn default_hook(info: &PanicInfo<'_>) {
259287
let name = thread.as_ref().and_then(|t| t.name()).unwrap_or("<unnamed>");
260288

261289
let write = |err: &mut dyn crate::io::Write| {
262-
let _ = writeln!(err, "thread '{name}' panicked at {location}:\n{msg}");
290+
let msg_fmt = if cfg!(feature="panic_color_errors") {
291+
format_error_message(msg)
292+
} else {
293+
msg.to_string()
294+
};
295+
296+
let _ = writeln!(err, "thread '{name}' panicked at {location}:\n{msg_fmt}");
263297

264298
static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
265299

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use std::panic;
2+
3+
fn main(){
4+
panic::highlight_errors(true);
5+
//~^ ERROR E0658
6+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0658]: use of unstable library feature 'panic_color_errors'
2+
--> $DIR/feature-gate-panic_color_errors.rs:4:5
3+
|
4+
LL | panic::highlight_errors(true);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: add `#![feature(panic_color_errors)]` to the crate attributes to enable
8+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
9+
10+
error: aborting due to 1 previous error
11+
12+
For more information about this error, try `rustc --explain E0658`.

tests/ui/panic_color_errors/in1.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![feature(panic_color_errors)]
2+
3+
static mut I: [u64; 2] = [0; 2];
4+
5+
fn foo(x: u64) {
6+
if x == 0 {
7+
unsafe{
8+
let j = 12;
9+
I[j] = 0;
10+
}
11+
} else {
12+
foo(x-1);
13+
}
14+
}
15+
16+
fn main() {
17+
std::panic::highlight_errors(true);
18+
foo(100);
19+
}

tests/ui/panic_color_errors/in2.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(panic_color_errors)]
2+
3+
fn foo(x: u64) {
4+
if x == 0 {
5+
panic!("Oops sometging went wrong");
6+
} else {
7+
foo(x-1);
8+
}
9+
}
10+
11+
fn main() {
12+
std::panic::highlight_errors(true);
13+
foo(100);
14+
}

tests/ui/panic_color_errors/in3.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![feature(panic_color_errors)]
2+
3+
static mut I: [u64; 2] = [0; 2];
4+
5+
fn foo(x: u64) {
6+
if x == 0 {
7+
unsafe{
8+
let j = 12;
9+
I[j] = 0;
10+
}
11+
} else {
12+
foo(x-1);
13+
}
14+
}
15+
16+
fn main() {
17+
std::panic::highlight_errors(false);
18+
foo(100);
19+
}

tests/ui/panic_color_errors/in4.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(panic_color_errors)]
2+
3+
fn foo(x: u64) {
4+
if x == 0 {
5+
panic!("Oops sometging went wrong");
6+
} else {
7+
foo(x-1);
8+
}
9+
}
10+
11+
fn main() {
12+
std::panic::highlight_errors(false);
13+
foo(100);
14+
}

tests/ui/panic_color_errors/in5.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![feature(panic_color_errors)]
2+
3+
static mut I: [u64; 2] = [0; 2];
4+
5+
fn foo(x: u64) {
6+
if x == 0 {
7+
unsafe{
8+
let j = 12;
9+
I[j] = 0;
10+
}
11+
} else {
12+
foo(x-1);
13+
}
14+
}
15+
16+
fn main() {
17+
std::panic::highlight_errors(false);
18+
foo(100);
19+
}

tests/ui/panic_color_errors/in6.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(panic_color_errors)]
2+
3+
fn foo(x: u64) {
4+
if x == 0 {
5+
panic!("Oops sometging went wrong");
6+
} else {
7+
foo(x-1);
8+
}
9+
}
10+
11+
fn main() {
12+
std::panic::highlight_errors(false);
13+
foo(100);
14+
}

0 commit comments

Comments
 (0)