-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Resolve relative class type at compile time #11460
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
Open
Girgias
wants to merge
24
commits into
php:master
Choose a base branch
from
Girgias:reflection-co-variance
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.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
86d8f35
Add tests for relative class types
Girgias 6006c31
Resolve relative types at compile time when possible
Girgias d18d306
Support for resolving self/parent/static in ReflectionType
Girgias f640603
Resolve trait relative class types when added to a class
Girgias 9894438
More tests
Girgias 18c6501
Only allocate new arg_infos if types are being resolved
Girgias 560b1d6
Only allocate new zend_type_list if types are being resolved
Girgias 08648dd
Address minor review comments
Girgias ebfb5af
Drop return for engine compiler error bail
Girgias c9a39aa
Change function name
Girgias 274f10b
Add eval test
Girgias 8969b8d
Add test in multiple distinct unrelated classes
Girgias 6c4f630
Do not preserve BC for self/parent and always resolve when possible
Girgias d67ba9a
Add tests and try to resolve bound closure
Girgias f40e9bf
Solve bound closure
Girgias eb3c7e8
Fix resolution for closures by propagating Closure object
Girgias 41ef059
Fix memory leak when calling constructor manually
Girgias 118ca1c
Remove self/parent as known zend_strings
Girgias c0a2da3
Address review comments
Girgias 3f275c4
Remove object copy which is not need anymore
Girgias aa79d51
Add more tests
Girgias 62095c4
Add test failing assertion about memory being freed
Girgias 5bea1b0
[skip-ci] W.I.P. changes to try and fix test
Girgias 8fc28e9
[skip-ci] More test and assertion to try to figure out wth is going on
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
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
File renamed without changes.
14 changes: 14 additions & 0 deletions
14
Zend/tests/traits/trait_parent_type_in_class_no_parent.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-- | ||
Cannot use a trait which references parent as a type in a class with no parent, single type | ||
--FILE-- | ||
<?php | ||
trait TraitExample { | ||
public function bar(): parent { return parent::class; } | ||
} | ||
|
||
class A { | ||
use TraitExample; | ||
} | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot use trait which has "parent" as a type when current class scope has no parent in %s on line %d |
14 changes: 14 additions & 0 deletions
14
Zend/tests/traits/trait_parent_type_in_class_no_parent_variation2.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-- | ||
Cannot use a trait which references parent as a type in a class with no parent, nullable type | ||
--FILE-- | ||
<?php | ||
trait TraitExample { | ||
public function bar(): ?parent { return parent::class; } | ||
} | ||
|
||
class A { | ||
use TraitExample; | ||
} | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot use trait which has "parent" as a type when current class scope has no parent in %s on line %d |
14 changes: 14 additions & 0 deletions
14
Zend/tests/traits/trait_parent_type_in_class_no_parent_variation3.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-- | ||
Cannot use a trait which references parent as a type in a class with no parent, union type | ||
--FILE-- | ||
<?php | ||
trait TraitExample { | ||
public function bar(): int|parent { return parent::class; } | ||
} | ||
|
||
class A { | ||
use TraitExample; | ||
} | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot use trait which has "parent" as a type when current class scope has no parent in %s on line %d |
14 changes: 14 additions & 0 deletions
14
Zend/tests/traits/trait_parent_type_in_class_no_parent_variation4.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-- | ||
Cannot use a trait which references parent as a type in a class with no parent, DNF type | ||
--FILE-- | ||
<?php | ||
trait TraitExample { | ||
public function bar(): (X&Y)|parent { return parent::class; } | ||
} | ||
|
||
class A { | ||
use TraitExample; | ||
} | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot use trait which has "parent" as a type when current class scope has no parent in %s on line %d |
6 changes: 2 additions & 4 deletions
6
...es/invalid_types/invalid_parent_type.phpt → ...alid_unresolved_relative_type_parent.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
4 changes: 2 additions & 2 deletions
4
...ypes/invalid_types/invalid_self_type.phpt → ...nvalid_unresolved_relative_type_self.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
19 changes: 19 additions & 0 deletions
19
Zend/tests/type_declarations/intersection_types/resolved_relative_type_valid.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,19 @@ | ||
--TEST-- | ||
parent/self type can take part in an intersection type if it is resolvable at compile time | ||
--FILE-- | ||
<?php | ||
|
||
class A {} | ||
|
||
class B extends A { | ||
public function foo(): parent&Iterator {} | ||
} | ||
|
||
class C extends A { | ||
public function foo(): self&Iterator {} | ||
} | ||
|
||
?> | ||
==DONE== | ||
--EXPECT-- | ||
==DONE== |
10 changes: 10 additions & 0 deletions
10
...pe_declarations/relative_class_types/invalid_types/invalid_parent_type_outside_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,10 @@ | ||
--TEST-- | ||
Cannot use parent type outside a class | ||
--FILE-- | ||
<?php | ||
|
||
function foo($x): parent {}; | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot use "parent" when no class scope is active in %s on line %d |
10 changes: 10 additions & 0 deletions
10
...type_declarations/relative_class_types/invalid_types/invalid_self_type_outside_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,10 @@ | ||
--TEST-- | ||
Cannot use self type outside a class | ||
--FILE-- | ||
<?php | ||
|
||
function foo($x): self {}; | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot use "self" when no class scope is active in %s on line %d |
10 changes: 10 additions & 0 deletions
10
...pe_declarations/relative_class_types/invalid_types/invalid_static_type_outside_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,10 @@ | ||
--TEST-- | ||
Cannot use static type outside a class | ||
--FILE-- | ||
<?php | ||
|
||
function foo($x): static {}; | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot use "static" when no class scope is active in %s on line %d |
13 changes: 13 additions & 0 deletions
13
Zend/tests/type_declarations/relative_class_types/relative_type_in_closures.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,13 @@ | ||
--TEST-- | ||
Relative class types can be used for closures as it may be bound to a class | ||
--FILE-- | ||
<?php | ||
|
||
$fn1 = function($x): self {}; | ||
$fn2 = function($x): parent {}; | ||
$fn3 = function($x): static {}; | ||
|
||
?> | ||
DONE | ||
--EXPECT-- | ||
DONE |
27 changes: 27 additions & 0 deletions
27
...sts/type_declarations/relative_class_types/relative_type_in_evaled_class_using_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,27 @@ | ||
--TEST-- | ||
Eval Class definition should not leak memory when using compiled traits | ||
--FILE-- | ||
<?php | ||
|
||
trait TraitCompiled { | ||
public function bar(): self { return new self; } | ||
} | ||
|
||
const EVAL_CODE = <<<'CODE' | ||
class A { | ||
use TraitCompiled; | ||
} | ||
CODE; | ||
|
||
eval(EVAL_CODE); | ||
|
||
$a1 = new A(); | ||
$a2 = $a1->bar(); | ||
var_dump($a2); | ||
|
||
?> | ||
DONE | ||
--EXPECT-- | ||
object(A)#2 (0) { | ||
} | ||
DONE |
27 changes: 27 additions & 0 deletions
27
Zend/tests/type_declarations/relative_class_types/relative_type_in_evaled_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,27 @@ | ||
--TEST-- | ||
Eval code should not leak memory when using traits | ||
--FILE-- | ||
<?php | ||
|
||
const EVAL_CODE = <<<'CODE' | ||
trait TraitEval { | ||
public function bar(): self { return new self; } | ||
} | ||
CODE; | ||
|
||
eval(EVAL_CODE); | ||
|
||
class A { | ||
use TraitEval; | ||
} | ||
|
||
$a1 = new A(); | ||
$a2 = $a1->bar(); | ||
var_dump($a2); | ||
|
||
?> | ||
DONE | ||
--EXPECT-- | ||
object(A)#2 (0) { | ||
} | ||
DONE |
15 changes: 15 additions & 0 deletions
15
...s/type_declarations/union_types/redundant_types/duplicate_relative_class_parent_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,15 @@ | ||
--TEST-- | ||
Duplicate parent type | ||
--FILE-- | ||
<?php | ||
|
||
class Foo { | ||
public function method(array $data) {} | ||
} | ||
class Bar extends Foo { | ||
public function method(array $data): parent|parent {} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Duplicate type Foo is redundant in %s on line %d | ||
12 changes: 12 additions & 0 deletions
12
...sts/type_declarations/union_types/redundant_types/duplicate_relative_class_self_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,12 @@ | ||
--TEST-- | ||
Duplicate self type | ||
--FILE-- | ||
<?php | ||
|
||
class Foo { | ||
public function method(array $data): self|self {} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Duplicate type Foo is redundant in %s on line %d |
12 changes: 12 additions & 0 deletions
12
...s/type_declarations/union_types/redundant_types/duplicate_relative_class_static_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,12 @@ | ||
--TEST-- | ||
Duplicate static type | ||
--FILE-- | ||
<?php | ||
|
||
class Foo { | ||
public function method(array $data): static|static {} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Duplicate type static is redundant in %s on line %d |
12 changes: 12 additions & 0 deletions
12
...ations/union_types/redundant_types/duplicate_resolved_relative_class_type_variation1.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-- | ||
Relative class type self resolving to an existing entry (after variation) | ||
--FILE-- | ||
<?php | ||
|
||
class Foo { | ||
public function method(array $data): Foo|self {} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Duplicate type Foo is redundant in %s on line %d |
12 changes: 12 additions & 0 deletions
12
...ations/union_types/redundant_types/duplicate_resolved_relative_class_type_variation2.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-- | ||
Relative class type self resolving to an existing entry (before variation) | ||
--FILE-- | ||
<?php | ||
|
||
class Foo { | ||
public function method(array $data): self|Foo {} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Duplicate type Foo is redundant in %s on line %d |
15 changes: 15 additions & 0 deletions
15
...ations/union_types/redundant_types/duplicate_resolved_relative_class_type_variation3.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,15 @@ | ||
--TEST-- | ||
Relative class type parent resolving to an existing entry (after variation) | ||
--FILE-- | ||
<?php | ||
|
||
class Foo { | ||
public function method(array $data) {} | ||
} | ||
class Bar extends Foo { | ||
public function method(array $data): Foo|parent {} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Duplicate type Foo is redundant in %s on line %d |
15 changes: 15 additions & 0 deletions
15
...ations/union_types/redundant_types/duplicate_resolved_relative_class_type_variation4.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,15 @@ | ||
--TEST-- | ||
Relative class type parent resolving to an existing entry (before variation) | ||
--FILE-- | ||
<?php | ||
|
||
class Foo { | ||
public function method(array $data) {} | ||
} | ||
class Bar extends Foo { | ||
public function method(array $data): parent|Foo {} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Duplicate type Foo is redundant 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.
Uh oh!
There was an error while loading. Please reload this page.