Skip to content

Confusing diagnostic when using format strings in const fn #93665

Closed
@nsunderland1

Description

@nsunderland1

Reproduced in the Playground on 1.58.1 stable.

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=fe56b4c08a046bd087207e0649379a33

const fn failure() {
    panic!("{:?}", 0);
}

fn main() {}

The current output is:

error[[E0658]](https://doc.rust-lang.org/stable/error-index.html#E0658): function pointer casts are not allowed in constant functions
 [--> src/main.rs:2:20
](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=de5939e5ea116da7ce863563832ea7b9#)  |
2 |     panic!("{:?}", 0);
  |                    ^
  |
  = note: [see issue #57563 <https://github.com/rust-lang/rust/issues/57563>](https://github.com/rust-lang/rust/issues/57563) for more information
  = note: this error originates in the macro `$crate::const_format_args` (in Nightly builds, run with -Z macro-backtrace for more info)

error[[E0015]](https://doc.rust-lang.org/stable/error-index.html#E0015): calls in constant functions are limited to constant functions, tuple structs and tuple variants
 [--> src/main.rs:2:5
](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=de5939e5ea116da7ce863563832ea7b9#)  |
2 |     panic!("{:?}", 0);
  |     ^^^^^^^^^^^^^^^^^
  |
  = note: this error originates in the macro `$crate::const_format_args` (in Nightly builds, run with -Z macro-backtrace for more info)

panic with formatting isn't fully supported in const contexts, but the diagnostic is surprising, since it's not obvious that there are any function pointer casts happening. This should give a more direct message about what I've done wrong.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions