Skip to content

[red-knot] simplify union size limit handling #17429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 6 additions & 21 deletions crates/red_knot_python_semantic/src/types/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,13 @@ impl<'db> UnionBuilder<'db> {
// means we shouldn't add it. Otherwise, add a new `UnionElement::StringLiterals`
// containing it.
Type::StringLiteral(literal) => {
let mut too_large = false;
let mut found = false;
for element in &mut self.elements {
match element {
UnionElement::StringLiterals(literals) => {
if literals.len() >= MAX_UNION_LITERALS {
too_large = true;
break;
let replace_with = KnownClass::Str.to_instance(self.db);
return self.add(replace_with);
}
literals.insert(literal);
found = true;
Expand All @@ -116,10 +115,6 @@ impl<'db> UnionBuilder<'db> {
_ => {}
}
}
if too_large {
let replace_with = KnownClass::Str.to_instance(self.db);
return self.add(replace_with);
}
if !found {
self.elements
.push(UnionElement::StringLiterals(FxOrderSet::from_iter([
Expand All @@ -130,13 +125,12 @@ impl<'db> UnionBuilder<'db> {
// Same for bytes literals as for string literals, above.
Type::BytesLiteral(literal) => {
let mut found = false;
let mut too_large = false;
for element in &mut self.elements {
match element {
UnionElement::BytesLiterals(literals) => {
if literals.len() >= MAX_UNION_LITERALS {
too_large = true;
break;
let replace_with = KnownClass::Bytes.to_instance(self.db);
return self.add(replace_with);
}
literals.insert(literal);
found = true;
Expand All @@ -148,10 +142,6 @@ impl<'db> UnionBuilder<'db> {
_ => {}
}
}
if too_large {
let replace_with = KnownClass::Bytes.to_instance(self.db);
return self.add(replace_with);
}
if !found {
self.elements
.push(UnionElement::BytesLiterals(FxOrderSet::from_iter([
Expand All @@ -162,13 +152,12 @@ impl<'db> UnionBuilder<'db> {
// And same for int literals as well.
Type::IntLiteral(literal) => {
let mut found = false;
let mut too_large = false;
for element in &mut self.elements {
match element {
UnionElement::IntLiterals(literals) => {
if literals.len() >= MAX_UNION_LITERALS {
too_large = true;
break;
let replace_with = KnownClass::Int.to_instance(self.db);
return self.add(replace_with);
}
literals.insert(literal);
found = true;
Expand All @@ -180,10 +169,6 @@ impl<'db> UnionBuilder<'db> {
_ => {}
}
}
if too_large {
let replace_with = KnownClass::Int.to_instance(self.db);
return self.add(replace_with);
}
if !found {
self.elements
.push(UnionElement::IntLiterals(FxOrderSet::from_iter([literal])));
Expand Down
Loading