Skip to content

Don't allow flattened format_args in const. #139624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

m-ou-se
Copy link
Member

@m-ou-se m-ou-se commented Apr 10, 2025

Fixes #139136

Fixes #139621

We allow format_args!("a") in const, but don't allow any format_args with arguments in const, such as format_args!("{}", arg).

However, we accidentally allow format_args!("hello {}", "world") in const, as it gets flattened to format_args!("hello world").

This also applies to panic in const.

This wasn't supposed to happen. I added protection against this in the format args flattening code, but I accidentally marked a function as const that shouldn't have been const but this was removed in #135139.

This is a breaking change. The crater found no breakage, however.

This breaks things like:

const _: () = if false { panic!("a {}", "a") };

and

const F: std::fmt::Arguments<'static> = format_args!("a {}", "a");

@rustbot

This comment was marked as outdated.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Apr 10, 2025
@m-ou-se m-ou-se assigned m-ou-se and unassigned Mark-Simulacrum Apr 10, 2025
@m-ou-se
Copy link
Member Author

m-ou-se commented Apr 10, 2025

@bors try

bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 10, 2025
Don't allow flattened format_args in const.

Fixes rust-lang#139136 and rust-lang#139621 by breaking the 'flattened format_args' cases.

This is a breaking change.

Let's try a crater run.
@bors
Copy link
Collaborator

bors commented Apr 10, 2025

⌛ Trying commit cc791eb with merge 6619027...

@m-ou-se m-ou-se added S-experimental Status: Ongoing experiment that does not require reviewing and won't be merged in its current state. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 10, 2025
@m-ou-se m-ou-se mentioned this pull request Apr 7, 2025
29 tasks
@bors
Copy link
Collaborator

bors commented Apr 10, 2025

☀️ Try build successful - checks-actions
Build commit: 6619027 (6619027b13a87a3597115f2c8231f107bf3a7a5f)

@m-ou-se
Copy link
Member Author

m-ou-se commented Apr 10, 2025

@craterbot check

@craterbot
Copy link
Collaborator

👌 Experiment pr-139624 created and queued.
🤖 Automatically detected try build 6619027
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-experimental Status: Ongoing experiment that does not require reviewing and won't be merged in its current state. labels Apr 10, 2025
@craterbot
Copy link
Collaborator

🚧 Experiment pr-139624 is now running

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot
Copy link
Collaborator

🎉 Experiment pr-139624 is completed!
📊 3 regressed and 5 fixed (612545 total)
📰 Open the full report.

⚠️ If you notice any spurious failure please add them to the denylist!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Apr 11, 2025
@RalfJung
Copy link
Member

These all seem spurious.

Landing this requires FCP, but I'm not sure which team -- lang and/or libs-api?

@m-ou-se m-ou-se added T-lang Relevant to the language team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Apr 11, 2025
@m-ou-se m-ou-se marked this pull request as ready for review April 11, 2025 12:05
@m-ou-se
Copy link
Member Author

m-ou-se commented Apr 11, 2025

@rfcbot merge

@rfcbot
Copy link
Collaborator

rfcbot commented Apr 11, 2025

Team member @m-ou-se has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Apr 11, 2025
@m-ou-se m-ou-se added needs-fcp This change is insta-stable, so needs a completed FCP to proceed. S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 11, 2025
@RalfJung RalfJung added I-lang-nominated Nominated for discussion during a lang team meeting. I-lang-easy-decision Issue: The decision needed by the team is conjectured to be easy; this does not imply nomination labels Apr 11, 2025
@m-ou-se m-ou-se added the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Apr 11, 2025
@traviscross
Copy link
Contributor

@rfcbot reviewed

If we didn't mean to do it and there's no breakage, we should indeed claw it back.

I'm curious, though, to know whether this is something we might later want to allow and what would need to happen for us to be happy to do that.

@RalfJung
Copy link
Member

I'm curious, though, to know whether this is something we might later want to allow and what would need to happen for us to be happy to do that.

As a first step, we should have a very clear idea of which cases we want to allow, and not have this be just "whatever that compiler pass can deal with". :)

@m-ou-se
Copy link
Member Author

m-ou-se commented Apr 11, 2025

I'm curious, though, to know whether this is something we might later want to allow and what would need to happen for us to be happy to do that.

Making some guesses: My expectation is that in the future we will be able to allow all format_args!() invocations in const (since it doesn't do any formatting yet, just sets up the structure). For panic!(), I think the best plan is to accept all panic!() invitations in const, and do the formatting of the message (for the diagnostic) on a best-effort basis (e.g. only format builtin types).

@Amanieu Amanieu removed the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Apr 15, 2025
@joshtriplett
Copy link
Member

I would love to see all format_args! invocations supported in const, but until then, 👍 for reverting this and not accidentally supporting whatever the flattening pass happens to handle.

@nikomatsakis
Copy link
Contributor

@rfcbot reviewed

@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Apr 16, 2025
@rfcbot
Copy link
Collaborator

rfcbot commented Apr 16, 2025

🔔 This is now entering its final comment period, as per the review above. 🔔

@tmandry
Copy link
Member

tmandry commented Apr 16, 2025

@rfcbot reviewed

@traviscross traviscross removed the I-lang-nominated Nominated for discussion during a lang team meeting. label Apr 16, 2025
@m-ou-se
Copy link
Member Author

m-ou-se commented Apr 22, 2025

but I accidentally marked a function as const that shouldn't have been const

This wasn't actually the case. I did implement this correctly. The problem was caused by #135139, which added const to a bunch of functions without properly considering the effect on the format_args!() macro.

This means that this problem has only been around since 1.86.0, which explains why we didn't find any actual breakage.

It might make sense to backport this change to land it in 1.87.0.

@m-ou-se m-ou-se added the beta-nominated Nominated for backporting to the compiler in the beta channel. label Apr 22, 2025
/// format_args!() and panic!() with arguments in const, even when not evaluated:
///
/// ```compile_fail,E0015
/// const _: () = if false { panic!("a {}", "a") };
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a test to tests/ui/consts/const-eval/format.rs as well? That seems to be where we have similar tests like this -- using a doctest is somewhat unconventional.

@m-ou-se m-ou-se added the beta-accepted Accepted for backporting to the compiler in the beta channel. label Apr 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
beta-accepted Accepted for backporting to the compiler in the beta channel. beta-nominated Nominated for backporting to the compiler in the beta channel. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. I-lang-easy-decision Issue: The decision needed by the team is conjectured to be easy; this does not imply nomination needs-fcp This change is insta-stable, so needs a completed FCP to proceed. S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. T-lang Relevant to the language team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet