Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make
PanicInfo::message
infallible #115561Make
PanicInfo::message
infallible #115561Changes from 2 commits
9ad005b
df19b29
6042f52
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be good if this also attempts to downcast to
String
, just like the default panic hook does:rust/library/std/src/panicking.rs
Line 262 in 8769c26
Then
panic_any(some_string)
, andpanic!(format!(..))
in Rust ≤2018, will also work.(For a
String
you'll have to useformat_args!("{}", s)
though, rather thannew_v1
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can't be done, as we'd be borrowing from
msg
while also using it as the payload (and thus moving out of it).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. It is possible, since the payload isn't actually consumed until after the PanicInfo is gone (at the end of rust_panic_with_hook), but it'd require refactoring some of the code in this file quite a bit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is the
payload.get()
method call, not the move.BoxMeUp::get
must be mutable in order for the#[panic_handler]
to be able to lazily allocate aString
by formatting theMessage
from thePanicInfo
that gets passed to it.It's all a bit roundabout, but if we're willing to break some use cases that assume there's a
String
in the payload forpanic!("{}", 42)
, then that does seem fixable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The specific problematic impl is https://github.com/rust-lang/rust/blob/1b9567fcaa842b1028f9913838c4b70952470767/library/std/src/panicking.rs#L585
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, but .get() is always called when constructing the PanicInfo that's passed to the panic hook, so at that point the String will have been allocated and can be borrowed by format_args!().
So,
rust_panic_with_hook
could just .get() the payload first and then try downcasting it to&'static str
andString
to create aformat_args!("{s}")
to make thePanicInfo
with. (That also avoids formattingmessage
twice when someone uses.message()
in a panic hook. (Since at that point the message has already been formatted into aString
.))There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anyway, a lot of the annoyances here come from PanicInfo (the argument to #[panic_handler]) and PanicInfo (the argument to the panic hook) being the same type. I'm hoping we might be able to do something about that: #115974 That'd make this all a lot easier. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default panic hook prints
"Box<dyn Any>"
in that case. I'm not particularly attached to that specific message, but we should probably keep those the same.rust/library/std/src/panicking.rs
Line 264 in 8769c26
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or, even better: the default panic hook should not do any downcasting and instead use .message(), now that it works in all cases.