From 64d18db7e3d1f92673dbae7b5ba8eeb1abe81479 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Mon, 12 Jan 2026 15:30:32 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=20for/foreach=20=E7=9A=84=20?= =?UTF-8?q?key/value=20=E5=8F=98=E9=87=8F=E7=9A=84=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E9=87=8D=E5=A4=8D=E6=A3=80=E6=B5=8B=EF=BC=8C?= =?UTF-8?q?=E5=9C=A8=20C++=20=E4=B8=AD=E4=BD=BF=E7=94=A8=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E9=81=AE=E8=94=BD=EF=BC=88variable=20shadowing=EF=BC=89?= =?UTF-8?q?=E4=B8=8D=E8=A6=86=E7=9B=96global/local=20var?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Php/CompilerBase.php | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index def04d6d..e6b1da62 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -1074,9 +1074,6 @@ class CompilerBase extends \PhpAot\Core\Translator if ($expr->getType() === 'Expr_Assign') { $left = $expr->var; $name = $this->parseIdentifier($left); - if ($this->hasGlobalVar($name)) { - $this->fatalError($left, 'Cannot assign to global variable in for loop'); - } $type = $this->detectExprType($expr->expr); $code .= $type . ' ' . $name . ' = ' . '(' . $this->parseIdentifier($expr->expr) . ');'; } @@ -1961,10 +1958,6 @@ class CompilerBase extends \PhpAot\Core\Translator $code .= 'for (auto iter = ' . $iteratorVar . '.begin(); iter != ' . $iteratorVar . '.end(); ++iter) {' . PHP_EOL; $this->indentLevel++; if ($node->keyVar) { - // foreach 的 key/value 不能与全局变量同名 - if ($this->hasGlobalVar($keyVar)) { - $this->fatalError($node->keyVar, 'Cannot redefine key variable: ' . $this->unescapeVarName($keyVar)); - } $code .= self::TYPE_VAR . ' ' . $this->getIndent() . ' ' . $keyVar . ' = iter.key();' . PHP_EOL; } @@ -1976,10 +1969,6 @@ class CompilerBase extends \PhpAot\Core\Translator $dim = $this->parseIdentifier($node->valueVar->dim); $code .= $this->getIndent() . "$array.offsetSet($dim, iter.value());"; } else { - // foreach 的 key/value 不能与全局变量同名 - if ($this->hasGlobalVar($valueVar)) { - $this->fatalError($node->valueVar, 'Cannot redefine value variable: ' . $this->unescapeVarName($valueVar)); - } $code .= self::TYPE_VAR . ' ' . $this->getIndent() . ' ' . $valueVar . ' = iter.value();' . PHP_EOL; } $code .= $this->parseStmts($stmts);