Repository navigation
fix: a type check loads a class the first time it needs the class's declaration - #149
Merged
Merged
Conversation
…eclaration A type argument, a Map key type and a function type name classes as text, so a check could meet a class no code had loaded yet. It looked each class up without the autoloader and answered "does not fit": Stream<Order> was refused for Feed<DatabaseEntity> until Order loaded, a plain PHP array keyed "ripe" was refused for Map<Ripeness, int> until Ripeness loaded, and a closure returning Kennel<Dog> was no Function<Kennel<Animal>()> until Kennel loaded. Every Composer app, which loads each class when code first names it, hit this. sharp_type_class is the one lookup these checks share. It runs the autoloader the first time, as Java and C# load a class on first use, and only looks the class up once an exception is pending. The autoloader's exception reaches the check's caller, and a class it finds no file for gives the TypeError it gave before.
heyJordanParker
added a commit
that referenced
this pull request
Oct 16, 2026
Master brings #142, #146, #147, #148 and #149. Each conflict keeps both sides' behavior: - A lambda's result type: master's `AnalysisArtifacts::lambda_result` is the one mechanism. `try` in a lambda reads it, and #143's `BlockContext::lambda_result_type` with its setters is gone. - A `match`'s declared type reaching its PHP and its arms: master's code, with `Expression::Match` kept in `takes_declared_type`, without which the PHP `match` takes no declared type. - `==` on a struct or a case: master's per-side type arguments for a generic struct, and a generic enum's case fields read the enum's type arguments from the side that gives them, through `with_type_arguments`. - A call's declared type: master's `declared_return_type` goes through `as_declared_ancestor`, so `Result.Ok(…)` into a `Result<int, E>` fixes the case's type arguments for the arguments and for the result. - The bridge: master's `generic_statics`, with `class_metadata` taking the class it describes, as a generic enum's case needs. - Spec section 11 keeps master's inference bullets and #143's rule for a static call of a generic class.
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.
Fixes: a type check refused a valid value whenever a class the check needed hadn't loaded yet, which every Composer app hits, since Composer loads each class when code first names it.
Stream<Order>was refused forFeed<DatabaseEntity>until some code loadedOrder."ripe"was refused for aMap<Ripeness, int>property until some code loadedRipeness.Kennel<Dog>was noFunction<Kennel<Animal>()>until some code loadedKennel.Problem
A type argument names a class as text.
new Stream<Order>()storesApp.Orderon the object and loads noOrder. A key type, a function type and a class value name their classes as text too. To answer, the check needs the class's declaration: whatOrderextends, which case ofRipeness"ripe"backs, whetherKennel's parameter isout.ext/sharp/sharp.clooked each class up withZEND_FETCH_CLASS_NO_AUTOLOADand read a class that isn't loaded as one that doesn't fit.PHP's own type checks never autoload, and they are right not to: an object of a class that isn't loaded can't exist. A PHP# type argument breaks that premise, because
Stream<Order>exists before anyOrderdoes.The engine tests didn't catch it, because each fixture declared all its types in one file, so every class was loaded before any check ran. One type per file, which
feat/filesbrings, turned the bug into 3 failing engine tests there.Solution
sharp_type_class(name)is the one lookup the checks share. It loads the class through the autoloader the first time a check needs the class's declaration, as Java and C# load a class on first use. A loaded class stays in the class table, so the autoloader runs once for it. After that, the check costs the same hash lookup as before.sharp_type_node_class, which variance, bounds, function types andsharp_type_withinread through, uses it.sharp_type_fitsnow loads the type's class before the bound's. Loading the type's class loads every class it extends or implements, so the bound's class is loaded whenever it is one of them.sharp_type_holds_keyuses it for the enum aMapkey type names.sharp_class_value_isuses it for the class a class value names. It already ran the autoloader, throughzend_lookup_class.new. The lookup returns NULL, the check answers false, and every entry point already skips its TypeError while an exception is pending:sharp_type_check_arguments,sharp_type_verify,sharp_property_acceptsandSharp\Internal\accepts. Once an exception is pending,sharp_type_classonly looks classes up and runs no more user code.The other
ZEND_FETCH_CLASS_NO_AUTOLOADlookups insharp.cstay as they are. Each looks up a class that an already loaded class or object names, so the class is always loaded and no autoloader could change the answer:sharp_type_node_ancestor: the classes a loaded class's header names.sharp_type_scope: a loaded class's traits, and the interface of a loadedDefaultstrait.sharp_class_uses_trait: a linked class's traits.sharp_object_is: the class an object must be an instance of. A class that isn't loaded can have no instance.sharp_class_value_is: the class ofClass<T>, once the named class is loaded.sharp_hears: an event's listener type.Loading a class mid-check is safe. The autoloader runs while a check walks a value:
ZVAL_COPY, the callee's argument slot, the return's temporary, oraccepts's parameter. So a write the autoloader makes to the variable it came from separates first. The test's autoloader writes to the very array a property check is walking, and the property keeps the array as it was written.Sharp\MaporSharp\Setis held while it is walked, as before.Verification
Zend/tests/sharp/type_checks_autoload.phpt. An autoloader loads one fixture file per class of namespaceOrchard, logs each class it is asked for, throws forQuince, and finds no file forMedlar. On master, the 3 rows and the throwing autoloader fail:Picker::weigh(Picker::apples()), aCrate<Apple>forHarvest<Fruit>: master throws "must be of type Orchard.Harvest<Orchard.Fruit>, Orchard.Crate<Orchard.Apple> given", and the fix printsweighed.['ripe' => 3]toStall::$stock, aMap<Ripeness, int>: master throws "key expected Orchard.Ripeness, got string 'ripe'", and the fix stores 3.() => new Tray<Pear>()isFunction<Tray<Fruit>()>: master answers false, and the fix true.Zend/tests/sharp/type_checks_autoload_preload.phptchecks an out type argument and a backed enum key during the preload request. A request then findsAppleandRipenesspreloaded, and asks the autoloader only for the classes preloading didn't load.USE_ZEND_ALLOC=0 MallocScribble=1, preloaded and not, and all 200 printed the same output as the unscribbled run.sharp/bin/test --opcache Zend/tests/sharppasses 296 of 296 in each of its 3 modes on macOS.sharp/bin/test --opcache --repeat --passespasses the two new tests,function_types.phpt,type_arguments_entry_variance.phptandtable_property_writes.phptin all 22 modes.Performance
Release builds, configured with
--disable-alland no--enable-debug, withopcache.enable_cli=1, master 3bf73ce against this branch.Checks\Tally::$amounts, aList<float>, from plain PHP:/usr/bin/time -l: master 1055.8M and 1057.5M, this branch 1050.0M and 1049.3M.Picker::weigh($crate)with aCrate<Apple>from plain PHP, afterAppleis loaded:A lookup of a loaded class takes the same path through
zend_lookup_class_exwith the autoloader allowed as without it, so both loops are inside master's own spread.Known limits
sharp_type_acceptswalks the value a second time to take ints as floats, and PHP keeps no record of a class the autoloader didn't find. A class that loads is asked for once. The test's log pins both.ZEND_ASSERT(!(op_array->fn_flags & ZEND_ACC_CLOSURE) || !(op_array->scope))inzend_jit,ext/opcache/jit/zend_jit.cline 1539, underopcache.jit=function. A plain PHP class does the same, on this branch and without PHP#, so it is a separate bug. The request after the preload checks the function type.