Skip to content

Optimizer: resolve arg/prop/return class hints via op_array scope - #23475

Open
jorgsowa wants to merge 2 commits into
php:masterfrom
jorgsowa:optimizer/thread-op-array-class-resolution
Open

jorgsowa wants to merge 2 commits into
php:masterfrom
jorgsowa:optimizer/thread-op-array-class-resolution

Conversation

@jorgsowa

@jorgsowa jorgsowa commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

zend_convert_type() called zend_optimizer_get_class_entry() with a NULL op_array (marked with a TODO). This threads the op_array through, so a class hint that names the current class can resolve via op_array->scope when that class is unlinked in the script: it's conditionally declared, or its parent isn't known at compile time (e.g. it lives in another file). self/parent aren't affected; they're already replaced with real names at compile time wherever the scope is known. The fallback is sound because code running in scope X means X is the class declared under that name.

The previous commit passes the op_array through zend_convert_type(),
which used to call zend_optimizer_get_class_entry() with a NULL
op_array. This lets a class hint naming the current class resolve via
op_array->scope when that class is unlinked in the script, i.e. it is
conditionally declared or its parent is not known at compile time.
self/parent are unaffected: they are substituted at compile time
wherever the scope is known. The fallback is sound because code
executing in scope X implies that X is the class declared under that
name.
@jorgsowa
jorgsowa force-pushed the optimizer/thread-op-array-class-resolution branch from 74d5f65 to e359b00 Compare October 16, 2026 21:03
@jorgsowa

Copy link
Copy Markdown
Contributor Author

Follow-up commit:

  • Added the test ext/opcache/tests/opt/type_inference_unlinked_scope.phpt.
  • Trace JIT: the two call sites that only use the type mask no longer look up the class.
  • Replaced the cast with ZEND_USER_CODE() / &func->op_array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant