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

From: Date: Sat, 13 Mar 2021 01:56:33 +0000
Subject: Doc #80858 [Opn]: Undefined behavior when match branch is function returning void
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-18632@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80858&edit=1

 ID:                 80858
 Updated by:         girgias@php.net
 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
-Operating System:   Any
+Operating System:   
 PHP Version:        8.0.3
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[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


Thread (5 messages)

« previous php.doc.bugs (#18632) next »