Elide provably-passing return type checks at compile time - #23305
Open
staabm wants to merge 1 commit into
Open
Conversation
Without the opcache optimizer every 'return <expr>;' in a typed function executes ZEND_VERIFY_RETURN_TYPE. Skip emitting it when the check provably always passes: - expressions whose opcode always produces a specific type accepted by the return type: comparisons, instanceof, isset/empty and boolean operators produce bool; concatenation produces string; - 'return $this->prop;' where the property is declared in the class being compiled and its type is a subtype of the return type. Property types are invariant under inheritance, and typed property reads always yield conforming values: uninitialized reads throw before the return, and __get()/get-hook results are verified against the property type. Only same-class declarations are visible during method compilation, which keeps the proof local. Closures are excluded (Closure::bind can rebind scopes), as are by-ref returns, generators, union/intersection property types and context-dependent return types (static, callable). Coercion semantics are preserved by requiring the value type set to be a subset of the return type set (e.g. an int property returned as float still emits the check). Getter-style code drops one opcode per return; 'return $x === null;' style predicates drop the check entirely.
staabm
referenced
this pull request
Aug 16, 2026
Without the opcache optimizer every 'return <expr>;' in a typed function executes ZEND_VERIFY_RETURN_TYPE. Skip emitting it when the check provably always passes: - expressions whose opcode always produces a specific type accepted by the return type: comparisons, instanceof, isset/empty and boolean operators produce bool; concatenation produces string; - 'return $this->prop;' where the property is declared in the class being compiled and its type is a subtype of the return type. Property types are invariant under inheritance, and typed property reads always yield conforming values: uninitialized reads throw before the return, and __get()/get-hook results are verified against the property type. Only same-class declarations are visible during method compilation, which keeps the proof local. Closures are excluded (Closure::bind can rebind scopes), as are by-ref returns, generators, union/intersection property types and context-dependent return types (static, callable). Coercion semantics are preserved by requiring the value type set to be a subset of the return type set (e.g. an int property returned as float still emits the check). Getter-style code drops one opcode per return; 'return $x === null;' style predicates drop the check entirely.
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.
disclaimer: this change was generated by claude opus. I have little experience with php-src development
Without the opcache optimizer every 'return ;' in a typed function executes ZEND_VERIFY_RETURN_TYPE. Skip emitting it when the check provably always passes:
Closures are excluded (Closure::bind can rebind scopes), as are by-ref returns, generators, union/intersection property types and context-dependent return types (static, callable). Coercion semantics are preserved by requiring the value type set to be a subset of the return type set (e.g. an int property returned as float still emits the check).
Getter-style code drops one opcode per return; 'return $x === null;' style predicates drop the check entirely.
before PR
after PR
using return_type_check_elision_repro.php