Skip to content

Commit

Permalink
[red-knot] SemanticIndexBuilder visits value before target in named…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
dylwil3 authored Aug 22, 2024
1 parent 02c4373 commit 2edd32a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions crates/red_knot_python_semantic/src/semantic_index/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = 0
(x := x + 1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
x = 0
if x := x + 1:
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
match x:
case [1, 0] if x := x[:0]:
y = 1

0 comments on commit 2edd32a

Please sign in to comment.