From 4ff12b58d12cda33fbc8234958a7b8b48e70a68b Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 10 Jun 2026 14:55:54 +0800 Subject: [PATCH] =?UTF-8?q?fix(php):=20=E4=BF=AE=E5=A4=8D=E7=88=B6?= =?UTF-8?q?=E7=B1=BB=E5=BC=95=E7=94=A8=E5=92=8C=E5=91=BD=E5=90=8D=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在parent类型声明前检查classDef是否存在,避免在类外部使用parent关键字时报错 - 改进命名参数缺失默认值时的错误节点定位逻辑 - 为parent类引用添加类上下文检查,防止在类外部使用parent关键字 --- src/Php/CompilerBase.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 8b32f224..acdb85fd 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -1012,6 +1012,9 @@ class CompilerBase extends \PhpAot\Core\Translator if ($typeName === 'self') { $class = $this->getFullClassName(); } elseif ($typeName === 'parent') { + if (!$this->classDef) { + $this->fatalError($type, 'Cannot use "parent" type declaration outside a class'); + } $class = $this->classDef->extends; } elseif ($typeName === 'static') { // static 类无法在编译期获取 @@ -2645,7 +2648,14 @@ class CompilerBase extends \PhpAot\Core\Translator } if (!isset($args[$k])) { if ($argInfo->defaultValue === null) { - $this->fatalError($callArgs[$i], 'Named argument `' . $argInfo->name . '` is missing default value'); + $errorNode = null; + foreach ($callArgs as $a) { + if ($a instanceof Node\Arg && $a->name) { + $errorNode = $a; + break; + } + } + $this->fatalError($errorNode ?? reset($callArgs), 'Named argument `' . $argInfo->name . '` is missing default value'); } $args[$k] = new Node\Arg($argInfo->defaultValue); } @@ -3214,6 +3224,9 @@ class CompilerBase extends \PhpAot\Core\Translator if ($className === 'self') { $className = $this->getFullClassName(); } elseif ($className === 'parent') { + if (!$this->classDef) { + $this->fatalError($expr, 'Cannot use "parent" outside a class'); + } $className = $this->classDef->extends; } else { $className = $this->getNamespacedClassName($className);