Skip to content

Commit dec11c9

Browse files
committed
Auto merge of rust-lang#80851 - m-ou-se:panic-2021, r=petrochenkov
Implement Rust 2021 panic This implements the Rust 2021 versions of `panic!()`. See rust-lang#80162 and rust-lang/rfcs#3007. It does so by replacing `{std, core}::panic!()` by a bulitin macro that expands to either `$crate::panic::panic_2015!(..)` or `$crate::panic::panic_2021!(..)` depending on the edition of the caller. This does not yet make std's panic an alias for core's panic on Rust 2021 as the RFC proposes. That will be a separate change: rust-lang@c5273bd That change is blocked on figuring out what to do with rust-lang#80846 first.
2 parents 0e0566d + 70ac654 commit dec11c9

File tree

5 files changed

+89
-1
lines changed

5 files changed

+89
-1
lines changed

core/src/macros/mod.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
#[cfg(bootstrap)]
12
#[doc(include = "panic.md")]
23
#[macro_export]
3-
#[allow_internal_unstable(core_panic, const_caller_location)]
4+
#[allow_internal_unstable(core_panic)]
45
#[stable(feature = "core", since = "1.6.0")]
56
#[rustc_diagnostic_item = "core_panic_macro"]
67
macro_rules! 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
@@ -259,6 +259,7 @@
259259
#![feature(dropck_eyepatch)]
260260
#![feature(duration_constants)]
261261
#![feature(duration_zero)]
262+
#![feature(edition_panic)]
262263
#![feature(exact_size_is_empty)]
263264
#![feature(exhaustive_patterns)]
264265
#![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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,27 @@ use crate::sync::{Arc, Mutex, RwLock};
1818
use crate::task::{Context, Poll};
1919
use crate::thread::Result;
2020

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

0 commit comments

Comments
 (0)