Repository navigation
feat: structs, and Map and Set as engine objects (spec section 10) - #123
Conversation
Problem No part of spec section 10 exists. On mago-sharp master c3a2b5a, `struct`, `readonly struct`, an object initializer and `with` are parse errors, `required` on a property is "not supported yet", and `[Key]` is an unknown attribute. Section 10's four Guard 1 entries fail on those parse errors, and examples in sections 12, 16, 23 and 24 lean on structs too. No design says how a struct lowers to PHP the engine runs, or how plain PHP is kept from changing one. Solution A design note for team-lead's approval, in the shape of the section 15 note. It lists every rule of section 10 and every other example that uses a struct, each with its mago compile output today, the PHP it lowers to (run on the slot 7 engine), and its mechanism in the checker, the bridge and the engine. A struct is a final class whose properties lower as a class's do, == runs a generated op_Equality, with is PHP 8.5's clone with values, and parse is generated per struct from the checker's types. It proposes the standard-library choices for parse with their precedent, lists six spec questions for Jordan, and orders 4 mago-sharp and 4 php-sharp PRs against #166, #454, generics R3c and the collection Slice's engine PR. docs/ └── architecture/ └── slices/ └── 10-structs.md <- new: the section 10 design note
Problem The structs note planned 4 mago-sharp and 4 php-sharp PRs, and refused laws on a struct. Jordan's direction is that a Slice lands in as few PRs as it can, because each PR is another full round of suites, review and merge, and team-lead approved the note with rulings that change three parts of it: laws on a struct, collection ==, and who adds the harness files. Solution "The PRs, in order" now plans one mago-sharp PR and one php-sharp PR for all of section 10. The mago-sharp PR opens after #166 merges, and the php-sharp PR follows the collection Slice's engine PR. A new "Rulings" section records team-lead's Queue 2. Language rule 12 now lets a struct state laws, collection == joins the mago-sharp PR, the harness's Username and OrderRow move to team-lead's spec PR, and the tests gain a struct law row and collection == rows. docs/ └── architecture/ └── slices/ └── 10-structs.md <- Rulings; one PR per repo; struct laws; collection ==
…tions 10 and 16)
Problem: `struct`, `readonly struct`, `with` and an object initializer
were parse errors, so section 10's struct examples and section 16's
initializer could not compile.
Solution: a struct is its own node, `Struct`, recorded by the codex as a
final class with `MetadataFlags::STRUCT` and its backing type. The
bridge lowers it to a final PHP class with a generated `op_Equality`
that compares each property with its own `==`. `with` and the object
initializer share one analysis, which writes each value to the object
being created, and one lowering, a closure bound to that object.
The checker refuses `readonly struct`, `set`, `protected`, `operator ==`
on a struct, a class in a struct's header, a class extending a struct,
`===` on a struct, `==` on a struct with a property that has no `==`,
and a write to a struct after it is created, naming a method that
returns a changed copy. Plain PHP sees an `init` property as the
`protected(set)` it runs as.
crates/
├── syntax/src/
│ ├── cst/cst/struct.rs, with.rs <- new: Struct; With, ObjectInitializer, NamedValue
│ ├── cst/cst/{expression,instantiation,statement,mod}.rs, cst/node.rs, walker/mod.rs
│ │ <- the new nodes
│ └── parser/internal/
│ ├── struct.rs <- new: is_at_struct, parse_struct_with_attributes_and_modifiers
│ ├── instantiation.rs, expression.rs <- the initializer after `new`, and `with`
│ └── class_like/mod.rs <- parse_class_like_members, shared by every class-like
├── names/src/internal/walker.rs <- a struct's members bind as a class's
├── codex/src/
│ ├── metadata/{flags,class_like}.rs <- MetadataFlags::STRUCT, struct_properties
│ ├── scanner/{mod,class_like,property}.rs <- a struct as a final class; operator ==, parse, tryParse
│ └── ttype/mod.rs <- a backed struct keys a Map
├── semantics/src/internal/ <- check_struct and the struct declaration rules
├── analyzer/src/
│ ├── statement/class_like/struct.rs <- new: a struct's header
│ ├── expression/with.rs <- new: with and the object initializer
│ ├── expression/binary/comparison.rs <- == and === on a struct
│ ├── resolver/property.rs, visibility/mod.rs
│ │ <- a struct never changes; init written while created
│ └── code.rs <- invalid-struct, missing-required-property
├── fingerprint/src/ <- Struct
└── sharp-bridge/src/lower.rs, lower/types.rs <- r#struct, struct_equality, instantiation, with, bound_to
composer/src/Sdk/Syntax/NodeKind.php <- regenerated
Problem: `==` on two lists or two maps said "not supported yet", so a
struct holding a `List` could not compare at all, and a search of a
`List<List<int>>` was refused the same way. A value pattern ran `===`,
so `match (point) { origin => … }` never matched an equal struct.
Solution: two lists compare element by element and two maps compare the
values under each key regardless of order, each element with its own
`==`. Elements that compare by `===` keep PHP's `===` on the arrays.
Other collections run `count` and `\array_all` with the element's `==`,
a nullable side equal only to null. A collection whose elements `==`
cannot compare is refused with the element's reason, a list never
equals a map, and a `Set` stays not supported yet. A value pattern
writes `==`, which the checker types as `===` in PHP# and the bridge
lowers from the types.
crates/
├── analyzer/
│ ├── src/expression/binary/comparison.rs <- collection_refusal, element_reason, never_equal
│ └── tests/sharp.rs <- collections_compare_by_value replaces the not-supported test
├── sharp-bridge/
│ ├── src/lower.rs <- collection_equality, compares_identically, Side::Index
│ └── tests/lower.rs <- collection ==, struct value pattern, nested search rows
└── syntax/src/utils/pattern.rs <- a value pattern compares with ==
Problem: section 10 lets `public struct Username : string` key a `Map`,
but nothing fixed which member holds the backing value, the bridge
stored only a backed enum as its value, and a loop refused a struct key
as "mixing a backed enum with other types".
Solution: a backed struct's main constructor takes one get-only property
`value` of the backing type, else "A struct backed by `string` holds its
backing value in `public Username(public string value { get; })`."
(question 1's proposal). `backed_enum` becomes `backed`, so a struct key
goes in as `->value`, and a loop reads it back through
`new \App\Username((string) $key)`, cast for a `string` backing value
because PHP stores an all-digit key as an int. The loop check accepts
one backed struct as it accepts one backed enum.
crates/
├── analyzer/
│ ├── src/statement/mod.rs <- report_key_mixing_a_backed_type
│ └── tests/sharp.rs <- backed struct rows in the loop key test
├── semantics/src/internal/checker/
│ └── class_like/mod.rs <- holds_backing_value
└── sharp-bridge/
├── src/lower.rs <- backed, key_read_back through new
├── src/lower/types.rs <- Types::struct_backing_type
└── tests/lower.rs <- backed struct key test
Problem: section 10 gives every struct a static `parse`, which reads a
`Map<string, Any?>` or an object's public properties and throws one
error listing every bad field, and a `tryParse` that gives null. The
codex typed both, but the bridge wrote no body, `[Key]` renamed
nothing, and a struct holding an enum without a backing value or a
function had no refusal.
Solution: the bridge writes `parse` and `tryParse` per struct
(decision 5) from the fields the codex gives: the main constructor's
parameters, then each property an object initializer may set
(question 3's proposal). Each field is read from its key, `[Key]`'s
when written, and checked as its type: a scalar, an `int` into a
`float`, a backed enum from its value, a nested struct through its
own `parse` with its problems under the field's path, a `List`, `Map`
or `Set` element by element, a class instance as it is, `Any?` as it
is, and a union as its first member that reads it. Every problem
lands in one `\Sharp\InvalidFields`, so the spec's
`customerId: expected int, got string 'abc'; plan_code: missing`
comes out to the byte on the slot 7 engine. The struct is created
through its main constructor with the arguments spread by name, so a
missing key takes its default. The analyzer refuses `[Key]` off a
struct's main constructor and two fields read from one key (language
rule 10), and refuses `X.parse` when a field holds an enum without a
backing value or a function, also nested (rule 11).
crates/
├── codex/src/metadata/
│ ├── class_like.rs <- ParsedField
│ └── mod.rs <- CodebaseMetadata::struct_parse_fields
├── analyzer/src/
│ ├── expression/call/static_method_call.rs <- rule 11 at the call
│ ├── statement/attributes/mod.rs <- rule 10, `[Key]`'s place
│ └── statement/class_like/struct.rs <- rule 10's keys, check_struct_parse
└── sharp-bridge/
├── src/lower.rs <- struct_parse, struct_try_parse, parse_value and its reads
├── src/lower/types.rs <- is_struct, backing_type, struct_parse_fields, array_parameters
└── tests/lower.rs <- parse's frame and each field kind; tree_without
…efused whole
Problem: ruling 3 gives a struct laws as section 28 gives a class, but
sharp-lean translated only classes and enums, so a struct's law was
never proven or refused. A generic struct, `struct Box<T>`, was a run
of parse errors, and question 4 refuses it whole until generics R3c
can read `T` while `parse` runs.
Solution: sharp-lean translates a struct through the class path, as
the `final` class it lowers to, and the structure-rule facts declare
it as a class. The parser reads a struct's type parameters, nested
angle brackets included, as one "A generic struct is not supported
yet in PHP#." error, and parses the header and members after them.
crates/
├── sharp-lean/src/
│ ├── translate/mod.rs <- Translator::class takes a struct's name, header and members
│ └── facts.rs <- walk_in_struct declares a class
└── syntax/
├── src/parser/internal/struct.rs <- type parameters refused whole
└── tests/sharp.rs <- a_generic_struct_is_one_not_supported_error_and_the_struct_parses_on
tests/compile.rs <- a_struct_states_a_law
Problem: Jordan answered question 1: any struct keys a `Map` by its
whole value, with no declaration, and the `: string` struct header is
gone. The branch still parsed a backing type on a struct, checked the
`public Username(public string value { get; })` shape, stored a struct
key as its `->value` and read it back through the main constructor.
All of it encodes the rejected shape.
Solution: the `Struct` node loses `backing_type_hint`, and its header
parses interfaces only. A type such as `string` there is one parse
error at the type, "A struct's header lists the interfaces it
implements: a struct keys a `Map` by its whole value.", and the
interfaces after it still parse. The codex registers no backing type
for a struct. The backing-value shape check, the struct arms of the
bridge's key write and loop read-back, and the analyzer's key-mixing
wording go back to enums only. Struct keys come back with their own
key text once that design is ruled.
crates/
├── syntax/
│ ├── src/cst/cst/struct.rs <- backing_type_hint removed
│ ├── src/cst/node.rs, src/walker/mod.rs <- no backing child
│ ├── src/error.rs <- ParseError::StructHeaderTypeInSharp
│ ├── src/parser/internal/struct.rs <- parse_struct_header
│ └── tests/sharp.rs <- a_struct_header_lists_interfaces_only
├── hir/src/lower/error/mod.rs <- the new parse error
├── fingerprint/src/class_like/mod.rs <- no backing in a struct's fingerprint
├── codex/src/
│ ├── scanner/class_like.rs <- register_struct passes no backing type
│ └── metadata/flags.rs <- STRUCT's doc
├── semantics/src/internal/checker/class_like/mod.rs <- backing checks removed
├── analyzer/src/statement/
│ ├── mod.rs <- key mixing names a backed enum again
│ └── class_like/struct.rs <- rule 3's note
├── analyzer/tests/sharp.rs <- the key-mixing test's struct rows removed
└── sharp-bridge/
├── src/lower.rs, src/lower/types.rs <- backed_enum, struct_backing_type removed
└── tests/lower.rs <- the backed struct key test removed
Problem: a property written `required` was "not supported yet", and a
struct created without one of its properties compiled: `new Point { x:
1 }` left `y` unset until the engine threw on the first read. Jordan's
Q2 rules that a struct's property without an initial value is set at
creation, a `T?` one too. Task #463: a get-only property that no
constructor sets compiled.
Solution: `MetadataFlags::REQUIRED` marks a property written `required`,
and a struct's property without an initial value that is not a main
constructor parameter. Neither takes a `null` default. The codex scanner
records on each constructor, main or named, the properties it assigns on
every path, as `MethodMetadata::assigned_properties`: both arms of an
`if`/`else`, every `match` arm, and a path that throws counts as done. A
call to a helper method counts for nothing, as in C# and Swift. At each
`new`, the required properties that the constructor running (a named
constructor together with the main constructor) does not assign must be
named by the object initializer: "`new Point` must set `y` and `label`."
A `required` property without `init` or `set` is refused where it is
written. A get-only property with no initial value that no constructor
assigns is an error: "`count` is get-only and never set: set it in the
main constructor, or give it an initial value."
crates/
├── syntax/
│ ├── src/parser/internal/class_like/method.rs <- `required` goes on a property
│ ├── src/cst/cst/with.rs <- doc paragraphs clippy asks for
│ └── tests/sharp.rs <- `required` rows name a method
├── codex/src/
│ ├── metadata/flags.rs <- MetadataFlags::REQUIRED
│ ├── metadata/function_like.rs <- MethodMetadata::assigned_properties
│ ├── scanner/property.rs <- `required` sets REQUIRED, no null default
│ ├── scanner/class_like.rs <- require_struct_properties
│ ├── scanner/function_like.rs <- definitely_assigned_properties
│ └── scanner/mod.rs <- a named constructor's assigned set
├── semantics/
│ ├── src/internal/checker/sharp.rs <- `required` needs `init` or `set`
│ └── tests/sharp.rs <- a_required_property_has_init_or_set
├── analyzer/
│ ├── src/expression/instantiation.rs <- check_required_properties at `new`
│ ├── src/statement/class_like/initialization.rs <- check_get_only_initialization
│ ├── src/statement/class_like/mod.rs <- runs it
│ └── tests/sharp.rs <- every_required_property_is_set_where_its_object_is_created,
│ a_get_only_property_no_constructor_sets_is_an_error
└── sharp-bridge/tests/lower.rs <- fixtures set their get-only properties;
parse reports a missing `owner`
Problem: Jordan's question 1, ruled in Queue 4: any struct is a Map key
by its whole value, and two keys are one key when the struct's == says
so. The checker refused every struct key, as "A `Map`'s keys are `int`,
`string` or a type with an `int` or `string` backing value, and `Spot`
has none." Queue 7 adds that a struct holding a class instance, at any
depth, can't key a Map, since a class's own == matches no text.
Solution: the codex sees a struct key as a string, the key text the
engine stores it under, so a Map type, an index and a literal take a
struct key. The analyzer refuses a struct holding a class, an
interface, a function or Any where a Map or Set type is written and
where a literal's key infers one: "`Order` can't key a `Map`: `plan` is
a `Plan`, a class, and a key holds only values." A loop over a Map
keyed by one struct reads its keys back, and one mixing a struct with
other key types is refused, as a mixed enum key is. The bridge gives
each struct the engine interface \Sharp\Struct, writes a struct key as
\Sharp\Map::key($struct) and reads a loop key back with
\Sharp\Map::readKey($key). A .sharp class can't implement Sharp.Struct.
crates/
├── codex/src/ttype/mod.rs <- get_backing_key_type: a struct key is a string
├── analyzer/
│ ├── src/statement/class_like/struct.rs <- report_class_held_by_key, held_class
│ ├── src/statement/function_like/mod.rs <- Map and Set types refuse a struct holding a class
│ ├── src/expression/array.rs <- so does a literal's key
│ ├── src/statement/mod.rs <- report_key_mixing_a_stored_key
│ └── tests/sharp.rs <- a_struct_keys_a_map_unless_it_holds_a_class_instance,
│ a_sharp_class_cannot_implement_the_struct_marker,
│ the loop and map key rows
└── sharp-bridge/
├── src/lower.rs <- \Sharp\Struct, stored_key and key_read_back for structs
└── tests/lower.rs <- a_struct_key_goes_in_as_its_key_text_and_a_loop_reads_it_back
Problem: Jordan's answer to question 1 (team-lead's Queue 4) makes any
struct a Map key by its whole value. PHP refuses an object as an array
key, "Cannot access offset of type Spot on array", so the engine must
store a struct under a text two equal structs share and read the text
back as the struct. Queue 8 approved Sharp\Struct, Sharp\Map::key and
Sharp\Map::readKey with three conditions: only a lowered struct
implements Sharp\Struct, readKey builds only a struct, and one encoder
serves every key position.
Solution: Sharp\Struct is an engine interface that only a final class
compiled from a .sharp file may implement, as only an enum implements
UnitEnum. Sharp\Map::key writes the key text: the class name, then the
stored properties as JSON in declaration order, a nested struct by its
own text, an enum case as Class::Case, an array in key order, and the
shortest round-trip float with -0.0 as 0.0. Sharp\Map::readKey reads it
back without the constructor, checks each property's type as an
assignment does, and throws ValueError for any other text, a class that
is no struct, or nesting past 512. Sharp\Set::from and the groupBy and
associateBy slot write through the same sharp_struct_key. The note takes
team-lead's Queues 3 to 9, and the harness gains Username and OrderRow.
ext/sharp/sharp.c <- Sharp\Struct, Sharp\Map::key and readKey, the key text encoder and
reader, the struct arms of sharp_collection_key_slot and sharp_set_key
Zend/tests/sharp/
├── Spot.sharp, Tone.sharp, Route.sharp, <- new: the key fixtures, one type per file
│ Boxed.sharp, StructKeys.sharp
├── struct_keys.inc, struct_keys.phpt <- new: one key per equal value, the read back, every value kind's round
│ trip, and every refusal
├── struct_marker.phpt <- new: a plain PHP class can't implement Sharp\Struct
└── set_methods*.phpt <- Set::from's message names a struct
sharp/harness/app/
├── Username.sharp <- new (Queue 9)
└── OrderRow.sharp <- new (Queue 9)
docs/architecture/slices/10-structs.md <- Queues 3 to 9: the whole-value key, its text, the NaN gap, the JSON
toString rule for the JSON Slice, and the dependencies
…s one error Problem: section 10's language rules ran with no test. The checker's refusals of `readonly struct` and other modifiers, a `set` accessor, a member modifier only a subclass reads, `operator ==`, a write after creation, a class in a struct's header, a class extending a struct, `===` and `==` on a struct, `with` and the object initializer's writes, and the `parse` rules each had no row, and neither had the `with` and initializer lowering or their parse. A class extending a struct also gave a second error, "`Child`'s constructor must call `Name`'s with `: super(…)`.", for the struct it may not extend. Solution: one test per rule, its forms as rows, each help's fix compiled. `check_super_call` skips a struct base, whose inheritance `check_class_like_extends` already refuses. crates/ ├── analyzer/ │ ├── src/statement/class_like/method.rs <- check_super_call: no super call into a struct │ └── tests/sharp.rs <- a_struct_never_changes, a_struct_inherits_nothing_and_implements_interfaces, │ a_struct_compares_by_value, │ with_and_an_object_initializer_set_init_and_set_properties, │ every_struct_parses_and_classes_do_not ├── semantics/tests/sharp.rs <- a_struct_takes_only_public, a_struct_property_takes_init_not_set, │ a_struct_member_is_private_where_a_class_would_be_protected, │ a_struct_declares_no_operator_equality ├── syntax/tests/sharp.rs <- with_copies_the_object_before_it_with_the_values_it_names, │ an_object_initializer_follows_new └── sharp-bridge/tests/lower.rs <- with_and_an_initializer_run_in_a_closure_bound_to_the_object
…te path Problem: the note's test list named tests the plan proposed, not the ones mago-sharp's branch holds, and decision 4 described plain writes and `clone` inside the class, while the bridge runs an object initializer and `with` through one bound closure everywhere. Solution: the test list names each test on mago-sharp's feat/slice-structs at f13fe23 with what it proves, and decision 4 states the one path with why: an initializer's writes are statements inside an expression, and `clone` alone measured no faster than the closure. docs/architecture/slices/10-structs.md <- decision 4; "The tests"
Problem: team-lead's Queue 10 fixes generics R3c's walk, which PHP#'s every check of a value against a type calls: \Sharp\Internal\accepts(string $type, mixed &$value, string $path, array &$problems): bool. `parse` checked each type with its own generated code and wrote each problem through \Sharp\InvalidFields::expected, a second check beside the walk. The walk writes the problem text, and takes only a struct object where a struct is expected. Solution: `parse` reads each field into a hidden local, builds what the walk takes only as an object, then calls the walk once with the field's type text and its key as the path. A nested struct is built through its own `parse`, element by element inside a collection, and its problems move under the field's path, where the walk's own problem for it is dropped. A union of structs takes the first that builds. A backed enum case is built from its backing value. Once the walk accepts a value, each `Set` in it holds its elements under their keys. The per-type readers, the key check and the problem text go, since the walk does all three. `Types::type_text` writes decision 72's type text for the types a struct holds; generics R1 and R2 (mago-sharp #152) spell every other type, and the two meet where #152 merges. crates/sharp-bridge/ ├── src/lower.rs <- parse_field, parse_build, parse_nested, parse_sets; parse_value, parse_atomic, │ parse_enum, parse_struct, parse_collection, parse_key, parse_error, type_name gone ├── src/lower/types.rs <- Types::type_text └── tests/lower.rs <- a_struct_parses_each_field_from_its_key_and_creates_itself_through_its_main_constructor, parse_reads_each_field_as_its_type: each field through the walk
… as a value
Problem: every struct's generated `parse` reads `[Key("plan_code")]`
and throws `Sharp\InvalidFields`, and neither class existed in the
standard library. The engine also had no test that a struct behaves
as a value: a changed copy, `==` by value, collection `==`, `with`
and the object initializer, and plain PHP unable to change or inherit
one. team-lead's Queue 10 fixed R3c's walk as the one type check, so
the walk writes every problem text and `InvalidFields.expected` has no
caller.
Solution: `Sharp.Key` is the attribute the spec names. `Sharp.InvalidFields`
is a `ValueError` holding `fields`, path to problem, whose message joins
them with `; `, and has no `expected`. `structs.phpt` runs the struct
value rules in every opcache mode and both JITs. The note records
ruling 20, and decision 5 and standard-library choice 2 read through
R3c's `\Sharp\Internal\accepts`.
php-sharp
├── sharp/composer/library/Sharp/
│ ├── Key.sharp <- new: [Key("plan_code")]
│ └── InvalidFields.sharp <- new: InvalidFields : ValueError, with its fields
├── Zend/tests/sharp/
│ ├── Stamp.sharp <- new: a struct with an init body and a with method
│ ├── Structs.sharp <- new: creates, copies and compares Stamps
│ ├── structs.inc <- new: 100 rounds, then plain PHP's write, clone and subclass
│ └── structs.phpt <- new
└── docs/architecture/slices/10-structs.md
<- ruling 20; decision 5, choice 2 and the parse section through
the walk; the structs test's fixtures
Problem: team-lead's Queue 11 rules that `parse` tries the structs of a
union in the order the source writes them, as serde's untagged enums
try their variants. `parse` took them in the order of the checker's
union, which `TUnion::from_vec` sorts. So `Point|Address` built an
`Address` from an input that fits both, as `Address|Point` does.
Solution: `parse` reads each field's type as the struct writes it,
from its main constructor parameter or its property, and orders a
union's structs by it. A collection's element type comes from the
written `List`, `Set` or `Map`, so `Map<string, Point|Address>` tries
`Point` first too. Run on the slot 8 engine with an input that fits
both: `Address|Point` built an `Address`, while `(Point|Address)?` and
`Map<string, Point|Address>` built a `Point`.
crates/sharp-bridge/
├── src/lower.rs <- struct_parse takes the Struct; parse_field and parse_build take the written
│ hint; written_hint, written_members, element_hint
└── tests/lower.rs <- parse_reads_each_field_as_its_type: `(Point|Address)?` and
`Map<string, Point|Address>` beside `Address|Point`
Problem: team-lead accepted Queue 10 with three rulings: problem text names a class by its full dotted name, a union of structs is tried in the order the source writes it, and the bridge keeps one type_text writer. The note did not record them, and decision 5 still said a union of structs takes the first that builds without saying in which order. Solution: ruling 21 records the three rulings. Decision 5 says a union of structs is tried in written order, read from the field's written type, because the checker's union holds its members sorted. The parse section's run adds the row whose input fits both structs. php-sharp └── docs/architecture/slices/10-structs.md <- ruling 21; decision 5's union order; the source-order run
…ery Map method Problem: #166 gave `Map` its `has`, `get`, `remove` and `keys()`, and refused a key type that mixes a backed enum with other types wherever a key goes in or comes out: the loop, `keys()`, the methods, an index, a literal, `groupBy` and `associateBy`. It also checks a `groupBy` or `associateBy` key by spec section 12's rule instead of the `array-key` bound. This branch keys a `Map` by a struct's key text, so each of those positions must take a struct key as it takes a backed enum, and a struct that holds a class instance can now reach `groupBy`, where team-lead's Queue 7 refuses it. Solution: the shared check is `keys_mix_a_stored_key`, and one class of keys passes it, one backed enum or one struct, so `Map<Spot|string, int>` is refused at every position and `Map<Spot, int>` reads and writes everywhere. `key_read_back`, which the loop and `keys()` share, reads a struct key back through `\Sharp\Map::readKey`, and `has`, `get` and `remove` write it through `\Sharp\Map::key` as an index does. A `groupBy` or `associateBy` key that is a struct holding a class instance is refused with Queue 7's message, and its `Map` takes the error key, as a key that keys no `Map` does. `map_key_error` keeps #166's "may be null" and names the struct. Run on the slot 8 engine: `groupBy` and `associateBy` by a struct key store its key text, a loop and `keys()` read each key back as the struct, `has` and `get` find an equal struct, and `remove` drops it. crates/analyzer/src/ ├── statement/mod.rs <- keys_mix_a_stored_key, key_type_mixes_a_stored_key: one enum or │ one struct; the struct in report_keys_sharing_entries' text ├── statement/function_like/mod.rs <- map_key_error: #166's reason with the struct wording ├── invocation/return_type_fetcher.rs <- a groupBy or associateBy key holding a class instance is refused └── expression/{array,array_access,assignment/array_assignment,call/method_call}.rs <- the renamed check crates/sharp-bridge/src/lower.rs <- key_read_back takes #166's signature and keeps the struct arm crates/analyzer/tests/sharp.rs <- a_struct_keys_a_map_unless_it_holds_a_class_instance: groupBy rows; a_map_key_type_that_is_no_value_is_an_error: #166's rows; a_map_key_type_that_mixes_a_backed_enum_or_a_struct_with_other_types_ is_refused_where_a_key_goes_in_or_out: a struct at every position crates/sharp-bridge/tests/lower.rs <- a_struct_key_goes_in_as_its_key_text_and_a_loop_reads_it_back: has, get, remove and keys()
…rflow Problem: `==` on a struct that holds itself, as `Node? next` or `List<Tree> branches`, crashed `mago compile` with a stack overflow. `struct_property_refusal` keeps the structs it is checking, and stops at one it meets again, but it checked each property's type through `sharp_refusal`, which started that list afresh, as `collection_refusal` did for a list's elements. PR #174's patch-coverage run found 60 struct lines no test ran; a probe for one of them hit the crash. The rest were rules with no row, arms no program reaches, and a backing-type arm left from before a struct's header named interfaces only. Solution: `refusal` is `sharp_refusal` inside the structs being checked, and the struct and collection branches pass that list on, so a struct compares by the rest of its properties. Every rule the run found unrun has its row: each property `==` can't compare and why, a list of structs or of lists, a search of structs, a `new` or `with` of no struct, a header naming no type, a struct declared twice, a struct's constant, named constructor, law, attribute and `self`, and `parse` of a function, an `Any`, a `bool`, an `int`-backed enum, a pure enum, a list of lists of `Set`s and `Point|List<Point>`. The arms no program reaches are deleted: a named `Closure`, which PHP# writes as a function type, a named enum, which the codex gives as an enum, `Object`, which PHP# has no type for yet, and a struct's backing type. An impossible arm is an `unreachable!` with its reason. The struct property message takes "an" before a vowel, as "`data` is an `Any`". On the slot 8 engine, `==` on equal chains of `Node` and trees of `Tree` gives true, and on chains or trees that differ in a value, a `null` or a depth gives false. crates/analyzer/src/ ├── expression/binary/comparison.rs <- refusal threads `visiting` through structs and collections; │ struct_property_issue's article ├── statement/class_like/struct.rs <- analyze_struct, as analyze_event; report_class_held_by_key reads │ the structs a key names; the Closure and named enum arms deleted └── statement/class_like/initialization.rs <- a declared property's name span is unreachable! when missing crates/semantics/src/internal/checker/sharp.rs <- the struct backing-type arm deleted; supported's struct note is unreachable!, so it is no const fn crates/sharp-bridge/src/lower/types.rs <- the Object arm deleted crates/analyzer/tests/sharp.rs <- a_struct_compares_by_value, collections_compare_by_value, a_search_needs_equality_on_the_element_type, every_required_property_is_set_where_its_object_is_created, with_and_an_object_initializer_set_init_and_set_properties, a_struct_inherits_nothing_implements_interfaces_and_is_declared_once, a_struct_keys_a_map_unless_it_holds_a_class_instance, every_struct_parses_and_classes_do_not: the rows crates/semantics/tests/sharp.rs <- a_struct_takes_only_public, a_struct_member_is_private_where_a_class_ would_be_protected, self_names_the_class_and_self_where_it_goes crates/sharp-bridge/tests/lower.rs <- parse_reads_each_field_as_its_type: six field types; a_struct_is_a_final_class_whose_op_equality_compares_each_property: a header interface
… mutant has a row Problem: PR #174's mutants run left 17 mutants no test caught. One was a real defect: a struct's `protected static Size operator +` got two refusals, "A struct has no subclasses, so write `private`." and "An operator is `public static`". The first fix doesn't compile, because `check_operator` refuses every operator modifier but `public static`. The other 16 were rules with no row that fails when the rule breaks: a struct's other static method, a struct field nested in a struct, a struct that holds itself in `parse`, an abstract class's get-only property, a computed property of a key struct, a struct's `self` and duplicate member, a struct's property field and a method parameter named like a field in `parse`, and a struct two collections deep. Solution: the struct member check reads no operator's modifiers, so `check_operator` alone refuses them. Each mutant has a row that fails under it. Three mutants are equivalent: `ZEND_ACC_PUBLIC | ZEND_ACC_STATIC` as `^` gives the same flags, as the two are distinct bits. crates/semantics/src/internal/checker/sharp.rs <- check_struct_declaration reads no operator's modifiers crates/analyzer/tests/sharp.rs <- every_struct_parses_and_classes_do_not: Mind, Chain and Feeling.calm; a_get_only_property_no_constructor_sets_is_an_error: an abstract class; a_struct_keys_a_map_unless_it_holds_a_class_ instance: a computed property crates/semantics/tests/sharp.rs <- a_struct_member_is_private_where_a_class_would_be_protected: a protected operator; self_names_the_class_and_self_where_it_goes: the struct first; a_member_defined_twice_and_its_class_are_named_ as_sharp_writes_them: a struct crates/sharp-bridge/tests/lower.rs <- parse_reads_each_field_as_its_type: a property field, a method parameter named like a field, and List<List<Point>>
…ializer sets, and each struct error is reported once
Problem: review-mago-174 found three crashes and eight wrong results.
`mago compile` panicked on any struct that holds a function, as the
bridge wrote a `parse` it can't type. `mago analyze` panicked on a
struct beside another type in a search, and on `<`, `<=>` and
`sortedBy` over a struct, as the refusal text kept only class parts.
`parse` read a private field and demanded a key the main constructor
sets. A main constructor parameter named like the property it sets was
refused, and a bare `[Key]` was not found. A private or get-only struct
property with no value was reported at the `new` with a fix that doesn't
compile, `readonly struct` gave a false "never set", an unknown header
type called a struct a class, and a generic struct was reported three
times.
Solution: the bridge writes `parse` and `tryParse` only for a struct
whose every field has a type text, so a struct with a function field
has none, and the checker refuses the call. `==` on such a struct says
"`Spot` can't be compared: `onMove` is a function." Every refusal text
builder selects a class or a struct part, and a search of a struct
beside another type gets `==`'s different-types refusal. `parse` reads
the main constructor's parameters, then each publicly writable
property whose key no parameter reads, and a property the main
constructor sets is optional. `Key` is a standard name. Only a struct's
public `init` property is required at the `new`. Any other with no
value is reported once where it is declared. An `init` property is
never "never set". The header error names a struct a struct, in PHP#
words, and a generic struct keeps no members, so it is one error.
crates/analyzer/src/
├── expression/binary/comparison.rs <- refusal parts by Comparand::is_instance; a struct search's
│ different-types refusal; a function property's message
├── expression/instantiation.rs <- the required note names a public `init` property
├── statement/class_like/initialization.rs <- a struct's unrequired property is never set; `init` is set
└── statement/class_like/mod.rs <- "Struct `Odd` cannot implement unknown type"
crates/codex/src/
├── metadata/mod.rs <- struct_parse_fields: public write, a key once, constructor-set
│ properties optional
└── scanner/class_like.rs <- require_struct_properties: public write only
crates/names/src/internal/walker.rs <- `Key` resolves to Sharp\Key
crates/sharp-bridge/src/lower.rs <- struct_parse is None for a struct with a function field
crates/sharp-bridge/src/lower/types.rs <- type_text is None for a function; the unreachable! arm deleted
crates/syntax/src/parser/internal/struct.rs <- a generic struct keeps no members
crates/analyzer/tests/sharp.rs <- a_struct_compares_by_value, a_search_needs_equality_on_the_
element_type, every_struct_parses_and_classes_do_not,
every_required_property_is_set_where_its_object_is_created,
a_get_only_property_no_constructor_sets_is_an_error,
a_struct_inherits_nothing_implements_interfaces_and_is_declared_once
crates/sharp-bridge/tests/lower.rs <- a_struct_that_holds_a_function_has_no_parse (new),
parse_reads_each_field_as_its_type,
a_struct_is_a_final_class_whose_op_equality_compares_each_property
crates/syntax/tests/sharp.rs <- a_generic_struct_is_one_not_supported_error_and_the_struct_parses_on
…structs keep apart Problem: #170 added `MetadataFlags::EVENT` at `1 << 48`, the bit this branch's `STRUCT` held, so after a plain merge every struct read as an event and every event as a struct, with no conflict to say so. This branch also writes a struct's `init` property at its read visibility, public, which #170's interface check reads as settable, so a class that gives an interface's `set` property with `init` stopped being refused. Solution: keep master's `EVENT` at `1 << 48`, move `STRUCT` to `1 << 49` and `REQUIRED` to `1 << 50`, so every flag holds its own bit. A struct emitted is refused as no event, and `==` on an event is refused as on any class, so each row fails if the two flags ever share a bit again. #170's interface check treats an `init` property as set only while its object is created. The other conflicts keep both sides: master's `emit`, `on`, miscased keywords and its `list`/`function` local rule, beside the struct parser, which runs before a miscased type keyword is read, as an event does. crates/ ├── codex/src/metadata/flags.rs EVENT 1<<48 (master), STRUCT 1<<49, REQUIRED 1<<50 ├── codex/src/scanner/function_like.rs both sides' imports ├── analyzer/src/statement/mod.rs master's imports, minus TObject this branch no longer uses ├── analyzer/src/statement/class_like/mod.rs report_unfit_interface_property: an `init` property is not settable ├── analyzer/tests/sharp.rs emit_takes_an_event: `emit spot;` row │ a_struct_compares_by_value: `==` on two `Moved` events row ├── hir/src/lower/error/mod.rs both parse errors └── syntax/src/ ├── error.rs StructHeaderTypeInSharp beside MiscasedKeywordInSharp ├── cst/node.rs, walker/mod.rs both sides' imports └── parser/internal/ ├── statement.rs struct arm, master's emit arm and local-declaration kinds ├── class_like/mod.rs is_at_struct before read_php_type_declaration └── class_like/method.rs refuse_required beside parse_optional_on
Problem: `plan with { label: "b" }` on an enum and `moved with { x: 2 }` on an event compiled into a `clone` that
throws at run time. The analyzer reported each set value as a get-only write instead, and the event's help, "Declare
it `{ get; init; }`", is a fix an event can't take.
Solution: `With::analyze` refuses an object whose type holds an enum or an event, master's `is_event_class`, with
"`with` copies a struct or a class, and `Plan` is an enum." or "…, and `Moved` is an event.", at the copied value.
Its values are analyzed, and no property is written. `with` on a class compiles and runs as before.
crates/analyzer/
├── src/expression/with.rs With::analyze refuses through uncopyable, which names the enum or event and its kind
└── tests/sharp.rs with_and_an_object_initializer_set_init_and_set_properties: an enum row and an event row
…ites it Problem: `compile_accepts_a_class_that_renames_an_import_to_keep_the_short_name_free` wrote the spec's struct as a class and put a project `App\Key` attribute in place of the standard one, because neither a struct nor `Sharp.Key` existed when it was written. Since a bare `Key` names the standard attribute, which `[Key]` on a class refuses, the test failed with "Attribute class `Sharp.Key` not found". Solution: the test compiles the spec's own example, a struct `Entry` whose bare `Key` is the standard attribute beside `import Cache.Key as CacheKey;`, with `Sharp\Key` declared as the vendored library declares it. tests/ └── sharp.rs compile_accepts_a_struct_that_renames_an_import_to_keep_the_short_name_free
Problem: after the merge with #170 and #173, nightly CI's `a_property_default_of_100_000_terms_is_inferred_on_a_small_stack` overflowed its 128 KiB thread. The parser grows the stack only at a guard, when less than the 100 KiB red zone is left, and the frames from `parse_statement`'s guard to `parse_expression_with_precedence`'s, through a class and its property, came to about 93 KiB in an unoptimized macOS build, with `parse_statement_inner` alone at 48 KiB. Master's `emit` arm and this branch's struct arms each passed alone; together, on CI's larger frames, the chain passed the space the first guard leaves. With a 80 KiB red zone on a 90 KiB thread, the same test overflows locally. Solution: `parse_classlike_member` runs through `ensure_sufficient_stack`, as `parse_statement` does. A member is a recursive entry point, since its body holds statements and an anonymous class's members, so the chain splits in two, about 57 and 38 KiB. The 80 KiB red zone test passes with it. Parsing is unchanged. crates/syntax/src/parser/internal/class_like/ └── member.rs parse_classlike_member guards parse_classlike_member_inner, the member's former body
… no `set` fix
Problem: `parse` left a required property unset, so reading it threw "must not be accessed before initialization".
`struct_parse_fields` dropped a property whenever a main constructor parameter read the property's key, whether by
`[Key("y")]` on a parameter `x` or by a parameter `celsius` the constructor stores in another property. So nothing read
the property, while `new Clash(1)` was refused for leaving it unset. Separately, a struct under a PHP interface that
states `{ get; set; }` got "Declare `count` with `{ get; set; }`.", which a struct refuses.
Solution: a parameter drops a property only when the main constructor sets that property on every path, as
`assigned_properties` records. Otherwise both stay fields of one key, and the struct check reports "`y` is read twice:
by the main constructor parameter `x` and the property `y`." with the help "Give `x` another key with `[Key("…")]`.".
The first reader is always the parameter, since the parameters come first. `report_unfit_interface_property` gives a
struct the event's refusal with no help: "`Stocked` cannot give `total`, which `Settable` states with `set`: a struct
never changes.". Every struct that still compiles lowers the same `parse` as before, since only a refused struct gains
a field.
crates/
├── codex/src/metadata/mod.rs struct_parse_fields keeps a property the constructor doesn't set
├── analyzer/src/statement/class_like/
│ ├── struct.rs check_struct names each reader's kind and gives the rename help
│ └── mod.rs report_unfit_interface_property refuses a struct as an event
├── analyzer/tests/sharp.rs every_struct_parses_and_classes_do_not: both clashes, both
│ renamed forms, every help; the interface test: two struct rows
└── sharp-bridge/tests/lower.rs a_struct_parses_each_field_…: the second clash is refused
…s as a struct does
Problem: a struct could not declare `operator ==`, so a struct holding a function had no `==`, could not key a `Map`
and could not join a `Set`, while a class with the same fields could. Separately, only a struct had `parse` and
`tryParse`: `Invoice.parse(json)` on a PHP# class did not exist, `[Key]` on its constructor was refused, and a struct
field of a PHP# class type could not be read. A struct that declared its own `parse` also got a second, generated
one.
Solution: a struct's declared `operator ==` and `hash()` replace the `==` that compares property by property. The
codex writes no `op_Equality` for it, and `declares_equality` makes `==`, `!=`, `contains`, `groupBy`, a `Set` and a
`Map` key run the declared pair, whatever the struct holds, also as a field of another struct key. `==` without
`hash()` is refused with "declare it in `Coin`", with no "or a parent" since a struct has none.
Every concrete PHP# class that declares no member named `parse` or `tryParse` gets the generated pair, as a struct
does. `CodebaseMetadata::parses` is the one test the analyzer and the bridge share: the class's `parse` is declared
by the class itself and spans the whole class. `parse` reads the main constructor's parameters, inherited ones
included, then every public settable property that is no promoted parameter. The same `[Key("...")]` rule, the same
"read twice" refusal and the same `tryParse` apply. An abstract or static class gets no pair. A subclass of a class
that declares its own `parse`, such as a plain PHP `Clock::parse(string)`, keeps that one. A field `parse` can't build
is refused where `parse` is called, with why: a function, an abstract class, an interface, a plain PHP class, a class
whose `parse` reads no value, or a type such as `Object` that names no type to build. The bridge builds a nested PHP#
class, and each element of a `List` of one, through that class's own `parse`.
crates/
├── codex/src/
│ ├── scanner/class_like.rs register_class writes parse and tryParse for a concrete PHP#
│ │ class. create_struct_equality skips a declared ==, and
│ │ leaves_parse_free skips a declared parse
│ ├── populator/hierarchy.rs inherit_declared_parse keeps a parent's declared parse
│ └── metadata/mod.rs parses. struct_parse_fields reads an inherited constructor and
│ skips promoted properties
├── semantics/src/internal/checker/sharp.rs a struct's operator == is no longer refused
├── analyzer/src/
│ ├── expression/binary/comparison.rs a struct with a declared == compares through it
│ ├── expression/call/static_method_call.rs checks parse on every type that parses
│ ├── statement/attributes/mod.rs is_parsed_parameter: Key on any parsing type's constructor
│ └── statement/class_like/
│ ├── struct.rs declares_equality, report_keys_read_twice, and why each
│ │ unreadable field is refused
│ ├── method.rs check_hash names no parent for a struct
│ └── mod.rs Class::analyze reports keys read twice
├── sharp-bridge/src/
│ ├── lower.rs parse_methods for a class and a struct. A declared == skips
│ │ the generated op_Equality
│ └── lower/types.rs parses
├── semantics/tests/sharp.rs a_struct_declares_operators_as_a_class_does
├── analyzer/tests/sharp.rs a_struct_that_declares_equality_compares_and_keys_by_it,
│ a_class_parses_as_a_struct_does, every_struct_parses, the
│ Coin hash row
└── sharp-bridge/tests/
├── lower.rs a_class_parses_as_a_struct_does. Generated methods leave the
│ trees other tests pin
└── unit.rs an abstract class, which has no parse
… the write Problem: Jordan ruled that a Map and a Set become engine objects with hash() then == lookup, so any key with == and hash() works and no key is ever text. A PHP object is shared on assignment, while section 12 says a collection is a value copied only when one side writes to it, and section 13's local copy depends on that. php-ds, where the table comes from, gives shared objects only. Solution: the note records rulings 22 to 24 and decisions 8 and 9, and a new part, "Map and Set as engine objects". Sharp\Map and Sharp\Set hold php-ds v1.8.0's table, cut to what PHP# uses, with PHP#'s == and a hash for each key kind. One engine function, sharp_separate, copies a shared object into the holder before any index write, unset or changing PHP# method, in the VM, in the JIT helpers and in the collection receiver, so plain PHP's $map[$k] = $v separates too. The part lists what the bridge lowers differently, what the engine deletes (the key text, key() and readKey()), the done rows, the eighteen fixtures that use a Map or a Set, the order against collection-engine, #539, generics R3c and the one-repository move, and four questions for team-lead. Decision 6 is marked replaced, and the public names tree gains the new names with their slates. docs/architecture/slices/ └── 10-structs.md rulings 22 to 24; decisions 8 and 9; "Map and Set as engine objects"; the public names tree
Problem: team-lead's Queue 23 builds Map and Set as engine objects on #114 as it stands, and #114 rewrites the collection receiver and sharp_collection_key_slot, which this branch also changes. Solution: the merge keeps both sides. sharp.c conflicted twice. The first conflict keeps this branch's key text, which the Map and Set objects delete next, and #114's sharp_collection_renumber, and drops the copy of sharp_collection_key's header this side ended with, since #114 moved that function up. The second keeps #114's comment on sharp_collection_key_slot, which now names both the backed enum arm and the struct arm. StaticCollections' Log.note named its parameter event, which the Mago this branch builds refuses as a PHP# keyword, so it is entry. Checked with slot 9's Mago, which contains #114's pinned a9f1882: 175 of Zend/tests/sharp's 176 tests pass without opcache. opcodes.phpt fails because that Mago gives every PHP# class parse and tryParse (Queue 18), which no PHP twin has, so the test learns to leave them out when this branch moves the pin. ext/sharp/ └── sharp.c both conflicts resolved Zend/tests/sharp/ └── StaticCollections.sharp Log.note(string entry)
…able
Problem
A PHP# Map and Set are PHP arrays, so a key is an int or a string. A struct
key is stored as key text and read back by Sharp\Map::readKey, 5 and 5.0 are
two keys, and an empty Map encodes to JSON as []. Rulings 22 to 24 of
docs/architecture/slices/10-structs.md make them objects that compare keys by
PHP#'s ==, keep the key objects, and stay values.
Solution
ext/sharp/sharp_table.c is php-ds v1.8.0's ds_htable, cut down to its
buckets, collision chains and rehash, with its MIT notice. A key is found by
its hash, then by PHP#'s == within its chain: an int and a float as two
floats, a declared op_Equality with its hash(), a struct by its whole value,
a List in order, and a Map or Set regardless of order.
Sharp\Map and Sharp\Set are final internal classes over the table. Plain PHP
reads and writes them through their index. Map::of, Map::from, Set::from,
toArray, keys, values and toList cross to plain PHP. A Map encodes as a JSON
object, {} when empty, a backed enum key as its value, and any other key that
is no int or string throws JsonException.
A Map or a Set is a value. SHARP_SEPARATE copies a shared one into its holder
before every index write, compound write, nested write and unset, in the VM
and in the JIT's helpers, as SEPARATE_ARRAY copies a shared array. A plain PHP
function that writes to a Map it was passed leaves the caller's Map unchanged.
Sharp\Map::key and Sharp\Map::readKey stay until the next commit moves the
PHP# collection methods onto the objects. Until then 8 tests fail:
set_methods (3 files), sorted_by_null_keys (3 files) and struct_keys pass a
Set::from result to a PHP# method that still expects an array, and
opcodes.phpt fails as it did before, on the generated parse and tryParse.
Each of the 22 mutants of the separation sites, the table and the hashing
fails a row of the four new tests, which pass in the plain, repeat, prime,
use, tracing JIT and function JIT modes.
php-sharp/
├── Zend/
│ ├── zend_vm_def.h SHARP_SEPARATE before ASSIGN_DIM, ASSIGN_DIM_OP and UNSET_DIM on an object
│ ├── zend_vm_execute.h regenerated
│ ├── zend_execute.c SHARP_SEPARATE before a nested write through an object
│ └── tests/sharp/
│ ├── table_values.phpt every write through a holder copies a shared Map or Set
│ ├── table_keys.phpt keys found by PHP#'s ==, each kind of key a row
│ ├── table_order.phpt insertion order through growth, removal and slot reuse
│ └── table_crossings.phpt of, from, toArray, keys, values, toList, JSON and refusals
└── ext/
├── opcache/jit/zend_jit_helpers.c SHARP_SEPARATE in the assign, compound assign and nested write helpers
└── sharp/
├── config.m4 builds sharp_table.c
├── php_sharp.h SHARP_SEPARATE and sharp_separate
├── sharp_table.h new: the table, the object layout, sharp_equal and sharp_hash
├── sharp_table.c new: php-ds's table with PHP#'s == and hash
└── sharp.c Sharp\Map and Sharp\Set over the table, replacing the array Set::from
…t objects Problem: the bridge lowered a `Map` and a `Set` to PHP arrays. A backed enum key went in as its `->value` and a struct key as `\Sharp\Map::key()` text, and both read back through `from` or `\Sharp\Map::readKey`. php-sharp now runs a `Map` and a `Set` as engine objects that keep each key and compare keys by PHP#'s `==`, so the key text, the read-back and the array forms of `Map` and `Set` methods ran on objects the engine no longer gives. Plain PHP also received a `Map` keyed by a backed enum as an array of backing values, which the checker accepted wherever a `Map<string, int>` was expected. Solution: a `Map` literal lowers to `new \Sharp\Map([k1, v1, ...])`, a `Set` literal to `new \Sharp\Set([...])`, and a `Map` or `Set` type to `\Sharp\Map` or `\Sharp\Set`. Every `Map` and `Set` method runs on `Sharp\Collection`, so `INLINED_COLLECTION_METHODS` names only `List` methods. A key and an element go in as themselves, and a loop's key reads back as itself. A `Map` or a `Set` crosses into a plain PHP parameter whose declared type names `array` as `\iterator_to_array($m)`, or `\iterator_to_array($s, false)` for a `Set`. The type is native, from a stub or from a docblock, so `count` and `http_build_query` take the array. A parameter typed `mixed`, `iterable`, `object`, an interface or nothing takes the object. A `Map` keyed by a backed enum or a struct passed to a parameter that names `array` is refused once, naming the parameter and the key type. `count(counts)` on such a `Map` stays refused, and `counts.count()` counts it. A PHP array entering a `Map` or `Set` place goes through one function, `crossed`: a parameter, a return, a typed local, a typed property, and a local passed by reference to plain PHP, after the call, as `parse_str` writes it. A `Map<string, V>` takes each int key back as its `string`, and a `Map` keyed by a backed enum takes each backing value back as its case. A promoted `Map` or `Set` parameter is set in the constructor's first statements, beside the property it declares. The codex no longer reads a `Map` keyed by a backed enum as an array or an iterable of its backing values, so a `Map<Status, int>` is no `Map<string, int>` and no `iterable<string, int>`. crates/ ├── analyzer/src/invocation/ │ ├── arguments.rs check_array_crossing refuses a Map keyed by an object where plain PHP takes an array │ ├── analyzer.rs a refused crossing is reported once, not again as a type mismatch │ └── template_inference.rs a Map's key infers a template as itself, and a Set as the list of its elements ├── codex/src/ttype/comparator/ │ ├── array_comparator.rs no backing-value view of a Map's key │ └── iterable_comparator.rs no backing-value view of an iterable's key ├── names/src/lib.rs INLINED_COLLECTION_METHODS holds List methods only ├── prelude/assets/extensions/ │ └── sharp.php MapMethods and SetMethods run on Sharp\Collection ├── sharp-bridge/src/ │ ├── lower.rs Map and Set literals, hints, methods and == on the objects. call, crossed and │ │ crossed_value convert at each crossing. The key text, key read-back and holds_key go │ ├── lower/types.rs call_parameters, constructor_parameters, parent_constructor_parameters, │ │ property_type and plain_property. key_arguments, map_key_type and is_struct go │ └── lower/inline.rs a method with a Map or Set parameter has no inline form ├── analyzer/tests/sharp.rs a_map_keyed_by_an_object_is_refused_where_plain_php_takes_an_array, and Map rows that │ follow the object rules └── sharp-bridge/tests/lower.rs every Map and Set tree as the objects, and a row per crossing place
…in PHP arrays cross at the call Problem The previous commit made Sharp\Map and Sharp\Set engine objects, but every PHP# collection method still ran on a PHP array, a struct key still went in as Sharp\Map::key text and read back through Sharp\Map::readKey, and a Map or Set field's initial value could not hold an object, since PHP refuses a new expression in a property default. Eight tests failed. Rulings 23 to 25 of docs/architecture/slices/10-structs.md also set how a Map or Set meets plain PHP: the object itself, or a PHP array where the plain PHP parameter's declared type names array. Solution Sharp\Collection, the receiver of every PHP# collection method, also wraps a Sharp\Map or a Sharp\Set. get, has, put, remove, contains, add, count, keys, toList, filter, entries, groupBy and associateBy run on the table, find a key by PHP#'s ==, and change the collection in its holder through SHARP_SEPARATE. groupBy and associateBy build a Sharp\Map. The VM, the optimizer's type inference and both JITs treat a Sharp\Map or Sharp\Set receiver as they treat an array, through SHARP_IS_COLLECTION and sharp_may_be_table. The struct key text goes: sharp_struct_key, the key writer and reader, and Sharp\Map::key and Sharp\Map::readKey. Map::of and Set::from go too. A Map literal is new Sharp\Map([k1, v1, ...]) and a Set literal new Sharp\Set([...]). Map::from stays as the converter for a plain PHP keyed array. A .sharp file's property default may hold exactly new \Sharp\Map(...) or new \Sharp\Set(...). A .php file still may not. docs/spec.md: sections 10 and 12 said plain PHP receives a Map as an array, an enum key's backing value and a struct key's text. They now say plain PHP receives the Map itself, or a PHP array where its parameter names array, and that a Map keyed by an object is refused there. Every test passes in the plain, prime, use, repeat, tracing JIT and function JIT modes. The tests whose expected output changed from the array shape to the object shape are the collection_methods, enum_keys and set_methods files in every mode, index_changes, library_binary, library_regex, library_url, opcodes, static_collections, struct_keys and the four table tests. php-sharp/ ├── Zend/ │ ├── zend_compile.c a .sharp property default may create a Sharp\Map or a Sharp\Set │ ├── zend_vm_def.h a Sharp\Map or Sharp\Set receiver becomes a Sharp\Collection │ ├── zend_vm_execute.h regenerated │ ├── zend_execute.c a Map index reads null for a missing key, a List index throws │ ├── Optimizer/zend_inference.c a Sharp\Map or Sharp\Set receiver infers a Sharp\Collection │ └── tests/sharp/ │ ├── collection_crossings.phpt new: a Map or Set to an array, mixed, untyped and iterable parameter, and a │ │ PHP array back into a Map local, return and parameter │ ├── collection_defaults.phpt new: each object its own Map and Set, and no new in a .php default │ ├── Crossings.sharp, Tallies.sharp, harness/Arrays.inc, harness/PlainDefault.inc new fixtures │ └── *.phpt, *.inc expected output follows the objects ├── ext/ │ ├── opcache/jit/ │ │ ├── zend_jit.c zend_jit_sharp_receiver_may_be_collection and zend_jit_sharp_receiver_may_be_table │ │ ├── zend_jit_trace.c the same in the tracing JIT │ │ └── zend_jit_helpers.c php_sharp.h comes from zend_jit.c │ └── sharp/ │ ├── php_sharp.h SHARP_IS_TABLE, SHARP_IS_COLLECTION and sharp_may_be_table │ ├── sharp.c Sharp\Collection over the table. The struct key text, Map::of and Set::from go │ ├── sharp.stub.php Sharp\Collection's methods for a Map and a Set │ └── sharp_arginfo.h regenerated └── docs/spec.md what plain PHP receives, in sections 10 and 12
# Conflicts: # docs/spec.md # ext/sharp/sharp_unit.h # sharp/baseline/spec~HEAD
# Conflicts: # sharp/mago/.github/actions/setup-lean/action.yml # sharp/mago/.github/workflows/ci.yml
Problem sharp/baseline/spec lists the spec examples Guard 1 expects to fail. After the merge of php-sharp master and of mago-sharp feat/slice-structs into sharp/mago, 23 of them pass, and sharp/bin/spec fails while a passing example stays listed. Solution sharp/bin/spec, run with the engine and the mago built from this tree, printed these removals, and a second run passes against the new baseline. Section 10 removes struct-changes. struct-parse stays until generics R3c's Sharp\Internal\accepts lands, and struct-parse-object until the Object type of task #454 lands. sharp/baseline/spec/ ├── §2/keyword-members, §2/keyword-names removed ├── §5/interface-members removed ├── §9.1/constructor-call-this, named-constructors, set-once, super-call removed ├── §10/struct-changes removed ├── §12/collection-read-methods, collection-write-methods, enum-map-keys, │ index-changes, natural-remove, nullable-map-values removed ├── §15/emit-forms, extern-event, extern-event-effects, listener-errors, │ standalone-event removed ├── §16/initializer-shorthand removed ├── §17/braces removed ├── §23/import-rename removed └── §28/module-law removed
…w Any
Problem
The checker refused a Map whose key type mixes a backed enum or a struct with
other types, such as Map<string|Status, int>, at every loop, keys(), get, has,
remove, index, literal and groupBy. Its message said the engine holds such a key
as its backing value or its key text. Since Sharp\Map keeps each key as itself
and finds it by == and hash(), that is false, and the spec refuses no mixed key
type. Spec section 24 and decision 48 still said an empty Map below Any encodes
as a JSON array. Section 10 said a struct keys a Map only when every field has
==, which leaves out a struct that declares == with hash(). And the crossing
refusal named an iterable<string, int> parameter as Iterable<int>.
Solution
The refusals and their callers go: keys_mix_a_stored_key,
key_type_mixes_a_stored_key, report_keys_read_back and
report_keys_sharing_entries. Every comment that described a key stored as its
backing value or key text now describes the object, get_backing_key_type's doc
among them. A plain PHP spread of a Sharp\Set is typed as the list its
positions give.
The checker names a plain PHP iterable with a key type as written, as
iterable<string, int>, and a PHP# Iterable<T> as before.
Section 24 and decisions 39 and 48: a Map is a JSON object and a List a JSON
array wherever it is, below Any too. The json-encoding example shows an empty
Map below Any as {} and an empty List as []. The engine gives the same:
json_encode(['counts' => new Sharp\Map()]) is {"counts":{}}. Decision 27 says
a Map keeps the key itself and what plain PHP receives. Section 10 states both
ways a struct keys a Map.
php-sharp/
├── docs/
│ ├── spec.md section 10's struct key rule, section 24's JSON rule and example
│ └── decisions/
│ ├── 027-map-keys.md a Map keeps the key itself, and what plain PHP receives
│ ├── 039-replacing-array-and-object-casts.md JSON by kind, below Any too
│ └── 048-json-encode-below-any.md the value's kind everywhere, the old rule rejected
├── Zend/tests/sharp/
│ ├── MixedKeys.sharp new: a Map<string|Standing, int> written, read and looped
│ └── mixed_keys.phpt new: "active" and Standing::Active are two entries, each its own key type
└── sharp/mago/crates/
├── analyzer/src/
│ ├── statement/mod.rs the four mixed-key functions and the loop's check go
│ ├── expression/array.rs, array_access.rs, assignment/array_assignment.rs,
│ │ call/method_call.rs, invocation/return_type_fetcher.rs their callers go
│ ├── expression/array.rs a plain PHP Set spread is a list
│ ├── utils/names.rs iterable<K, V> keeps its key type
│ └── statement/class_like/struct.rs, function_like/mod.rs, utils/expression/array.rs comments
├── analyzer/tests/sharp.rs a_map_key_type_that_mixes_a_backed_enum_or_a_struct_with_other_types_takes_every_key,
│ the iterable<string, int> text, and a plain PHP Set spread
└── codex/src/ttype/ get_backing_key_type's doc, and TArray::Set's
…bjects, and a class with == and hash() keys a Map
Problem
Three checker and protocol paths still treated a Map as the PHP array it used
to run as. List.wrap(map) was refused because "a List and a Map both run as PHP
arrays", though a Map is one object now and wrap holds it in a one-element List.
The external analyzer protocol sent a Set as an array keyed by its elements and
a Map keyed by a backed enum as an array keyed by backing values, so a plugin
never saw Sharp\Map or Sharp\Set. And the checker refused a Map keyed by a class
that declares operator == with hash(), and a struct that holds such a class,
where spec section 12 keys a Map by anything with == and hash(). Its message
listed only int, string, a backed enum and a struct.
Solution
List.wrap refuses only a T that can be a List, the one thing wrap returns as it
is. A Map or a Set T is held in a one-element List. The analyzer note and the
stub doc say only that a List is returned as it is.
The protocol sends a Set as Sharp\Set<T> and a keyed array as Sharp\Map<K, V>,
each key as itself, wherever the code that holds the type is PHP#. A type is as
its file's code sees it: an analyzed file's types by that file's dialect, a
declaration's types by the dialect of the file they are written in, a provider
request's by its calling file. In plain PHP code a keyed array stays a PHP array,
unless its keys can be objects, which no PHP array holds. A type a plugin returns
that names Sharp\Map<K, V> or Sharp\Set<T> is decoded back to the Map or Set.
The backing-key encoder goes. No plugin in the tree reads the old array shape.
get_backing_key_type treats a class that declares or inherits operator == as a
key, as it does a struct, so every key check takes it: a Map type, a Set
element, a literal key and a groupBy key. A struct that holds such a class keys
a Map. Only a struct is checked for the class instances it holds, so a class key
is refused once, by the key rule. The key message in the analyzer and the
semantics checker lists what spec section 12 lists, and the struct key note
describes the hash and == lookup instead of key text.
php-sharp/
├── Zend/tests/sharp/
│ ├── Wraps.sharp new: List.wrap of a Map and of a Set
│ ├── list_wrap.phpt each is one element, the same Map or Set
│ ├── Pass.sharp new: a class with operator == and hash()
│ ├── ClassKeys.sharp new: a Map<Pass, int> and a Set<Pass>
│ └── class_keys.phpt new: two distinct == passes are one entry, in every opcache mode
└── sharp/mago/crates/
├── analyzer/src/
│ ├── invocation/return_type_fetcher.rs wrap refuses only a T that can be a List
│ ├── external/protocol.rs Map and Set as objects, by dialect, and decoded back; tests
│ ├── external/lifecycle.rs an analyzed file's types by its dialect
│ ├── external/metadata.rs a declaration's types by its file's dialect
│ ├── external/mod.rs the session's dialect of a span; property and forwarding requests
│ ├── plugin/registry.rs, resolver/property.rs the property request takes the dialect
│ ├── statement/class_like/struct.rs a held class with == is a value; only structs are checked
│ └── statement/function_like/mod.rs the key message lists the spec's keys
├── analyzer/tests/sharp.rs a_class_that_declares_equality_and_hash_keys_a_map, the wrap rows,
│ and the new key text
├── codex/src/ttype/mod.rs get_backing_key_type takes a class with ==
├── prelude/assets/extensions/sharp.php the List.wrap doc
└── semantics/ the key message, and its tests
…object it receives
Problem
Plain PHP receives a PHP# Map or Set as a Sharp\Map or Sharp\Set object, but
the analyzer typed it in a .php file as the PHP array it used to be:
array<K, V>, or set<T>. So is_array() on one was reported as always true where
it is always false, and a PHP# parameter typed as the array refused the object
plain PHP had just received. The engine classes also lacked ArrayAccess, which
the class shape in docs/architecture/slices/10-structs.md gives them, so an
index read had no interface to resolve through.
Solution
Sharp\Map and Sharp\Set implement ArrayAccess, as WeakMap does: the index keeps
running through the object's own handlers, offsetGet and offsetExists run as the
index reads, and offsetSet and offsetUnset throw, because a write through a
method call has no holder to separate into and would reach every holder of a
shared one. The prelude declares both classes with their templates, so an index
read and a loop resolve through ArrayAccess<K, V> and IteratorAggregate<K, V>.
In a .php file, a type a PHP# declaration gives is what plain PHP receives,
get_php_received_type: each Map and Set in it, inside a collection and in a
function's parameters and return too, is the object. That holds for a return, an
instance or static property read, and a callback's argument. A PHP# parameter
and a property write take get_php_passed_type: the object, or at the top the
PHP array PHP# turns into one, a List for a Set. The protocol builds its
Sharp\Map and Sharp\Set through the same codex function, get_collection_object.
The plain PHP corpus gives byte-identical reports against master, 13,729 lines.
A mixed probe changes only the .php lines that read a PHP# Map or Set.
php-sharp/
├── ext/sharp/sharp.c ArrayAccess, with offsetGet, offsetExists, and offsetSet and
│ offsetUnset that throw
├── Zend/tests/sharp/table_crossings.phpt instanceof ArrayAccess, the four methods, a hash() that throws
├── docs/architecture/slices/10-structs.md the index runs through the handlers; offsetSet throws
└── sharp/mago/crates/
├── codex/src/ttype/mod.rs get_collection_object, get_php_received_type, get_php_passed_type
├── prelude/assets/extensions/sharp.php final classes Sharp\Map and Sharp\Set
├── analyzer/src/
│ ├── invocation/return_type_fetcher.rs a PHP# return, as plain PHP receives it
│ ├── invocation/analyzer.rs a PHP# parameter, as plain PHP passes it
│ ├── resolver/property.rs, static_property.rs a PHP# property read and write
│ └── external/protocol.rs the object through get_collection_object; the received row
└── analyzer/tests/sharp.rs plain_php_receives_a_map_or_a_set_as_the_object_it_runs_as, and the
Map parameter's Sharp\Map|array text
|
Review verdict: CHANGES NEEDED Head reviewed: php-sharp #123 at Ten changes are needed. Two more defects sit inside the feature but also exist on
The rest of the judged list holds. That is covered in the last section, with the commands that ran. 1. StructsThe help for a write to a get-only struct property names a fix that does not compile
namespace Repro;
public struct Basket
{
public Basket(public Map<string, int> counts { get; }) { }
public void drop() { this.counts.remove("x"); }
}
namespace Repro;
public struct Crate
{
public Crate(public Map<string, int> counts { get; }) { }
public Crate dropped() => this with { counts: [:] };
}Today With a method that returns the struct, the help names that method, even when the method is the It must name a fix that compiles. For a get-only property, the help names the main constructor, 2. KeysTwo sibling classes that share a declared
|
# Conflicts: # sharp/mago/crates/semantics/src/internal/checker/sharp.rs # sharp/mago/crates/sharp-lean/src/translate/mod.rs
…and its crossings Problem The review of #123 found programs compiled wrong and valid programs refused: - A plain PHP index write through a get-only, init, hooked or struct Map property did nothing and raised nothing. The engine copied the object for the write fetch, as it does for any object, and the write went into the copy. - Two sibling classes that inherit one operator == were equal by ==, yet two entries in a Map and a Set. - A Map passed to a plain PHP parameter typed Countable, Traversable, JsonSerializable, object or Sharp\Map was refused. - A plain PHP closure held in a PHP# local received the Sharp\Map object for its array parameter and threw TypeError. - A PHP array crossing into Map<string|Level, int> kept "5" as the int 5. - Set<string|Mood> and Set<Mood?> were "not supported yet", and a Set of a class without == got that message in place of the key rule. - A pure enum, float, bool or null key was refused by a message that says section 19 gives them ==. - A write to a get-only struct property named `with`, or a method that is a `with` on it, as its fix, and neither compiles. - ["5": 1, "sku-2": 2] typed "5" as the int 5, and Statics.shared["a"] = 1 on a static Map gave mixed-property-access. Solution - The engine treats a Sharp\Map or Sharp\Set like an array in the write fetches of zend_std_read_property, zend_fetch_property_address and the JIT, so PHP raises its own "Cannot indirectly modify" and "Indirect modification" errors. - sharp_equal compares two unrelated classes by the op_Equality both resolve to. - The comparator, under PHP# rules, compares a Map or a Set reaching an object type as the Sharp\Map or Sharp\Set object it runs as. - Lowering::call takes PlainParameter, a declared parameter or one of a plain PHP closure's signature, so a local holding a plain closure crosses as a named call does. A PHP# function type still takes the object. - Lowering::crossed casts each key to string when the key type holds string and no int. - is_key_type is the one key rule: int, float, string, bool, null, an enum, a struct, or a class that declares operator ==. A Set's element takes it too, in the analyzer and in the semantic arm. collection_key_error words it for a Map or a Set. A null index fits a Map whose key type holds null. A Map keyed by anything but int or string is refused where plain PHP takes an array. - A get-only struct property's help names the main constructor, or init with `with`. is_set_by_with is shared with the `with` check. - A PHP# literal keeps "5" a string. An index write to Class.name updates the static property, and the bridge never asks a static property for hooks. - sharp/baseline/spec lists event-declarations: its Order declares get-only properties no constructor sets, which this branch refuses. Zend/ ├── zend_object_handlers.c three write-fetch branches treat a table as an array ├── zend_execute.c the VM's write fetch of a guarded property too └── tests/sharp/ ├── Guarded.sharp get-only, init, hooked, set-hooked, struct, static Maps ├── table_values.phpt each guarded write raises PHP's error; a PHP# static write ├── table_keys.phpt siblings share an inherited ==; an unrelated == stays apart ├── ClassKeys.sharp PHP# sibling classes under one == ├── class_keys.phpt their Map and Set count ├── Crossings.sharp object parameters, a closure local, a function type, string|Level ├── collection_crossings.phpt their run rows └── harness/Arrays.inc plain PHP functions typed Countable, Traversable, … ext/ ├── sharp/sharp_table.c sharp_equal's sibling arm └── opcache/jit/ ├── zend_jit_helpers.c zend_jit_fetch_obj_w_copies └── zend_jit_ir.c the three write-fetch branches call it sharp/ ├── baseline/spec/§15/event-declarations └── mago/crates/ ├── codex/src/ttype/ │ ├── mod.rs is_key_type │ └── comparator/atomic_comparator.rs a table reaching an object type ├── semantics/ │ ├── src/internal/checker/sharp.rs is_key_hint; the Set arm takes Map keys │ └── tests/sharp.rs ├── analyzer/ │ ├── src/statement/function_like/mod.rs report_collection_keys_without_equality, collection_key_error │ ├── src/statement/mod.rs │ ├── src/expression/array.rs literal and spread keys │ ├── src/expression/with.rs is_set_by_with │ ├── src/expression/assignment/array_assignment.rs Class.name index writes │ ├── src/resolver/property.rs the get-only struct help │ ├── src/invocation/arguments.rs the array crossing key check │ ├── src/invocation/return_type_fetcher.rs │ ├── src/utils/expression/array.rs a null Map index │ └── tests/sharp.rs └── sharp-bridge/src/ ├── lower.rs call over PlainParameter; crossed's key cast; static targets └── lower/types.rs PlainParameter, closure_parameters docs/ ├── decisions/027-map-keys.md the key rule └── architecture/slices/10-structs.md a Set's element
Problem
Section 12 lists a plain PHP write to a PHP# property as a crossing, so
`$order->prices = ['a' => 1]` and `Order::$shared = [...]` must work. The
property is typed `\Sharp\Map` or `\Sharp\Set`, so the engine refused the
array with a TypeError. A `set` hook can't convert it: PHP refuses hooks on
static properties, and a set hook breaks every index write through the
property. The analyzer refused the same writes in plain PHP: an all-digit key
such as '5', which PHP holds as an int, and a backed enum's backing values.
Solution
The bridge gives each `Map` or `Set` property `#[\Sharp\Crossing(plan)]`, a
plan that mirrors its own crossing: ['map', key, values], ['set', elements]
or ['list', elements], the key null, 'string' for an int key back as its
string, or the backed enum whose case each backing value is. One helper,
`key_crossing`, decides the key for both. Where the engine checks a typed
property write, instance, static, by reference, through reflection and
through a typed reference, an array on a property with the plan crosses into
the object, in strict mode too, as the engine coerces a scalar. The property
keeps its type, so index writes through it stay plain writes. A property's
`set` body takes `\Sharp\Map|array` and crosses first, as a parameter does.
`get_php_passed_type` now types the array plain PHP passes or writes with the
keys PHP holds: backing values, and an int beside a string.
`get_backing_array_type` did that for arguments alone and goes.
The `event-declarations` example gives `Order` a main constructor that sets
`id` and `total`, so it leaves the spec baseline. Decision 027's chosen
option is a key stored as itself.
php-sharp
├── Zend/zend_execute.c <- i_zend_check_property_type crosses an array; a typed
│ reference takes it through zend_coerce_for_property
├── ext/sharp/sharp.c, php_sharp.h <- Sharp\Crossing; sharp_cross_property builds the object
│ from the plan; sharp_crosses_property
├── Zend/tests/sharp/Larder.sharp <- new fixture: instance, static, promoted, nullable,
│ hooked, enum-keyed and nested Map and Set properties
├── Zend/tests/sharp/table_property_writes.phpt
│ <- new: each write form, an index write after it, and
│ the refusals, in strict mode
├── Zend/tests/sharp/Sets.inc, EnumKeys.inc, Collections.inc, Inbox.inc
│ <- the twins carry #[\Sharp\Crossing]; the states set
│ hook takes \Sharp\Set|array and crosses it
├── docs/spec.md <- event-declarations: Order's main constructor
├── sharp/baseline/spec/§15/event-declarations
│ <- deleted: the example passes
├── docs/decisions/027-map-keys.md <- chosen: a key is stored as itself
├── docs/architecture/slices/10-structs.md <- the property crossing and its public names
└── sharp/mago/crates/
├── sharp-bridge/src/lower.rs <- KeyCrossing, key_crossing, crossing_plan; attributes
│ adds #[\Sharp\Crossing]; a Map or Set set hook
│ crosses its value; table_or_array, collection_hint
├── sharp-bridge/tests/lower.rs <- the plan of each property type
├── codex/src/ttype/mod.rs <- get_php_passed_type: the keys PHP holds
├── analyzer/src/invocation/arguments.rs
│ <- get_backing_array_type deleted
└── analyzer/tests/sharp.rs <- plain PHP passes and writes those keys
…eside structs and Map and Set objects
Problem
master gained spec section 32's modules and container, section 26's
extensions, and the warm-compile fix. Both sides changed the same bridge,
scanner, checker and flag code, so 13 files conflicted, and four places
compiled apart but not together.
Solution
Each side's behaviour stays, through one mechanism where both had one:
- A parameter's `Map` or `Set` crosses through `collection_parameters`,
which now takes any parameters, so master's extension members cross a
`Map` as well as a `Set`. master's `set_parameters`, `set_parameter` and
`set_from` go: `collection_parameters`, `collection_hint` and `crossed`
do their job.
- `attributes` takes the one group the lowering adds last: a property's
`#[\Sharp\Crossing]` or master's parameter `#[\Sharp\Internal\Service]`,
each from its own builder over a shared `attribute_group`.
- The test hooks of a test module's file go on a class in `class`, since
`class_members` serves a struct too and the module names only classes.
- `STRUCT` and `REQUIRED` move to flag bits 51 and 52, after master's
extension flags.
- The bridge's `constructor_parameters` for `new` becomes `new_parameters`,
beside master's `constructor_parameters` for the container.
- master's extension call drops the key argument our side deleted with the
key text, and `Key` joins the default class names master moved to
`shadowable_default_classes`.
- Two of master's test-module tests assert their rule rather than "no
method" and "no return anywhere", which every class's generated `parse`
and `tryParse` broke.
- `Stamp.sharp` calls `value.trim()`, as section 26 now requires.
`struct-equality` passes, a struct's `==` calling the new `trim()`, and
leaves the spec baseline.
- `sharp_arginfo.h` is regenerated from the merged stub.
php-sharp
├── ext/sharp/sharp_arginfo.h <- regenerated from the merged sharp.stub.php
├── Zend/tests/sharp/Stamp.sharp <- value.trim()
├── sharp/baseline/spec/§19/struct-equality <- deleted: the example passes
├── docs/architecture/slices/10-structs.md <- names collection_parameters and crossed
└── sharp/mago/
├── scripts/regen-analyzer-issue-codes.php
│ <- both sides' issue codes
└── crates/
├── codex/src/metadata/flags.rs <- STRUCT 1 << 51, REQUIRED 1 << 52
├── codex/src/scanner/ <- register_struct and walk_struct beside modules'
│ bindings; the names binding enum by its module path
├── names/src/lib.rs, internal/walker.rs
│ <- Sharp\Key a default class name
├── semantics/src/internal/checker/sharp.rs
│ <- check_struct_declaration beside check_extension
├── syntax/src/parser/internal/class_like/method.rs
│ <- both doc lines
└── sharp-bridge/
├── src/lower.rs <- collection_parameters for every parameter list;
│ attributes, crossing_attribute, service_attribute,
│ attribute_group; test hooks in class
├── src/lower/inline.rs <- form refuses a Map or Set parameter
├── src/lower/types.rs <- new_parameters
└── tests/lower.rs <- both sides' tests; two test-module asserts narrowed
…d Set objects Problem master gained spec section 29's foreign classes after the last merge, so #123 conflicted again in five files, and a struct had no answer to the checker's new question of whether a class-like is `foreign`. Solution Each side's behaviour stays: - `FOREIGN` keeps master's flag bit 51, and `STRUCT` and `REQUIRED` move to 52 and 53. - `Sharp\Key` joins the default class names master now lists in full. - The issue-code list holds both sides' codes, and `code.rs` matches what the script generates. - `method_call.rs` keeps master's `StaticPropertyName` for its static-store check and drops the key read-back reports our side deleted with the key text. - A struct is never `foreign`, since it takes only `public`. - `sharp_arginfo.h` is regenerated from the merged stub. php-sharp ├── ext/sharp/sharp_arginfo.h <- regenerated from the merged sharp.stub.php └── sharp/mago/ ├── scripts/regen-analyzer-issue-codes.php │ <- both sides' issue codes └── crates/ ├── codex/src/metadata/flags.rs <- FOREIGN 1 << 51, STRUCT 1 << 52, REQUIRED 1 << 53 ├── names/src/lib.rs <- Sharp\Key among the default class names ├── analyzer/src/expression/call/method_call.rs │ <- StaticPropertyName kept, the key reports' imports gone └── semantics/src/internal/checker/sharp.rs <- a struct's ClassLike is not foreign
… holds takes a Map by its __invoke
Problem
patch-coverage found 202 lines no Rust test ran. Most are the crossing of a
Map or a Set into plain PHP, and the dialect an external analyzer reads a
type in. Only engine tests ran them. Four lines could not run at all: two
impossible branches, and a `?` whose only own region is its error branch.
Covering the call of a plain PHP callable a local holds found a bug. A
PHP# call of a local that holds a plain PHP object passed a Map as the
object, so the object's `__invoke(array $items)` threw a TypeError.
Solution
- A call of a local crosses each argument by the closure signature of the
local's type, or else by the call the analysis resolved, an object's
`__invoke` among them. `closure_parameters` takes a local typed as one
signature.
- `ExternalAnalyzer::get_property_type` reads the dialect from the session,
by the access's span, as a metadata query does. `PluginRegistry` and the
property resolver pass no dialect, as on master.
- `declares_equality` never meets a name the codebase lacks, and PHP#
writes no array shape: both are `unreachable!` with the reason.
- The static or instance property write binds its result before `?`.
- Rust tests cover each crossing and each request an external analyzer
reads a type in, PHP# and plain PHP as rows. Each new row was seen to
fail first or caught a mutant of its site.
php-sharp
├── Zend/tests/sharp/
│ ├── collection_crossings.phpt <- a plain PHP object a local holds: array 2
│ ├── Crossings.sharp <- invoked() calls a local ArrayCounter
│ └── harness/Arrays.inc <- ArrayCounter with __invoke(array $counts)
└── sharp/mago/crates/
├── sharp-bridge/
│ ├── src/lower.rs <- a local's call falls back to call_parameters
│ ├── src/lower/types.rs <- closure_parameters takes one signature
│ └── tests/lower.rs <- four crossing tests: into array parameters,
│ through references, a local's callable, and in
└── analyzer/src/
├── expression/assignment/array_assignment.rs
│ <- the property write binds `written` before `?`
├── statement/class_like/struct.rs <- declares_equality: unreachable! for a missing name
├── statement/function_like/mod.rs <- a Map type without a key type: unreachable!
├── resolver/property.rs <- no dialect argument, and its doc comment back on
│ find_property_in_class
├── plugin/registry.rs <- get_property_type as on master
└── external/
├── mod.rs <- the property request reads session.dialect(span);
│ a test of property and forwarding requests
├── lifecycle.rs <- one `dialect` local in the snapshot; a test of
│ snapshots, after-file requests and queries
├── metadata.rs <- tests of codebase queries and assertions
└── protocol.rs <- shared fixtures and analysis; tests of provider
requests and returned collections
Brings #124: plain PHP is called and read through its namespace, and a statement that never runs is refused. Problem #124 refuses every bare call of a plain PHP function and lowers `global.x(…)` and `Alias.fn(…)` through the method-call path. This branch crosses each Map and Set into a plain PHP `array` parameter where the bare call lowered. So after a plain merge `global.implode(",", tags)` would pass the Set object to PHP, and Crossings.sharp, which calls the harness's functions bare, would not compile. Solution - A call through a namespace crosses its arguments by `call_parameters`, as a static call does. A bare call is now only a pattern's PHP shape, such as `is_string`, and takes the object. - Crossings.sharp and Roster.sharp call plain PHP through `global.`, and the spec's crossing rule writes `global.count(counts)`. - `Sharp\Replaces` leaves the default names with #124's attribute, and `Sharp\Key` stays. - The analyzer and semantics tests keep this branch's expectations with #124's `global.` calls. A test of a Map keyed by an object names `global.count`, and `Set.from` stays a missing method, because `Sharp\Set` is the engine class each Set is. - opcodes.phpt keeps this branch's rows with #124's Interop count. php-sharp ├── Zend/tests/sharp/ │ ├── Crossings.sharp <- each harness function through global. │ ├── Roster.sharp <- global.implode around kept.map(n => n) │ └── opcodes.phpt <- Interop: 5 op arrays ├── docs/spec.md <- global.count(counts) in the crossing rule └── sharp/mago/crates/ ├── names/src/lib.rs <- Sharp\Key without Sharp\Replaces ├── sharp-bridge/ │ ├── src/lower.rs <- the namespace call crosses by call_parameters; │ │ exit is unreachable, as on master │ ├── src/lower/types.rs <- namespace_function and namespace_constant beside │ │ call_parameters and closure_parameters │ └── tests/lower.rs <- a global.implode row; master's throw doc ├── analyzer/tests/sharp.rs <- this branch's rows with global. calls └── semantics/tests/sharp.rs <- this branch's key message, master's echo advice
Brings #102: reified generics, with their type checks of values plain PHP gives, and makes them hold this branch's Map and Set objects. Problem #102 keeps master's model of a Map and a Set: a PHP array keyed by each backing value, read back through `from` or `(string)`. This branch made each one a `Sharp\Map` or `Sharp\Set` object that keeps every key as itself. After a plain merge: - #102's type walk took a Map or a Set only as a PHP array, so every `Sharp\Map` plain PHP passed to a `Map<…>` parameter was refused, and a PHP# typed property took a PHP array from plain PHP without crossing it. - A union or nullable key type, as `Map<string|Level, int>`, refused the int key PHP holds the string "5" as. - The checker refused `Map<TKey, int>` with `TKey : Status & HasLabel`. - The lowering, the checker, the analyzer and the codex each carried both key models. Solution This branch's model wins for keys. Every generics feature of #102 stays. - The type walk reads a `Sharp\Map` or `Sharp\Set` entry by entry, each key as the value it is, and a refusal names the bad entry's path. A PHP array it takes for a PHP# Map or Set property crosses into the object, as the property's #[\Sharp\Crossing] plans it. A parameter's PHP array still crosses in the method's first statements, by its crossing plan. - A union or a nullable key type takes the keys any member takes. - An intersection keys a Map when one of its members does. - Master's backing-key read-back, `Types::backed_enum` and `get_sole_backed_enum` go. A class value keys no Map, as #102 rules. - `Lowering::call` takes a generic method's type arguments, so one path crosses arguments and appends them. `Lowering::instantiation` gives a generic class's `new`, `new TItem(…)`, `new (type)(…)` and `new Self(…)` their type arguments beside an object initializer. - `erase` lowers a Map or a Set to its class. A Map property's type is `Sharp\Map` under the type text a plain PHP write is checked against. - A struct header takes a generic interface. - The dead `target_dialect` of template inference goes. - The spec baseline drops struct-parse, which passes once both sides meet. php-sharp ├── Zend/tests/sharp/ │ ├── Generics.inc <- the twin's Map properties are Sharp\Map │ ├── TypeArgumentsChecks.sharp <- ordered and counted take a Map and a Set │ ├── type_arguments_entry_collection.phpt │ │ <- rows: a Sharp\Map or Sharp\Set object passes, │ │ a bad entry fails naming its path, a PHP array │ │ converts, the key "5" stays "5" for string and │ │ string? │ ├── type_arguments_call_hidden.phpt │ │ <- reads the Map's keys with keys() │ ├── table_property_writes.phpt <- a key no case backs is the walk's TypeError │ ├── enum_keys*.phpt <- the walk refuses "missing" before the crossing │ ├── set_methods*.phpt <- the walk refuses 1.5 for Set<string> │ └── opcodes.phpt <- both sides' rows ├── ext/sharp/ │ ├── sharp.c <- the walk reads Sharp\Map and Sharp\Set; │ │ sharp_property_accepts crosses a PHP array; │ │ sharp_type_holds_key reads T? and unions │ ├── php_sharp.h <- both sides' declarations │ └── sharp_arginfo.h <- regenerated from the stub ├── Zend/zend_compile.c <- both sides' prop_decl ├── sharp/baseline/spec/§10/struct-parse │ <- removed: the example passes └── sharp/mago/crates/ ├── codex/src/ttype/mod.rs <- is_key_type reads intersections; │ get_backing_key_type composed ├── semantics/src/internal/checker/sharp.rs │ <- check_generic keeps this branch's key rule ├── analyzer/src/invocation/template_inference.rs │ <- direct key comparisons, no target_dialect ├── sharp-bridge/src/lower.rs <- call with type arguments; one instantiation; │ erase of Map and Set ├── sharp-bridge/src/lower/types.rs │ <- backed_enum goes ├── syntax/src/parser/internal/ <- struct header takes a generic interface └── */tests/ <- both sides' rows in this branch's key model
Problem A `.sharp` file that passes a local to a plain PHP reference parameter and then to an untyped one reported "This assignment to type `mixed` may violate a reference constraint." `mixed` is a PHP name: PHP# writes it `Any?`, and a Map as `Map<int, string>`, not `array<int, string>`. Solution The issue renders both types through `display_type`, as the other assignment issues do. A `.php` file keeps Mago's type ids. The refusal of a typed local at a wider plain PHP reference parameter stays: plain PHP can write a wider value back into the local. sharp/mago/crates/analyzer/ ├── src/expression/assignment/mod.rs <- both types through display_type └── tests/sharp.rs <- a row: the issue names Any? and Map<int, string>
Two files conflicted, and each keeps both sides. ext/sharp/sharp.c: master replaced the old Sharp\Set and its `from` with Sharp\Map and Sharp\Set tables, at the place this branch added Sharp\Class. The merge keeps Sharp\Class and its `of` after Sharp\List, drops the old Set's lines that only this branch still held, and registers Sharp\Class before master's Sharp\Struct, tables and Crossing. crates/semantics/src/internal/checker/sharp.rs: master's check_struct_declaration and this branch's check_extension_type_parameters were added at one place. Both stay whole, the extension check first.
…nference Problem The merge with structs (#123) compiled no analyzer: master's infer_unfixed_templates no longer takes the target's dialect, and fits in resolver/extension.rs still passed `Dialect::Sharp`, so a generic extension block's receiver could not be matched at all. Solution fits calls infer_unfixed_templates as master declares it, and the Dialect import it no longer needs is gone. The analyzer and bridge suites, the class value engine tests and every spec example pass on the merge. sharp/mago/crates/analyzer/src/ └── resolver/extension.rs fits drops the dialect argument
…, into feat/events-engine Kept both imports in static_method_call.rs and regenerated sharp_arginfo.h. Co-Authored-By: Claude <[email protected]>
Problem
Spec section 10 declares structs: values compared by
==, changed throughwith, read from untrusted input withparseandtryParse, and usable asMapkeys andSetelements by their whole value. None of it compiled. AMapand aSetran as PHP arrays, so a key was anintor astring, a struct key was stored as generated key text,5and5.0were two keys, and an emptyMapencoded to JSON as[].The design note is
docs/architecture/slices/10-structs.md, with its rulings 1 to 26.Solution
Structs, in
sharp/mago. The checker and the bridge for every rule of section 10. A struct never changes.==compares field by field, or runs a declaredoperator ==withhash().withcopies.parseandtryParsereport every bad field at once with its path. PHP# classes get the sameparse.MapandSetare engine objects.ext/sharp/sharp_table.cis php-ds v1.8.0's hash table, with its MIT notice. It finds a key byhash(), then by PHP#'s==.Sharp\MapandSharp\Setkeep each key object and copy on a shared write throughSHARP_SEPARATE, in the VM and the JIT. Every PHP#MapandSetmethod runs onSharp\Collectionover the table. AListstays a PHP array.Plain PHP. A
Mapor aSetpassed to a plain PHP parameter whose declared type namesarraycrosses as\iterator_to_array(). Any other parameter takes the object. AMapkeyed by anything butintorstringpassed to such a parameter is a compile error that names the parameter and the key type. A PHP array entering aMaporSetplace is converted by one bridge function,crossed, for a parameter, a return, a typed local, a typed property, and a by-reference argument after the call.Property defaults. A
.sharpproperty default may hold exactlynew \Sharp\Map(...)ornew \Sharp\Set(...). A.phpfile still may not.Removed.
sharp_struct_key,Sharp\Map::key,Sharp\Map::readKey,Map::of,Set::from, and the bridge's key text and key read-back.Tests whose expected output changed from the array shape to the object shape
Zend/tests/sharp/collection_methods.phpt,collection_methods_jit_function.phpt,collection_methods_jit_tracing.phptZend/tests/sharp/enum_keys.phpt,enum_keys_jit_function.phpt,enum_keys_jit_tracing.phptZend/tests/sharp/set_methods.phpt,set_methods_jit_function.phpt,set_methods_jit_tracing.phptZend/tests/sharp/index_changes.phptZend/tests/sharp/library_binary.phpt,library_regex.phpt,library_url.phptZend/tests/sharp/opcodes.phptZend/tests/sharp/static_collections.phptZend/tests/sharp/struct_keys.phptZend/tests/sharp/table_crossings.phpt,table_keys.phpt,table_order.phpt,table_values.phptNew:
collection_crossings.phptandcollection_defaults.phpt.Guard 1
23 entries leave
sharp/baseline/spec, among them §10struct-changesand §12enum-map-keys.sharp/bin/specpasses against the new baseline with the engine and the mago built from this tree. §10struct-parsestays until generics R3c'sSharp\Internal\acceptslands. §10struct-parse-objectstays until theObjecttype of task #454 lands.Order
This branch carries #114 (
feat/collection-engine). CI goes green once the engine PRs ahead of it land: #114, then #102, whose type walk this branch then merges to add theMapandSetarms.Verification
Zend/tests/sharp: 182 of 182 pass without opcache, priming and using the file cache, with--repeat 2, and with the tracing and function JITs, using the mago built fromsharp/mago.sharp/mago: fmt,clippy --workspace --all-targets -D warnings, and the tests ofmago-analyzer,mago-codex,mago-names,mago-preludeandmago-sharp-bridgepass.