hhvm
hhvm copied to clipboard
Hack: Check against null with assignment in if not picked up
<?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)
Bump
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.