Skip to content

Commit 9f904d1

Browse files
committed
[ty] fix a bug with global symbol lookup from eager scopes
1 parent 6cc3393 commit 9f904d1

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed
Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
# Documentation of two fuzzer panics involving comprehensions
1+
# Regression test for https://github.com/astral-sh/ruff/pull/20962
2+
# error message:
3+
# `place_by_id: execute: too many cycle iterations`
24

3-
Type inference for comprehensions was added in <https://github.com/astral-sh/ruff/pull/20962>. It
4-
added two new fuzzer panics that are documented here for regression testing.
5-
6-
## Too many cycle iterations in `place_by_id`
7-
8-
<!-- expect-panic: too many cycle iterations -->
9-
10-
```py
115
name_5(name_3)
126
[0 for unique_name_0 in unique_name_1 for unique_name_2 in name_3]
137

@@ -34,4 +28,3 @@ def name_3():
3428
@name_3
3529
async def name_5():
3630
pass
37-
```

crates/ty_python_semantic/resources/mdtest/comprehensions/basic.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ Iterating over an unbound iterable yields `Unknown`:
5858
# error: [not-iterable] "Object of type `int` is not iterable"
5959
# revealed: tuple[int, Unknown]
6060
[reveal_type((x, z)) for x in range(3) for z in x]
61+
62+
# error: [unresolved-reference] "Name `foo` used when not defined"
63+
foo
64+
foo = [
65+
# revealed: tuple[int, Unknown]
66+
reveal_type((x, z))
67+
for x in range(3)
68+
# error: [unresolved-reference] "Name `foo` used when not defined"
69+
for z in [foo]
70+
]
71+
72+
baz = [
73+
# revealed: tuple[int, Unknown]
74+
reveal_type((x, z))
75+
for x in range(3)
76+
# error: [unresolved-reference] "Name `baz` used when not defined"
77+
for z in [baz]
78+
]
6179
```
6280

6381
## Starred expressions

crates/ty_python_semantic/src/types/infer/builder.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8022,6 +8022,9 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
80228022
FileScopeId::global(),
80238023
ConstraintKey::NarrowingConstraint(constraint),
80248024
));
8025+
// Reaching here means that no bindings are found in any scope.
8026+
// Since `explicit_global_symbol` may return a cycle initial value, we return `Place::Undefined` here.
8027+
return Place::Undefined.into();
80258028
}
80268029
EnclosingSnapshotResult::FoundBindings(bindings) => {
80278030
let place = place_from_bindings(db, bindings).map_type(|ty| {

0 commit comments

Comments
 (0)