fix(php): 修复原生类型变量和对象属性的 unset 操作问题

- 修改对象属性 unset 警告消息为更准确的描述
- 添加对原生类型变量的 unset 操作检查和警告
- 阻止原生类型变量执行 unset 操作
- 新增 unset 原生类型变量的测试用例
- 完善变量类型检查逻辑以区分原生类型
pull/1/head
韩天峰 4 months ago
parent fbeac08f20
commit 0e319c48de
  1. 9
      examples/error/unset-int-var.php
  2. 9
      src/Php/CompilerBase.php

@ -0,0 +1,9 @@
<?php
use native_types;
function main()
{
$x = 100;
unset($x);
var_dump($x);
}

@ -3432,7 +3432,7 @@ class CompilerBase extends \PhpAot\Core\Translator
if ($object === 'this_' and $this->isIdExpr($var->name)) {
$propName = $this->parseIdentifier($var->name);
if ($this->hasObjectPropVar($this->getObjectPropVarName($object, $propName))) {
$this->warning($var, "Unset of object property `{$propName}` is not allowed");
$this->warning($var, "Object property `$propName` of native types cannot be unset");
$lines = [];
$lines[] = $this->getObjectPropVarName($object, $propName) . " = 0;";
}
@ -3442,7 +3442,12 @@ class CompilerBase extends \PhpAot\Core\Translator
if (!$this->hasVar($name)) {
$this->errorUndefinedVariable($var);
}
$lines[] = "{$name}.unset();";
$type = $this->getVarType($name);
if ($this->isNativeType($type)) {
$this->warning($var, "Variable of native type `\${$name}` cannot be unset");
} else {
$lines[] = "{$name}.unset();";
}
} else {
$this->fatalError($var, "Unsupported unset type `{$var->getType()}`");
}

Loading…
Cancel
Save