Skip to content

fix: a type check loads a class the first time it needs the class's declaration - #149

Merged
heyJordanParker merged 3 commits into
masterfrom
fix/type-checks-autoload
Oct 16, 2026
Merged

heyJordanParker merged 3 commits into
masterfrom
fix/type-checks-autoload

Conversation

@heyJordanParker

Copy link
Copy Markdown
Owner

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.

  • An out type argument. Stream<Order> was refused for Feed<DatabaseEntity> until some code loaded Order.
  • A backed enum key. A plain PHP array keyed "ripe" was refused for a Map<Ripeness, int> property until some code loaded Ripeness.
  • A function type. A closure that returns Kennel<Dog> was no Function<Kennel<Animal>()> until some code loaded Kennel.

Problem

A type argument names a class as text. new Stream<Order>() stores App.Order on the object and loads no Order. 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: what Order extends, which case of Ripeness "ripe" backs, whether Kennel's parameter is out. ext/sharp/sharp.c looked each class up with ZEND_FETCH_CLASS_NO_AUTOLOAD and 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 any Order does.

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/files brings, 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 and sharp_type_within read through, uses it. sharp_type_fits now 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_key uses it for the enum a Map key type names.
  • sharp_class_value_is uses it for the class a class value names. It already ran the autoloader, through zend_lookup_class.
  • An exception the autoloader throws reaches the caller, as from 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_accepts and Sharp\Internal\accepts. Once an exception is pending, sharp_type_class only looks classes up and runs no more user code.
  • A class the autoloader can't find gives the same TypeError as before, naming the class.

The other ZEND_FETCH_CLASS_NO_AUTOLOAD lookups in sharp.c stay 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 loaded Defaults trait.
  • 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 of Class<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:

  • A checked array is a copy: the property write's ZVAL_COPY, the callee's argument slot, the return's temporary, or accepts'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.
  • A Sharp\Map or Sharp\Set is held while it is walked, as before.
  • The type nodes live in the check's own arena, which no user code reaches.
  • The spelled lists the check reads were spelled before the walk, and a nested check that runs from the autoloader only adds to their table.
  • The engine never autoloads while compiling, so a check that runs during compilation stays a plain lookup.
  • During the preload request the autoloader runs as in any request, and preloading keeps each class it loads.

Verification

  • New test, on master's layout: Zend/tests/sharp/type_checks_autoload.phpt. An autoloader loads one fixture file per class of namespace Orchard, logs each class it is asked for, throws for Quince, and finds no file for Medlar. On master, the 3 rows and the throwing autoloader fail:
    • Picker::weigh(Picker::apples()), a Crate<Apple> for Harvest<Fruit>: master throws "must be of type Orchard.Harvest<Orchard.Fruit>, Orchard.Crate<Orchard.Apple> given", and the fix prints weighed.
    • A plain PHP write of ['ripe' => 3] to Stall::$stock, a Map<Ripeness, int>: master throws "key expected Orchard.Ripeness, got string 'ripe'", and the fix stores 3.
    • () => new Tray<Pear>() is Function<Tray<Fruit>()>: master answers false, and the fix true.
    • An autoloader that throws: master throws a TypeError, and the fix lets the autoloader's RuntimeException through.
    • A class with no file gives the same TypeError on both.
  • Preloading: Zend/tests/sharp/type_checks_autoload_preload.phpt checks an out type argument and a backed enum key during the preload request. A request then finds Apple and Ripeness preloaded, and asks the autoloader only for the classes preloading didn't load.
  • Scribbled memory: each test's rows ran 200 times under USE_ZEND_ALLOC=0 MallocScribble=1, preloaded and not, and all 200 printed the same output as the unscribbled run.
  • Engine tests: sharp/bin/test --opcache Zend/tests/sharp passes 296 of 296 in each of its 3 modes on macOS. sharp/bin/test --opcache --repeat --passes passes the two new tests, function_types.phpt, type_arguments_entry_variance.phpt and table_property_writes.phpt in all 22 modes.

Performance

Release builds, configured with --disable-all and no --enable-debug, with opcache.enable_cli=1, master 3bf73ce against this branch.

  • One million checked property writes, fix: a preload script that reads a module, calls a checked method or emits leaves the request working #146's loop, which sets Checks\Tally::$amounts, a List<float>, from plain PHP:
    • Wall time, 4 alternating runs each: master 46.5 to 48.0 ms, this branch 46.9 to 47.8 ms.
    • Instructions retired for the whole process, from /usr/bin/time -l: master 1055.8M and 1057.5M, this branch 1050.0M and 1049.3M.
  • One million checked calls whose check looks up a class, Picker::weigh($crate) with a Crate<Apple> from plain PHP, after Apple is loaded:
    • Wall time: master 425.8 and 426.2 ms, this branch 429.3 and 428.1 ms.
    • Instructions retired: master 7809.7M and 7808.3M, this branch 7808.4M and 7807.8M.

A lookup of a loaded class takes the same path through zend_lookup_class_ex with the autoloader allowed as without it, so both loops are inside master's own spread.

Known limits

  • A class the autoloader can't find is asked for twice in one failed check from plain PHP. sharp_type_accepts walks 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.
  • The preload test checks no function type during the preload request. A closure that a class method makes during the preload request trips ZEND_ASSERT(!(op_array->fn_flags & ZEND_ACC_CLOSURE) || !(op_array->scope)) in zend_jit, ext/opcache/jit/zend_jit.c line 1539, under opcache.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.

…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
heyJordanParker merged commit 3dacb57 into master Oct 16, 2026
19 checks passed
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.
@heyJordanParker
heyJordanParker deleted the fix/type-checks-autoload branch November 3, 2026 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant