Skip to content

feat(args): declare untyped and union parameter and return types - #857

Merged
ptondereau merged 4 commits into
extphprs:mainfrom
ChiragAgg5k:feat/union-untyped-args
Nov 25, 2026
Merged

ptondereau merged 4 commits into
extphprs:mainfrom
ChiragAgg5k:feat/union-untyped-args

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Problem

An internal function's parameter always gets a type: Arg::new takes a single DataType, and the closest to PHP's untyped function f($value) is mixed. Union types can't be expressed at all; see #199, which this partly addresses.

Reflection and PHP's inheritance checks see the difference. A PHP class whose method has an untyped parameter, or an int|float one, can't be reproduced. A port that needs it has to patch the engine's copy of the argument info after registration.

Motivation: Appwrite is porting Utopia PHP libraries to Rust as classes of a PHP extension. Their base classes declare isValid($value) untyped and int|float parameters, and PHP subclasses in application code override them.

Change

  • Arg::untyped(name) declares a parameter without a type: ReflectionParameter::hasType() is false, and overrides may declare any type or none.

  • Arg::or(DataType) adds a member to a union: Arg::new("value", DataType::Long).or(DataType::Double) declares int|float $value.

  • FunctionBuilder::or_returns(DataType) does the same for the return type.

  • Allowed members: the argument's type and every member go through ZendType::scalar_type_mask (bool, int, float, string, array, object), plus null for a member.

  • Refused, with the new Error::InvalidUnionType from build:

    • any other type: a class (it would need a zend_type_list), iterable, resource, void, mixed, undef, a reference, a pointer, …;
    • a union on an untyped argument;
    • or_returns without returns.

    These are refused rather than producing masks PHP can't read, or being dropped silently.

  • FunctionHandler exported: it's the type of the handler FunctionBuilder::new takes, and is now exported from ext_php_rs::builders.

  • Size: the union is stored as Option<Box<Vec<DataType>>>, under an explained #[expect(clippy::box_collection)]. Arg travels in Result::Err (consume), so it has to stay under result_large_err.

Enforcement (documented)

These types are what reflection and the inheritance checks see:

  • Arguments: release builds of PHP don't check an internal function's arguments against them, so the handler must check what it receives.
  • Return values: debug builds do check the return value against the declared return type and assert on a mismatch. A handler declared : int|float must not return null, even on bad input.

Tests

  • Unit:
    • every DataType variant as a union member, with its expected mask or InvalidUnionType;
    • an untyped argument with a union;
    • or_returns without returns;
    • the mask of a union return type.
  • Integration (tests/src/integration/arg_types): reflection of untyped and union parameter and return types, plus a PHP subclass overriding abstract methods declared with them, which PHP's inheritance check accepts.
  • Guide: a section in types/functions.md.

Future work

Stub generation (describe::Parameter) holds a single DataType: an untyped parameter is described with no type, a union as its first member. Describing unions needs describe to carry several types.

🤖 Generated with Claude Code

Part of: appwrite-labs/cloud#7503

@coveralls

coveralls commented Nov 17, 2026 •

Copy link
Copy Markdown

Coverage Report for CI Build 45176341594

Coverage increased (+0.3%) to 77.102%

Details

  • Coverage increased (+0.3%) from the base build.
  • Patch coverage: 2 uncovered changes across 1 file (154 of 156 lines covered, 98.72%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
src/args.rs 100 98 98.0%
Total (3 files) 156 154 98.72%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 16233
Covered Lines: 12516
Line Coverage: 77.1%
Coverage Strength: 67.23 hits per line

💛 - Coveralls

@codspeed

codspeed Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 51 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 51 untouched benchmarks


Comparing ChiragAgg5k:feat/union-untyped-args (66cc14e) with main (b7ffba7)

Open in CodSpeed

ChiragAgg5k and others added 3 commits November 23, 2026 12:28
An internal function's parameter always got a type: `Arg::new` takes a
single `DataType`, and the closest to an untyped `function f($value)` was
`mixed`. Reflection and PHP's inheritance checks see the difference: a PHP
class whose method had an untyped parameter, or an `int|float` one, could
not be reproduced, and code that relied on it had to patch the engine's
copy of the argument info after registration.

- `Arg::untyped(name)` declares a parameter without a type.
- `Arg::or(DataType)` adds a member to a union of scalar types (and `null`):
  `Arg::new("value", DataType::Long).or(DataType::Double)` is
  `int|float $value`.
- `FunctionBuilder::or_returns(DataType)` does the same for the return type.

A class type in a union would need a type list and fails `build` with
`Error::ZvalConversion`. `FunctionHandler`, the type of the handler
`FunctionBuilder::new` takes, is exported from `builders`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review of the union support:

- Union members go through `ZendType::scalar_type_mask`, the allowlist the
  typed properties use (`bool`, `int`, `float`, `string`, `array`,
  `object`), plus `null`. `.or(Resource)` and `.or(Undef)` gave the masks
  that crash `ReflectionType::__toString`, `.or(Reference)` and `.or(Ptr)`
  stray bits, `.or(Void)` and `.or(Mixed)` types PHP forbids, and
  `iterable` a bit only its own arginfo can carry; they now fail `build`
  with `Error::InvalidUnionType`, as does a class type.
- A union on an `Arg::untyped` argument, and `or_returns` without
  `returns`, fail `build` instead of being dropped.
- The docs say the handler must check its arguments (release builds do not
  check them against the declared types), and must return a value of the
  declared union (debug builds assert on it).

Unit tests cover every `DataType` variant as a union member.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
LeakSanitizer reported the function name the test never registered.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ChiragAgg5k
ChiragAgg5k force-pushed the feat/union-untyped-args branch from 16e99f1 to 52f04c2 Compare November 23, 2026 12:33
Nightly rustfmt (2026-11-22) no longer breaks a line inside a markdown link, so tools/update_lib_docs.sh produced a different copy and the macro docs check failed.
@ptondereau
ptondereau merged commit 642adb8 into extphprs:main Nov 25, 2026
85 checks passed
@ext-php-rs-release ext-php-rs-release Bot mentioned this pull request Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants