[ty] Minor followups to #22048 (#22082)

This commit is contained in:
Alex Waygood 2025-12-19 13:58:59 +00:00 committed by GitHub
parent f9d1a282fb
commit 28cdbb18c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 10 additions and 11 deletions

View File

@ -325,6 +325,9 @@ impl<'db> UnionBuilder<'db> {
} }
}; };
let mut ty_negated_cache = None;
let mut ty_negated = || *ty_negated_cache.get_or_insert_with(|| ty.negate(self.db));
match ty { match ty {
Type::Union(union) => { Type::Union(union) => {
let new_elements = union.elements(self.db); let new_elements = union.elements(self.db);
@ -365,7 +368,6 @@ impl<'db> UnionBuilder<'db> {
Type::StringLiteral(literal) => { Type::StringLiteral(literal) => {
let mut found = None; let mut found = None;
let mut to_remove = None; let mut to_remove = None;
let mut ty_negated = None;
for (index, element) in self.elements.iter_mut().enumerate() { for (index, element) in self.elements.iter_mut().enumerate() {
match element { match element {
UnionElement::StringLiterals(literals) => { UnionElement::StringLiterals(literals) => {
@ -387,8 +389,7 @@ impl<'db> UnionBuilder<'db> {
to_remove = Some(index); to_remove = Some(index);
continue; continue;
} }
let negated = ty_negated.get_or_insert_with(|| ty.negate(self.db)); if ty_negated().is_subtype_of(self.db, *existing) {
if negated.is_subtype_of(self.db, *existing) {
// The type that includes both this new element, and its negation // The type that includes both this new element, and its negation
// (or a supertype of its negation), must be simply `object`. // (or a supertype of its negation), must be simply `object`.
self.collapse_to_object(); self.collapse_to_object();
@ -414,7 +415,6 @@ impl<'db> UnionBuilder<'db> {
Type::BytesLiteral(literal) => { Type::BytesLiteral(literal) => {
let mut found = None; let mut found = None;
let mut to_remove = None; let mut to_remove = None;
let mut ty_negated = None;
for (index, element) in self.elements.iter_mut().enumerate() { for (index, element) in self.elements.iter_mut().enumerate() {
match element { match element {
UnionElement::BytesLiterals(literals) => { UnionElement::BytesLiterals(literals) => {
@ -430,13 +430,13 @@ impl<'db> UnionBuilder<'db> {
if ty.is_subtype_of(self.db, *existing) { if ty.is_subtype_of(self.db, *existing) {
return; return;
} }
// e.g. `existing` could be `Literal[b""] & Any`,
// and `ty` could be `Literal[b""]`
if existing.is_subtype_of(self.db, ty) { if existing.is_subtype_of(self.db, ty) {
to_remove = Some(index); to_remove = Some(index);
continue; continue;
} }
if ty_negated().is_subtype_of(self.db, *existing) {
let negated = ty_negated.get_or_insert_with(|| ty.negate(self.db));
if negated.is_subtype_of(self.db, *existing) {
// The type that includes both this new element, and its negation // The type that includes both this new element, and its negation
// (or a supertype of its negation), must be simply `object`. // (or a supertype of its negation), must be simply `object`.
self.collapse_to_object(); self.collapse_to_object();
@ -462,7 +462,6 @@ impl<'db> UnionBuilder<'db> {
Type::IntLiteral(literal) => { Type::IntLiteral(literal) => {
let mut found = None; let mut found = None;
let mut to_remove = None; let mut to_remove = None;
let mut ty_negated = None;
for (index, element) in self.elements.iter_mut().enumerate() { for (index, element) in self.elements.iter_mut().enumerate() {
match element { match element {
UnionElement::IntLiterals(literals) => { UnionElement::IntLiterals(literals) => {
@ -478,13 +477,13 @@ impl<'db> UnionBuilder<'db> {
if ty.is_subtype_of(self.db, *existing) { if ty.is_subtype_of(self.db, *existing) {
return; return;
} }
// e.g. `existing` could be `Literal[1] & Any`,
// and `ty` could be `Literal[1]`
if existing.is_subtype_of(self.db, ty) { if existing.is_subtype_of(self.db, ty) {
to_remove = Some(index); to_remove = Some(index);
continue; continue;
} }
if ty_negated().is_subtype_of(self.db, *existing) {
let negated = ty_negated.get_or_insert_with(|| ty.negate(self.db));
if negated.is_subtype_of(self.db, *existing) {
// The type that includes both this new element, and its negation // The type that includes both this new element, and its negation
// (or a supertype of its negation), must be simply `object`. // (or a supertype of its negation), must be simply `object`.
self.collapse_to_object(); self.collapse_to_object();