Skip to content

Commit 365b1b3

Browse files
committed
Implement new panic!() behaviour for Rust 2021.
1 parent 1046e75 commit 365b1b3

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

core/src/macros/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[cfg(bootstrap)]
12
#[doc(include = "panic.md")]
23
#[macro_export]
34
#[allow_internal_unstable(core_panic)]
@@ -18,6 +19,21 @@ macro_rules! panic {
1819
);
1920
}
2021

22+
#[cfg(not(bootstrap))]
23+
#[doc(include = "panic.md")]
24+
#[macro_export]
25+
#[rustc_builtin_macro = "core_panic"]
26+
#[allow_internal_unstable(edition_panic)]
27+
#[stable(feature = "core", since = "1.6.0")]
28+
#[rustc_diagnostic_item = "core_panic_macro"]
29+
macro_rules! panic {
30+
// Expands to either `$crate::panic::panic_2015` or `$crate::panic::panic_2021`
31+
// depending on the edition of the caller.
32+
($($arg:tt)*) => {
33+
/* compiler built-in */
34+
};
35+
}
36+
2137
/// Asserts that two expressions are equal to each other (using [`PartialEq`]).
2238
///
2339
/// On panic, this macro will print the values of the expressions with their

core/src/panic.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,40 @@
55
use crate::any::Any;
66
use crate::fmt;
77

8+
#[doc(hidden)]
9+
#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
10+
#[allow_internal_unstable(core_panic)]
11+
#[rustc_diagnostic_item = "core_panic_2015_macro"]
12+
#[rustc_macro_transparency = "semitransparent"]
13+
pub macro panic_2015 {
14+
() => (
15+
$crate::panicking::panic("explicit panic")
16+
),
17+
($msg:literal $(,)?) => (
18+
$crate::panicking::panic($msg)
19+
),
20+
($msg:expr $(,)?) => (
21+
$crate::panicking::panic_str($msg)
22+
),
23+
($fmt:expr, $($arg:tt)+) => (
24+
$crate::panicking::panic_fmt($crate::format_args!($fmt, $($arg)+))
25+
),
26+
}
27+
28+
#[doc(hidden)]
29+
#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
30+
#[allow_internal_unstable(core_panic)]
31+
#[rustc_diagnostic_item = "core_panic_2021_macro"]
32+
#[rustc_macro_transparency = "semitransparent"]
33+
pub macro panic_2021 {
34+
() => (
35+
$crate::panicking::panic("explicit panic")
36+
),
37+
($($t:tt)+) => (
38+
$crate::panicking::panic_fmt($crate::format_args!($($t)+))
39+
),
40+
}
41+
842
/// A struct providing information about a panic.
943
///
1044
/// `PanicInfo` structure is passed to a panic hook set by the [`set_hook`]

std/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@
258258
#![feature(dropck_eyepatch)]
259259
#![feature(duration_constants)]
260260
#![feature(duration_zero)]
261+
#![feature(edition_panic)]
261262
#![feature(exact_size_is_empty)]
262263
#![feature(exhaustive_patterns)]
263264
#![feature(extend_one)]

std/src/macros.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! library. Each macro is available for use when linking against the standard
55
//! library.
66
7+
#[cfg(bootstrap)]
78
#[doc(include = "../../core/src/macros/panic.md")]
89
#[macro_export]
910
#[stable(feature = "rust1", since = "1.0.0")]
@@ -17,6 +18,21 @@ macro_rules! panic {
1718
});
1819
}
1920

21+
#[cfg(not(bootstrap))]
22+
#[doc(include = "../../core/src/macros/panic.md")]
23+
#[macro_export]
24+
#[rustc_builtin_macro = "std_panic"]
25+
#[stable(feature = "rust1", since = "1.0.0")]
26+
#[allow_internal_unstable(edition_panic)]
27+
#[cfg_attr(not(test), rustc_diagnostic_item = "std_panic_macro")]
28+
macro_rules! panic {
29+
// Expands to either `$crate::panic::panic_2015` or `$crate::panic::panic_2021`
30+
// depending on the edition of the caller.
31+
($($arg:tt)*) => {
32+
/* compiler built-in */
33+
};
34+
}
35+
2036
/// Prints to the standard output.
2137
///
2238
/// Equivalent to the [`println!`] macro except that a newline is not printed at

std/src/panic.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,36 @@ use crate::sync::{Arc, Mutex, RwLock};
1717
use crate::task::{Context, Poll};
1818
use crate::thread::Result;
1919

20+
#[doc(hidden)]
21+
#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
22+
#[allow_internal_unstable(libstd_sys_internals)]
23+
#[cfg_attr(not(test), rustc_diagnostic_item = "std_panic_2015_macro")]
24+
#[rustc_macro_transparency = "semitransparent"]
25+
pub macro panic_2015 {
26+
() => ({
27+
$crate::rt::begin_panic("explicit panic")
28+
}),
29+
($msg:expr $(,)?) => ({
30+
$crate::rt::begin_panic($msg)
31+
}),
32+
($fmt:expr, $($arg:tt)+) => ({
33+
$crate::rt::begin_panic_fmt(&$crate::format_args!($fmt, $($arg)+))
34+
}),
35+
}
36+
37+
#[doc(hidden)]
38+
#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
39+
#[allow_internal_unstable(libstd_sys_internals)]
40+
#[rustc_macro_transparency = "semitransparent"]
41+
pub macro panic_2021 {
42+
() => ({
43+
$crate::rt::begin_panic("explicit panic")
44+
}),
45+
($($t:tt)+) => ({
46+
$crate::rt::begin_panic_fmt(&$crate::format_args!($($t)+))
47+
}),
48+
}
49+
2050
#[stable(feature = "panic_hooks", since = "1.10.0")]
2151
pub use crate::panicking::{set_hook, take_hook};
2252

0 commit comments

Comments
 (0)