From d86d3f431dfb879b8470654ba7dcdaadc956f06f Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Sun, 30 Aug 2026 12:39:02 +0800 Subject: [PATCH] test(optimizer): cover folded type-check side effects --- .../is-type-fold-side-effects.phpt | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/compiler/optimizations/is-type-fold-side-effects.phpt b/tests/compiler/optimizations/is-type-fold-side-effects.phpt index 9c57b948..f34f31f1 100644 --- a/tests/compiler/optimizations/is-type-fold-side-effects.phpt +++ b/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