mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-03 15:15:33 +00:00
[red-knot] Fix more edge cases for intersection simplification with LiteralString
and AlwaysTruthy
/AlwaysFalsy
(#15496)
This commit is contained in:
parent
8712438aec
commit
55a7f72035
2 changed files with 130 additions and 109 deletions
|
@ -680,7 +680,7 @@ simplified, due to the fact that a `LiteralString` inhabitant is known to have `
|
||||||
exactly `str` (and not a subclass of `str`):
|
exactly `str` (and not a subclass of `str`):
|
||||||
|
|
||||||
```py
|
```py
|
||||||
from knot_extensions import Intersection, Not, AlwaysTruthy, AlwaysFalsy
|
from knot_extensions import Intersection, Not, AlwaysTruthy, AlwaysFalsy, Unknown
|
||||||
from typing_extensions import LiteralString
|
from typing_extensions import LiteralString
|
||||||
|
|
||||||
def f(
|
def f(
|
||||||
|
@ -690,6 +690,10 @@ def f(
|
||||||
d: Intersection[LiteralString, Not[AlwaysFalsy]],
|
d: Intersection[LiteralString, Not[AlwaysFalsy]],
|
||||||
e: Intersection[AlwaysFalsy, LiteralString],
|
e: Intersection[AlwaysFalsy, LiteralString],
|
||||||
f: Intersection[Not[AlwaysTruthy], LiteralString],
|
f: Intersection[Not[AlwaysTruthy], LiteralString],
|
||||||
|
g: Intersection[AlwaysTruthy, LiteralString],
|
||||||
|
h: Intersection[Not[AlwaysFalsy], LiteralString],
|
||||||
|
i: Intersection[Unknown, LiteralString, AlwaysFalsy],
|
||||||
|
j: Intersection[Not[AlwaysTruthy], Unknown, LiteralString],
|
||||||
):
|
):
|
||||||
reveal_type(a) # revealed: LiteralString & ~Literal[""]
|
reveal_type(a) # revealed: LiteralString & ~Literal[""]
|
||||||
reveal_type(b) # revealed: Literal[""]
|
reveal_type(b) # revealed: Literal[""]
|
||||||
|
@ -697,6 +701,10 @@ def f(
|
||||||
reveal_type(d) # revealed: LiteralString & ~Literal[""]
|
reveal_type(d) # revealed: LiteralString & ~Literal[""]
|
||||||
reveal_type(e) # revealed: Literal[""]
|
reveal_type(e) # revealed: Literal[""]
|
||||||
reveal_type(f) # revealed: Literal[""]
|
reveal_type(f) # revealed: Literal[""]
|
||||||
|
reveal_type(g) # revealed: LiteralString & ~Literal[""]
|
||||||
|
reveal_type(h) # revealed: LiteralString & ~Literal[""]
|
||||||
|
reveal_type(i) # revealed: Unknown & Literal[""]
|
||||||
|
reveal_type(j) # revealed: Unknown & Literal[""]
|
||||||
```
|
```
|
||||||
|
|
||||||
## Addition of a type to an intersection with many non-disjoint types
|
## Addition of a type to an intersection with many non-disjoint types
|
||||||
|
|
|
@ -247,19 +247,43 @@ struct InnerIntersectionBuilder<'db> {
|
||||||
impl<'db> InnerIntersectionBuilder<'db> {
|
impl<'db> InnerIntersectionBuilder<'db> {
|
||||||
/// Adds a positive type to this intersection.
|
/// Adds a positive type to this intersection.
|
||||||
fn add_positive(&mut self, db: &'db dyn Db, mut new_positive: Type<'db>) {
|
fn add_positive(&mut self, db: &'db dyn Db, mut new_positive: Type<'db>) {
|
||||||
if new_positive == Type::AlwaysTruthy && self.positive.contains(&Type::LiteralString) {
|
match new_positive {
|
||||||
|
// `LiteralString & AlwaysTruthy` -> `LiteralString & ~Literal[""]`
|
||||||
|
Type::AlwaysTruthy if self.positive.contains(&Type::LiteralString) => {
|
||||||
self.add_negative(db, Type::string_literal(db, ""));
|
self.add_negative(db, Type::string_literal(db, ""));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
// `LiteralString & AlwaysFalsy` -> `Literal[""]`
|
||||||
if let Type::Intersection(other) = new_positive {
|
Type::AlwaysFalsy if self.positive.swap_remove(&Type::LiteralString) => {
|
||||||
|
self.add_positive(db, Type::string_literal(db, ""));
|
||||||
|
}
|
||||||
|
// `AlwaysTruthy & LiteralString` -> `LiteralString & ~Literal[""]`
|
||||||
|
Type::LiteralString if self.positive.swap_remove(&Type::AlwaysTruthy) => {
|
||||||
|
self.add_positive(db, Type::LiteralString);
|
||||||
|
self.add_negative(db, Type::string_literal(db, ""));
|
||||||
|
}
|
||||||
|
// `AlwaysFalsy & LiteralString` -> `Literal[""]`
|
||||||
|
Type::LiteralString if self.positive.swap_remove(&Type::AlwaysFalsy) => {
|
||||||
|
self.add_positive(db, Type::string_literal(db, ""));
|
||||||
|
}
|
||||||
|
// `LiteralString & ~AlwaysTruthy` -> `LiteralString & AlwaysFalsy` -> `Literal[""]`
|
||||||
|
Type::LiteralString if self.negative.swap_remove(&Type::AlwaysTruthy) => {
|
||||||
|
self.add_positive(db, Type::string_literal(db, ""));
|
||||||
|
}
|
||||||
|
// `LiteralString & ~AlwaysFalsy` -> `LiteralString & ~Literal[""]`
|
||||||
|
Type::LiteralString if self.negative.swap_remove(&Type::AlwaysFalsy) => {
|
||||||
|
self.add_positive(db, Type::LiteralString);
|
||||||
|
self.add_negative(db, Type::string_literal(db, ""));
|
||||||
|
}
|
||||||
|
// `(A & B & ~C) & (D & E & ~F)` -> `A & B & D & E & ~C & ~F`
|
||||||
|
Type::Intersection(other) => {
|
||||||
for pos in other.positive(db) {
|
for pos in other.positive(db) {
|
||||||
self.add_positive(db, *pos);
|
self.add_positive(db, *pos);
|
||||||
}
|
}
|
||||||
for neg in other.negative(db) {
|
for neg in other.negative(db) {
|
||||||
self.add_negative(db, *neg);
|
self.add_negative(db, *neg);
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
_ => {
|
||||||
let addition_is_bool_instance = new_positive
|
let addition_is_bool_instance = new_positive
|
||||||
.into_instance()
|
.into_instance()
|
||||||
.and_then(|instance| instance.class.known(db))
|
.and_then(|instance| instance.class.known(db))
|
||||||
|
@ -275,10 +299,6 @@ impl<'db> InnerIntersectionBuilder<'db> {
|
||||||
Type::AlwaysFalsy if addition_is_bool_instance => {
|
Type::AlwaysFalsy if addition_is_bool_instance => {
|
||||||
new_positive = Type::BooleanLiteral(false);
|
new_positive = Type::BooleanLiteral(false);
|
||||||
}
|
}
|
||||||
// `AlwaysFalsy & LiteralString` -> `Literal[""]`
|
|
||||||
Type::AlwaysFalsy if new_positive.is_literal_string() => {
|
|
||||||
new_positive = Type::string_literal(db, "");
|
|
||||||
}
|
|
||||||
Type::Instance(InstanceType { class })
|
Type::Instance(InstanceType { class })
|
||||||
if class.is_known(db, KnownClass::Bool) =>
|
if class.is_known(db, KnownClass::Bool) =>
|
||||||
{
|
{
|
||||||
|
@ -294,10 +314,6 @@ impl<'db> InnerIntersectionBuilder<'db> {
|
||||||
_ => continue,
|
_ => continue,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// `LiteralString & AlwaysFalsy` -> `Literal[""]`
|
|
||||||
Type::LiteralString if new_positive == Type::AlwaysFalsy => {
|
|
||||||
new_positive = Type::string_literal(db, "");
|
|
||||||
}
|
|
||||||
_ => continue,
|
_ => continue,
|
||||||
}
|
}
|
||||||
self.positive.swap_remove_index(index);
|
self.positive.swap_remove_index(index);
|
||||||
|
@ -325,10 +341,6 @@ impl<'db> InnerIntersectionBuilder<'db> {
|
||||||
self.negative.swap_remove_index(index);
|
self.negative.swap_remove_index(index);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else if new_positive.is_literal_string() {
|
|
||||||
if self.negative.swap_remove(&Type::AlwaysTruthy) {
|
|
||||||
new_positive = Type::string_literal(db, "");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut to_remove = SmallVec::<[usize; 1]>::new();
|
let mut to_remove = SmallVec::<[usize; 1]>::new();
|
||||||
|
@ -374,6 +386,7 @@ impl<'db> InnerIntersectionBuilder<'db> {
|
||||||
self.positive.insert(new_positive);
|
self.positive.insert(new_positive);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Adds a negative type to this intersection.
|
/// Adds a negative type to this intersection.
|
||||||
fn add_negative(&mut self, db: &'db dyn Db, new_negative: Type<'db>) {
|
fn add_negative(&mut self, db: &'db dyn Db, new_negative: Type<'db>) {
|
||||||
|
@ -438,8 +451,8 @@ impl<'db> InnerIntersectionBuilder<'db> {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for index in to_remove.iter().rev() {
|
for index in to_remove.into_iter().rev() {
|
||||||
self.negative.swap_remove_index(*index);
|
self.negative.swap_remove_index(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
for existing_positive in &self.positive {
|
for existing_positive in &self.positive {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue