Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ pub fn join_path_syms(path: impl IntoIterator<Item = impl Borrow<Symbol>>) -> St
for sym in iter {
let sym = *sym.borrow();
debug_assert_ne!(sym, kw::PathRoot);
s.push_str("::");
s.push_str(sym.as_str());
s.extend(["::", sym.as_str()]);
}
s
}
Expand All @@ -201,8 +200,7 @@ pub fn join_path_idents(path: impl IntoIterator<Item = impl Borrow<Ident>>) -> S
for ident in iter {
let ident = *ident.borrow();
debug_assert_ne!(ident.name, kw::PathRoot);
s.push_str("::");
s.push_str(&ident.to_string());
s.extend(["::", &ident.to_string()]);
}
s
}
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_builtin_macros/src/assert/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
if self.paths.contains(&path_ident) {
return;
} else {
self.fmt_string.push_str(" ");
self.fmt_string.push_str(path_ident.as_str());
self.fmt_string.push_str(" = {:?}\n");
self.fmt_string.extend([" ", path_ident.as_str(), " = {:?}\n"]);
let _ = self.paths.insert(path_ident);
}
let curr_capture_idx = self.capture_decls.len();
Expand Down
26 changes: 14 additions & 12 deletions library/alloc/src/borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ use core::ops::{Deref, DerefPure};
use Cow::*;

use crate::fmt;
#[cfg(not(no_global_oom_handling))]
use crate::string::String;

// FIXME(inference): const bounds removed due to inference regressions found by crater;
// see https://github.com/rust-lang/rust/issues/147964
Expand Down Expand Up @@ -496,12 +494,14 @@ impl<'a> AddAssign<&'a str> for Cow<'a, str> {
if self.is_empty() {
*self = Cow::Borrowed(rhs)
} else if !rhs.is_empty() {
if let Cow::Borrowed(lhs) = *self {
let mut s = String::with_capacity(lhs.len() + rhs.len());
s.push_str(lhs);
*self = Cow::Owned(s);
match self {
Self::Borrowed(lhs) => {
*self = Cow::Owned([lhs, rhs].into_iter().collect());
}
Self::Owned(lhs) => {
lhs.push_str(&rhs);
}
}
self.to_mut().push_str(rhs);
}
}
}
Expand All @@ -513,12 +513,14 @@ impl<'a> AddAssign<Cow<'a, str>> for Cow<'a, str> {
if self.is_empty() {
*self = rhs
} else if !rhs.is_empty() {
if let Cow::Borrowed(lhs) = *self {
let mut s = String::with_capacity(lhs.len() + rhs.len());
s.push_str(lhs);
*self = Cow::Owned(s);
match self {
Self::Borrowed(lhs) => {
*self = Cow::Owned([&*lhs, &*rhs].into_iter().collect());
}
Self::Owned(lhs) => {
lhs.push_str(&rhs);
}
}
self.to_mut().push_str(&rhs);
}
}
}
108 changes: 103 additions & 5 deletions library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use core::ops::AddAssign;
use core::ops::Bound::{Excluded, Included, Unbounded};
use core::ops::{self, Range, RangeBounds};
use core::str::pattern::{Pattern, Utf8Pattern};
use core::{fmt, hash, ptr, slice};
use core::{array, fmt, hash, ptr, slice};

#[cfg(not(no_global_oom_handling))]
use crate::alloc::Allocator;
Expand Down Expand Up @@ -2203,6 +2203,35 @@ impl String {
let slice = self.vec.leak();
unsafe { from_utf8_unchecked_mut(slice) }
}

/// SAFETY: `impl AsRef<str> for S` must be stable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// SAFETY: `impl AsRef<str> for S` must be stable
/// SAFETY: When calling `<S as AsRef<str>::as_ref()` multiple times, the same value must be returned.

at first I thought you were talking about a consistent trait impl needing to apply in the face of specialization.

#[cfg(not(no_global_oom_handling))]
unsafe fn extend_many<S: AsRef<str>>(&mut self, vals: &[S]) {
let additional = vals.iter().fold(0usize, |a, s| a.saturating_add(s.as_ref().len()));
self.reserve(additional);

let mut spare = self.vec.spare_capacity_mut().as_mut_ptr().cast_init();
for val in vals {
let val = val.as_ref();
// SAFETY:
// - `val` is a valid &str, so `val.as_ptr()` is valid
// for `val.len()` bytes and properly initialized.
// - `spare` points to valid spare capacity in the Vec
// with enough space for `val.len()` bytes.
// This is guaranteed because the caller ensures
// that `.as_ref()` is stable, and the saturating
// addition stops undercounting by overflow.
// - Both pointers are byte-aligned and the regions cannot overlap.
unsafe {
ptr::copy_nonoverlapping(val.as_ptr(), spare, val.len());
spare = spare.add(val.len());
}
}

let new_len = self.vec.len() + additional;
// SAFETY: the elements have just been initialized
unsafe { self.vec.set_len(new_len) }
}
}

impl FromUtf8Error {
Expand Down Expand Up @@ -2502,7 +2531,8 @@ impl<'a> Extend<&'a char> for String {
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> Extend<&'a str> for String {
fn extend<I: IntoIterator<Item = &'a str>>(&mut self, iter: I) {
iter.into_iter().for_each(move |s| self.push_str(s));
// SAFETY: `impl AsRef<str> for &str` is stable
unsafe { self.extend_many_chunked(iter.into_iter()) }
}

#[inline]
Expand All @@ -2515,15 +2545,17 @@ impl<'a> Extend<&'a str> for String {
#[stable(feature = "box_str2", since = "1.45.0")]
impl<A: Allocator> Extend<Box<str, A>> for String {
fn extend<I: IntoIterator<Item = Box<str, A>>>(&mut self, iter: I) {
iter.into_iter().for_each(move |s| self.push_str(&s));
// SAFETY: `impl AsRef<str> for Box<str, A>` is stable
unsafe { self.extend_many_chunked(iter.into_iter()) }
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "extend_string", since = "1.4.0")]
impl Extend<String> for String {
fn extend<I: IntoIterator<Item = String>>(&mut self, iter: I) {
iter.into_iter().for_each(move |s| self.push_str(&s));
// SAFETY: `impl AsRef<str> for String` is stable
unsafe { self.extend_many_chunked(iter.into_iter()) }
}

#[inline]
Expand All @@ -2536,7 +2568,8 @@ impl Extend<String> for String {
#[stable(feature = "herd_cows", since = "1.19.0")]
impl<'a> Extend<Cow<'a, str>> for String {
fn extend<I: IntoIterator<Item = Cow<'a, str>>>(&mut self, iter: I) {
iter.into_iter().for_each(move |s| self.push_str(&s));
// SAFETY: `impl AsRef<str> for Cow<'a, str>` is stable
unsafe { self.extend_many_chunked(iter.into_iter()) }
}

#[inline]
Expand Down Expand Up @@ -2573,6 +2606,71 @@ impl<'a> Extend<&'a core::ascii::Char> for String {
}
}

#[cfg(not(no_global_oom_handling))]
trait ExtendManySpec<S, I> {
/// SAFETY: `impl AsRef<str> for S` must be stable
unsafe fn extend_many_chunked(&mut self, iter: I);
}

#[cfg(not(no_global_oom_handling))]
impl<S, I> ExtendManySpec<S, I> for String
where
S: AsRef<str>,
I: Iterator<Item = S>,
{
default unsafe fn extend_many_chunked(&mut self, mut iter: I) {
let mut repeat = true;
while repeat {
let chunk = match iter.next_chunk::<8>() {
Ok(chunk) => chunk.into_iter(),
Err(partial_chunk) => {
repeat = false;
partial_chunk
}
};

// SAFETY: the caller ensures that `impl AsRef<str> for S` is stable
unsafe { self.extend_many(chunk.as_slice()) }
}
}
}

// TODO: specialization thinks that this is a conflicting implementation
/*
#[cfg(not(no_global_oom_handling))]
impl<'a, S> ExtendManySpec<S, slice::Iter<'a, S>> for String
where
S: AsRef<str>,
{
unsafe fn extend_many_chunked(&mut self, iter: slice::Iter<'a, S>) {
// SAFETY: the caller ensures that `impl AsRef<str> for S` is stable
unsafe { self.extend_many(iter.as_slice()) }
}
}
*/

#[cfg(not(no_global_oom_handling))]
impl<S, const N: usize> ExtendManySpec<S, array::IntoIter<S, N>> for String
where
S: AsRef<str>,
{
unsafe fn extend_many_chunked(&mut self, iter: array::IntoIter<S, N>) {
// SAFETY: the caller ensures that `impl AsRef<str> for S` is stable
unsafe { self.extend_many(iter.as_slice()) }
}
}

#[cfg(not(no_global_oom_handling))]
impl<S> ExtendManySpec<S, vec::IntoIter<S>> for String
where
S: AsRef<str>,
{
unsafe fn extend_many_chunked(&mut self, iter: vec::IntoIter<S>) {
// SAFETY: the caller ensures that `impl AsRef<str> for S` is stable
unsafe { self.extend_many(iter.as_slice()) }
}
}

/// A convenience impl that delegates to the impl for `&str`.
///
/// # Examples
Expand Down
4 changes: 1 addition & 3 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1645,9 +1645,7 @@ pub(crate) fn plain_text_from_events<'a>(
match &event {
Event::Text(text) => s.push_str(text),
Event::Code(code) => {
s.push('`');
s.push_str(code);
s.push('`');
s.extend(["`", code, "`"]);
}
Event::HardBreak | Event::SoftBreak => s.push(' '),
Event::Start(Tag::CodeBlock(..)) => break,
Expand Down
3 changes: 1 addition & 2 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2257,8 +2257,7 @@ pub(crate) fn compare_names(left: &str, right: &str) -> Ordering {

pub(super) fn full_path(cx: &Context<'_>, item: &clean::Item) -> String {
let mut s = join_path_syms(&cx.current);
s.push_str("::");
s.push_str(item.name.unwrap().as_str());
s.extend(["::", item.name.unwrap().as_str()]);
s
}

Expand Down
Loading