Skip to content

Commit 951e878

Browse files
authored
Unrolled build for rust-lang#124941
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 7c2012d + 404519a commit 951e878

File tree

9 files changed

+18
-21
lines changed

9 files changed

+18
-21
lines changed

library/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)]

library/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

library/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),

library/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)]

src/tools/clippy/tests/ui/from_str_radix_10.fixed

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(const_int_from_str)]
21
#![warn(clippy::from_str_radix_10)]
32

43
mod some_mod {
@@ -61,7 +60,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
6160
Ok(())
6261
}
6362

64-
fn issue_12732() {
63+
// https://github.com/rust-lang/rust-clippy/issues/12731
64+
fn issue_12731() {
6565
const A: Result<u32, std::num::ParseIntError> = u32::from_str_radix("123", 10);
6666
const B: () = {
6767
let _ = u32::from_str_radix("123", 10);

src/tools/clippy/tests/ui/from_str_radix_10.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(const_int_from_str)]
21
#![warn(clippy::from_str_radix_10)]
32

43
mod some_mod {
@@ -61,7 +60,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
6160
Ok(())
6261
}
6362

64-
fn issue_12732() {
63+
// https://github.com/rust-lang/rust-clippy/issues/12731
64+
fn issue_12731() {
6565
const A: Result<u32, std::num::ParseIntError> = u32::from_str_radix("123", 10);
6666
const B: () = {
6767
let _ = u32::from_str_radix("123", 10);

src/tools/clippy/tests/ui/from_str_radix_10.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this call to `from_str_radix` can be replaced with a call to `str::parse`
2-
--> tests/ui/from_str_radix_10.rs:29:5
2+
--> tests/ui/from_str_radix_10.rs:28:5
33
|
44
LL | u32::from_str_radix("30", 10)?;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `"30".parse::<u32>()`
@@ -8,43 +8,43 @@ LL | u32::from_str_radix("30", 10)?;
88
= help: to override `-D warnings` add `#[allow(clippy::from_str_radix_10)]`
99

1010
error: this call to `from_str_radix` can be replaced with a call to `str::parse`
11-
--> tests/ui/from_str_radix_10.rs:32:5
11+
--> tests/ui/from_str_radix_10.rs:31:5
1212
|
1313
LL | i64::from_str_radix("24", 10)?;
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `"24".parse::<i64>()`
1515

1616
error: this call to `from_str_radix` can be replaced with a call to `str::parse`
17-
--> tests/ui/from_str_radix_10.rs:34:5
17+
--> tests/ui/from_str_radix_10.rs:33:5
1818
|
1919
LL | isize::from_str_radix("100", 10)?;
2020
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `"100".parse::<isize>()`
2121

2222
error: this call to `from_str_radix` can be replaced with a call to `str::parse`
23-
--> tests/ui/from_str_radix_10.rs:36:5
23+
--> tests/ui/from_str_radix_10.rs:35:5
2424
|
2525
LL | u8::from_str_radix("7", 10)?;
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `"7".parse::<u8>()`
2727

2828
error: this call to `from_str_radix` can be replaced with a call to `str::parse`
29-
--> tests/ui/from_str_radix_10.rs:38:5
29+
--> tests/ui/from_str_radix_10.rs:37:5
3030
|
3131
LL | u16::from_str_radix(&("10".to_owned() + "5"), 10)?;
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `("10".to_owned() + "5").parse::<u16>()`
3333

3434
error: this call to `from_str_radix` can be replaced with a call to `str::parse`
35-
--> tests/ui/from_str_radix_10.rs:40:5
35+
--> tests/ui/from_str_radix_10.rs:39:5
3636
|
3737
LL | i128::from_str_radix(Test + Test, 10)?;
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(Test + Test).parse::<i128>()`
3939

4040
error: this call to `from_str_radix` can be replaced with a call to `str::parse`
41-
--> tests/ui/from_str_radix_10.rs:44:5
41+
--> tests/ui/from_str_radix_10.rs:43:5
4242
|
4343
LL | i32::from_str_radix(string, 10)?;
4444
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `string.parse::<i32>()`
4545

4646
error: this call to `from_str_radix` can be replaced with a call to `str::parse`
47-
--> tests/ui/from_str_radix_10.rs:48:5
47+
--> tests/ui/from_str_radix_10.rs:47:5
4848
|
4949
LL | i32::from_str_radix(&stringier, 10)?;
5050
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `stringier.parse::<i32>()`

tests/ui/consts/const-eval/parse_ints.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(const_int_from_str)]
2-
31
const _OK: () = match i32::from_str_radix("-1234", 10) {
42
Ok(x) => assert!(x == -1234),
53
Err(_) => panic!(),

tests/ui/consts/const-eval/parse_ints.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ error[E0080]: evaluation of constant value failed
66
note: inside `core::num::<impl u64>::from_str_radix`
77
--> $SRC_DIR/core/src/num/mod.rs:LL:COL
88
note: inside `_TOO_LOW`
9-
--> $DIR/parse_ints.rs:7:24
9+
--> $DIR/parse_ints.rs:5:24
1010
|
1111
LL | const _TOO_LOW: () = { u64::from_str_radix("12345ABCD", 1); };
1212
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -20,7 +20,7 @@ error[E0080]: evaluation of constant value failed
2020
note: inside `core::num::<impl u64>::from_str_radix`
2121
--> $SRC_DIR/core/src/num/mod.rs:LL:COL
2222
note: inside `_TOO_HIGH`
23-
--> $DIR/parse_ints.rs:8:25
23+
--> $DIR/parse_ints.rs:6:25
2424
|
2525
LL | const _TOO_HIGH: () = { u64::from_str_radix("12345ABCD", 37); };
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)