Skip to content

Commit 4346e7d

Browse files
committed
Add Specialization
1 parent fe76d56 commit 4346e7d

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

crates/red_knot_python_semantic/src/types/generics.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,19 @@ impl<'db> GenericContext<'db> {
7474
}
7575
parameter
7676
}
77+
78+
pub(crate) fn specialize(
79+
self,
80+
db: &'db dyn Db,
81+
types: Box<[Type<'db>]>,
82+
) -> Specialization<'db> {
83+
Specialization::new(db, self, types)
84+
}
85+
}
86+
87+
/// An assignment of a specific type to each type variable in a generic scope.
88+
#[salsa::tracked(debug)]
89+
pub(crate) struct Specialization<'db> {
90+
generic_context: GenericContext<'db>,
91+
types: Box<[Type<'db>]>,
7792
}

crates/red_knot_python_semantic/src/types/infer.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5607,13 +5607,30 @@ impl<'db> TypeInferenceBuilder<'db> {
56075607
value_ty,
56085608
generic_context.signature(self.db()),
56095609
));
5610-
if let Err(CallError(_, bindings)) =
5611-
Bindings::match_parameters(signatures, &mut call_argument_types)
5612-
.check_types(self.db(), &mut call_argument_types)
5610+
let bindings = match Bindings::match_parameters(signatures, &mut call_argument_types)
5611+
.check_types(self.db(), &mut call_argument_types)
56135612
{
5614-
bindings.report_diagnostics(&self.context, subscript.into());
5615-
return Type::unknown();
5616-
}
5613+
Ok(bindings) => bindings,
5614+
Err(CallError(_, bindings)) => {
5615+
bindings.report_diagnostics(&self.context, subscript.into());
5616+
return Type::unknown();
5617+
}
5618+
};
5619+
let callable = bindings
5620+
.into_iter()
5621+
.next()
5622+
.expect("valid bindings should have one callable");
5623+
let (_, overload) = callable
5624+
.matching_overload()
5625+
.expect("valid bindings should have matching overload");
5626+
let _specialization = generic_context.specialize(
5627+
self.db(),
5628+
overload
5629+
.parameter_types()
5630+
.iter()
5631+
.map(|ty| ty.unwrap_or(Type::unknown()))
5632+
.collect(),
5633+
);
56175634
value_ty
56185635
}
56195636

0 commit comments

Comments
 (0)