hhvm icon indicating copy to clipboard operation
hhvm copied to clipboard

Hack: Check against null with assignment in if not picked up

Open HRMsimon opened this issue 11 years ago • 2 comments

<?hh

class A {
    public function getA(): ?A {
        return null;
    }

    public function setA(A $a): void {}

    public function test() {
        $a = $this->getA();
        if ($a !== null) {
            $this->setA($a);
        }
        if (($a = $this->getA()) !== null) {
            $this->setA($a);
        }
        if (($a = $this->getA()) && $a !== null) {
            $this->setA($a);
        }
    }
}
$ hh_single_type_check test.php 
File "test.php", line 16, characters 25-26:
Invalid argument (Typing[4110])
File "test.php", line 8, characters 26-26:
This is an object of type A
File "test.php", line 4, characters 29-30:
It is incompatible with a nullable type

When doing the assignment outside the if, then the null check in the condition, it works. (1st if)

If doing the assignment, then doing the check in a separate parts of the condition it works. (3rd if)

If doing the assignment and the check in the same part of the condition, it doesn't work. (2nd if)

HRMsimon avatar Aug 18 '15 05:08 HRMsimon

Bump

steelbrain avatar Sep 06 '15 02:09 steelbrain

I am going over old issues on this repository, to see which ones apply to the current versions of hhvm.

This repro nolonger works in hhvm 4.38.0 and above. This is an assignment as an expression.

($a = $this->getA())

does not "return" a value anymore. This behavior can be controlled using disable_lval_as_an_expression=false .hhconfig directive.

lexidor avatar May 24 '20 00:05 lexidor