Skip to content

Commit 88d0720

Browse files
authored
[red-knot] Reduce Name clones in call signature checking (#15335)
1 parent 2ca31e4 commit 88d0720

File tree

4 files changed

+21
-25
lines changed

4 files changed

+21
-25
lines changed

crates/red_knot_python_semantic/src/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1827,7 +1827,7 @@ impl<'db> Type<'db> {
18271827

18281828
/// Return the outcome of calling an object of this type.
18291829
#[must_use]
1830-
fn call(self, db: &'db dyn Db, arguments: &CallArguments<'db>) -> CallOutcome<'db> {
1830+
fn call(self, db: &'db dyn Db, arguments: &CallArguments<'_, 'db>) -> CallOutcome<'db> {
18311831
match self {
18321832
Type::FunctionLiteral(function_type) => {
18331833
let mut binding = bind_call(db, arguments, function_type.signature(db), Some(self));
@@ -1995,7 +1995,7 @@ impl<'db> Type<'db> {
19951995
self,
19961996
db: &'db dyn Db,
19971997
name: &str,
1998-
arguments: &CallArguments<'db>,
1998+
arguments: &CallArguments<'_, 'db>,
19991999
) -> CallDunderResult<'db> {
20002000
match self.to_meta_type(db).member(db, name) {
20012001
Symbol::Type(callable_ty, Boundness::Bound) => {

crates/red_knot_python_semantic/src/types/call/arguments.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use super::Type;
2-
use ruff_python_ast::name::Name;
32

43
/// Typed arguments for a single call, in source order.
54
#[derive(Clone, Debug, Default)]
6-
pub(crate) struct CallArguments<'db>(Vec<Argument<'db>>);
5+
pub(crate) struct CallArguments<'a, 'db>(Vec<Argument<'a, 'db>>);
76

8-
impl<'db> CallArguments<'db> {
7+
impl<'a, 'db> CallArguments<'a, 'db> {
98
/// Create a [`CallArguments`] from an iterator over non-variadic positional argument types.
109
pub(crate) fn positional(positional_tys: impl IntoIterator<Item = Type<'db>>) -> Self {
1110
positional_tys
@@ -22,7 +21,7 @@ impl<'db> CallArguments<'db> {
2221
Self(arguments)
2322
}
2423

25-
pub(crate) fn iter(&self) -> impl Iterator<Item = &Argument<'db>> {
24+
pub(crate) fn iter(&self) -> impl Iterator<Item = &Argument<'a, 'db>> {
2625
self.0.iter()
2726
}
2827

@@ -32,34 +31,34 @@ impl<'db> CallArguments<'db> {
3231
}
3332
}
3433

35-
impl<'db, 'a> IntoIterator for &'a CallArguments<'db> {
36-
type Item = &'a Argument<'db>;
37-
type IntoIter = std::slice::Iter<'a, Argument<'db>>;
34+
impl<'db, 'a, 'b> IntoIterator for &'b CallArguments<'a, 'db> {
35+
type Item = &'b Argument<'a, 'db>;
36+
type IntoIter = std::slice::Iter<'b, Argument<'a, 'db>>;
3837

3938
fn into_iter(self) -> Self::IntoIter {
4039
self.0.iter()
4140
}
4241
}
4342

44-
impl<'db> FromIterator<Argument<'db>> for CallArguments<'db> {
45-
fn from_iter<T: IntoIterator<Item = Argument<'db>>>(iter: T) -> Self {
43+
impl<'a, 'db> FromIterator<Argument<'a, 'db>> for CallArguments<'a, 'db> {
44+
fn from_iter<T: IntoIterator<Item = Argument<'a, 'db>>>(iter: T) -> Self {
4645
Self(iter.into_iter().collect())
4746
}
4847
}
4948

5049
#[derive(Clone, Debug)]
51-
pub(crate) enum Argument<'db> {
50+
pub(crate) enum Argument<'a, 'db> {
5251
/// A positional argument.
5352
Positional(Type<'db>),
5453
/// A starred positional argument (e.g. `*args`).
5554
Variadic(Type<'db>),
5655
/// A keyword argument (e.g. `a=1`).
57-
Keyword { name: Name, ty: Type<'db> },
56+
Keyword { name: &'a str, ty: Type<'db> },
5857
/// The double-starred keywords argument (e.g. `**kwargs`).
5958
Keywords(Type<'db>),
6059
}
6160

62-
impl<'db> Argument<'db> {
61+
impl<'db> Argument<'_, 'db> {
6362
fn ty(&self) -> Type<'db> {
6463
match self {
6564
Self::Positional(ty) => *ty,

crates/red_knot_python_semantic/src/types/call/bind.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use ruff_python_ast as ast;
1414
/// parameters, and any errors resulting from binding the call.
1515
pub(crate) fn bind_call<'db>(
1616
db: &'db dyn Db,
17-
arguments: &CallArguments<'db>,
17+
arguments: &CallArguments<'_, 'db>,
1818
signature: &Signature<'db>,
1919
callable_ty: Option<Type<'db>>,
2020
) -> CallBinding<'db> {
@@ -45,7 +45,7 @@ pub(crate) fn bind_call<'db>(
4545
.or_else(|| parameters.keyword_variadic())
4646
else {
4747
errors.push(CallBindingError::UnknownArgument {
48-
argument_name: name.clone(),
48+
argument_name: ast::name::Name::new(name),
4949
argument_index,
5050
});
5151
continue;
@@ -168,7 +168,7 @@ impl<'db> CallBinding<'db> {
168168
}
169169
}
170170

171-
fn callable_name(&self, db: &'db dyn Db) -> Option<&ast::name::Name> {
171+
fn callable_name(&self, db: &'db dyn Db) -> Option<&str> {
172172
match self.callable_ty {
173173
Some(Type::FunctionLiteral(function)) => Some(function.name(db)),
174174
Some(Type::ClassLiteral(class_type)) => Some(class_type.class.name(db)),
@@ -272,7 +272,7 @@ impl<'db> CallBindingError<'db> {
272272
&self,
273273
context: &InferContext<'db>,
274274
node: ast::AnyNodeRef,
275-
callable_name: Option<&ast::name::Name>,
275+
callable_name: Option<&str>,
276276
) {
277277
match self {
278278
Self::InvalidArgumentType {

crates/red_knot_python_semantic/src/types/infer.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,11 +2523,11 @@ impl<'db> TypeInferenceBuilder<'db> {
25232523
self.infer_expression(expression)
25242524
}
25252525

2526-
fn infer_arguments(
2526+
fn infer_arguments<'a>(
25272527
&mut self,
2528-
arguments: &ast::Arguments,
2528+
arguments: &'a ast::Arguments,
25292529
infer_as_type_expressions: bool,
2530-
) -> CallArguments<'db> {
2530+
) -> CallArguments<'a, 'db> {
25312531
let infer_argument_type = if infer_as_type_expressions {
25322532
Self::infer_type_expression
25332533
} else {
@@ -2558,10 +2558,7 @@ impl<'db> TypeInferenceBuilder<'db> {
25582558
}) => {
25592559
let ty = infer_argument_type(self, value);
25602560
if let Some(arg) = arg {
2561-
Argument::Keyword {
2562-
name: arg.id.clone(),
2563-
ty,
2564-
}
2561+
Argument::Keyword { name: &arg.id, ty }
25652562
} else {
25662563
// TODO diagnostic if not last
25672564
Argument::Keywords(ty)

0 commit comments

Comments
 (0)