Auto merge of #131724 - matthiaskrgr:rollup-ntgkkk8, r=matthiaskrgr · qinheping/verify-rust-std@76342d9 (original) (raw)
8 files changed
lines changed
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -7,7 +7,7 @@ use core::borrow::Borrow; | ||
| 7 | 7 | use core::ffi::{CStr, c_char}; |
| 8 | 8 | use core::num::NonZero; |
| 9 | 9 | use core::slice::memchr; |
| 10 | -use core::str::{self, Utf8Error}; | |
| 10 | +use core::str::{self, FromStr, Utf8Error}; | |
| 11 | 11 | use core::{fmt, mem, ops, ptr, slice}; |
| 12 | 12 | |
| 13 | 13 | use crate::borrow::{Cow, ToOwned}; |
| @@ -817,6 +817,30 @@ impl From<Vec<NonZero>> for CString { | ||
| 817 | 817 | } |
| 818 | 818 | } |
| 819 | 819 | |
| 820 | +impl FromStr for CString { | |
| 821 | +type Err = NulError; | |
| 822 | + | |
| 823 | +/// Converts a string `s` into a [`CString`]. | |
| 824 | + /// | |
| 825 | + /// This method is equivalent to [`CString::new`]. | |
| 826 | + #[inline] | |
| 827 | +fn from_str(s: &str) -> Result<Self, Self::Err> { | |
| 828 | +Self::new(s) | |
| 829 | +} | |
| 830 | +} | |
| 831 | + | |
| 832 | +impl TryFrom<CString> for String { | |
| 833 | +type Error = IntoStringError; | |
| 834 | + | |
| 835 | +/// Converts a [`CString`] into a [`String`] if it contains valid UTF-8 data. | |
| 836 | + /// | |
| 837 | + /// This method is equivalent to [`CString::into_string`]. | |
| 838 | + #[inline] | |
| 839 | +fn try_from(value: CString) -> Result<Self, Self::Error> { | |
| 840 | + value.into_string() | |
| 841 | +} | |
| 842 | +} | |
| 843 | + | |
| 820 | 844 | #[cfg(not(test))] |
| 821 | 845 | #[stable(feature = "more_box_slice_clone", since = "1.29.0")] |
| 822 | 846 | impl Clone for Box<CStr> { |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1282,8 +1282,9 @@ impl char { | ||
| 1282 | 1282 | /// |
| 1283 | 1283 | /// [`to_ascii_uppercase()`]: #method.to_ascii_uppercase |
| 1284 | 1284 | #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] |
| 1285 | -#[rustc_const_unstable(feature = "const_make_ascii", issue = "130698")] | |
| 1285 | +#[rustc_const_stable(feature = "const_make_ascii", since = "CURRENT_RUSTC_VERSION")] | |
| 1286 | 1286 | #[inline] |
| 1287 | +#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))] | |
| 1287 | 1288 | pub const fn make_ascii_uppercase(&mut self) { |
| 1288 | 1289 | *self = self.to_ascii_uppercase(); |
| 1289 | 1290 | } |
| @@ -1308,8 +1309,9 @@ impl char { | ||
| 1308 | 1309 | /// |
| 1309 | 1310 | /// [`to_ascii_lowercase()`]: #method.to_ascii_lowercase |
| 1310 | 1311 | #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] |
| 1311 | -#[rustc_const_unstable(feature = "const_make_ascii", issue = "130698")] | |
| 1312 | +#[rustc_const_stable(feature = "const_make_ascii", since = "CURRENT_RUSTC_VERSION")] | |
| 1312 | 1313 | #[inline] |
| 1314 | +#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))] | |
| 1313 | 1315 | pub const fn make_ascii_lowercase(&mut self) { |
| 1314 | 1316 | *self = self.to_ascii_lowercase(); |
| 1315 | 1317 | } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -125,7 +125,6 @@ | ||
| 125 | 125 | #![feature(const_heap)] |
| 126 | 126 | #![feature(const_index_range_slice_index)] |
| 127 | 127 | #![feature(const_likely)] |
| 128 | -#![feature(const_make_ascii)] | |
| 129 | 128 | #![feature(const_nonnull_new)] |
| 130 | 129 | #![feature(const_num_midpoint)] |
| 131 | 130 | #![feature(const_option_ext)] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -624,8 +624,9 @@ impl u8 { | ||
| 624 | 624 | /// |
| 625 | 625 | /// [`to_ascii_uppercase`]: Self::to_ascii_uppercase |
| 626 | 626 | #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] |
| 627 | -#[rustc_const_unstable(feature = "const_make_ascii", issue = "130698")] | |
| 627 | +#[rustc_const_stable(feature = "const_make_ascii", since = "CURRENT_RUSTC_VERSION")] | |
| 628 | 628 | #[inline] |
| 629 | +#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))] | |
| 629 | 630 | pub const fn make_ascii_uppercase(&mut self) { |
| 630 | 631 | *self = self.to_ascii_uppercase(); |
| 631 | 632 | } |
| @@ -650,8 +651,9 @@ impl u8 { | ||
| 650 | 651 | /// |
| 651 | 652 | /// [`to_ascii_lowercase`]: Self::to_ascii_lowercase |
| 652 | 653 | #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] |
| 653 | -#[rustc_const_unstable(feature = "const_make_ascii", issue = "130698")] | |
| 654 | +#[rustc_const_stable(feature = "const_make_ascii", since = "CURRENT_RUSTC_VERSION")] | |
| 654 | 655 | #[inline] |
| 656 | +#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))] | |
| 655 | 657 | pub const fn make_ascii_lowercase(&mut self) { |
| 656 | 658 | *self = self.to_ascii_lowercase(); |
| 657 | 659 | } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -67,8 +67,9 @@ impl [u8] { | ||
| 67 | 67 | /// |
| 68 | 68 | /// [`to_ascii_uppercase`]: #method.to_ascii_uppercase |
| 69 | 69 | #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] |
| 70 | -#[rustc_const_unstable(feature = "const_make_ascii", issue = "130698")] | |
| 70 | +#[rustc_const_stable(feature = "const_make_ascii", since = "CURRENT_RUSTC_VERSION")] | |
| 71 | 71 | #[inline] |
| 72 | +#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))] | |
| 72 | 73 | pub const fn make_ascii_uppercase(&mut self) { |
| 73 | 74 | // FIXME(const-hack): We would like to simply iterate using `for` loops but this isn't currently allowed in constant expressions. |
| 74 | 75 | let mut i = 0; |
| @@ -89,8 +90,9 @@ impl [u8] { | ||
| 89 | 90 | /// |
| 90 | 91 | /// [`to_ascii_lowercase`]: #method.to_ascii_lowercase |
| 91 | 92 | #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] |
| 92 | -#[rustc_const_unstable(feature = "const_make_ascii", issue = "130698")] | |
| 93 | +#[rustc_const_stable(feature = "const_make_ascii", since = "CURRENT_RUSTC_VERSION")] | |
| 93 | 94 | #[inline] |
| 95 | +#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))] | |
| 94 | 96 | pub const fn make_ascii_lowercase(&mut self) { |
| 95 | 97 | // FIXME(const-hack): We would like to simply iterate using `for` loops but this isn't currently allowed in constant expressions. |
| 96 | 98 | let mut i = 0; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2475,8 +2475,9 @@ impl str { | ||
| 2475 | 2475 | /// assert_eq!("GRüßE, JüRGEN ❤", s); |
| 2476 | 2476 | /// ``` |
| 2477 | 2477 | #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] |
| 2478 | -#[rustc_const_unstable(feature = "const_make_ascii", issue = "130698")] | |
| 2478 | +#[rustc_const_stable(feature = "const_make_ascii", since = "CURRENT_RUSTC_VERSION")] | |
| 2479 | 2479 | #[inline] |
| 2480 | +#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))] | |
| 2480 | 2481 | pub const fn make_ascii_uppercase(&mut self) { |
| 2481 | 2482 | // SAFETY: changing ASCII letters only does not invalidate UTF-8. |
| 2482 | 2483 | let me = unsafe { self.as_bytes_mut() }; |
| @@ -2503,8 +2504,9 @@ impl str { | ||
| 2503 | 2504 | /// assert_eq!("grÜße, jÜrgen ❤", s); |
| 2504 | 2505 | /// ``` |
| 2505 | 2506 | #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] |
| 2506 | -#[rustc_const_unstable(feature = "const_make_ascii", issue = "130698")] | |
| 2507 | +#[rustc_const_stable(feature = "const_make_ascii", since = "CURRENT_RUSTC_VERSION")] | |
| 2507 | 2508 | #[inline] |
| 2509 | +#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))] | |
| 2508 | 2510 | pub const fn make_ascii_lowercase(&mut self) { |
| 2509 | 2511 | // SAFETY: changing ASCII letters only does not invalidate UTF-8. |
| 2510 | 2512 | let me = unsafe { self.as_bytes_mut() }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -213,11 +213,9 @@ impl Duration { | ||
| 213 | 213 | // SAFETY: nanos < NANOS_PER_SEC, therefore nanos is within the valid range |
| 214 | 214 | Duration { secs, nanos: unsafe { Nanoseconds(nanos) } } |
| 215 | 215 | } else { |
| 216 | -// FIXME(const-hack): use `.expect` once that is possible. | |
| 217 | -let secs = match secs.checked_add((nanos / NANOS_PER_SEC) as u64) { | |
| 218 | -Some(secs) => secs, | |
| 219 | -None => panic!("overflow in Duration::new"), | |
| 220 | -}; | |
| 216 | +let secs = secs | |
| 217 | +.checked_add((nanos / NANOS_PER_SEC) as u64) | |
| 218 | +.expect("overflow in Duration::new"); | |
| 221 | 219 | let nanos = nanos % NANOS_PER_SEC; |
| 222 | 220 | // SAFETY: nanos % NANOS_PER_SEC < NANOS_PER_SEC, therefore nanos is within the valid range |
| 223 | 221 | Duration { secs, nanos: unsafe { Nanoseconds(nanos) } } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -18,17 +18,6 @@ use crate::sys_common::AsInner; | ||
| 18 | 18 | use crate::sys_common::wstr::WStrUnits; |
| 19 | 19 | use crate::{fmt, io, iter, vec}; |
| 20 | 20 | |
| 21 | -/// This is the const equivalent to `NonZero::new(n).unwrap()` | |
| 22 | -/// | |
| 23 | -/// FIXME(const-hack): This can be removed once `Option::unwrap` is stably const. | |
| 24 | -/// See the `const_option` feature (#67441). | |
| 25 | -const fn non_zero_u16(n: u16) -> NonZero<u16> { | |
| 26 | -match NonZero::new(n) { | |
| 27 | -Some(n) => n, | |
| 28 | -None => panic!("called `unwrap` on a `None` value"), | |
| 29 | -} | |
| 30 | -} | |
| 31 | - | |
| 32 | 21 | pub fn args() -> Args { |
| 33 | 22 | // SAFETY: `GetCommandLineW` returns a pointer to a null terminated UTF-16 |
| 34 | 23 | // string so it's safe for `WStrUnits` to use. |
| @@ -66,10 +55,10 @@ fn parse_lp_cmd_line<'a, F: Fn() -> OsString>( | ||
| 66 | 55 | lp_cmd_line: Option<WStrUnits<'a>>, |
| 67 | 56 | exe_name: F, |
| 68 | 57 | ) -> Vec<OsString> { |
| 69 | -const BACKSLASH: NonZero<u16> = non_zero_u16(b'\\' as u16); | |
| 70 | -const QUOTE: NonZero<u16> = non_zero_u16(b'"' as u16); | |
| 71 | -const TAB: NonZero<u16> = non_zero_u16(b'\t' as u16); | |
| 72 | -const SPACE: NonZero<u16> = non_zero_u16(b' ' as u16); | |
| 58 | +const BACKSLASH: NonZero<u16> = NonZero::new(b'\\' as u16).unwrap(); | |
| 59 | +const QUOTE: NonZero<u16> = NonZero::new(b'"' as u16).unwrap(); | |
| 60 | +const TAB: NonZero<u16> = NonZero::new(b'\t' as u16).unwrap(); | |
| 61 | +const SPACE: NonZero<u16> = NonZero::new(b' ' as u16).unwrap(); | |
| 73 | 62 | |
| 74 | 63 | let mut ret_val = Vec::new(); |
| 75 | 64 | // If the cmd line pointer is null or it points to an empty string then |