mirror of https://github.com/astral-sh/ruff
[ty] Cache `KnownClass::to_class_literal` (#22000)
This commit is contained in:
parent
01c0a3e960
commit
b2b0ad38ea
|
|
@ -5075,35 +5075,52 @@ impl KnownClass {
|
||||||
///
|
///
|
||||||
/// If the class cannot be found in typeshed, a debug-level log message will be emitted stating this.
|
/// If the class cannot be found in typeshed, a debug-level log message will be emitted stating this.
|
||||||
pub(crate) fn try_to_class_literal(self, db: &dyn Db) -> Option<ClassLiteral<'_>> {
|
pub(crate) fn try_to_class_literal(self, db: &dyn Db) -> Option<ClassLiteral<'_>> {
|
||||||
// a cache of the `KnownClass`es that we have already failed to lookup in typeshed
|
#[salsa::interned(heap_size=ruff_memory_usage::heap_size)]
|
||||||
// (and therefore that we've already logged a warning for)
|
struct KnownClassArgument {
|
||||||
static MESSAGES: LazyLock<Mutex<FxHashSet<KnownClass>>> = LazyLock::new(Mutex::default);
|
class: KnownClass,
|
||||||
|
}
|
||||||
|
|
||||||
self.try_to_class_literal_without_logging(db)
|
fn known_class_to_class_literal_initial<'db>(
|
||||||
.or_else(|lookup_error| {
|
_db: &'db dyn Db,
|
||||||
if MESSAGES.lock().unwrap().insert(self) {
|
_id: salsa::Id,
|
||||||
|
_class: KnownClassArgument<'db>,
|
||||||
|
) -> Option<ClassLiteral<'db>> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
#[salsa::tracked(cycle_initial=known_class_to_class_literal_initial, heap_size=ruff_memory_usage::heap_size)]
|
||||||
|
fn known_class_to_class_literal<'db>(
|
||||||
|
db: &'db dyn Db,
|
||||||
|
class: KnownClassArgument<'db>,
|
||||||
|
) -> Option<ClassLiteral<'db>> {
|
||||||
|
let class = class.class(db);
|
||||||
|
class
|
||||||
|
.try_to_class_literal_without_logging(db)
|
||||||
|
.or_else(|lookup_error| {
|
||||||
if matches!(
|
if matches!(
|
||||||
lookup_error,
|
lookup_error,
|
||||||
KnownClassLookupError::ClassPossiblyUnbound { .. }
|
KnownClassLookupError::ClassPossiblyUnbound { .. }
|
||||||
) {
|
) {
|
||||||
tracing::info!("{}", lookup_error.display(db, self));
|
tracing::info!("{}", lookup_error.display(db, class));
|
||||||
} else {
|
} else {
|
||||||
tracing::info!(
|
tracing::info!(
|
||||||
"{}. Falling back to `Unknown` for the symbol instead.",
|
"{}. Falling back to `Unknown` for the symbol instead.",
|
||||||
lookup_error.display(db, self)
|
lookup_error.display(db, class)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
match lookup_error {
|
match lookup_error {
|
||||||
KnownClassLookupError::ClassPossiblyUnbound { class_literal, .. } => {
|
KnownClassLookupError::ClassPossiblyUnbound { class_literal, .. } => {
|
||||||
Ok(class_literal)
|
Ok(class_literal)
|
||||||
|
}
|
||||||
|
KnownClassLookupError::ClassNotFound { .. }
|
||||||
|
| KnownClassLookupError::SymbolNotAClass { .. } => Err(()),
|
||||||
}
|
}
|
||||||
KnownClassLookupError::ClassNotFound { .. }
|
})
|
||||||
| KnownClassLookupError::SymbolNotAClass { .. } => Err(()),
|
.ok()
|
||||||
}
|
}
|
||||||
})
|
|
||||||
.ok()
|
known_class_to_class_literal(db, KnownClassArgument::new(db, self))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Lookup a [`KnownClass`] in typeshed and return a [`Type`] representing that class-literal.
|
/// Lookup a [`KnownClass`] in typeshed and return a [`Type`] representing that class-literal.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue