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: 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(message: Option<&'a fmt::Arguments<'a>>,
49 location: Location<'a>)
52 PanicInfo { payload: &NoPayload, location, message }
57 pub fn set_payload(&mut self, info: &'a (dyn Any + Send)) {
61 /// Returns the payload associated with the panic.
63 /// This will commonly, but not always, be a `&'static str` or [`String`].
65 /// [`String`]: ../../std/string/struct.String.html
72 /// panic::set_hook(Box::new(|panic_info| {
73 /// println!("panic occurred: {:?}", panic_info.payload().downcast_ref::<&str>().unwrap());
76 /// panic!("Normal panic");
78 #[stable(feature = "panic_hooks", since = "1.10.0")]
79 pub fn payload(&self) -> &(dyn Any + Send) {
83 /// If the `panic!` macro from the `core` crate (not from `std`)
84 /// was used with a formatting string and some additional arguments,
85 /// returns that message ready to be used for example with [`fmt::write`]
87 /// [`fmt::write`]: ../fmt/fn.write.html
88 #[unstable(feature = "panic_info_message", issue = "44489")]
89 pub fn message(&self) -> Option<&fmt::Arguments> {
93 /// Returns information about the location from which the panic originated,
96 /// This method will currently always return [`Some`], but this may change
97 /// in future versions.
99 /// [`Some`]: ../../std/option/enum.Option.html#variant.Some
106 /// panic::set_hook(Box::new(|panic_info| {
107 /// if let Some(location) = panic_info.location() {
108 /// println!("panic occurred in file '{}' at line {}", location.file(),
109 /// location.line());
111 /// println!("panic occurred but can't get location information...");
115 /// panic!("Normal panic");
117 #[stable(feature = "panic_hooks", since = "1.10.0")]
118 pub fn location(&self) -> Option<&Location> {
119 // NOTE: If this is changed to sometimes return None,
120 // deal with that case in std::panicking::default_hook and std::panicking::begin_panic_fmt.
125 #[stable(feature = "panic_hook_display", since = "1.26.0")]
126 impl fmt::Display for PanicInfo<'_> {
127 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
128 formatter.write_str("panicked at ")?;
129 if let Some(message) = self.message {
130 write!(formatter, "'{}', ", message)?
131 } else if let Some(payload) = self.payload.downcast_ref::<&'static str>() {
132 write!(formatter, "'{}', ", payload)?
134 // NOTE: we cannot use downcast_ref::<String>() here
135 // since String is not available in libcore!
136 // The payload is a String when `std::panic!` is called with multiple arguments,
137 // but in that case the message is also available.
139 self.location.fmt(formatter)
143 /// A struct containing information about the location of a panic.
145 /// This structure is created by the [`location`] method of [`PanicInfo`].
147 /// [`location`]: ../../std/panic/struct.PanicInfo.html#method.location
148 /// [`PanicInfo`]: ../../std/panic/struct.PanicInfo.html
155 /// panic::set_hook(Box::new(|panic_info| {
156 /// if let Some(location) = panic_info.location() {
157 /// println!("panic occurred in file '{}' at line {}", location.file(), location.line());
159 /// println!("panic occurred but can't get location information...");
163 /// panic!("Normal panic");
166 #[stable(feature = "panic_hooks", since = "1.10.0")]
167 pub struct Location<'a> {
173 impl<'a> Location<'a> {
174 #![unstable(feature = "panic_internals",
175 reason = "internal details of the implementation of the `panic!` \
179 pub fn internal_constructor(file: &'a str, line: u32, col: u32) -> Self {
180 Location { file, line, col }
183 /// Returns the name of the source file from which the panic originated.
190 /// panic::set_hook(Box::new(|panic_info| {
191 /// if let Some(location) = panic_info.location() {
192 /// println!("panic occurred in file '{}'", location.file());
194 /// println!("panic occurred but can't get location information...");
198 /// panic!("Normal panic");
200 #[stable(feature = "panic_hooks", since = "1.10.0")]
201 pub fn file(&self) -> &str {
205 /// Returns the line number from which the panic originated.
212 /// panic::set_hook(Box::new(|panic_info| {
213 /// if let Some(location) = panic_info.location() {
214 /// println!("panic occurred at line {}", location.line());
216 /// println!("panic occurred but can't get location information...");
220 /// panic!("Normal panic");
222 #[stable(feature = "panic_hooks", since = "1.10.0")]
223 pub fn line(&self) -> u32 {
227 /// Returns the column from which the panic originated.
234 /// panic::set_hook(Box::new(|panic_info| {
235 /// if let Some(location) = panic_info.location() {
236 /// println!("panic occurred at column {}", location.column());
238 /// println!("panic occurred but can't get location information...");
242 /// panic!("Normal panic");
244 #[stable(feature = "panic_col", since = "1.25.0")]
245 pub fn column(&self) -> u32 {
250 #[stable(feature = "panic_hook_display", since = "1.26.0")]
251 impl fmt::Display for Location<'_> {
252 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
253 write!(formatter, "{}:{}:{}", self.file, self.line, self.col)
257 /// An internal trait used by libstd to pass data from libstd to `panic_unwind`
258 /// and other panic runtimes. Not intended to be stabilized any time soon, do
260 #[unstable(feature = "std_internals", issue = "0")]
262 pub unsafe trait BoxMeUp {
263 fn box_me_up(&mut self) -> *mut (dyn Any + Send);
264 fn get(&mut self) -> &(dyn Any + Send);