Skip to content

Commit 9f1afde

Browse files
committed
wip
1 parent 0868e73 commit 9f1afde

File tree

5 files changed

+38
-19
lines changed

5 files changed

+38
-19
lines changed

crates/red_knot_python_semantic/src/symbol.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -549,10 +549,11 @@ fn symbol_from_bindings_impl<'db>(
549549
};
550550

551551
if let Some(second) = types.next() {
552-
Symbol::Type(
553-
UnionType::from_elements(db, [first, second].into_iter().chain(types)),
554-
boundness,
555-
)
552+
let ty = UnionBuilder::new(db)
553+
.extend([first, second])
554+
.extend(types)
555+
.build();
556+
Symbol::Type(ty, boundness)
556557
} else {
557558
Symbol::Type(first, boundness)
558559
}

crates/red_knot_python_semantic/src/types.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4177,12 +4177,7 @@ impl<'db> UnionType<'db> {
41774177
I: IntoIterator<Item = T>,
41784178
T: Into<Type<'db>>,
41794179
{
4180-
elements
4181-
.into_iter()
4182-
.fold(UnionBuilder::new(db), |builder, element| {
4183-
builder.add(element.into())
4184-
})
4185-
.build()
4180+
UnionBuilder::new(db).extend(elements).build()
41864181
}
41874182

41884183
/// Apply a transformation function to all elements of the union,

crates/red_knot_python_semantic/src/types/builder.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,27 @@ impl<'db> UnionBuilder<'db> {
4343
}
4444
}
4545

46+
pub(crate) fn with_capacity(db: &'db dyn Db, capacity: usize) -> Self {
47+
Self {
48+
db,
49+
elements: Vec::with_capacity(capacity),
50+
}
51+
}
52+
53+
pub(crate) fn extend<I, T>(mut self, types: I) -> Self
54+
where
55+
I: IntoIterator<Item = T>,
56+
T: Into<Type<'db>>,
57+
{
58+
let types = types.into_iter();
59+
let (lower, upper) = types.size_hint();
60+
self.elements.reserve(upper.unwrap_or(lower));
61+
for ty in types {
62+
self = self.add(ty.into());
63+
}
64+
self
65+
}
66+
4667
/// Collapse the union to a single type: `object`.
4768
fn collapse_to_object(mut self) -> Self {
4869
self.elements.clear();

crates/red_knot_python_semantic/src/types/call.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::context::InferContext;
2-
use super::{Signature, Type};
2+
use super::{Signature, Type, UnionBuilder};
33
use crate::types::UnionType;
44
use crate::Db;
55

@@ -128,13 +128,13 @@ impl<'db> CallError<'db> {
128128
// combined with `Type::Unknown`
129129
CallError::Union {
130130
errors, bindings, ..
131-
} => Some(UnionType::from_elements(
132-
db,
133-
bindings
134-
.iter()
135-
.map(CallBinding::return_type)
136-
.chain(errors.iter().map(|err| err.fallback_return_type(db))),
137-
)),
131+
} => Some(
132+
UnionBuilder::new(db)
133+
.extend(bindings.iter().map(CallBinding::return_type))
134+
.extend(errors.iter().map(|err| err.fallback_return_type(db)))
135+
.build(),
136+
),
137+
138138
Self::PossiblyUnboundDunderCall { outcome, .. } => Some(outcome.return_type(db)),
139139
Self::BindingError { binding } => Some(binding.return_type()),
140140
}

crates/red_knot_python_semantic/src/types/infer.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4079,7 +4079,9 @@ impl<'db> TypeInferenceBuilder<'db> {
40794079
}
40804080
});
40814081

4082-
UnionType::from_elements(db, elements)
4082+
UnionBuilder::with_capacity(db, n_values)
4083+
.extend(elements)
4084+
.build()
40834085
}
40844086

40854087
fn infer_compare_expression(&mut self, compare: &ast::ExprCompare) -> Type<'db> {

0 commit comments

Comments
 (0)