-
Notifications
You must be signed in to change notification settings - Fork 8k
Make intersection types nullable #7259
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
+232
−30
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
10 changes: 6 additions & 4 deletions
10
Zend/tests/type_declarations/intersection_types/implicit_nullable_intersection_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 |
---|---|---|
@@ -1,10 +1,12 @@ | ||
--TEST-- | ||
Intersection types cannot be implicitly nullable | ||
Intersection types can be implicitly nullable as the others | ||
--FILE-- | ||
<?php | ||
|
||
function foo(X&Y $foo = null) {} | ||
function foo(X&Y $foo = null) { return $foo; } | ||
|
||
var_dump(foo()); | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot use null as default value for parameter $foo of type X&Y in %s on line %d | ||
--EXPECT-- | ||
NULL |
10 changes: 0 additions & 10 deletions
10
Zend/tests/type_declarations/intersection_types/invalid_types/invalid_nullable_type.phpt
This file was deleted.
Oops, something went wrong.
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
31 changes: 31 additions & 0 deletions
31
Zend/tests/type_declarations/intersection_types/typed_reference2.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-- | ||
Intersection types and typed reference | ||
--FILE-- | ||
<?php | ||
|
||
interface X {} | ||
interface Y {} | ||
interface Z {} | ||
|
||
class A implements X, Y, Z {} | ||
class B implements X, Y {} | ||
|
||
class Test { | ||
public ?X&Y $y; | ||
public X&Z $z; | ||
} | ||
$test = new Test; | ||
$r = new A; | ||
$test->y =& $r; | ||
$test->z =& $r; | ||
|
||
|
||
try { | ||
$r = null; | ||
} catch (\TypeError $e) { | ||
echo $e->getMessage(), \PHP_EOL; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
Cannot assign null to reference held by property Test::$z of type X&Z |
27 changes: 27 additions & 0 deletions
27
Zend/tests/type_declarations/intersection_types/typed_reference3.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-- | ||
Intersection types and typed reference | ||
--FILE-- | ||
<?php | ||
|
||
interface X {} | ||
interface Y {} | ||
interface Z {} | ||
|
||
class A implements X, Y, Z {} | ||
class B implements X, Y {} | ||
|
||
class Test { | ||
public ?X&Y $y; | ||
public ?X&Z $z; | ||
} | ||
$test = new Test; | ||
$r = new A; | ||
$test->y =& $r; | ||
$test->z =& $r; | ||
|
||
$r = null; | ||
|
||
?> | ||
==DONE== | ||
--EXPECT-- | ||
==DONE== |
32 changes: 32 additions & 0 deletions
32
Zend/tests/type_declarations/intersection_types/variance/invalid7.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,32 @@ | ||
--TEST-- | ||
Invalid nullable widening | ||
--FILE-- | ||
<?php | ||
|
||
interface A {} | ||
interface B {} | ||
interface C {} | ||
|
||
class Test implements A, B, C {} | ||
|
||
class Foo { | ||
public function foo(): A { | ||
return new Test(); | ||
} | ||
} | ||
|
||
class FooChild extends Foo { | ||
public function foo(): A&B { | ||
return new Test(); | ||
} | ||
} | ||
|
||
class FooSecondChild extends FooChild { | ||
public function foo(): ?A&B { | ||
return new Test(); | ||
} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Declaration of FooSecondChild::foo(): ?A&B must be compatible with FooChild::foo(): A&B in %s on line %d |
18 changes: 18 additions & 0 deletions
18
Zend/tests/type_declarations/intersection_types/variance/invalid8.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,18 @@ | ||
--TEST-- | ||
Intersection type removing nullable | ||
--FILE-- | ||
<?php | ||
|
||
class A {} | ||
class B extends A {} | ||
|
||
class Test { | ||
public ?A&B $prop; | ||
} | ||
class Test2 extends Test { | ||
public A&B $prop; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Type of Test2::$prop must be ?A&B (as in class Test) in %s on line %d |
21 changes: 21 additions & 0 deletions
21
Zend/tests/type_declarations/intersection_types/variance/invalid9.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-- | ||
Invalid nullable narrowing | ||
--FILE-- | ||
<?php | ||
|
||
interface A {} | ||
interface B {} | ||
|
||
class Foo { | ||
public function foo(?A&B $foo) { | ||
} | ||
} | ||
|
||
class FooChild extends Foo { | ||
public function foo(A&B $foo) { | ||
} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Declaration of FooChild::foo(A&B $foo) must be compatible with Foo::foo(?A&B $foo) in %s on line %d |
29 changes: 29 additions & 0 deletions
29
Zend/tests/type_declarations/intersection_types/variance/valid10.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,29 @@ | ||
--TEST-- | ||
Valid intersection type variance | ||
--FILE-- | ||
<?php | ||
|
||
interface X {} | ||
interface Y {} | ||
interface Z {} | ||
|
||
class TestParent implements X, Y, Z {} | ||
class TestChild implements Z {} | ||
|
||
class A { | ||
public ?X&Y $prop; | ||
|
||
public function method1(X&Y&Z $a): ?X&Y {} | ||
public function method2(X&Y $a): ?X {} | ||
} | ||
class B extends A { | ||
public ?X&Y $prop; | ||
|
||
public function method1(?X&Y $a): X&Y&Z {} | ||
public function method2(?X $a): X&Y {} | ||
} | ||
|
||
?> | ||
===DONE=== | ||
--EXPECT-- | ||
===DONE=== |
33 changes: 33 additions & 0 deletions
33
Zend/tests/type_declarations/intersection_types/variance/valid9.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,33 @@ | ||
--TEST-- | ||
Valid inheritence - co-variance | ||
--FILE-- | ||
<?php | ||
|
||
interface A {} | ||
interface B {} | ||
interface C {} | ||
|
||
class Test implements A, B, C {} | ||
|
||
class Foo { | ||
public function foo(): ?A&B { | ||
return null; | ||
} | ||
} | ||
|
||
class FooChild extends Foo { | ||
public function foo(): A&B { | ||
return new Test(); | ||
} | ||
} | ||
|
||
$o = new Foo(); | ||
var_dump($o->foo()); | ||
$o = new FooChild(); | ||
var_dump($o->foo()); | ||
|
||
?> | ||
--EXPECTF-- | ||
NULL | ||
object(Test)#%d (0) { | ||
} |
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
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.
Regarding the nullability syntax. I find
?X&Y
, while technically unambiguous due tonull&Y
not making sense, just isn't great for readability.Further, we decided against
?X|Y
in the union types RFC, and I think that decision should hold (even though the reasons are somewhat different.I've got an implementation for
(X&Y)|null
, which is my preferred syntax at: https://gist.github.com/sgolemon/660b105e6894a6341792a479cda8f76bHowever, we are much too late for 8.1 at this point and syntax is too important to rush, so this is going to have to wait for 8.2 no matter what we do.
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.
Agreed.
I'm not opposed to allowing parenthesis, but requiring them seems unnecessary - the precedence of
&
/|
and&&
/||
is known in other contexts in php (operator precedence).If
(A&B)|null
was allowed, there'd be the question of whether(A)|null
should be allowed,(A)|(null)
, etc. It seems like it should (IF allowed), though the syntax seems less readableAgreed.
Uh oh!
There was an error while loading. Please reload this page.
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 don't think you can use
?X|Y
being disallowed to conclude that everyone would also be against the special case of?X&Y
. At the time in the Github discussion I argued against?(X|Y)
, because it added parenthesis to not be ambiguous which would otherwise not be needed, andX|Y|null
was available as an alternative. In that case having a more complicated syntax only to somehow include?
would have been counterproductive, in my opinion.Yet I very much see the value of
?X&Y
- it enables optional parameters/properties, and it does not contain parenthesis. If combined union type and intersection type syntax ever makes it into PHP, it would be easy to not allow?
there, just as?
is not allowed for union types. So the syntax seems forward-compatible and geared towards a common use case in PHP.Uh oh!
There was an error while loading. Please reload this page.
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.
That's a fair complaint. For me it comes down to finding a balance between providing the functionality that's being asked for quite strongly, and not over-delivering syntax that hasn't been entirely thought through. That's why my version of this patch only allows
null
on the right hand side, and requires parens around the intersection. It's the MOST restrictive syntax, and thereby the least likely to cause issues after we've spent more time thinking about it.