Skip to content

Commit 737afeb

Browse files
committed
rename other TypeVisitor, fix walking of PEP-695 aliases
1 parent 1aa2fe0 commit 737afeb

File tree

11 files changed

+153
-68
lines changed

11 files changed

+153
-68
lines changed

crates/ty_python_semantic/src/types.rs

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use ruff_text_size::{Ranged, TextRange};
2121
use type_ordering::union_or_intersection_elements_ordering;
2222

2323
pub(crate) use self::builder::{IntersectionBuilder, UnionBuilder};
24-
pub(crate) use self::cyclic::TypeVisitor;
24+
pub(crate) use self::cyclic::TypeTransformer;
2525
pub use self::diagnostic::TypeCheckDiagnostics;
2626
pub(crate) use self::diagnostic::register_lints;
2727
pub(crate) use self::infer::{
@@ -403,7 +403,7 @@ impl<'db> PropertyInstanceType<'db> {
403403
Self::new(db, getter, setter)
404404
}
405405

406-
fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeVisitor<'db>) -> Self {
406+
fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeTransformer<'db>) -> Self {
407407
Self::new(
408408
db,
409409
self.getter(db).map(|ty| ty.normalized_impl(db, visitor)),
@@ -977,12 +977,16 @@ impl<'db> Type<'db> {
977977
/// - Converts class-based protocols into synthesized protocols
978978
#[must_use]
979979
pub fn normalized(self, db: &'db dyn Db) -> Self {
980-
let mut visitor = TypeVisitor::default();
980+
let mut visitor = TypeTransformer::default();
981981
self.normalized_impl(db, &mut visitor)
982982
}
983983

984984
#[must_use]
985-
pub(crate) fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeVisitor<'db>) -> Self {
985+
pub(crate) fn normalized_impl(
986+
self,
987+
db: &'db dyn Db,
988+
visitor: &mut TypeTransformer<'db>,
989+
) -> Self {
986990
match self {
987991
Type::Union(union) => {
988992
visitor.visit(self, |v| Type::Union(union.normalized_impl(db, v)))
@@ -5661,7 +5665,7 @@ impl<'db> TypeMapping<'_, 'db> {
56615665
}
56625666
}
56635667

5664-
fn normalized_impl(&self, db: &'db dyn Db, visitor: &mut TypeVisitor<'db>) -> Self {
5668+
fn normalized_impl(&self, db: &'db dyn Db, visitor: &mut TypeTransformer<'db>) -> Self {
56655669
match self {
56665670
TypeMapping::Specialization(specialization) => {
56675671
TypeMapping::Specialization(specialization.normalized_impl(db, visitor))
@@ -5732,7 +5736,7 @@ fn walk_known_instance_type<'db, V: visitor::TypeVisitor<'db> + ?Sized>(
57325736
}
57335737

57345738
impl<'db> KnownInstanceType<'db> {
5735-
fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeVisitor<'db>) -> Self {
5739+
fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeTransformer<'db>) -> Self {
57365740
match self {
57375741
Self::SubscriptedProtocol(context) => {
57385742
Self::SubscriptedProtocol(context.normalized_impl(db, visitor))
@@ -6137,7 +6141,11 @@ impl<'db> TypeVarInstance<'db> {
61376141
}
61386142
}
61396143

6140-
pub(crate) fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeVisitor<'db>) -> Self {
6144+
pub(crate) fn normalized_impl(
6145+
self,
6146+
db: &'db dyn Db,
6147+
visitor: &mut TypeTransformer<'db>,
6148+
) -> Self {
61416149
Self::new(
61426150
db,
61436151
self.name(db),
@@ -6206,7 +6214,7 @@ fn walk_type_var_bounds<'db, V: visitor::TypeVisitor<'db> + ?Sized>(
62066214
}
62076215

62086216
impl<'db> TypeVarBoundOrConstraints<'db> {
6209-
fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeVisitor<'db>) -> Self {
6217+
fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeTransformer<'db>) -> Self {
62106218
match self {
62116219
TypeVarBoundOrConstraints::UpperBound(bound) => {
62126220
TypeVarBoundOrConstraints::UpperBound(bound.normalized_impl(db, visitor))
@@ -7133,7 +7141,7 @@ impl<'db> BoundMethodType<'db> {
71337141
))
71347142
}
71357143

7136-
fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeVisitor<'db>) -> Self {
7144+
fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeTransformer<'db>) -> Self {
71377145
Self::new(
71387146
db,
71397147
self.function(db).normalized_impl(db, visitor),
@@ -7250,7 +7258,7 @@ impl<'db> CallableType<'db> {
72507258
/// Return a "normalized" version of this `Callable` type.
72517259
///
72527260
/// See [`Type::normalized`] for more details.
7253-
fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeVisitor<'db>) -> Self {
7261+
fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeTransformer<'db>) -> Self {
72547262
CallableType::new(
72557263
db,
72567264
self.signatures(db).normalized_impl(db, visitor),
@@ -7408,7 +7416,7 @@ impl<'db> MethodWrapperKind<'db> {
74087416
}
74097417
}
74107418

7411-
fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeVisitor<'db>) -> Self {
7419+
fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeTransformer<'db>) -> Self {
74127420
match self {
74137421
MethodWrapperKind::FunctionTypeDunderGet(function) => {
74147422
MethodWrapperKind::FunctionTypeDunderGet(function.normalized_impl(db, visitor))
@@ -7516,10 +7524,11 @@ pub struct PEP695TypeAliasType<'db> {
75167524
impl get_size2::GetSize for PEP695TypeAliasType<'_> {}
75177525

75187526
fn walk_pep_695_type_alias<'db, V: visitor::TypeVisitor<'db> + ?Sized>(
7519-
_db: &'db dyn Db,
7520-
_type_alias: PEP695TypeAliasType<'db>,
7521-
_visitor: &mut V,
7527+
db: &'db dyn Db,
7528+
type_alias: PEP695TypeAliasType<'db>,
7529+
visitor: &mut V,
75227530
) {
7531+
visitor.visit_type(db, type_alias.value_type(db));
75237532
}
75247533

75257534
#[salsa::tracked]
@@ -7541,7 +7550,7 @@ impl<'db> PEP695TypeAliasType<'db> {
75417550
definition_expression_type(db, definition, &type_alias_stmt_node.value)
75427551
}
75437552

7544-
fn normalized_impl(self, _db: &'db dyn Db, _visitor: &mut TypeVisitor<'db>) -> Self {
7553+
fn normalized_impl(self, _db: &'db dyn Db, _visitor: &mut TypeTransformer<'db>) -> Self {
75457554
self
75467555
}
75477556
}
@@ -7570,7 +7579,7 @@ fn walk_bare_type_alias<'db, V: visitor::TypeVisitor<'db> + ?Sized>(
75707579
}
75717580

75727581
impl<'db> BareTypeAliasType<'db> {
7573-
fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeVisitor<'db>) -> Self {
7582+
fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeTransformer<'db>) -> Self {
75747583
Self::new(
75757584
db,
75767585
self.name(db),
@@ -7604,7 +7613,11 @@ fn walk_type_alias_type<'db, V: visitor::TypeVisitor<'db> + ?Sized>(
76047613
}
76057614

76067615
impl<'db> TypeAliasType<'db> {
7607-
pub(crate) fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeVisitor<'db>) -> Self {
7616+
pub(crate) fn normalized_impl(
7617+
self,
7618+
db: &'db dyn Db,
7619+
visitor: &mut TypeTransformer<'db>,
7620+
) -> Self {
76087621
match self {
76097622
TypeAliasType::PEP695(type_alias) => {
76107623
TypeAliasType::PEP695(type_alias.normalized_impl(db, visitor))
@@ -7830,10 +7843,14 @@ impl<'db> UnionType<'db> {
78307843
/// See [`Type::normalized`] for more details.
78317844
#[must_use]
78327845
pub(crate) fn normalized(self, db: &'db dyn Db) -> Self {
7833-
self.normalized_impl(db, &mut TypeVisitor::default())
7846+
self.normalized_impl(db, &mut TypeTransformer::default())
78347847
}
78357848

7836-
pub(crate) fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeVisitor<'db>) -> Self {
7849+
pub(crate) fn normalized_impl(
7850+
self,
7851+
db: &'db dyn Db,
7852+
visitor: &mut TypeTransformer<'db>,
7853+
) -> Self {
78377854
let mut new_elements: Vec<Type<'db>> = self
78387855
.elements(db)
78397856
.iter()
@@ -7904,15 +7921,19 @@ impl<'db> IntersectionType<'db> {
79047921
/// See [`Type::normalized`] for more details.
79057922
#[must_use]
79067923
pub(crate) fn normalized(self, db: &'db dyn Db) -> Self {
7907-
let mut visitor = TypeVisitor::default();
7924+
let mut visitor = TypeTransformer::default();
79087925
self.normalized_impl(db, &mut visitor)
79097926
}
79107927

7911-
pub(crate) fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeVisitor<'db>) -> Self {
7928+
pub(crate) fn normalized_impl(
7929+
self,
7930+
db: &'db dyn Db,
7931+
visitor: &mut TypeTransformer<'db>,
7932+
) -> Self {
79127933
fn normalized_set<'db>(
79137934
db: &'db dyn Db,
79147935
elements: &FxOrderSet<Type<'db>>,
7915-
visitor: &mut TypeVisitor<'db>,
7936+
visitor: &mut TypeTransformer<'db>,
79167937
) -> FxOrderSet<Type<'db>> {
79177938
let mut elements: FxOrderSet<Type<'db>> = elements
79187939
.iter()
@@ -8165,7 +8186,7 @@ pub enum SuperOwnerKind<'db> {
81658186
}
81668187

81678188
impl<'db> SuperOwnerKind<'db> {
8168-
fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeVisitor<'db>) -> Self {
8189+
fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeTransformer<'db>) -> Self {
81698190
match self {
81708191
SuperOwnerKind::Dynamic(dynamic) => SuperOwnerKind::Dynamic(dynamic.normalized()),
81718192
SuperOwnerKind::Class(class) => {
@@ -8423,7 +8444,11 @@ impl<'db> BoundSuperType<'db> {
84238444
}
84248445
}
84258446

8426-
pub(super) fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeVisitor<'db>) -> Self {
8447+
pub(super) fn normalized_impl(
8448+
self,
8449+
db: &'db dyn Db,
8450+
visitor: &mut TypeTransformer<'db>,
8451+
) -> Self {
84278452
Self::new(
84288453
db,
84298454
self.pivot_class(db).normalized_impl(db, visitor),

crates/ty_python_semantic/src/types/class.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ use crate::types::signatures::{CallableSignature, Parameter, Parameters, Signatu
2121
use crate::types::tuple::TupleType;
2222
use crate::types::{
2323
BareTypeAliasType, Binding, BoundSuperError, BoundSuperType, CallableType, DataclassParams,
24-
KnownInstanceType, TypeAliasType, TypeMapping, TypeRelation, TypeVarBoundOrConstraints,
25-
TypeVarInstance, TypeVarKind, TypeVisitor, infer_definition_types,
24+
KnownInstanceType, TypeAliasType, TypeMapping, TypeRelation, TypeTransformer,
25+
TypeVarBoundOrConstraints, TypeVarInstance, TypeVarKind, infer_definition_types,
2626
};
2727
use crate::{
2828
Db, FxOrderSet, KnownModule, Program,
@@ -190,7 +190,11 @@ pub(super) fn walk_generic_alias<'db, V: super::visitor::TypeVisitor<'db> + ?Siz
190190
impl get_size2::GetSize for GenericAlias<'_> {}
191191

192192
impl<'db> GenericAlias<'db> {
193-
pub(super) fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeVisitor<'db>) -> Self {
193+
pub(super) fn normalized_impl(
194+
self,
195+
db: &'db dyn Db,
196+
visitor: &mut TypeTransformer<'db>,
197+
) -> Self {
194198
Self::new(
195199
db,
196200
self.origin(db),
@@ -261,7 +265,11 @@ pub enum ClassType<'db> {
261265

262266
#[salsa::tracked]
263267
impl<'db> ClassType<'db> {
264-
pub(super) fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeVisitor<'db>) -> Self {
268+
pub(super) fn normalized_impl(
269+
self,
270+
db: &'db dyn Db,
271+
visitor: &mut TypeTransformer<'db>,
272+
) -> Self {
265273
match self {
266274
Self::NonGeneric(_) => self,
267275
Self::Generic(generic) => Self::Generic(generic.normalized_impl(db, visitor)),

crates/ty_python_semantic/src/types/class_base.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::Db;
22
use crate::types::generics::Specialization;
33
use crate::types::{
44
ClassType, DynamicType, KnownClass, KnownInstanceType, MroError, MroIterator, SpecialFormType,
5-
Type, TypeMapping, TypeVisitor, todo_type,
5+
Type, TypeMapping, TypeTransformer, todo_type,
66
};
77

88
/// Enumeration of the possible kinds of types we allow in class bases.
@@ -31,7 +31,11 @@ impl<'db> ClassBase<'db> {
3131
Self::Dynamic(DynamicType::Unknown)
3232
}
3333

34-
pub(crate) fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeVisitor<'db>) -> Self {
34+
pub(crate) fn normalized_impl(
35+
self,
36+
db: &'db dyn Db,
37+
visitor: &mut TypeTransformer<'db>,
38+
) -> Self {
3539
match self {
3640
Self::Dynamic(dynamic) => Self::Dynamic(dynamic.normalized()),
3741
Self::Class(class) => Self::Class(class.normalized_impl(db, visitor)),

crates/ty_python_semantic/src/types/cyclic.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use crate::FxOrderSet;
1+
use crate::FxIndexSet;
22
use crate::types::Type;
33

44
#[derive(Debug, Default)]
5-
pub(crate) struct TypeVisitor<'db> {
6-
seen: FxOrderSet<Type<'db>>,
5+
pub(crate) struct TypeTransformer<'db> {
6+
seen: FxIndexSet<Type<'db>>,
77
}
88

9-
impl<'db> TypeVisitor<'db> {
9+
impl<'db> TypeTransformer<'db> {
1010
pub(crate) fn visit(
1111
&mut self,
1212
ty: Type<'db>,

crates/ty_python_semantic/src/types/function.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ use crate::types::signatures::{CallableSignature, Signature};
7676
use crate::types::visitor::any_over_type;
7777
use crate::types::{
7878
BoundMethodType, CallableType, DynamicType, KnownClass, Type, TypeMapping, TypeRelation,
79-
TypeVarInstance, TypeVisitor, walk_type_mapping,
79+
TypeTransformer, TypeVarInstance, walk_type_mapping,
8080
};
8181
use crate::{Db, FxOrderSet, ModuleName, resolve_module};
8282

@@ -556,7 +556,7 @@ impl<'db> FunctionLiteral<'db> {
556556
}))
557557
}
558558

559-
fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeVisitor<'db>) -> Self {
559+
fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeTransformer<'db>) -> Self {
560560
let context = self
561561
.inherited_generic_context(db)
562562
.map(|ctx| ctx.normalized_impl(db, visitor));
@@ -841,11 +841,15 @@ impl<'db> FunctionType<'db> {
841841
}
842842

843843
pub(crate) fn normalized(self, db: &'db dyn Db) -> Self {
844-
let mut visitor = TypeVisitor::default();
844+
let mut visitor = TypeTransformer::default();
845845
self.normalized_impl(db, &mut visitor)
846846
}
847847

848-
pub(crate) fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeVisitor<'db>) -> Self {
848+
pub(crate) fn normalized_impl(
849+
self,
850+
db: &'db dyn Db,
851+
visitor: &mut TypeTransformer<'db>,
852+
) -> Self {
849853
let mappings: Box<_> = self
850854
.type_mappings(db)
851855
.iter()

crates/ty_python_semantic/src/types/generics.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use crate::types::instance::{NominalInstanceType, Protocol, ProtocolInstanceType
1010
use crate::types::signatures::{Parameter, Parameters, Signature};
1111
use crate::types::tuple::{TupleSpec, TupleType};
1212
use crate::types::{
13-
KnownInstanceType, Type, TypeMapping, TypeRelation, TypeVarBoundOrConstraints, TypeVarInstance,
14-
TypeVarVariance, TypeVisitor, UnionType, declaration_type,
13+
KnownInstanceType, Type, TypeMapping, TypeRelation, TypeTransformer, TypeVarBoundOrConstraints,
14+
TypeVarInstance, TypeVarVariance, UnionType, declaration_type,
1515
};
1616
use crate::{Db, FxOrderSet};
1717

@@ -243,7 +243,11 @@ impl<'db> GenericContext<'db> {
243243
Specialization::new(db, self, expanded.into_boxed_slice(), None)
244244
}
245245

246-
pub(crate) fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeVisitor<'db>) -> Self {
246+
pub(crate) fn normalized_impl(
247+
self,
248+
db: &'db dyn Db,
249+
visitor: &mut TypeTransformer<'db>,
250+
) -> Self {
247251
let variables: FxOrderSet<_> = self
248252
.variables(db)
249253
.iter()
@@ -400,7 +404,11 @@ impl<'db> Specialization<'db> {
400404
Specialization::new(db, self.generic_context(db), types, None)
401405
}
402406

403-
pub(crate) fn normalized_impl(self, db: &'db dyn Db, visitor: &mut TypeVisitor<'db>) -> Self {
407+
pub(crate) fn normalized_impl(
408+
self,
409+
db: &'db dyn Db,
410+
visitor: &mut TypeTransformer<'db>,
411+
) -> Self {
404412
let types: Box<[_]> = self
405413
.types(db)
406414
.iter()
@@ -571,7 +579,7 @@ impl<'db> PartialSpecialization<'_, 'db> {
571579
pub(crate) fn normalized_impl(
572580
&self,
573581
db: &'db dyn Db,
574-
visitor: &mut TypeVisitor<'db>,
582+
visitor: &mut TypeTransformer<'db>,
575583
) -> PartialSpecialization<'db, 'db> {
576584
let generic_context = self.generic_context.normalized_impl(db, visitor);
577585
let types: Cow<_> = self

0 commit comments

Comments
 (0)