# Code using """pointers""" is inherently difficult/impossible to debug, because Debug on pointer types (Arc, references, etc) doesn't include criticial information

**URL:** <https://internals.rust-lang.org/t/code-using-pointers-is-inherently-difficult-impossible-to-debug-because-debug-on-pointer-types-arc-references-etc-doesnt-include-criticial-information/15324>\
**Category:** libs\
**Created:** [September 11, 2021, 9:40pm UTC](https://internals.rust-lang.org/t/code-using-pointers-is-inherently-difficult-impossible-to-debug-because-debug-on-pointer-types-arc-references-etc-doesnt-include-criticial-information/15324 "2021-09-11T21:40:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Soni](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Soni](https://internals.rust-lang.org/u/Soni)\
**Post date:** [September 11, 2021, 9:40pm UTC](https://internals.rust-lang.org/t/code-using-pointers-is-inherently-difficult-impossible-to-debug-because-debug-on-pointer-types-arc-references-etc-doesnt-include-criticial-information/15324/1 "2021-09-11T21:40:59Z")

</div>

Pet peeves: `dbg!(an_arc)` doesn't show that it's an `Arc`, nor which instance of an `Arc` it is, `dbg!(an_str)` doesn't show that it's a `&str`, nor which instance of `&str` it is, etc. There are a few more cases like this in the stdlib. Is there any particular reason why the stdlib is unhelpful about debugging these types? These `Debug` impls that relate to references/smart pointers should include the pointer value while at it, so you can tell if you got two "identical" values mixed up somewhere. This is important both where interior mutability is concerned, as interior mutability is opaque to debugging, as well as where unsafe code is concerned, because pointer operations between different allocations are UB (particularly relevant with slices).

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [September 11, 2021, 9:55pm UTC](https://internals.rust-lang.org/t/code-using-pointers-is-inherently-difficult-impossible-to-debug-because-debug-on-pointer-types-arc-references-etc-doesnt-include-criticial-information/15324/2 "2021-09-11T21:55:58Z")

</div>

The general assumption is that you know the type when you're looking at the `Debug` formatting of some value. This is, ultimately, just an arbitrary decision made to reduce noise in the `{:?}` debug repr.

If you want the typename of the root object, you can easily just make your own `dbg!` (even shadowing the implicit prelude one) that also prints `std::any::type_name`. For non-root objects, you should have enough type context to know if something is boxed.

---

<div class="post-metadata">

**Author:** ![Volker\_Weissmann](https://avatars.discourse-cdn.com/v4/letter/v/c2a13f/32.png) [@Volker\_Weissmann](https://internals.rust-lang.org/u/Volker_Weissmann)\
**Post date:** [September 11, 2021, 10:23pm UTC](https://internals.rust-lang.org/t/code-using-pointers-is-inherently-difficult-impossible-to-debug-because-debug-on-pointer-types-arc-references-etc-doesnt-include-criticial-information/15324/3 "2021-09-11T22:23:12Z")

</div>

Note that `rust-analyzer` exists. It can show you the type of variables inside your editor.

---

<div class="post-metadata">

**Author:** ![ayosec](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ayosec/32/3970_2.png) [@ayosec](https://internals.rust-lang.org/u/ayosec)\
**Post date:** [September 12, 2021, 3:16am UTC](https://internals.rust-lang.org/t/code-using-pointers-is-inherently-difficult-impossible-to-debug-because-debug-on-pointer-types-arc-references-etc-doesnt-include-criticial-information/15324/4 "2021-09-12T03:16:30Z")

</div>

You can implement that macro with [`std::any::type_name`](https://doc.rust-lang.org/stable/std/any/fn.type_name.html).

For example:

```rust
macro_rules! type_dbg {
    ($val:expr) => {
        match $val {
            tmp => {
                // Copy of `std::any::type_name_of_val`, which is nightly-only.
                fn type_name_of_val<T: ?Sized>(_val: &T) -> &'static str {
                    std::any::type_name::<T>()
                }
                
                eprintln!(
                    "[{}:{}] {} : {} = {:#?}",
                    file!(),
                    line!(),
                    stringify!($val),
                    type_name_of_val(&tmp),
                    &tmp
                );

                tmp
            }
        }
    };
}

```

[Link to the playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=aa2f8b74085798cf6a298bf29d486051).

---

<div class="post-metadata">

**Author:** ![Soni](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Soni](https://internals.rust-lang.org/u/Soni)\
**Post date:** [September 12, 2021, 5:42pm UTC](https://internals.rust-lang.org/t/code-using-pointers-is-inherently-difficult-impossible-to-debug-because-debug-on-pointer-types-arc-references-etc-doesnt-include-criticial-information/15324/5 "2021-09-12T17:42:45Z")

</div>

Sorry, we may have misrepresented our point.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [September 12, 2021, 8:18pm UTC](https://internals.rust-lang.org/t/code-using-pointers-is-inherently-difficult-impossible-to-debug-because-debug-on-pointer-types-arc-references-etc-doesnt-include-criticial-information/15324/6 "2021-09-12T20:18:10Z")

</div>

If the pointer value matters to debuggging, you're expected to use [`std::fmt::Pointer`](https://doc.rust-lang.org/std/fmt/trait.Pointer.html), `{:p}`.

[For example:](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f6978ef15642aed668e23aaf0dfdd03b)

```rust
macro_rules! dbg_ptr {
    ($val:expr) => {
        match $val {
            ref tmp => {
                // Copy of `std::any::type_name_of_val`, which is nightly-only.
                fn type_name_of_val<T: ?Sized>(_val: &T) -> &'static str {
                    std::any::type_name::<T>()
                }
                
                eprintln!(
                    "[{file}:{line}] {expr} : {ty} = [{ptr:p}] {val:#?}",
                    file=file!(),
                    line=line!(),
                    expr=stringify!($val),
                    ty=type_name_of_val(tmp),
                    ptr=*tmp,
                    val=tmp,
                );

                tmp
            }
        }
    }
}

```

> [@CAD97](#):
>
> This is, ultimately, just an arbitrary decision made to reduce noise in the `{:?}` debug repr.

In the common case, pointer identity doesn't matter for correctness (and where it does, the borrow checker handles it) so it's fine to leave it out and opt-in.

---

<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:** [December 11, 2021, 8:19pm UTC](https://internals.rust-lang.org/t/code-using-pointers-is-inherently-difficult-impossible-to-debug-because-debug-on-pointer-types-arc-references-etc-doesnt-include-criticial-information/15324/7 "2021-12-11T20:19:11Z")

</div>

Closed on December 11, 2021, 8:19pm UTC

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