mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 13:25:17 +00:00
[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:
parent
02c4373a49
commit
2edd32aa31
5 changed files with 27 additions and 2 deletions
|
@ -658,11 +658,11 @@ where
|
||||||
}
|
}
|
||||||
ast::Expr::Named(node) => {
|
ast::Expr::Named(node) => {
|
||||||
debug_assert!(self.current_assignment.is_none());
|
debug_assert!(self.current_assignment.is_none());
|
||||||
self.current_assignment = Some(node.into());
|
|
||||||
// TODO walrus in comprehensions is implicitly nonlocal
|
// TODO walrus in comprehensions is implicitly nonlocal
|
||||||
|
self.visit_expr(&node.value);
|
||||||
|
self.current_assignment = Some(node.into());
|
||||||
self.visit_expr(&node.target);
|
self.visit_expr(&node.target);
|
||||||
self.current_assignment = None;
|
self.current_assignment = None;
|
||||||
self.visit_expr(&node.value);
|
|
||||||
}
|
}
|
||||||
ast::Expr::Lambda(lambda) => {
|
ast::Expr::Lambda(lambda) => {
|
||||||
if let Some(parameters) = &lambda.parameters {
|
if let Some(parameters) = &lambda.parameters {
|
||||||
|
|
|
@ -2290,6 +2290,23 @@ mod tests {
|
||||||
Ok(())
|
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]
|
#[test]
|
||||||
fn ifexpr() -> anyhow::Result<()> {
|
fn ifexpr() -> anyhow::Result<()> {
|
||||||
let mut db = setup_db();
|
let mut db = setup_db();
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
x = 0
|
||||||
|
(x := x + 1)
|
|
@ -0,0 +1,3 @@
|
||||||
|
x = 0
|
||||||
|
if x := x + 1:
|
||||||
|
pass
|
|
@ -0,0 +1,3 @@
|
||||||
|
match x:
|
||||||
|
case [1, 0] if x := x[:0]:
|
||||||
|
y = 1
|
Loading…
Add table
Add a link
Reference in a new issue