Skip to content

feat: a tuple, a class value and Object are tested like any other type - #168

Merged
heyJordanParker merged 5 commits into
masterfrom
fix/type-tests
Oct 18, 2026
Merged

heyJordanParker merged 5 commits into
masterfrom
fix/type-tests

Conversation

@heyJordanParker

Copy link
Copy Markdown
Owner

value as [int, string], a tuple in a union pattern such as value is int|[int, string] pair, every is, as and match arm of Class<Order>, and every test of Object were refused as "This type is not supported yet in PHP#." Spec section 21 says every type answers is, as and match the same way, and v1 leaves no "not supported yet".

What a PHP# developer can now write

  • as a tuple gives the value when it has the tuple's count and each element's type, and null otherwise: pair as [int, string].
  • A tuple in a union pattern tests the same: value is int|[int, string] pair, and a match arm int|[int, string] p => …. A pattern that starts with [ stays a list pattern, so value is [int, string] keeps testing the count and each element, as it did.
  • Class<T> in is, as and an arm tests that the value is a class value of T or of a class that extends or implements T: typeof(RushOrder) is Class<Order> is true, typeof(Invoice) is Class<Order> is false, "hello" is Class<Order> is false, and an object is no class value.
  • A class value is the string that names its class, as PHP's Order::class is. So a string that names RushOrder is a Class<Order>, as the engine's parameter check already takes it.
  • Object in is and as tests that the value is an object.
  • Each test narrows to the type written, so pair as [int, string] is a [int, string]? that unpacks.

Checker

  • sharp/mago/crates/syntax/src/utils/pattern.rs: is_engine_tested takes Object, every generic type, Class<T> included, and a tuple. Their test is instanceof a name spanning the type, which the bridge lowers to SHARP_IS_TYPE, whose sharp_type_holds already tests a tuple, a class value and an object. The engine is unchanged.
  • sharp/mago/crates/analyzer/src/expression/mod.rs: analyze_php_shape records the type a tuple or an Object test reads.
  • sharp/mago/crates/semantics/src/internal/checker/sharp.rs: the as refusal fires only for a union that holds a type outside the slice, as int|null. Its note and the pattern note name every type a test takes.
  • docs/spec.md section 21 gains the checked examples tuple-tests and class-value-tests.

Tests

type_tests.phpt and type_tests_preload.phpt run the same 24 rows without and with preloading: a tuple union that matches, a wrong count, a wrong element type, an int in the union, as a tuple that unpacks, as on a wrong count and a wrong element type, as a union holding a tuple on an int and on a string, a match arm with a tuple union that matches and one that misses, is Class<Order> on a subclass, an unrelated class, a plain string, an object and a string that names the subclass, as Class<Order> on a subclass, an unrelated class and a plain string, a match arm of Class<Order>, int|Class<Order>, and Object on an object, a class value and an int. Both pass without opcache, with the file cache, with shared memory twice in one process, with each optimizer pass alone, and under the tracing and the function JIT.

The semantics, analyzer and bridge tests gain a test each, and three tests that used Class<T> as their refused type now use Class<T>|null.

Not in this PR, after v1

  • x is Self and x is void in a pattern say "This type is not supported yet", where as Self and as void give the permanent errors.
  • A list pattern [int, int] tested on a [int, string] says "This element is a never in a [int, string]", naming never for string.
  • A refused as, as value as int|null, adds "Redundant == comparison: `` is never null." with an empty name on its next null check.
  • == on a value whose type holds a tuple, as int|[int, string] with int, is "== on a collection is not supported yet".
  • A parameter type that starts with a tuple in a union, as [int, string]|int held, is a bare parse error, and so is value is [int, string] pair.
  • In a pattern, value is [int, string]|int reads as (value is [int, string]) | int.

Problem

Spec section 21: "Every type answers `is`, `as` and `match` the same way,
function types included." The semantic check refused `as [int, string]`,
a tuple in a union pattern such as `int|[int, string] p`, every test of
`Class<T>`, and every test of `Object`, as "This type is not supported
yet in PHP#." `value is [int, string]` already worked, since a pattern
that starts with `[` is a list pattern.

Solution

The engine's `sharp_type_holds` already tests each of these types: a
tuple by its count and each element, `Class<T>` by the class a string
names and the classes it extends or implements, and `Object` as an
object. So only the checker's gate moves.

- `is_engine_tested` in `pattern.rs` takes `Object`, every generic type,
  `Class<T>` included, and a tuple. Their test is `instanceof` a name
  spanning the type, which the bridge lowers to `SHARP_IS_TYPE`.
- `analyze_php_shape` records the type a tuple or `Object` test reads,
  as it does for a collection, so the value narrows to the type written.
- A class value is the string that names its class, as PHP's
  `Order::class` is, so a string that names `RushOrder` is a
  `Class<Order>`, as the engine's parameter check already takes it.
- The `as` refusal now fires only for a union that holds a type outside
  the slice, as `int|null`, and its note and the pattern note name every
  type a test takes.
- Spec section 21 gains the checked examples `tuple-tests` and
  `class-value-tests`.

Zend/tests/sharp/
├── TypeTests/                 the fixture: Order, RushOrder, Invoice and the tests
├── type_tests.phpt            every row, in every engine mode
└── type_tests_preload.phpt    the same rows under opcache preloading
Problem

#168's review found seven gaps in how `is`, `as` and `match` test a
tuple and a class value.

- `"hello" is Class<Object>` and `"" is Class<Any>` were true: the engine
  let any string pass `Class<Object>`, `Class<Any>`, `Class<Any?>` and a
  `Class` of an unresolved type parameter.
- `typeof(Box<Order>) is Class<Box>` was false: the analysis fills a
  generic class's omitted type arguments with `Any?`, so the bridge wrote
  the test as `Class<App.Box<Any?>>`, which `Box<Order>` does not fit.
- `"App.RushOrder" is Class<Order>` was false: the engine looked a class
  value up only by PHP's spelling, `App\RushOrder`.
- `is [int, string]` narrowed the value to `List<Any?>`, so
  `const [count, label] = pair` and a `[int, string]?` return failed.
- `value is [int, string]|int`, `value is [int, string] pair` and the
  parameter `[int, string]|int held` were parse errors.
- `is void`, `is Self` and `as int|null` said "not supported yet".
- `value is [int, int]` on a `[int, string]` said "This element is a
  `never`".

Solution

- `sharp_class_value_is` finds the class a string names, by either
  spelling. `Class<Object>` takes the class value of a class, and
  `Class<Any>`, `Class<Any?>` and an unresolved parameter take the class
  value of a type whose every class exists. `new` of a class value reads
  both spellings too.
- The bridge writes a class whose type arguments are all unspecified as
  the class alone, as `class_value_class` already reads it.
- A list pattern of types alone records its tuple once its PHP is
  analyzed, and the `is` it belongs to narrows to that tuple. The engine
  still runs the cheap list test there, then the count and each element.
- The parser reads a tuple type with the `|` members and the name after
  it, in a pattern and in a declared type.
- `is void` and `is Self` say each is only a return type. `as int|null`
  is the `as int?` error and help, and `is int|null` the nullable
  pattern error. The `as` refusal they replaced and its test are gone.
- The list-pattern message reads the tuple's element at its index.
- Spec section 21 says a class value in PHP# words, with
  `"App.RushOrder"`.

Zend/tests/sharp/
├── TypeTests/Box.sharp        the generic class `Class<Box>` tests
├── TypeTests/TypeTests.sharp  rows 24 to 37, one or more per change
├── type_tests.phpt            every row, in every engine mode
└── type_tests_preload.phpt    the same rows under opcache preloading
@heyJordanParker
heyJordanParker merged commit f322f1d into master Oct 18, 2026
24 checks passed
heyJordanParker added a commit that referenced this pull request Oct 18, 2026
…d beside the permanent refusals

Problem

#159, #165, #168, #170 and #171 landed on master. #159 gives attributes
to interfaces, events, constants, accessors and lambdas, #168 gives
every pattern type a test, and the collections work builds tuple and
`Iterable` constants. #166's refusals of those forms as "not supported
yet" are now refusals of code that compiles. `==` on an `Iterable` was
still "not supported yet", and a `.sharp` file the engine refused stayed
in PHP's included files, so a second `require_once` skipped it and a
later use said "Class not found".

Solution

The merge keeps master's attribute arms, its `as` arm and its
attribute-argument refusal, keeps `report_accessor` with #159's
observer-attribute arm and its `ACCESSORS` note, and drops the arms
master made unreachable: `is_undecided`'s attribute lists, pattern
types with no test and tuple constants.

`==` on an `Iterable`, or on a type parameter bound by a collection,
is a permanent error that names `toList()`: "`==` compares a `List`, a
`Map` or a `Set`, and an `Iterable` is none of them: compare
`a.toList() == b.toList()`." A search and a struct property say the
same.

A `.sharp` file the engine refuses leaves PHP's included files, so a
second `require_once` reports the same refusal.

Spec section 20's `case-set-field` states its result, `// true`, which
the spec tool now runs.

php-sharp/
├── Zend/tests/sharp/refusal_required_again.phpt     a refused file is required again
├── docs/spec.md                                      case-set-field gives true
├── ext/sharp/sharp.c                                 sharp_compile_file drops a refused file
└── sharp/mago/crates/
    ├── analyzer/
    │   ├── src/expression/binary/comparison.rs       `==` on an Iterable names toList()
    │   └── tests/sharp.rs                            its rows, and a row that compiles toList()
    ├── hir/src/lower/error/mod.rs                    both sides' error list
    └── semantics/
        ├── src/internal/checker/sharp.rs             is_undecided, report_accessor, the as arm
        └── tests/sharp.rs                            rows for what master builds
@heyJordanParker
heyJordanParker deleted the fix/type-tests branch November 3, 2026 22:08
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.

1 participant