Skip to content

Commit 390bb34

Browse files
committed
Auto merge of rust-lang#92155 - m-ou-se:panic-fn, r=eddyb
Use panic() instead of panic!() in some places in core. See rust-lang#92068 and rust-lang#92140. This avoids the `panic!()` macro in a few potentially hot paths. This becomes more relevant when switching `core` to Rust 2021, as it'll avoid format_args!() and save some compilation time. (It doesn't make a huge difference, but still.) (Also the errors in const panic become slightly nicer.)
2 parents 5aa0239 + 22c4160 commit 390bb34

File tree

8 files changed

+26
-20
lines changed

8 files changed

+26
-20
lines changed

Diff for: library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
#![feature(const_type_id)]
142142
#![feature(const_type_name)]
143143
#![feature(const_default_impls)]
144+
#![feature(core_panic)]
144145
#![feature(duration_consts_float)]
145146
#![feature(maybe_uninit_uninit_array)]
146147
#![feature(ptr_metadata)]

Diff for: library/core/src/macros/mod.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -589,9 +589,10 @@ macro_rules! writeln {
589589
/// ```
590590
#[macro_export]
591591
#[stable(feature = "rust1", since = "1.0.0")]
592+
#[allow_internal_unstable(core_panic)]
592593
macro_rules! unreachable {
593594
() => ({
594-
$crate::panic!("internal error: entered unreachable code")
595+
$crate::panicking::panic("internal error: entered unreachable code")
595596
});
596597
($msg:expr $(,)?) => ({
597598
$crate::unreachable!("{}", $msg)
@@ -674,8 +675,9 @@ macro_rules! unreachable {
674675
/// ```
675676
#[macro_export]
676677
#[stable(feature = "rust1", since = "1.0.0")]
678+
#[allow_internal_unstable(core_panic)]
677679
macro_rules! unimplemented {
678-
() => ($crate::panic!("not implemented"));
680+
() => ($crate::panicking::panic("not implemented"));
679681
($($arg:tt)+) => ($crate::panic!("not implemented: {}", $crate::format_args!($($arg)+)));
680682
}
681683

@@ -735,8 +737,9 @@ macro_rules! unimplemented {
735737
/// ```
736738
#[macro_export]
737739
#[stable(feature = "todo_macro", since = "1.40.0")]
740+
#[allow_internal_unstable(core_panic)]
738741
macro_rules! todo {
739-
() => ($crate::panic!("not yet implemented"));
742+
() => ($crate::panicking::panic("not yet implemented"));
740743
($($arg:tt)+) => ($crate::panic!("not yet implemented: {}", $crate::format_args!($($arg)+)));
741744
}
742745

Diff for: library/core/src/option.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@
501501
#![stable(feature = "rust1", since = "1.0.0")]
502502

503503
use crate::iter::{FromIterator, FusedIterator, TrustedLen};
504+
use crate::panicking::{panic, panic_str};
504505
use crate::pin::Pin;
505506
use crate::{
506507
convert, hint, mem,
@@ -755,7 +756,7 @@ impl<T> Option<T> {
755756
pub const fn unwrap(self) -> T {
756757
match self {
757758
Some(val) => val,
758-
None => panic!("called `Option::unwrap()` on a `None` value"),
759+
None => panic("called `Option::unwrap()` on a `None` value"),
759760
}
760761
}
761762

@@ -1815,8 +1816,9 @@ impl<T, E> Option<Result<T, E>> {
18151816
#[cfg_attr(feature = "panic_immediate_abort", inline)]
18161817
#[cold]
18171818
#[track_caller]
1819+
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
18181820
const fn expect_failed(msg: &str) -> ! {
1819-
panic!("{}", msg)
1821+
panic_str(msg)
18201822
}
18211823

18221824
/////////////////////////////////////////////////////////////////////////////

Diff for: src/test/mir-opt/issue_76432.test.SimplifyComparisonIntegral.diff

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
let mut _19: *const T; // in scope 0 at $DIR/issue_76432.rs:9:54: 9:68
2222
let mut _20: *const T; // in scope 0 at $DIR/issue_76432.rs:9:70: 9:84
2323
let mut _21: *const T; // in scope 0 at $DIR/issue_76432.rs:9:70: 9:84
24-
let mut _22: !; // in scope 0 at $SRC_DIR/core/src/panic.rs:LL:COL
24+
let mut _22: !; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
2525
let mut _23: &[T; 3]; // in scope 0 at $DIR/issue_76432.rs:7:19: 7:29
2626
scope 1 {
2727
debug v => _2; // in scope 1 at $DIR/issue_76432.rs:7:9: 7:10
@@ -66,10 +66,10 @@
6666
}
6767

6868
bb1: {
69-
StorageLive(_22); // scope 1 at $SRC_DIR/core/src/panic.rs:LL:COL
70-
core::panicking::panic(const "internal error: entered unreachable code"); // scope 1 at $SRC_DIR/core/src/panic.rs:LL:COL
69+
StorageLive(_22); // scope 1 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
70+
core::panicking::panic(const "internal error: entered unreachable code"); // scope 1 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
7171
// mir::Constant
72-
// + span: $SRC_DIR/core/src/panic.rs:LL:COL
72+
// + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL
7373
// + literal: Const { ty: fn(&'static str) -> ! {core::panicking::panic}, val: Value(Scalar(<ZST>)) }
7474
// ty::Const
7575
// + ty: &str

Diff for: src/test/ui/consts/const-eval/const_panic.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ error[E0080]: evaluation of constant value failed
2020
LL | const Y: () = std::unreachable!();
2121
| ^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic.rs:12:15
2222
|
23-
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
23+
= note: this error originates in the macro `std::unreachable` (in Nightly builds, run with -Z macro-backtrace for more info)
2424

2525
error[E0080]: evaluation of constant value failed
2626
--> $DIR/const_panic.rs:15:15
2727
|
2828
LL | const X: () = std::unimplemented!();
2929
| ^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'not implemented', $DIR/const_panic.rs:15:15
3030
|
31-
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
31+
= note: this error originates in the macro `std::unimplemented` (in Nightly builds, run with -Z macro-backtrace for more info)
3232

3333
error[E0080]: evaluation of constant value failed
3434
--> $DIR/const_panic.rs:18:15
@@ -68,15 +68,15 @@ error[E0080]: evaluation of constant value failed
6868
LL | const Y_CORE: () = core::unreachable!();
6969
| ^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic.rs:30:20
7070
|
71-
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
71+
= note: this error originates in the macro `core::unreachable` (in Nightly builds, run with -Z macro-backtrace for more info)
7272

7373
error[E0080]: evaluation of constant value failed
7474
--> $DIR/const_panic.rs:33:20
7575
|
7676
LL | const X_CORE: () = core::unimplemented!();
7777
| ^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'not implemented', $DIR/const_panic.rs:33:20
7878
|
79-
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
79+
= note: this error originates in the macro `core::unimplemented` (in Nightly builds, run with -Z macro-backtrace for more info)
8080

8181
error[E0080]: evaluation of constant value failed
8282
--> $DIR/const_panic.rs:36:20

Diff for: src/test/ui/consts/const-eval/const_panic_2021.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ error[E0080]: evaluation of constant value failed
2020
LL | const C: () = std::unreachable!();
2121
| ^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic_2021.rs:12:15
2222
|
23-
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
23+
= note: this error originates in the macro `std::unreachable` (in Nightly builds, run with -Z macro-backtrace for more info)
2424

2525
error[E0080]: evaluation of constant value failed
2626
--> $DIR/const_panic_2021.rs:15:15
2727
|
2828
LL | const D: () = std::unimplemented!();
2929
| ^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'not implemented', $DIR/const_panic_2021.rs:15:15
3030
|
31-
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
31+
= note: this error originates in the macro `std::unimplemented` (in Nightly builds, run with -Z macro-backtrace for more info)
3232

3333
error[E0080]: evaluation of constant value failed
3434
--> $DIR/const_panic_2021.rs:18:15
@@ -60,15 +60,15 @@ error[E0080]: evaluation of constant value failed
6060
LL | const C_CORE: () = core::unreachable!();
6161
| ^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic_2021.rs:27:20
6262
|
63-
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
63+
= note: this error originates in the macro `core::unreachable` (in Nightly builds, run with -Z macro-backtrace for more info)
6464

6565
error[E0080]: evaluation of constant value failed
6666
--> $DIR/const_panic_2021.rs:30:20
6767
|
6868
LL | const D_CORE: () = core::unimplemented!();
6969
| ^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'not implemented', $DIR/const_panic_2021.rs:30:20
7070
|
71-
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
71+
= note: this error originates in the macro `core::unimplemented` (in Nightly builds, run with -Z macro-backtrace for more info)
7272

7373
error[E0080]: evaluation of constant value failed
7474
--> $DIR/const_panic_2021.rs:33:20

Diff for: src/test/ui/consts/const-eval/const_panic_libcore_bin.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ error[E0080]: evaluation of constant value failed
1212
LL | const Y: () = unreachable!();
1313
| ^^^^^^^^^^^^^^ the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic_libcore_bin.rs:11:15
1414
|
15-
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
15+
= note: this error originates in the macro `unreachable` (in Nightly builds, run with -Z macro-backtrace for more info)
1616

1717
error[E0080]: evaluation of constant value failed
1818
--> $DIR/const_panic_libcore_bin.rs:14:15
1919
|
2020
LL | const X: () = unimplemented!();
2121
| ^^^^^^^^^^^^^^^^ the evaluated program panicked at 'not implemented', $DIR/const_panic_libcore_bin.rs:14:15
2222
|
23-
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
23+
= note: this error originates in the macro `unimplemented` (in Nightly builds, run with -Z macro-backtrace for more info)
2424

2525
error: aborting due to 3 previous errors
2626

Diff for: src/tools/clippy/tests/ui/missing_panics_doc.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ note: first possible panic found here
4242
|
4343
LL | todo!()
4444
| ^^^^^^^
45-
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
45+
= note: this error originates in the macro `todo` (in Nightly builds, run with -Z macro-backtrace for more info)
4646

4747
error: docs for function which may panic missing `# Panics` section
4848
--> $DIR/missing_panics_doc.rs:22:1

0 commit comments

Comments
 (0)