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 Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or 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

Add a Patch

Pull Requests

Add a Pull Request

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.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Oct 26 06:03:34 2021 UTC