php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80596 Invalid union type TypeError in anonymous classes
Submitted: 2021-01-06 12:16 UTC Modified: 2021-01-06 12:20 UTC
From: daniil at daniil dot it Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 8.0.0 OS: Arch linux
Private report: No CVE-ID: None
 [2021-01-06 12:16 UTC] daniil at daniil dot it
Description:
------------
Type errors thrown for union typehints containing self in an anonymous class contain only class@anonymous in the error message, without the full typehint.

Test script:
---------------
class a {
    public function test(): self|string
    {
        return new \stdClass;
    }
};

$b = new class {
    public function test(): self|string
    {
        return new \stdClass;
    }
};

try {
    (new a)->test();
} catch (\Throwable $e) {
    echo $e->getMessage().PHP_EOL;
}

try {
    $b->test();
} catch (\Throwable $e) {
    echo $e->getMessage().PHP_EOL;
}


Expected result:
----------------
a::test(): Return value must be of type a|string, stdClass returned
class@anonymous::test(): Return value must be of type class@anonymous|string, stdClass returned

Actual result:
--------------
a::test(): Return value must be of type a|string, stdClass returned
class@anonymous::test(): Return value must be of type class@anonymous, stdClass returned

Patches

fix-anonymous-class-union-typehints (last revision 2021-01-13 09:46 UTC by daniil at daniil dot it)

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-01-06 12:20 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2021-01-06 12:20 UTC] cmb@php.net
Confirmed: <https://3v4l.org/e83WV>.
 [2021-01-13 09:46 UTC] daniil at daniil dot it
The following patch has been added/updated:

Patch Name: fix-anonymous-class-union-typehints
Revision:   1610531200
URL:        https://bugs.php.net/patch-display.php?bug=80596&patch=fix-anonymous-class-union-typehints&revision=1610531200
 [2021-01-13 14:13 UTC] cmb@php.net
The following pull request has been associated:

Patch Name: Fix #80596 - broken anonymous class union typehint errors
On GitHub:  https://github.com/php/php-src/pull/6601
Patch:      https://github.com/php/php-src/pull/6601.patch
 [2021-01-14 09:04 UTC] nikic@php.net
Automatic comment on behalf of daniil@daniil.it
Revision: http://git.php.net/?p=php-src.git;a=commit;h=f9fbba41b66e0e9ccd40ff064d00cfe987c2c5bb
Log: Fixed bug #80596: Fix anonymous class union typehint errors
 [2021-01-14 09:04 UTC] nikic@php.net
-Status: Verified +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 14:01:29 2024 UTC