Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
RFC: https://wiki.php.net/rfc/noreturn_type
The
nevertype is used as the return type of a function that always throwsexceptions or interrupts the flow of execution with
exit/die.nevervs@kphp-no-returnKPHP has the
@kphp-no-returnannotation, which indicates that a functionalways interrupts the flow of execution with
exit/die.At the same time, exceptions cannot be thrown from the function with this annotation,
unlike functions returning
neverin PHP 8.In our codebase we don't use the
@kphp-no-returntag, the only place whereit is used is in
functions.txt, where we mark theexit/diefunctions.So the new type
neveris less strict, but in general looks like a complete replacementfor the annotation, which can be removed as unnecessary. With new type,
exit/diefunctions will return
never.Since
@kphp-no-returnaffects the construction of the CFG (Control flow graph),which occurs before the type inference, in order for the new behavior to remain the
same, we establish that the presence of an explicit type hint
neveror@return neveris equivalent to adding the
@kphp-no-returnannotation.Thus, the new functions with
nevertypehint will be taken into account when buildingthe CFG, and the old ones (even if they always interrupt the flow of execution) also will not.
Code gen
Previously, functions marked with the
@kphp-no-returnannotation generated functions with__attribute__((noreturn)), but since functions can now also throw exceptions, the functioncannot be marked with this attribute, because in case of exceptions we implicitly return
null(due to specifics of the implementation of exceptions).
Compile-time errors
When using annotations, checking that something is explicitly or implicitly returned from
a function happened in
FinalCheckPass, now, sinceneveris a new type, such errorsare checked by existing type checks and there is no need to add new additional checks.
nevercan only be used in type hints for the return type of a function, therefore, weadded checks for its use for parameters and for class properties.
This check was lost for
void, so a check was added for this type as well.