Skip to content

non_fmt_panics suggestion breaks for some const functions #88429

Closed
@ehuss

Description

@ehuss

I tried this code:

#![feature(const_panic)]
#![warn(non_fmt_panics)]
pub const fn foo(explanation: &'static str) {
    panic!(explanation);
}

This provides a suggestion to replace the code with:

    panic!("{}", explanation);

However, this will not compile in a const function:

error[E0658]: function pointer casts are not allowed in constant functions
 --> src/lib.rs:4:18
  |
4 |     panic!("{}", explanation);
  |                  ^^^^^^^^^^^
  |
  = note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
  = help: add `#![feature(const_fn_fn_ptr_basics)]` to the crate attributes to enable
  = note: this error originates in the macro `$crate::const_format_args` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
 --> src/lib.rs:4:5
  |
4 |     panic!("{}", explanation);
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: this error originates in the macro `$crate::const_format_args` (in Nightly builds, run with -Z macro-backtrace for more info)

I'm not actually sure what the correct suggestion should be, since panic_any is not const.

Found during the 2021 crater run for:

Meta

rustc --version --verbose:

rustc 1.56.0-nightly (b03ccace5 2021-08-24)
binary: rustc
commit-hash: b03ccace573bb91e27625c190a0f7807045a1012
commit-date: 2021-08-24
host: x86_64-apple-darwin
release: 1.56.0-nightly
LLVM version: 13.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-edition-2021Area: The 2021 editionA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions