Skip to content

Add _value API for number literals in proc-macro#154608

Open
GuillaumeGomez wants to merge 3 commits into
rust-lang:mainfrom
GuillaumeGomez:number_value_proc-macro_API
Open

Add _value API for number literals in proc-macro#154608
GuillaumeGomez wants to merge 3 commits into
rust-lang:mainfrom
GuillaumeGomez:number_value_proc-macro_API

Conversation

@GuillaumeGomez
Copy link
Copy Markdown
Member

@GuillaumeGomez GuillaumeGomez commented Mar 30, 2026

View all comments

Part of #136652.

This PR adds the *_value for numbers (integers and floats). However, f16 and f128 are voluntarily left out as they're still unstable. Adding support for them is just a matter of uncommenting two lines, so should be fine.

Setting same reviewer as last time.

r? @Urgau

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 30, 2026
@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez GuillaumeGomez force-pushed the number_value_proc-macro_API branch from ae03536 to 7cb03fd Compare March 31, 2026 09:53
@GuillaumeGomez
Copy link
Copy Markdown
Member Author

My favourite kind of failure: works locally but not in CI. T_T

Updated the value in the tests to see which one exactly is failing.

@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez GuillaumeGomez force-pushed the number_value_proc-macro_API branch from 7cb03fd to c755b56 Compare March 31, 2026 13:14
@GuillaumeGomez
Copy link
Copy Markdown
Member Author

Bug comes from an issue with GCC proc-macro ABI. Ignoring this test on GCC backend for now.

@GuillaumeGomez
Copy link
Copy Markdown
Member Author

CI fixed. :)

@Urgau
Copy link
Copy Markdown
Member

Urgau commented Mar 31, 2026

A libs (or maybe even libs-api) reviewer seems more appropriate to review this PR.

r? Amanieu (since you reviewed #136355)

@rustbot rustbot assigned Amanieu and unassigned Urgau Mar 31, 2026
@Urgau
Copy link
Copy Markdown
Member

Urgau commented Mar 31, 2026

I missed it already has an accepted ACP. The PR can be reviewed by libs reviewer.

r? libs

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Mar 31, 2026
@rustbot rustbot assigned Mark-Simulacrum and unassigned Amanieu Mar 31, 2026
Comment thread library/proc_macro/src/lib.rs Outdated
Comment thread library/proc_macro/src/lib.rs Outdated
Hexadecimal = 16,
}

fn parse_number(value: &str) -> (String, Base) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this exactly in sync with what the rustc parser permits?

I guess that would be a mix of https://doc.rust-lang.org/nightly/reference/tokens.html#grammar-INTEGER_LITERAL and whatever we then tokenize from it?

Perhaps this should go over the bridge and ask rustc to parse the string for us?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no code in rustc currently which would allow this conversion without including a lot of other things. My code is a simplified version of integer_lit (from rustc_ast/src/util/literal.rs) which parses both the number, its base and its suffix (which we already have) and returns a LitKind, which is also unnecessary here since we already know the end type.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think my question is not about specific code in rustc but rather whether the implementation here parses exactly the same literals that rustc would from the given token(s). Is that the case? Essentially, can we document these functions as being bug-for-bug compatible with what rustc would do, or do we need a separate grammar for this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point, the compiler already ran on the literal so all errors have been checked already. The remaining potential errors mostly come from trying to convert a int/float not fitting the expected output.

Comment thread library/proc_macro/src/lib.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 4, 2026
@GuillaumeGomez GuillaumeGomez force-pushed the number_value_proc-macro_API branch from c755b56 to ebb3161 Compare April 10, 2026 14:36
@rustbot

This comment has been minimized.

@GuillaumeGomez
Copy link
Copy Markdown
Member Author

Added the method for f16, renamed the function only_digits into strip_underscores and improve the code of parse_number a bit to remove allocations.

@GuillaumeGomez
Copy link
Copy Markdown
Member Author

Forgot to change the status.

@rustbot ready

cc @Mark-Simulacrum

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 12, 2026
@Mark-Simulacrum
Copy link
Copy Markdown
Member

Can you reply to my comment here? #154608 (comment)

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 31, 2026
@GuillaumeGomez
Copy link
Copy Markdown
Member Author

Forgot to do so. Answered it.

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 2, 2026
@Mark-Simulacrum
Copy link
Copy Markdown
Member

I added an unresolved question to follow up on the equivalency, for now this can go in since it's unstable. I think we want a stronger guarantee than I think you're saying this has right now (as noted in the question on the tracking issue).

@bors r+

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Jun 6, 2026

📌 Commit ebb3161 has been approved by Mark-Simulacrum

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 6, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jun 6, 2026
…acro_API, r=Mark-Simulacrum

Add `_value` API for number literals in proc-macro

Part of rust-lang#136652.

This PR adds the `*_value` for numbers (integers and floats). However, `f16` and `f128` are voluntarily left out as they're still unstable. Adding support for them is just a matter of uncommenting two lines, so should be fine.

Setting same reviewer as last time.

r? @Urgau
@JonathanBrouwer
Copy link
Copy Markdown
Contributor

💔 I suspect this PR failed tests as part of a rollup
@bors r-

After fixing the problem, consider running a try job for the failed job before re-approving.

Link to failure: #157536 (comment)

@rust-bors rust-bors Bot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jun 6, 2026
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Jun 6, 2026

This pull request was unapproved.

This PR was contained in a rollup (#157536), which was unapproved.

View changes since this unapproval

@rustbot

This comment has been minimized.

@GuillaumeGomez GuillaumeGomez force-pushed the number_value_proc-macro_API branch from 7278ace to e673d20 Compare June 6, 2026 22:38
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jun 6, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@GuillaumeGomez
Copy link
Copy Markdown
Member Author

Ah I see, #![feature(f16)] is not enough. I added an extra cfg check on the assert calls to ensure it's only run when it can actually work.

Also rebased on main because of the bot's message.

@bors try x86_64-mingw-1

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Jun 6, 2026

Unknown argument "x86_64-mingw-1". Did you mean to use @bors jobs=<jobs>|parent=<parent>? Run @bors help to see available commands.

@GuillaumeGomez
Copy link
Copy Markdown
Member Author

@bors try jobs=x86_64-mingw-1

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Jun 6, 2026
…=<try>

Add `_value` API for number literals in proc-macro


try-job: x86_64-mingw-1
@rust-log-analyzer
Copy link
Copy Markdown
Collaborator

The job aarch64-gnu-llvm-21-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
---- [ui] tests/ui/proc-macro/test.rs stdout ----

error: auxiliary build of /checkout/tests/ui/proc-macro/auxiliary/api/proc_macro_api_tests.rs failed to compile: 
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/proc-macro/auxiliary/api/proc_macro_api_tests.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/proc-macro/test/auxiliary" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers" "--edition=2021" "--crate-type" "proc-macro" "--extern" "proc_macro" "-L" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/proc-macro/test/auxiliary"
stdout: none
--- stderr -------------------------------
error[E0658]: `cfg(target_has_reliable_f16)` is experimental and subject to change
##[error]  --> /checkout/tests/ui/proc-macro/auxiliary/api/literal.rs:105:13
   |
LL |     if cfg!(target_has_reliable_f16) {
   |             ^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: add `#![feature(cfg_target_has_reliable_f16_f128)]` to the crate attributes to enable
   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: `cfg(target_has_reliable_f16)` is experimental and subject to change
##[error]  --> /checkout/tests/ui/proc-macro/auxiliary/api/literal.rs:111:13
   |
LL |     if cfg!(target_has_reliable_f16) {
   |             ^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: add `#![feature(cfg_target_has_reliable_f16_f128)]` to the crate attributes to enable
   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

warning: unexpected `cfg` condition name: `target_has_reliable_f16`
##[warning]  --> /checkout/tests/ui/proc-macro/auxiliary/api/literal.rs:105:13
   |
LL |     if cfg!(target_has_reliable_f16) {

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Jun 7, 2026

💔 Test for 1d1d12a failed: CI. Failed job:

@rust-log-analyzer
Copy link
Copy Markdown
Collaborator

The job x86_64-mingw-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
failures:

---- [ui] tests\ui\proc-macro\test.rs stdout ----

error: auxiliary build of D:\a\rust\rust\tests\ui\proc-macro\auxiliary\api/proc_macro_api_tests.rs failed to compile: 
status: exit code: 1
command: PATH="D:\a\rust\rust\build\x86_64-pc-windows-gnu\stage2\bin;D:\a\rust\rust\build\x86_64-pc-windows-gnu\bootstrap-tools\x86_64-pc-windows-gnu\release\deps;D:\a\rust\rust\ninja;D:\a\rust\rust\mingw64\bin;C:\msys64\usr\bin;D:\a\rust\rust\sccache;C:\Program Files\MongoDB\Server\7.0\bin;C:\vcpkg;C:\tools\zstd;C:\hostedtoolcache\windows\stack\3.9.3\x64;C:\cabal\bin;C:\ghcup\bin;C:\mingw64\bin;C:\Program Files\dotnet;C:\Program Files\MySQL\MySQL Server 8.0\bin;C:\Program Files\R\R-4.6.0\bin\x64;C:\SeleniumWebDrivers\GeckoDriver;C:\SeleniumWebDrivers\EdgeDriver;C:\SeleniumWebDrivers\ChromeDriver;C:\Program Files (x86)\sbt\bin;C:\Program Files (x86)\GitHub CLI;C:\Program Files\Git\bin;C:\Program Files (x86)\pipx_bin;C:\npm\prefix;C:\hostedtoolcache\windows\go\1.24.13\x64\bin;C:\hostedtoolcache\windows\Python\3.12.10\x64\Scripts;C:\hostedtoolcache\windows\Python\3.12.10\x64;C:\hostedtoolcache\windows\Ruby\3.3.11\x64\bin;C:\Program Files\OpenSSL\bin;C:\tools\kotlinc\bin;C:\hostedtoolcache\windows\Java_Temurin-Hotspot_jdk\17.0.19-10\x64\bin;C:\Program Files\ImageMagick-7.1.2-Q16-HDRI;C:\Program Files\Microsoft SDKs\Azure\CLI2\wbin;C:\ProgramData\kind;C:\ProgramData\Chocolatey\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Program Files\PowerShell\7;C:\Program Files\Microsoft\Web Platform Installer;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn;C:\Program Files\Microsoft SQL Server\150\Tools\Binn;C:\Program Files\dotnet;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit;C:\Program Files (x86)\WiX Toolset v3.14\bin;C:\Program Files\Microsoft SQL Server\130\DTS\Binn;C:\Program Files\Microsoft SQL Server\140\DTS\Binn;C:\Program Files\Microsoft SQL Server\150\DTS\Binn;C:\Program Files\Microsoft SQL Server\160\DTS\Binn;C:\Program Files\Microsoft SQL Server\170\DTS\Binn;C:\ProgramData\chocolatey\lib\pulumi\tools\Pulumi\bin;C:\Program Files\CMake\bin;C:\Strawberry\c\bin;C:\Strawberry\perl\site\bin;C:\Strawberry\perl\bin;C:\ProgramData\chocolatey\lib\maven\apache-maven-3.9.16\bin;C:\Program Files\Microsoft Service Fabric\bin\Fabric\Fabric.Code;C:\Program Files\Microsoft SDKs\Service Fabric\Tools\ServiceFabricLocalClusterManager;C:\Program Files\nodejs;C:\Program Files\Git\cmd;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Program Files\GitHub CLI;C:\tools\php;C:\Program Files (x86)\sbt\bin;C:\Program Files\Amazon\AWSCLIV2;C:\Program Files\Amazon\SessionManagerPlugin\bin;C:\Program Files\Amazon\AWSSAMCLI\bin;C:\Program Files\Microsoft SQL Server\130\Tools\Binn;C:\Program Files\mongosh;C:\Program Files\LLVM\bin;C:\Program Files (x86)\LLVM\bin;C:\Users\runneradmin\.dotnet\tools;C:\Users\runneradmin\.cargo\bin;C:\Users\runneradmin\AppData\Local\Microsoft\WindowsApps" "D:\\a\\rust\\rust\\build\\x86_64-pc-windows-gnu\\stage2\\bin\\rustc.exe" "D:\\a\\rust\\rust\\tests\\ui\\proc-macro\\auxiliary\\api/proc_macro_api_tests.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=C:\\Users\\runneradmin\\.cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=D:\\a\\rust\\rust\\vendor" "--sysroot" "D:\\a\\rust\\rust\\build\\x86_64-pc-windows-gnu\\stage2" "--target=x86_64-pc-windows-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "-C" "prefer-dynamic" "--out-dir" "D:\\a\\rust\\rust\\build\\x86_64-pc-windows-gnu\\test\\ui\\proc-macro\\test\\auxiliary" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-gnu\\native\\rust-test-helpers" "--edition=2021" "--crate-type" "proc-macro" "--extern" "proc_macro" "-L" "D:\\a\\rust\\rust\\build\\x86_64-pc-windows-gnu\\test\\ui\\proc-macro\\test\\auxiliary"
stdout: none
--- stderr -------------------------------
error[E0658]: `cfg(target_has_reliable_f16)` is experimental and subject to change
##[error]  --> D:\a\rust\rust\tests\ui\proc-macro\auxiliary\api\literal.rs:105:13
   |
LL |     if cfg!(target_has_reliable_f16) {
   |             ^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: add `#![feature(cfg_target_has_reliable_f16_f128)]` to the crate attributes to enable
   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: `cfg(target_has_reliable_f16)` is experimental and subject to change
##[error]  --> D:\a\rust\rust\tests\ui\proc-macro\auxiliary\api\literal.rs:111:13
   |
LL |     if cfg!(target_has_reliable_f16) {
   |             ^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: add `#![feature(cfg_target_has_reliable_f16_f128)]` to the crate attributes to enable
   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

warning: unexpected `cfg` condition name: `target_has_reliable_f16`
##[warning]  --> D:\a\rust\rust\tests\ui\proc-macro\auxiliary\api\literal.rs:105:13
   |
LL |     if cfg!(target_has_reliable_f16) {
   |             ^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: expected names are: `FALSE` and `test` and 32 more
   = help: to expect this configuration use `--check-cfg=cfg(target_has_reliable_f16)`
   = note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
   = note: `#[warn(unexpected_cfgs)]` on by default

warning: unexpected `cfg` condition name: `target_has_reliable_f16`
##[warning]  --> D:\a\rust\rust\tests\ui\proc-macro\auxiliary\api\literal.rs:111:13
   |
LL |     if cfg!(target_has_reliable_f16) {
   |             ^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: to expect this configuration use `--check-cfg=cfg(target_has_reliable_f16)`
   = note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration

warning: the `#![feature]` attribute can only be used at the crate root
##[warning]  --> D:\a\rust\rust\tests\ui\proc-macro\auxiliary\api\literal.rs:3:1
   |
LL | #![feature(f16)]
   | ^^^^^^^^^^^^^^^^
   |
   = help: the crate root is at `D:\a\rust\rust\tests\ui\proc-macro\auxiliary\api/proc_macro_api_tests.rs`
   = note: `#[warn(unused_attributes)]` (part of `#[warn(unused)]`) on by default

error: aborting due to 2 previous errors; 3 warnings emitted

For more information about this error, try `rustc --explain E0658`.
---

Some tests failed in compiletest suite=ui mode=ui host=x86_64-pc-windows-gnu target=x86_64-pc-windows-gnu
Bootstrap failed while executing `test --stage 2 --skip=compiler --skip=src`
Build completed unsuccessfully in 1:24:59
make: *** [Makefile:126: ci-mingw-x] Error 1
  local time: Sun Jun  7 00:13:39 CUT 2026
  network time: Sun, 07 Jun 2026 00:13:39 GMT
##[error]Process completed with exit code 2.
##[group]Run echo "disk usage:"
echo "disk usage:"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants