Skip to content

Commit 4f9a3b4

Browse files
committed
Use FxOrderSet to store generic context typevars
1 parent 547fced commit 4f9a3b4

File tree

3 files changed

+15
-21
lines changed

3 files changed

+15
-21
lines changed

crates/red_knot_python_semantic/src/types/display.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::types::{
1414
StringLiteralType, SubclassOfInner, Type, TypeVarBoundOrConstraints, TypeVarInstance,
1515
UnionType, WrapperDescriptorKind,
1616
};
17-
use crate::Db;
17+
use crate::{Db, FxOrderSet};
1818
use rustc_hash::FxHashMap;
1919

2020
impl<'db> Type<'db> {
@@ -317,7 +317,7 @@ impl<'db> GenericContext<'db> {
317317
}
318318

319319
pub struct DisplayGenericContext<'db> {
320-
typevars: &'db [TypeVarInstance<'db>],
320+
typevars: &'db FxOrderSet<TypeVarInstance<'db>>,
321321
db: &'db dyn Db,
322322
}
323323

@@ -376,7 +376,7 @@ impl<'db> Specialization<'db> {
376376
}
377377

378378
pub struct DisplaySpecialization<'db> {
379-
typevars: &'db [TypeVarInstance<'db>],
379+
typevars: &'db FxOrderSet<TypeVarInstance<'db>>,
380380
types: &'db [Type<'db>],
381381
db: &'db dyn Db,
382382
full: bool,

crates/red_knot_python_semantic/src/types/generics.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use itertools::Itertools;
21
use ruff_python_ast as ast;
32
use rustc_hash::FxHashMap;
43

@@ -17,7 +16,7 @@ use crate::{Db, FxOrderSet};
1716
#[salsa::interned(debug)]
1817
pub struct GenericContext<'db> {
1918
#[return_ref]
20-
pub(crate) variables: Box<[TypeVarInstance<'db>]>,
19+
pub(crate) variables: FxOrderSet<TypeVarInstance<'db>>,
2120
}
2221

2322
impl<'db> GenericContext<'db> {
@@ -27,7 +26,7 @@ impl<'db> GenericContext<'db> {
2726
index: &'db SemanticIndex<'db>,
2827
type_params_node: &ast::TypeParams,
2928
) -> Self {
30-
let variables: Box<[_]> = type_params_node
29+
let variables: FxOrderSet<_> = type_params_node
3130
.iter()
3231
.filter_map(|type_param| Self::variable_from_type_param(db, index, type_param))
3332
.collect();
@@ -77,7 +76,6 @@ impl<'db> GenericContext<'db> {
7776
if variables.is_empty() {
7877
return None;
7978
}
80-
let variables: Box<[_]> = variables.into_iter().collect();
8179
Some(Self::new(db, variables))
8280
}
8381

@@ -94,7 +92,6 @@ impl<'db> GenericContext<'db> {
9492
if variables.is_empty() {
9593
return None;
9694
}
97-
let variables: Box<[_]> = variables.into_iter().collect();
9895
Some(Self::new(db, variables))
9996
}
10097

@@ -149,19 +146,17 @@ impl<'db> GenericContext<'db> {
149146
}
150147

151148
pub(crate) fn is_subset_of(self, db: &'db dyn Db, other: GenericContext<'db>) -> bool {
152-
for variable in self.variables(db) {
153-
if !other.variables(db).iter().contains(variable) {
154-
return false;
155-
}
156-
}
157-
true
149+
self.variables(db).is_subset(other.variables(db))
158150
}
159151

152+
/// Creates a specialization of this generic context. Panics if the length of `types` does not
153+
/// match the number of typevars in the generic context.
160154
pub(crate) fn specialize(
161155
self,
162156
db: &'db dyn Db,
163157
types: Box<[Type<'db>]>,
164158
) -> Specialization<'db> {
159+
assert!(self.variables(db).len() == types.len());
165160
Specialization::new(db, self, types)
166161
}
167162
}
@@ -232,12 +227,11 @@ impl<'db> Specialization<'db> {
232227
/// Returns the type that a typevar is specialized to, or None if the typevar isn't part of
233228
/// this specialization.
234229
pub(crate) fn get(self, db: &'db dyn Db, typevar: TypeVarInstance<'db>) -> Option<Type<'db>> {
235-
self.generic_context(db)
230+
let index = self
231+
.generic_context(db)
236232
.variables(db)
237-
.into_iter()
238-
.zip(self.types(db))
239-
.find(|(var, _)| **var == typevar)
240-
.map(|(_, ty)| *ty)
233+
.get_index_of(&typevar)?;
234+
Some(self.types(db)[index])
241235
}
242236

243237
pub(crate) fn is_subtype_of(self, db: &'db dyn Db, other: Specialization<'db>) -> bool {

crates/red_knot_python_semantic/src/types/infer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ use crate::types::{
9393
};
9494
use crate::unpack::{Unpack, UnpackPosition};
9595
use crate::util::subscript::{PyIndex, PySlice};
96-
use crate::Db;
96+
use crate::{Db, FxOrderSet};
9797

9898
use super::context::{InNoTypeCheck, InferContext};
9999
use super::diagnostic::{
@@ -6998,7 +6998,7 @@ impl<'db> TypeInferenceBuilder<'db> {
69986998
value_node: &ast::Expr,
69996999
typevars: &[Type<'db>],
70007000
) -> Type<'db> {
7001-
let typevars: Option<Box<[_]>> = typevars
7001+
let typevars: Option<FxOrderSet<_>> = typevars
70027002
.iter()
70037003
.map(|typevar| match typevar {
70047004
Type::KnownInstance(KnownInstanceType::TypeVar(typevar)) => Some(*typevar),

0 commit comments

Comments
 (0)