Skip to content

Commit 1a9fc18

Browse files
committed
panic_bounds_check: use caller_location, like PanicFnLangItem
1 parent 2cb0b85 commit 1a9fc18

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/libcore/panicking.rs

+18
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,24 @@ pub fn panic(expr: &str) -> ! {
5252
panic_fmt(fmt::Arguments::new_v1(&[expr], &[]), Location::caller())
5353
}
5454

55+
#[cfg(not(bootstrap))]
56+
#[cold]
57+
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
58+
#[track_caller]
59+
#[lang = "panic_bounds_check"] // needed by codegen for panic on OOB array/slice access
60+
fn panic_bounds_check(index: usize, len: usize) -> ! {
61+
if cfg!(feature = "panic_immediate_abort") {
62+
unsafe { super::intrinsics::abort() }
63+
}
64+
65+
panic_fmt(
66+
format_args!("index out of bounds: the len is {} but the index is {}", len, index),
67+
Location::caller(),
68+
)
69+
}
70+
71+
// For bootstrap, we need a variant with the old argument order.
72+
#[cfg(bootstrap)]
5573
#[cold]
5674
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
5775
#[lang = "panic_bounds_check"] // needed by codegen for panic on OOB array/slice access

src/librustc_codegen_ssa/mir/block.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,15 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
415415
AssertKind::BoundsCheck { ref len, ref index } => {
416416
let len = self.codegen_operand(&mut bx, len).immediate();
417417
let index = self.codegen_operand(&mut bx, index).immediate();
418-
(lang_items::PanicBoundsCheckFnLangItem, vec![location, index, len])
418+
// It's `fn panic_bounds_check(index: usize, len: usize)`, and
419+
// `#[track_caller]` adds an implicit third argument.
420+
(lang_items::PanicBoundsCheckFnLangItem, vec![index, len, location])
419421
}
420422
_ => {
421423
let msg_str = Symbol::intern(msg.description());
422424
let msg = bx.const_str(msg_str);
425+
// It's `pub fn panic(expr: &str)`, with the wide reference being passed
426+
// as two arguments, and `#[track_caller]` adds an implicit third argument.
423427
(lang_items::PanicFnLangItem, vec![msg.0, msg.1, location])
424428
}
425429
};

0 commit comments

Comments
 (0)