diff --git a/docs/en/COMPILE_TIME_FUNCTIONS.md b/docs/en/COMPILE_TIME_FUNCTIONS.md index 8a661710..191d514d 100644 --- a/docs/en/COMPILE_TIME_FUNCTIONS.md +++ b/docs/en/COMPILE_TIME_FUNCTIONS.md @@ -50,7 +50,7 @@ There are currently 14 `std::` compile-time entry points. | `std::bigInt($value)` | Constructs a BigInt. | Implicit construction from a float variable is not allowed. | | `std::decimal($value)` | Constructs a Decimal. | A float variable must be converted via string or integer; float literals are handled per the original literal. | | `std::bigFloat($value)` | Constructs a BigFloat. | Requires 1 value parameter. | -| `std::any($value)` | Degrades the expression to `mixed/any`. | Native objects and native-object std containers cannot escape through it. | +| `std::any([$value])` | Degrades the expression to `mixed/any`; when omitted, the value defaults to `null`. | Native objects and native-object std containers cannot escape through it. | | `std::ref($target)` | Explicitly passes a target by reference. | Only accepts variables, array elements, or object properties and is only valid as a call argument wrapper. | | `std::expected($condition)` | Marks a condition as usually true. | Accepts exactly one non-unpacked argument and returns bool. | | `std::unexpected($condition)` | Marks a condition as usually false. | Accepts exactly one non-unpacked argument and returns bool. | diff --git a/docs/zh-cn/COMPILE_TIME_FUNCTIONS.md b/docs/zh-cn/COMPILE_TIME_FUNCTIONS.md index c72c1020..9b456a38 100644 --- a/docs/zh-cn/COMPILE_TIME_FUNCTIONS.md +++ b/docs/zh-cn/COMPILE_TIME_FUNCTIONS.md @@ -48,7 +48,7 @@ TypePHP 不再为编译器指令保留任何全局函数名。编译期 API 最 | `std::bigInt($value)` | 构造 BigInt。 | 不允许从 float 变量隐式构造。 | | `std::decimal($value)` | 构造 Decimal。 | float 变量需改用字符串或整型;float 字面量会按原始字面量处理。 | | `std::bigFloat($value)` | 构造 BigFloat。 | 需要 1 个值参数。 | -| `std::any($value)` | 将表达式降级为 `mixed/any`。 | Native 对象及包含 Native 对象的 std 容器不能通过它逃逸。 | +| `std::any([$value])` | 将表达式降级为 `mixed/any`;省略参数时,初始值为 `null`。 | Native 对象及包含 Native 对象的 std 容器不能通过它逃逸。 | | `std::ref($target)` | 显式以引用方式传递目标。 | 只接受变量、数组元素或对象属性,且仅可作为调用参数的引用包装器。 | | `std::expected($condition)` | 标记条件通常为真。 | 只接受一个非展开参数并返回 bool。 | | `std::unexpected($condition)` | 标记条件通常为假。 | 只接受一个非展开参数并返回 bool。 | diff --git a/src/Parser/FunctionCallTrait.php b/src/Parser/FunctionCallTrait.php index eb202b43..2c42292d 100644 --- a/src/Parser/FunctionCallTrait.php +++ b/src/Parser/FunctionCallTrait.php @@ -217,15 +217,19 @@ trait FunctionCallTrait } /** - * Erase the static type of a value through std::any(). + * Erase the static type of a value through std::any(). An omitted value + * explicitly creates dynamic storage initialized to null. */ protected function parseAnyCompileTimeCall(CallLike $expr): string { + if (count($expr->args) === 0) { + return self::VALUE_NULL; + } if (count($expr->args) !== 1 || !$expr->args[0] instanceof Node\Arg || $expr->args[0]->unpack ) { - $this->fatalError($expr, 'The std::any function expects exactly one non-unpacked argument'); + $this->fatalError($expr, 'The std::any function expects zero or one non-unpacked argument'); } $value = $expr->args[0]->value; if ($this->isNativeObjectClass($this->detectClassOfExpr($value))) { diff --git a/tests/compiler/basic/std-any.phpt b/tests/compiler/basic/std-any.phpt index 68ddc96c..cd0516a1 100644 --- a/tests/compiler/basic/std-any.phpt +++ b/tests/compiler/basic/std-any.phpt @@ -4,10 +4,17 @@ any --EXPECT-- +NULL +string(11) "initialized" float(2.5)