Skip to content
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

[red-knot] Treat empty intersection as 'object', fix intersection simplification #13880

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Narrowing for nested conditionals

## Multiple negative contributions

```py
def int_instance() -> int: ...

Expand All @@ -11,3 +13,14 @@ if x != 1:
if x != 3:
reveal_type(x) # revealed: int & ~Literal[1] & ~Literal[2] & ~Literal[3]
```

## Multiple negative contributions with simplification

```py
x = 1 if flag1 else 2 if flag2 else 3

if x != 1:
reveal_type(x) # revealed: Literal[2, 3]
if x != 2:
reveal_type(x) # revealed: Literal[3]
Comment on lines +22 to +25
Copy link
Contributor Author

@sharkdp sharkdp Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the fix in this PR, I would previously get

Suggested change
if x != 1:
reveal_type(x) # revealed: Literal[2, 3]
if x != 2:
reveal_type(x) # revealed: Literal[3]
if x != 1:
reveal_type(x) # revealed: Literal[2, 3]
if x != 2:
reveal_type(x) # revealed: ~Literal[2] | Literal[3]

Note how the bug only shows up if you add more than one constraint.

```
22 changes: 11 additions & 11 deletions crates/red_knot_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,18 @@ fn bindings_ty<'db>(
binding,
constraints,
}| {
let mut constraint_tys =
constraints.filter_map(|constraint| narrowing_constraint(db, constraint, binding));
let mut constraint_tys = constraints
.filter_map(|constraint| narrowing_constraint(db, constraint, binding))
.peekable();

let binding_ty = binding_ty(db, binding);
if let Some(first_constraint_ty) = constraint_tys.next() {
let mut builder = IntersectionBuilder::new(db);
builder = builder
.add_positive(binding_ty)
.add_positive(first_constraint_ty);
for constraint_ty in constraint_tys {
builder = builder.add_positive(constraint_ty);
}
builder.build()
if constraint_tys.peek().is_some() {
constraint_tys
.fold(
IntersectionBuilder::new(db).add_positive(binding_ty),
IntersectionBuilder::add_positive,
)
.build()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just an unrelated code improvement. No functional change. Let me know if you prefer the old version.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the new version is nicer, thank you. Don't know if there is any performance difference, but I'm comfortable leaving that question to experimentation if/when this code is identified as a hot spot in profiling.

} else {
binding_ty
}
Expand Down
14 changes: 13 additions & 1 deletion crates/red_knot_python_semantic/src/types/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ impl<'db> InnerIntersectionBuilder<'db> {
// A & B = Never if A and B are disjoint
if new_positive.is_disjoint_from(db, *existing_positive) {
*self = Self::new();
self.positive.insert(Type::Never);
return;
}
}
Expand All @@ -265,6 +266,7 @@ impl<'db> InnerIntersectionBuilder<'db> {
// S & ~T = Never if S <: T
if new_positive.is_subtype_of(db, *existing_negative) {
*self = Self::new();
self.positive.insert(Type::Never);
return;
}
// A & ~B = A if A and B are disjoint
Expand Down Expand Up @@ -328,6 +330,7 @@ impl<'db> InnerIntersectionBuilder<'db> {
// S & ~T = Never if S <: T
if existing_positive.is_subtype_of(db, new_negative) {
*self = Self::new();
self.positive.insert(Type::Never);
return;
}
// A & ~B = A if A and B are disjoint
Expand All @@ -351,7 +354,7 @@ impl<'db> InnerIntersectionBuilder<'db> {
fn build(mut self, db: &'db dyn Db) -> Type<'db> {
self.simplify_unbound();
match (self.positive.len(), self.negative.len()) {
(0, 0) => Type::Never,
(0, 0) => KnownClass::Object.to_instance(db),
(1, 0) => self.positive[0],
_ => {
self.positive.shrink_to_fit();
Expand Down Expand Up @@ -523,6 +526,15 @@ mod tests {
assert_eq!(intersection.neg_vec(&db), &[t0]);
}

#[test]
fn build_intersection_empty_intersection_equals_object() {
let db = setup_db();

let ty = IntersectionBuilder::new(&db).build();

assert_eq!(ty, KnownClass::Object.to_instance(&db));
}

#[test]
fn build_intersection_flatten_positive() {
let db = setup_db();
Expand Down
Loading