rust-lang/rust

Error using `const_format_args` in const context

zhassan-aws opened this issue · 3 comments

I tried this code:

#![feature(const_format_args)]

const fn my_const_fn(msg: &'static str) {
    let _  = const_format_args!("{}", msg);
    //panic!("{}", msg);
}

fn main() {
    my_const_fn("msg");
}

I expected to see this happen: The documentation for const_format_args points out that it's used for the const_panic feature. So I was expecting the above program to compile.

Instead, this happened:

$ rustc test.rs 
error[E0015]: cannot call non-const formatting macro in constant functions
 --> test.rs:4:39
  |
4 |     let _  = const_format_args!("{}", msg);
  |                                       ^^^
  |
  = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
  = note: this error originates in the macro `const_format_args` (in Nightly builds, run with -Z macro-backtrace for more info)

note: erroneous constant used
 --> test.rs:4:33
  |
4 |     let _  = const_format_args!("{}", msg);
  |                                 ^^^^

error: aborting due to previous error

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

The program compiles if I replace the const_format_args line with the commented panic line. Is this expected?

Meta

rustc --version --verbose:

$ rustc --version --verbose
rustc 1.69.0-nightly (31f858d9a 2023-02-28)
binary: rustc
commit-hash: 31f858d9a511f24fedb8ed997b28304fec809630
commit-date: 2023-02-28
host: x86_64-unknown-linux-gnu
release: 1.69.0-nightly
LLVM version: 15.0.7

I also get the same error with format_args, and without using a 'static str:

#![feature(const_fmt_arguments_new)]

const fn my_const_fn() {
    let _  = format_args!("{}", "literal");
    //panic!("{}", "literal");
}

fn main() {
    my_const_fn();
}

Output:

$ rustc test2.rs 
error[E0015]: cannot call non-const formatting macro in constant functions
 --> test2.rs:4:33
  |
4 |     let _  = format_args!("{}", "literal");
  |                                 ^^^^^^^^^
  |
  = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
  = note: this error originates in the macro `format_args` (in Nightly builds, run with -Z macro-backtrace for more info)

note: erroneous constant used
 --> test2.rs:4:27
  |
4 |     let _  = format_args!("{}", "literal");
  |                           ^^^^

note: erroneous constant used
 --> test2.rs:4:33
  |
4 |     let _  = format_args!("{}", "literal");
  |                                 ^^^^^^^^^
  |
  = note: this note originates in the macro `format_args` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

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

The reason panic!("{}", something) works in const, is because of a special case for panic!("{}", arg) in the panic macro: https://doc.rust-lang.org/1.67.0/src/core/panic.rs.html#53

const_format_args!() currently only works for fully static format strings without any placeholders/arguments. (E.g. const_format_args!("hello").)

I'm closing this, because the const_format_args feature is only an internal implementation detail of const panic, and not meant to be stabilized or usable for other purposes.

(Unstable features without tracking issue (#[unstable(…, issue = "none")]) are only for internal use, not on a path towards stabilization.)