Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gen_stub: Using $this when not in object context #18108

Open
DanielEScherzer opened this issue Mar 18, 2025 · 2 comments
Open

gen_stub: Using $this when not in object context #18108

DanielEScherzer opened this issue Mar 18, 2025 · 2 comments

Comments

@DanielEScherzer
Copy link
Member

Description

In ext/zend_test/test.stub.php I added

    /** @var string */
    const TEMP_TEST = __FILE__;

and then ran
php build/gen_stub.php ext/zend_test/test.stub.php

Output:

root@228aa53d048d:/usr/src/php# php build/gen_stub.php ext/zend_test/test.stub.php

Fatal error: Uncaught Error: Using $this when not in object context in /usr/src/php/build/gen_stub.php:2173
Stack trace:
#0 /usr/src/php/build/PHP-Parser-5.3.1/lib/PhpParser/ConstExprEvaluator.php(148): EvaluatedValue::{closure}(Object(PhpParser\Node\Scalar\MagicConst\File))
#1 /usr/src/php/build/PHP-Parser-5.3.1/lib/PhpParser/ConstExprEvaluator.php(102): PhpParser\ConstExprEvaluator->evaluate(Object(PhpParser\Node\Scalar\MagicConst\File))
#2 /usr/src/php/build/gen_stub.php(2209): PhpParser\ConstExprEvaluator->evaluateDirectly(Object(PhpParser\Node\Scalar\MagicConst\File))
#3 /usr/src/php/build/gen_stub.php(2615): EvaluatedValue::createFromExpression(Object(PhpParser\Node\Scalar\MagicConst\File), Object(SimpleType), NULL, Array)
#4 /usr/src/php/build/gen_stub.php(5204): ConstInfo->getDeclaration(Array)
#5 /usr/src/php/build/gen_stub.php(125): generateArgInfoCode('test', Object(FileInfo), Array, 'e496fa1faa84677...')
#6 /usr/src/php/build/gen_stub.php(6127): processStubFile('ext/zend_test/t...', Object(Context))
#7 {main}
  thrown in /usr/src/php/build/gen_stub.php on line 2173

Cause:

php-src/build/gen_stub.php

Lines 2119 to 2125 in 6329248

/**
* @param array<string, ConstInfo> $allConstInfos
*/
public static function createFromExpression(Expr $expr, ?SimpleType $constType, ?string $cConstName, array $allConstInfos): EvaluatedValue
{
// This visitor replaces the PHP constants by C constants. It allows direct expansion of the compiled constants, e.g. later in the pretty printer.
$visitor = new class($allConstInfos) extends PhpParser\NodeVisitorAbstract

... later in that same static function ...

php-src/build/gen_stub.php

Lines 2169 to 2174 in 6329248

$evaluator = new ConstExprEvaluator(
static function (Expr $expr) use ($allConstInfos, &$isUnknownConstValue) {
// $expr is a ConstFetch with a name of a C macro here
if (!$expr instanceof Expr\ConstFetch) {
throw new Exception($this->getVariableTypeName() . " " . $this->name->__toString() . " has an unsupported value");
}

getVariableTypeName() is only something that exists on VariableLike instances so I'm not even sure what the correct replacement should be

PHP Version

6329248

Operating System

No response

@DanielEScherzer
Copy link
Member Author

CC @kocsismate - it looks like this has been an issue since 8.2

php-src/build/gen_stub.php

Lines 1588 to 1597 in e144c58

public static function createFromExpression(Expr $expr, ?SimpleType $constType, ?string $cConstName, iterable $allConstInfos): EvaluatedValue
{
$originatingConst = null;
$isUnknownConstValue = null;
$evaluator = new ConstExprEvaluator(
function (Expr $expr) use ($allConstInfos, &$constType, &$originatingConst, &$isUnknownConstValue) {
if (!$expr instanceof Expr\ConstFetch && !$expr instanceof Expr\ClassConstFetch) {
throw new Exception($this->getVariableTypeName() . " " . $this->getVariableLikeName() . " has an unsupported value");
}

Given that the bug is in part of the build system, should it be fixed on 8.2? Just 8.3+? Just master?

@TimWolla
Copy link
Member

Given that this hasn't been a problem so far, it's somewhat feature request-ish. I'd say master (unless an earlier branch makes merges easier).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants