diff --git a/src/Generator/ClosureGenerator.php b/src/Generator/ClosureGenerator.php index 781781ac..886cdc47 100644 --- a/src/Generator/ClosureGenerator.php +++ b/src/Generator/ClosureGenerator.php @@ -196,7 +196,8 @@ trait ClosureGenerator } if ($useItem->byRef) { // 闭包的 use 语法,若为引用类型,可以就地创建变量 - if (!isset($oriContext->localVars[$var])) { + if (!isset($oriContext->localVars[$var]) + && !isset($oriContext->staticVars[$var])) { $oriContext->localVars[$var] = Type::REF; } $useVars[] = $this->convertToRef($useItem->var); diff --git a/src/Preprocessor.php b/src/Preprocessor.php index 5c4d813b..2d311e00 100644 --- a/src/Preprocessor.php +++ b/src/Preprocessor.php @@ -920,10 +920,10 @@ class Preprocessor extends CompilerBase if ($value instanceof Node\Expr\ClassConstFetch && $this->isNameExpr($value->class) && $this->isIdExpr($value->name)) { - $class = $this->getResolvedPhpName($value->class); + $targetClass = $this->getResolvedPhpName($value->class); $constant = $value->name->toString(); if (strtolower($constant) === 'class') { - return $class; + return $targetClass; } $targets = [ 'Int' => Type::INT, @@ -939,7 +939,7 @@ class Preprocessor extends CompilerBase 'Stream' => Type::STREAM, 'Box' => Type::BOX, ]; - if (strcasecmp($class, 'Type') === 0 && isset($targets[$constant])) { + if (strcasecmp($targetClass, 'Type') === 0 && isset($targets[$constant])) { return $targets[$constant]; } } diff --git a/src/Translator.php b/src/Translator.php index b0b3cf61..14916aec 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -58,7 +58,7 @@ class Translator extends Preprocessor use ResourceCompilationTrait; use ClassConstantValueTrait; - public const string VERSION = '0.4.0'; + public const string VERSION = '0.4.1'; public const string APP_NAME = 'TypePHP Compiler (AOT)'; protected const string MODULE_NAME_PREFIX = 'app_'; diff --git a/tests/compiler/static/static-vars-closure-reference.phpt b/tests/compiler/static/static-vars-closure-reference.phpt new file mode 100644 index 00000000..b6d9a649 --- /dev/null +++ b/tests/compiler/static/static-vars-closure-reference.phpt @@ -0,0 +1,26 @@ +--TEST-- +static variable captured by reference is not redeclared as a local variable +--FILE-- + +--EXPECT-- +int(1) +int(1)