Skip to content

Nightly rejects panic!(..) invocation which is accepted by stable with "cannot call non-const formatting macro in constants" #140585

New issue

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

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

Already on GitHub? # to your account

Closed
horazont opened this issue May 2, 2025 · 4 comments
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@horazont
Copy link

horazont commented May 2, 2025

Code

I tried this code:

const _: () = {
    if false {
        panic!("hello world: {}", "foo");  // works only in stable
    }
};

Playground

I expected to see this happen: Compiles without error.

Instead, this happened:

   Compiling playground v0.0.1 (/playground)
error[E0015]: cannot call non-const formatting macro in constants
 --> src/lib.rs:3:9
  |
3 |         panic!("hello world: {}", "foo");  // works only in stable
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: calls in constants are limited to constant functions, tuple structs and tuple variants

Version it worked on

It most recently worked on: 1.87 (beta).

Version with regression

rustc --version --verbose:

~/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustc --version --verbose
rustc 1.88.0-nightly (3350c1eb3 2025-05-01)
binary: rustc
commit-hash: 3350c1eb3fd8fe1bee1ed4c76944d707bd256876
commit-date: 2025-05-01
host: x86_64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.2
```

@horazont horazont added C-bug Category: This is a bug. regression-untriaged Untriaged performance or correctness regression. labels May 2, 2025
@rustbot rustbot added I-prioritize Issue: Indicates that prioritization has been requested for this issue. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 2, 2025
@theemathas
Copy link
Contributor

theemathas commented May 2, 2025

This is intentional. See #139624.

Do you have a use case for this?

cc @m-ou-se

@m-ou-se
Copy link
Member

m-ou-se commented May 2, 2025

@horazont Did you come across this code in the wild?

This code is accidentally accepted in Rust 1.86, and will be rejected again starting from 1.87.0.

@jieyouxu jieyouxu added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 2, 2025
@horazont
Copy link
Author

horazont commented May 2, 2025

@m-ou-se Yeah, I wrote code against 1.86 and was super happy that it worked :D.

Here is the commit which switches to a behaviour supported on both 1.86 and nigthly: https://gitlab.com/xmpp-rs/xmpp-rs/-/commit/2a940fb5e0f9444d5940295e2643d60a5ada7b79 (nevermind the "Note that we cannot replace it with concat!(..), because we may have { and } in the strings..." comment, that was before I realized I could just use replace and I forgot to remove the comment).

So we used this to avoid having to replace "{" and "}" in strings generated from a proc macro for an error message.

So it's not the end of the world if the breakage was intentional, we have a workable workaround (as seen above).

@apiraino apiraino removed the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label May 3, 2025
@jieyouxu
Copy link
Member

jieyouxu commented May 3, 2025

Triage: AFAICT this is intentional, and #139624 was FCP'd and beta-backported. The relnotes is #140338.

@jieyouxu jieyouxu closed this as completed May 3, 2025
@jieyouxu jieyouxu added C-discussion Category: Discussion or questions that doesn't represent real issues. and removed regression-untriaged Untriaged performance or correctness regression. C-bug Category: This is a bug. labels May 3, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants