rust-lang / rust-lang/rust

format args should suggest using the formatter for const values when it makes sense

Open
#121,849 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-suggestion-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

Code
const fn msg() -> &'static str {
    "this is a panic"
}

const fn panic_please() -> ! {
    let e = msg();
    panic!("{e}")
}

fn main() {
    panic_please()
}
Current output
Errors
Exited with status 101
Standard Error
   Compiling playground v0.0.1 (/playground)
error[E0015]: cannot call non-const formatting macro in constant functions
 --> src/main.rs:7:13
  |
7 |     panic!("{e}")
  |             ^^^
  |
  = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
  = note: this error originates in the macro `$crate::const_format_args` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0015]: cannot call non-const fn `Arguments::<'_>::new_v1` in constant functions
 --> src/main.rs:7:5
  |
7 |     panic!("{e}")
  |     ^^^^^^^^^^^^^
  |
  = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
  = note: this error originates in the macro `$crate::const_format_args` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0015`.
error: could not compile `playground` (bin "playground") due to 2 previous errors
Standard Output
Desired output
Something along the lines of

Errors
Exited with status 101
Standard Error
   Compiling playground v0.0.1 (/playground)
error[E0015]: cannot call non-const formatting macro in constant functions
 --> src/main.rs:7:13
  |
7 |     panic!("{e}")
  |             ^^^
  |
  = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
  = note: consider using `panic!("{}", e)` which calls Arguments::new_const

etc...
Rationale and extra context

When not familiar with Arguments and the formatting internals it can be a bit weird to see the panic which morally has a const input fail to compile, especially since clippy heavily encourages users to use the panic!("{e}") syntactic form, while panic!("{}", e) will work just fine in this case

Other cases
All formatting macros likely "suffer" from this
Rust Version
rustc 1.78.0-nightly (2bf78d12d 2024-02-18)
binary: rustc
commit-hash: 2bf78d12d33ae02d10010309a0d85dd04e7cff72
commit-date: 2024-02-18
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 18.1.0
Anything else?

Maybe update the formatting macro to automatically call the new_const function if it's able to do so instead of changing the diagnostics, and if it can't then change the diagnostics message (I don't know the exact formatting for those messages but I think you get the idea)

Cheers

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the reported E0015 errors with the provided Rust code and compare the current and desired diagnostics. Investigate the compiler's formatting-macro and constant-formatting paths, then determine whether the completed change should automatically use the const formatter or provide a targeted suggestion for const values. Verify the behavior across the other formatting macros mentioned in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.