From 2ef23587356d559fed45ad37bff5544e7891ccaa Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 18 Mar 2026 17:30:26 +0800 Subject: [PATCH] =?UTF-8?q?fix(compiler):=20=E7=A7=BB=E9=99=A4=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E7=B1=BB=E5=9E=8B=E9=87=8D=E6=96=B0=E8=B5=8B=E5=80=BC?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 删除了变量类型不匹配时的致命错误检查 - 允许相同变量名在不同类型的std::转换间灵活赋值 - 更新了本地变量类型验证和添加逻辑 - 修改了表达式解析和类型转换流程 --- docs/UNSUPPORTED_SYNTAX.md | 394 +++++++++++++++++++++++++++++++++++++ src/Php/CompilerBase.php | 4 - tests/aot/native-type.phpt | 18 ++ 3 files changed, 412 insertions(+), 4 deletions(-) diff --git a/docs/UNSUPPORTED_SYNTAX.md b/docs/UNSUPPORTED_SYNTAX.md index ca2bd370..d4366e54 100644 --- a/docs/UNSUPPORTED_SYNTAX.md +++ b/docs/UNSUPPORTED_SYNTAX.md @@ -270,6 +270,400 @@ $c = $a + $b; // 需要类型转换,可能有性能损失 --- +## 🔒 类型系统对比 + +### 动态类型系统(ZVAL - 默认) + +**特点**: 变量可以在运行时自由改变类型 + +**示例**: +```php +id = $id; + $this->price = $price; + $this->name = $name; + } + + // ✅ 类型安全的 getter/setter + public function getPrice(): std::float { + return $this->price; + } + + // ❌ 错误:类型不匹配 + public function setPrice(std::int $price) { + $this->price = $price; // 编译错误 + } +} +``` + +#### 场景三:循环和计数器 + +```php + std::int(PHP_INT_MAX - 1000)) { + // 转为 ZVAL 处理大数 + return (int)$a / (int)$b; + } + + return std::float($a) / std::float($b); +} +``` + +#### 3. 渐进式迁移 + +```php +hasVar($var)) { $this->addLocalVar($var, $type); - } else { - if ($this->getVarType($var) !== $type) { - $this->fatalError($left, "Cannot re-assign {$var} to {$type}"); - } } $expr = $this->parseExpr($right->args[0]->value); return $var . ' = ' . $this->convertExprFromType($type, $expr); diff --git a/tests/aot/native-type.phpt b/tests/aot/native-type.phpt index 467a361e..79dd7297 100644 --- a/tests/aot/native-type.phpt +++ b/tests/aot/native-type.phpt @@ -12,9 +12,27 @@ function main() $c = std::bool(true); var_dump($c); + + $a = 99; + $d = std::int($a); + var_dump($d); + + $e = '2026_'; + $f = std::float($e); + var_dump($f); + + $h = 10; + var_dump($h / 4); + + $i = std::int(10); + var_dump($i / 4); } ?> --EXPECT-- int(100) float(100) bool(true) +int(99) +float(2026) +float(2.5) +int(2)