Skip to content
Closed
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
3 changes: 1 addition & 2 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2698,8 +2698,7 @@ impl HumanEmitter {
[SubstitutionHighlight { start: 0, end }] if *end == line_to_add.len() => {
buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
}
[] => {
// FIXME: needed? Doesn't get exercised in any test.
[] | [SubstitutionHighlight { start: 0, end: 0 }] => {
self.draw_col_separator_no_space(buffer, *row_num, max_line_num_len + 1);
}
_ => {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ impl Input {
}
}

#[derive(Clone, Hash, Debug, HashStable_Generic, PartialEq, Encodable, Decodable)]
#[derive(Clone, Hash, Debug, HashStable_Generic, PartialEq, Eq, Encodable, Decodable)]
pub enum OutFileName {
Real(PathBuf),
Stdout,
Expand Down
1 change: 1 addition & 0 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
#![feature(slice_range)]
#![feature(std_internals)]
#![feature(str_internals)]
#![feature(string_replace_in_place)]
#![feature(temporary_niche_types)]
#![feature(trusted_fused)]
#![feature(trusted_len)]
Expand Down
61 changes: 61 additions & 0 deletions library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2090,6 +2090,67 @@ impl String {
unsafe { self.as_mut_vec() }.splice((start, end), replace_with.bytes());
}

/// Replaces the leftmost occurrence of a pattern with another string, in-place.
///
/// This method can be preferred over [`string = string.replacen(..., 1);`][replacen],
/// as it can use the `String`'s existing capacity to prevent a reallocation if
/// sufficient space is available.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// #![feature(string_replace_in_place)]
///
/// let mut s = String::from("Test Results: ❌❌❌");
///
/// // Replace the leftmost ❌ with a ✅
/// s.replace_first('❌', "✅");
/// assert_eq!(s, "Test Results: ✅❌❌");
/// ```
///
/// [replacen]: ../../std/primitive.str.html#method.replacen
#[cfg(not(no_global_oom_handling))]
#[unstable(feature = "string_replace_in_place", issue = "147949")]
pub fn replace_first<P: Pattern>(&mut self, from: P, to: &str) {
let range = match self.match_indices(from).next() {
Some((start, match_str)) => start..start + match_str.len(),
None => return,
};

self.replace_range(range, to);
}

/// Replaces the rightmost occurrence of a pattern with another string, in-place.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// #![feature(string_replace_in_place)]
///
/// let mut s = String::from("Test Results: ❌❌❌");
///
/// // Replace the rightmost ❌ with a ✅
/// s.replace_last('❌', "✅");
/// assert_eq!(s, "Test Results: ❌❌✅");
/// ```
#[cfg(not(no_global_oom_handling))]
#[unstable(feature = "string_replace_in_place", issue = "147949")]
pub fn replace_last<P: Pattern>(&mut self, from: P, to: &str)
where
for<'a> P::Searcher<'a>: core::str::pattern::ReverseSearcher<'a>,
{
let range = match self.rmatch_indices(from).next() {
Some((start, match_str)) => start..start + match_str.len(),
None => return,
};

self.replace_range(range, to);
}

/// Converts this `String` into a <code>[Box]<[str]></code>.
///
/// Before doing the conversion, this method discards excess capacity like [`shrink_to_fit`].
Expand Down
1 change: 1 addition & 0 deletions library/alloctests/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#![feature(local_waker)]
#![feature(str_as_str)]
#![feature(strict_provenance_lints)]
#![feature(string_replace_in_place)]
#![feature(vec_deque_pop_if)]
#![feature(vec_deque_truncate_front)]
#![feature(unique_rc_arc)]
Expand Down
34 changes: 34 additions & 0 deletions library/alloctests/tests/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,40 @@ fn test_replace_range_evil_end_bound() {
assert_eq!(Ok(""), str::from_utf8(s.as_bytes()));
}

#[test]
fn test_replace_first() {
let mut s = String::from("~ First ❌ Middle ❌ Last ❌ ~");
s.replace_first("❌", "✅✅");
assert_eq!(s, "~ First ✅✅ Middle ❌ Last ❌ ~");
s.replace_first("🦀", "😳");
assert_eq!(s, "~ First ✅✅ Middle ❌ Last ❌ ~");

let mut s = String::from("❌");
s.replace_first('❌', "✅✅");
assert_eq!(s, "✅✅");

let mut s = String::from("");
s.replace_first('🌌', "❌");
assert_eq!(s, "");
}

#[test]
fn test_replace_last() {
let mut s = String::from("~ First ❌ Middle ❌ Last ❌ ~");
s.replace_last("❌", "✅✅");
assert_eq!(s, "~ First ❌ Middle ❌ Last ✅✅ ~");
s.replace_last("🦀", "😳");
assert_eq!(s, "~ First ❌ Middle ❌ Last ✅✅ ~");

let mut s = String::from("❌");
s.replace_last::<char>('❌', "✅✅");
assert_eq!(s, "✅✅");

let mut s = String::from("");
s.replace_last::<char>('🌌', "❌");
assert_eq!(s, "");
}

#[test]
fn test_extend_ref() {
let mut a = "foo".to_string();
Expand Down
19 changes: 19 additions & 0 deletions library/std/src/os/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,20 @@ pub trait CommandExt: Sealed {
/// [1]: https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfoa
#[unstable(feature = "windows_process_extensions_startupinfo", issue = "141010")]
fn startupinfo_force_feedback(&mut self, enabled: Option<bool>) -> &mut process::Command;

/// If this flag is set to `true`, each inheritable handle in the calling
/// process is inherited by the new process. If the flag is `false`, the
/// handles are not inherited.
///
/// The default value for this flag is `true`.
///
/// **Note** that inherited handles have the same value and access rights
/// as the original handles. For additional discussion of inheritable handles,
/// see the [Remarks][1] section of the `CreateProcessW` documentation.
///
/// [1]: https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw#remarks
#[unstable(feature = "windows_process_extensions_inherit_handles", issue = "146407")]
fn inherit_handles(&mut self, inherit_handles: bool) -> &mut process::Command;
}

#[stable(feature = "windows_process_extensions", since = "1.16.0")]
Expand Down Expand Up @@ -421,6 +435,11 @@ impl CommandExt for process::Command {
self.as_inner_mut().startupinfo_force_feedback(enabled);
self
}

fn inherit_handles(&mut self, inherit_handles: bool) -> &mut process::Command {
self.as_inner_mut().inherit_handles(inherit_handles);
self
}
}

#[unstable(feature = "windows_process_extensions_main_thread_handle", issue = "96723")]
Expand Down
5 changes: 1 addition & 4 deletions library/std/src/sys/pal/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

use crate::io::ErrorKind;

#[cfg(not(target_os = "espidf"))]
#[macro_use]
pub mod weak;

#[cfg(target_os = "fuchsia")]
pub mod fuchsia;
pub mod futex;
Expand All @@ -19,6 +15,7 @@ pub mod stack_overflow;
pub mod sync;
pub mod thread_parking;
pub mod time;
pub mod weak;

#[cfg(target_os = "espidf")]
pub fn init(_argc: isize, _argv: *const *const u8, _sigpipe: u8) {}
Expand Down
45 changes: 17 additions & 28 deletions library/std/src/sys/pal/unix/stack_overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ mod imp {
use super::Handler;
use super::thread_info::{delete_current_info, set_current_info, with_current_info};
use crate::ops::Range;
use crate::sync::OnceLock;
use crate::sync::atomic::{Atomic, AtomicBool, AtomicPtr, AtomicUsize, Ordering};
use crate::sys::pal::unix::os;
use crate::{io, mem, ptr};
Expand Down Expand Up @@ -406,6 +405,10 @@ mod imp {
} else if cfg!(all(target_os = "linux", target_env = "musl")) {
install_main_guard_linux_musl(page_size)
} else if cfg!(target_os = "freebsd") {
#[cfg(not(target_os = "freebsd"))]
return None;
// The FreeBSD code cannot be checked on non-BSDs.
#[cfg(target_os = "freebsd")]
install_main_guard_freebsd(page_size)
} else if cfg!(any(target_os = "netbsd", target_os = "openbsd")) {
install_main_guard_bsds(page_size)
Expand Down Expand Up @@ -442,6 +445,7 @@ mod imp {
}

#[forbid(unsafe_op_in_unsafe_fn)]
#[cfg(target_os = "freebsd")]
unsafe fn install_main_guard_freebsd(page_size: usize) -> Option<Range<usize>> {
// FreeBSD's stack autogrows, and optionally includes a guard page
// at the bottom. If we try to remap the bottom of the stack
Expand All @@ -453,38 +457,23 @@ mod imp {
// by the security.bsd.stack_guard_page sysctl.
// By default it is 1, checking once is enough since it is
// a boot time config value.
static PAGES: OnceLock<usize> = OnceLock::new();
static PAGES: crate::sync::OnceLock<usize> = crate::sync::OnceLock::new();

let pages = PAGES.get_or_init(|| {
use crate::sys::weak::dlsym;
dlsym!(
fn sysctlbyname(
name: *const libc::c_char,
oldp: *mut libc::c_void,
oldlenp: *mut libc::size_t,
newp: *const libc::c_void,
newlen: libc::size_t,
) -> libc::c_int;
);
let mut guard: usize = 0;
let mut size = size_of_val(&guard);
let oid = c"security.bsd.stack_guard_page";
match sysctlbyname.get() {
Some(fcn)
if unsafe {
fcn(
oid.as_ptr(),
(&raw mut guard).cast(),
&raw mut size,
ptr::null_mut(),
0,
) == 0
} =>
{
guard
}
_ => 1,
}

let r = unsafe {
libc::sysctlbyname(
oid.as_ptr(),
(&raw mut guard).cast(),
&raw mut size,
ptr::null_mut(),
0,
)
};
if r == 0 { guard } else { 1 }
});
Some(guardaddr..guardaddr + pages * page_size)
}
Expand Down
Loading
Loading