-
Notifications
You must be signed in to change notification settings - Fork 8k
[RFC] Pattern matching #20574
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
Draft
iluuu1994
wants to merge
13
commits into
php:master
Choose a base branch
from
iluuu1994:pattern-matching-3
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,166
−592
Draft
[RFC] Pattern matching #20574
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
febcdb8
Implement pattern matching
iluuu1994 c7024e4
Switch to alternative object syntax
iluuu1994 9c9eedc
[skip ci] Add support for IS_TYPE to zend_dump.c
iluuu1994 542df1c
Improve sccp
iluuu1994 1d3eefe
Use system rounded_up_to_pow2() if available
iluuu1994 20943c0
[skip ci] Name structs
iluuu1994 08727b2
[skip ci] Improve test output for exceptions
iluuu1994 65b0b10
[skip ci] Add comment to explain why we're using negation for label o…
iluuu1994 522d4af
[skip ci] Remove fixme
iluuu1994 7eaa949
[skip ci] Remove leftover ..< and ..= operators
iluuu1994 47e16f1
[skip ci] Remove unused macro
iluuu1994 91a4b45
Fix conflicting ast flags
iluuu1994 249c88c
[skip ci] Specify error class in test
iluuu1994 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| --TEST-- | ||
| And pattern | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| interface A {} | ||
| interface B {} | ||
| interface C {} | ||
| class Foo implements A, B {} | ||
|
|
||
| var_dump(1 is int & 1); | ||
| var_dump(2 is int & (1|2)); | ||
| var_dump(3 is float & 1); | ||
| var_dump(4 is int & float); | ||
| var_dump([] is [] & [...]); | ||
| var_dump('foo' is string & 'bar'); | ||
| var_dump(new Foo() is A&B); | ||
| var_dump(new Foo() is (A&C)); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| bool(true) | ||
| bool(true) | ||
| bool(false) | ||
| bool(false) | ||
| bool(true) | ||
| bool(false) | ||
| bool(true) | ||
| bool(false) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| --TEST-- | ||
| Array pattern | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| var_dump([] is []); | ||
| var_dump(42 is []); | ||
| var_dump('foo' is []); | ||
| var_dump([42] is [42]); | ||
| var_dump([42] is []); | ||
| var_dump([42] is [43]); | ||
| var_dump([42] is ['0' => 42]); | ||
| var_dump([42, 43] is [42]); | ||
| var_dump([42, 43] is [42, ...]); | ||
| var_dump([42] is [$a]); | ||
| var_dump($a); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| bool(true) | ||
| bool(false) | ||
| bool(false) | ||
| bool(true) | ||
| bool(false) | ||
| bool(false) | ||
| bool(true) | ||
| bool(false) | ||
| bool(true) | ||
| bool(true) | ||
| int(42) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| --TEST-- | ||
| Array pattern with mixed implicit and explicit keys | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| var_dump([] is ['foo', 1 => 'bar']); | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| Fatal error: Must not mix implicit and explicit array keys in array pattern in %s on line %d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| --TEST-- | ||
| Object pattern matching | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| (function () { | ||
| $o = new stdClass(); | ||
|
|
||
| try { | ||
| var_dump($o is self); | ||
| } catch (Throwable $e) { | ||
| echo $e->getMessage(), "\n"; | ||
iluuu1994 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| try { | ||
| var_dump($o is parent); | ||
| } catch (Throwable $e) { | ||
| echo $e->getMessage(), "\n"; | ||
| } | ||
|
|
||
| try { | ||
| var_dump($o is static); | ||
| } catch (Throwable $e) { | ||
| echo $e->getMessage(), "\n"; | ||
| } | ||
| })(); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| Cannot access "self" when no class scope is active | ||
| Cannot access "parent" when no class scope is active | ||
| Cannot access "static" when no class scope is active | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| --TEST-- | ||
| Binding pattern | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| class Box { | ||
| public function __construct( | ||
| public $value, | ||
| ) {} | ||
| } | ||
|
|
||
| class NotBox { | ||
| public function __construct( | ||
| public $value, | ||
| ) {} | ||
| } | ||
|
|
||
| class Many { | ||
| public function __construct( | ||
| public $a = 1, | ||
| public $b = 2, | ||
| public $c = 3, | ||
| public $d = 4, | ||
| public $e = 5, | ||
| public $f = 6, | ||
| public $g = 7, | ||
| public $h = 8, | ||
| public $i = 9, | ||
| public $j = 10, | ||
| ) {} | ||
| } | ||
|
|
||
| var_dump(10 is $a); | ||
| var_dump($a); | ||
|
|
||
| var_dump('Hello world' is $a); | ||
| var_dump($a); | ||
|
|
||
| var_dump(new Box(42) is Box(value: $a)); | ||
| var_dump($a); | ||
|
|
||
| var_dump(new NotBox(43) is Box(value: $a)); | ||
| var_dump($a); | ||
|
|
||
| var_dump(43 is $a & int); | ||
| var_dump($a); | ||
|
|
||
| var_dump([] is $a & string); | ||
| var_dump($a); | ||
|
|
||
| var_dump(new Many() is Many(:$a, :$b, :$c, :$d)); | ||
| var_dump($a, $b, $c, $d, isset($e)); | ||
|
|
||
| var_dump(new Many() is Many(:$a, :$b, :$c, :$d, :$e, :$f, :$g, :$h, :$i, :$j)); | ||
| var_dump($a, $b, $c, $d, $e, $f, $g, $h, $i, $j); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| bool(true) | ||
| int(10) | ||
| bool(true) | ||
| string(11) "Hello world" | ||
| bool(true) | ||
| int(42) | ||
| bool(false) | ||
| int(42) | ||
| bool(true) | ||
| int(43) | ||
| bool(false) | ||
| int(43) | ||
| bool(true) | ||
| int(1) | ||
| int(2) | ||
| int(3) | ||
| int(4) | ||
| bool(false) | ||
| bool(true) | ||
| int(1) | ||
| int(2) | ||
| int(3) | ||
| int(4) | ||
| int(5) | ||
| int(6) | ||
| int(7) | ||
| int(8) | ||
| int(9) | ||
| int(10) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| --TEST-- | ||
| Object pattern matching destructor | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| class Foo { | ||
| public function __destruct() { | ||
| throw new Exception('Here'); | ||
| } | ||
| } | ||
|
|
||
| $foo = new Foo(); | ||
| $bar = 'bar'; | ||
|
|
||
| try { | ||
| 42 is $foo & $bar; | ||
iluuu1994 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch (Exception $e) { | ||
| echo $e->getMessage(), "\n"; | ||
| } | ||
|
|
||
| var_dump($foo); | ||
| var_dump($bar); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| Here | ||
| int(42) | ||
| string(3) "bar" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --TEST-- | ||
| Must not bind to variables in | pattern | ||
| --FILE-- | ||
| <?php | ||
| var_dump(42 is $foo|$bar); | ||
| ?> | ||
| --EXPECTF-- | ||
| Fatal error: Must not bind to variables inside | pattern in %s on line %d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --TEST-- | ||
| Must not bind to variables in | pattern | ||
| --FILE-- | ||
| <?php | ||
| var_dump(42 is 42|(43 & $bar)); | ||
| ?> | ||
| --EXPECTF-- | ||
| Fatal error: Must not bind to variables inside | pattern in %s on line %d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| --TEST-- | ||
| Pattern matching: Bug 001 | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| function test($a) { | ||
| $a is 42|43; | ||
| } | ||
|
|
||
| test(42); | ||
|
|
||
| ?> | ||
| ===DONE=== | ||
| --EXPECT-- | ||
| ===DONE=== |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.