From c5f38a3d057dd63fc5e4b4316548df932f7896e4 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Fri, 5 Jun 2026 19:17:00 +0800 Subject: [PATCH] =?UTF-8?q?fix(parser):=20=E4=BF=AE=E5=A4=8D=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E7=B1=BB=E5=9E=8B=E6=8E=A8=E6=96=AD=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 当右侧表达式为无类型但左侧变量已有已知原生类型时, 使用左侧类型进行转换以确保类型安全 - 在赋值操作中添加类型检查逻辑,避免类型丢失问题 - 更新 fibo 和 fibo_r 函数参数类型声明为 int - 添加 issetor 性能测试用例以验证类型推断优化效果 --- examples/bench.php | 4 ++-- perf/issetor.php | 15 +++++++++++++++ src/Php/Parser/AssignOpTrait.php | 11 +++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 perf/issetor.php diff --git a/examples/bench.php b/examples/bench.php index acdf8a04..762b9490 100644 --- a/examples/bench.php +++ b/examples/bench.php @@ -179,11 +179,11 @@ function ary3($n) { /****/ -function fibo_r($n){ +function fibo_r(int $n){ return(($n < 2) ? 1 : fibo_r($n - 2) + fibo_r($n - 1)); } -function fibo($n) { +function fibo(int $n) { $r = fibo_r($n); print "$r\n"; } diff --git a/perf/issetor.php b/perf/issetor.php new file mode 100644 index 00000000..6b324814 --- /dev/null +++ b/perf/issetor.php @@ -0,0 +1,15 @@ +parseAssignArrayDim($left, $right); } + // When the RHS is untyped (TYPE_VAR) but the LHS variable already has a known + // native type, use the LHS type for the conversion so that e.g. + // php::Int i; ... i = n; (n is php::Var) + // becomes i = php::toInt(n); + if ($finalVarType === self::TYPE_VAR && $this->hasVar($var)) { + $varType = $this->getVarType($var); + if ($varType !== self::TYPE_VAR) { + $finalVarType = $varType; + } + } + $rightExpr = $this->parseAssignRightExpr($right); $leftExprType = $this->detectTypeOfExpr($left); $rightExprType = $this->detectTypeOfExpr($right);