-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
base: master
Are you sure you want to change the base?
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
@bors try |
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.
☀️ Try build successful - checks-actions |
@craterbot check |
👌 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
🚧 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
🎉 Experiment
|
These all seem spurious. Landing this requires FCP, but I'm not sure which team -- lang and/or libs-api? |
@rfcbot merge |
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. |
@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. |
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". :) |
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). |
I would love to see all |
@rfcbot reviewed |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
@rfcbot reviewed |
This wasn't actually the case. I did implement this correctly. The problem was caused by #135139, which added 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. |
/// format_args!() and panic!() with arguments in const, even when not evaluated: | ||
/// | ||
/// ```compile_fail,E0015 | ||
/// const _: () = if false { panic!("a {}", "a") }; |
There was a problem hiding this comment.
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.
Fixes #139136
Fixes #139621
We allow
format_args!("a")
in const, but don't allow any format_args with arguments in const, such asformat_args!("{}", arg)
.However, we accidentally allow
format_args!("hello {}", "world")
in const, as it gets flattened toformat_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 constbut this was removed in #135139.This is a breaking change. The crater found no breakage, however.
This breaks things like:
and