php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #80858 Undefined behavior when match branch is function returning void
Submitted: 2021-03-13 01:45 UTC Modified: 2021-03-13 01:56 UTC
From: patrick dot kusebauch at gmail dot com Assigned:
Status: Open Package: Documentation problem
PHP Version: 8.0.3 OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: patrick dot kusebauch at gmail dot com
New email:
PHP Version: OS:

 

 [2021-03-13 01:45 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-03-13 01:56 UTC] girgias@php.net
-Operating System: Any +Operating System:
 [2021-03-13 01:56 UTC] girgias@php.net
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.
 [2022-12-23 09:03 UTC] amin dot jabari242 at gmail dot com
And today everything is working very well. 
(https://www.myjdfaccount.net/)github.com
 [2022-12-26 08:08 UTC] ambari242 at gmail dot com
Thanks for sharing that. (https://www.yourtexasbenefits.ltd/)github.com
 [2023-03-26 20:29 UTC] myjohnjdf at gmail dot com
The information you have posted is very useful. The sites you have referred was good. Thanks for sharing. (https://myjdfaccount.top/)github.com
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC