Auto merge of #142728 - kornelski:string-track, r=tgross35

Let String pass #[track_caller] to its Vec calls

I've added `#[track_caller]` to `String` methods that delegate to `Vec` methods that already have `#[track_caller]`.

I've also added `#[track_caller]` to methods that have `assert!` or `panic!` due to invalid inputs.
This commit is contained in:
bors 2025-06-22 23:30:10 +00:00
commit 8387d61a6e

View file

@ -1105,6 +1105,7 @@ impl String {
/// ```
#[cfg(not(no_global_oom_handling))]
#[inline]
#[track_caller]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_confusables("append", "push")]
#[rustc_diagnostic_item = "string_push_str"]
@ -1135,6 +1136,7 @@ impl String {
/// ```
#[cfg(not(no_global_oom_handling))]
#[stable(feature = "string_extend_from_within", since = "1.87.0")]
#[track_caller]
pub fn extend_from_within<R>(&mut self, src: R)
where
R: RangeBounds<usize>,
@ -1206,6 +1208,7 @@ impl String {
/// ```
#[cfg(not(no_global_oom_handling))]
#[inline]
#[track_caller]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn reserve(&mut self, additional: usize) {
self.vec.reserve(additional)
@ -1257,6 +1260,7 @@ impl String {
#[cfg(not(no_global_oom_handling))]
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[track_caller]
pub fn reserve_exact(&mut self, additional: usize) {
self.vec.reserve_exact(additional)
}
@ -1352,6 +1356,7 @@ impl String {
/// ```
#[cfg(not(no_global_oom_handling))]
#[inline]
#[track_caller]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn shrink_to_fit(&mut self) {
self.vec.shrink_to_fit()
@ -1379,6 +1384,7 @@ impl String {
/// ```
#[cfg(not(no_global_oom_handling))]
#[inline]
#[track_caller]
#[stable(feature = "shrink_to", since = "1.56.0")]
pub fn shrink_to(&mut self, min_capacity: usize) {
self.vec.shrink_to(min_capacity)
@ -1400,6 +1406,7 @@ impl String {
#[cfg(not(no_global_oom_handling))]
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[track_caller]
pub fn push(&mut self, ch: char) {
let len = self.len();
let ch_len = ch.len_utf8();
@ -1456,6 +1463,7 @@ impl String {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[track_caller]
pub fn truncate(&mut self, new_len: usize) {
if new_len <= self.len() {
assert!(self.is_char_boundary(new_len));
@ -1511,6 +1519,7 @@ impl String {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[track_caller]
#[rustc_confusables("delete", "take")]
pub fn remove(&mut self, idx: usize) -> char {
let ch = match self[idx..].chars().next() {
@ -1705,6 +1714,7 @@ impl String {
/// ```
#[cfg(not(no_global_oom_handling))]
#[inline]
#[track_caller]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_confusables("set")]
pub fn insert(&mut self, idx: usize, ch: char) {
@ -1761,6 +1771,7 @@ impl String {
/// ```
#[cfg(not(no_global_oom_handling))]
#[inline]
#[track_caller]
#[stable(feature = "insert_str", since = "1.16.0")]
#[rustc_diagnostic_item = "string_insert_str"]
pub fn insert_str(&mut self, idx: usize, string: &str) {
@ -1889,6 +1900,7 @@ impl String {
/// ```
#[cfg(not(no_global_oom_handling))]
#[inline]
#[track_caller]
#[stable(feature = "string_split_off", since = "1.16.0")]
#[must_use = "use `.truncate()` if you don't need the other half"]
pub fn split_off(&mut self, at: usize) -> String {
@ -1953,6 +1965,7 @@ impl String {
/// assert_eq!(s, "");
/// ```
#[stable(feature = "drain", since = "1.6.0")]
#[track_caller]
pub fn drain<R>(&mut self, range: R) -> Drain<'_>
where
R: RangeBounds<usize>,
@ -2052,6 +2065,7 @@ impl String {
/// ```
#[cfg(not(no_global_oom_handling))]
#[stable(feature = "splice", since = "1.27.0")]
#[track_caller]
pub fn replace_range<R>(&mut self, range: R, replace_with: &str)
where
R: RangeBounds<usize>,
@ -2101,6 +2115,7 @@ impl String {
#[stable(feature = "box_str", since = "1.4.0")]
#[must_use = "`self` will be dropped if the result is not used"]
#[inline]
#[track_caller]
pub fn into_boxed_str(self) -> Box<str> {
let slice = self.vec.into_boxed_slice();
unsafe { from_boxed_utf8_unchecked(slice) }
@ -2288,6 +2303,7 @@ impl Error for FromUtf16Error {
#[cfg(not(no_global_oom_handling))]
#[stable(feature = "rust1", since = "1.0.0")]
impl Clone for String {
#[track_caller]
fn clone(&self) -> Self {
String { vec: self.vec.clone() }
}
@ -2296,6 +2312,7 @@ impl Clone for String {
///
/// This method is preferred over simply assigning `source.clone()` to `self`,
/// as it avoids reallocation if possible.
#[track_caller]
fn clone_from(&mut self, source: &Self) {
self.vec.clone_from(&source.vec);
}
@ -2469,11 +2486,14 @@ impl<'a> Extend<Cow<'a, str>> for String {
#[cfg(not(no_global_oom_handling))]
#[unstable(feature = "ascii_char", issue = "110998")]
impl Extend<core::ascii::Char> for String {
#[inline]
#[track_caller]
fn extend<I: IntoIterator<Item = core::ascii::Char>>(&mut self, iter: I) {
self.vec.extend(iter.into_iter().map(|c| c.to_u8()));
}
#[inline]
#[track_caller]
fn extend_one(&mut self, c: core::ascii::Char) {
self.vec.push(c.to_u8());
}
@ -2482,11 +2502,14 @@ impl Extend<core::ascii::Char> for String {
#[cfg(not(no_global_oom_handling))]
#[unstable(feature = "ascii_char", issue = "110998")]
impl<'a> Extend<&'a core::ascii::Char> for String {
#[inline]
#[track_caller]
fn extend<I: IntoIterator<Item = &'a core::ascii::Char>>(&mut self, iter: I) {
self.extend(iter.into_iter().cloned());
}
#[inline]
#[track_caller]
fn extend_one(&mut self, c: &'a core::ascii::Char) {
self.vec.push(c.to_u8());
}