-
Notifications
You must be signed in to change notification settings - Fork 7.8k
[PoC] Limited Abstract Generics #18260
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
Girgias
wants to merge
16
commits into
php:master
Choose a base branch
from
Girgias:associated-types
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.
+906
−59
Draft
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
169108a
Zend: Use pointer to zend_type for variance checks
Girgias 6ba4d45
Add T_TYPE token
Girgias 5eae57a
Support minimal compilation of associated type
Girgias b8430af
Associate type to CE and minimal duplicate check
Girgias 10034ea
Prevent associated type from being in a union or intersection type
Girgias 604d484
Support invariant bound types for associated types
Girgias e6fe7d4
Add true global for mixed type
Girgias 50201ea
Store zend_type ptr in associated types HT
Girgias 2d80238
Add parser support for constraint type
Girgias fbd8440
Add variance checking for constrained associated type
Girgias 665fbbe
Add basic support for extending interfaces with associated type
Girgias 074a3dc
Fix namespaces interfering with AT
Girgias 1fbd7ea
Improve AT constraint indication when dumping type to string
Girgias 9d9a6e7
Fix various unhandled cases where AT is part of a union type
Girgias 9955269
[skip ci] Get explicit notation working
Girgias 4b8cb7e
Remove most of associated types
Girgias 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
31 changes: 31 additions & 0 deletions
31
Zend/tests/type_declarations/abstract_generics/abstract_generic_001.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,31 @@ | ||
--TEST-- | ||
Abstract generic types basic | ||
--FILE-- | ||
<?php | ||
|
||
interface I<T> { | ||
public function foo(T $param): T; | ||
} | ||
|
||
class CS implements I<string> { | ||
public function foo(string $param): string { | ||
return $param . '!'; | ||
} | ||
} | ||
|
||
class CI implements I<int> { | ||
public function foo(int $param): int { | ||
return $param + 42; | ||
} | ||
} | ||
|
||
$cs = new CS(); | ||
var_dump($cs->foo("Hello")); | ||
|
||
$ci = new CI(); | ||
var_dump($ci->foo(5)); | ||
|
||
?> | ||
--EXPECT-- | ||
string(6) "Hello!" | ||
int(47) |
31 changes: 31 additions & 0 deletions
31
Zend/tests/type_declarations/abstract_generics/abstract_generic_type_with_constraint.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,31 @@ | ||
--TEST-- | ||
Abstract generic type with a constraint | ||
--FILE-- | ||
<?php | ||
|
||
interface I<T : int|string> { | ||
public function foo(T $param): T; | ||
} | ||
|
||
class CS implements I<string> { | ||
public function foo(string $param): string { | ||
return $param . '!'; | ||
} | ||
} | ||
|
||
class CI implements I<int> { | ||
public function foo(int $param): int { | ||
return $param + 42; | ||
} | ||
} | ||
|
||
$cs = new CS(); | ||
var_dump($cs->foo("Hello")); | ||
|
||
$ci = new CI(); | ||
var_dump($ci->foo(5)); | ||
|
||
?> | ||
--EXPECT-- | ||
string(6) "Hello!" | ||
int(47) |
16 changes: 16 additions & 0 deletions
16
...sts/type_declarations/abstract_generics/abstract_generic_type_with_constraint_failed.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,16 @@ | ||
--TEST-- | ||
Abstract generic type with a constraint that is not satisfied | ||
--FILE-- | ||
<?php | ||
|
||
interface I<T : int|string> { | ||
public function foo(T $param): T; | ||
} | ||
|
||
class C implements I<float> { | ||
public function foo(float $param): float {} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Bound type float is not a subtype of the constraint type string|int of generic type T of interface I in %s on line %d |
100 changes: 100 additions & 0 deletions
100
Zend/tests/type_declarations/abstract_generics/big_example.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,100 @@ | ||
--TEST-- | ||
Concrete example of using AT | ||
--CREDITS-- | ||
Levi Morrison | ||
--FILE-- | ||
<?php declare(strict_types=1); | ||
|
||
namespace Sequence; | ||
|
||
// No null. This is probably going to be painful, but let's try it. | ||
interface Sequence<Item : object|array|string|float|int|bool> | ||
{ | ||
function next(): ?Item; | ||
|
||
/** | ||
* @param callable(Item, Item): Item $f | ||
* @return ?Item | ||
*/ | ||
function reduce(callable $f): ?Item; | ||
} | ||
|
||
final class StringTablePair | ||
{ | ||
public function __construct( | ||
public readonly string $string, | ||
public readonly int $id, | ||
) {} | ||
} | ||
|
||
final class StringTableSequence implements Sequence<StringTablePair> | ||
{ | ||
private array $strings; | ||
|
||
public function __construct(StringTable $string_table) { | ||
$this->strings = $string_table->to_assoc_array(); | ||
} | ||
|
||
function next(): ?StringTablePair | ||
{ | ||
$key = \array_key_first($this->strings); | ||
if (!isset($key)) { | ||
return null; | ||
} | ||
$value = \array_shift($this->strings); | ||
return new StringTablePair($key, $value); | ||
} | ||
|
||
/** | ||
* @param callable(Item, Item): Item $f | ||
* @return ?Item | ||
*/ | ||
function reduce(callable $f): ?StringTablePair | ||
{ | ||
$reduction = $this->next(); | ||
if (!isset($reduction)) { | ||
return null; | ||
} | ||
|
||
while (($next = $this->next()) !== null) { | ||
$reduction = $f($reduction, $next); | ||
} | ||
return $reduction; | ||
} | ||
} | ||
|
||
final class StringTable | ||
{ | ||
private array $strings = ["" => 0]; | ||
|
||
public function __construct() {} | ||
|
||
public function offsetGet(string $offset): int | ||
{ | ||
return $this->strings[$offset] ?? throw new \Exception(); | ||
} | ||
|
||
public function offsetExists(string $offset): bool | ||
{ | ||
return \isset($this->strings[$offset]); | ||
} | ||
|
||
public function intern(string $str): int | ||
{ | ||
return $this->strings[$str] | ||
?? ($this->strings[$str] = \count($this->strings)); | ||
} | ||
|
||
public function to_sequence(): StringTableSequence | ||
{ | ||
return new StringTableSequence($this); | ||
} | ||
|
||
public function to_assoc_array(): array { | ||
return $this->strings; | ||
} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Generic type cannot be part of a union type in %s on line %d | ||
12 changes: 12 additions & 0 deletions
12
...pe_declarations/abstract_generics/errors/abstract_generic_cannot_be_in_intersection1.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,12 @@ | ||
--TEST-- | ||
Abstract generic type cannot be in intersection (simple intersection with class type) | ||
--FILE-- | ||
<?php | ||
|
||
interface I<T> { | ||
public function foo(T&Traversable $param): T&Traversable; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Generic type cannot be part of an intersection type in %s on line %d |
12 changes: 12 additions & 0 deletions
12
...pe_declarations/abstract_generics/errors/abstract_generic_cannot_be_in_intersection2.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,12 @@ | ||
--TEST-- | ||
Abstract generic type cannot be in intersection (DNF type) | ||
--FILE-- | ||
<?php | ||
|
||
interface I<T> { | ||
public function foo(stdClass|(T&Traversable) $param): stdClass|(T&Traversable); | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Generic type cannot be part of an intersection type in %s on line %d |
12 changes: 12 additions & 0 deletions
12
...ests/type_declarations/abstract_generics/errors/abstract_generic_cannot_be_in_union1.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,12 @@ | ||
--TEST-- | ||
Abstract generic type cannot be in union (simple union with built-in type) | ||
--FILE-- | ||
<?php | ||
|
||
interface I<T> { | ||
public function foo(T|int $param): T|int; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Generic type cannot be part of a union type in %s on line %d |
12 changes: 12 additions & 0 deletions
12
...ests/type_declarations/abstract_generics/errors/abstract_generic_cannot_be_in_union2.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,12 @@ | ||
--TEST-- | ||
Abstract generic type cannot be in union (simple union with class type) | ||
--FILE-- | ||
<?php | ||
|
||
interface I<T> { | ||
public function foo(T|stdClass $param): T|stdClass; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Generic type cannot be part of a union type in %s on line %d |
12 changes: 12 additions & 0 deletions
12
...ests/type_declarations/abstract_generics/errors/abstract_generic_cannot_be_in_union3.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,12 @@ | ||
--TEST-- | ||
Abstract generic type cannot be in union (DNF type) | ||
--FILE-- | ||
<?php | ||
|
||
interface I<T> { | ||
public function foo(T|(Traversable&Countable) $param): T|(Traversable&Countable); | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Generic type cannot be part of a union type in %s on line %d |
12 changes: 12 additions & 0 deletions
12
...ests/type_declarations/abstract_generics/errors/abstract_generic_cannot_be_in_union4.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,12 @@ | ||
--TEST-- | ||
Abstract generic type cannot be in union (nullable type union with ?) | ||
--FILE-- | ||
<?php | ||
|
||
interface I<T> { | ||
public function foo(?T $param): ?T; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Generic type cannot be part of a union type in %s on line %d |
12 changes: 12 additions & 0 deletions
12
...ests/type_declarations/abstract_generics/errors/abstract_generic_cannot_be_in_union5.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,12 @@ | ||
--TEST-- | ||
Abstract generic type cannot be in union (forced allowed null) | ||
--FILE-- | ||
<?php | ||
|
||
interface I<T> { | ||
public function foo(T $param = null): T; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Generic type cannot be part of a union type (implicitly nullable due to default null value) in %s on line %d |
12 changes: 12 additions & 0 deletions
12
Zend/tests/type_declarations/abstract_generics/errors/abstract_generic_in_class.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,12 @@ | ||
--TEST-- | ||
Abstract generic type in class is invalid | ||
--FILE-- | ||
<?php | ||
|
||
class C<T> { | ||
public function foo(T $param): T; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Parse error: syntax error, unexpected token "<", expecting "{" in %s on line %d |
12 changes: 12 additions & 0 deletions
12
Zend/tests/type_declarations/abstract_generics/errors/abstract_generic_in_trait.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,12 @@ | ||
--TEST-- | ||
Abstract generic type in trait is invalid | ||
--FILE-- | ||
<?php | ||
|
||
trait C<T> { | ||
public function foo(T $param): T; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Parse error: syntax error, unexpected token "<", expecting "{" in %s on line %d |
12 changes: 12 additions & 0 deletions
12
Zend/tests/type_declarations/abstract_generics/errors/abstract_generic_redeclared.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,12 @@ | ||
--TEST-- | ||
Abstract generic type that is redeclared | ||
--FILE-- | ||
<?php | ||
|
||
interface I<T, S, T> { | ||
public function foo(T&Traversable $param): T&Traversable; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Duplicate generic parameter T in %s on line %d |
16 changes: 16 additions & 0 deletions
16
Zend/tests/type_declarations/abstract_generics/errors/no_bound_abstract_generic_type.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,16 @@ | ||
--TEST-- | ||
Implementing class does not bind any abstract generic type | ||
--FILE-- | ||
<?php | ||
|
||
interface I<T> { | ||
public function foo(T $param): T; | ||
} | ||
|
||
class C implements I { | ||
public function foo(float $param): float {} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot implement I as it has generic parameters which are not specified in %s on line %d |
16 changes: 16 additions & 0 deletions
16
...declarations/abstract_generics/errors/no_bound_abstract_generic_type_with_constraint.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,16 @@ | ||
--TEST-- | ||
Implementing class does not bind any abstract generic type | ||
--FILE-- | ||
<?php | ||
|
||
interface I<T : int|string> { | ||
public function foo(T $param): T; | ||
} | ||
|
||
class C implements I { | ||
public function foo(float $param): float {} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot implement I as it has generic parameters which are not specified in %s on line %d |
20 changes: 20 additions & 0 deletions
20
...tions/abstract_generics/errors/no_bound_abstract_generic_type_with_prior_bound_types.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,20 @@ | ||
--TEST-- | ||
Implementing class does not bind any abstract generic type | ||
--FILE-- | ||
<?php | ||
|
||
interface I1<T> { | ||
public function foo(T $param): T; | ||
} | ||
interface I2<T> { | ||
public function bar(T $param): T; | ||
} | ||
|
||
class C implements I1<float>, I2 { | ||
public function foo(float $param): float {} | ||
public function bar(string $param): string {} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot implement I2 as it has generic parameters which are not specified in %s on line %d |
21 changes: 21 additions & 0 deletions
21
.../tests/type_declarations/abstract_generics/extended_interface_abstract_generic_types.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,21 @@ | ||
--TEST-- | ||
Abstract generic type behaviour in extended interface | ||
--FILE-- | ||
<?php | ||
|
||
interface I<T : int|string|(Traversable&Countable)> { | ||
public function foo(T $param): T; | ||
} | ||
|
||
interface I2<T> extends I<T> { | ||
public function bar(int $o, T $param): T; | ||
} | ||
|
||
class C implements I2<string> { | ||
public function foo(string $param): string {} | ||
public function bar(int $o, float $param): float {} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Constraint type mixed of generic type T of interface I2 is not a subtype of the constraint type (Traversable&Countable)|string|int of generic type T of interface I in %s on line %d |
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.
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.
When I saw my example in here, I got excited that you added basic union support. Nope! 😆
One day 🙏🏻