[ty] Correctly handle calls to functions marked as returning Never / NoReturn (#18333)

## Summary

`ty` does not understand that calls to functions which have been
annotated as having a return type of `Never` / `NoReturn` are terminal.

This PR fixes that, by adding new reachability constraints when call
expressions are seen. If the call expression evaluates to `Never`, the
code following it will be considered to be unreachable. Note that, for
adding these constraints, we only consider call expressions at the
statement level, and that too only inside function scopes. This is
because otherwise, the number of such constraints becomes too high, and
evaluating them later on during type inference results in a major
performance degradation.

Fixes https://github.com/astral-sh/ty/issues/180

## Test Plan

New mdtests.

## Ecosystem changes

This PR removes the following false-positives:
- "Function can implicitly return `None`, which is not assignable to
...".
- "Name `foo` used when possibly not defind" - because the branch in
which it is not defined has a `NoReturn` call, or when `foo` was
imported in a `try`, and the except had a `NoReturn` call.

---------

Co-authored-by: David Peter <mail@david-peter.de>
This commit is contained in:
Abhijeet Prasad Bodas 2025-07-05 00:22:52 +05:30 committed by GitHub
parent a33cff2b12
commit f4bd74ab6a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 437 additions and 72 deletions

View file

@ -35,8 +35,8 @@ use crate::semantic_index::place::{
PlaceExprWithFlags, PlaceTableBuilder, Scope, ScopeId, ScopeKind, ScopedPlaceId,
};
use crate::semantic_index::predicate::{
PatternPredicate, PatternPredicateKind, Predicate, PredicateNode, PredicateOrLiteral,
ScopedPredicateId, StarImportPlaceholderPredicate,
CallableAndCallExpr, PatternPredicate, PatternPredicateKind, Predicate, PredicateNode,
PredicateOrLiteral, ScopedPredicateId, StarImportPlaceholderPredicate,
};
use crate::semantic_index::re_exports::exported_names;
use crate::semantic_index::reachability_constraints::{
@ -1901,11 +1901,45 @@ impl<'ast> Visitor<'ast> for SemanticIndexBuilder<'_, 'ast> {
value,
range: _,
node_index: _,
}) if self.in_module_scope() => {
if let Some(expr) = dunder_all_extend_argument(value) {
self.add_standalone_expression(expr);
}) => {
if self.in_module_scope() {
if let Some(expr) = dunder_all_extend_argument(value) {
self.add_standalone_expression(expr);
}
}
self.visit_expr(value);
// If the statement is a call, it could possibly be a call to a function
// marked with `NoReturn` (for example, `sys.exit()`). In this case, we use a special
// kind of constraint to mark the following code as unreachable.
//
// Ideally, these constraints should be added for every call expression, even those in
// sub-expressions and in the module-level scope. But doing so makes the number of
// such constraints so high that it significantly degrades performance. We thus cut
// scope here and add these constraints only at statement level function calls,
// like `sys.exit()`, and not within sub-expression like `3 + sys.exit()` etc.
//
// We also only add these inside function scopes, since considering module-level
// constraints can affect the the type of imported symbols, leading to a lot more
// work in third-party code.
if let ast::Expr::Call(ast::ExprCall { func, .. }) = value.as_ref() {
if !self.source_type.is_stub() && self.in_function_scope() {
let callable = self.add_standalone_expression(func);
let call_expr = self.add_standalone_expression(value.as_ref());
let predicate = Predicate {
node: PredicateNode::ReturnsNever(CallableAndCallExpr {
callable,
call_expr,
}),
is_positive: false,
};
self.record_reachability_constraint(PredicateOrLiteral::Predicate(
predicate,
));
}
}
}
_ => {
walk_stmt(self, stmt);