-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Implement basic variance support #4185
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
11 commits
Select commit
Hold shift + click to select a range
8242953
Avoid early-binding on unresolved types
nikic 7e40812
Implement variance support
nikic 81b7229
Don't use instanceof_function directly
nikic 5f5cd6b
Fix handling of linking in progress state during variance checks
nikic 2ef3977
Adopt some tests from variance-7.4 branch
nikic 750bbe7
Add class order test cases
nikic 4002ffb
Require types to be available for preloading
nikic 3a9af21
Also check LINKING_IN_PROGRESS when resolving "parent"
nikic 49a0cca
Specify whether inheritance error is caused by failure to load class
nikic b3f5d0a
Adjust error message
nikic 78fead7
Add comments
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?php | ||
|
||
class Foo {} | ||
class_alias('Foo', '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,16 @@ | ||
--TEST-- | ||
Bug #76451: Aliases during inheritance type checks affected by opcache | ||
--FILE-- | ||
<?php | ||
require __DIR__ . "/bug76451.inc"; | ||
|
||
class A { | ||
public function test(Foo $foo) {} | ||
} | ||
class B extends A { | ||
public function test(Bar $foo) {} | ||
} | ||
?> | ||
===DONE=== | ||
--EXPECT-- | ||
===DONE=== |
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 @@ | ||
<?php | ||
class A { | ||
public function test(Foo $foo) {} | ||
} | ||
class B extends A { | ||
public function test(Bar $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,12 @@ | ||
--TEST-- | ||
Bug #76451: Aliases during inheritance type checks affected by opcache (variation) | ||
--FILE-- | ||
<?php | ||
class Foo {} | ||
class_alias('Foo', 'Bar'); | ||
|
||
require __DIR__ . '/bug76451_2.inc'; | ||
?> | ||
===DONE=== | ||
--EXPECT-- | ||
===DONE=== |
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
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-- | ||
Returns are covariant, but we don't allow the code due to class ordering | ||
--FILE-- | ||
<?php | ||
|
||
class A { | ||
public function method() : B {} | ||
} | ||
class B extends A { | ||
public function method() : C {} | ||
} | ||
class C extends B { | ||
} | ||
|
||
new C; | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Could not check compatibility between B::method(): C and A::method(): B, because class C is not available in %s on line %d |
25 changes: 25 additions & 0 deletions
25
Zend/tests/type_declarations/variance/class_order_autoload.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,25 @@ | ||
--TEST-- | ||
Returns are covariant, but we don't allow the code due to class ordering (autoload variation) | ||
--FILE-- | ||
<?php | ||
|
||
spl_autoload_register(function($class) { | ||
if ($class === 'A') { | ||
class A { | ||
public function method() : B {} | ||
} | ||
} else if ($class == 'B') { | ||
class B extends A { | ||
public function method() : C {} | ||
} | ||
} else { | ||
class C extends B { | ||
} | ||
} | ||
}); | ||
|
||
$c = new C; | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Could not check compatibility between B::method(): C and A::method(): B, because class C is not available in %s on line %d |
26 changes: 26 additions & 0 deletions
26
Zend/tests/type_declarations/variance/enum_forward_compat.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,26 @@ | ||
--TEST-- | ||
Forward compatibility with types that look like classes but aren't | ||
--FILE-- | ||
<?php | ||
|
||
spl_autoload_register(function($class) { | ||
var_dump($class); | ||
if ($class === 'X') { | ||
class X {} | ||
} else { | ||
class Y {} | ||
} | ||
}); | ||
|
||
class A { | ||
public function method(X $param) : object {} | ||
} | ||
|
||
class B extends A { | ||
public function method(object $param) : Y {} | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
string(1) "X" | ||
string(1) "Y" |
22 changes: 22 additions & 0 deletions
22
Zend/tests/type_declarations/variance/object_variance.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-- | ||
Testing object's variance in inheritance | ||
--FILE-- | ||
<?php | ||
|
||
interface I1 { | ||
function method1(I1 $o): object; | ||
} | ||
interface I2 extends I1 { | ||
function method1(object $o): I1; | ||
} | ||
final class C1 implements I2 { | ||
function method1($o = null): self { | ||
return $this; | ||
} | ||
} | ||
|
||
$o = new C1(); | ||
echo get_class($o->method1()); | ||
?> | ||
--EXPECT-- | ||
C1 |
45 changes: 45 additions & 0 deletions
45
Zend/tests/type_declarations/variance/parent_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,45 @@ | ||
--TEST-- | ||
Use of parent inside a class that has / has no parent | ||
--FILE-- | ||
<?php | ||
|
||
// Illegal: A::parent is ill-defined | ||
class A { | ||
public function method(parent $x) {} | ||
} | ||
class B extends A { | ||
public function method(parent $x) {} | ||
} | ||
|
||
// Legal: A2::parent == P2 | ||
class P2 {} | ||
class A2 extends P2 { | ||
public function method(parent $x) {} | ||
} | ||
class B2 extends A2 { | ||
public function method(P2 $x) {} | ||
} | ||
|
||
// Legal: B3::parent == A3 is subclass of A3::parent == P3 in covariant position | ||
class P3 {} | ||
class A3 extends P3 { | ||
public function method($x): parent {} | ||
} | ||
class B3 extends A3 { | ||
public function method($x): parent {} | ||
} | ||
|
||
// Illegal: B4::parent == A4 is subclass of A4::parent == P4 in contravariant position | ||
class P4 {} | ||
class A4 extends P4 { | ||
public function method(parent $x) {} | ||
} | ||
class B4 extends A4 { | ||
public function method(parent $x) {} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Warning: Declaration of B4::method(A4 $x) should be compatible with A4::method(P4 $x) in %s on line %d | ||
|
||
Warning: Could not check compatibility between B::method(A $x) and A::method(parent $x), because class parent is not available 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 |
---|---|---|
|
@@ -1131,14 +1131,16 @@ ZEND_API void zend_do_delayed_early_binding(const zend_op_array *op_array, uint3 | |
if (first_early_binding_opline != (uint32_t)-1) { | ||
zend_bool orig_in_compilation = CG(in_compilation); | ||
uint32_t opline_num = first_early_binding_opline; | ||
zend_class_entry *ce; | ||
|
||
CG(in_compilation) = 1; | ||
while (opline_num != (uint32_t)-1) { | ||
const zend_op *opline = &op_array->opcodes[opline_num]; | ||
zval *lcname = RT_CONSTANT(opline, opline->op1); | ||
zval *parent_name = RT_CONSTANT(opline, opline->op2); | ||
if ((ce = zend_lookup_class_ex(Z_STR_P(parent_name), Z_STR_P(parent_name + 1), 0)) != NULL) { | ||
do_bind_class(RT_CONSTANT(&op_array->opcodes[opline_num], op_array->opcodes[opline_num].op1), ce); | ||
zend_class_entry *ce = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname + 1)); | ||
zend_class_entry *parent_ce = zend_lookup_class_ex(Z_STR_P(parent_name), Z_STR_P(parent_name + 1), 0); | ||
if (ce && parent_ce && zend_can_early_bind(ce, parent_ce)) { | ||
do_bind_class(lcname, parent_ce); | ||
} | ||
opline_num = op_array->opcodes[opline_num].result.opline_num; | ||
} | ||
|
@@ -6392,6 +6394,7 @@ zend_op *zend_compile_class_decl(zend_ast *ast, zend_bool toplevel) /* {{{ */ | |
&& !(CG(compiler_options) & ZEND_COMPILE_PRELOAD) /* delay inheritance till preloading */ | ||
&& ((parent_ce->type != ZEND_INTERNAL_CLASS) || !(CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_CLASSES)) | ||
&& ((parent_ce->type != ZEND_USER_CLASS) || !(CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES) || (parent_ce->info.user.filename == ce->info.user.filename)) | ||
&& zend_can_early_bind(ce, parent_ce) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should try to reduce overhead of zend_can_early_bind(), but we can do it later. |
||
) { | ||
if (EXPECTED(zend_hash_add_ptr(CG(class_table), lcname, ce) != NULL)) { | ||
CG(zend_lineno) = decl->end_lineno; | ||
|
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.
Why is this allowed at all?
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.
I had originally added a compile-time check for this in 7.4, but reverted it again in deb44d4 because it broke Mockery, which is used by Laravel. Maybe we can do this for PHP 8.
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.
might be a good idea to add this into deprecations for PHP-7.4