# "Print this variable on panic" annotations

**URL:** <https://internals.rust-lang.org/t/print-this-variable-on-panic-annotations/6150>\
**Category:** libs\
**Created:** [November 1, 2017, 2:27pm UTC](https://internals.rust-lang.org/t/print-this-variable-on-panic-annotations/6150 "2017-11-01T14:27:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![zackw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/zackw/32/2071_2.png) [@zackw](https://internals.rust-lang.org/u/zackw)\
**Post date:** [November 1, 2017, 2:27pm UTC](https://internals.rust-lang.org/t/print-this-variable-on-panic-annotations/6150/1 "2017-11-01T14:27:04Z")

</div>

In complex code where you can’t completely eliminate the possibility of a panic, backtraces can be made significantly more informative by including the values of important local variables. I don’t actually have a good example to hand in Rust, but back in 2006 when I was working on [monotone](https://www.monotone.ca/) (Graydon’s big project _before_ Rust, if you haven’t heard of it; pretty much the Velvet Underground of DVCSes) we made very heavy use of this. A decent self-contained example from that code (C++) is the [normalize\_path](https://code.monotone.ca/p/monotone/source/tree/h:net.venge.monotone/src/paths.cc#L259) function, which begins

```rust
258	static string
259	normalize_path(string const & in)
260	{
261 string inT = in;
262 string leader;
263 MM(inT);

```

`MM(inT)` means “If anything in this function causes it to throw an exception, the error message that eventually gets printed should include the contents of the variable `inT`.” It then goes on to do a bunch of string mangling that could conceivably have a bug that causes it to run off the end, or something like that; if it does, you get to know what input caused the problem. Later in the code you will see uses of an `I(expression)` macro, which is this codebase’s version of `assert!`, and pairs with `MM`.

I think this would be a valuable and easy feature to add to Rust, and the main reason I’m posting about it here instead of just going and hacking it up and submitting a PR is that I don’t have a good _name_ for it. `MM` is much too cryptic, obviously. (It originally comes from [GNU Nana](https://github.com/pjmaker/nana/), which had this entire vocabulary of one- and two-character assertion-related macros.) The implementation of `MM` calls things that are currently tagged to be printed “musings”, which is at least a _word_, but not a terribly clear one. Anyone have a suggestion?

---

<div class="post-metadata">

**Author:** ![mchlrhw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mchlrhw/32/2696_2.png) [@mchlrhw](https://internals.rust-lang.org/u/mchlrhw)\
**Post date:** [November 2, 2017, 9:12am UTC](https://internals.rust-lang.org/t/print-this-variable-on-panic-annotations/6150/2 "2017-11-02T09:12:53Z")

</div>

This feels like you’re trying to float a variable to the top level from somewhere deeper into the program, so `surface(inT)` could work. Another alternative is to think of it as shining a light on something, so `highlight(inT)`; `introspect(inT)` also comes to mind. It’s always difficult to coin new terminology that doesn’t clash with at least one pre-existing domain though, so I fully expect to have stepped on someone’s toes with one of those.

---

<div class="post-metadata">

**Author:** ![Michael-F-Bryan](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/michael-f-bryan/32/2815_2.png) [@Michael-F-Bryan](https://internals.rust-lang.org/u/Michael-F-Bryan)\
**Post date:** [November 2, 2017, 10:53am UTC](https://internals.rust-lang.org/t/print-this-variable-on-panic-annotations/6150/3 "2017-11-02T10:53:44Z")

</div>

I think there’s a way to see if you’re currently panicking. You could couple that with some dummy type who’s drop implementation is simply a check to see if you are panicking and then try to print that string.

It’s probably not the nicest way of doing things, nor will it catch all panics, but it may be good enough. Don’t forget that panicking while you’re already panicking will usually abort!

[(playground)](https://play.rust-lang.org/?gist=55a0c50246be9ee59732e81e8f83219a&version=stable)

---

<div class="post-metadata">

**Author:** ![Ixrec](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ixrec/32/6754_2.png) [@Ixrec](https://internals.rust-lang.org/u/Ixrec)\
**Post date:** [November 2, 2017, 12:37pm UTC](https://internals.rust-lang.org/t/print-this-variable-on-panic-annotations/6150/4 "2017-11-02T12:37:03Z")

</div>

For naming, I’d rather not try to coin any new verbiage for this and instead do something simplistic like `show_in_panics!(foo)` that is at least potentially self-evident.

---

<div class="post-metadata">

**Author:** ![zackw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/zackw/32/2071_2.png) [@zackw](https://internals.rust-lang.org/u/zackw)\
**Post date:** [November 2, 2017, 3:39pm UTC](https://internals.rust-lang.org/t/print-this-variable-on-panic-annotations/6150/5 "2017-11-02T15:39:16Z")

</div>

Your demo made me realize that this idea has a nasty problem with reference lifetimes/borrow conflicts. 🙁

Using lxrec’s name suggestion for now, and imagining your code being used for the underlying implementation, we would have

```rust
fn munge(orig_s: &str) -> String {
   show_on_panic!(orig_s);
   let mut s = orig_s.to_owned();
   show_on_panic!(s);

   // ... stuff which modifies s in place ...

   s
}

```

with `show_on_panic!(s)` expanding to something like

```rust
    let _gensym_0002 = PanicPrint::new(&s);

```

and now we have an object live for the entire function, holding an immutable reference to `s`, which is going to block whatever the rest of the function does to modify `s` in place.

We could use a raw pointer instead, but that would give up lifetime safety as well as mutability safety, and I’m worried that with sufficiently complicated control flow and/or lifetimes we would wind up maybe trying to print an object that’s already been destroyed. Two-phase borrow and NLL both address _related_ problems but I do not know if they address this case.

Maybe this is addressed sufficiently by what you already did to make the ShowOnPanic struct itself be tied to the lifetime of the object? You had

```rust
pub struct PanicPrint<'a, D: Display + ?Sized + 'a> {
    msg: &'a D,
}

```

so the lifetime _of the struct_ is constrained by `'a` even if if `msg` becomes `*const D` instead of `&'a D`, right?

(Also, given the probable use cases, I suspect that should be `D: Debug` but that’s not important right now.)

---

<div class="post-metadata">

**Author:** ![Michael-F-Bryan](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/michael-f-bryan/32/2815_2.png) [@Michael-F-Bryan](https://internals.rust-lang.org/u/Michael-F-Bryan)\
**Post date:** [November 3, 2017, 1:51am UTC](https://internals.rust-lang.org/t/print-this-variable-on-panic-annotations/6150/6 "2017-11-03T01:51:55Z")

</div>

Another alternative is you could store a string representation by either using `format!()` or the `to_string()` method available to anything implementing `Display`. Of course that now means you don't have a zero-cost mechanism for doing something only during a panic...

> [@zackw](#):
>
> (Also, given the probable use cases, I suspect that should be D: Debug but that’s not important right now.)

Good point!

---

<div class="post-metadata">

**Author:** ![kriomant](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kriomant/32/2901_2.png) [@kriomant](https://internals.rust-lang.org/u/kriomant)\
**Post date:** [November 4, 2017, 7:10am UTC](https://internals.rust-lang.org/t/print-this-variable-on-panic-annotations/6150/7 "2017-11-04T07:10:37Z")

</div>

You may find this crate useful: [panic-context](https://crates.io/crates/panic-context)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:29am UTC](https://internals.rust-lang.org/t/print-this-variable-on-panic-annotations/6150/8 "2019-03-25T08:29:11Z")

</div>

Closed on March 25, 2019, 8:29am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
