mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 05:14:52 +00:00
[ty] Handle cycles when finding implicit attributes (#19833)
Some checks are pending
CI / benchmarks-walltime (push) Blocked by required conditions
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / benchmarks-instrumented (push) Blocked by required conditions
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
Some checks are pending
CI / benchmarks-walltime (push) Blocked by required conditions
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / benchmarks-instrumented (push) Blocked by required conditions
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
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
1 changed files with 41 additions and 2 deletions
|
@ -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…
Add table
Add a link
Reference in a new issue