salsa cache is_subtype_of, with cycle handling

This commit is contained in:
Carl Meyer 2025-09-18 15:23:02 -07:00 committed by Ibraheem Ahmed
parent ab05f3b3ee
commit 3c75a7705d
2 changed files with 21 additions and 1 deletions

View file

@ -369,5 +369,5 @@ type Y = X | str | dict[str, Y]
def _(y: Y):
if isinstance(y, dict):
reveal_type(y)
reveal_type(y) # revealed: dict[str, X] | dict[str, Y]
```

View file

@ -1423,6 +1423,7 @@ impl<'db> Type<'db> {
/// P`, but not `B <: P`. Losing transitivity of subtyping is not tenable (it makes union and
/// intersection simplification dependent on the order in which elements are added), so we do
/// not use this more general definition of subtyping.
#[salsa::tracked(cycle_fn=is_subtype_of_cycle_recover, cycle_initial=is_subtype_of_cycle_initial)]
pub(crate) fn is_subtype_of(self, db: &'db dyn Db, target: Type<'db>) -> bool {
self.when_subtype_of(db, target).is_always_satisfied()
}
@ -6690,6 +6691,25 @@ impl<'db> VarianceInferable<'db> for Type<'db> {
}
}
#[allow(clippy::trivially_copy_pass_by_ref)]
fn is_subtype_of_cycle_recover<'db>(
_db: &'db dyn Db,
_value: &bool,
_count: u32,
_subtype: Type<'db>,
_supertype: Type<'db>,
) -> salsa::CycleRecoveryAction<bool> {
salsa::CycleRecoveryAction::Iterate
}
fn is_subtype_of_cycle_initial<'db>(
_db: &'db dyn Db,
_subtype: Type<'db>,
_supertype: Type<'db>,
) -> bool {
true
}
fn apply_specialization_cycle_recover<'db>(
_db: &'db dyn Db,
_value: &Type<'db>,