-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[red-knot] Avoid panicking when hitting failures looking up AST information #13701
base: main
Are you sure you want to change the base?
Changes from all commits
933c4c0
eb8eae2
e3af8dc
8ca348d
c0292e0
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 |
---|---|---|
|
@@ -9,6 +9,7 @@ use ruff_python_ast as ast; | |
use ruff_python_ast::name::Name; | ||
use ruff_python_ast::visitor::{walk_expr, walk_pattern, walk_stmt, Visitor}; | ||
use ruff_python_ast::AnyParameterRef; | ||
use ruff_python_ast::Expr; | ||
|
||
use crate::ast_node_ref::AstNodeRef; | ||
use crate::semantic_index::ast_ids::node_key::ExpressionNodeKey; | ||
|
@@ -212,7 +213,9 @@ impl<'db> SemanticIndexBuilder<'db> { | |
let existing_definition = self | ||
.definitions_by_node | ||
.insert(definition_node.key(), definition); | ||
debug_assert_eq!(existing_definition, None); | ||
if existing_definition.is_some() { | ||
tracing::warn!("Existing definition was unexpectedly evicted"); | ||
} | ||
|
||
if category.is_binding() { | ||
self.mark_symbol_bound(symbol); | ||
|
@@ -574,13 +577,21 @@ where | |
} | ||
ast::Stmt::AnnAssign(node) => { | ||
debug_assert!(self.current_assignment.is_none()); | ||
self.visit_expr(&node.annotation); | ||
if let Some(value) = &node.value { | ||
self.visit_expr(value); | ||
let valid_target = matches!( | ||
*node.target, | ||
Expr::Attribute(_) | Expr::Subscript(_) | Expr::Name(_) | ||
); | ||
if valid_target { | ||
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. if we visit with an invalid target we get downstream errors on re-defining things. There's likely a way to visit here with an invalid target that doesn't generate issues |
||
self.visit_expr(&node.annotation); | ||
if let Some(value) = &node.value { | ||
self.visit_expr(value); | ||
} | ||
self.current_assignment = Some(node.into()); | ||
self.visit_expr(&node.target); | ||
self.current_assignment = None; | ||
} else { | ||
tracing::warn!("Annotated assignment with invalid target received"); | ||
} | ||
self.current_assignment = Some(node.into()); | ||
self.visit_expr(&node.target); | ||
self.current_assignment = None; | ||
} | ||
ast::Stmt::AugAssign( | ||
aug_assign @ ast::StmtAugAssign { | ||
|
@@ -879,12 +890,17 @@ where | |
walk_expr(self, expr); | ||
} | ||
ast::Expr::Named(node) => { | ||
debug_assert!(self.current_assignment.is_none()); | ||
// TODO walrus in comprehensions is implicitly nonlocal | ||
self.visit_expr(&node.value); | ||
self.current_assignment = Some(node.into()); | ||
self.visit_expr(&node.target); | ||
self.current_assignment = None; | ||
if self.current_assignment.is_some() { | ||
// This can happen if we have something like x = y := 2 | ||
// which is invalid syntax but still is provided in the AST | ||
tracing::warn!("Current assignment is unexpectedly set"); | ||
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 limit the use of |
||
} else { | ||
// TODO walrus in comprehensions is implicitly nonlocal | ||
self.visit_expr(&node.value); | ||
self.current_assignment = Some(node.into()); | ||
self.visit_expr(&node.target); | ||
self.current_assignment = None; | ||
} | ||
} | ||
ast::Expr::Lambda(lambda) => { | ||
if let Some(parameters) = &lambda.parameters { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,8 +58,13 @@ impl HasTy for ast::ExpressionRef<'_> { | |
let file_scope = index.expression_scope_id(*self); | ||
let scope = file_scope.to_scope_id(model.db, model.file); | ||
|
||
let expression_id = self.scoped_ast_id(model.db, scope); | ||
infer_scope_types(model.db, scope).expression_ty(expression_id) | ||
if let Some(expression_id) = self.scoped_ast_id(model.db, scope) { | ||
let lookup = infer_scope_types(model.db, scope).try_expression_ty(expression_id); | ||
lookup.unwrap_or(Type::Unknown) | ||
} else { | ||
tracing::warn!("Couldn't find expression ID"); | ||
Type::Unknown | ||
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 feel like it would make a lot of sense for there to be something like That way, if |
||
} | ||
} | ||
} | ||
|
||
|
@@ -153,8 +158,10 @@ macro_rules! impl_binding_has_ty { | |
#[inline] | ||
fn ty<'db>(&self, model: &SemanticModel<'db>) -> Type<'db> { | ||
let index = semantic_index(model.db, model.file); | ||
let binding = index.definition(self); | ||
binding_ty(model.db, binding) | ||
match index.definition(self) { | ||
Some(binding) => binding_ty(model.db, binding), | ||
None => Type::Unknown, | ||
} | ||
} | ||
} | ||
}; | ||
|
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.
There are a bunch of warnings I added just to try and get things working.
In total there are 228 warnings (of which this one is only hit 5 times), so it's pretty surmountable all things considered