Repository navigation
fix: plain PHP gets a Map or a Set as its PHP array at every crossing - #163
Merged
Merged
Conversation
…h plain PHP as arrays at every crossing Plain PHP code and the framework read a PHP# Map or Set as a Sharp\Map or Sharp\Set object at a property read, a return, an argument, a constant and an attribute argument, so array_merge, ksort and Laravel's array rule refuse it, and a PHP# interface method returning Any? fits no untyped plain PHP method. The note records each crossing's state on master 754ab7c, the mechanism team-lead approved with the answers to its seven questions, and what it costs a PHP# read of its own property: nothing at run time. docs/architecture/slices/ └── collections-to-plain-php.md <- new: the Slice note
Plain PHP code received a PHP# Map or Set as the object wherever no `array` parameter asked for the array: a result, a property it read, a constant, an attribute's argument and an argument to an untyped parameter. So array functions, `is_array` and array type hints failed on values PHP# handed over. Spec section 12 now says plain PHP receives the PHP array everywhere: a Map is the array of its entries and a Set the list of its elements, at any depth inside a List. - A function whose result holds a Map or a Set carries ZEND_ACC_SHARP_CROSSING, and each of its returns emits ZEND_SHARP_RETURN_ARRAY, which gives a plain PHP caller the array and a PHP# caller the object. - A property whose type holds one carries the same flag, and a plain PHP read gives the array through `->`, `get_object_vars`, `(array)`, `foreach` and `ReflectionProperty::getValue`. PHP# reads its own property as the Map, and a plain PHP write still crosses back in. - Reflection types each such property, parameter and result `array`. - A Map or Set constant is its PHP array, and an attribute's argument too. - A call through a function value hands `\Sharp\Internal\arrayOf` the closure, which keeps the object for a closure of PHP#. - A result that erases to mixed declares no PHP return type, unless it overrides a typed plain PHP method. - A PHP# class forwards a method its PHP# interface types and its plain PHP parent leaves untyped, with the interface's type, and the checker no longer refuses that class. - A Map keyed by a class, a struct, a collection or an enum without backing values reaches plain PHP as the Map, and so does a Map two of whose keys are one PHP array key. The optimizer, the function JIT and the tracing JIT learn the array a plain PHP read or call gets, and collection_crossings runs a row for each crossing, under preloading too.
patch-coverage found lines no test ran: each PHP type a forwarded method declares, a method two interfaces require, a spread in a constant's collection literal, and an Any? result that overrides a typed plain PHP method. forwarded_methods reads the parent's method in one chain, so a missing one needs no branch of its own.
Plain PHP gets a Map of string keys and a Set as PHP arrays, so the serialize rows that read PHP# results print the arrays, with each key. A Map keyed by an enum without backing values, a struct or a class still reaches plain PHP as the Map. The rows about the Map and Set objects themselves take a Map keyed by an enum, or make the object with new.
…s its type A class constant typed `[int, string]` or `Iterable<int>` got "This type is not supported yet in PHP#." Each holds a list literal, a value known before the code runs, so the checker takes both, and PHP types the constant `array` or `iterable`. A read of a PHP# constant that declares a collection, a tuple or an Iterable now has the declared type, where it had its value's own type: a list literal under `Set<string>` read as a List, and a tuple's read refused its unpacking. A class constant holds no object, so a read of a Map or Set constant makes its PHP array the object again, and PHP#'s Set and Map methods run on it.
…lace The review of the crossing found plain PHP still meeting a Sharp\Map or a Sharp\Set, a leak, and constants that stopped their class loading. - An index read of a crossing property from plain PHP, as `$box->payload['a']`, leaked one array per read under both JITs. The inference dropped the read's count for any property. It now keeps it unless it resolves the property and finds no crossing. - A Map or Set constant read inside a constant's value, a literal, an attribute argument or a default lowers to the bare constant, its PHP array. A property's crossing makes the object of a default the compiler folded. Elsewhere a constant read makes the object once and keeps it under the constant's own key, so a later read is one lookup. - A generator's yielded values cross as returns do. So do the values of an `Any`, `Any?`, type parameter or `Iterable` result or property. - The bridge decides each crossing from the declared type. A Map whose key type unites two of `int`, `string` and backed enums, as `int|string` does, stays the Map: the checker refuses it into an `array` parameter, and the run-time check on its keys is gone. - A result's crossing moves from its fn_flags bit to the attr of its result type, which leaves that bit free. A Map result lowers to `\Sharp\Map|array`, so a plain PHP class can implement or override it. - `empty()`, `get_mangled_object_vars`, `get_class_vars`, `array_walk` and `ArrayIterator` over an object, and ReflectionClass's static property reads give the array. Reflection reads a property as `array` by its crossing flag, not by its file's name. - A plain PHP generator held as `Iterable<Map<string, int>>` gives PHP# each element as the Map. The serialize rows test the Map and Set objects again, made with new, and a PHP# helper names the class PHP# holds behind each field.
…unheld nested key A read of a Set constant through a type parameter makes the object each time, since its class is known only when the code runs, and a result of `List<Map<List<int>, int>>` doesn't cross, since its Map's key has no PHP array form. Each row runs a line no other row ran.
The checker refused any Map keyed by int|string into a plain PHP array parameter. PHP# types a plain PHP array as Map<int|string, Any?>, so it refused passing any PHP array back to plain PHP, the missing-super-call help's own code included, and the standard library's Regex, Url and Binary results reached plain PHP as Sharp\Map objects. - Every Map whose keys a PHP array holds crosses as its array, whatever its key type. The checker, the bridge and reflection decide that from the key type through one codex function, get_php_array_key_type. - When the array comes out with fewer entries than the Map, two of its keys were one PHP array key, as 5 and "5" are, or a backed enum case and its backing value. The crossing then throws a ValueError that names both keys, and never drops an entry. A Map with no such keys pays one count compare. - The JIT's static property read saves its opline before the crossing, so the exception handler frees the result it leaves. - Regex.match, Regex.matchAll, Url.parseQuery and Binary.unpack give plain PHP arrays again, and Repository.locals is Map<int|string, Any?> again.
…plain-php Two files conflicted. arguments.rs keeps both import lists. The generated sharp_arginfo.h is rebuilt from the merged sharp.stub.php with build/gen_stub.php.
heyJordanParker
added a commit
that referenced
this pull request
Oct 18, 2026
#163 gives plain PHP the array form of a PHP# Set or Map field, so the rows that read the field's class from plain PHP threw or printed "array". Each row now asks PHP# whether the field holds the object, and expects the array form where plain PHP reads it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A PHP#
MaporSetreached plain PHP as theSharp\MaporSharp\Setobject wherever no parameter typedarrayasked for the array. A plain PHP caller that read a result, a property, a constant or an attribute's argument got an object, soarray_merge,is_arrayand anarraytype hint all failed. Spec section 12 now says plain PHP receives the PHP array at every crossing.What a PHP# developer can now write
Mapis the array of its entries, a backed enum key as its backing value, and aSetis the list of its elements. EachMapandSetinside aListcrosses too.Any,Any?, type parameter orIterableresult or property.->, an index read,get_object_vars,get_mangled_object_vars,get_class_vars, an(array)cast,foreach,array_walkandArrayIteratorover the object,ReflectionProperty::getValue, andReflectionClass::getStaticPropertyValueandgetStaticProperties.empty()tests the array.serialize,var_exportandvar_dumpkeep the object.array.Mapor aSet, witharray, since such a result lowers to\Sharp\Map|array, as a parameter does.Map, and a PHP array plain PHP writes there becomes aMapagain. A plain PHP generator held asIterable<Map<string, int>>gives PHP# each element as theMap.\Countable,\Traversable,objector an interface, takes theMapor theSetitself. One that namesarray,iterableormixed, or declares no type, takes the array.\Sharp\Internal\arrayOfreads the closure at run time.mixed, asAny,Any?or an unbounded type parameter, declares no PHP return type, so a plain PHP parent's untyped method fits it.getMorphClass(), implements a PHP# interface that types it. The class forwards the method with the interface's type.Mapkeyed by a class, a struct, a collection or an enum without backing values reaches plain PHP as theMap, since no PHP array holds its keys. Passing one to anarrayparameter is a compile error.Mapcrosses as its array,int|stringkeys included, so a PHP array plain PHP gave, which PHP# typesMap<int|string, Any?>, passes back to plain PHP. When two of its keys would be one PHP array key, as5and"5", or a backed enum case and its backing value, the crossing throws aValueErrorthat names both: "ThisMapholds both5and"5", which plain PHP keeps as one key, so it can't become a plain PHP array". It never drops an entry.Typed class constants
public const Map<string, int> LIMITS = ["card": 100];,public const [int, string] PAIR = [7, "seven"];andpublic const Iterable<int> STEPS = [1, 2];compile and load. PHP types themarray,arrayanditerable.Iterablehas the declared type.MaporSetconstant makes the object once and shares it, kept under the constant's own key, so a later read costs one lookup. Inside a constant expression, as another constant's value, a literal, an attribute argument or a default, the read is the bare constant, its PHP array, and a property's or parameter's crossing makes the object of it.Functionconstant stays refused. A PHP# constant's value is a literal, a constant,typeof(Class), a collection literal or operators on them. Widening that list is an open question.Engine
ZEND_ACC_SHARP_CROSSINGis a property flag,1 << 16, set when the property's#[\Sharp\Crossing(plan, readsArray)]says plain PHP reads its array. Nofn_flagsbit is used: the bridge marks a crossing result withZEND_SHARP_RESULT_CROSSESin the attr of its result type, and each return and yield of the function emitsZEND_SHARP_RETURN_ARRAY.\Sharp\Crossingtakes an optional second argument,readsArray,falsefor aMapwhose keys no PHP array holds, and anullplan for a property that only crosses outward.\Sharp\Internal\keeptakes its closure optionally, and gives null for a key it holds nothing under.sharp_table_arraychecks for colliding keys with the count: an array with fewer entries than theMapthrows. The JIT's static property read saves its opline before it crosses, so the exception handler frees the result.Iterablecheck converts each element it accepts.sharp/mago: the bridge, the checker and reflection decide from the declared key type, through codex'sget_php_array_key_type, whether a PHP array holds aMap's keys.Tests
collection_crossings.phptruns a row for each crossing, each seen failing first: results, yields, arguments, property reads through each reader, index reads, statics, constants in each constant-expression place, attribute arguments, reflection types, function values,Any, type-parameter andIterableplaces, a plain PHP class implementing and overriding aMapresult, unbacked keys, colliding keys into anarrayparameter, anAnyresult andarrayOf, anint|stringMapwith no collision, aget_object_varsarray passed back to plain PHP, and a timing row that holds aMapconstant read to at most five static reads.collection_crossings_preload.phptruns the plain PHP readers with preloading, colliding static and return reads included.mixed_keys.phptreads itsMap<string|Standing, int>in PHP# and gets theValueErrorin plain PHP. The analyzer'smissing-super-callrow checks that the help's code compiles. The standard library tests readRegex.match,Regex.matchAll,Url.parseQueryandBinary.unpackas arrays again. The PHP# suite passes without opcache, with the file cache, with shared memory, under every optimizer pass and under both JITs. The upstream suite matches its baseline.table_serialize.phptand its preloaded twin serialize theMapandSetobjects again, made withnew, and a PHP# helper names the class behind each field.