Revert "Avoid invalid NaN lint machine-applicable suggestion in const context"

Reverts PR https://github.com/rust-lang/rust/pull/114486 (commit 1305a43d0a)
This commit is contained in:
Eduardo Sánchez Muñoz 2024-09-09 18:20:27 +02:00
parent e3c822df4e
commit c39ae569d6
5 changed files with 15 additions and 16 deletions

View File

@ -1739,7 +1739,7 @@ pub(crate) enum InvalidNanComparisons {
#[diag(lint_invalid_nan_comparisons_eq_ne)] #[diag(lint_invalid_nan_comparisons_eq_ne)]
EqNe { EqNe {
#[subdiagnostic] #[subdiagnostic]
suggestion: Option<InvalidNanComparisonsSuggestion>, suggestion: InvalidNanComparisonsSuggestion,
}, },
#[diag(lint_invalid_nan_comparisons_lt_le_gt_ge)] #[diag(lint_invalid_nan_comparisons_lt_le_gt_ge)]
LtLeGtGe, LtLeGtGe,

View File

@ -605,36 +605,32 @@ fn lint_nan<'tcx>(
} }
fn eq_ne( fn eq_ne(
cx: &LateContext<'_>,
e: &hir::Expr<'_>, e: &hir::Expr<'_>,
l: &hir::Expr<'_>, l: &hir::Expr<'_>,
r: &hir::Expr<'_>, r: &hir::Expr<'_>,
f: impl FnOnce(Span, Span) -> InvalidNanComparisonsSuggestion, f: impl FnOnce(Span, Span) -> InvalidNanComparisonsSuggestion,
) -> InvalidNanComparisons { ) -> InvalidNanComparisons {
// FIXME(#72505): This suggestion can be restored if `f{32,64}::is_nan` is made const. let suggestion = if let Some(l_span) = l.span.find_ancestor_inside(e.span)
let suggestion = (!cx.tcx.hir().is_inside_const_context(e.hir_id)).then(|| { && let Some(r_span) = r.span.find_ancestor_inside(e.span)
if let Some(l_span) = l.span.find_ancestor_inside(e.span) {
&& let Some(r_span) = r.span.find_ancestor_inside(e.span) f(l_span, r_span)
{ } else {
f(l_span, r_span) InvalidNanComparisonsSuggestion::Spanless
} else { };
InvalidNanComparisonsSuggestion::Spanless
}
});
InvalidNanComparisons::EqNe { suggestion } InvalidNanComparisons::EqNe { suggestion }
} }
let lint = match binop.node { let lint = match binop.node {
hir::BinOpKind::Eq | hir::BinOpKind::Ne if is_nan(cx, l) => { hir::BinOpKind::Eq | hir::BinOpKind::Ne if is_nan(cx, l) => {
eq_ne(cx, e, l, r, |l_span, r_span| InvalidNanComparisonsSuggestion::Spanful { eq_ne(e, l, r, |l_span, r_span| InvalidNanComparisonsSuggestion::Spanful {
nan_plus_binop: l_span.until(r_span), nan_plus_binop: l_span.until(r_span),
float: r_span.shrink_to_hi(), float: r_span.shrink_to_hi(),
neg: (binop.node == hir::BinOpKind::Ne).then(|| r_span.shrink_to_lo()), neg: (binop.node == hir::BinOpKind::Ne).then(|| r_span.shrink_to_lo()),
}) })
} }
hir::BinOpKind::Eq | hir::BinOpKind::Ne if is_nan(cx, r) => { hir::BinOpKind::Eq | hir::BinOpKind::Ne if is_nan(cx, r) => {
eq_ne(cx, e, l, r, |l_span, r_span| InvalidNanComparisonsSuggestion::Spanful { eq_ne(e, l, r, |l_span, r_span| InvalidNanComparisonsSuggestion::Spanful {
nan_plus_binop: l_span.shrink_to_hi().to(r_span), nan_plus_binop: l_span.shrink_to_hi().to(r_span),
float: l_span.shrink_to_hi(), float: l_span.shrink_to_hi(),
neg: (binop.node == hir::BinOpKind::Ne).then(|| l_span.shrink_to_lo()), neg: (binop.node == hir::BinOpKind::Ne).then(|| l_span.shrink_to_lo()),

View File

@ -6,7 +6,6 @@
#![feature(f16_const)] #![feature(f16_const)]
#![feature(f128_const)] #![feature(f128_const)]
#![feature(const_float_classify)]
use std::hint::black_box; use std::hint::black_box;
use std::num::FpCategory::*; use std::num::FpCategory::*;

View File

@ -3,7 +3,6 @@
// This tests the float classification functions, for regular runtime code and for const evaluation. // This tests the float classification functions, for regular runtime code and for const evaluation.
#![feature(const_float_classify)]
#![feature(f16)] #![feature(f16)]
#![feature(f128)] #![feature(f128)]
#![feature(f16_const)] #![feature(f16_const)]

View File

@ -5,6 +5,11 @@ LL | const TEST: bool = 5f32 == f32::NAN;
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
| |
= note: `#[warn(invalid_nan_comparisons)]` on by default = note: `#[warn(invalid_nan_comparisons)]` on by default
help: use `f32::is_nan()` or `f64::is_nan()` instead
|
LL - const TEST: bool = 5f32 == f32::NAN;
LL + const TEST: bool = 5f32.is_nan();
|
warning: incorrect NaN comparison, NaN cannot be directly compared to itself warning: incorrect NaN comparison, NaN cannot be directly compared to itself
--> $DIR/invalid-nan-comparison.rs:14:5 --> $DIR/invalid-nan-comparison.rs:14:5