Doc #80858 [Com]: Undefined behavior when match branch is function returning void

[email protected] ("myjohnjdf at gmail dot com") Sun, 26 Mar 2023 20:29:49 +0000
Newsgroups php.doc.bugs
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=80858&edit=1

 ID:                 80858
 Comment by:         myjohnjdf at gmail dot com
 Reported by:        patrick dot kusebauch at gmail dot com
 Summary:            Undefined behavior when match branch is function
                     returning void
 Status:             Open
 Type:               Documentation Problem
 Package:            Documentation problem
 PHP Version:        8.0.3
 Block user comment: N
 Private report:     N

 New Comment:

The information you have posted is very useful. The sites you have referred was good. Thanks for sharing. (https://myjdfaccount.top/)github.com


Previous Comments:
------------------------------------------------------------------------
[2022-12-26 08:08:27] ambari242 at gmail dot com

Thanks for sharing that. (https://www.yourtexasbenefits.ltd/)github.com

------------------------------------------------------------------------
[2022-12-23 09:03:50] amin dot jabari242 at gmail dot com

And today everything is working very well. 
(https://www.myjdfaccount.net/)github.com

------------------------------------------------------------------------
[2021-03-13 01:56:33] [email protected]

This is not undefined behaviour because of match, all functions in PHP by default will return NULL even if they are declared void.

This might need to be said in the 'void' type section on the Type Declaration page (https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.void).

A parse error doesn't make sense as it's at best a compile error (using result of void function), however changing any such behaviour to hard error would require an RFC.

------------------------------------------------------------------------
[2021-03-13 01:45:12] patrick dot kusebauch at gmail dot com

Description:
------------
In the case that you have a match statement and the expression in one of the branches is a function that returns `void`, there is no mention what is the expected behavior in the documentation. Right now it seems that it evaluates to `NULL`. 

This behavior is not well understood as can be seen here: https://github.com/phpstan/phpstan/issues/4292 and PHPStorm will flag it as a possible bug: "'void' method result used".

This is not limited to a case where a match statement result is assigned. All these issues (PHPStorm, PHPStan) are present even if the match result is not used.

Test script:
---------------
<?php

function nothing(): void
{
 
}

$result = match ('inserted') {
    'inserted' => nothing(),
};

var_dump($result);

Expected result:
----------------
Either parse error on the branch or at least parse error in the assignment to `result` would make sense to me.

Actual result:
--------------
NULL


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=80858&edit=1