Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a2dbfd3

Browse files
authoredJul 26, 2024
Rollup merge of rust-lang#124941 - Skgland:stabilize-const-int-from-str, r=dtolnay
Stabilize const `{integer}::from_str_radix` i.e. `const_int_from_str` This PR stabilizes the feature `const_int_from_str`. - ACP Issue: rust-lang/libs-team#74 - Implementation PR: rust-lang#99322 - Part of Tracking Issue: rust-lang#59133 API Change Diff: ```diff impl {integer} { - pub fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>; + pub const fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>; } impl ParseIntError { - pub fn kind(&self) -> &IntErrorKind; + pub const fn kind(&self) -> &IntErrorKind; } ``` This makes it easier to parse integers at compile-time, e.g. the example from the Tracking Issue: ```rust env!("SOMETHING").parse::<usize>().unwrap() ``` could now be achived with ```rust match usize::from_str_radix(env!("SOMETHING"), 10) { Ok(val) => val, Err(err) => panic!("Invalid value for SOMETHING environment variable."), } ``` rather than having to depend on a library that implements or manually implement the parsing at compile-time. --- Checklist based on [Libs Stabilization Guide - When there's const involved](https://std-dev-guide.rust-lang.org/development/stabilization.html#when-theres-const-involved) I am treating this as a [partial stabilization](https://std-dev-guide.rust-lang.org/development/stabilization.html#partial-stabilizations) as it shares a tracking issue (and is rather small), so directly opening the partial stabilization PR for the subset (feature `const_int_from_str`) being stabilized. - [x] ping Constant Evaluation WG - [x] no unsafe involved - [x] no `#[allow_internal_unstable]` - [ ] usage of `intrinsic::const_eval_select` rust-lang#124625 in `from_str_radix_assert` to change the error message between compile-time and run-time - [ ] [rust-labg/libs-api FCP](rust-lang#124941 (comment))
2 parents 22ce603 + 6b549ba commit a2dbfd3

File tree

4 files changed

+4
-5
lines changed

4 files changed

+4
-5
lines changed
 

‎core/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@
127127
#![feature(const_hash)]
128128
#![feature(const_heap)]
129129
#![feature(const_index_range_slice_index)]
130-
#![feature(const_int_from_str)]
131130
#![feature(const_intrinsic_copy)]
132131
#![feature(const_intrinsic_forget)]
133132
#![feature(const_ipv4)]

‎core/src/num/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ pub enum IntErrorKind {
113113
impl ParseIntError {
114114
/// Outputs the detailed cause of parsing an integer failing.
115115
#[must_use]
116-
#[rustc_const_unstable(feature = "const_int_from_str", issue = "59133")]
116+
#[rustc_const_stable(feature = "const_int_from_str", since = "CURRENT_RUSTC_VERSION")]
117117
#[stable(feature = "int_error_matching", since = "1.55.0")]
118118
pub const fn kind(&self) -> &IntErrorKind {
119119
&self.kind

‎core/src/num/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,7 @@ from_str_radix_int_impl! { isize i8 i16 i32 i64 i128 usize u8 u16 u32 u64 u128 }
13861386
#[doc(hidden)]
13871387
#[inline(always)]
13881388
#[unstable(issue = "none", feature = "std_internals")]
1389+
#[rustc_const_stable(feature = "const_int_from_str", since = "CURRENT_RUSTC_VERSION")]
13891390
pub const fn can_not_overflow<T>(radix: u32, is_signed_ty: bool, digits: &[u8]) -> bool {
13901391
radix <= 16 && digits.len() <= mem::size_of::<T>() * 2 - is_signed_ty as usize
13911392
}
@@ -1435,7 +1436,7 @@ macro_rules! from_str_radix {
14351436
#[doc = concat!("assert_eq!(", stringify!($int_ty), "::from_str_radix(\"A\", 16), Ok(10));")]
14361437
/// ```
14371438
#[stable(feature = "rust1", since = "1.0.0")]
1438-
#[rustc_const_unstable(feature = "const_int_from_str", issue = "59133")]
1439+
#[rustc_const_stable(feature = "const_int_from_str", since = "CURRENT_RUSTC_VERSION")]
14391440
pub const fn from_str_radix(src: &str, radix: u32) -> Result<$int_ty, ParseIntError> {
14401441
use self::IntErrorKind::*;
14411442
use self::ParseIntError as PIE;
@@ -1565,7 +1566,7 @@ macro_rules! from_str_radix_size_impl {
15651566
#[doc = concat!("assert_eq!(", stringify!($size), "::from_str_radix(\"A\", 16), Ok(10));")]
15661567
/// ```
15671568
#[stable(feature = "rust1", since = "1.0.0")]
1568-
#[rustc_const_unstable(feature = "const_int_from_str", issue = "59133")]
1569+
#[rustc_const_stable(feature = "const_int_from_str", since = "CURRENT_RUSTC_VERSION")]
15691570
pub const fn from_str_radix(src: &str, radix: u32) -> Result<$size, ParseIntError> {
15701571
match <$t>::from_str_radix(src, radix) {
15711572
Ok(x) => Ok(x as $size),

‎core/tests/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#![feature(const_hash)]
1717
#![feature(const_heap)]
1818
#![feature(const_intrinsic_copy)]
19-
#![feature(const_int_from_str)]
2019
#![feature(const_maybe_uninit_as_mut_ptr)]
2120
#![feature(const_nonnull_new)]
2221
#![feature(const_pointer_is_aligned)]

0 commit comments

Comments
 (0)
Failed to load comments.