Skip to content

Commit 3a542a8

Browse files
authored
[ty] Handle cycles when finding implicit attributes (#19833)
The [minimal reproduction](https://gist.github.com/dcreager/fc53c59b30d7ce71d478dcb2c1c56444) of astral-sh/ty#948 is an example of a class with implicit attributes whose types end up depending on themselves. Our existing cycle detection for `infer_expression_types` is usually enough to handle this situation correctly, but when there are very many of these implicit attributes, we get a combinatorial explosion of running time and memory usage. Adding a separate cycle handler for `ClassLiteral::implicit_attribute` lets us catch and recover from this situation earlier. Closes astral-sh/ty#948
1 parent 4be6fc0 commit 3a542a8

File tree

1 file changed

+41
-2
lines changed
  • crates/ty_python_semantic/src/types

1 file changed

+41
-2
lines changed

crates/ty_python_semantic/src/types/class.rs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,26 @@ fn inheritance_cycle_initial<'db>(
8989
None
9090
}
9191

92+
fn implicit_attribute_recover<'db>(
93+
_db: &'db dyn Db,
94+
_value: &PlaceAndQualifiers<'db>,
95+
_count: u32,
96+
_class_body_scope: ScopeId<'db>,
97+
_name: String,
98+
_target_method_decorator: MethodDecorator,
99+
) -> salsa::CycleRecoveryAction<PlaceAndQualifiers<'db>> {
100+
salsa::CycleRecoveryAction::Iterate
101+
}
102+
103+
fn implicit_attribute_initial<'db>(
104+
_db: &'db dyn Db,
105+
_class_body_scope: ScopeId<'db>,
106+
_name: String,
107+
_target_method_decorator: MethodDecorator,
108+
) -> PlaceAndQualifiers<'db> {
109+
Place::Unbound.into()
110+
}
111+
92112
fn try_mro_cycle_recover<'db>(
93113
_db: &'db dyn Db,
94114
_value: &Result<Mro<'db>, MroError<'db>>,
@@ -2359,6 +2379,25 @@ impl<'db> ClassLiteral<'db> {
23592379
class_body_scope: ScopeId<'db>,
23602380
name: &str,
23612381
target_method_decorator: MethodDecorator,
2382+
) -> PlaceAndQualifiers<'db> {
2383+
Self::implicit_attribute_inner(
2384+
db,
2385+
class_body_scope,
2386+
name.to_string(),
2387+
target_method_decorator,
2388+
)
2389+
}
2390+
2391+
#[salsa::tracked(
2392+
cycle_fn=implicit_attribute_recover,
2393+
cycle_initial=implicit_attribute_initial,
2394+
heap_size=ruff_memory_usage::heap_size,
2395+
)]
2396+
fn implicit_attribute_inner(
2397+
db: &'db dyn Db,
2398+
class_body_scope: ScopeId<'db>,
2399+
name: String,
2400+
target_method_decorator: MethodDecorator,
23622401
) -> PlaceAndQualifiers<'db> {
23632402
// If we do not see any declarations of an attribute, neither in the class body nor in
23642403
// any method, we build a union of `Unknown` with the inferred types of all bindings of
@@ -2392,7 +2431,7 @@ impl<'db> ClassLiteral<'db> {
23922431

23932432
// First check declarations
23942433
for (attribute_declarations, method_scope_id) in
2395-
attribute_declarations(db, class_body_scope, name)
2434+
attribute_declarations(db, class_body_scope, &name)
23962435
{
23972436
let method_scope = method_scope_id.to_scope_id(db, file);
23982437
if !is_valid_scope(method_scope) {
@@ -2450,7 +2489,7 @@ impl<'db> ClassLiteral<'db> {
24502489
}
24512490

24522491
for (attribute_assignments, method_scope_id) in
2453-
attribute_assignments(db, class_body_scope, name)
2492+
attribute_assignments(db, class_body_scope, &name)
24542493
{
24552494
let method_scope = method_scope_id.to_scope_id(db, file);
24562495
if !is_valid_scope(method_scope) {

0 commit comments

Comments
 (0)