]> git.lizzy.rs Git - rust.git/blobdiff - src/libcore/panicking.rs
Rollup merge of #70038 - DutchGhost:const-forget-tests, r=RalfJung
[rust.git] / src / libcore / panicking.rs
index 61b764f2d6206ad7773ede553601ea130ce968d1..3587f3f0ebf56c738feb4be9a8db8327052a6424 100644 (file)
@@ -32,6 +32,7 @@
 use crate::fmt;
 use crate::panic::{Location, PanicInfo};
 
+/// The underlying implementation of libcore's `panic!` macro when no formatting is used.
 #[cold]
 // never inline unless panic_immediate_abort to avoid code
 // bloat at the call sites as much as possible
@@ -49,9 +50,28 @@ pub fn panic(expr: &str) -> ! {
     // truncation and padding (even though none is used here). Using
     // Arguments::new_v1 may allow the compiler to omit Formatter::pad from the
     // output binary, saving up to a few kilobytes.
-    panic_fmt(fmt::Arguments::new_v1(&[expr], &[]), Location::caller())
+    #[cfg(not(bootstrap))]
+    panic_fmt(fmt::Arguments::new_v1(&[expr], &[]));
+    #[cfg(bootstrap)]
+    panic_fmt(fmt::Arguments::new_v1(&[expr], &[]), Location::caller());
 }
 
+#[cfg(not(bootstrap))]
+#[cold]
+#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
+#[track_caller]
+#[lang = "panic_bounds_check"] // needed by codegen for panic on OOB array/slice access
+fn panic_bounds_check(index: usize, len: usize) -> ! {
+    if cfg!(feature = "panic_immediate_abort") {
+        unsafe { super::intrinsics::abort() }
+    }
+
+    panic!("index out of bounds: the len is {} but the index is {}", len, index)
+}
+
+// For bootstrap, we need a variant with the old argument order, and a corresponding
+// `panic_fmt`.
+#[cfg(bootstrap)]
 #[cold]
 #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
 #[lang = "panic_bounds_check"] // needed by codegen for panic on OOB array/slice access
@@ -66,10 +86,12 @@ fn panic_bounds_check(location: &Location<'_>, index: usize, len: usize) -> ! {
     )
 }
 
+/// The underlying implementation of libcore's `panic!` macro when formatting is used.
 #[cold]
 #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
 #[cfg_attr(feature = "panic_immediate_abort", inline)]
-pub fn panic_fmt(fmt: fmt::Arguments<'_>, location: &Location<'_>) -> ! {
+#[cfg_attr(not(bootstrap), track_caller)]
+pub fn panic_fmt(fmt: fmt::Arguments<'_>, #[cfg(bootstrap)] location: &Location<'_>) -> ! {
     if cfg!(feature = "panic_immediate_abort") {
         unsafe { super::intrinsics::abort() }
     }
@@ -81,6 +103,10 @@ pub fn panic_fmt(fmt: fmt::Arguments<'_>, location: &Location<'_>) -> ! {
         fn panic_impl(pi: &PanicInfo<'_>) -> !;
     }
 
+    #[cfg(bootstrap)]
     let pi = PanicInfo::internal_constructor(Some(&fmt), location);
+    #[cfg(not(bootstrap))]
+    let pi = PanicInfo::internal_constructor(Some(&fmt), Location::caller());
+
     unsafe { panic_impl(&pi) }
 }