mirror of https://github.com/astral-sh/ruff
[ty] Handle cycles when finding implicit attributes (#19833)
The [minimal reproduction](https://gist.github.com/dcreager/fc53c59b30d7ce71d478dcb2c1c56444) of https://github.com/astral-sh/ty/issues/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 https://github.com/astral-sh/ty/issues/948
This commit is contained in:
parent
4be6fc0979
commit
3a542a80f6
|
|
@ -89,6 +89,26 @@ fn inheritance_cycle_initial<'db>(
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn implicit_attribute_recover<'db>(
|
||||||
|
_db: &'db dyn Db,
|
||||||
|
_value: &PlaceAndQualifiers<'db>,
|
||||||
|
_count: u32,
|
||||||
|
_class_body_scope: ScopeId<'db>,
|
||||||
|
_name: String,
|
||||||
|
_target_method_decorator: MethodDecorator,
|
||||||
|
) -> salsa::CycleRecoveryAction<PlaceAndQualifiers<'db>> {
|
||||||
|
salsa::CycleRecoveryAction::Iterate
|
||||||
|
}
|
||||||
|
|
||||||
|
fn implicit_attribute_initial<'db>(
|
||||||
|
_db: &'db dyn Db,
|
||||||
|
_class_body_scope: ScopeId<'db>,
|
||||||
|
_name: String,
|
||||||
|
_target_method_decorator: MethodDecorator,
|
||||||
|
) -> PlaceAndQualifiers<'db> {
|
||||||
|
Place::Unbound.into()
|
||||||
|
}
|
||||||
|
|
||||||
fn try_mro_cycle_recover<'db>(
|
fn try_mro_cycle_recover<'db>(
|
||||||
_db: &'db dyn Db,
|
_db: &'db dyn Db,
|
||||||
_value: &Result<Mro<'db>, MroError<'db>>,
|
_value: &Result<Mro<'db>, MroError<'db>>,
|
||||||
|
|
@ -2359,6 +2379,25 @@ impl<'db> ClassLiteral<'db> {
|
||||||
class_body_scope: ScopeId<'db>,
|
class_body_scope: ScopeId<'db>,
|
||||||
name: &str,
|
name: &str,
|
||||||
target_method_decorator: MethodDecorator,
|
target_method_decorator: MethodDecorator,
|
||||||
|
) -> PlaceAndQualifiers<'db> {
|
||||||
|
Self::implicit_attribute_inner(
|
||||||
|
db,
|
||||||
|
class_body_scope,
|
||||||
|
name.to_string(),
|
||||||
|
target_method_decorator,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[salsa::tracked(
|
||||||
|
cycle_fn=implicit_attribute_recover,
|
||||||
|
cycle_initial=implicit_attribute_initial,
|
||||||
|
heap_size=ruff_memory_usage::heap_size,
|
||||||
|
)]
|
||||||
|
fn implicit_attribute_inner(
|
||||||
|
db: &'db dyn Db,
|
||||||
|
class_body_scope: ScopeId<'db>,
|
||||||
|
name: String,
|
||||||
|
target_method_decorator: MethodDecorator,
|
||||||
) -> PlaceAndQualifiers<'db> {
|
) -> PlaceAndQualifiers<'db> {
|
||||||
// If we do not see any declarations of an attribute, neither in the class body nor in
|
// If we do not see any declarations of an attribute, neither in the class body nor in
|
||||||
// any method, we build a union of `Unknown` with the inferred types of all bindings of
|
// any method, we build a union of `Unknown` with the inferred types of all bindings of
|
||||||
|
|
@ -2392,7 +2431,7 @@ impl<'db> ClassLiteral<'db> {
|
||||||
|
|
||||||
// First check declarations
|
// First check declarations
|
||||||
for (attribute_declarations, method_scope_id) in
|
for (attribute_declarations, method_scope_id) in
|
||||||
attribute_declarations(db, class_body_scope, name)
|
attribute_declarations(db, class_body_scope, &name)
|
||||||
{
|
{
|
||||||
let method_scope = method_scope_id.to_scope_id(db, file);
|
let method_scope = method_scope_id.to_scope_id(db, file);
|
||||||
if !is_valid_scope(method_scope) {
|
if !is_valid_scope(method_scope) {
|
||||||
|
|
@ -2450,7 +2489,7 @@ impl<'db> ClassLiteral<'db> {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (attribute_assignments, method_scope_id) in
|
for (attribute_assignments, method_scope_id) in
|
||||||
attribute_assignments(db, class_body_scope, name)
|
attribute_assignments(db, class_body_scope, &name)
|
||||||
{
|
{
|
||||||
let method_scope = method_scope_id.to_scope_id(db, file);
|
let method_scope = method_scope_id.to_scope_id(db, file);
|
||||||
if !is_valid_scope(method_scope) {
|
if !is_valid_scope(method_scope) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue