Skip to content

Commit eb32ba3

Browse files
sharkdpsecond-ed
authored andcommitted
[ty] Fix 'too many cycle iterations' for unions of literals (astral-sh#20137)
## Summary Decrease the maximum number of literals in a union before we collapse to the supertype. The better fix for this will be astral-sh/ty#957, but it is very tempting to solve this for now by simply decreasing the limit by one, to get below the salsa limit of 200. closes astral-sh/ty#660 ## Test Plan Added a regression test that would previously lead to a "too many cycle iterations" panic.
1 parent b0856d6 commit eb32ba3

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

crates/ty_python_semantic/resources/mdtest/attributes.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2309,6 +2309,20 @@ reveal_type(Toggle().x) # revealed: Literal[True]
23092309
reveal_type(Toggle().y) # revealed: Unknown | Literal[True]
23102310
```
23112311

2312+
Make sure that the growing union of literals `Literal[0, 1, 2, ...]` collapses to `int` during
2313+
fixpoint iteration. This is a regression test for <https://github.com/astral-sh/ty/issues/660>.
2314+
2315+
```py
2316+
class Counter:
2317+
def __init__(self: "Counter"):
2318+
self.count = 0
2319+
2320+
def increment(self: "Counter"):
2321+
self.count = self.count + 1
2322+
2323+
reveal_type(Counter().count) # revealed: Unknown | int
2324+
```
2325+
23122326
### Builtin types attributes
23132327

23142328
This test can probably be removed eventually, but we currently include it because we do not yet

crates/ty_python_semantic/src/types/builder.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,10 @@ enum ReduceResult<'db> {
202202

203203
// TODO increase this once we extend `UnionElement` throughout all union/intersection
204204
// representations, so that we can make large unions of literals fast in all operations.
205-
const MAX_UNION_LITERALS: usize = 200;
205+
//
206+
// For now (until we solve https://github.com/astral-sh/ty/issues/957), keep this number
207+
// below 200, which is the salsa fixpoint iteration limit.
208+
const MAX_UNION_LITERALS: usize = 199;
206209

207210
pub(crate) struct UnionBuilder<'db> {
208211
elements: Vec<UnionElement<'db>>,

0 commit comments

Comments
 (0)