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
never
type is used as the return type of a function that always throwsexceptions or interrupts the flow of execution with
exit
/die
.never
vs@kphp-no-return
KPHP has the
@kphp-no-return
annotation, 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
never
in PHP 8.In our codebase we don't use the
@kphp-no-return
tag, the only place whereit is used is in
functions.txt
, where we mark theexit
/die
functions.So the new type
never
is less strict, but in general looks like a complete replacementfor the annotation, which can be removed as unnecessary. With new type,
exit
/die
functions will return
never
.Since
@kphp-no-return
affects 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
never
or@return never
is equivalent to adding the
@kphp-no-return
annotation.Thus, the new functions with
never
typehint 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-return
annotation 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, sincenever
is a new type, such errorsare checked by existing type checks and there is no need to add new additional checks.
never
can 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.