Repository navigation
feat: a tuple, a class value and Object are tested like any other type - #168
Merged
Merged
Conversation
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
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
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.
value as [int, string], a tuple in a union pattern such asvalue is int|[int, string] pair, everyis,asandmatcharm ofClass<Order>, and every test ofObjectwere refused as "This type is not supported yet in PHP#." Spec section 21 says every type answersis,asandmatchthe same way, and v1 leaves no "not supported yet".What a PHP# developer can now write
asa tuple gives the value when it has the tuple's count and each element's type, and null otherwise:pair as [int, string].value is int|[int, string] pair, and amatcharmint|[int, string] p => …. A pattern that starts with[stays a list pattern, sovalue is [int, string]keeps testing the count and each element, as it did.Class<T>inis,asand an arm tests that the value is a class value ofTor of a class that extends or implementsT: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.Order::classis. So a string that namesRushOrderis aClass<Order>, as the engine's parameter check already takes it.Objectinisandastests that the value is an object.pair as [int, string]is a[int, string]?that unpacks.Checker
sharp/mago/crates/syntax/src/utils/pattern.rs:is_engine_testedtakesObject, every generic type,Class<T>included, and a tuple. Their test isinstanceofa name spanning the type, which the bridge lowers toSHARP_IS_TYPE, whosesharp_type_holdsalready tests a tuple, a class value and an object. The engine is unchanged.sharp/mago/crates/analyzer/src/expression/mod.rs:analyze_php_shaperecords the type a tuple or anObjecttest reads.sharp/mago/crates/semantics/src/internal/checker/sharp.rs: theasrefusal fires only for a union that holds a type outside the slice, asint|null. Its note and the pattern note name every type a test takes.docs/spec.mdsection 21 gains the checked examplestuple-testsandclass-value-tests.Tests
type_tests.phptandtype_tests_preload.phptrun 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,asa tuple that unpacks,ason a wrong count and a wrong element type,asa union holding a tuple on an int and on a string, amatcharm 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, amatcharm ofClass<Order>,int|Class<Order>, andObjecton 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 useClass<T>|null.Not in this PR, after v1
x is Selfandx is voidin a pattern say "This type is not supported yet", whereas Selfandas voidgive the permanent errors.[int, int]tested on a[int, string]says "This element is aneverin a[int, string]", namingneverforstring.as, asvalue as int|null, adds "Redundant==comparison: `` is nevernull." with an empty name on its next null check.==on a value whose type holds a tuple, asint|[int, string]withint, is "==on a collection is not supported yet".[int, string]|int held, is a bare parse error, and so isvalue is [int, string] pair.value is [int, string]|intreads as(value is [int, string]) | int.