fix(optimizer): fold is_null for native scalars

master
韩天峰 9 hours ago
parent d321666721
commit 0f335a8135
  1. 11
      src/Optimizer/FuncCallOptimizer.php
  2. 47
      tests/compiler/type_decl/is-null-native-scalar.phpt

@ -693,7 +693,16 @@ trait FuncCallOptimizer
if ($this->isNativeObjectClass($this->detectClassOfExpr($value))) {
return '(' . $this->parseExprAsValue($value) . ' == nullptr)';
}
return '(' . $this->parseExprAsValue($value) . ').isNull()';
$type = $this->detectTypeOfExpr($value);
$valueCode = $this->parseExprAsValue($value);
if ($this->isNativeType($type)) {
// Fixed native scalars cannot contain null. Keep evaluating the
// operand because a call or increment may still have side effects.
return '((void) (' . $valueCode . '), false)';
}
return '(' . $valueCode . ').isNull()';
}
protected function genIsCallable(string $n, Node\Expr\FuncCall $e, array $c): string|false

@ -0,0 +1,47 @@
--TEST--
is_null returns false for fixed native scalars and preserves operand side effects
--FILE--
<?php
use native_types;
function checkInt(int $value): bool
{
return is_null($value);
}
function checkNullableInt(?int $value): bool
{
return is_null($value);
}
function emitInt(): int
{
echo "emitInt\n";
return 42;
}
function main(): void
{
$intValue = 42;
$floatValue = 1.5;
$boolValue = true;
var_dump(is_null($intValue));
var_dump(is_null($floatValue));
var_dump(is_null($boolValue));
var_dump(checkInt($intValue));
var_dump(is_null(emitInt()));
var_dump(checkNullableInt(null));
var_dump(checkNullableInt($intValue));
}
?>
--EXPECT--
bool(false)
bool(false)
bool(false)
bool(false)
emitInt
bool(false)
bool(true)
bool(false)
Loading…
Cancel
Save