Repository navigation
fix(compiler): emit compilable C++ for readonly scalar reads and string ternaries - #186
Merged
Merged
Conversation
… storage Readonly properties are intentionally excluded from scalar reference hoisting, so their generic read yields php::Variant. Using that value to initialize a php::Int/php::Float local produces invalid C++ when a readonly typed property feeds a switch discriminant or a typed return. Convert the dynamic read back to the declared scalar storage instead.
A Zend-backed string property read lowers to php::Variant while a string literal lowers to php::String, so the branches have no common C++ type when their declared types match but their storage differs. Convert both string branches through the declared string storage; other scalar storage already has a common type and needs no change.
Handle readonly scalar storage before either receiver-specific hoisting path, including typed parameters and local objects. Recover readable readonly declarations for expression receivers without changing dynamic lookup for other properties. Use the existing scoped property reader before conversion so uninitialized slots throw instead of becoming zero or false, while lexical visibility and the prohibition on hoisted readonly references remain enforced. Add codegen and runtime coverage for parameter, local and expression receivers, float/bool reads, and uninitialized int/float/bool properties. Validated PHP 8.4/8.5 related PHPUnit suites (51 tests, 171 assertions each) and all 103 switch/control_flow/object_property PHPTs on Linux PHP 8.4.
yuan-dian
added a commit
to yuan-dian/typephp
that referenced
this pull request
Nov 13, 2026
A readonly scalar read keeps the dynamic value source, so its result is materialized as a Variant. swoole#186 correctly converts the read back to the declared scalar, but it also stamped the expression with the declared type. The surrounding expression then trusted that type and dropped its own conversion: `$rect->width / 2.0` compiled to a raw Variant division assigned to php::Float, which is C2440. Keep the conversion in the read and leave the lowered type alone, so the arithmetic keeps its toFloat/toInt. The switch discriminant consequently falls back to PHP's loose equality over the Variant, which matches the value the read actually returns. Cover the readonly float division that the Drift2 layout code hits.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
AOT 编译在两种常见的类型退化场景下生成非法 C++,直接阻断整个工程构建(g++ / MSVC 均失败):
switch ($this->status)中判别式被声明为php::Int,却用返回php::Variant的 generic 读取初始化。php::Variant、另一个是const php::String&,裸 C++ 条件表达式没有公共类型。复现
根因
SsaPropOptimizer::canHoistObjectPropBySafety()对 readonly 属性返回false,这是有意的:promoted 属性的初始化顺序会让函数级引用不安全(NativePropertyTest::testReadonlyPropertiesDoNotUseNativeScalarReferences已有断言保护)。emitNativeInstancePropertyTypedFetch()因此直接return null,调用方退回 generic 读取(php::Variant),而下游仍按声明类型(php::Int/php::Float)使用该值。parseTernary只比较detectTypeOfExpr得到的声明类型,不比较getLoweredExpressionType的存储类型。声明类型一致时不设置ATTR_LOWERED_TYPE、也不包裹分支,于是php::Variant与php::String直接进入同一个条件表达式。注意Variant与php::Int有公共类型(Variant),int/float/bool 三元本来可编译,因此修复只需覆盖字符串。改动
src/Parser/PropertyAccessTrait.php(emitNativeInstancePropertyTypedFetch):readonly 属性无法 hoist 时,对 generic 动态读取结果调用convertExprFromType($def->type, $getter),恢复声明的标量存储类型,并标记 value source 为DYNAMIC。不放宽 hoist 排除,readonly 语义不变。src/Parser/SelectionExpressionTrait.php(parseTernary):新增一个收窄分支——当$ifType === $elseType === Type::STR且两侧 lowered storage 不同时,把两个分支都经convertExprFromType(Type::STR, ...)统一,并设置ATTR_LOWERED_TYPE = Type::STR。其余类型路径输出保持字节级不变。usesNativeScalarStorage()判断(INT/FLOAT/BOOL)时到达,string/array/object 属性的既有生成路径不受影响。测试
新增 6 个回归用例(实现 + codegen 断言 + 运行时 PHPT 成对提交):
phpunit/src/ReadonlyTypedPropertyReadTest.php+phpunit/code/readonly-typed-property-read.phpphp::toInt(this_.attr(、php::toFloat(this_.attr(、switch (tmp_var_0)_object_prop_this___status/___ratio/AttrMode::Update(readonly 仍不 hoist)phpunit/src/TernaryStorageTypeTest.php+phpunit/code/ternary-storage-unification.phpphp::toString(this_.attr(、php::toString(php::empty_string)? (this_.attr(/? (_typephp_static_property_tests/compiler/switch/readonly-typed-property.phpttests/compiler/control_flow/ternary-typed-property-storage.phpt验证
phpunit:新增用例OK (2 tests, 12 assertions)。build-rc=0、Successfully compiled 9 files;产物运行输出与 PHP 8.4 解释执行逐字节一致(含 readonly int/bool、static/实例字符串三元、null/?->三元)。tests/compiler/{switch,control_flow,object_property}共 102/102 PASS(-j1串行)。phpunit与干净 master 基线一致,仅存在既有的swoole/php-nano缺失与本地 phpx/clang 头不匹配失败,无新增失败。风险与兼容性