Skip to content

Commit 3dc78b0

Browse files
[ty] Use HasOptionalDefinition for except handlers (#23739)
## Summary See: #23708 (comment).
1 parent a6a5e8d commit 3dc78b0

3 files changed

Lines changed: 33 additions & 36 deletions

File tree

crates/ty_ide/src/goto.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ use ty_python_semantic::types::ide_support::{
2121
typed_dict_key_definition,
2222
};
2323
use ty_python_semantic::{
24-
HasDefinition, HasType, ImportAliasResolution, SemanticModel, TypeQualifiers,
25-
definitions_for_imported_symbol, definitions_for_name,
24+
HasDefinition, HasOptionalDefinition, HasType, ImportAliasResolution, SemanticModel,
25+
TypeQualifiers, definitions_for_imported_symbol, definitions_for_name,
2626
};
2727

2828
#[derive(Clone, Debug)]
@@ -331,7 +331,7 @@ impl GotoTarget<'_> {
331331
GotoTarget::ImportSymbolAlias { alias, .. }
332332
| GotoTarget::ImportModuleAlias { alias, .. }
333333
| GotoTarget::ImportExportedName { alias, .. } => alias.inferred_type(model),
334-
GotoTarget::ExceptVariable(except) => model.except_handler_type(except),
334+
GotoTarget::ExceptVariable(except) => except.inferred_type(model),
335335
GotoTarget::KeywordArgument { keyword, .. } => keyword.value.inferred_type(model),
336336
// When asking the type of a callable, usually you want the callable itself?
337337
// (i.e. the type of `MyClass` in `MyClass()` is `<class MyClass>` and not `() -> MyClass`)
@@ -515,8 +515,8 @@ impl GotoTarget<'_> {
515515
)),
516516

517517
// For exception variables, they are their own definitions (like parameters)
518-
GotoTarget::ExceptVariable(except_handler) => model
519-
.except_handler_definition(except_handler)
518+
GotoTarget::ExceptVariable(except_handler) => except_handler
519+
.optional_definition(model)
520520
.map(|definition| vec![ResolvedDefinition::Definition(definition)]),
521521

522522
// Patterns are glorified assignments but we have to look them up by ident

crates/ty_python_semantic/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ pub use program::{Program, ProgramSettings};
1414
pub use python_platform::PythonPlatform;
1515
use rustc_hash::FxHasher;
1616
pub use semantic_model::{
17-
Completion, HasDefinition, HasType, MemberDefinition, NameKind, SemanticModel,
17+
Completion, HasDefinition, HasOptionalDefinition, HasType, MemberDefinition, NameKind,
18+
SemanticModel,
1819
};
1920
pub use suppression::{
2021
UNUSED_IGNORE_COMMENT, is_unused_ignore_comment_lint, suppress_all, suppress_single,

crates/ty_python_semantic/src/semantic_model.rs

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -304,14 +304,10 @@ impl<'db> SemanticModel<'db> {
304304
.scope(self.db)
305305
.file_scope_id(self.db),
306306
),
307-
ast::AnyNodeRef::ExceptHandlerExceptHandler(handler) => self
308-
.except_handler_definition(handler)
307+
ast::AnyNodeRef::ExceptHandlerExceptHandler(handler) => handler
308+
.optional_definition(self)
309309
.map(|definition| definition.scope(self.db).file_scope_id(self.db))
310-
.or_else(|| {
311-
handler.type_.as_deref().and_then(|handled_exceptions| {
312-
index.try_expression_scope_id(handled_exceptions)
313-
})
314-
})
310+
.or_else(|| index.try_expression_scope_id(handler.type_.as_deref()?))
315311
.or(Some(FileScopeId::global())),
316312
ast::AnyNodeRef::TypeParamTypeVar(var) => {
317313
Some(var.definition(self).scope(self.db).file_scope_id(self.db))
@@ -328,29 +324,6 @@ impl<'db> SemanticModel<'db> {
328324
}
329325
}
330326

331-
/// Returns the definition for an exception-handler variable.
332-
///
333-
/// Exception handlers only have a definition when they bind a name (`except E as name:`).
334-
pub fn except_handler_definition(
335-
&self,
336-
handler: &ast::ExceptHandlerExceptHandler,
337-
) -> Option<Definition<'db>> {
338-
handler.name.as_ref()?;
339-
let index = semantic_index(self.db, self.file);
340-
Some(index.expect_single_definition(handler))
341-
}
342-
343-
/// Returns the inferred type of an exception-handler variable.
344-
///
345-
/// Exception handlers only bind a variable when they have a name (`except E as name:`).
346-
pub fn except_handler_type(
347-
&self,
348-
handler: &ast::ExceptHandlerExceptHandler,
349-
) -> Option<Type<'db>> {
350-
let definition = self.except_handler_definition(handler)?;
351-
Some(binding_type(self.db, definition))
352-
}
353-
354327
/// Get a "safe" [`ast::AnyNodeRef`] to use for referring to the given (sub-)AST node.
355328
///
356329
/// If we're analyzing a string annotation, it will return the string literal's node.
@@ -543,6 +516,14 @@ pub trait HasDefinition {
543516
fn definition<'db>(&self, model: &SemanticModel<'db>) -> Definition<'db>;
544517
}
545518

519+
pub trait HasOptionalDefinition {
520+
/// Returns the definition of `self`, if it has one.
521+
///
522+
/// ## Panics
523+
/// May panic if `self` is from another file than `model`.
524+
fn optional_definition<'db>(&self, model: &SemanticModel<'db>) -> Option<Definition<'db>>;
525+
}
526+
546527
impl HasType for ast::ExprRef<'_> {
547528
fn inferred_type<'db>(&self, model: &SemanticModel<'db>) -> Option<Type<'db>> {
548529
let index = semantic_index(model.db, model.file);
@@ -679,6 +660,21 @@ impl HasType for ast::Alias {
679660
}
680661
}
681662

663+
impl HasOptionalDefinition for ast::ExceptHandlerExceptHandler {
664+
fn optional_definition<'db>(&self, model: &SemanticModel<'db>) -> Option<Definition<'db>> {
665+
self.name.as_ref()?;
666+
let index = semantic_index(model.db, model.file);
667+
Some(index.expect_single_definition(self))
668+
}
669+
}
670+
671+
impl HasType for ast::ExceptHandlerExceptHandler {
672+
fn inferred_type<'db>(&self, model: &SemanticModel<'db>) -> Option<Type<'db>> {
673+
let definition = self.optional_definition(model)?;
674+
Some(binding_type(model.db, definition))
675+
}
676+
}
677+
682678
/// Implemented by types for which the semantic index tracks their scope.
683679
pub(crate) trait HasTrackedScope: HasNodeIndex {}
684680

0 commit comments

Comments
 (0)