Bug #79495 [Opn]: PHP fails to emit notices about undefined variables in switch expression
| From: | cmb@php.net | Date: | Wed, 22 Apr 2020 17:27:18 +0000 |
| Subject: | Bug #79495 [Opn]: PHP fails to emit notices about undefined variables in switch expression | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-226720@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79495&edit=1
ID: 79495
Updated by: cmb@php.net
Reported by: tandre@php.net
Summary: PHP fails to emit notices about undefined variables
in switch expression
Status: Open
Type: Bug
Package: Output Control
PHP Version: Next Major Version
Block user comment: N
Private report: N
New Comment:
If the switch body is empty, or has just a default clause, the
switch is superfluous, and as such optimized away by the compiler
(not OPcache). If there should be diagnostic message, it should
be about the superfluous switch, not the irrelevant variable in
the switch expression.
Previous Comments:
------------------------------------------------------------------------
[2020-04-19 17:46:01] tandre@php.net
Description:
------------
PHP does not emit notices in switch where the only case is the default case (or where there are no
cases)
This can be fixed by Zend/zend_compile.c emitting a CHECK_VAR opcode when the temporary value is
IS_CV (a compiled variable such as $undefSwitch), in the cases where:
- there are no case statements
- if the only case statement is the default case.
Noticed in https://github.com/php/php-src/pull/5371#discussion_r410947575
https://bugs.php.net/bug.php?id=52426 is
related, but it's from 2010 (php 5.2), and I expect opinions have changed since then, e.g. with
https://wiki.php.net/rfc/engine_warnings
getting approved
Test script:
---------------
<?php
ini_set('display_errors', 'stderr');
error_reporting(E_ALL);
echo $undefVar; // This does emit a notice
switch($undefSwitch) {} // This does not emit a notice
switch($undefSwitch2) { default: echo "default\n";} // This does not emit a notice
Expected result:
----------------
PHP should emit an undefined variable notice for the two switch statements in the test script
Actual result:
--------------
PHP does not emit any notices for the two switch statements in the test script, in php 5.3, 5.6,
7.0, 7.4, and 8.0-dev
Apparently, it did emit notices in 5.5 - https://bugs.php.net/bug.php?id=68105 suggests that
emitting notices was the expected behavior for 5.5.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79495&edit=1