-
Notifications
You must be signed in to change notification settings - Fork 7.9k
first class callable conversion #7019
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
Closed
Closed
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
b335041
first class callable conversion
krakjoe 3116072
tests
krakjoe 211f869
ws
krakjoe 02ec61c
preserve called scope
krakjoe 92e240f
fix test as suggested, for real this time
krakjoe ea39a46
test and fix
krakjoe 6a771bb
handle attributes nicely
krakjoe af43182
Forbid callable syntax + nullsafe
nikic b004008
Test strict types behavior
nikic 4dd984c
fix call graph
nikic 6c036bd
Fix indent
nikic d7488c3
Fix assertion handling
nikic 0c433d3
Add additional test coverage
nikic 180125d
Fix optimizer support for first-class callables
nikic 7ea5139
Some JIT handling
nikic f42744e
Bail out from tracing JIT
nikic 4370252
Remove assert
nikic f3af1d4
Add test I forgot to git add
nikic 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--TEST-- | ||
First Class Callable from Internal | ||
--FILE-- | ||
<?php | ||
$sprintf = sprintf(...); | ||
|
||
echo $sprintf("Hello %s", "World"); | ||
?> | ||
--EXPECT-- | ||
Hello World |
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,14 @@ | ||
--TEST-- | ||
First Class Callable from User | ||
--FILE-- | ||
<?php | ||
function foo() { | ||
return "OK"; | ||
} | ||
|
||
$foo = foo(...); | ||
|
||
echo $foo(); | ||
?> | ||
--EXPECT-- | ||
OK |
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,17 @@ | ||
--TEST-- | ||
First Class Callable from Method | ||
--FILE-- | ||
<?php | ||
class Foo { | ||
public function bar() { | ||
echo "OK"; | ||
} | ||
} | ||
|
||
$foo = new Foo; | ||
$bar = $foo->bar(...); | ||
|
||
echo $bar(); | ||
?> | ||
--EXPECT-- | ||
OK |
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,21 @@ | ||
--TEST-- | ||
First Class Callable from Private Scope | ||
--FILE-- | ||
<?php | ||
class Foo { | ||
private function method() { | ||
return __METHOD__; | ||
} | ||
|
||
public function bar() { | ||
return $this->method(...); | ||
} | ||
} | ||
|
||
$foo = new Foo; | ||
$bar = $foo->bar(...); | ||
|
||
echo ($bar())(); | ||
?> | ||
--EXPECT-- | ||
Foo::method |
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,26 @@ | ||
--TEST-- | ||
First Class Callable from Magic | ||
--FILE-- | ||
<?php | ||
class Foo { | ||
public function __call($method, $args) { | ||
return $method; | ||
} | ||
|
||
public static function __callStatic($method, $args) { | ||
return $method; | ||
} | ||
} | ||
|
||
$foo = new Foo; | ||
$bar = $foo->anythingInstance(...); | ||
|
||
echo $bar() . PHP_EOL; | ||
|
||
$qux = Foo::anythingStatic(...); | ||
|
||
echo $qux(); | ||
?> | ||
--EXPECT-- | ||
anythingInstance | ||
anythingStatic |
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,13 @@ | ||
--TEST-- | ||
First Class Callable from Closure | ||
--FILE-- | ||
<?php | ||
$foo = function(){}; | ||
$bar = $foo(...); | ||
|
||
if ($foo === $bar) { | ||
echo "OK"; | ||
} | ||
?> | ||
--EXPECT-- | ||
OK |
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,12 @@ | ||
--TEST-- | ||
First Class Callable from Special Compiler Function | ||
--FILE-- | ||
<?php | ||
$strlen = strlen(...); | ||
|
||
if ($strlen("Hello World") === 11) { | ||
echo "OK"; | ||
} | ||
?> | ||
--EXPECT-- | ||
OK |
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,12 @@ | ||
--TEST-- | ||
First Class Callable from NEW | ||
--FILE-- | ||
<?php | ||
class Foo { | ||
|
||
} | ||
|
||
new Foo(...); | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot create Closure for new expression in %s on line 6 |
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,14 @@ | ||
--TEST-- | ||
First Class Callable from Closure::__invoke | ||
--FILE-- | ||
<?php | ||
$closure = function() { | ||
return "OK"; | ||
}; | ||
|
||
$foo = $closure->__invoke(...); | ||
|
||
echo $foo(); | ||
?> | ||
--EXPECT-- | ||
OK |
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,18 @@ | ||
--TEST-- | ||
First Class Callable preserve Called Scope | ||
--FILE-- | ||
<?php | ||
class Foo { | ||
public static function method() { | ||
return static::class; | ||
} | ||
} | ||
|
||
class Bar extends Foo {} | ||
|
||
$bar = Bar::method(...); | ||
|
||
echo $bar(); | ||
?> | ||
--EXPECT-- | ||
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,11 @@ | ||
--TEST-- | ||
First Class Callable Attribute Error | ||
--FILE-- | ||
<?php | ||
#[Attribute(...)] | ||
class Foo { | ||
|
||
} | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot create Closure as attribute argument in %s on line 3 |
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-- | ||
First class callable with nullsafe method call | ||
--FILE-- | ||
<?php | ||
|
||
$foo?->bar(...); | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot combine nullsafe operator with Closure creation 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,10 @@ | ||
--TEST-- | ||
First class callable with nullsafe method call (nested) | ||
--FILE-- | ||
<?php | ||
|
||
$foo?->foo->bar(...); | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot combine nullsafe operator with Closure creation 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,11 @@ | ||
--TEST-- | ||
First class callable with nullsafe method call (unrelated) | ||
--FILE-- | ||
<?php | ||
|
||
$foo = null; | ||
var_dump($foo?->foo($foo->bar(...))); | ||
|
||
?> | ||
--EXPECT-- | ||
NULL |
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,24 @@ | ||
--TEST-- | ||
First class callables and strict types | ||
--FILE-- | ||
<?php | ||
declare(strict_types=1); | ||
|
||
function test(int $i) { | ||
var_dump($i); | ||
} | ||
|
||
require __DIR__ . '/first_class_callable_015_weak.inc'; | ||
require __DIR__ . '/first_class_callable_015_strict.inc'; | ||
$fn = test(...); | ||
do_weak_call($fn); | ||
try { | ||
do_strict_call($fn); | ||
} catch (Error $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
int(42) | ||
test(): Argument #1 ($i) must be of type int, string given, called 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,5 @@ | ||
<?php declare(strict_types=1); | ||
|
||
function do_strict_call(Closure $fn) { | ||
$fn("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,5 @@ | ||
<?php | ||
|
||
function do_weak_call(Closure $fn) { | ||
$fn("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,25 @@ | ||
--TEST-- | ||
Acquire callable to assert() | ||
--FILE-- | ||
<?php | ||
|
||
namespace Foo; | ||
|
||
$assert = assert(...); | ||
$assert(1 == 1.0, "Message 1"); | ||
try { | ||
$assert(1 == 2.0, "Message 2"); | ||
} catch (\AssertionError $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
try { | ||
assert(false && strlen(...)); | ||
} catch (\AssertionError $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
Message 2 | ||
assert(false && strlen(...)) |
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,53 @@ | ||
--TEST-- | ||
Acquire callable through various dynamic constructs | ||
--FILE-- | ||
<?php | ||
|
||
class A { | ||
public static function b($x) { | ||
return $x; | ||
} | ||
|
||
public function c($x) { | ||
return $x; | ||
} | ||
} | ||
|
||
$name = 'strlen'; | ||
$fn = $name(...); | ||
var_dump($fn('x')); | ||
|
||
$name = ['A', 'b']; | ||
$fn = $name(...); | ||
var_dump($fn(2)); | ||
|
||
$name = [new A, 'c']; | ||
$fn = $name(...); | ||
var_dump($fn(3)); | ||
|
||
$name1 = 'A'; | ||
$name2 = 'b'; | ||
$fn = $name1::$name2(...); | ||
var_dump($fn(4)); | ||
|
||
$name2 = 'c'; | ||
$fn = (new A)->$name2(...); | ||
var_dump($fn(5)); | ||
|
||
$fn = [A::class, 'b'](...); | ||
var_dump($fn(6)); | ||
|
||
$o = new stdClass; | ||
$o->prop = A::b(...); | ||
($o->prop)(7); | ||
|
||
$nam | ||
|
||
?> | ||
--EXPECT-- | ||
int(1) | ||
int(2) | ||
int(3) | ||
int(4) | ||
int(5) | ||
int(6) |
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.