-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[ty] Fix panic on incomplete except handlers #23708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -285,33 +285,48 @@ impl<'db> SemanticModel<'db> { | |
| // Nodes implementing `HasDefinition` | ||
| ast::AnyNodeRef::StmtFunctionDef(function) => Some( | ||
| function | ||
| .definition(self) | ||
| .definition(self)? | ||
| .scope(self.db) | ||
| .file_scope_id(self.db), | ||
| ), | ||
| ast::AnyNodeRef::StmtClassDef(class) => { | ||
| Some(class.definition(self).scope(self.db).file_scope_id(self.db)) | ||
| } | ||
| ast::AnyNodeRef::Parameter(parameter) => Some( | ||
| parameter | ||
| .definition(self) | ||
| ast::AnyNodeRef::StmtClassDef(class) => Some( | ||
| class | ||
| .definition(self)? | ||
| .scope(self.db) | ||
| .file_scope_id(self.db), | ||
| ), | ||
| ast::AnyNodeRef::ParameterWithDefault(parameter) => Some( | ||
| ast::AnyNodeRef::Parameter(parameter) => Some( | ||
| parameter | ||
| .definition(self) | ||
| .definition(self)? | ||
| .scope(self.db) | ||
| .file_scope_id(self.db), | ||
| ), | ||
| ast::AnyNodeRef::ExceptHandlerExceptHandler(handler) => Some( | ||
| handler | ||
| .definition(self) | ||
| ast::AnyNodeRef::ParameterWithDefault(parameter) => Some( | ||
| parameter | ||
| .definition(self)? | ||
| .scope(self.db) | ||
| .file_scope_id(self.db), | ||
| ), | ||
| ast::AnyNodeRef::ExceptHandlerExceptHandler(handler) => { | ||
| if handler.name.is_some() { | ||
| Some( | ||
| handler | ||
| .definition(self)? | ||
| .scope(self.db) | ||
| .file_scope_id(self.db), | ||
| ) | ||
| } else { | ||
| handler | ||
| .type_ | ||
| .as_deref() | ||
| .and_then(|handled_exceptions| { | ||
| index.try_expression_scope_id(handled_exceptions) | ||
| }) | ||
| .or(Some(FileScopeId::global())) | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| } | ||
| ast::AnyNodeRef::TypeParamTypeVar(var) => { | ||
| Some(var.definition(self).scope(self.db).file_scope_id(self.db)) | ||
| Some(var.definition(self)?.scope(self.db).file_scope_id(self.db)) | ||
| } | ||
|
|
||
| // Fallback | ||
|
|
@@ -514,7 +529,7 @@ pub trait HasDefinition { | |
| /// | ||
| /// ## Panics | ||
| /// May panic if `self` is from another file than `model`. | ||
| fn definition<'db>(&self, model: &SemanticModel<'db>) -> Definition<'db>; | ||
| fn definition<'db>(&self, model: &SemanticModel<'db>) -> Option<Definition<'db>>; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the docs here be updated to say when callers ought to return |
||
| } | ||
|
|
||
| impl HasType for ast::ExprRef<'_> { | ||
|
|
@@ -621,16 +636,16 @@ macro_rules! impl_binding_has_ty_def { | |
| ($ty: ty) => { | ||
| impl HasDefinition for $ty { | ||
| #[inline] | ||
| fn definition<'db>(&self, model: &SemanticModel<'db>) -> Definition<'db> { | ||
| fn definition<'db>(&self, model: &SemanticModel<'db>) -> Option<Definition<'db>> { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit unfortunate. Have you explored whether we could always create a definition in
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hah, isn't this what you suggested in astral-sh/ty#2401 (comment)?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will take a look.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Haha, I completely forgot about that. It's certainly the easiest change :)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could also consider removing the
Ahhh... this doesn't work because
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I changed things up to remove |
||
| let index = semantic_index(model.db, model.file); | ||
| index.expect_single_definition(self) | ||
| Some(index.expect_single_definition(self)) | ||
| } | ||
| } | ||
|
|
||
| impl HasType for $ty { | ||
| #[inline] | ||
| fn inferred_type<'db>(&self, model: &SemanticModel<'db>) -> Option<Type<'db>> { | ||
| let binding = HasDefinition::definition(self, model); | ||
| let binding = HasDefinition::definition(self, model)?; | ||
| Some(binding_type(model.db, binding)) | ||
| } | ||
| } | ||
|
|
@@ -641,9 +656,25 @@ impl_binding_has_ty_def!(ast::StmtFunctionDef); | |
| impl_binding_has_ty_def!(ast::StmtClassDef); | ||
| impl_binding_has_ty_def!(ast::Parameter); | ||
| impl_binding_has_ty_def!(ast::ParameterWithDefault); | ||
| impl_binding_has_ty_def!(ast::ExceptHandlerExceptHandler); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it was necessary to remove both the |
||
| impl_binding_has_ty_def!(ast::TypeParamTypeVar); | ||
|
|
||
| impl HasDefinition for ast::ExceptHandlerExceptHandler { | ||
| #[inline] | ||
| fn definition<'db>(&self, model: &SemanticModel<'db>) -> Option<Definition<'db>> { | ||
| self.name.as_ref()?; | ||
| let index = semantic_index(model.db, model.file); | ||
| Some(index.expect_single_definition(self)) | ||
| } | ||
| } | ||
|
|
||
| impl HasType for ast::ExceptHandlerExceptHandler { | ||
| #[inline] | ||
| fn inferred_type<'db>(&self, model: &SemanticModel<'db>) -> Option<Type<'db>> { | ||
| let binding = HasDefinition::definition(self, model)?; | ||
| Some(binding_type(model.db, binding)) | ||
| } | ||
| } | ||
|
|
||
| impl HasType for ast::Alias { | ||
| fn inferred_type<'db>(&self, model: &SemanticModel<'db>) -> Option<Type<'db>> { | ||
| if &self.name == "*" { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like all these early returns assume that
function.definitionis alwaysSome(maybe that's fine; it just feels a bit fragile). If it's none, we'd probably want to prefer using the fallback scope instead of returningNone.