[red-knot] SemanticIndexBuilder visits value before target in named expressions (#13053)

The `SemanticIndexBuilder` was causing a cycle in a salsa query by
attempting to resolve the target before the value in a named expression
(e.g. `x := x+1`). This PR swaps the order, avoiding a panic.

Closes #13012.
This commit is contained in:
Dylan 2024-08-22 09:59:13 -05:00 committed by GitHub
parent 02c4373a49
commit 2edd32aa31
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 27 additions and 2 deletions

View file

@ -658,11 +658,11 @@ where
}
ast::Expr::Named(node) => {
debug_assert!(self.current_assignment.is_none());
self.current_assignment = Some(node.into());
// 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;
self.visit_expr(&node.value);
}
ast::Expr::Lambda(lambda) => {
if let Some(parameters) = &lambda.parameters {

View file

@ -2290,6 +2290,23 @@ mod tests {
Ok(())
}
#[test]
fn walrus_self_plus_one() -> anyhow::Result<()> {
let mut db = setup_db();
db.write_dedented(
"src/a.py",
"
x = 0
(x := x + 1)
",
)?;
assert_public_ty(&db, "src/a.py", "x", "Literal[1]");
Ok(())
}
#[test]
fn ifexpr() -> anyhow::Result<()> {
let mut db = setup_db();