php-cfg icon indicating copy to clipboard operation
php-cfg copied to clipboard

Assertion fail when returning an anonymous function.

Open quanyang opened this issue 9 years ago • 6 comments

In the following code example, an anonymous function is returned, acting like a closure. However, the Printer class expects a function name.

function generateComparisonFunctionForKey($key) {
        return function ($left, $right) use ($key) {
            if ($left[$key] == $right[$key])
                return 0;
            else
                return ($left[$key] < $right[$key]) ? -1 : 1;
        };
    }

Error:

Warning: assert(): Assertion failed in /Users/<redacted>/php-cfg/lib/PHPCfg/Printer.php on line 46

quanyang avatar Mar 15 '16 13:03 quanyang

The assertion error is fixed by https://github.com/ircmaxell/php-cfg/commit/d43ed9f52f320cabf025a82b622bae9ecc48e483.

However the way we handle bound closure variables currently is not good (and for the reference case outright violates SSA). Closure body also shouldn't be part of the same CFG really...

nikic avatar Mar 15 '16 15:03 nikic

Yea, seems like an issue I'll face later.

quanyang avatar Mar 15 '16 15:03 quanyang

Follow-up fix: https://github.com/ircmaxell/php-cfg/commit/8e85ad3fb7b79b345cb49cceeb6bd982d83ea421

I didn't have notices enabled locally, so missed that part.

nikic avatar Mar 15 '16 16:03 nikic

In https://github.com/ircmaxell/php-cfg/commit/0d54187759ac945e4cf02d8c8dff82fb121192e0 I've introduced a Func structure, so each function has a clearly separated CFG. The Funcs are collected in a Script, which is what is returned by the parser now.

Closures still need extra work for the use variable handling.

nikic avatar Mar 22 '16 17:03 nikic

Does GraphViz still work?

quanyang avatar Mar 28 '16 09:03 quanyang

GraphViz printer fixed in https://github.com/ircmaxell/php-cfg/commit/1b0e384103c5484ee1778514fe89888c908309c5. Also added a printScript method to print all functions into one graph.

nikic avatar Mar 28 '16 13:03 nikic