Short-circuit NoReturn analysis for Any or Unknown calls - #11419
Short-circuit NoReturn analysis for Any or Unknown calls#11419Bill Schnurr (bschnurr) wants to merge 3 commits into
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| if (isUnion(type)) { | ||
| return type.priv.subtypes.every((subtype) => { | ||
| callback(subtype); | ||
| return callback(subtype); |
There was a problem hiding this comment.
every expects the return result from callback
| const subtypeLiteralTypeName = getLiteralTypeClassName(subtype); | ||
| if (!subtypeLiteralTypeName) { | ||
| foundMismatch = true; | ||
| return undefined; |
There was a problem hiding this comment.
use early exit
This comment has been minimized.
This comment has been minimized.
a98974d to
0e65014
Compare
|
🔒 Automated review in progress — Rich Chiodo (@rchiodo) is auto-reviewing this PR. |
|
Verification: The relevant tests could not be fully run in the isolated environment; this review is not fully verified. |
Rich Chiodo (rchiodo)
left a comment
There was a problem hiding this comment.
Approved via Review Center.
0e65014 to
618ec5f
Compare
|
/benchmark |
618ec5f to
25e1ec9
Compare
|
/benchmark |
1 similar comment
|
/benchmark |
25e1ec9 to
ae76d44
Compare
|
/benchmark |
This comment has been minimized.
This comment has been minimized.
|
Verification: The relevant tests could not be fully run in the isolated environment; this review is not fully verified. |
This comment has been minimized.
This comment has been minimized.
|
/benchmark |
Document that a union call target containing Any or Unknown does not make subsequent code unreachable.
ae76d44 to
d9a9a3d
Compare
|
Verification: The relevant tests could not be fully run in the isolated environment; this review is not fully verified. |
This comment has been minimized.
This comment has been minimized.
|
/benchmark |
This comment has been minimized.
This comment has been minimized.
Stop scanning a callable union once Any or Unknown proves the call cannot be guaranteed to return NoReturn.
This comment has been minimized.
This comment has been minimized.
|
/benchmark |
This comment has been minimized.
This comment has been minimized.
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Summary
AnyorUnknown.NoReturn | UnknownandNoReturn | Anycall targets.Why the early exit is correct
A call is considered
NoReturnonly when every possible call-target subtype is known not to return normally.AnyandUnknowncan represent aNoReturncallable, but they can also represent a callable that returns normally. They therefore provide insufficient information to prove that the call does not return.This is also equivalent to the previous counting implementation. Each subtype increments
subtypeCount, but anAnyorUnknownsubtype matches none of the cases that incrementnoReturnTypeCount. Once one is encountered,noReturnTypeCount === subtypeCountcannot be true, even if every remaining subtype isNoReturn. Returningfalseimmediately preserves the existing reachability result while avoiding unnecessary analysis of later union members.Any reproducible mypy_primer diagnostic differences should be reviewed for correctness rather than treated as regressions solely because diagnostics changed.
The separate
typeUtilshelper optimization remains in #11681.Validation
pnpm --dir packages/pyright-internal exec jest typeEvaluator1.test -t "Unreachable1" --forceExitpnpm --dir packages/pyright-internal run buildpnpm exec prettier --check packages/pyright-internal/src/analyzer/codeFlowEngine.ts