[ty] Completely remove the NoReturn shortcut optimization#23378
Open
[ty] Completely remove the NoReturn shortcut optimization#23378
NoReturn shortcut optimization#23378Conversation
Typing conformance resultsNo changes detected ✅Current numbersThe percentage of diagnostics emitted that were expected errors held steady at 85.05%. The percentage of expected errors that received a diagnostic held steady at 78.05%. The number of fully passing files held steady at 63/132. |
Memory usage reportSummary
Significant changesClick to expand detailed breakdownflake8
trio
sphinx
prefect
|
|
Merging this PR will not alter performance
Comparing Footnotes
|
61263d4 to
88cf7b2
Compare
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
call-non-callable |
0 | 2 | 0 |
invalid-argument-type |
0 | 1 | 0 |
invalid-return-type |
0 | 0 | 1 |
type-assertion-failure |
0 | 1 | 0 |
unused-type-ignore-comment |
1 | 0 | 0 |
| Total | 1 | 4 | 1 |
26011a1 to
f87e255
Compare
NoReturn shortcut optimizationNoReturn shortcut optimization
f87e255 to
b679d98
Compare
NoReturn shortcut optimizationNoReturn shortcut optimization
sharkdp
added a commit
that referenced
this pull request
Feb 20, 2026
## Summary
If a generic function's return type depends on a type variable, and the
argument passed resolves that type variable to `Never`, the call should
still be treated as terminal:
```py
def identity[T](x: T) -> T:
return x
def f() -> Never:
identity(exit()) # should be detected as terminal
```
This is a tiny win for correctness, but unfortunately a small drop in
performance and slight increase in memory usage, because we need to
infer more call expressions upfront. If we think it's not important
enough, I'm also okay to change this to a test-only PR that documents
this as a known limitation. If we merge it, I might follow up with an
idea to simplify the code in a [slightly larger refactoring
PR](#23378).
## Memory usage
Insignificant changes on some large internal projects, a 2% *decrease*
in memory usage when running on home-assistant/core.
## Ecosystem
No ecosystem changes, as far as I can tell.
## Test Plan
New Markdown tests
b679d98 to
8ef8dbe
Compare
This comment was marked as resolved.
This comment was marked as resolved.
knutwannheden
pushed a commit
to openrewrite/ruff
that referenced
this pull request
Feb 20, 2026
## Summary
If a generic function's return type depends on a type variable, and the
argument passed resolves that type variable to `Never`, the call should
still be treated as terminal:
```py
def identity[T](x: T) -> T:
return x
def f() -> Never:
identity(exit()) # should be detected as terminal
```
This is a tiny win for correctness, but unfortunately a small drop in
performance and slight increase in memory usage, because we need to
infer more call expressions upfront. If we think it's not important
enough, I'm also okay to change this to a test-only PR that documents
this as a known limitation. If we merge it, I might follow up with an
idea to simplify the code in a [slightly larger refactoring
PR](astral-sh#23378).
## Memory usage
Insignificant changes on some large internal projects, a 2% *decrease*
in memory usage when running on home-assistant/core.
## Ecosystem
No ecosystem changes, as far as I can tell.
## Test Plan
New Markdown tests
62840c1 to
7836851
Compare
This comment was marked as outdated.
This comment was marked as outdated.
7836851 to
4eb2a72
Compare
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.
Summary
Function calls are relevant for control flow analysis because they may return
Never/NoReturn, and can therefore be terminal. In order to support this, we recordReturnsNever(…)constraints during semantic index building for statement-level calls (in almost all situations). These constraints keep track of the call expression such that they can be evaluated during reachability analysis and type narrowing.For example, if we see a call
f(a, b, c), we keep track of this full call expression. The return type could depend on the arguments (overloads, generics), so to determine if a call is terminal, we need to evaluate the full call expression.For performance reasons, this analysis contained a short-cut where we looked at the return type annotation of the invoked callable (
f). Under certain conditions, we could immediately see that a call would definitely be terminal. This previously helped with performance, but is now apparently detrimental.The optimization recently caused problems for generic function calls, so we had to exclude those. It now turns out there was another bug, as I figured out by looking at the ecosystem results on this PR. When the callable expression could not be upcasted to a callable type, we assumed that the call would never be terminal. However, if the callable itself is
Never, that should also be considered a terminal call. This can happen in unreachable code. Consider this rather weird case, that I extracted from a hydpy ecosystem hit:On
main, the revealed type ofxisLiteral["a"], which is wrong. Since thefail()call itself happens in unreachable code, we inferNeverforfailitself, and therefore considered theif x == "a"branch to not be terminal. On this branch, that bug is fixed and we correctly revealNeverfor the type ofx.So the idea here is to get rid of this optimization all together and to simply evaluate the full call expression in all cases, which also makes this much easier to reason about.
(I find the
AlwaysFalse/AlwaysTrue-answer of this evaluation very rather confusing, because it's sort of negated twice; I plan to change this in a follow-up)Performance
We previously merged #19867 to address a performance problem in this part of the codebase. It seems to me that the original problem was related to the short-cut path, but I'm not sure if this could bring back the lock congestion problem.
In any case, this PR seems to be a performance win across the board on our usual benchmarks. And there is also a small decrease in memory usage.
Ecosystem impact
The disappearing diagnostics all seem to be related to cases like above, where the terminal call happened in unreachable code.
Test Plan
Existing tests.