Skip to content
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

Error using const_format_args in const context #108595

Closed
zhassan-aws opened this issue Mar 1, 2023 · 3 comments
Closed

Error using const_format_args in const context #108595

zhassan-aws opened this issue Mar 1, 2023 · 3 comments
Labels
C-bug Category: This is a bug. requires-nightly This issue requires a nightly compiler in some way.

Comments

@zhassan-aws
Copy link

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
@zhassan-aws zhassan-aws added the C-bug Category: This is a bug. label Mar 1, 2023
@Noratrieb Noratrieb added the requires-nightly This issue requires a nightly compiler in some way. label Mar 1, 2023
@zhassan-aws
Copy link
Author

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`.

@m-ou-se
Copy link
Member

m-ou-se commented Mar 6, 2023

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").)

@m-ou-se
Copy link
Member

m-ou-se commented Mar 6, 2023

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.)

@m-ou-se m-ou-se closed this as completed Mar 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. requires-nightly This issue requires a nightly compiler in some way.
Projects
None yet
Development

No branches or pull requests

3 participants