[ty] fix more ecosystem/fuzzer panics with fixpoint (#17758)

## Summary

Add cycle handling for `try_metaclass` and `pep695_generic_context`
queries, as well as adjusting the cycle handling for `try_mro` to ensure
that it short-circuits on cycles and won't grow MROs indefinitely.

This reduces the number of failing fuzzer seeds from 68 to 17. The
latter count includes fuzzer seeds 120, 160, and 335, all of which
previously panicked but now either hang or are very slow; I've
temporarily skipped those seeds in the fuzzer until I can dig into that
slowness further.

This also allows us to move some more ecosystem projects from `bad.txt`
to `good.txt`, which I've done in
https://github.com/astral-sh/ruff/pull/17903

## Test Plan

Added mdtests.
This commit is contained in:
Carl Meyer 2025-05-08 20:36:20 -07:00 committed by GitHub
parent f78367979e
commit 3d2485eb1b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 168 additions and 46 deletions

View file

@ -60,28 +60,6 @@ fn explicit_bases_cycle_initial<'db>(
Box::default()
}
fn try_mro_cycle_recover<'db>(
_db: &'db dyn Db,
_value: &Result<Mro<'db>, MroError<'db>>,
_count: u32,
_self: ClassLiteral<'db>,
_specialization: Option<Specialization<'db>>,
) -> salsa::CycleRecoveryAction<Result<Mro<'db>, MroError<'db>>> {
salsa::CycleRecoveryAction::Iterate
}
#[expect(clippy::unnecessary_wraps)]
fn try_mro_cycle_initial<'db>(
db: &'db dyn Db,
self_: ClassLiteral<'db>,
specialization: Option<Specialization<'db>>,
) -> Result<Mro<'db>, MroError<'db>> {
Ok(Mro::from_error(
db,
self_.apply_optional_specialization(db, specialization),
))
}
#[expect(clippy::ref_option, clippy::trivially_copy_pass_by_ref)]
fn inheritance_cycle_recover<'db>(
_db: &'db dyn Db,
@ -99,6 +77,48 @@ fn inheritance_cycle_initial<'db>(
None
}
fn try_mro_cycle_recover<'db>(
_db: &'db dyn Db,
_value: &Result<Mro<'db>, MroError<'db>>,
_count: u32,
_self: ClassLiteral<'db>,
_specialization: Option<Specialization<'db>>,
) -> salsa::CycleRecoveryAction<Result<Mro<'db>, MroError<'db>>> {
salsa::CycleRecoveryAction::Iterate
}
fn try_mro_cycle_initial<'db>(
db: &'db dyn Db,
self_: ClassLiteral<'db>,
specialization: Option<Specialization<'db>>,
) -> Result<Mro<'db>, MroError<'db>> {
Err(MroError::cycle(
db,
self_.apply_optional_specialization(db, specialization),
))
}
fn try_metaclass_cycle_recover<'db>(
_db: &'db dyn Db,
_value: &Result<(Type<'db>, Option<DataclassTransformerParams>), MetaclassError<'db>>,
_count: u32,
_self: ClassLiteral<'db>,
) -> salsa::CycleRecoveryAction<
Result<(Type<'db>, Option<DataclassTransformerParams>), MetaclassError<'db>>,
> {
salsa::CycleRecoveryAction::Iterate
}
#[allow(clippy::unnecessary_wraps)]
fn try_metaclass_cycle_initial<'db>(
_db: &'db dyn Db,
_self_: ClassLiteral<'db>,
) -> Result<(Type<'db>, Option<DataclassTransformerParams>), MetaclassError<'db>> {
Err(MetaclassError {
kind: MetaclassErrorKind::Cycle,
})
}
/// A category of classes with code generation capabilities (with synthesized methods).
#[derive(Clone, Copy, Debug, PartialEq)]
enum CodeGeneratorKind {
@ -462,6 +482,23 @@ pub struct ClassLiteral<'db> {
pub(crate) dataclass_transformer_params: Option<DataclassTransformerParams>,
}
#[expect(clippy::trivially_copy_pass_by_ref, clippy::ref_option)]
fn pep695_generic_context_cycle_recover<'db>(
_db: &'db dyn Db,
_value: &Option<GenericContext<'db>>,
_count: u32,
_self: ClassLiteral<'db>,
) -> salsa::CycleRecoveryAction<Option<GenericContext<'db>>> {
salsa::CycleRecoveryAction::Iterate
}
fn pep695_generic_context_cycle_initial<'db>(
_db: &'db dyn Db,
_self: ClassLiteral<'db>,
) -> Option<GenericContext<'db>> {
None
}
#[salsa::tracked]
impl<'db> ClassLiteral<'db> {
/// Return `true` if this class represents `known_class`
@ -487,7 +524,7 @@ impl<'db> ClassLiteral<'db> {
.or_else(|| self.inherited_legacy_generic_context(db))
}
#[salsa::tracked]
#[salsa::tracked(cycle_fn=pep695_generic_context_cycle_recover, cycle_initial=pep695_generic_context_cycle_initial)]
pub(crate) fn pep695_generic_context(self, db: &'db dyn Db) -> Option<GenericContext<'db>> {
let scope = self.body_scope(db);
let class_def_node = scope.node(db).expect_class();
@ -786,7 +823,10 @@ impl<'db> ClassLiteral<'db> {
}
/// Return the metaclass of this class, or an error if the metaclass cannot be inferred.
#[salsa::tracked]
#[salsa::tracked(
cycle_fn=try_metaclass_cycle_recover,
cycle_initial=try_metaclass_cycle_initial,
)]
pub(super) fn try_metaclass(
self,
db: &'db dyn Db,
@ -798,8 +838,15 @@ impl<'db> ClassLiteral<'db> {
if base_classes.peek().is_some() && self.inheritance_cycle(db).is_some() {
// We emit diagnostics for cyclic class definitions elsewhere.
// Avoid attempting to infer the metaclass if the class is cyclically defined:
// it would be easy to enter an infinite loop.
// Avoid attempting to infer the metaclass if the class is cyclically defined.
return Ok((SubclassOfType::subclass_of_unknown(), None));
}
if self
.try_mro(db, None)
.as_ref()
.is_err_and(MroError::is_cycle)
{
return Ok((SubclassOfType::subclass_of_unknown(), None));
}
@ -2737,6 +2784,8 @@ pub(super) enum MetaclassErrorKind<'db> {
NotCallable(Type<'db>),
/// The metaclass is of a union type whose some members are not callable
PartlyNotCallable(Type<'db>),
/// A cycle was encountered attempting to determine the metaclass
Cycle,
}
#[cfg(test)]

View file

@ -1,6 +1,6 @@
use crate::types::generics::{GenericContext, Specialization, TypeMapping};
use crate::types::{
todo_type, ClassType, DynamicType, KnownClass, KnownInstanceType, MroIterator, Type,
todo_type, ClassType, DynamicType, KnownClass, KnownInstanceType, MroError, MroIterator, Type,
};
use crate::Db;
@ -233,6 +233,19 @@ impl<'db> ClassBase<'db> {
}
}
pub(super) fn has_cyclic_mro(self, db: &'db dyn Db) -> bool {
match self {
ClassBase::Class(class) => {
let (class_literal, specialization) = class.class_literal(db);
class_literal
.try_mro(db, specialization)
.as_ref()
.is_err_and(MroError::is_cycle)
}
ClassBase::Dynamic(_) | ClassBase::Generic(_) | ClassBase::Protocol => false,
}
}
/// Iterate over the MRO of this base
pub(super) fn mro(
self,

View file

@ -44,7 +44,7 @@ impl<'db> GenericContext<'db> {
let Type::KnownInstance(KnownInstanceType::TypeVar(typevar)) =
declaration_type(db, definition).inner_type()
else {
panic!("typevar should be inferred as a TypeVarInstance");
return None;
};
Some(typevar)
}

View file

@ -816,8 +816,8 @@ impl<'db> TypeInferenceBuilder<'db> {
));
}
}
// Attempting to determine the MRO of a class or if the class has a metaclass conflict
// is impossible if the class is cyclically defined; there's nothing more to do here.
// If a class is cyclically defined, that's a sufficient error to report; the
// following checks (which are all inheritance-based) aren't even relevant.
continue;
}
@ -921,6 +921,17 @@ impl<'db> TypeInferenceBuilder<'db> {
));
}
}
MroErrorKind::InheritanceCycle => {
if let Some(builder) = self
.context
.report_lint(&CYCLIC_CLASS_DEFINITION, class_node)
{
builder.into_diagnostic(format_args!(
"Cyclic definition of `{}` (class cannot inherit from itself)",
class.name(self.db())
));
}
}
}
}
Ok(_) => check_class_slots(&self.context, class, class_node),
@ -929,6 +940,17 @@ impl<'db> TypeInferenceBuilder<'db> {
// (4) Check that the class's metaclass can be determined without error.
if let Err(metaclass_error) = class.try_metaclass(self.db()) {
match metaclass_error.reason() {
MetaclassErrorKind::Cycle => {
if let Some(builder) = self
.context
.report_lint(&CYCLIC_CLASS_DEFINITION, class_node)
{
builder.into_diagnostic(format_args!(
"Cyclic definition of `{}`",
class.name(self.db())
));
}
}
MetaclassErrorKind::NotCallable(ty) => {
if let Some(builder) =
self.context.report_lint(&INVALID_METACLASS, class_node)

View file

@ -68,21 +68,9 @@ impl<'db> Mro<'db> {
class: ClassLiteral<'db>,
specialization: Option<Specialization<'db>>,
) -> Result<Self, MroErrorKind<'db>> {
let class_bases = class.explicit_bases(db);
if !class_bases.is_empty() && class.inheritance_cycle(db).is_some() {
// We emit errors for cyclically defined classes elsewhere.
// It's important that we don't even try to infer the MRO for a cyclically defined class,
// or we'll end up in an infinite loop.
return Ok(Mro::from_error(
db,
class.apply_optional_specialization(db, specialization),
));
}
let class_type = class.apply_optional_specialization(db, specialization);
match class_bases {
match class.explicit_bases(db) {
// `builtins.object` is the special case:
// the only class in Python that has an MRO with length <2
[] if class.is_object(db) => Ok(Self::from([
@ -116,9 +104,15 @@ impl<'db> Mro<'db> {
[single_base] => ClassBase::try_from_type(db, *single_base).map_or_else(
|| Err(MroErrorKind::InvalidBases(Box::from([(0, *single_base)]))),
|single_base| {
Ok(std::iter::once(ClassBase::Class(class_type))
if single_base.has_cyclic_mro(db) {
Err(MroErrorKind::InheritanceCycle)
} else {
Ok(std::iter::once(ClassBase::Class(
class.apply_optional_specialization(db, specialization),
))
.chain(single_base.mro(db, specialization))
.collect())
}
},
),
@ -144,6 +138,9 @@ impl<'db> Mro<'db> {
let mut seqs = vec![VecDeque::from([ClassBase::Class(class_type)])];
for base in &valid_bases {
if base.has_cyclic_mro(db) {
return Err(MroErrorKind::InheritanceCycle);
}
seqs.push(base.mro(db, specialization).collect());
}
seqs.push(
@ -331,6 +328,15 @@ pub(super) struct MroError<'db> {
}
impl<'db> MroError<'db> {
/// Construct an MRO error of kind `InheritanceCycle`.
pub(super) fn cycle(db: &'db dyn Db, class: ClassType<'db>) -> Self {
MroErrorKind::InheritanceCycle.into_mro_error(db, class)
}
pub(super) fn is_cycle(&self) -> bool {
matches!(self.kind, MroErrorKind::InheritanceCycle)
}
/// Return an [`MroErrorKind`] variant describing why we could not resolve the MRO for this class.
pub(super) fn reason(&self) -> &MroErrorKind<'db> {
&self.kind
@ -363,6 +369,9 @@ pub(super) enum MroErrorKind<'db> {
/// See [`DuplicateBaseError`] for more details.
DuplicateBases(Box<[DuplicateBaseError<'db>]>),
/// A cycle was encountered resolving the class' bases.
InheritanceCycle,
/// The MRO is otherwise unresolvable through the C3-merge algorithm.
///
/// See [`c3_merge`] for more details.