-
Notifications
You must be signed in to change notification settings - Fork 272
Void return type #150
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
Void return type #150
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 |
---|---|---|
|
@@ -903,6 +903,10 @@ by value or byRef. If *expression* is omitted the value `NULL` is used. | |
If execution flows into the closing brace (`}`) of a function, `return | ||
NULL;` is implied. | ||
|
||
Explicit `return` statements with *expression* given are not permitted within a | ||
function with a `void` [return type](13-functions.md#return-typing) and MUST | ||
cause a compile-time fatal error. | ||
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 think this is the only part of the spec that mentions "compile-time fatal error" aside from the definition of 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. Also, is "Explicit 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 could make it clear that 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. Small correction: the spec is written in descriptive mode ("causes fatal error" rather than "MUST cause fatal error"). Otherwise, it looks clear enough. |
||
|
||
A function may have any number of `return` statements, whose returned | ||
values may have different types. | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,6 +74,7 @@ A function is called via the function-call operator [`()`](10-expressions.md#fun | |
|
||
<i>return-type:</i> | ||
: <i>type-declaration</i> | ||
: void | ||
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. This grammar isn't representative of how PHP actually implements this (making it be part of 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. No I think it's fine - void is valid only as return type, not any type declaration. |
||
|
||
<i>type-declaration:</i> | ||
array | ||
|
@@ -185,6 +186,9 @@ be compatible with the defined type, using the same rules as for parameter type | |
are not allowed for typed returns. If the value of the [`return` statement](11-statements.md#the-return-statement) | ||
does not pass the type check, a fatal error is produced. | ||
|
||
The `void` type is a special type that can only be used as a return type, and | ||
not in other contexts. It has no effect at runtime, see the [`return` statement](11-statements.md#the-return-statement). | ||
|
||
## Type check modes | ||
|
||
The type checking can be performed in two modes, strict and coercive (default). | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--TEST-- | ||
void return type: acceptable cases | ||
--FILE-- | ||
<?php | ||
|
||
function foo(): void { | ||
// okay | ||
} | ||
|
||
foo(); | ||
|
||
function bar(): void { | ||
return; // okay | ||
} | ||
|
||
bar(); | ||
|
||
echo "OK!", PHP_EOL; | ||
--EXPECT-- | ||
OK! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--TEST-- | ||
void return type: unacceptable cases: explicit NULL return | ||
--FILE-- | ||
<?php | ||
|
||
function foo(): void { | ||
return NULL; // not permitted in a void function | ||
} | ||
|
||
// Note the lack of function call: function validated at compile-time | ||
--EXPECTF-- | ||
Fatal error: A void function must not return a value in %s on line %d |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--TEST-- | ||
void return type: unacceptable cases: explicit return of some other value | ||
--FILE-- | ||
<?php | ||
|
||
function foo(): void { | ||
return -1; // not permitted in a void function | ||
} | ||
|
||
// Note the lack of function call: function validated at compile-time | ||
--EXPECTF-- | ||
Fatal error: A void function must not return a value in %s on line %d |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--TEST-- | ||
void return type: not valid as a parameter type | ||
--FILE-- | ||
<?php | ||
|
||
function foobar(void $a) {} | ||
--EXPECTF-- | ||
Fatal error: void cannot be used as a parameter type in %s on line %d | ||
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. Not sure if this test should be included, since a reasonable implementation might consider this a syntax error. (Also, should we really be testing error messages anyway? They're not in the spec. :/) 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. Well, not really, we could rewrite the tests to only check if there's exception now. If anybody has the time ;) |
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.
The Oxford comma went missing here.
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.
Oh, yeah, my bad. Luckily it doesn't make a difference here, though.
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.
@GrahamCampbell I've fixed it: 1935a4b
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 already sent a PR though: #151.
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.
Oops, sorry. I guess it'll have to be closed now D: