Skip to content

The Not ! operator in combination with NAN fails with OPcache enabled #15773

Open

Description

@jsiefer

Description

The following code:

$a = 100.0;
$b = \NAN;

$cmp = $a > $b; // since NAN always false;
$cmp2 = !($a > $b); // $expected true;

var_dump($cmp);
var_dump($cmp2);

assert($cmp === false);
assert($cmp2 === true);

Resulted in this output:

bool(false)
bool(true)

Resulted in this output with opcache enabled:

bool(false)
bool(false) ❌

But I expected this output instead:

bool(false)
bool(true)

The issue only seems to appear using opcache. Also spotted on different environments with older PHP versions.

PHP 8.3.11 (cli) (built: Aug 30 2024 16:21:34) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.11, Copyright (c) Zend Technologies
    with Xdebug v3.3.1, Copyright (c) 2002-2023, by Derick Rethans
    with Zend OPcache v8.3.11, Copyright (c), by Zend Technologies

PHP Version

PHP 8.3.11

Operating System

No response

Activity

  1. cmb69 commented on Sep 6, 2024

    @cmb69
    Member

    This is likely due to the optimizer converting !($a > $b) to $a <= $b.

    If I remember correctly, this has been reported before. Well, might have had https://bugs.php.net/bug.php?id=80701 in mind.

  2. cmb69 commented on Sep 6, 2024

    @cmb69
    Member

    This is likely due to the optimizer converting !($a > $b) to $a <= $b.

    Indeed (simplified script):

         ; (lines=6, args=0, vars=3, tmps=5)
         ; (before optimizer)
         ; C:\php-sdk\phpdev\vs17\x64\gh15773.php:1-13
         ; return  [] RANGE[0..0]
    0000 ASSIGN CV0($a) float(100)
    0001 ASSIGN CV1($b) float(nan)
    0002 T5 = IS_SMALLER CV1($b) CV0($a)
    0003 T6 = BOOL_NOT T5
    0004 ASSIGN CV2($cmp2) T6
    0005 RETURN int(1)
    
    $_main:
         ; (lines=5, args=0, vars=3, tmps=1)
         ; (after optimizer)
         ; C:\php-sdk\phpdev\vs17\x64\gh15773.php:1-13
    0000 ASSIGN CV0($a) float(100)
    0001 ASSIGN CV1($b) float(nan)
    0002 T3 = IS_SMALLER_OR_EQUAL CV0($a) CV1($b)
    0003 ASSIGN CV2($cmp2) T3
    0004 RETURN int(1)
    

    Might be wontfix, since the optimization is generally useful, and NaN is very special anyway. Maybe PHP should throw on any NaN comparisons.

  3. jsiefer commented on Sep 6, 2024

    @jsiefer
    Author

    I found it super mean to debug, but I am with you on the performance optimization.

    Maybe its possible to show warnings if given pattern is used. Might be something for static type checkers.... just thinking loud

  4. cmb69 commented on Sep 6, 2024

    @cmb69
    Member

    Well, actually NaN is a somewhat special case of incomparable values, and the [documentation](language.operators.comparison.incomparable states:

    While identity comparison (=== and !==) can be applied to arbitrary values, the other comparison operators should only be applied to comparable values. The result of comparing incomparable values is undefined, and should not be relied upon.

    That's the easy way out.

    Maybe its possible to show warnings if given pattern is used.

    Yeah, a warning would be less intrusive than throwing, but would also be a BC break. It seems to me that this warrants the RFC process.

    Might be something for static type checkers.

    A static type checker may not be able to detect all cases, especially as NaN is a float, and can be the result of a couple of operations.

  5. changed the title [-]The Not ! opperator in componation with NAN fails with OPcache enabled[/-] [+]The Not ! operator in combination with NAN fails with OPcache enabled[/+] on Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions