]> git.lizzy.rs Git - rust.git/commitdiff
Add optional messages to the unreachable macro.
authorMichael Sproul <micsproul@gmail.com>
Tue, 11 Nov 2014 03:33:20 +0000 (19:33 -0800)
committerMichael Sproul <micsproul@gmail.com>
Tue, 11 Nov 2014 03:35:25 +0000 (19:35 -0800)
Closes #18842.

src/libstd/macros.rs
src/test/run-fail/unreachable-fmt-msg.rs [new file with mode: 0644]
src/test/run-fail/unreachable-macro-fail.rs [deleted file]
src/test/run-fail/unreachable-static-msg.rs [new file with mode: 0644]
src/test/run-fail/unreachable.rs [new file with mode: 0644]

index d82147947de65077126a7bbf852d67f7c9ff5cd9..0c91542e5eb9863bcc0083cf88577d43663bc6b9 100644 (file)
@@ -211,7 +211,15 @@ macro_rules! debug_assert_eq(
 /// ```
 #[macro_export]
 macro_rules! unreachable(
-    () => (panic!("internal error: entered unreachable code"))
+    () => ({
+        panic!("internal error: entered unreachable code")
+    });
+    ($msg:expr) => ({
+        unreachable!("{}", $msg)
+    });
+    ($fmt:expr, $($arg:tt)*) => ({
+        panic!(concat!("internal error: entered unreachable code: ", $fmt), $($arg)*)
+    });
 )
 
 /// A standardised placeholder for marking unfinished code. It panics with the
diff --git a/src/test/run-fail/unreachable-fmt-msg.rs b/src/test/run-fail/unreachable-fmt-msg.rs
new file mode 100644 (file)
index 0000000..f005fa4
--- /dev/null
@@ -0,0 +1,14 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// error-pattern:internal error: entered unreachable code: 6 is not prime
+fn main() {
+    unreachable!("{} is not {}", 6u32, "prime");
+}
diff --git a/src/test/run-fail/unreachable-macro-fail.rs b/src/test/run-fail/unreachable-macro-fail.rs
deleted file mode 100644 (file)
index 07e05c6..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-// error-pattern:internal error: entered unreachable code
-fn main() { unreachable!() }
diff --git a/src/test/run-fail/unreachable-static-msg.rs b/src/test/run-fail/unreachable-static-msg.rs
new file mode 100644 (file)
index 0000000..25894a8
--- /dev/null
@@ -0,0 +1,12 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// error-pattern:internal error: entered unreachable code: uhoh
+fn main() { unreachable!("uhoh") }
diff --git a/src/test/run-fail/unreachable.rs b/src/test/run-fail/unreachable.rs
new file mode 100644 (file)
index 0000000..07e05c6
--- /dev/null
@@ -0,0 +1,12 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// error-pattern:internal error: entered unreachable code
+fn main() { unreachable!() }