-
Notifications
You must be signed in to change notification settings - Fork 7.8k
[RFC] Partial Function Application #6898
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
35 commits
Select commit
Hold shift + click to select a range
f445a7d
partial application
krakjoe f522e46
test coverage
krakjoe 638dc40
closure bind/call support
krakjoe 4c18688
fix a couple of bugs
krakjoe dcec4f9
fix another bug
krakjoe e29da6f
I need a break
krakjoe 216c101
fix an asan error, and an off by one
krakjoe 6bd2f25
a test, and fix
krakjoe 4f4e6de
fix place holder -> placeholder
krakjoe 003ecbe
fix inconsistency
krakjoe 800be73
fix magic assertion failure where result is unused
krakjoe f7863f4
collect argv
krakjoe 004166f
used/unused trampoline test
krakjoe 667d3d6
drop dup check
krakjoe 3fd01b6
fix ubsan
krakjoe 90fcab6
Update Zend/zend_language_parser.y
krakjoe 0fafba3
Update Zend/zend_compile.c
krakjoe bdf2108
drop dup check for permanent trampoline
krakjoe 89a5330
fix flags
krakjoe ead14d3
fixing things
krakjoe b9aa841
Update Zend/zend_partial.c
krakjoe a1d76e6
fix tests, fix count
krakjoe 88aa29c
comments
krakjoe 81b64ff
drop silly macro
krakjoe 67c3b66
fix another gc issue
krakjoe 3f8a12a
fix uaf in unfinished calls, fix leak args
krakjoe bde700f
put assert back
krakjoe b699d65
fix indent, repeat type
krakjoe 0e8a0a0
use hash add variants
krakjoe 711b33a
insert null in debug info
krakjoe 9400a69
drop sprintf for zend_string_init
krakjoe 583264d
fix uaf, order of destruction
krakjoe 6afa0d1
set ce ptr
krakjoe 12d6006
fix null ptr deref
krakjoe fd62047
fix wrong signature check
krakjoe 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--TEST-- | ||
Partial application compile errors: multiple ... | ||
--FILE-- | ||
<?php | ||
foo(..., ...); | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Variadic placeholder may only appear once 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,9 @@ | ||
--TEST-- | ||
Partial application compile errors: only named arguments after ... | ||
--FILE-- | ||
<?php | ||
foo(..., ?); | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Only named arguments may follow variadic placeholder 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,9 @@ | ||
--TEST-- | ||
Partial application compile errors: named arguments must come after placeholder | ||
--FILE-- | ||
<?php | ||
foo(n: 5, ?); | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Named arguments must come after all placeholders 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-- | ||
Partial application compile errors: named arguments must come after variadic placeholder | ||
--FILE-- | ||
<?php | ||
foo(n: 5, ...); | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Named arguments must come after all placeholders 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,9 @@ | ||
--TEST-- | ||
Partial application compile errors: follow variadic with un-named arg | ||
--FILE-- | ||
<?php | ||
foo(..., $a); | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Only named arguments may follow variadic placeholder 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,9 @@ | ||
--TEST-- | ||
Partial application compile errors: mix application with unpack (placeholder after) | ||
--FILE-- | ||
<?php | ||
foo(...["foo" => "bar"], ...); | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot combine partial application and unpacking %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,9 @@ | ||
--TEST-- | ||
Partial application compile errors: mix application with unpack (placeholder before) | ||
--FILE-- | ||
<?php | ||
foo(..., ...["foo" => "bar"]); | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot combine partial application and unpacking %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,38 @@ | ||
--TEST-- | ||
Partial application errors: placeholder count errors | ||
--FILE-- | ||
<?php | ||
function foo($a, $b, $c) { | ||
|
||
} | ||
|
||
try { | ||
foo(?); | ||
} catch (Error $ex) { | ||
printf("%s\n", $ex->getMessage()); | ||
} | ||
|
||
try { | ||
foo(?, ?, ?, ?); | ||
} catch (Error $ex) { | ||
printf("%s\n", $ex->getMessage()); | ||
} | ||
|
||
try { | ||
property_exists(?); | ||
} catch (Error $ex) { | ||
printf("%s\n", $ex->getMessage()); | ||
} | ||
|
||
try { | ||
usleep(?, ?); | ||
} catch (Error $ex) { | ||
printf("%s\n", $ex->getMessage()); | ||
} | ||
?> | ||
--EXPECTF-- | ||
not enough arguments or placeholders for application of foo, 1 given and exactly 3 expected, declared in %s on line 2 | ||
too many arguments or placeholders for application of foo, 4 given and a maximum of 3 expected, declared in %s on line 2 | ||
not enough arguments or placeholders for application of property_exists, 1 given and exactly 2 expected | ||
too many arguments or placeholders for application of usleep, 2 given and a maximum of 1 expected | ||
|
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-- | ||
Partial application errors: named parameter overwrites placeholder | ||
--FILE-- | ||
<?php | ||
function foo($a) { | ||
|
||
} | ||
|
||
try { | ||
foo(?, a: 1); | ||
} catch (Error $ex) { | ||
printf("%s\n", $ex->getMessage()); | ||
} | ||
?> | ||
--EXPECT-- | ||
Named parameter $a overwrites previous placeholder | ||
|
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,78 @@ | ||
--TEST-- | ||
Partial application errors: missing parameters | ||
--FILE-- | ||
<?php | ||
function foo($a, ...$b) { | ||
|
||
} | ||
|
||
function bar($a, $b, $c) {} | ||
|
||
$foo = foo(?); | ||
|
||
try { | ||
$foo(); | ||
} catch (Error $ex) { | ||
printf("%s\n", $ex->getMessage()); | ||
} | ||
|
||
$foo = foo(?, ?); | ||
|
||
try { | ||
$foo(1); | ||
} catch (Error $ex) { | ||
printf("%s\n", $ex->getMessage()); | ||
} | ||
|
||
$bar = bar(?, ?, ...); | ||
|
||
try { | ||
$bar(1); | ||
} catch (Error $ex) { | ||
printf("%s\n", $ex->getMessage()); | ||
} | ||
|
||
class Foo { | ||
public function bar($a, ...$b) {} | ||
} | ||
|
||
$foo = new Foo; | ||
|
||
$bar = $foo->bar(?); | ||
|
||
try { | ||
$bar(); | ||
} catch (Error $ex) { | ||
printf("%s\n", $ex->getMessage()); | ||
} | ||
|
||
$usleep = usleep(...); | ||
|
||
try { | ||
$usleep(); | ||
} catch (Error $ex) { | ||
printf("%s\n", $ex->getMessage()); | ||
} | ||
|
||
$usleep = usleep(?); | ||
|
||
try { | ||
$usleep(); | ||
} catch (Error $ex) { | ||
printf("%s\n", $ex->getMessage()); | ||
} | ||
|
||
try { | ||
$usleep(1, 2); | ||
} catch (Error $ex) { | ||
printf("%s\n", $ex->getMessage()); | ||
} | ||
?> | ||
--EXPECTF-- | ||
not enough arguments for application of foo, 0 given and exactly 1 expected, declared in %s on line 8 | ||
not enough arguments for application of foo, 1 given and exactly 2 expected, declared in %s on line 16 | ||
not enough arguments for application of bar, 1 given and at least 2 expected, declared in %s on line 24 | ||
not enough arguments for application of Foo::bar, 0 given and exactly 1 expected, declared in %s on line 38 | ||
not enough arguments for implementation of usleep, 0 given and exactly 1 expected | ||
not enough arguments for application of usleep, 0 given and exactly 1 expected | ||
too many arguments for application of usleep, 2 given and a maximum of 1 expected |
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-- | ||
Partial application ast export | ||
--INI-- | ||
assert.exception=1 | ||
--FILE-- | ||
<?php | ||
try { | ||
assert(0 && foo(?) && foo(...)); | ||
} catch (Error $ex) { | ||
printf("%s\n", $ex->getMessage()); | ||
} | ||
?> | ||
--EXPECT-- | ||
assert(0 && foo(?) && foo(...)) |
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-- | ||
Partial application named parameters: extra collection | ||
--FILE-- | ||
<?php | ||
function foo(...$args) { | ||
var_dump($args); | ||
} | ||
|
||
$foo = foo(..., foo: "foo"); | ||
|
||
$bar = $foo(..., bar: "bar"); | ||
|
||
$baz = $bar(..., baz: "baz"); | ||
|
||
$baz(); | ||
|
||
$foo = foo(...); | ||
|
||
$bar = $foo(..., bar: "bar"); | ||
|
||
$baz = $bar(..., baz: "baz"); | ||
|
||
$baz(); | ||
|
||
$foo = foo(..., foo: "foo"); | ||
|
||
$foo(bar: "bar"); | ||
?> | ||
--EXPECT-- | ||
array(3) { | ||
["foo"]=> | ||
string(3) "foo" | ||
["bar"]=> | ||
string(3) "bar" | ||
["baz"]=> | ||
string(3) "baz" | ||
} | ||
array(2) { | ||
["bar"]=> | ||
string(3) "bar" | ||
["baz"]=> | ||
string(3) "baz" | ||
} | ||
array(2) { | ||
["foo"]=> | ||
string(3) "foo" | ||
["bar"]=> | ||
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,24 @@ | ||
--TEST-- | ||
Partial application factory: pass | ||
--FILE-- | ||
<?php | ||
class Foo { | ||
public function __destruct() { | ||
printf("%s\n", __METHOD__); | ||
} | ||
} | ||
|
||
$foo = new Foo(...); | ||
|
||
$two = [$foo(), $foo()]; | ||
|
||
if ($two[0] === $two[1]) { | ||
var_dump($two); | ||
} else { | ||
echo "OK\n"; | ||
} | ||
?> | ||
--EXPECTF-- | ||
OK | ||
Foo::__destruct | ||
Foo::__destruct |
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-- | ||
Partial application factory: normal | ||
--FILE-- | ||
<?php | ||
class Foo { | ||
public function __construct($a) { | ||
printf("%s\n", __METHOD__); | ||
} | ||
|
||
public function __destruct() { | ||
printf("%s\n", __METHOD__); | ||
} | ||
} | ||
|
||
$foo = new Foo(...); | ||
|
||
$two = [$foo(1), $foo(1)]; | ||
|
||
if ($two[0] === $two[1]) { | ||
var_dump($two); | ||
} else { | ||
echo "OK\n"; | ||
} | ||
?> | ||
--EXPECTF-- | ||
Foo::__construct | ||
Foo::__construct | ||
OK | ||
Foo::__destruct | ||
Foo::__destruct |
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-- | ||
Partial application factory: exception | ||
--FILE-- | ||
<?php | ||
class Foo { | ||
public function __construct() { | ||
throw new Exception("boo"); | ||
} | ||
|
||
public function __destruct() { | ||
printf("%s\n", __METHOD__); | ||
} | ||
} | ||
|
||
$foo = new Foo(...); | ||
|
||
try { | ||
$foo(); | ||
} catch (Exception $ex) { | ||
printf("%s\n", $ex->getMessage()); | ||
} | ||
?> | ||
--EXPECT-- | ||
boo |
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-- | ||
Partial application factory object properties initialization | ||
--FILE-- | ||
<?php | ||
class Foo { | ||
public function __construct(public int $arg = 1) {} | ||
} | ||
|
||
$foo = new Foo(...); | ||
|
||
var_dump($foo()); | ||
?> | ||
--EXPECTF-- | ||
object(Foo)#%d (1) { | ||
["arg"]=> | ||
int(1) | ||
} | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency, this should be ArgumentCountError ( https://3v4l.org/PbJh5 )