Test, should_panic: somehow hide stack traces?

I have a test that is marked with

    #[test]
    #[should_panic]

as a result, in the console output, there is a partial stack trace (which my IDE interprets and makes the file names clickable & blue); as a result, it takes me a while to go from

"something went wrong, I got a stack trace" to "oh, I'm supposed to get a stack trace, this is should_panic"

Question: is there some config flag which tells should_panic to also surpress the stack trace output?

You can override the default panic hook inside the function itself to suppress backtrace printing:

std::panic::set_hook(Box::new(drop));
1 Like

I am getting a very weird error:

254 |         std::panic::set_hook(Box::new(drop));
    |                              ^^^^^^^^^^^^^^ one type is more general than the other
    |
    = note: expected type `FnOnce<(&PanicInfo<'_>,)>`
               found type `FnOnce<(&PanicInfo<'_>,)>`

Especially weird as the two types, as far as I can tell, as identical.

1 Like
        let f  = |_: &PanicInfo| {};
        std::panic::set_hook(Box::new(f));

appears to work, so you have definitely solved my problem. However, if anyone knows the answer, I'm still curious about the error above.

Ah, it's because Rust doesn't currently have the ability to convert a function generic over a type to a function generic over a lifetime: drop implements for<T> Fn(T), but Rust doesn't see that as a superset of for<'a, 'b> Fn(&'a PanicInfo<'b>), even though it in theory is. So what Rust is doing is instantiating a drop::<&'1 PanicInfo<'2>> where '1 and '2 are some arbitrarily chosen concrete lifetimes, but that doesn't compile because set_hook requires the function to accept a generic lifetime.

1 Like

might as well do

std::panic::set_hook(Box::new(|_| {}))

then :wink:

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.