Skip to content

Commit 837dd14

Browse files
committed
Add optional messages to the unreachable macro.
Closes #18842.
1 parent 40fb87d commit 837dd14

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

src/libstd/macros.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,15 @@ macro_rules! debug_assert_eq(
211211
/// ```
212212
#[macro_export]
213213
macro_rules! unreachable(
214-
() => (panic!("internal error: entered unreachable code"))
214+
() => ({
215+
panic!("internal error: entered unreachable code")
216+
});
217+
($msg:expr) => ({
218+
unreachable!("{}", $msg)
219+
});
220+
($fmt:expr, $($arg:tt)*) => ({
221+
panic!(concat!("internal error: entered unreachable code: ", $fmt), $($arg)*)
222+
});
215223
)
216224

217225
/// A standardised placeholder for marking unfinished code. It panics with the
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// error-pattern:internal error: entered unreachable code: 6 is not prime
12+
fn main() {
13+
unreachable!("{} is not {}", 6u32, "prime");
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// error-pattern:internal error: entered unreachable code: uhoh
12+
fn main() { unreachable!("uhoh") }

0 commit comments

Comments
 (0)