test(optimizer): cover folded type-check side effects

master
韩天峰 4 hours ago
parent bf0d7aa3a4
commit d86d3f431d
  1. 31
      tests/compiler/optimizations/is-type-fold-side-effects.phpt

@ -20,6 +20,18 @@ function boolSource(): bool
return true;
}
function assignmentSource(): int
{
echo "assignment-called\n";
return 73;
}
function exceptionSource(): int
{
echo "exception-called\n";
throw new RuntimeException('folded-exception');
}
function main(): void
{
if (is_int(intSource())) {
@ -32,6 +44,20 @@ function main(): void
// Plain variables still fold without extra evaluation.
$n = 7;
echo is_int($n) ? "var-int\n" : "var-not-int\n";
// The folded operand must run exactly once and preserve its result.
$assigned = 0;
echo is_int($assigned = assignmentSource()) ? "assigned-int\n" : "assigned-not-int\n";
echo "assigned-value=", $assigned, "\n";
// Folding the predicate must not suppress an exception from its operand.
try {
if (is_int(exceptionSource())) {
echo "exception-not-thrown\n";
}
} catch (RuntimeException $e) {
echo "caught=", $e->getMessage(), "\n";
}
}
?>
--EXPECT--
@ -42,3 +68,8 @@ is-float
bool-called
is-bool
var-int
assignment-called
assigned-int
assigned-value=73
exception-called
caught=folded-exception

Loading…
Cancel
Save