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

Confusing diagnostic when using format strings in const fn #93665

Closed
nsunderland1 opened this issue Feb 4, 2022 · 1 comment · Fixed by #98276
Closed

Confusing diagnostic when using format strings in const fn #93665

nsunderland1 opened this issue Feb 4, 2022 · 1 comment · Fixed by #98276
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nsunderland1
Copy link
Contributor

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.

@nsunderland1 nsunderland1 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 4, 2022
@estebank estebank added A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-const-fn D-confusing Diagnostics: Confusing error or lint that should be reworked. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. labels Feb 5, 2022
@oli-obk
Copy link
Contributor

oli-obk commented Feb 7, 2022

Note that on nightly we only get

error[[E0015]](https://doc.rust-lang.org/nightly/error-index.html#E0015): calls in constant functions are limited to constant functions, tuple structs and tuple variants
 [--> src/main.rs:2:20
](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021#)  |
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)

without any additional info (but at least the function pointer diagnostic is gone)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants