about summary refs log tree commit diff
path: root/library/core
diff options
context:
space:
mode:
authorLoïc BRANSTETT <lolo.branstett@numericable.fr>2022-01-21 23:04:06 +0100
committerLoïc BRANSTETT <lolo.branstett@numericable.fr>2022-01-31 17:09:31 +0100
commit565710b33cb20c901b8b3371d1364cf7fb11e79b (patch)
treeea8b01a50153f7188172d0a924b86fa30471d4a2 /library/core
parent86f5e177bca8121e1edc9864023a8ea61acf9034 (diff)
downloadrust-565710b33cb20c901b8b3371d1364cf7fb11e79b.tar.gz
rust-565710b33cb20c901b8b3371d1364cf7fb11e79b.zip
Fix invalid special casing of the unreachable! macro
Diffstat (limited to 'library/core')
-rw-r--r--library/core/src/macros/mod.rs16
-rw-r--r--library/core/src/panic.rs33
-rw-r--r--library/core/src/panicking.rs8
3 files changed, 57 insertions, 0 deletions
diff --git a/library/core/src/macros/mod.rs b/library/core/src/macros/mod.rs
index 0cc428d6962..a9e90556f6c 100644
--- a/library/core/src/macros/mod.rs
+++ b/library/core/src/macros/mod.rs
@@ -594,6 +594,22 @@ macro_rules! writeln {
 ///     unreachable!("The loop should always return");
 /// }
 /// ```
+#[cfg(not(bootstrap))]
+#[macro_export]
+#[rustc_builtin_macro(unreachable)]
+#[allow_internal_unstable(edition_panic)]
+#[stable(feature = "rust1", since = "1.0.0")]
+#[cfg_attr(not(test), rustc_diagnostic_item = "unreachable_macro")]
+macro_rules! unreachable {
+    // Expands to either `$crate::panic::unreachable_2015` or `$crate::panic::unreachable_2021`
+    // depending on the edition of the caller.
+    ($($arg:tt)*) => {
+        /* compiler built-in */
+    };
+}
+
+/// unreachable!() macro
+#[cfg(bootstrap)]
 #[macro_export]
 #[stable(feature = "rust1", since = "1.0.0")]
 #[cfg_attr(not(test), rustc_diagnostic_item = "unreachable_macro")]
diff --git a/library/core/src/panic.rs b/library/core/src/panic.rs
index 7a8b04d6f3c..0be3f06ff6c 100644
--- a/library/core/src/panic.rs
+++ b/library/core/src/panic.rs
@@ -58,6 +58,39 @@ pub macro panic_2021 {
     ),
 }
 
+#[doc(hidden)]
+#[unstable(feature = "edition_panic", issue = "none", reason = "use unreachable!() instead")]
+#[allow_internal_unstable(core_panic)]
+#[rustc_diagnostic_item = "unreachable_2015_macro"]
+#[rustc_macro_transparency = "semitransparent"]
+pub macro unreachable_2015 {
+    () => (
+        $crate::panicking::panic("internal error: entered unreachable code")
+    ),
+    // Use of `unreachable_display` for non_fmt_panic lint.
+    // NOTE: the message ("internal error ...") is embeded directly in unreachable_display
+    ($msg:expr $(,)?) => (
+        $crate::panicking::unreachable_display(&$msg)
+    ),
+    ($fmt:expr, $($arg:tt)*) => (
+        $crate::panic!($crate::concat!("internal error: entered unreachable code: ", $fmt), $($arg)*)
+    ),
+}
+
+#[doc(hidden)]
+#[unstable(feature = "edition_panic", issue = "none", reason = "use unreachable!() instead")]
+#[allow_internal_unstable(core_panic)]
+#[rustc_diagnostic_item = "unreachable_2021_macro"]
+#[rustc_macro_transparency = "semitransparent"]
+pub macro unreachable_2021 {
+    () => (
+        $crate::panicking::panic("internal error: entered unreachable code")
+    ),
+    ($($t:tt)+) => (
+        $crate::panic!("internal error: entered unreachable code: {}", $crate::format_args!($($t)+))
+    ),
+}
+
 /// An internal trait used by libstd to pass data from libstd to `panic_unwind`
 /// and other panic runtimes. Not intended to be stabilized any time soon, do
 /// not use.
diff --git a/library/core/src/panicking.rs b/library/core/src/panicking.rs
index 5078eea07a1..d857b4948b5 100644
--- a/library/core/src/panicking.rs
+++ b/library/core/src/panicking.rs
@@ -56,6 +56,14 @@ pub const fn panic_str(expr: &str) -> ! {
     panic_display(&expr);
 }
 
+#[cfg(not(bootstrap))]
+#[inline]
+#[track_caller]
+#[rustc_diagnostic_item = "unreachable_display"] // needed for `non-fmt-panics` lint
+pub fn unreachable_display<T: fmt::Display>(x: &T) -> ! {
+    panic_fmt(format_args!("internal error: entered unreachable code: {}", *x));
+}
+
 #[inline]
 #[track_caller]
 #[lang = "panic_display"] // needed for const-evaluated panics