From 3c75a7705dfb10ffea76948337f30fa698539891 Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Thu, 18 Sep 2025 15:23:02 -0700 Subject: [PATCH] salsa cache is_subtype_of, with cycle handling --- .../resources/mdtest/pep695_type_aliases.md | 2 +- crates/ty_python_semantic/src/types.rs | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/crates/ty_python_semantic/resources/mdtest/pep695_type_aliases.md b/crates/ty_python_semantic/resources/mdtest/pep695_type_aliases.md index da915ac39b..ed4716494d 100644 --- a/crates/ty_python_semantic/resources/mdtest/pep695_type_aliases.md +++ b/crates/ty_python_semantic/resources/mdtest/pep695_type_aliases.md @@ -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] ``` diff --git a/crates/ty_python_semantic/src/types.rs b/crates/ty_python_semantic/src/types.rs index c2c11c5820..f8de6a507e 100644 --- a/crates/ty_python_semantic/src/types.rs +++ b/crates/ty_python_semantic/src/types.rs @@ -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 { + 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>,