Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Convert exit (and die) from language constructs to functions #13483

Merged
merged 9 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
4 changes: 1 addition & 3 deletions Zend/Optimizer/block_pass.c
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,6 @@ static void zend_optimize_block(zend_basic_block *block, zend_op_array *op_array
break;

case ZEND_RETURN:
case ZEND_EXIT:
if (opline->op1_type == IS_TMP_VAR) {
src = VAR_SOURCE(opline->op1);
if (src && src->opcode == ZEND_QM_ASSIGN) {
Expand Down Expand Up @@ -1221,8 +1220,7 @@ static void zend_jmp_optimization(zend_basic_block *block, zend_op_array *op_arr
target = op_array->opcodes + target_block->start;
if ((target->opcode == ZEND_RETURN ||
target->opcode == ZEND_RETURN_BY_REF ||
target->opcode == ZEND_GENERATOR_RETURN ||
target->opcode == ZEND_EXIT) &&
target->opcode == ZEND_GENERATOR_RETURN) &&
!(op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
/* JMP L, L: RETURN to immediate RETURN */
*last_op = *target;
Expand Down
1 change: 0 additions & 1 deletion Zend/Optimizer/pass1.c
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ void zend_optimizer_pass1(zend_op_array *op_array, zend_optimizer_ctx *ctx)
case ZEND_RETURN:
case ZEND_RETURN_BY_REF:
case ZEND_GENERATOR_RETURN:
case ZEND_EXIT:
case ZEND_THROW:
case ZEND_MATCH_ERROR:
case ZEND_CATCH:
Expand Down
3 changes: 1 addition & 2 deletions Zend/Optimizer/pass3.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ void zend_optimizer_pass3(zend_op_array *op_array, zend_optimizer_ctx *ctx)
MAKE_NOP(opline);
} else if ((target->opcode == ZEND_RETURN ||
target->opcode == ZEND_RETURN_BY_REF ||
target->opcode == ZEND_GENERATOR_RETURN ||
target->opcode == ZEND_EXIT) &&
target->opcode == ZEND_GENERATOR_RETURN) &&
!(op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
/* JMP L, L: RETURN to immediate RETURN */
*opline = *target;
Expand Down
4 changes: 0 additions & 4 deletions Zend/Optimizer/zend_call_graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,6 @@ ZEND_API void zend_analyze_calls(zend_arena **arena, zend_script *script, uint32
call_info->send_unpack = 1;
}
break;
case ZEND_EXIT:
/* In this case the DO_CALL opcode may have been dropped
* and caller_call_opline will be NULL. */
break;
}
opline++;
}
Expand Down
2 changes: 0 additions & 2 deletions Zend/Optimizer/zend_cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ ZEND_API void zend_build_cfg(zend_arena **arena, const zend_op_array *op_array,
}
break;
case ZEND_MATCH_ERROR:
case ZEND_EXIT:
case ZEND_THROW:
/* Don't treat THROW as terminator if it's used in expression context,
* as we may lose live ranges when eliminating unreachable code. */
Expand Down Expand Up @@ -506,7 +505,6 @@ ZEND_API void zend_build_cfg(zend_arena **arena, const zend_op_array *op_array,
case ZEND_RETURN:
case ZEND_RETURN_BY_REF:
case ZEND_GENERATOR_RETURN:
case ZEND_EXIT:
case ZEND_THROW:
case ZEND_MATCH_ERROR:
case ZEND_VERIFY_NEVER_TYPE:
Expand Down
3 changes: 3 additions & 0 deletions Zend/tests/arginfo_zpp_mismatch.inc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ function skipFunction($function): bool {
|| $function === 'readline'
|| $function === 'readline_read_history'
|| $function === 'readline_write_history'
/* terminates script */
|| $function === 'exit'
|| $function === 'die'
/* intentionally violate invariants */
|| $function === 'zend_create_unterminated_string'
|| $function === 'zend_test_array_return'
Expand Down
2 changes: 1 addition & 1 deletion Zend/tests/bug77339.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Foo
if (!isset($arguments[0])) {
var_dump(['getSomeWhat']);
var_dump($arguments);
exit;
exit();
}
}
echo "OK\n";
Expand Down
13 changes: 0 additions & 13 deletions Zend/tests/die_string_cast_exception.phpt

This file was deleted.

16 changes: 16 additions & 0 deletions Zend/tests/exit/ast_print_assert_die_const.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Printing AST of die "constant" via assert
--INI--
zend.assertions=1
--FILE--
<?php

try {
assert(0 && die);
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}

?>
--EXPECT--
AssertionError: assert(0 && \exit())
16 changes: 16 additions & 0 deletions Zend/tests/exit/ast_print_assert_die_function.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Printing AST of die function via assert
--INI--
zend.assertions=1
--FILE--
<?php

try {
assert(0 && die());
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}

?>
--EXPECT--
AssertionError: assert(0 && \exit())
16 changes: 16 additions & 0 deletions Zend/tests/exit/ast_print_assert_exit_const.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Printing AST of exit "constant" via assert
--INI--
zend.assertions=1
--FILE--
<?php

try {
assert(0 && exit);
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}

?>
--EXPECT--
AssertionError: assert(0 && \exit())
16 changes: 16 additions & 0 deletions Zend/tests/exit/ast_print_assert_exit_function.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Printing AST of exit function via assert
--INI--
zend.assertions=1
--FILE--
<?php

try {
assert(0 && exit());
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}

?>
--EXPECT--
AssertionError: assert(0 && \exit())
37 changes: 37 additions & 0 deletions Zend/tests/exit/define_class_members_exit_die.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--TEST--
Can define die and exit as class methods, constants and property
--FILE--
<?php

class Foo {
public $exit;
public $die;

const die = 5;
const exit = 10;

public function exit() {
return 20;
}

public function die() {
return 15;
}
}

var_dump(Foo::die);
var_dump(Foo::exit);
$o = new Foo();
var_dump($o->exit);
var_dump($o->die);
var_dump($o->exit());
var_dump($o->die());

?>
--EXPECT--
int(5)
int(10)
NULL
NULL
int(20)
int(15)
12 changes: 12 additions & 0 deletions Zend/tests/exit/define_die_constant.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Attempting to define die constant
--FILE--
<?php

const die = 5;

var_dump(die);

?>
--EXPECTF--
Parse error: syntax error, unexpected token "exit", expecting identifier in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/exit/define_die_constant_namespace.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Attempting to define die constant in a namespace
--FILE--
<?php

namespace Foo;

const die = 5;

var_dump(die);

?>
--EXPECTF--
Parse error: syntax error, unexpected token "exit", expecting identifier in %s on line %d
10 changes: 10 additions & 0 deletions Zend/tests/exit/define_die_function.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Attempting to define die() function
--FILE--
<?php

function die() { }

?>
--EXPECTF--
Parse error: syntax error, unexpected token "exit", expecting "(" in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/exit/define_die_function_namespace.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Attempting to define die() function in a namespace
--FILE--
<?php

namespace Foo;

function die() { }

var_dump(die());

?>
--EXPECTF--
Parse error: syntax error, unexpected token "exit", expecting "(" in %s on line %d
12 changes: 12 additions & 0 deletions Zend/tests/exit/define_exit_constant.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Attempting to define exit constant
--FILE--
<?php

const exit = 5;

var_dump(exit);

?>
--EXPECTF--
Parse error: syntax error, unexpected token "exit", expecting identifier in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/exit/define_exit_constant_namespace.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Attempting to define exit constant in a namespace
--FILE--
<?php

namespace Foo;

const exit = 5;

var_dump(exit);

?>
--EXPECTF--
Parse error: syntax error, unexpected token "exit", expecting identifier in %s on line %d
10 changes: 10 additions & 0 deletions Zend/tests/exit/define_exit_function.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Attempting to define exit() function
--FILE--
<?php

function exit() { }

?>
--EXPECTF--
Parse error: syntax error, unexpected token "exit", expecting "(" in %s on line %d
12 changes: 12 additions & 0 deletions Zend/tests/exit/define_exit_function_namespace.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Attempting to define exit() function in a namespace
--FILE--
<?php

namespace Foo;

function exit() { }

?>
--EXPECTF--
Parse error: syntax error, unexpected token "exit", expecting "(" in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/exit/define_goto_label_die.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Attempting to define a goto label called die
--FILE--
<?php

echo "Before\n";

echo "In between\n";
die:
echo "After\n";

?>
--EXPECTF--
Parse error: syntax error, unexpected token ":" in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/exit/define_goto_label_die_with_jump.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Attempting to define a goto label called die and jump to it
--FILE--
<?php

echo "Before\n";
goto die;
echo "In between\n";
die:
echo "After\n";

?>
--EXPECTF--
Parse error: syntax error, unexpected token "exit", expecting identifier in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/exit/define_goto_label_exit.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Attempting to define a goto label called exit
--FILE--
<?php

echo "Before\n";

echo "In between\n";
exit:
echo "After\n";

?>
--EXPECTF--
Parse error: syntax error, unexpected token ":" in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/exit/define_goto_label_exit_with_jump.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Attempting to define a goto label called exit and jump to it
--FILE--
<?php

echo "Before\n";
goto exit;
echo "In between\n";
exit:
echo "After\n";

?>
--EXPECTF--
Parse error: syntax error, unexpected token "exit", expecting identifier in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/exit/die_string_cast_exception.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Bug #79777: String cast exception during die should be handled gracefully
--FILE--
<?php

try {
die(new stdClass);
} catch (TypeError $e) {
echo $e->getMessage(), PHP_EOL;
}

?>
--EXPECT--
exit(): Argument #1 ($code) must be of type string|int, stdClass given
12 changes: 12 additions & 0 deletions Zend/tests/exit/disabling_die.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Using disable_functions INI to remove die
--INI--
disable_functions=die
--FILE--
<?php

die();

?>
--EXPECT--
Warning: Cannot disable function die() in Unknown on line 0
Loading
Loading