Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

invalid_nan_comparisons suggests invalid 5f32.is_nan() in const context #114471

Closed
matthiaskrgr opened this issue Aug 4, 2023 · 0 comments · Fixed by #114486
Closed

invalid_nan_comparisons suggests invalid 5f32.is_nan() in const context #114471

matthiaskrgr opened this issue Aug 4, 2023 · 0 comments · Fixed by #114486
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

Code

fn main() {
    const TEST: bool = 5f32 == f32::NAN;
}

Current output

warning: constant `TEST` is never used
 --> src/main.rs:2:11
  |
2 |     const TEST: bool = 5f32 == f32::NAN;
  |           ^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: incorrect NaN comparison, NaN cannot be directly compared to itself
 --> src/main.rs:2:24
  |
2 |     const TEST: bool = 5f32 == f32::NAN;
  |                        ^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(invalid_nan_comparisons)]` on by default
help: use `f32::is_nan()` or `f64::is_nan()` instead
  |
2 -     const TEST: bool = 5f32 == f32::NAN;
2 +     const TEST: bool = 5f32.is_nan();
  |

warning: 2 warnings emitted

Desired output

No response

Rationale and extra context

The suggestion is auto applicable

fn main() {
    const TEST: bool = 5f32.is_nan();
}

but does not compiled because it relies on unstabilized features

error: `core::f32::<impl f32>::is_nan` is not yet stable as a const fn
 --> src/main.rs:2:24
  |
2 |     const TEST: bool = 5f32.is_nan();
  |                        ^^^^^^^^^^^^^
  |
  = help: add `#![feature(const_float_classify)]` to the crate attributes to enable

error: aborting due to previous error

Other cases

No response

Anything else?

rustc 1.73.0-nightly (474709a 2023-08-03)
binary: rustc
commit-hash: 474709a
commit-date: 2023-08-03
host: x86_64-unknown-linux-gnu
release: 1.73.0-nightly
LLVM version: 16.0.5

@matthiaskrgr matthiaskrgr 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. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Aug 4, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 4, 2023
@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 5, 2023
@bors bors closed this as completed in 1305a43 Aug 6, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in 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