rust-lang / rust-lang/rfcs

Make the `impl Debug for Any (+ Send)` (and `Box` forms) more useful

Open
#1,389 12 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

T-libs
Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

Belated follow-up to the discussion on https://github.com/rust-lang/rust/issues/27719

Currently, the Debug impl for Box<Any> is almost useless. It just prints "Box<Any>".

One of the main uses of Box<Any> in the stdlib is to carry the message of a panic!() invocation back to the parent, out of thread::spawn().join() or thread::catch_panic(). In this case, the Box<Any> could be one of three possibilities:

  • &'static str, if panic!() was called with a string literal and no formatting arguments.
  • String, if panic!() was called with formatting arguments.
  • Any other Send type which was passed to panic!() besides a string literal.

While the first case is technically a subset of the third, I count it as its own item because it's a common pattern when panic!() wants to give an error message but doesn't have any dynamic data to add to it.

Given the comparatively negligible overhead of downcasting, I think impl Debug for Box<Any> should test the first two cases before printing its uselessly generic message:

impl Debug for Box<Any> {
    fn fmt(&self, fmt: &mut Formatter) -> fmt::Result<()>, {
        if let Ok(str_slice) = self.downcast_ref::<&'static str>() {
            fmt.pad(str_slice);
        } else if let Ok(string) = self.downcast_ref::<String>() {
            fmt.pad(string);
        } else {
            fmt.pad("Box<Any>");
        } 
    }
}

Of course, this applies to impl Debug for Any (+ Send) as well.

While this is technically a breaking change since it modifies stable behavior, I doubt anyone was relying on the exact value printed by this impl, given its completely uninformative message.

In fact, this should massively reduce the occurrence of the uninformative Thread ### panicked at Box`` message in ICE reports and StackOverflow questions. It appears this specialization is already being done: https://github.com/rust-lang/rust/blob/master/src/libstd/panicking.rs#L33

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the existing specialization in src/libstd/panicking.rs and the discussion linked from the issue. Check whether Debug implementations for Any, Send, and Box forms already handle string slices and String values. Done means the relevant implementations produce useful messages for those cases while retaining a generic fallback for other types.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.