-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Allow null
and false
as standalone types
#7546
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--TEST-- | ||
Null can be used as a standalone type | ||
--FILE-- | ||
<?php | ||
|
||
function test(null $v): null { | ||
return $v; | ||
} | ||
|
||
var_dump(test(null)); | ||
|
||
?> | ||
--EXPECT-- | ||
NULL |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--TEST-- | ||
Test typed properties allow null | ||
--FILE-- | ||
<?php | ||
class Foo { | ||
public null $value; | ||
} | ||
|
||
$foo = new Foo(); | ||
$foo->value = null; | ||
|
||
try { | ||
$foo->value = 1; | ||
} catch (\TypeError $e) { | ||
echo $e->getMessage(); | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
Cannot assign int to property Foo::$value of type null |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--TEST-- | ||
Test typed properties allow false | ||
--FILE-- | ||
<?php | ||
class Foo { | ||
public false $value; | ||
} | ||
|
||
$foo = new Foo(); | ||
$foo->value = false; | ||
|
||
try { | ||
$foo->value = true; | ||
} catch (\TypeError $e) { | ||
echo $e->getMessage(); | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
Cannot assign bool to property Foo::$value of type false |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--TEST-- | ||
Typed null|false return without value generates compile-time error | ||
--FILE-- | ||
<?php | ||
|
||
function test() : null|false { | ||
return; | ||
} | ||
|
||
test(); | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: A function with return type must return a value (did you mean "return null;" instead of "return;"?) in %s on line %d |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--TEST-- | ||
Typed null return without value generates compile-time error | ||
--FILE-- | ||
<?php | ||
|
||
function test() : null { | ||
return; | ||
} | ||
|
||
test(); | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: A function with return type must return a value (did you mean "return null;" instead of "return;"?) in %s on line %d |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--TEST-- | ||
Null and false can be used in a union type | ||
--FILE-- | ||
<?php | ||
|
||
function test1(): null|false {} | ||
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. nit: Would be useful and help readability to call these and test the expected behavior, and have example implementations where Readers of the RFC might be looking at the tests to see how it's used
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. I'll add such a test case |
||
function test2(): false|null {} | ||
|
||
?> | ||
===DONE=== | ||
--EXPECT-- | ||
===DONE=== |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
--TEST-- | ||
False cannot be used as a standalone type | ||
False can be used as a standalone type | ||
--FILE-- | ||
<?php | ||
|
||
function test(): false {} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: False cannot be used as a standalone type in %s on line %d | ||
===DONE=== | ||
--EXPECT-- | ||
===DONE=== |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--TEST-- | ||
False can be used as a standalone type even with implicit nullability | ||
--FILE-- | ||
<?php | ||
|
||
function test(false $v = null) {} | ||
|
||
?> | ||
===DONE=== | ||
--EXPECT-- | ||
===DONE=== |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
--TEST-- | ||
Null cannot be used as a standalone type | ||
Null can be used as a standalone type | ||
--FILE-- | ||
<?php | ||
|
||
function test(): null {} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Null cannot be used as a standalone type in %s on line %d | ||
===DONE=== | ||
--EXPECT-- | ||
===DONE=== |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
--TEST-- | ||
Nullable false cannot be used as a standalone type | ||
Nullable false can be used as a standalone type | ||
--FILE-- | ||
<?php | ||
|
||
function test(): ?false {} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: False cannot be used as a standalone type in %s on line %d | ||
===DONE=== | ||
--EXPECT-- | ||
===DONE=== |
Uh oh!
There was an error while loading. Please reload this page.