diff options
author | Ilija Tovilo <ilija.tovilo@me.com> | 2020-03-19 00:51:51 +0100 |
---|---|---|
committer | Nikita Popov <nikita.ppv@gmail.com> | 2020-04-23 12:51:23 +0200 |
commit | 0810fcd0d0eb907b79b9619f73dc7d13aed5c611 (patch) | |
tree | 197e6e5588743bf8c71441cc38c3b8bf280efff6 /Zend/tests/throw | |
parent | 64d30c19768484c8eb7225818fe32d7e3696557f (diff) | |
download | php-git-0810fcd0d0eb907b79b9619f73dc7d13aed5c611.tar.gz |
Make throw statement an expression
RFC: https://wiki.php.net/rfc/throw_expression
This has an open issue with temporaries that are live at the time
of the throw being leaked. Landing this now for easier testing and
will revert if we cannot resolve the issue.
Closes GH-5279.
Diffstat (limited to 'Zend/tests/throw')
-rw-r--r-- | Zend/tests/throw/001.phpt | 175 | ||||
-rw-r--r-- | Zend/tests/throw/002.phpt | 127 |
2 files changed, 302 insertions, 0 deletions
diff --git a/Zend/tests/throw/001.phpt b/Zend/tests/throw/001.phpt new file mode 100644 index 0000000000..072d9f45b5 --- /dev/null +++ b/Zend/tests/throw/001.phpt @@ -0,0 +1,175 @@ +--TEST-- +throw expression +--FILE-- +<?php + +try { + $result = true && throw new Exception("true && throw"); + var_dump($result); +} catch (Exception $e) { + var_dump($e->getMessage()); +} + +try { + $result = false && throw new Exception("false && throw"); + var_dump($result); +} catch (Exception $e) { + var_dump($e->getMessage()); +} + +try { + $result = true and throw new Exception("true and throw"); + var_dump($result); +} catch (Exception $e) { + var_dump($e->getMessage()); +} + +try { + $result = false and throw new Exception("false and throw"); + var_dump($result); +} catch (Exception $e) { + var_dump($e->getMessage()); +} + +try { + $result = true || throw new Exception("true || throw"); + var_dump($result); +} catch (Exception $e) { + var_dump($e->getMessage()); +} + +try { + $result = false || throw new Exception("false || throw"); + var_dump($result); +} catch (Exception $e) { + var_dump($e->getMessage()); +} + +try { + $result = true or throw new Exception("true or throw"); + var_dump($result); +} catch (Exception $e) { + var_dump($e->getMessage()); +} + +try { + $result = false or throw new Exception("false or throw"); + var_dump($result); +} catch (Exception $e) { + var_dump($e->getMessage()); +} + +try { + $result = null ?? throw new Exception("null ?? throw"); + var_dump($result); +} catch (Exception $e) { + var_dump($e->getMessage()); +} + +try { + $result = "foo" ?? throw new Exception('"foo" ?? throw'); + var_dump($result); +} catch (Exception $e) { + var_dump($e->getMessage()); +} + +try { + $result = null ?: throw new Exception("null ?: throw"); + var_dump($result); +} catch (Exception $e) { + var_dump($e->getMessage()); +} + +try { + $result = "foo" ?: throw new Exception('"foo" ?: throw'); + var_dump($result); +} catch (Exception $e) { + var_dump($e->getMessage()); +} + +try { + $callable = fn() => throw new Exception("fn() => throw"); + var_dump("not yet"); + $callable(); +} catch (Exception $e) { + var_dump($e->getMessage()); +} + +$result = "bar"; +try { + $result = throw new Exception(); +} catch (Exception $e) {} +var_dump($result); + +try { + var_dump( + throw new Exception("exception 1"), + throw new Exception("exception 2") + ); +} catch (Exception $e) { + var_dump($e->getMessage()); +} + +try { + $result = true ? true : throw new Exception("true ? true : throw"); + var_dump($result); +} catch (Exception $e) { + var_dump($e->getMessage()); +} + +try { + $result = false ? true : throw new Exception("false ? true : throw"); + var_dump($result); +} catch (Exception $e) { + var_dump($e->getMessage()); +} + +try { + throw new Exception() + 1; +} catch (Throwable $e) { + var_dump($e->getMessage()); +} + +try { + throw $exception = new Exception('throw $exception = new Exception();'); +} catch (Exception $e) {} +var_dump($exception->getMessage()); + +try { + $exception = null; + throw $exception ??= new Exception('throw $exception ??= new Exception();'); +} catch (Exception $e) {} +var_dump($exception->getMessage()); + +try { + throw null ?? new Exception('throw null ?? new Exception();'); +} catch (Exception $e) { + var_dump($e->getMessage()); +} + +?> +--EXPECTF-- +string(13) "true && throw" +bool(false) +string(14) "true and throw" +bool(false) +bool(true) +string(14) "false || throw" +bool(true) +string(14) "false or throw" +string(13) "null ?? throw" +string(3) "foo" +string(13) "null ?: throw" +string(3) "foo" +string(7) "not yet" +string(13) "fn() => throw" +string(3) "bar" +string(11) "exception 1" +bool(true) +string(20) "false ? true : throw" + +Notice: Object of class Exception could not be converted to number in %s on line %d +string(22) "Can only throw objects" +string(35) "throw $exception = new Exception();" +string(37) "throw $exception ??= new Exception();" +string(30) "throw null ?? new Exception();" diff --git a/Zend/tests/throw/002.phpt b/Zend/tests/throw/002.phpt new file mode 100644 index 0000000000..8736c27fc3 --- /dev/null +++ b/Zend/tests/throw/002.phpt @@ -0,0 +1,127 @@ +--TEST-- +Test throw with various expressions +--FILE-- +<?php + +class Foo { + public function createNotFoundException() { + return new Exception('Not found'); + } + + public function throwException() { + throw $this->createNotFoundException(); + } + + public static function staticCreateNotFoundException() { + return new Exception('Static not found'); + } + + public static function staticThrowException() { + throw static::staticCreateNotFoundException(); + } +} + +try { + (new Foo())->throwException(); +} catch(Exception $e) { + echo $e->getMessage() . "\n"; +} + +try { + Foo::staticThrowException(); +} catch(Exception $e) { + echo $e->getMessage() . "\n"; +} + +try { + throw true ? new Exception('Ternary true 1') : new Exception('Ternary true 2'); +} catch(Exception $e) { + echo $e->getMessage() . "\n"; +} + +try { + throw false ? new Exception('Ternary false 1') : new Exception('Ternary false 2'); +} catch(Exception $e) { + echo $e->getMessage() . "\n"; +} + +try { + $exception1 = new Exception('Coalesce non-null 1'); + $exception2 = new Exception('Coalesce non-null 2'); + throw $exception1 ?? $exception2; +} catch(Exception $e) { + echo $e->getMessage() . "\n"; +} + +try { + $exception1 = null; + $exception2 = new Exception('Coalesce null 2'); + throw $exception1 ?? $exception2; +} catch(Exception $e) { + echo $e->getMessage() . "\n"; +} + +try { + throw $exception = new Exception('Assignment'); +} catch(Exception $e) { + echo $e->getMessage() . "\n"; +} + +try { + $exception = null; + throw $exception ??= new Exception('Coalesce assignment null'); +} catch(Exception $e) { + echo $e->getMessage() . "\n"; +} + +try { + $exception = new Exception('Coalesce assignment non-null 1'); + throw $exception ??= new Exception('Coalesce assignment non-null 2'); +} catch(Exception $e) { + echo $e->getMessage() . "\n"; +} + +$andConditionalTest = function ($condition1, $condition2) { + throw $condition1 && $condition2 + ? new Exception('And in conditional 1') + : new Exception('And in conditional 2'); +}; + +try { + $andConditionalTest(false, false); +} catch(Exception $e) { + echo $e->getMessage() . "\n"; +} + +try { + $andConditionalTest(false, true); +} catch (Exception $e) { + echo $e->getMessage() . "\n"; +} + +try { + $andConditionalTest(true, false); +} catch (Exception $e) { + echo $e->getMessage() . "\n"; +} + +try { + $andConditionalTest(true, true); +} catch (Exception $e) { + echo $e->getMessage() . "\n"; +} + +--EXPECT-- +Not found +Static not found +Ternary true 1 +Ternary false 2 +Coalesce non-null 1 +Coalesce null 2 +Assignment +Coalesce assignment null +Coalesce assignment non-null 1 +And in conditional 2 +And in conditional 2 +And in conditional 2 +And in conditional 1 |