1 //! Panic support in the standard library.
3 #![unstable(feature = "core_panic_info",
4 reason = "newly available in libcore",
10 /// A struct providing information about a panic.
12 /// `PanicInfo` structure is passed to a panic hook set by the [`set_hook`]
15 /// [`set_hook`]: ../../std/panic/fn.set_hook.html
22 /// panic::set_hook(Box::new(|panic_info| {
23 /// if let Some(s) = panic_info.payload().downcast_ref::<&str>() {
24 /// println!("panic occurred: {:?}", s);
26 /// println!("panic occurred");
30 /// panic!("Normal panic");
32 #[lang = "panic_info"]
33 #[stable(feature = "panic_hooks", since = "1.10.0")]
35 pub struct PanicInfo<'a> {
36 payload: &'a (dyn Any + Send),
37 message: Option<&'a fmt::Arguments<'a>>,
38 location: &'a Location<'a>,
41 impl<'a> PanicInfo<'a> {
42 #![unstable(feature = "panic_internals",
43 reason = "internal details of the implementation of the `panic!` \
48 pub fn internal_constructor(
49 message: Option<&'a fmt::Arguments<'a>>,
50 location: &'a Location<'a>,
62 pub fn set_payload(&mut self, info: &'a (dyn Any + Send)) {
66 /// Returns the payload associated with the panic.
68 /// This will commonly, but not always, be a `&'static str` or [`String`].
70 /// [`String`]: ../../std/string/struct.String.html
77 /// panic::set_hook(Box::new(|panic_info| {
78 /// println!("panic occurred: {:?}", panic_info.payload().downcast_ref::<&str>().unwrap());
81 /// panic!("Normal panic");
83 #[stable(feature = "panic_hooks", since = "1.10.0")]
84 pub fn payload(&self) -> &(dyn Any + Send) {
88 /// If the `panic!` macro from the `core` crate (not from `std`)
89 /// was used with a formatting string and some additional arguments,
90 /// returns that message ready to be used for example with [`fmt::write`]
92 /// [`fmt::write`]: ../fmt/fn.write.html
93 #[unstable(feature = "panic_info_message", issue = "44489")]
94 pub fn message(&self) -> Option<&fmt::Arguments<'_>> {
98 /// Returns information about the location from which the panic originated,
101 /// This method will currently always return [`Some`], but this may change
102 /// in future versions.
104 /// [`Some`]: ../../std/option/enum.Option.html#variant.Some
111 /// panic::set_hook(Box::new(|panic_info| {
112 /// if let Some(location) = panic_info.location() {
113 /// println!("panic occurred in file '{}' at line {}", location.file(),
114 /// location.line());
116 /// println!("panic occurred but can't get location information...");
120 /// panic!("Normal panic");
122 #[stable(feature = "panic_hooks", since = "1.10.0")]
123 pub fn location(&self) -> Option<&Location<'_>> {
124 // NOTE: If this is changed to sometimes return None,
125 // deal with that case in std::panicking::default_hook and std::panicking::begin_panic_fmt.
130 #[stable(feature = "panic_hook_display", since = "1.26.0")]
131 impl fmt::Display for PanicInfo<'_> {
132 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
133 formatter.write_str("panicked at ")?;
134 if let Some(message) = self.message {
135 write!(formatter, "'{}', ", message)?
136 } else if let Some(payload) = self.payload.downcast_ref::<&'static str>() {
137 write!(formatter, "'{}', ", payload)?
139 // NOTE: we cannot use downcast_ref::<String>() here
140 // since String is not available in libcore!
141 // The payload is a String when `std::panic!` is called with multiple arguments,
142 // but in that case the message is also available.
144 self.location.fmt(formatter)
148 /// A struct containing information about the location of a panic.
150 /// This structure is created by the [`location`] method of [`PanicInfo`].
152 /// [`location`]: ../../std/panic/struct.PanicInfo.html#method.location
153 /// [`PanicInfo`]: ../../std/panic/struct.PanicInfo.html
160 /// panic::set_hook(Box::new(|panic_info| {
161 /// if let Some(location) = panic_info.location() {
162 /// println!("panic occurred in file '{}' at line {}", location.file(), location.line());
164 /// println!("panic occurred but can't get location information...");
168 /// panic!("Normal panic");
170 #[cfg_attr(not(bootstrap), lang = "panic_location")]
172 #[stable(feature = "panic_hooks", since = "1.10.0")]
173 pub struct Location<'a> {
179 impl<'a> Location<'a> {
180 #![unstable(feature = "panic_internals",
181 reason = "internal details of the implementation of the `panic!` \
185 pub const fn internal_constructor(file: &'a str, line: u32, col: u32) -> Self {
186 Location { file, line, col }
189 /// Returns the name of the source file from which the panic originated.
196 /// panic::set_hook(Box::new(|panic_info| {
197 /// if let Some(location) = panic_info.location() {
198 /// println!("panic occurred in file '{}'", location.file());
200 /// println!("panic occurred but can't get location information...");
204 /// panic!("Normal panic");
206 #[stable(feature = "panic_hooks", since = "1.10.0")]
207 pub fn file(&self) -> &str {
211 /// Returns the line number from which the panic originated.
218 /// panic::set_hook(Box::new(|panic_info| {
219 /// if let Some(location) = panic_info.location() {
220 /// println!("panic occurred at line {}", location.line());
222 /// println!("panic occurred but can't get location information...");
226 /// panic!("Normal panic");
228 #[stable(feature = "panic_hooks", since = "1.10.0")]
229 pub fn line(&self) -> u32 {
233 /// Returns the column from which the panic originated.
240 /// panic::set_hook(Box::new(|panic_info| {
241 /// if let Some(location) = panic_info.location() {
242 /// println!("panic occurred at column {}", location.column());
244 /// println!("panic occurred but can't get location information...");
248 /// panic!("Normal panic");
250 #[stable(feature = "panic_col", since = "1.25.0")]
251 pub fn column(&self) -> u32 {
256 #[stable(feature = "panic_hook_display", since = "1.26.0")]
257 impl fmt::Display for Location<'_> {
258 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
259 write!(formatter, "{}:{}:{}", self.file, self.line, self.col)
263 /// An internal trait used by libstd to pass data from libstd to `panic_unwind`
264 /// and other panic runtimes. Not intended to be stabilized any time soon, do
266 #[unstable(feature = "std_internals", issue = "0")]
268 pub unsafe trait BoxMeUp {
269 fn box_me_up(&mut self) -> *mut (dyn Any + Send);
270 fn get(&mut self) -> &(dyn Any + Send);