Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Zend/tests/exit_finally_3.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ function test() {
var_dump(test());

?>
--EXPECT--
Exit
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d
Exit
5 changes: 3 additions & 2 deletions Zend/tests/gc/gc_050.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ for ($i = 0; $i < 100000; $i++) {
}
echo "OK\n";
?>
--EXPECT--
OK
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d
OK
5 changes: 3 additions & 2 deletions Zend/tests/generators/finally/return_return.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ $gen = gen();
$gen->rewind(); // force run

?>
--EXPECT--
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d
before return
before return in inner finally
outer finally run
outer finally run
5 changes: 3 additions & 2 deletions Zend/tests/generators/finally/yield_return.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ foreach (foo(1, 5) as $x) {
echo $x, "\n";
}
?>
--EXPECT--
1
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d
1
5 changes: 3 additions & 2 deletions Zend/tests/generators/get_return_and_finally.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ try {
}

?>
--EXPECT--
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d
int(42)
gen2() throw
Cannot get return value of a generator that hasn't returned
Cannot get return value of a generator that hasn't returned
5 changes: 3 additions & 2 deletions Zend/tests/generators/gh11028_1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ test("false", false);
test("true", true);
test("object", new stdClass);
?>
--EXPECT--
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d
yield null
Keys must be of type int|string during array unpacking
yield false
Keys must be of type int|string during array unpacking
yield true
Keys must be of type int|string during array unpacking
yield object
Keys must be of type int|string during array unpacking
Keys must be of type int|string during array unpacking
2 changes: 2 additions & 0 deletions Zend/tests/generators/gh11028_2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ $c = (function () {
})()[0];
?>
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d

Warning: Undefined variable $a in %s on line %d

Fatal error: Uncaught Error: Keys must be of type int|string during array unpacking in %s:%d
Expand Down
5 changes: 3 additions & 2 deletions Zend/tests/generators/gh11028_3.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ try {
echo $e->getMessage(), "\n";
}
?>
--EXPECT--
exception
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d
exception
5 changes: 3 additions & 2 deletions Zend/tests/generators/yield_in_finally_cleanup.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ gen4()->rewind();

?>
===DONE===
--EXPECT--
===DONE===
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d
===DONE===
2 changes: 2 additions & 0 deletions Zend/tests/oss_fuzz_438780145.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ test();

?>
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d

Fatal error: Uncaught TypeError: test(): Return value must be of type int, string returned in %s:%d
Stack trace:
#0 %s(%d): test()
Expand Down
2 changes: 2 additions & 0 deletions Zend/tests/return_types/029.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ function foo() : array {
foo();
?>
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d

Fatal error: Uncaught Exception: xxxx in %s:%d
Stack trace:
#0 %s(%d): foo()
Expand Down
2 changes: 2 additions & 0 deletions Zend/tests/return_types/never_finally_return.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ function foo() : never {
// Note the lack of function call: function validated at compile-time
?>
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d

Fatal error: A never-returning function must not return in %s on line %d
5 changes: 3 additions & 2 deletions Zend/tests/try/bug70228.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ function foo() {

var_dump(foo());
?>
--EXPECT--
string(2) "bb"
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d
string(2) "bb"
5 changes: 3 additions & 2 deletions Zend/tests/try/bug70228_2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ function test() {

var_dump(test());
?>
--EXPECT--
int(42)
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d
int(42)
5 changes: 3 additions & 2 deletions Zend/tests/try/bug70228_3.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ try {
} while ($e);
}
?>
--EXPECT--
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d
2
1
1
5 changes: 3 additions & 2 deletions Zend/tests/try/bug70228_4.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ try {
} while ($e);
}
?>
--EXPECT--
1
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d
1
5 changes: 3 additions & 2 deletions Zend/tests/try/bug70228_6.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ function test($x) {

var_dump(test([1]));
?>
--EXPECT--
int(42)
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d
int(42)
7 changes: 5 additions & 2 deletions Zend/tests/try/catch_finally_003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ function &bar($a) {
var_dump(foo("para"));
var_dump(bar("para"));
?>
--EXPECT--
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d

Deprecated: Returning from a finally block is deprecated in %s on line %d
string(3) "try"
string(7) "finally"
string(7) "finally"
try
string(4) "para"
string(4) "para"
5 changes: 3 additions & 2 deletions Zend/tests/try/catch_finally_005.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ function foo ($a) {

var_dump(foo("para"));
?>
--EXPECT--
int(3)
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d
int(3)
5 changes: 3 additions & 2 deletions Zend/tests/try/catch_finally_006.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ try {
var_dump($e->getMessage());
}
?>
--EXPECT--
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d
string(4) "para"
string(7) "finally"
string(6) "return"
string(6) "return"
5 changes: 3 additions & 2 deletions Zend/tests/try/finally_goto_005.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ label: try {
}

?>
--EXPECT--
success
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d
success
86 changes: 86 additions & 0 deletions Zend/tests/try/finally_return_deprecation.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
--TEST--
Returning from a finally block is deprecated
--FILE--
<?php

function withValue() {
try {
} finally {
return 1;
}
}

function withoutValue() {
try {
} finally {
return;
}
}

// Not deprecated: the return belongs to the try block, not the finally.
function inTry() {
try {
return 1;
} finally {
}
}

// Not deprecated: the return belongs to a catch block.
function inCatch() {
try {
throw new Exception();
} catch (Exception $e) {
return 1;
} finally {
}
}

// Not deprecated: the return belongs to a nested closure, not the finally.
function nestedClosure() {
try {
} finally {
$f = function () {
return 1;
};
}
}

// Deprecated twice: both returns are lexically inside the outer finally, even
// though one of them sits in a nested try block.
function nestedTry() {
try {
} finally {
try {
return 1;
} finally {
return 2;
}
}
}

// Deprecated once: only the closure's own finally return is flagged. The outer
// finally must not leak into the closure, so the closure's try return is left alone.
function closureWithTry() {
try {
} finally {
$f = function () {
try {
return 1;
} finally {
return 2;
}
};
}
}

?>
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d

Deprecated: Returning from a finally block is deprecated in %s on line %d

Deprecated: Returning from a finally block is deprecated in %s on line %d

Deprecated: Returning from a finally block is deprecated in %s on line %d

Deprecated: Returning from a finally block is deprecated in %s on line %d
5 changes: 3 additions & 2 deletions Zend/tests/try/try_catch_finally_003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ function foo () {

var_dump(foo());
?>
--EXPECT--
1234int(4)
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d
1234int(4)
5 changes: 3 additions & 2 deletions Zend/tests/try/try_finally_013.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function foo() {

foo();
?>
--EXPECT--
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d
try
finally
finally
5 changes: 3 additions & 2 deletions Zend/tests/try/try_finally_014.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function foo() {

foo();
?>
--EXPECT--
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d
try
finally
finally
5 changes: 3 additions & 2 deletions Zend/tests/try/try_finally_021.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ foreach ([0] as $_) {
}
}
?>
--EXPECT--
ok
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d
ok
ok
1 change: 1 addition & 0 deletions Zend/tests/try/try_finally_023.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ try {

?>
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d
Exception: 1 in %s:%d
Stack trace:
#0 %s(%d): test()
Expand Down
1 change: 1 addition & 0 deletions Zend/tests/try/try_finally_027.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ try {

?>
--EXPECTF--
Deprecated: Returning from a finally block is deprecated in %s on line %d
Exception: 1 in %s:%d
Stack trace:
#0 %s(%d): test()
Expand Down
8 changes: 8 additions & 0 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ void zend_oparray_context_begin(zend_oparray_context *prev_context, zend_op_arra
CG(context).brk_cont_array = NULL;
CG(context).labels = NULL;
CG(context).in_jmp_frameless_branch = false;
CG(context).in_finally = false;
CG(context).active_property_info_name = NULL;
CG(context).active_property_hook_kind = (zend_property_hook_kind)-1;
}
Expand Down Expand Up @@ -5993,6 +5994,10 @@ static void zend_compile_return(const zend_ast *ast) /* {{{ */
zend_compile_expr(&expr_node, expr_ast);
}

if (CG(context).in_finally) {
zend_error(E_DEPRECATED, "Returning from a finally block is deprecated");
}

if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)
&& (expr_node.op_type == IS_CV || (by_ref && expr_node.op_type == IS_VAR))
&& zend_has_finally()) {
Expand Down Expand Up @@ -7163,7 +7168,10 @@ static void zend_compile_try(const zend_ast *ast) /* {{{ */

zend_emit_op(NULL, ZEND_JMP, NULL, NULL);

bool orig_in_finally = CG(context).in_finally;
CG(context).in_finally = true;
zend_compile_stmt(finally_ast);
CG(context).in_finally = orig_in_finally;

CG(active_op_array)->try_catch_array[try_catch_offset].finally_op = opnum_jmp + 1;
CG(active_op_array)->try_catch_array[try_catch_offset].finally_end
Expand Down
Loading
Loading