From 615d738abea23715f649e51cf27112451a0f607b Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 8 Oct 2023 20:36:25 +0200 Subject: [PATCH] ensure unary minus propagates NaN payloads exactly --- compiler/rustc_const_eval/src/interpret/operator.rs | 1 + src/tools/miri/tests/pass/float_nan.rs | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/compiler/rustc_const_eval/src/interpret/operator.rs b/compiler/rustc_const_eval/src/interpret/operator.rs index fe8572d9c6f..a685b499d0e 100644 --- a/compiler/rustc_const_eval/src/interpret/operator.rs +++ b/compiler/rustc_const_eval/src/interpret/operator.rs @@ -461,6 +461,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { Ok((ImmTy::from_bool(res, *self.tcx), false)) } ty::Float(fty) => { + // No NaN adjustment here, `-` is a bitwise operation! let res = match (un_op, fty) { (Neg, FloatTy::F32) => Scalar::from_f32(-val.to_f32()?), (Neg, FloatTy::F64) => Scalar::from_f64(-val.to_f64()?), diff --git a/src/tools/miri/tests/pass/float_nan.rs b/src/tools/miri/tests/pass/float_nan.rs index 8fa567aa106..cb9cc42c601 100644 --- a/src/tools/miri/tests/pass/float_nan.rs +++ b/src/tools/miri/tests/pass/float_nan.rs @@ -241,6 +241,14 @@ fn test_f32() { ]), || F32::from(just1 % all1_snan), ); + + // Unary `-` must preserve payloads exactly. + check_all_outcomes(HashSet::from_iter([F32::nan(Neg, Quiet, all1_payload)]), || { + F32::from(-all1) + }); + check_all_outcomes(HashSet::from_iter([F32::nan(Neg, Signaling, all1_payload)]), || { + F32::from(-all1_snan) + }); } fn test_f64() {