Skip to content

Commit

Permalink
Auto merge of rust-lang#131724 - matthiaskrgr:rollup-ntgkkk8, r=matth…
Browse files Browse the repository at this point in the history
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#130608 (Implemented `FromStr` for `CString` and `TryFrom<CString>` for `String`)
 - rust-lang#130635 (Add `&pin (mut|const) T` type position sugar)
 - rust-lang#130747 (improve error messages for `C-cmse-nonsecure-entry` functions)
 - rust-lang#131137 (Add 1.82 release notes)
 - rust-lang#131328 (Remove unnecessary sorts in `rustc_hir_analysis`)
 - rust-lang#131496 (Stabilise `const_make_ascii`.)
 - rust-lang#131706 (Fix two const-hacks)

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Oct 15, 2024
2 parents cc7730e + da7ca22 commit 76342d9
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 30 deletions.
26 changes: 25 additions & 1 deletion alloc/src/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use core::borrow::Borrow;
use core::ffi::{CStr, c_char};
use core::num::NonZero;
use core::slice::memchr;
use core::str::{self, Utf8Error};
use core::str::{self, FromStr, Utf8Error};
use core::{fmt, mem, ops, ptr, slice};

use crate::borrow::{Cow, ToOwned};
Expand Down Expand Up @@ -817,6 +817,30 @@ impl From<Vec<NonZero<u8>>> for CString {
}
}

impl FromStr for CString {
type Err = NulError;

/// Converts a string `s` into a [`CString`].
///
/// This method is equivalent to [`CString::new`].
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::new(s)
}
}

impl TryFrom<CString> for String {
type Error = IntoStringError;

/// Converts a [`CString`] into a [`String`] if it contains valid UTF-8 data.
///
/// This method is equivalent to [`CString::into_string`].
#[inline]
fn try_from(value: CString) -> Result<Self, Self::Error> {
value.into_string()
}
}

#[cfg(not(test))]
#[stable(feature = "more_box_slice_clone", since = "1.29.0")]
impl Clone for Box<CStr> {
Expand Down
6 changes: 4 additions & 2 deletions core/src/char/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1282,8 +1282,9 @@ impl char {
///
/// [`to_ascii_uppercase()`]: #method.to_ascii_uppercase
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
#[rustc_const_unstable(feature = "const_make_ascii", issue = "130698")]
#[rustc_const_stable(feature = "const_make_ascii", since = "CURRENT_RUSTC_VERSION")]
#[inline]
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
pub const fn make_ascii_uppercase(&mut self) {
*self = self.to_ascii_uppercase();
}
Expand All @@ -1308,8 +1309,9 @@ impl char {
///
/// [`to_ascii_lowercase()`]: #method.to_ascii_lowercase
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
#[rustc_const_unstable(feature = "const_make_ascii", issue = "130698")]
#[rustc_const_stable(feature = "const_make_ascii", since = "CURRENT_RUSTC_VERSION")]
#[inline]
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
pub const fn make_ascii_lowercase(&mut self) {
*self = self.to_ascii_lowercase();
}
Expand Down
1 change: 0 additions & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@
#![feature(const_heap)]
#![feature(const_index_range_slice_index)]
#![feature(const_likely)]
#![feature(const_make_ascii)]
#![feature(const_nonnull_new)]
#![feature(const_num_midpoint)]
#![feature(const_option_ext)]
Expand Down
6 changes: 4 additions & 2 deletions core/src/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,8 +624,9 @@ impl u8 {
///
/// [`to_ascii_uppercase`]: Self::to_ascii_uppercase
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
#[rustc_const_unstable(feature = "const_make_ascii", issue = "130698")]
#[rustc_const_stable(feature = "const_make_ascii", since = "CURRENT_RUSTC_VERSION")]
#[inline]
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
pub const fn make_ascii_uppercase(&mut self) {
*self = self.to_ascii_uppercase();
}
Expand All @@ -650,8 +651,9 @@ impl u8 {
///
/// [`to_ascii_lowercase`]: Self::to_ascii_lowercase
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
#[rustc_const_unstable(feature = "const_make_ascii", issue = "130698")]
#[rustc_const_stable(feature = "const_make_ascii", since = "CURRENT_RUSTC_VERSION")]
#[inline]
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
pub const fn make_ascii_lowercase(&mut self) {
*self = self.to_ascii_lowercase();
}
Expand Down
6 changes: 4 additions & 2 deletions core/src/slice/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ impl [u8] {
///
/// [`to_ascii_uppercase`]: #method.to_ascii_uppercase
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
#[rustc_const_unstable(feature = "const_make_ascii", issue = "130698")]
#[rustc_const_stable(feature = "const_make_ascii", since = "CURRENT_RUSTC_VERSION")]
#[inline]
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
pub const fn make_ascii_uppercase(&mut self) {
// FIXME(const-hack): We would like to simply iterate using `for` loops but this isn't currently allowed in constant expressions.
let mut i = 0;
Expand All @@ -89,8 +90,9 @@ impl [u8] {
///
/// [`to_ascii_lowercase`]: #method.to_ascii_lowercase
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
#[rustc_const_unstable(feature = "const_make_ascii", issue = "130698")]
#[rustc_const_stable(feature = "const_make_ascii", since = "CURRENT_RUSTC_VERSION")]
#[inline]
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
pub const fn make_ascii_lowercase(&mut self) {
// FIXME(const-hack): We would like to simply iterate using `for` loops but this isn't currently allowed in constant expressions.
let mut i = 0;
Expand Down
6 changes: 4 additions & 2 deletions core/src/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2475,8 +2475,9 @@ impl str {
/// assert_eq!("GRüßE, JüRGEN ❤", s);
/// ```
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
#[rustc_const_unstable(feature = "const_make_ascii", issue = "130698")]
#[rustc_const_stable(feature = "const_make_ascii", since = "CURRENT_RUSTC_VERSION")]
#[inline]
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
pub const fn make_ascii_uppercase(&mut self) {
// SAFETY: changing ASCII letters only does not invalidate UTF-8.
let me = unsafe { self.as_bytes_mut() };
Expand All @@ -2503,8 +2504,9 @@ impl str {
/// assert_eq!("grÜße, jÜrgen ❤", s);
/// ```
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
#[rustc_const_unstable(feature = "const_make_ascii", issue = "130698")]
#[rustc_const_stable(feature = "const_make_ascii", since = "CURRENT_RUSTC_VERSION")]
#[inline]
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
pub const fn make_ascii_lowercase(&mut self) {
// SAFETY: changing ASCII letters only does not invalidate UTF-8.
let me = unsafe { self.as_bytes_mut() };
Expand Down
8 changes: 3 additions & 5 deletions core/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,9 @@ impl Duration {
// SAFETY: nanos < NANOS_PER_SEC, therefore nanos is within the valid range
Duration { secs, nanos: unsafe { Nanoseconds(nanos) } }
} else {
// FIXME(const-hack): use `.expect` once that is possible.
let secs = match secs.checked_add((nanos / NANOS_PER_SEC) as u64) {
Some(secs) => secs,
None => panic!("overflow in Duration::new"),
};
let secs = secs
.checked_add((nanos / NANOS_PER_SEC) as u64)
.expect("overflow in Duration::new");
let nanos = nanos % NANOS_PER_SEC;
// SAFETY: nanos % NANOS_PER_SEC < NANOS_PER_SEC, therefore nanos is within the valid range
Duration { secs, nanos: unsafe { Nanoseconds(nanos) } }
Expand Down
19 changes: 4 additions & 15 deletions std/src/sys/pal/windows/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,6 @@ use crate::sys_common::AsInner;
use crate::sys_common::wstr::WStrUnits;
use crate::{fmt, io, iter, vec};

/// This is the const equivalent to `NonZero::new(n).unwrap()`
///
/// FIXME(const-hack): This can be removed once `Option::unwrap` is stably const.
/// See the `const_option` feature (#67441).
const fn non_zero_u16(n: u16) -> NonZero<u16> {
match NonZero::new(n) {
Some(n) => n,
None => panic!("called `unwrap` on a `None` value"),
}
}

pub fn args() -> Args {
// SAFETY: `GetCommandLineW` returns a pointer to a null terminated UTF-16
// string so it's safe for `WStrUnits` to use.
Expand Down Expand Up @@ -66,10 +55,10 @@ fn parse_lp_cmd_line<'a, F: Fn() -> OsString>(
lp_cmd_line: Option<WStrUnits<'a>>,
exe_name: F,
) -> Vec<OsString> {
const BACKSLASH: NonZero<u16> = non_zero_u16(b'\\' as u16);
const QUOTE: NonZero<u16> = non_zero_u16(b'"' as u16);
const TAB: NonZero<u16> = non_zero_u16(b'\t' as u16);
const SPACE: NonZero<u16> = non_zero_u16(b' ' as u16);
const BACKSLASH: NonZero<u16> = NonZero::new(b'\\' as u16).unwrap();
const QUOTE: NonZero<u16> = NonZero::new(b'"' as u16).unwrap();
const TAB: NonZero<u16> = NonZero::new(b'\t' as u16).unwrap();
const SPACE: NonZero<u16> = NonZero::new(b' ' as u16).unwrap();

let mut ret_val = Vec::new();
// If the cmd line pointer is null or it points to an empty string then
Expand Down

0 comments on commit 76342d9

Please sign in to comment.