-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Named params implementation #5357
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
31 commits
Select commit
Hold shift + click to select a range
424f0b6
Partial named params implementation
nikic 4afe5b4
Check for duplicate parameter names in internal functions
nikic a8e7462
Add AST export support
nikic 8b14583
Fix test name
nikic 75964c0
Add tests for Attribut(flags: )
nikic e550f4a
Only check for undef in RECV(_INIT)
nikic d346bbd
Check for undef in recv jit
nikic 8bdd1a9
Add flag to distinguish named fcalls
nikic 883c0a5
SEND may throw for named params
nikic 3683f0e
For unpacks, we should also assume there may be named args
nikic 368500b
Free extra named args in jit
nikic d0492b1
Initialize icall undef args in jit
nikic bb79eed
Always emit type check in RECV_INIT
nikic 268b69c
Fix attribtue tests for new syntax
nikic fc37bc0
WIP
nikic 5fddc6f
Fixes
nikic 06a4ab7
Cleanup
nikic a46694c
jit
nikic 1aec287
Fix windows?
nikic a164e5d
Rename opcode to be more precise
nikic 90dac08
Make flag meaning more precise
nikic a9a0252
Make compiler code a bit smarter
nikic 2700037
Move function back to where it was
nikic 0f9131e
Rename function to match opcode name
nikic 141ded0
jit cleanup
nikic 077886d
Support extra named params in backtraces
nikic 3e45549
Prepare for extra named params reuse
nikic e77c2b6
Reuse extra_named_params if possible
nikic e31838a
Combine check for extra named params and allocated call
nikic ed108d4
Drop one of the free extra named params helpers
nikic a6b566d
Revert an incorrect change
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
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,37 @@ | ||
--TEST-- | ||
Check that __call() and __callStatic() work with named parameters | ||
--FILE-- | ||
<?php | ||
|
||
class Test { | ||
public function __call(string $method, array $args) { | ||
$this->{'_'.$method}(...$args); | ||
} | ||
|
||
public static function __callStatic(string $method, array $args) { | ||
(new static)->{'_'.$method}(...$args); | ||
} | ||
|
||
private function _method($a = 'a', $b = 'b') { | ||
echo "a: $a, b: $b\n"; | ||
} | ||
} | ||
|
||
$obj = new class { public function __toString() { return "STR"; } }; | ||
|
||
$test = new Test; | ||
$test->method(a: 'A', b: 'B'); | ||
$test->method(b: 'B'); | ||
$test->method(b: $obj); | ||
Test::method(a: 'A', b: 'B'); | ||
Test::method(b: 'B'); | ||
Test::method(b: $obj); | ||
|
||
?> | ||
--EXPECT-- | ||
a: A, b: B | ||
a: a, b: B | ||
a: a, b: STR | ||
a: A, b: B | ||
a: a, b: B | ||
a: a, b: STR |
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,64 @@ | ||
--TEST-- | ||
Check that __invoke() works with named parameters | ||
--FILE-- | ||
<?php | ||
|
||
class Test { | ||
public function __invoke($a = 'a', $b = 'b') { | ||
echo "a: $a, b: $b\n"; | ||
} | ||
} | ||
|
||
class Test2 { | ||
public function __invoke($a = 'a', $b = 'b', ...$rest) { | ||
echo "a: $a, b: $b\n"; | ||
var_dump($rest); | ||
} | ||
} | ||
|
||
$test = new Test; | ||
$test(b: 'B', a: 'A'); | ||
$test(b: 'B'); | ||
try { | ||
$test(b: 'B', c: 'C'); | ||
} catch (Error $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
echo "\n"; | ||
|
||
$test2 = new Test2; | ||
$test2(b: 'B', a: 'A', c: 'C'); | ||
$test2(b: 'B', c: 'C'); | ||
echo "\n"; | ||
|
||
$test3 = function($a = 'a', $b = 'b') { | ||
echo "a: $a, b: $b\n"; | ||
}; | ||
$test3(b: 'B', a: 'A'); | ||
$test3(b: 'B'); | ||
try { | ||
$test3(b: 'B', c: 'C'); | ||
} catch (Error $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
a: A, b: B | ||
a: a, b: B | ||
Unknown named parameter $c | ||
|
||
a: A, b: B | ||
array(1) { | ||
["c"]=> | ||
string(1) "C" | ||
} | ||
a: a, b: B | ||
array(1) { | ||
["c"]=> | ||
string(1) "C" | ||
} | ||
|
||
a: A, b: B | ||
a: a, b: B | ||
Unknown named parameter $c |
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,50 @@ | ||
--TEST-- | ||
Named params in attributes | ||
--FILE-- | ||
<?php | ||
|
||
@@Attribute | ||
class MyAttribute { | ||
public function __construct( | ||
public $a = 'a', | ||
public $b = 'b', | ||
public $c = 'c', | ||
) {} | ||
} | ||
|
||
@@MyAttribute('A', c: 'C') | ||
class Test1 {} | ||
|
||
@@MyAttribute('A', a: 'C') | ||
class Test2 {} | ||
|
||
$attr = (new ReflectionClass(Test1::class))->getAttributes()[0]; | ||
var_dump($attr->getName()); | ||
var_dump($attr->getArguments()); | ||
var_dump($attr->newInstance()); | ||
|
||
$attr = (new ReflectionClass(Test2::class))->getAttributes()[0]; | ||
try { | ||
var_dump($attr->newInstance()); | ||
} catch (Error $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
string(11) "MyAttribute" | ||
array(2) { | ||
[0]=> | ||
string(1) "A" | ||
["c"]=> | ||
string(1) "C" | ||
} | ||
object(MyAttribute)#1 (3) { | ||
["a"]=> | ||
string(1) "A" | ||
["b"]=> | ||
string(1) "b" | ||
["c"]=> | ||
string(1) "C" | ||
} | ||
Named parameter $a overwrites previous argument |
11 changes: 11 additions & 0 deletions
11
Zend/tests/named_params/attributes_duplicate_named_param.phpt
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-- | ||
Named params in attributes: Duplicate named parameter error | ||
--FILE-- | ||
<?php | ||
|
||
@@MyAttribute(a: 'A', a: 'A') | ||
class Test {} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Duplicate named parameter $a 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,21 @@ | ||
--TEST-- | ||
Named flags parameter for Attribute attribute | ||
--FILE-- | ||
<?php | ||
|
||
@@Attribute(flags: Attribute::TARGET_CLASS) | ||
class MyAttribute { | ||
} | ||
|
||
@@MyAttribute | ||
function test() {} | ||
|
||
(new ReflectionFunction('test'))->getAttributes()[0]->newInstance(); | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Uncaught Error: Attribute "MyAttribute" cannot target function (allowed targets: class) in %s:%d | ||
Stack trace: | ||
#0 %s(%d): ReflectionAttribute->newInstance() | ||
#1 {main} | ||
thrown in %s on line %d |
22 changes: 22 additions & 0 deletions
22
Zend/tests/named_params/attributes_named_flags_incorrect.phpt
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,22 @@ | ||
--TEST-- | ||
Named flags parameter for Attribute attribute (incorrect parameter name) | ||
--FILE-- | ||
<?php | ||
|
||
// TODO: This should error at compile-time. | ||
@@Attribute(not_flags: Attribute::TARGET_CLASS) | ||
class MyAttribute { | ||
} | ||
|
||
@@MyAttribute | ||
function test() {} | ||
|
||
(new ReflectionFunction('test'))->getAttributes()[0]->newInstance(); | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Uncaught Error: Attribute "MyAttribute" cannot target function (allowed targets: class) in %s:%d | ||
Stack trace: | ||
#0 %s(%d): ReflectionAttribute->newInstance() | ||
#1 {main} | ||
thrown in %s on line %d |
14 changes: 14 additions & 0 deletions
14
Zend/tests/named_params/attributes_positional_after_named.phpt
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-- | ||
Named params in attributes: Positional after named error | ||
--FILE-- | ||
<?php | ||
|
||
@@Attribute | ||
class MyAttribute { } | ||
|
||
@@MyAttribute(a: 'A', 'B') | ||
class Test {} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot use positional argument after named argument 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,69 @@ | ||
--TEST-- | ||
Extra named params in backtraces | ||
--FILE-- | ||
<?php | ||
|
||
function test($a, ...$rest) { | ||
var_dump(debug_backtrace()); | ||
debug_print_backtrace(); | ||
throw new Exception("Test"); | ||
} | ||
|
||
try { | ||
test(1, 2, x: 3, y: 4); | ||
} catch (Exception $e) { | ||
var_dump($e->getTrace()); | ||
echo $e, "\n"; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
array(1) { | ||
[0]=> | ||
array(4) { | ||
["file"]=> | ||
string(%d) "%s" | ||
["line"]=> | ||
int(10) | ||
["function"]=> | ||
string(4) "test" | ||
["args"]=> | ||
array(4) { | ||
[0]=> | ||
int(1) | ||
[1]=> | ||
int(2) | ||
["x"]=> | ||
int(3) | ||
["y"]=> | ||
int(4) | ||
} | ||
} | ||
} | ||
#0 test(1, 2, x: 3, y: 4) called at [%s:10] | ||
array(1) { | ||
[0]=> | ||
array(4) { | ||
["file"]=> | ||
string(%d) "%s" | ||
["line"]=> | ||
int(10) | ||
["function"]=> | ||
string(4) "test" | ||
["args"]=> | ||
array(4) { | ||
[0]=> | ||
int(1) | ||
[1]=> | ||
int(2) | ||
["x"]=> | ||
int(3) | ||
["y"]=> | ||
int(4) | ||
} | ||
} | ||
} | ||
Exception: Test in %s:%d | ||
Stack trace: | ||
#0 %s(%d): test(1, 2, x: 3, y: 4) | ||
#1 {main} |
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.