Skip to content

Commit a7ab7b1

Browse files
committed
#[track_caller] on core::ops::{Index, IndexMut}.
1 parent 5aa8f19 commit a7ab7b1

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

src/libcore/ops/index.rs

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ pub trait Index<Idx: ?Sized> {
6565

6666
/// Performs the indexing (`container[index]`) operation.
6767
#[stable(feature = "rust1", since = "1.0.0")]
68+
#[cfg_attr(not(bootstrap), track_caller)]
6869
fn index(&self, index: Idx) -> &Self::Output;
6970
}
7071

@@ -166,5 +167,6 @@ see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#ind
166167
pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
167168
/// Performs the mutable indexing (`container[index]`) operation.
168169
#[stable(feature = "rust1", since = "1.0.0")]
170+
#[cfg_attr(not(bootstrap), track_caller)]
169171
fn index_mut(&mut self, index: Idx) -> &mut Self::Output;
170172
}

src/libcore/slice/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -2306,6 +2306,7 @@ impl<T> [T] {
23062306
/// assert_eq!(&bytes, b"Hello, Wello!");
23072307
/// ```
23082308
#[stable(feature = "copy_within", since = "1.37.0")]
2309+
#[track_caller]
23092310
pub fn copy_within<R: ops::RangeBounds<usize>>(&mut self, src: R, dest: usize)
23102311
where
23112312
T: Copy,
@@ -2721,18 +2722,21 @@ where
27212722

27222723
#[inline(never)]
27232724
#[cold]
2725+
#[track_caller]
27242726
fn slice_index_len_fail(index: usize, len: usize) -> ! {
27252727
panic!("index {} out of range for slice of length {}", index, len);
27262728
}
27272729

27282730
#[inline(never)]
27292731
#[cold]
2732+
#[track_caller]
27302733
fn slice_index_order_fail(index: usize, end: usize) -> ! {
27312734
panic!("slice index starts at {} but ends at {}", index, end);
27322735
}
27332736

27342737
#[inline(never)]
27352738
#[cold]
2739+
#[track_caller]
27362740
fn slice_index_overflow_fail() -> ! {
27372741
panic!("attempted to index slice up to maximum usize");
27382742
}
@@ -2804,11 +2808,13 @@ pub trait SliceIndex<T: ?Sized>: private_slice_index::Sealed {
28042808
/// Returns a shared reference to the output at this location, panicking
28052809
/// if out of bounds.
28062810
#[unstable(feature = "slice_index_methods", issue = "none")]
2811+
#[cfg_attr(not(bootstrap), track_caller)]
28072812
fn index(self, slice: &T) -> &Self::Output;
28082813

28092814
/// Returns a mutable reference to the output at this location, panicking
28102815
/// if out of bounds.
28112816
#[unstable(feature = "slice_index_methods", issue = "none")]
2817+
#[cfg_attr(not(bootstrap), track_caller)]
28122818
fn index_mut(self, slice: &mut T) -> &mut Self::Output;
28132819
}
28142820

src/libcore/str/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1794,6 +1794,7 @@ mod traits {
17941794

17951795
#[inline(never)]
17961796
#[cold]
1797+
#[track_caller]
17971798
fn str_index_overflow_fail() -> ! {
17981799
panic!("attempted to index str up to maximum usize");
17991800
}
@@ -2185,6 +2186,7 @@ fn truncate_to_char_boundary(s: &str, mut max: usize) -> (bool, &str) {
21852186

21862187
#[inline(never)]
21872188
#[cold]
2189+
#[track_caller]
21882190
fn slice_error_fail(s: &str, begin: usize, end: usize) -> ! {
21892191
const MAX_DISPLAY_LENGTH: usize = 256;
21902192
let (truncated, s_trunc) = truncate_to_char_boundary(s, MAX_DISPLAY_LENGTH);

src/test/ui/rfc-2091-track-caller/std-panic-locations.rs

+22
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
// ignore-wasm32-bare compiled with panic=abort by default
33

44
#![feature(option_expect_none, option_unwrap_none)]
5+
#![allow(unconditional_panic)]
56

67
//! Test that panic locations for `#[track_caller]` functions in std have the correct
78
//! location reported.
89
10+
use std::collections::{BTreeMap, HashMap, VecDeque};
11+
use std::ops::{Index, IndexMut};
12+
913
fn main() {
1014
// inspect the `PanicInfo` we receive to ensure the right file is the source
1115
std::panic::set_hook(Box::new(|info| {
@@ -35,4 +39,22 @@ fn main() {
3539
let fine: Result<(), ()> = Ok(());
3640
assert_panicked(|| fine.unwrap_err());
3741
assert_panicked(|| fine.expect_err(""));
42+
43+
let mut small = [0]; // the implementation backing str, vec, etc
44+
assert_panicked(move || { small.index(1); });
45+
assert_panicked(move || { small[1]; });
46+
assert_panicked(move || { small.index_mut(1); });
47+
assert_panicked(move || { small[1] += 1; });
48+
49+
let sorted: BTreeMap<bool, bool> = Default::default();
50+
assert_panicked(|| { sorted.index(&false); });
51+
assert_panicked(|| { sorted[&false]; });
52+
53+
let unsorted: HashMap<bool, bool> = Default::default();
54+
assert_panicked(|| { unsorted.index(&false); });
55+
assert_panicked(|| { unsorted[&false]; });
56+
57+
let weirdo: VecDeque<()> = Default::default();
58+
assert_panicked(|| { weirdo.index(1); });
59+
assert_panicked(|| { weirdo[1]; });
3860
}

0 commit comments

Comments
 (0)