Implement `int_format_into` feature
I took over rust-lang/rust#138338 with @madhav-madhusoodanan's approval.
Since https://github.com/rust-lang/rust/pull/136264, a lot of changes happened so I made use of them to reduce the number of changes.
ACP approval: https://github.com/rust-lang/libs-team/issues/546#issuecomment-2707244569
Associated Issue
- https://github.com/rust-lang/rust/issues/138215
r? @hanna-kruppe
Failed to set assignee to hanna-kruppe: invalid assignee
Note: Only org members with at least the repository "read" role, users with write permissions, or people who have commented on the PR may be assigned.
r? @Amanieu
The job mingw-check-tidy failed! Check out the build log: (web) (plain)
Click to see the possible cause of the failure (guessed by this bot)
info: removing rustup binaries
info: rustup is uninstalled
##[group]Image checksum input
mingw-check-tidy
# We use the ghcr base image because ghcr doesn't have a rate limit
# and the mingw-check-tidy job doesn't cache docker images in CI.
FROM ghcr.io/rust-lang/ubuntu:22.04
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y --no-install-recommends \
g++ \
---
COPY host-x86_64/mingw-check-1/validate-toolstate.sh /scripts/
COPY host-x86_64/mingw-check-1/validate-error-codes.sh /scripts/
# NOTE: intentionally uses python2 for x.py so we can test it still works.
# validate-toolstate only runs in our CI, so it's ok for it to only support python3.
ENV SCRIPT TIDY_PRINT_DIFF=1 npm install eslint@$(head -n 1 /tmp/eslint.version) && \
python2.7 ../x.py test --stage 0 src/tools/tidy tidyselftest --extra-checks=py,cpp
#
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
# pip-compile --allow-unsafe --generate-hashes reuse-requirements.in
---
#15 3.032 Building wheels for collected packages: reuse
#15 3.034 Building wheel for reuse (pyproject.toml): started
#15 3.259 Building wheel for reuse (pyproject.toml): finished with status 'done'
#15 3.260 Created wheel for reuse: filename=reuse-4.0.3-cp310-cp310-manylinux_2_35_x86_64.whl size=132719 sha256=d2a2565e7037ad3883fb9337653f2e25bbb588534fbef3697286cbc26d1bf634
#15 3.261 Stored in directory: /tmp/pip-ephem-wheel-cache-ffc0mbaq/wheels/3d/8d/0a/e0fc6aba4494b28a967ab5eaf951c121d9c677958714e34532
#15 3.263 Successfully built reuse
#15 3.263 Installing collected packages: boolean-py, binaryornot, tomlkit, reuse, python-debian, markupsafe, license-expression, jinja2, chardet, attrs
#15 3.679 Successfully installed attrs-23.2.0 binaryornot-0.4.4 boolean-py-4.0 chardet-5.2.0 jinja2-3.1.4 license-expression-30.3.0 markupsafe-2.1.5 python-debian-0.1.49 reuse-4.0.3 tomlkit-0.13.0
#15 3.679 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
#15 4.246 Collecting virtualenv
#15 4.317 Downloading virtualenv-20.31.2-py3-none-any.whl (6.1 MB)
#15 4.435 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.1/6.1 MB 52.5 MB/s eta 0:00:00
#15 4.498 Collecting filelock<4,>=3.12.2
#15 4.513 Downloading filelock-3.18.0-py3-none-any.whl (16 kB)
#15 4.552 Collecting platformdirs<5,>=3.9.1
#15 4.567 Downloading platformdirs-4.3.8-py3-none-any.whl (18 kB)
#15 4.592 Collecting distlib<1,>=0.3.7
#15 4.607 Downloading distlib-0.3.9-py2.py3-none-any.whl (468 kB)
#15 4.615 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 469.0/469.0 KB 89.9 MB/s eta 0:00:00
#15 4.698 Installing collected packages: distlib, platformdirs, filelock, virtualenv
#15 4.895 Successfully installed distlib-0.3.9 filelock-3.18.0 platformdirs-4.3.8 virtualenv-20.31.2
#15 4.895 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
#15 DONE 5.0s
#16 [10/11] COPY host-x86_64/mingw-check-1/validate-toolstate.sh /scripts/
#16 DONE 0.0s
---
DirectMap4k: 135104 kB
DirectMap2M: 9302016 kB
DirectMap1G: 9437184 kB
##[endgroup]
Executing TIDY_PRINT_DIFF=1 npm install eslint@$(head -n 1 /tmp/eslint.version) && python2.7 ../x.py test --stage 0 src/tools/tidy tidyselftest --extra-checks=py,cpp
+ head -n 1 /tmp/eslint.version
+ TIDY_PRINT_DIFF=1 npm install [email protected]
npm WARN deprecated [email protected]: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
npm WARN deprecated [email protected]: Rimraf versions prior to v4 are no longer supported
npm WARN deprecated @humanwhocodes/[email protected]: Use @eslint/config-array instead
npm WARN deprecated [email protected]: Glob versions prior to v9 are no longer supported
npm WARN deprecated @humanwhocodes/[email protected]: Use @eslint/object-schema instead
npm WARN deprecated [email protected]: This version is no longer supported. Please see https://eslint.org/version-support for other options.
added 89 packages in 3s
17 packages are looking for funding
run `npm fund` for details
+ python2.7 ../x.py test --stage 0 src/tools/tidy tidyselftest --extra-checks=py,cpp
##[group]Building bootstrap
Finished `dev` profile [unoptimized] target(s) in 0.05s
##[endgroup]
WARN: currently no CI rustc builds have rustc debug assertions enabled. Please either set `rust.debug-assertions` to `false` if you want to use download CI rustc or set `rust.download-rustc` to `false`.
WARN: `rust.debug-assertions = true` will prevent downloading CI rustc as alt CI rustc is not currently built with debug assertions.
---
[TIMING] core::build_steps::tool::Tidy { compiler: Compiler { stage: 0, host: x86_64-unknown-linux-gnu, forced_compiler: false }, target: x86_64-unknown-linux-gnu } -- 0.000
fmt check
Diff in /checkout/library/core/src/fmt/num_buffer.rs:37:
#[derive(Debug)]
pub struct NumBuffer<T: NumBufferTrait> {
// FIXME: Once const generics feature is working, use `T::BUF_SIZE` instead of 40.
- pub(crate) buf: [MaybeUninit::<u8>; 40],
+ pub(crate) buf: [MaybeUninit<u8>; 40],
// FIXME: Remove this field once we can actually use `T`.
phantom: core::marker::PhantomData<T>,
}
Diff in /checkout/library/core/src/fmt/num_buffer.rs:48:
#[unstable(feature = "int_format_into", issue = "138215")]
pub const fn new() -> Self {
// FIXME: Once const generics feature is working, use `T::BUF_SIZE` instead of 40.
- NumBuffer {
- buf: [MaybeUninit::<u8>::uninit(); 40],
- phantom: core::marker::PhantomData,
- }
+ NumBuffer { buf: [MaybeUninit::<u8>::uninit(); 40], phantom: core::marker::PhantomData }
}
/// Returns the length of the buffer.
fmt: checked 6039 files
Build completed unsuccessfully in 0:00:44
The job mingw-check-tidy failed! Check out the build log: (web) (plain)
Click to see the possible cause of the failure (guessed by this bot)
info: removing rustup binaries
info: rustup is uninstalled
##[group]Image checksum input
mingw-check-tidy
# We use the ghcr base image because ghcr doesn't have a rate limit
# and the mingw-check-tidy job doesn't cache docker images in CI.
FROM ghcr.io/rust-lang/ubuntu:22.04
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y --no-install-recommends \
g++ \
---
COPY host-x86_64/mingw-check-1/validate-toolstate.sh /scripts/
COPY host-x86_64/mingw-check-1/validate-error-codes.sh /scripts/
# NOTE: intentionally uses python2 for x.py so we can test it still works.
# validate-toolstate only runs in our CI, so it's ok for it to only support python3.
ENV SCRIPT TIDY_PRINT_DIFF=1 npm install eslint@$(head -n 1 /tmp/eslint.version) && \
python2.7 ../x.py test --stage 0 src/tools/tidy tidyselftest --extra-checks=py,cpp
#
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
# pip-compile --allow-unsafe --generate-hashes reuse-requirements.in
---
#15 2.998 Building wheels for collected packages: reuse
#15 2.999 Building wheel for reuse (pyproject.toml): started
#15 3.209 Building wheel for reuse (pyproject.toml): finished with status 'done'
#15 3.210 Created wheel for reuse: filename=reuse-4.0.3-cp310-cp310-manylinux_2_35_x86_64.whl size=132719 sha256=d2a2565e7037ad3883fb9337653f2e25bbb588534fbef3697286cbc26d1bf634
#15 3.211 Stored in directory: /tmp/pip-ephem-wheel-cache-j0zv5owi/wheels/3d/8d/0a/e0fc6aba4494b28a967ab5eaf951c121d9c677958714e34532
#15 3.213 Successfully built reuse
#15 3.213 Installing collected packages: boolean-py, binaryornot, tomlkit, reuse, python-debian, markupsafe, license-expression, jinja2, chardet, attrs
#15 3.607 Successfully installed attrs-23.2.0 binaryornot-0.4.4 boolean-py-4.0 chardet-5.2.0 jinja2-3.1.4 license-expression-30.3.0 markupsafe-2.1.5 python-debian-0.1.49 reuse-4.0.3 tomlkit-0.13.0
#15 3.608 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
#15 4.150 Collecting virtualenv
#15 4.217 Downloading virtualenv-20.31.2-py3-none-any.whl (6.1 MB)
#15 4.527 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.1/6.1 MB 19.7 MB/s eta 0:00:00
#15 4.592 Collecting filelock<4,>=3.12.2
#15 4.602 Downloading filelock-3.18.0-py3-none-any.whl (16 kB)
#15 4.625 Collecting distlib<1,>=0.3.7
#15 4.635 Downloading distlib-0.3.9-py2.py3-none-any.whl (468 kB)
#15 4.648 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 469.0/469.0 KB 40.7 MB/s eta 0:00:00
#15 4.688 Collecting platformdirs<5,>=3.9.1
#15 4.698 Downloading platformdirs-4.3.8-py3-none-any.whl (18 kB)
#15 4.778 Installing collected packages: distlib, platformdirs, filelock, virtualenv
#15 4.967 Successfully installed distlib-0.3.9 filelock-3.18.0 platformdirs-4.3.8 virtualenv-20.31.2
#15 4.968 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
#15 DONE 5.0s
#16 [10/11] COPY host-x86_64/mingw-check-1/validate-toolstate.sh /scripts/
#16 DONE 0.0s
---
DirectMap4k: 141248 kB
DirectMap2M: 9295872 kB
DirectMap1G: 9437184 kB
##[endgroup]
Executing TIDY_PRINT_DIFF=1 npm install eslint@$(head -n 1 /tmp/eslint.version) && python2.7 ../x.py test --stage 0 src/tools/tidy tidyselftest --extra-checks=py,cpp
+ head -n 1 /tmp/eslint.version
+ TIDY_PRINT_DIFF=1 npm install [email protected]
npm WARN deprecated [email protected]: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
npm WARN deprecated [email protected]: Rimraf versions prior to v4 are no longer supported
npm WARN deprecated @humanwhocodes/[email protected]: Use @eslint/config-array instead
npm WARN deprecated [email protected]: Glob versions prior to v9 are no longer supported
npm WARN deprecated @humanwhocodes/[email protected]: Use @eslint/object-schema instead
npm WARN deprecated [email protected]: This version is no longer supported. Please see https://eslint.org/version-support for other options.
added 89 packages in 3s
17 packages are looking for funding
run `npm fund` for details
+ python2.7 ../x.py test --stage 0 src/tools/tidy tidyselftest --extra-checks=py,cpp
##[group]Building bootstrap
Finished `dev` profile [unoptimized] target(s) in 0.05s
##[endgroup]
WARN: currently no CI rustc builds have rustc debug assertions enabled. Please either set `rust.debug-assertions` to `false` if you want to use download CI rustc or set `rust.download-rustc` to `false`.
WARN: `rust.debug-assertions = true` will prevent downloading CI rustc as alt CI rustc is not currently built with debug assertions.
---
fmt: checked 6050 files
tidy check
Running eslint on rustdoc JS files
tidy: Skipping binary file check, read-only filesystem
##[error]tidy error: /checkout/library/core/src/fmt/num.rs:265: undocumented unsafe
##[error]tidy error: /checkout/library/core/src/fmt/num.rs:373: undocumented unsafe
##[error]tidy error: /checkout/library/core/src/fmt/num.rs:411: undocumented unsafe
##[error]tidy error: /checkout/library/core/src/fmt/num.rs:447: undocumented unsafe
##[error]tidy error: /checkout/library/core/src/fmt/num.rs:798: undocumented unsafe
##[error]tidy error: /checkout/library/core/src/fmt/num.rs:832: undocumented unsafe
##[error]tidy error: /checkout/library/core/src/fmt/num.rs:880: undocumented unsafe
removing old virtual environment
creating virtual environment at '/checkout/obj/build/venv' using 'python3.10' and 'venv'
creating virtual environment at '/checkout/obj/build/venv' using 'python3.10' and 'virtualenv'
Requirement already satisfied: pip in ./build/venv/lib/python3.10/site-packages (25.1.1)
linting python files
All checks passed!
checking python file formatting
28 files already formatted
checking C++ file formatting
some tidy checks failed
Command has failed. Rerun with -v to see more details.
Build completed unsuccessfully in 0:01:13
local time: Fri Jun 6 09:55:40 UTC 2025
network time: Fri, 06 Jun 2025 09:55:40 GMT
##[error]Process completed with exit code 1.
Post job cleanup.
Applied suggestions and opened another PR for the number of digits of u128 computation to not add even more changes to this PR.
:umbrella: The latest upstream changes (presumably #142133) made this pull request unmergeable. Please resolve the merge conflicts.
Fixed merge conflict.
Maybe you want to take a look at this one too @tgross35 ? :)
I improved docs for the new API and rebased too.
:umbrella: The latest upstream changes (presumably #142294) made this pull request unmergeable. Please resolve the merge conflicts.
Fixed merge conflict.
:umbrella: The latest upstream changes (presumably #142795) made this pull request unmergeable. Please resolve the merge conflicts.
Thanks for the review @Amanieu! Applied all suggestions.
Also, gonna send a separate PR to use get_unchecked in this file because we could make things much better.
The job mingw-check-tidy failed! Check out the build log: (web) (plain)
Click to see the possible cause of the failure (guessed by this bot)
Running eslint on rustdoc JS files
`rustdoc-json-types` was not modified.
No error code explanation was removed!
tidy: Skipping binary file check, read-only filesystem
##[error]tidy error: /checkout/library/core/src/fmt/num.rs:352: undocumented unsafe
##[error]tidy error: /checkout/library/core/src/fmt/num.rs:397: undocumented unsafe
##[error]tidy error: /checkout/library/core/src/fmt/num.rs:850: undocumented unsafe
removing old virtual environment
creating virtual environment at '/checkout/obj/build/venv' using 'python3.10' and 'venv'
creating virtual environment at '/checkout/obj/build/venv' using 'python3.10' and 'virtualenv'
Requirement already satisfied: pip in ./build/venv/lib/python3.10/site-packages (25.1.1)
linting python files
All checks passed!
checking python file formatting
28 files already formatted
checking C++ file formatting
some tidy checks failed
Command has failed. Rerun with -v to see more details.
Build completed unsuccessfully in 0:01:18
local time: Mon Jun 30 15:46:47 UTC 2025
network time: Mon, 30 Jun 2025 15:46:47 GMT
##[error]Process completed with exit code 1.
Post job cleanup.
Safety annotation needs to be right on the unsafe block, not on the start of the expression. Well, noted.
The job mingw-check-2 failed! Check out the build log: (web) (plain)
Click to see the possible cause of the failure (guessed by this bot)
[RUSTC-TIMING] build_script_build test:false 0.496
error[E0080]: evaluation panicked: argument of integer logarithm must be positive
--> library/core/src/fmt/num_buffer.rs:15:41
|
15 | const BUF_SIZE: usize = $signed::MIN.ilog(10) as usize + 1;
| ^^^^^^^^^^^^^^^^^^^^^ evaluation of `<isize as fmt::num_buffer::NumBufferTrait>::BUF_SIZE` failed here
...
25 | / impl_NumBufferTrait! {
26 | | i8, u8,
27 | | i16, u16,
28 | | i32, u32,
---
error[E0080]: evaluation panicked: argument of integer logarithm must be positive
--> library/core/src/fmt/num_buffer.rs:15:41
|
15 | const BUF_SIZE: usize = $signed::MIN.ilog(10) as usize + 1;
| ^^^^^^^^^^^^^^^^^^^^^ evaluation of `<i8 as fmt::num_buffer::NumBufferTrait>::BUF_SIZE` failed here
...
25 | / impl_NumBufferTrait! {
26 | | i8, u8,
27 | | i16, u16,
28 | | i32, u32,
---
error[E0080]: evaluation panicked: argument of integer logarithm must be positive
--> library/core/src/fmt/num_buffer.rs:15:41
|
15 | const BUF_SIZE: usize = $signed::MIN.ilog(10) as usize + 1;
| ^^^^^^^^^^^^^^^^^^^^^ evaluation of `<i64 as fmt::num_buffer::NumBufferTrait>::BUF_SIZE` failed here
...
25 | / impl_NumBufferTrait! {
26 | | i8, u8,
27 | | i16, u16,
28 | | i32, u32,
---
error[E0080]: evaluation panicked: argument of integer logarithm must be positive
--> library/core/src/fmt/num_buffer.rs:15:41
|
15 | const BUF_SIZE: usize = $signed::MIN.ilog(10) as usize + 1;
| ^^^^^^^^^^^^^^^^^^^^^ evaluation of `<i32 as fmt::num_buffer::NumBufferTrait>::BUF_SIZE` failed here
...
25 | / impl_NumBufferTrait! {
26 | | i8, u8,
27 | | i16, u16,
28 | | i32, u32,
---
error[E0080]: evaluation panicked: argument of integer logarithm must be positive
--> library/core/src/fmt/num_buffer.rs:15:41
|
15 | const BUF_SIZE: usize = $signed::MIN.ilog(10) as usize + 1;
| ^^^^^^^^^^^^^^^^^^^^^ evaluation of `<i16 as fmt::num_buffer::NumBufferTrait>::BUF_SIZE` failed here
...
25 | / impl_NumBufferTrait! {
26 | | i8, u8,
27 | | i16, u16,
28 | | i32, u32,
---
error[E0080]: evaluation panicked: argument of integer logarithm must be positive
--> library/core/src/fmt/num_buffer.rs:15:41
|
15 | const BUF_SIZE: usize = $signed::MIN.ilog(10) as usize + 1;
| ^^^^^^^^^^^^^^^^^^^^^ evaluation of `<i128 as fmt::num_buffer::NumBufferTrait>::BUF_SIZE` failed here
...
25 | / impl_NumBufferTrait! {
26 | | i8, u8,
27 | | i16, u16,
28 | | i32, u32,
---
For more information about this error, try `rustc --explain E0080`.
error: could not document `core`
warning: build failed, waiting for other jobs to finish...
[RUSTC-TIMING] core test:false 32.888
Command has failed. Rerun with -v to see more details.
Build completed unsuccessfully in 0:00:36
local time: Mon Jun 30 18:55:15 UTC 2025
network time: Mon, 30 Jun 2025 18:55:16 GMT
##[error]Process completed with exit code 1.
Post job cleanup.
@Amanieu I went to the end of the usage of get_unchecked everywhere in https://github.com/rust-lang/rust/pull/143277 and basically, there is absolutely no difference, the compiler is already optimizing all the code correctly.
Note that there were some recent PRs removing unsafe code from int Display impls that didn’t make a difference (https://github.com/rust-lang/rust/pull/135265, https://github.com/rust-lang/rust/pull/136594).
Yeah I will just revert the get_unchecked calls I added here as well. No point in keeping them.
And done.
Applied suggestions.
@bors r+
:pushpin: Commit 9943c1933d2a9a343b088b6fd7ac76417da94871 has been approved by Amanieu
It is now in the queue for this repository.